Skip to content

Conversation

@rosaage
Copy link

@rosaage rosaage commented Oct 24, 2025

This is essentially just a wrapper for the exisiting C functions.
Writing is currently set to require PERM_MEMORY

I have tested this a bit, and it seems to work ok
The syntax is i2c.read(device_id, register, length) and i2c.write(device_id, register, data_table)
Reading returns a table of values, while writing returns a bool True on success or errors on a fail.
(Should this return a false and a string instead of error?)

Test script
print("Hello World!")

print(CONSOLE_TYPE)

-- device 3 is the mcu
-- 0x09 is volume slider
ui.echo(i2c.read(3, 0x09, 1)[1])

-- 0x0D is the system voltage
local volt = i2c.read(3, 0x0D, 1)
local voltf = volt[1] * 5 / 256.0
ui.echo(voltf)

-- 0x28 is Brightness of the WiFi/Power LED 
ui.echo("Reg: "..i2c.read(3, 0x28, 1)[1])
i2c.write(3, 0x28, {200})
ui.echo("Reg: "..i2c.read(3, 0x28, 1)[1])
i2c.write(3, 0x28, {150})
ui.echo("Reg: "..i2c.read(3, 0x28, 1)[1])
i2c.write(3, 0x28, {70})
ui.echo("Reg: "..i2c.read(3, 0x28, 1)[1])

-- 0x15-17 is not in use
local data = i2c.read(3, 0x15, 3)
ui.echo(string.format("Reg: %d, %d, %d", data[1], data[2], data[3]))
i2c.write(3, 0x15, {20, 30, 40})
data = i2c.read(3, 0x15, 3)
ui.echo(string.format("Reg: %d, %d, %d", data[1], data[2], data[3]))

Volume slider reads fine, voltage reads back 3.68 (seems ok), I don't see any LED stuff, but when reading back the same register I see the changes. The final test is reading and writing multiple registers, this also seems to work ok.

@ihaveamac
Copy link
Contributor

while writing returns a bool True on success or errors on a fail.

In this case I think it should not return anything, and raise an error if it fails. Or if there is a reason to return a specific value, then return that.

@MisterSheeple
Copy link

Since malicious or negligent i2c writes have the potential to cause bricks, I think an unlock mechanism that makes the user aware that the script is about to write to i2c would be pretty important to have.

@rosaage
Copy link
Author

rosaage commented Oct 25, 2025

Writing now returns nothing (nil) on a success or lua error on fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants