-
Notifications
You must be signed in to change notification settings - Fork 111
/
aht20.ts
82 lines (73 loc) · 2.38 KB
/
aht20.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
import * as ds from "@devicescript/core"
import { DriverError } from "./core"
import { startTemperatureHumidity } from "./servers"
import { I2CSensorDriver } from "./driver"
import { sleep } from "@devicescript/core"
const AHT20_ADDRESS = 0x38
const AHT20_BUSY = 0x80
const AHT20_OK = 0x08
const AHT20_READ_THROTTLE = 500
class AHT20Driver extends I2CSensorDriver<{
humidity: number
temperature: number
}> {
constructor() {
super(AHT20_ADDRESS, { readCacheTime: AHT20_READ_THROTTLE })
}
private async status() {
return (await this.readBuf(1))[0]
}
private async waitBusy() {
while ((await this.status()) & AHT20_BUSY) await ds.sleep(10)
}
override async initDriver() {
await this.writeBuf(hex`BA`) // reset
await sleep(20)
await this.writeBuf(hex`E10800`) // calibrate
await this.waitBusy()
if (!((await this.status()) & AHT20_OK))
throw new DriverError("can't init AHT20")
}
override async readData() {
await this.writeBuf(hex`AC3300`)
await this.waitBusy()
const data = await this.readBuf(6)
const h0 = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4)
const humidity = h0 * (100 / 0x100000)
const t0 = ((data[3] & 0xf) << 16) | (data[4] << 8) | data[5]
const temperature = t0 * (200.0 / 0x100000) - 50
return { humidity, temperature }
}
}
/**
* Start driver for AHT20 temperature/humidity sensor at I2C address `0x38`.
* @devsPart AHT20
* @devsServices temperature, humidity
* @see {@link https://asairsensors.com/wp-content/uploads/2021/09/Data-Sheet-AHT20-Humidity-and-Temperature-Sensor-ASAIR-V1.0.03.pdf | Datasheet}.
* @throws DriverError
*/
export async function startAHT20(options?: {
temperatureName?: string
humidityName?: string
baseName?: string
}) {
const { temperatureName, humidityName, baseName } = options || {}
const driver = new AHT20Driver()
await driver.init()
return startTemperatureHumidity(
{
min: -40,
max: 85,
errorValue: 1.5,
reading: async () => (await driver.read()).temperature,
name: temperatureName,
baseName,
},
{
errorValue: 4,
reading: async () => (await driver.read()).humidity,
name: humidityName,
baseName,
}
)
}