periph 1.1.0 → 1.2.1
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 +61 -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 +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- 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 +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- 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 +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- 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 +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- 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 +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- 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 +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -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 +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- 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/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -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 +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- 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 +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- 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 +89 -66
- 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 +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/motor/drv8830.js +239 -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 +72 -53
- 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/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -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/{transport → connection}/hx711.js +49 -22
- 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/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_ENABLE = 0x00;
|
|
4
|
+
const _REG_ATIME = 0x01;
|
|
5
|
+
const _REG_PTIME = 0x02;
|
|
6
|
+
const _REG_WTIME = 0x03;
|
|
7
|
+
const _REG_AILTL = 0x04;
|
|
8
|
+
const _REG_AILTH = 0x05;
|
|
9
|
+
const _REG_AIHTL = 0x06;
|
|
10
|
+
const _REG_AIHTH = 0x07;
|
|
11
|
+
const _REG_PILTL = 0x08;
|
|
12
|
+
const _REG_PILTH = 0x09;
|
|
13
|
+
const _REG_PIHTL = 0x0A;
|
|
14
|
+
const _REG_PIHTH = 0x0B;
|
|
15
|
+
const _REG_PERS = 0x0C;
|
|
16
|
+
const _REG_CONFIG = 0x0D;
|
|
17
|
+
const _REG_PPULSE = 0x0E;
|
|
18
|
+
const _REG_CONTROL = 0x0F;
|
|
19
|
+
const _REG_ID = 0x12;
|
|
20
|
+
const _REG_STATUS = 0x13;
|
|
21
|
+
const _REG_CH0DATAL = 0x14;
|
|
22
|
+
const _REG_CH0DATAH = 0x15;
|
|
23
|
+
const _REG_CH1DATAL = 0x16;
|
|
24
|
+
const _REG_CH1DATAH = 0x17;
|
|
25
|
+
const _REG_PDATAL = 0x18;
|
|
26
|
+
const _REG_PDATAH = 0x19;
|
|
27
|
+
const _REG_POFFSET = 0x1E;
|
|
28
|
+
|
|
29
|
+
const _CFN_CLEAR_PROXIMITY = 0x05;
|
|
30
|
+
const _CFN_CLEAR_ALS = 0x06;
|
|
31
|
+
const _CFN_CLEAR_BOTH = 0x07;
|
|
32
|
+
const _CMD_SPECIAL = 0xE0;
|
|
33
|
+
|
|
34
|
+
const _ATIME_DEFAULT = 0xDB;
|
|
35
|
+
const _PTIME_DEFAULT = 0xFF;
|
|
36
|
+
const _PPULSE_DEFAULT = 0x08;
|
|
37
|
+
const _CONTROL_DEFAULT = 0x20;
|
|
38
|
+
const _ENABLE_DEFAULT = 0x07;
|
|
39
|
+
|
|
40
|
+
const _CMD_WRITE = 0x80;
|
|
41
|
+
const _CMD_READ = 0xA0;
|
|
42
|
+
|
|
43
|
+
function _cmdWrite(reg) { return _CMD_WRITE | (reg & 0x1F); }
|
|
44
|
+
function _cmdRead(reg) { return _CMD_READ | (reg & 0x1F); }
|
|
45
|
+
function _cmdSpecial(f) { return _CMD_SPECIAL | (f & 0x1F); }
|
|
46
|
+
|
|
47
|
+
function _sleep(ms) {
|
|
48
|
+
const end = Date.now() + ms;
|
|
49
|
+
while (Date.now() < end) {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function _againFactor(againIdx, agl) {
|
|
53
|
+
if (!agl) return [1, 8, 16, 120][againIdx & 0x03];
|
|
54
|
+
return [1/6, 8/6, 16/6, 20][againIdx & 0x03];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* APDS-9930 digital ambient light and proximity sensor — minimal interface.
|
|
59
|
+
*
|
|
60
|
+
* Provides illuminance (lux, IR-compensated) and raw proximity count with
|
|
61
|
+
* no configuration required beyond the connection. Both engines are
|
|
62
|
+
* enabled at construction with sensible defaults that give stable
|
|
63
|
+
* readings under typical indoor/outdoor lighting.
|
|
64
|
+
*
|
|
65
|
+
* Default configuration (written at construction):
|
|
66
|
+
* - ATIME = 0xDB (101 ms integration — rejects 50/60 Hz fluorescent flicker)
|
|
67
|
+
* - PTIME = 0xFF (2.73 ms proximity ADC time, datasheet default)
|
|
68
|
+
* - PPULSE = 0x08 (8 LED pulses — factory-calibrated for 100 mm range)
|
|
69
|
+
* - CONTROL = 0x20 (PDIODE=Ch1, PDRIVE=100 mA, PGAIN=1x, AGAIN=1x)
|
|
70
|
+
* - ENABLE = 0x07 (PON + AEN + PEN; wait timer and interrupts disabled)
|
|
71
|
+
*
|
|
72
|
+
* Constructor caveat: JS constructors cannot be async, so the original
|
|
73
|
+
* synchronous ID-validation throw cannot be preserved exactly. The whole
|
|
74
|
+
* init sequence is fired off unawaited (matching the fire-and-forget
|
|
75
|
+
* convention used throughout this port); an ID mismatch now surfaces as
|
|
76
|
+
* an *unhandled promise rejection* shortly after construction rather
|
|
77
|
+
* than a synchronous throw from `new APDS9930Minimal(...)`. Callers
|
|
78
|
+
* that need to detect "wrong or absent device" deterministically should
|
|
79
|
+
* call an async method (e.g. `await sensor.lux()`) right after
|
|
80
|
+
* construction and handle its rejection.
|
|
81
|
+
*/
|
|
82
|
+
class APDS9930Minimal {
|
|
83
|
+
/**
|
|
84
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
|
|
85
|
+
*/
|
|
86
|
+
constructor(connection) {
|
|
87
|
+
this._conn = connection;
|
|
88
|
+
this._init();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async _init() {
|
|
92
|
+
_sleep(6);
|
|
93
|
+
const id = await this._readReg(_REG_ID);
|
|
94
|
+
if (id !== 0x39) throw new Error('APDS-9930 not found (ID=0x' + id.toString(16) + ', expected 0x39)');
|
|
95
|
+
await this._writeReg(_REG_ENABLE, 0x00);
|
|
96
|
+
await this._writeReg(_REG_ATIME, _ATIME_DEFAULT);
|
|
97
|
+
await this._writeReg(_REG_PTIME, _PTIME_DEFAULT);
|
|
98
|
+
await this._writeReg(_REG_PPULSE, _PPULSE_DEFAULT);
|
|
99
|
+
await this._writeReg(_REG_CONTROL, _CONTROL_DEFAULT);
|
|
100
|
+
await this._writeReg(_REG_ENABLE, _ENABLE_DEFAULT);
|
|
101
|
+
_sleep(12);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async _writeReg(reg, value) {
|
|
105
|
+
await this._conn.write(Buffer.from([_cmdWrite(reg), value & 0xFF]));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async _readReg(reg) {
|
|
109
|
+
return (await this._conn.writeRead(Buffer.from([_cmdRead(reg)]), 1))[0];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async _readReg16(reg) {
|
|
113
|
+
const buf = await this._conn.writeRead(Buffer.from([_cmdRead(reg)]), 2);
|
|
114
|
+
return (buf[1] << 8) | buf[0];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async _special(functionCode) {
|
|
118
|
+
await this._conn.write(Buffer.from([_cmdSpecial(functionCode)]));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Read the ambient illuminance.
|
|
123
|
+
*
|
|
124
|
+
* Uses Ch0 (visible + IR) and Ch1 (IR-only) to compensate for the
|
|
125
|
+
* IR component of ambient light, then applies the open-air lux
|
|
126
|
+
* coefficients from the datasheet.
|
|
127
|
+
*
|
|
128
|
+
* @returns {Promise<number>} Illuminance in lux.
|
|
129
|
+
*/
|
|
130
|
+
async lux() {
|
|
131
|
+
const ch0 = await this._readReg16(_REG_CH0DATAL);
|
|
132
|
+
const ch1 = await this._readReg16(_REG_CH1DATAL);
|
|
133
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
134
|
+
const cfg = await this._readReg(_REG_CONFIG);
|
|
135
|
+
const atime = await this._readReg(_REG_ATIME);
|
|
136
|
+
const alsitMs = 2.73 * (256 - atime);
|
|
137
|
+
const againX = _againFactor(ctrl & 0x03, (cfg & 0x04) !== 0);
|
|
138
|
+
let iac1 = ch0 - 1.862 * ch1;
|
|
139
|
+
let iac2 = 0.746 * ch0 - 1.291 * ch1;
|
|
140
|
+
let iac = iac1;
|
|
141
|
+
if (iac2 > iac) iac = iac2;
|
|
142
|
+
if (iac < 0) iac = 0;
|
|
143
|
+
const lpc = (0.49 * 52.0) / (alsitMs * againX);
|
|
144
|
+
return iac * lpc;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Read the proximity ADC count.
|
|
149
|
+
*
|
|
150
|
+
* Higher counts mean a closer object. Realistically limited to
|
|
151
|
+
* 10 bits (0-1023) at the default PTIME=0xFF (one ADC cycle).
|
|
152
|
+
*
|
|
153
|
+
* @returns {Promise<number>} Raw 16-bit proximity count.
|
|
154
|
+
*/
|
|
155
|
+
async proximity() {
|
|
156
|
+
return this._readReg16(_REG_PDATAL);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* APDS-9930 full interface — extends APDS9930Minimal.
|
|
162
|
+
*
|
|
163
|
+
* Adds ALS/proximity integration-time and gain configuration, raw
|
|
164
|
+
* channel reads, interrupt thresholds with persistence, status decoding,
|
|
165
|
+
* sleep-after-interrupt, and proximity offset compensation.
|
|
166
|
+
*/
|
|
167
|
+
class APDS9930Full extends APDS9930Minimal {
|
|
168
|
+
/**
|
|
169
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
|
|
170
|
+
*/
|
|
171
|
+
constructor(connection) {
|
|
172
|
+
super(connection);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Configure ALS integration time, AGAIN index, and AGL flag.
|
|
177
|
+
* @param {number} [atime=0xDB] ATIME register value 0-255.
|
|
178
|
+
* @param {number} [again=0] ALS gain index 0-3 (0=1x, 1=8x, 2=16x, 3=120x).
|
|
179
|
+
* @param {boolean} [agl=false] True to enable the AGL divide-by-6 gain-level bit.
|
|
180
|
+
* @returns {Promise<void>}
|
|
181
|
+
*/
|
|
182
|
+
async configureAls(atime = 0xDB, again = 0, agl = false) {
|
|
183
|
+
await this._writeReg(_REG_ATIME, atime & 0xFF);
|
|
184
|
+
const ctrl = (await this._readReg(_REG_CONTROL) & 0xFC) | (again & 0x03);
|
|
185
|
+
await this._writeReg(_REG_CONTROL, ctrl);
|
|
186
|
+
let cfg = await this._readReg(_REG_CONFIG);
|
|
187
|
+
if (agl) cfg |= 0x04; else cfg &= ~0x04;
|
|
188
|
+
cfg &= ~0x06;
|
|
189
|
+
await this._writeReg(_REG_CONFIG, cfg);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Configure proximity LED pulses, gain, drive, and ADC integration time.
|
|
194
|
+
* @param {number} [ppulse=8] Number of LED pulses 1-255.
|
|
195
|
+
* @param {number} [pgain=0] Proximity gain index 0-3.
|
|
196
|
+
* @param {number} [pdrive=0] LED drive current index 0-3.
|
|
197
|
+
* @param {boolean} [pdl=false] True to enable PDL (reduces drive to 1/9).
|
|
198
|
+
* @param {number} [ptime=0xFF] PTIME register value 0-255.
|
|
199
|
+
* @returns {Promise<void>}
|
|
200
|
+
*/
|
|
201
|
+
async configureProximity(ppulse = 8, pgain = 0, pdrive = 0, pdl = false, ptime = 0xFF) {
|
|
202
|
+
await this._writeReg(_REG_PPULSE, ppulse & 0xFF);
|
|
203
|
+
await this._writeReg(_REG_PTIME, ptime & 0xFF);
|
|
204
|
+
const ctrl = ((await this._readReg(_REG_CONTROL)) & 0x03)
|
|
205
|
+
| ((pdrive & 0x03) << 6)
|
|
206
|
+
| 0x20
|
|
207
|
+
| ((pgain & 0x03) << 2);
|
|
208
|
+
await this._writeReg(_REG_CONTROL, ctrl);
|
|
209
|
+
let cfg = await this._readReg(_REG_CONFIG);
|
|
210
|
+
if (pdl) cfg |= 0x01; else cfg &= ~0x01;
|
|
211
|
+
cfg &= ~0x06;
|
|
212
|
+
await this._writeReg(_REG_CONFIG, cfg);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Configure wait time and enable the wait timer.
|
|
217
|
+
* @param {number} [wtime=0xFF] WTIME register value 0-255.
|
|
218
|
+
* @param {boolean} [wlong=false] True to enable WLONG (multiplies wait by 12x).
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
async configureWait(wtime = 0xFF, wlong = false) {
|
|
222
|
+
await this._writeReg(_REG_WTIME, wtime & 0xFF);
|
|
223
|
+
let cfg = await this._readReg(_REG_CONFIG);
|
|
224
|
+
if (wlong) cfg |= 0x02; else cfg &= ~0x02;
|
|
225
|
+
cfg &= ~0x04;
|
|
226
|
+
await this._writeReg(_REG_CONFIG, cfg);
|
|
227
|
+
const en = (await this._readReg(_REG_ENABLE)) | 0x08;
|
|
228
|
+
await this._writeReg(_REG_ENABLE, en);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Clear WEN in ENABLE (disable the wait timer).
|
|
233
|
+
* @returns {Promise<void>}
|
|
234
|
+
*/
|
|
235
|
+
async disableWait() {
|
|
236
|
+
const en = (await this._readReg(_REG_ENABLE)) & ~0x08;
|
|
237
|
+
await this._writeReg(_REG_ENABLE, en);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Read the raw Ch0 (visible + IR) ADC count.
|
|
242
|
+
* @returns {Promise<number>} 16-bit ADC count.
|
|
243
|
+
*/
|
|
244
|
+
async ch0() { return this._readReg16(_REG_CH0DATAL); }
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Read the raw Ch1 (IR-only) ADC count.
|
|
248
|
+
* @returns {Promise<number>} 16-bit ADC count.
|
|
249
|
+
*/
|
|
250
|
+
async ch1() { return this._readReg16(_REG_CH1DATAL); }
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Read the STATUS register decoded into named fields.
|
|
254
|
+
* @returns {Promise<{avalid: boolean, pvalid: boolean, psat: boolean, aint: boolean, pint: boolean}>}
|
|
255
|
+
*/
|
|
256
|
+
async status() {
|
|
257
|
+
const s = await this._readReg(_REG_STATUS);
|
|
258
|
+
return {
|
|
259
|
+
avalid: (s & 0x01) !== 0,
|
|
260
|
+
pvalid: (s & 0x02) !== 0,
|
|
261
|
+
psat: (s & 0x40) !== 0,
|
|
262
|
+
aint: (s & 0x10) !== 0,
|
|
263
|
+
pint: (s & 0x20) !== 0
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Set ALS interrupt thresholds and enable AIEN. Thresholds are evaluated
|
|
269
|
+
* against raw Ch0 counts, not lux.
|
|
270
|
+
* @param {number} low 16-bit low threshold.
|
|
271
|
+
* @param {number} high 16-bit high threshold.
|
|
272
|
+
* @param {number} [persistence=1] APERS value 0-15.
|
|
273
|
+
* @returns {Promise<void>}
|
|
274
|
+
*/
|
|
275
|
+
async setAlsThresholds(low, high, persistence = 1) {
|
|
276
|
+
if (low > high) high = low;
|
|
277
|
+
await this._writeReg(_REG_AILTL, low & 0xFF);
|
|
278
|
+
await this._writeReg(_REG_AILTH, (low >> 8) & 0xFF);
|
|
279
|
+
await this._writeReg(_REG_AIHTL, high & 0xFF);
|
|
280
|
+
await this._writeReg(_REG_AIHTH, (high >> 8) & 0xFF);
|
|
281
|
+
const pers = (await this._readReg(_REG_PERS) & 0xF0) | (persistence & 0x0F);
|
|
282
|
+
await this._writeReg(_REG_PERS, pers);
|
|
283
|
+
const en = (await this._readReg(_REG_ENABLE)) | 0x10;
|
|
284
|
+
await this._writeReg(_REG_ENABLE, en);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Set proximity interrupt thresholds and enable PIEN.
|
|
289
|
+
* @param {number} low 16-bit low threshold.
|
|
290
|
+
* @param {number} high 16-bit high threshold.
|
|
291
|
+
* @param {number} [persistence=1] PPERS value 0-15.
|
|
292
|
+
* @returns {Promise<void>}
|
|
293
|
+
*/
|
|
294
|
+
async setProximityThresholds(low, high, persistence = 1) {
|
|
295
|
+
if (low > high) high = low;
|
|
296
|
+
await this._writeReg(_REG_PILTL, low & 0xFF);
|
|
297
|
+
await this._writeReg(_REG_PILTH, (low >> 8) & 0xFF);
|
|
298
|
+
await this._writeReg(_REG_PIHTL, high & 0xFF);
|
|
299
|
+
await this._writeReg(_REG_PIHTH, (high >> 8) & 0xFF);
|
|
300
|
+
const pers = (await this._readReg(_REG_PERS) & 0x0F) | ((persistence & 0x0F) << 4);
|
|
301
|
+
await this._writeReg(_REG_PERS, pers);
|
|
302
|
+
const en = (await this._readReg(_REG_ENABLE)) | 0x20;
|
|
303
|
+
await this._writeReg(_REG_ENABLE, en);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Clear pending interrupt(s).
|
|
308
|
+
* @param {('als'|'proximity'|'both')} [channel='both']
|
|
309
|
+
* @returns {Promise<void>}
|
|
310
|
+
*/
|
|
311
|
+
async clearInterrupt(channel = 'both') {
|
|
312
|
+
if (channel === 'als') await this._special(_CFN_CLEAR_ALS);
|
|
313
|
+
else if (channel === 'proximity') await this._special(_CFN_CLEAR_PROXIMITY);
|
|
314
|
+
else await this._special(_CFN_CLEAR_BOTH);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Set the proximity offset (sign-magnitude).
|
|
319
|
+
* @param {number} offset Signed integer -127..+127 (positive shifts data up).
|
|
320
|
+
* @returns {Promise<void>}
|
|
321
|
+
*/
|
|
322
|
+
async setProximityOffset(offset) {
|
|
323
|
+
const enc = offset >= 0
|
|
324
|
+
? (0x80 | (offset & 0x7F))
|
|
325
|
+
: ((-offset) & 0x7F);
|
|
326
|
+
await this._writeReg(_REG_POFFSET, enc);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Enable or disable SAI (sleep after interrupt).
|
|
331
|
+
* @param {boolean} enable True to set SAI, false to clear it.
|
|
332
|
+
* @returns {Promise<void>}
|
|
333
|
+
*/
|
|
334
|
+
async sleepAfterInterrupt(enable) {
|
|
335
|
+
const en = enable
|
|
336
|
+
? ((await this._readReg(_REG_ENABLE)) | 0x40)
|
|
337
|
+
: ((await this._readReg(_REG_ENABLE)) & ~0x40);
|
|
338
|
+
await this._writeReg(_REG_ENABLE, en);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
module.exports = { APDS9930Minimal, APDS9930Full };
|