{"id":13805556,"url":"https://github.com/erdetn/vi2c","last_synced_at":"2026-01-16T15:11:02.533Z","repository":{"id":182414419,"uuid":"408624394","full_name":"erdetn/vi2c","owner":"erdetn","description":"A tiny I2C library written in V","archived":false,"fork":false,"pushed_at":"2024-05-15T20:40:41.000Z","size":3320,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-18T21:48:02.728Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"V","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erdetn.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}},"created_at":"2021-09-20T23:00:05.000Z","updated_at":"2024-11-09T21:55:07.000Z","dependencies_parsed_at":"2024-08-04T01:15:03.846Z","dependency_job_id":null,"html_url":"https://github.com/erdetn/vi2c","commit_stats":null,"previous_names":["erdetn/vi2c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdetn%2Fvi2c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdetn%2Fvi2c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdetn%2Fvi2c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erdetn%2Fvi2c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erdetn","download_url":"https://codeload.github.com/erdetn/vi2c/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254012968,"owners_count":21999346,"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","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":[],"created_at":"2024-08-04T01:01:02.304Z","updated_at":"2026-01-16T15:11:02.510Z","avatar_url":"https://github.com/erdetn.png","language":"V","funding_links":[],"categories":["Libraries"],"sub_categories":["Serial Communications"],"readme":"# vi2c\n`vi2c` is a tiny I2C communication library written in V.\n\n# Documentation\n\n```struct I2CDevice```\n\nRepresents an I2C device.\nFields:\n\n* `m_fd (int)`: File descriptor (-1 if not open).\n* `m_name (string)`: Name of the device.\n* `m_device_address (u8)`: I2C device address.\n* `m_device_filename (string)`: File path of the I2C device.\n* `m_is_connected (bool)`: Indicates if the device is connected.\n* `m_is_forced (bool)`: Indicates if the connection was forced.\n* `m_is_10bit (bool)`: `true` if 10 bit address, otherwise 7 bit address.\n\n## Functions\n\n#### Function `new(filename string, address u8, name string, is_10bit bool) I2CDevice`\n\nCreates a new I2CDevice instance.\n\n* `filename (string)`: File path of the I2C device.\n* `address (u8)`: I2C device address.\n* `name (string)`: Name of the device.\n* `is_10bit (bool)`: `true` if 10 bit address, otherwise 7 bit address.\n\n____\n#### Function `connect(force_connection bool) bool`\n\nConnects to the I2C device.\n\n* `force_connection (bool)`: Indicates if the connection should be forced. Force (or not) using this slave address, even if it is already in use by a driver.\n\nReturns `true` if the connection is successful, otherwise `false`.\n____\n#### Function `read_data(max_length int) (int, []u8)`\n\nReads data from the I2C device.\n\n* `max_length (int)`: Maximum length of data to read.\n\nReturns the number of bytes read and the data as a byte slice.\n____\n#### Function `write_data(data []u8) u32`\n\nWrites data to the I2C device.\n\n* `data ([]u8)`: Data to write.\n\nReturns the number of bytes written.\n____\n#### Function `read_data_from_reg(reg u8, max_length int) (int, []u8)`\n\nReads data from a register of the I2C device.\n\n* `reg (u8)`: Register address to read from.\n* `max_length (int)`: Maximum length of data to read.\n  \nReturns the number of bytes read and the data as a byte slice.\n____\n#### Function `read_reg(reg u8) (int, []u8)`\n\nReads a register from the I2C device.\n* `reg (u8)`: Register address to read.\n\nReturns the value read from the register as a byte slice.\n____\n#### Function `write_reg_data(reg u8, data []u8) u32`\n\nWrites data to a register of the I2C device.\n* `reg (u8)`: Register address to write to.\n* `data ([]u8)`: Data to write.\n\nReturns the number of bytes written.\n____\n#### Function `write_reg(reg u8, value u8) u32`\n\nWrites a value to a register of the I2C device.\n* `reg (u8)`: Register address to write to.\n* `value (u8)`: Value to write.\n\nReturns the number of bytes written.\n____\n#### Function `disconnect()`\nDisconnects from the I2C device.\n____\n#### Function `is_forced() bool`\nChecks if the connection to the I2C device was forced.\nReturns `true` if the connection was forced, otherwise `false`.\n____\n#### Functon `is_connected() bool`\nChecks if the connection to the I2C device is established.\nReturns `true` if connected, otherwise `false`.\n____\n#### Function `name() string`\nGets the name of the I2C device.\nReturns the name of the device.\n____\n#### Function `filename() string`\nGets the file path of the I2C device.\nReturns the file path of the device.\n____\n#### Function `address() u8`\nGets the address of the I2C device.\nReturns the address of the device.\n____\n#### Function `fd() int`\nGets the file descriptor of the I2C device.\nReturns the file descriptor.\n____\n\n#### Function `set_retries(retries int) bool`\n\nSets the number of retries for I2C communication.\n* `retries (int)`: The number of retries to set.\n\nReturns `(bool)`: true if the retries were successfully set, otherwise `false`.\n\n----\n#### Function `set_timeout(timeout_ms int) bool`\nSets the timeout for I2C communication.\n* `timeout_ms (int)`: Timeout value in milliseconds.\n\nReturns `(bool)`: `true` if the timeout was successfully set, otherwise `false`.\n\n----\n#### Function `is_10bit() bool`\n\nChecks if the I2C device address is 10-bit.\nReturns `(bool)`: true if the I2C device address is 10-bit, otherwise `false`.\n\n---- \n#### Function `str() string`\nReturns a formatted string representing the I2C device.\n____\n\n## Example\n\n```v\nmodule main\n\nimport time\nimport vi2c\n\nfn main() {\n\tmut ic2_dev := vi2c.new('/dev/i2c-9', 0x48, 'Temp sensor')\n\n\tprintln(ic2_dev)\n\n\tif !ic2_dev.connect(true) {\n\t\tprintln('Failed to connect')\n\t\treturn\n\t}\n\n\tprintln(ic2_dev)\n\n\tfor _ in 1 .. 100 {\n\t\tlen, data := ic2_dev.read_data_from_reg(~0, 2)\n\n\t\tif len == 2 {\n\t\t\tval := 0.00390625 * f32(u32(data[0]) \u003c\u003c 8 | u32(data[1]))\n\t\t\tprintln('data: \u003c${data.hex()}\u003e temp: ${val} *C')\n\t\t}\n\t\ttime.sleep(1000000000)\n\t}\n\n\tic2_dev.disconnect()\n}\n\n```\n\n## Compile\n\n```sh\ncd ~/.vmodules\ncd vi2c/playground/\n```\n\nTo compile it for Linux host machine (x64/x86-64), make sure to specify the `include` path:\n```sh\nv -cflags '-I /usr/include/' . -o test_i2c_comm_x64\n```\n\nTo cross-compile it for **Aarch64**, make sure that `aarch64-linux-gnu-gcc` and corresponding libraries are installed. \nSet `aarch64-linux-gnu-gcc` as `-cc` compiler, disable and add the `include` of **Aarch64** - in this case `/usr/aarch64-linux-gnu/include/`.\n```sh\nv -cc aarch64-linux-gnu-gcc -gc none -cflags '--static -I /usr/aarch64-linux-gnu/include/' test_i2c_comm.v -o test_i2c_comm_aa64\n\n```\n\n\tNOTE: Please note that the library and the example code are tested in my Ubuntu 20.4 and an Aarch64 machine.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferdetn%2Fvi2c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferdetn%2Fvi2c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferdetn%2Fvi2c/lists"}