periph 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ADDR_SA0_LOW = 0x38;
|
|
4
|
+
const _ADDR_SA0_HIGH = 0x39;
|
|
5
|
+
|
|
6
|
+
const _CMD_MODE_SET = 0x40;
|
|
7
|
+
const _CMD_LOAD_PTR = 0x00;
|
|
8
|
+
const _CMD_DEVICE_SELECT = 0x60;
|
|
9
|
+
const _CMD_BANK_SELECT = 0x78;
|
|
10
|
+
const _CMD_BLINK_SELECT = 0x70;
|
|
11
|
+
|
|
12
|
+
const _MODE_1_4 = 0x00;
|
|
13
|
+
const _MODE_STATIC = 0x01;
|
|
14
|
+
const _MODE_1_2 = 0x02;
|
|
15
|
+
const _MODE_1_3 = 0x03;
|
|
16
|
+
|
|
17
|
+
const _BIAS_1_3 = 0x00;
|
|
18
|
+
const _BIAS_1_2 = 0x04;
|
|
19
|
+
|
|
20
|
+
const _DISPLAY_OFF = 0x00;
|
|
21
|
+
const _DISPLAY_ON = 0x08;
|
|
22
|
+
|
|
23
|
+
const _SEVEN_SEG = [
|
|
24
|
+
0xED, 0x60, 0xA7, 0xE3, 0x6A,
|
|
25
|
+
0xCB, 0xCF, 0xE0, 0xEF, 0xEB,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* PCF8576 40x4 universal LCD segment driver — minimal interface.
|
|
30
|
+
*
|
|
31
|
+
* Drives a single 7-segment LCD display (static or 1:4 multiplex) out of
|
|
32
|
+
* the box. The chip is write-only — the host never reads back. I2C address
|
|
33
|
+
* is 0x38 (SA0 = VSS) or 0x39 (SA0 = VDD).
|
|
34
|
+
*
|
|
35
|
+
* Default: 1:4 multiplex drive mode, 1/3 bias, display enabled, and a
|
|
36
|
+
* 7-segment digit lookup table for the default multiplex mode.
|
|
37
|
+
*
|
|
38
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
|
|
39
|
+
*/
|
|
40
|
+
class PCF8576Minimal {
|
|
41
|
+
constructor(connection) {
|
|
42
|
+
this._conn = connection;
|
|
43
|
+
this._backplanes = 4;
|
|
44
|
+
this._clear();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
_cmdMode(enable, bias, mode) {
|
|
48
|
+
return _CMD_MODE_SET | (enable ? _DISPLAY_ON : _DISPLAY_OFF) | bias | mode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async _sendCommands(cmds) {
|
|
52
|
+
const out = [];
|
|
53
|
+
for (let i = 0; i < cmds.length - 1; i++) {
|
|
54
|
+
out.push(0x80 | (cmds[i] & 0x7F));
|
|
55
|
+
}
|
|
56
|
+
out.push(cmds[cmds.length - 1] & 0x7F);
|
|
57
|
+
await this._conn.write(Buffer.from(out));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async _sendCommandsWithData(cmd, data) {
|
|
61
|
+
const out = [cmd & 0x7F, ...data];
|
|
62
|
+
await this._conn.write(Buffer.from(out));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async _clear() {
|
|
66
|
+
await this._sendCommands([this._cmdMode(true, _BIAS_1_3, _MODE_1_4)]);
|
|
67
|
+
const zeros = new Array(20).fill(0);
|
|
68
|
+
await this._sendCommandsWithData(_CMD_LOAD_PTR, zeros);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Zero all 40 columns of display RAM; all segments off.
|
|
73
|
+
* @returns {Promise<void>}
|
|
74
|
+
*/
|
|
75
|
+
async clear() {
|
|
76
|
+
await this._clear();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Set the data pointer to {@param address} and write raw data bytes.
|
|
81
|
+
*
|
|
82
|
+
* @param {number} address - RAM column address, 0-39.
|
|
83
|
+
* @param {Buffer|Uint8Array|number[]} data - Bytes to write to display RAM.
|
|
84
|
+
* @returns {Promise<void>}
|
|
85
|
+
*/
|
|
86
|
+
async writeRaw(address, data) {
|
|
87
|
+
if (address < 0 || address > 39) {
|
|
88
|
+
throw new RangeError('address must be in 0..39');
|
|
89
|
+
}
|
|
90
|
+
if (!data || data.length === 0) return;
|
|
91
|
+
await this._sendCommandsWithData(_CMD_LOAD_PTR | (address & 0x3F), Array.from(data));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Write one 7-segment byte at column {@param position} * 2.
|
|
96
|
+
*
|
|
97
|
+
* @param {number} position - Digit index, 0-19. Maps to RAM address position * 2.
|
|
98
|
+
* @param {number} segments - 7-segment byte (a/c/b/DP/f/e/g/d packed, MSB-first).
|
|
99
|
+
* Add 0x10 to set the decimal point.
|
|
100
|
+
* @returns {Promise<void>}
|
|
101
|
+
*/
|
|
102
|
+
async setDigit7seg(position, segments) {
|
|
103
|
+
if (position < 0 || position > 19) {
|
|
104
|
+
throw new RangeError('position must be in 0..19');
|
|
105
|
+
}
|
|
106
|
+
await this.writeRaw(position * 2, [segments & 0xFF]);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* PCF8576 full interface — extends PCF8576Minimal with drive mode, bias, and blink control.
|
|
112
|
+
*
|
|
113
|
+
* Adds the ability to switch drive modes (static, 1:2, 1:3, 1:4 multiplex),
|
|
114
|
+
* change bias (1:2 or 1/3), configure blinking, select RAM banks for
|
|
115
|
+
* static and 1:2 multiplex use, and change the device subaddress counter
|
|
116
|
+
* for cascaded displays.
|
|
117
|
+
*
|
|
118
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
|
|
119
|
+
*/
|
|
120
|
+
class PCF8576Full extends PCF8576Minimal {
|
|
121
|
+
static BLINK_OFF = 0;
|
|
122
|
+
static BLINK_2_HZ = 1;
|
|
123
|
+
static BLINK_1_HZ = 2;
|
|
124
|
+
static BLINK_0_5_HZ = 3;
|
|
125
|
+
|
|
126
|
+
static BIAS_1_3 = 0;
|
|
127
|
+
static BIAS_1_2 = 1;
|
|
128
|
+
|
|
129
|
+
static BACKPLANES_1 = 1;
|
|
130
|
+
static BACKPLANES_2 = 2;
|
|
131
|
+
static BACKPLANES_3 = 3;
|
|
132
|
+
static BACKPLANES_4 = 4;
|
|
133
|
+
|
|
134
|
+
static BANK_0 = 0;
|
|
135
|
+
static BANK_1 = 1;
|
|
136
|
+
|
|
137
|
+
constructor(connection) {
|
|
138
|
+
super(connection);
|
|
139
|
+
this._enabled = true;
|
|
140
|
+
this._bias = PCF8576Full.BIAS_1_3;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
_modeCode(backplanes) {
|
|
144
|
+
switch (backplanes) {
|
|
145
|
+
case PCF8576Full.BACKPLANES_1: return _MODE_STATIC;
|
|
146
|
+
case PCF8576Full.BACKPLANES_2: return _MODE_1_2;
|
|
147
|
+
case PCF8576Full.BACKPLANES_3: return _MODE_1_3;
|
|
148
|
+
case PCF8576Full.BACKPLANES_4: return _MODE_1_4;
|
|
149
|
+
default: return _MODE_1_4;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async _applyMode() {
|
|
154
|
+
const biasBits = (this._bias === PCF8576Full.BIAS_1_2) ? _BIAS_1_2 : _BIAS_1_3;
|
|
155
|
+
await this._sendCommands([this._cmdMode(this._enabled, biasBits, this._modeCode(this._backplanes))]);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Turn the display on (E = 1). RAM contents are preserved.
|
|
160
|
+
* @returns {Promise<void>}
|
|
161
|
+
*/
|
|
162
|
+
async enable() {
|
|
163
|
+
this._enabled = true;
|
|
164
|
+
await this._applyMode();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Blank the display output (E = 0). RAM contents are preserved.
|
|
169
|
+
* @returns {Promise<void>}
|
|
170
|
+
*/
|
|
171
|
+
async disable() {
|
|
172
|
+
this._enabled = false;
|
|
173
|
+
await this._applyMode();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Reconfigure drive mode and bias at runtime.
|
|
178
|
+
*
|
|
179
|
+
* @param {number} backplanes - Number of backplanes: 1 (static), 2 (1:2), 3 (1:3), 4 (1:4).
|
|
180
|
+
* @param {number} [bias=0] - 0 = 1/3 bias, 1 = 1/2 bias.
|
|
181
|
+
* @returns {Promise<void>}
|
|
182
|
+
*/
|
|
183
|
+
async setMode(backplanes, bias = 0) {
|
|
184
|
+
this._backplanes = backplanes;
|
|
185
|
+
this._bias = bias;
|
|
186
|
+
await this._applyMode();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Set the blink frequency.
|
|
191
|
+
*
|
|
192
|
+
* @param {number} frequency - 0 = off, 1 = ~2 Hz, 2 = ~1 Hz, 3 = ~0.5 Hz.
|
|
193
|
+
* @param {boolean} [alternateBank=false] - Alternate-RAM-bank blinking (static/1:2 only).
|
|
194
|
+
* @returns {Promise<void>}
|
|
195
|
+
*/
|
|
196
|
+
async setBlink(frequency, alternateBank = false) {
|
|
197
|
+
if (frequency < 0 || frequency > 3) {
|
|
198
|
+
throw new RangeError('frequency must be in 0..3');
|
|
199
|
+
}
|
|
200
|
+
const ab = alternateBank ? 0x04 : 0x00;
|
|
201
|
+
await this._sendCommands([_CMD_BLINK_SELECT | ab | (frequency & 0x03)]);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Select the active RAM bank.
|
|
206
|
+
*
|
|
207
|
+
* @param {number} inputBank - 0 (rows 0-1) or 1 (rows 2-3).
|
|
208
|
+
* @param {number} outputBank - 0 (rows 0-1) or 1 (rows 2-3).
|
|
209
|
+
* @returns {Promise<void>}
|
|
210
|
+
*/
|
|
211
|
+
async setBank(inputBank, outputBank) {
|
|
212
|
+
await this._sendCommands([_CMD_BANK_SELECT | ((inputBank & 1) << 1) | (outputBank & 1)]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Change the subaddress counter for cascaded displays.
|
|
217
|
+
*
|
|
218
|
+
* @param {number} subaddress - 0-7; must match the A0/A1/A2 pin state.
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
async deviceSelect(subaddress) {
|
|
222
|
+
if (subaddress < 0 || subaddress > 7) {
|
|
223
|
+
throw new RangeError('subaddress must be in 0..7');
|
|
224
|
+
}
|
|
225
|
+
await this._sendCommands([_CMD_DEVICE_SELECT | (subaddress & 0x07)]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
PCF8576Minimal.SEVEN_SEG = _SEVEN_SEG;
|
|
230
|
+
PCF8576Full.SEVEN_SEG = _SEVEN_SEG;
|
|
231
|
+
|
|
232
|
+
module.exports = { PCF8576Minimal, PCF8576Full };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_TRIGGER = Buffer.from([0xAC, 0x33, 0x00]);
|
|
4
|
+
const _CMD_SOFT_RESET = Buffer.from([0xBA]);
|
|
5
|
+
const _CMD_CAL_INIT_1 = Buffer.from([0x1B, 0x00, 0x00]);
|
|
6
|
+
const _CMD_CAL_INIT_2 = Buffer.from([0x1C, 0x00, 0x00]);
|
|
7
|
+
const _CMD_CAL_INIT_3 = Buffer.from([0x1E, 0x00, 0x00]);
|
|
8
|
+
|
|
9
|
+
const _STATUS_BUSY = 0x80;
|
|
10
|
+
const _STATUS_CAL = 0x08;
|
|
11
|
+
|
|
12
|
+
function _sleep(ms) {
|
|
13
|
+
const end = Date.now() + ms;
|
|
14
|
+
while (Date.now() < end) {}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* AHT21 temperature and humidity sensor — minimal interface.
|
|
19
|
+
*
|
|
20
|
+
* Provides temperature and humidity readings with no configuration beyond
|
|
21
|
+
* the connection. Handles power-on initialization, calibration check, and
|
|
22
|
+
* measurement triggering automatically.
|
|
23
|
+
*
|
|
24
|
+
* Default configuration (baked in at construction):
|
|
25
|
+
* - Measurement triggered on every read() call (no continuous mode)
|
|
26
|
+
* - 80 ms fixed wait after trigger (no busy-polling)
|
|
27
|
+
* - No CRC verification (reduces complexity; CRC check is Full-only)
|
|
28
|
+
*
|
|
29
|
+
* Constructor caveat: the calibration-check sequence below is conditional on
|
|
30
|
+
* read results (a genuine async dependency chain), but JS constructors
|
|
31
|
+
* cannot be async. It is fired off unawaited, matching the fire-and-forget
|
|
32
|
+
* convention used for every other constructor-time connection call in this
|
|
33
|
+
* port — in practice it settles before any subsequent await in caller code
|
|
34
|
+
* gets scheduled, since the underlying I2C connection is synchronous under
|
|
35
|
+
* the hood, but this is not a guaranteed blocking wait the way the
|
|
36
|
+
* pre-async version was. Call `await sensor.read()` once before relying on
|
|
37
|
+
* calibration state if this matters for your use case.
|
|
38
|
+
*/
|
|
39
|
+
class AHT21Minimal {
|
|
40
|
+
/**
|
|
41
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x38).
|
|
42
|
+
*/
|
|
43
|
+
constructor(connection) {
|
|
44
|
+
this._conn = connection;
|
|
45
|
+
this._init();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async _init() {
|
|
49
|
+
_sleep(100);
|
|
50
|
+
let status = await this._readStatus();
|
|
51
|
+
if ((status & 0x18) !== 0x18) {
|
|
52
|
+
await this._conn.write(_CMD_SOFT_RESET);
|
|
53
|
+
_sleep(20);
|
|
54
|
+
status = await this._readStatus();
|
|
55
|
+
if ((status & 0x18) !== 0x18) {
|
|
56
|
+
await this._conn.write(_CMD_CAL_INIT_1);
|
|
57
|
+
_sleep(10);
|
|
58
|
+
await this._conn.write(_CMD_CAL_INIT_2);
|
|
59
|
+
_sleep(10);
|
|
60
|
+
await this._conn.write(_CMD_CAL_INIT_3);
|
|
61
|
+
_sleep(10);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async _readStatus() {
|
|
67
|
+
return (await this._conn.read(1))[0];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Trigger a measurement and return temperature and humidity.
|
|
72
|
+
*
|
|
73
|
+
* Sends the trigger command, waits 80 ms, reads 6 bytes, and decodes
|
|
74
|
+
* the raw 20-bit values into physical units.
|
|
75
|
+
*
|
|
76
|
+
* @returns {Promise<{ temperature_c: number, humidity_pct: number }>}
|
|
77
|
+
* temperature_c: Temperature in degrees Celsius (-50 to 150 °C).
|
|
78
|
+
* humidity_pct: Relative humidity in percent (0 to 100 %RH).
|
|
79
|
+
*/
|
|
80
|
+
async read() {
|
|
81
|
+
await this._conn.write(_CMD_TRIGGER);
|
|
82
|
+
_sleep(80);
|
|
83
|
+
const data = await this._conn.read(6);
|
|
84
|
+
const rawRh = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
|
|
85
|
+
const rawT = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
|
|
86
|
+
const humidityPct = (rawRh / 1048576.0) * 100.0;
|
|
87
|
+
const temperatureC = (rawT / 1048576.0) * 200.0 - 50.0;
|
|
88
|
+
return { temperature_c: temperatureC, humidity_pct: humidityPct };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* AHT21 full interface — extends AHT21Minimal with CRC and status support.
|
|
94
|
+
*
|
|
95
|
+
* Adds CRC-8 verification, explicit soft reset, calibration status inspection,
|
|
96
|
+
* and individual temperature/humidity readings.
|
|
97
|
+
*/
|
|
98
|
+
class AHT21Full extends AHT21Minimal {
|
|
99
|
+
/**
|
|
100
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x38).
|
|
101
|
+
*/
|
|
102
|
+
constructor(connection) {
|
|
103
|
+
super(connection);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Trigger a measurement and return temperature only.
|
|
108
|
+
* @returns {Promise<number>} Temperature in degrees Celsius (-50 to 150 °C).
|
|
109
|
+
*/
|
|
110
|
+
async readTemperature() {
|
|
111
|
+
return (await this.read()).temperature_c;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Trigger a measurement and return humidity only.
|
|
116
|
+
* @returns {Promise<number>} Relative humidity in percent (0 to 100 %RH).
|
|
117
|
+
*/
|
|
118
|
+
async readHumidity() {
|
|
119
|
+
return (await this.read()).humidity_pct;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Trigger a measurement, read 7 bytes, and verify CRC-8.
|
|
124
|
+
*
|
|
125
|
+
* Uses polynomial x^8 + x^5 + x^4 + 1 (0x31) with initial value 0xFF
|
|
126
|
+
* to verify the CRC byte against bytes 0–5 of the response.
|
|
127
|
+
*
|
|
128
|
+
* @returns {Promise<{ temperature_c: number, humidity_pct: number, crc_ok: boolean }>}
|
|
129
|
+
* temperature_c: Temperature in degrees Celsius.
|
|
130
|
+
* humidity_pct: Relative humidity in percent.
|
|
131
|
+
* crc_ok: True if CRC-8 verification passed.
|
|
132
|
+
*/
|
|
133
|
+
async readWithCrc() {
|
|
134
|
+
await this._conn.write(_CMD_TRIGGER);
|
|
135
|
+
_sleep(80);
|
|
136
|
+
const data = await this._conn.read(7);
|
|
137
|
+
const rawRh = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
|
|
138
|
+
const rawT = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
|
|
139
|
+
const humidityPct = (rawRh / 1048576.0) * 100.0;
|
|
140
|
+
const temperatureC = (rawT / 1048576.0) * 200.0 - 50.0;
|
|
141
|
+
const crcOk = this._crc8(data.slice(0, 6)) === data[6];
|
|
142
|
+
return { temperature_c: temperatureC, humidity_pct: humidityPct, crc_ok: crcOk };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Send the soft reset command and wait 20 ms for recovery.
|
|
147
|
+
* @returns {Promise<void>}
|
|
148
|
+
*/
|
|
149
|
+
async softReset() {
|
|
150
|
+
await this._conn.write(_CMD_SOFT_RESET);
|
|
151
|
+
_sleep(20);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Check if the calibration bit is set in the status byte.
|
|
156
|
+
* @returns {Promise<boolean>} True if the sensor reports calibration enabled.
|
|
157
|
+
*/
|
|
158
|
+
async isCalibrated() {
|
|
159
|
+
return !!((await this._readStatus()) & _STATUS_CAL);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Check if the busy bit is set in the status byte.
|
|
164
|
+
* @returns {Promise<boolean>} True if a measurement is in progress.
|
|
165
|
+
*/
|
|
166
|
+
async isBusy() {
|
|
167
|
+
return !!((await this._readStatus()) & _STATUS_BUSY);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
_crc8(data) {
|
|
171
|
+
let crc = 0xFF;
|
|
172
|
+
for (let i = 0; i < data.length; i++) {
|
|
173
|
+
crc ^= data[i];
|
|
174
|
+
for (let j = 0; j < 8; j++) {
|
|
175
|
+
if (crc & 0x80)
|
|
176
|
+
crc = ((crc << 1) ^ 0x31) & 0xFF;
|
|
177
|
+
else
|
|
178
|
+
crc = (crc << 1) & 0xFF;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return crc;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
module.exports = { AHT21Minimal, AHT21Full };
|