summaryrefslogtreecommitdiff
path: root/ccs811.lua
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2024-01-17 20:10:47 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2024-01-17 20:10:47 +0100
commit99a22379fe64903029f2a8a39a16ea4df146ffb7 (patch)
tree88b5759642f50bdba07475b4f49f675a474d937d /ccs811.lua
Initial commit
Diffstat (limited to 'ccs811.lua')
-rw-r--r--ccs811.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/ccs811.lua b/ccs811.lua
new file mode 100644
index 0000000..8753303
--- /dev/null
+++ b/ccs811.lua
@@ -0,0 +1,66 @@
+local ccs811 = {}
+local device_address = 0x5a
+
+ccs811.mode = nil
+
+function ccs811.start()
+ local ret = true
+ i2c.start(0)
+ if not i2c.address(0, device_address, i2c.TRANSMITTER) then
+ ret = false
+ end
+ i2c.write(0, 0xf4)
+ i2c.stop(0)
+ return ret
+end
+
+function ccs811.setMode(mode)
+ local ret = true
+ i2c.start(0)
+ if not i2c.address(0, device_address, i2c.TRANSMITTER) then
+ ret = false
+ end
+ i2c.write(0, 0x01, bit.lshift(mode, 4))
+ i2c.stop(0)
+ ccs811.mode = mode
+ return ret
+end
+
+function ccs811.setEnv(humi, temp)
+ local ret = true
+ i2c.start(0)
+ if not i2c.address(0, device_address, i2c.TRANSMITTER) then
+ ret = false
+ end
+ i2c.write(0, 0x05, math.floor(humi) * 2, 0, (math.floor(temp) + 25) * 2, 0)
+ i2c.stop(0)
+ return ret
+end
+
+function ccs811.read()
+ local ret = true
+ if ccs811.mode == nil then
+ ccs811.setMode(1)
+ return
+ end
+ i2c.start(0)
+ if not i2c.address(0, device_address, i2c.TRANSMITTER) then
+ ret = false
+ end
+ i2c.write(0, 0x02)
+ i2c.start(0)
+ if not i2c.address(0, device_address, i2c.RECEIVER) then
+ ret = false
+ end
+ local data = i2c.read(0, 6)
+ i2c.stop(0)
+
+ ccs811.eco2 = bit.lshift(string.byte(data, 1), 8) + string.byte(data, 2)
+ ccs811.tvoc = bit.lshift(string.byte(data, 3), 8) + string.byte(data, 4)
+ ccs811.status = string.byte(data, 5)
+ ccs811.err_id = string.byte(data, 6)
+
+ return ret
+end
+
+return ccs811