diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2024-01-27 14:25:06 +0100 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2024-01-27 14:25:06 +0100 |
commit | a8b82a0a8ad4894bc6c65453a89b37a9a0c8ec1d (patch) | |
tree | 98b2f73acd8ad05721802ec4cfeac98f255327d0 | |
parent | a74b1e75fd9a585e3fc0d087661b413058425990 (diff) |
Hard-code I²C Bus ID. It's not like it's gonna change anytime soon.
-rw-r--r-- | init.lua | 2 | ||||
-rw-r--r-- | sen5x.lua | 65 |
2 files changed, 33 insertions, 34 deletions
@@ -17,7 +17,7 @@ gpio.mode(ledpin, gpio.OUTPUT) gpio.write(ledpin, 0) sen5x = require("sen5x") -i2c.setup(sen5x.bus_id, 2, 1, i2c.SLOW) +i2c.setup(0, 2, 1, i2c.SLOW) function log_restart() print("Network error " .. wifi.sta.status()) @@ -1,26 +1,25 @@ local sen5x = {} local device_address = 0x69 -sen5x.bus_id = 0 sen5x.status = "Initializing" function sen5x.start() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.TRANSMITTER) then return false end - i2c.write(sen5x.bus_id, {0x00, 0x21}) - i2c.stop(sen5x.bus_id) + i2c.write(0, {0x00, 0x21}) + i2c.stop(0) return true end function sen5x.stop() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.TRANSMITTER) then return false end - i2c.write(sen5x.bus_id, {0x01, 0x04}) - i2c.stop(sen5x.bus_id) + i2c.write(0, {0x01, 0x04}) + i2c.stop(0) return true end @@ -33,22 +32,22 @@ function sen5x.read_value(data, index) end function sen5x.prepare_read() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.TRANSMITTER) then return false end - i2c.write(sen5x.bus_id, {0x03, 0xc4}) - i2c.stop(sen5x.bus_id) + i2c.write(0, {0x03, 0xc4}) + i2c.stop(0) return true end function sen5x.read() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.RECEIVER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.RECEIVER) then return false end - local data = i2c.read(sen5x.bus_id, 24) - i2c.stop(sen5x.bus_id) + local data = i2c.read(0, 24) + i2c.stop(0) if not sen5x.crc_valid(data, 24) then return false end @@ -64,22 +63,22 @@ function sen5x.read() end function sen5x.prepare_read_status() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.TRANSMITTER) then return false end - i2c.write(sen5x.bus_id, {0xd2, 0x06}) - i2c.stop(sen5x.bus_id) + i2c.write(0, {0xd2, 0x06}) + i2c.stop(0) return true end function sen5x.read_status() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.RECEIVER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.RECEIVER) then return false end - local data = i2c.read(sen5x.bus_id, 6) - i2c.stop(sen5x.bus_id) + local data = i2c.read(0, 6) + i2c.stop(0) if not sen5x.crc_valid(data, 6) then return false end @@ -105,22 +104,22 @@ function sen5x.read_status() end function sen5x.prepare_get_product() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.TRANSMITTER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.TRANSMITTER) then return false end - i2c.write(sen5x.bus_id, {0xd0, 0x14}) - i2c.stop(sen5x.bus_id) + i2c.write(0, {0xd0, 0x14}) + i2c.stop(0) return true end function sen5x.get_product() - i2c.start(sen5x.bus_id) - if not i2c.address(sen5x.bus_id, device_address, i2c.RECEIVER) then + i2c.start(0) + if not i2c.address(0, device_address, i2c.RECEIVER) then return nil end - local data = i2c.read(sen5x.bus_id, 48) - i2c.stop(sen5x.bus_id) + local data = i2c.read(0, 48) + i2c.stop(0) if not sen5x.crc_valid(data, 48) then return false end |