{"id":15009223,"url":"https://github.com/amaork/libi2c","last_synced_at":"2025-10-08T11:15:12.400Z","repository":{"id":17845261,"uuid":"20760967","full_name":"amaork/libi2c","owner":"amaork","description":"Linux i2c library,  support C/C++/Python","archived":false,"fork":false,"pushed_at":"2025-05-28T06:40:50.000Z","size":66,"stargazers_count":182,"open_issues_count":11,"forks_count":66,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-09-23T04:38:49.969Z","etag":null,"topics":["c","i2c","i2c-device","python","python27","python3","raspberry-pi"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amaork.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2014-06-12T09:00:27.000Z","updated_at":"2025-09-20T11:18:48.000Z","dependencies_parsed_at":"2024-09-24T19:23:56.177Z","dependency_job_id":"b74599d6-c5ef-4b31-aacf-7430a55b61ce","html_url":"https://github.com/amaork/libi2c","commit_stats":{"total_commits":28,"total_committers":4,"mean_commits":7.0,"dds":0.2857142857142857,"last_synced_commit":"152989513fd1bf63e4c2d21cad82a9e0106e68aa"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/amaork/libi2c","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaork%2Flibi2c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaork%2Flibi2c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaork%2Flibi2c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaork%2Flibi2c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amaork","download_url":"https://codeload.github.com/amaork/libi2c/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaork%2Flibi2c/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278931698,"owners_count":26070800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","i2c","i2c-device","python","python27","python3","raspberry-pi"],"created_at":"2024-09-24T19:23:45.096Z","updated_at":"2025-10-08T11:15:12.377Z","avatar_url":"https://github.com/amaork.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"libi2c\n=======\n\nLinux userspace i2c library.\n\n\n## Features\n\n- Support C/C++ and Python.\n\n- Support Python2+, Python3+\n\n- Support multiple bus and devices.\n\n- Support 7-bit and 10-bit i2c slave address.\n\n- Support 1 - 4 byte internal address, auto convert.\n\n- Provide read/write/ioctl functions to operate i2c device.\n\n- Support 8/16/32/64/128/256 bytes page aligned write, read/write length are unlimited.\n\n- Using ioctl functions operate i2c can ignore i2c device ack signal and internal address.\n\n\n## Installation\n\n\tpip install pylibi2c\n\n\tor\n\n\tsudo python setup.py install\n\n\tor\n\n\tsudo make install\n\n\tor\n\n\tsudo make install PYTHON=pythonX.X\n\n## Interface\n\n\ti2c_ioctl_write (once max 16 bytes) are more efficient than i2c_write (once max 4 bytes).\n\n\t/* Close i2c bus */\n\tvoid i2c_close(int bus);\n\n\t/* Open i2c bus, return i2c bus fd */\n\tint i2c_open(const char *bus_name);\n\n\t/* I2C file I/O read, write */\n\tssize_t i2c_read(const I2CDevice *device, unsigned int iaddr, void *buf, size_t len);\n\tssize_t i2c_write(const I2CDevice *device, unsigned int iaddr, const void *buf, size_t len);\n\n\t/* I2c ioctl read, write can set i2c flags */\n\tssize_t i2c_ioctl_read(const I2CDevice *device, unsigned int iaddr, void *buf, size_t len);\n\tssize_t i2c_ioctl_write(const I2CDevice *device, unsigned int iaddr, const void *buf, size_t len);\n\n## Data structure\n\n**C/C++**\n\n\ttypedef struct i2c_device {\n\t\tint bus;\t\t\t/* I2C Bus fd, return from i2c_open */\n\t\tunsigned short addr;\t\t/* I2C device(slave) address */\n\t\tunsigned char tenbit;\t\t/* I2C is 10 bit device address */\n\t\tunsigned char delay;\t\t/* I2C internal operate delay, unit millisecond */\n\t\tunsigned short flags;\t\t/* I2C i2c_ioctl_read/write flags */\n\t\tunsigned int page_bytes;    \t/* I2C max number of bytes per page, 1K/2K 8, 4K/8K/16K 16, 32K/64K 32 etc */\n\t\tunsigned int iaddr_bytes;\t/* I2C device internal(word) address bytes, such as: 24C04 1 byte, 24C64 2 bytes */\n\t}I2CDevice;\n\n**Python**\n\n\tI2CDevice object\n\tI2CDevice(bus, addr, tenbit=False, iaddr_bytes=1, page_bytes=8, delay=1, flags=0)\n\ttenbit, delay, flags, page_bytes, iaddr_bytes are attributes can setter/getter after init\n\n\trequired args: bus, addr.\n\toptional args: tenbit(defult False, 7-bit), delay(defualt 1ms), flags(defualt 0), iaddr_bytes(defualt 1 byte internal address), page_bytes(default 8 bytes per page).\n\n\n## C/C++ Usage\n\n**1. First call `i2c_open` open i2c bus.**\n\n\tint bus;\n\n\t/* Open i2c bus /dev/i2c-0 */\n\tif ((bus = i2c_open(\"/dev/i2c-0\")) == -1) {\n\n\t\t/* Error process */\n\t}\n\n**2. Second fill `I2CDevice` struct, prepare read or write.**\n\n\tI2CDevice device;\n\tmemset(\u0026device, 0, sizeof(device));\n\n\t/* 24C04 */\n\tdevice.bus = bus;\t/* Bus 0 */\n\tdevice.addr = 0x50;\t/* Slave address is 0x50, 7-bit */\n\tdevice.iaddr_bytes = 1;\t/* Device internal address is 1 byte */\n\tdevice.page_bytes = 16; /* Device are capable of 16 bytes per page */\n\n**3. Call `i2c_read/write` or `i2c_ioctl_read/write` read or write i2c device.**\n\n\tunsigned char buffer[256];\n\tssize_t size = sizeof(buffer);\n\tmemset(buffer, 0, sizeof(buffer));\n\n\t/* From i2c 0x0 address read 256 bytes data to buffer */\n\tif ((i2c_read(\u0026device, 0x0, buffer, size)) != size) {\n\n\t\t/* Error process */\n\t}\n\n**4. Close i2c bus `i2c_close(bus)`.**\n\n\ti2c_close(bus);\n\n## Python Usage\n\n\timport ctypes\n\timport pylibi2c\n\n\t# Open i2c device @/dev/i2c-0, addr 0x50.\n\ti2c = pylibi2c.I2CDevice('/dev/i2c-0', 0x50)\n\n\t# Open i2c device @/dev/i2c-0, addr 0x50, 16bits internal address\n\ti2c = pylibi2c.I2CDevice('/dev/i2c-0', 0x50, iaddr_bytes=2)\n\n\t# Set delay\n\ti2c.delay = 10\n\n\t# Set page_bytes\n\ti2c.page_bytes = 16\n\n\t# Set flags\n\ti2c.flags = pylibi2c.I2C_M_IGNORE_NAK\n\n\t# Python2\n\tbuf = bytes(bytearray(256))\n\n\t# Python3\n\tbuf = bytes(256)\n\n\t# Write data to i2c, buf must be read-only type\n\tsize = i2c.write(0x0, buf)\n\n\t# From i2c 0x0(internal address) read 256 bytes data, using ioctl_read.\n\tdata = i2c.ioctl_read(0x0, 256)\n\n## Notice\n\n1. If i2c device do not have internal address, please use `i2c_ioctl_read/write` function for read/write, set`'iaddr_bytes=0`.\n\n2. If want ignore i2c device nak signal, please use `i2c_ioctl_read/write` function, set I2CDevice.falgs as `I2C_M_IGNORE_NAK`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famaork%2Flibi2c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famaork%2Flibi2c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famaork%2Flibi2c/lists"}