-
Notifications
You must be signed in to change notification settings - Fork 6
/
airQuality.ts
137 lines (111 loc) · 3.58 KB
/
airQuality.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Mary West @ SparkFun Electronics
* Ryan Mortenson https://github.com/ryanjmortenson
* August 25, 2017
* https://github.com/ADataDate/pxt-CCS811
* Development environment specifics:
* Written in Microsoft Makecode
* Tested with a SparkFun weather:bit, CCS811 sensor for micro:bit
*
* This code is released under the [MIT License](http://opensource.org/licenses/MIT).
* Please review the LICENSE.md file included with this example. If you have any questions
* or concerns with licensing, please contact techsupport@sparkfun.com.
* Distributed as-is; no warranty is given.
*/
/**
* Functions to operate the CCS811
*/
//% color=#33acff icon="\u27BE"
namespace airQuality {
//Keep track of CCS811 Start
let appStarted = false;
//CCS811 Addresses
const ccsAddr = 0x5B
const ccsStatus = 0x00
const ccsMeas = 0x01
const ccsAlg = 0x02
const ccsRaw = 0x03
const ccsEnv = 0x05
const ccsNtc = 0x06
const ccsThres = 0x10
const ccsBase = 0x11
const ccsHi = 0x20
const ccsHv = 0x21
const ccsBoot = 0x23
const ccsAppv = 0x24
const ccsErr = 0xE0
const ccsApps = 0xF4
const ccsReset = 0xFF
/**
* Easy test for ensuring I2C read is working
*/
//% weight=1 blockId="hardwareID" block="HWID"
export function hardwareID(): number {
let hardwareId = readCCSReg(0x20, NumberFormat.UInt8LE)
return hardwareId
}
/**
* Gets the CO2 data from the algorithm register
* of the CCS811 Air Quality Sensor
*/
//% weight=100 blockId="readCo2" block="Read eCO2"
export function readCo2(): number {
//read Algorithm Results register
let algRes = readCCSReg(ccsAlg, NumberFormat.UInt16BE)
return algRes
}
/**
* Gets the TVOC data from the algorithm register
* of the CCS811 Air Quality Sensor
*/
//% weight=90 blockId="readTvoc" block="Read TVOCs"
export function readTvoc(): number {
//read Algorithm Results register
let algRes = readCCSReg(ccsAlg, NumberFormat.Int32BE)
let Tvoc = (algRes & 0x0000FFFF)
return Tvoc
}
//% weight=2 blockId="readStatus" block="Device Status"
export function readStatus(): number {
//Return status of Device
let status = readCCSReg(ccsStatus, NumberFormat.UInt8LE)
return status
}
/**
* Read the device error code if there are
* any problems with device
*/
//% weight=3 blockId="readError" block="Device Error"
export function readError(): number {
//Return Error of Device
let error = readCCSReg(ccsErr, NumberFormat.Int8LE)
return error
}
/**
* Writes a value to a register on the CCS811 Air Quality Sensor
*/
function writeCCSReg(reg: number, val: number): void {
let test = reg << 8 | val
pins.i2cWriteNumber(ccsAddr, reg << 8 | val, NumberFormat.Int16BE)
}
/**
* Reads a value from a register on the CCS811 Air Quality Sensor
*/
function readCCSReg(reg: number, format: NumberFormat) {
pins.i2cWriteNumber(ccsAddr, reg, NumberFormat.UInt8LE, false)
let val = pins.i2cReadNumber(ccsAddr, format, false)
return val
}
/**
* Gets the CCS811 into app mode, and sets the measure register
* to pull data into Algorithm register every second.
*/
//% weight=100 blockId="AppStart" block="CCS811 Start"
export function appStart(): void {
if (appStarted) return;
pins.i2cWriteNumber(ccsAddr, ccsApps, NumberFormat.Int8LE)
writeCCSReg(ccsMeas, 0x10)
//init once
appStarted = true;
}
}