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
|
@@ -36,27 +36,27 @@ function _sleep(ms) {
|
|
|
36
36
|
*/
|
|
37
37
|
class EEPROM24AA02UIDMinimal {
|
|
38
38
|
/**
|
|
39
|
-
* @param {
|
|
39
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
|
|
40
40
|
*/
|
|
41
|
-
constructor(
|
|
42
|
-
this.
|
|
41
|
+
constructor(connection) {
|
|
42
|
+
this._conn = connection;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Read the chip's factory-programmed 32-bit unique serial number.
|
|
47
|
-
* @returns {Buffer} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
|
|
47
|
+
* @returns {Promise<Buffer>} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
|
|
48
48
|
*/
|
|
49
|
-
readUid() {
|
|
50
|
-
return this.
|
|
49
|
+
async readUid() {
|
|
50
|
+
return this._conn.writeRead(Buffer.from([ADDR_UID_BASE]), 4);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
54
|
* Read a single byte from user EEPROM at 0x00-0x7F.
|
|
55
55
|
* @param {number} address - Memory address 0-127.
|
|
56
|
-
* @returns {number} Byte value 0-255.
|
|
56
|
+
* @returns {Promise<number>} Byte value 0-255.
|
|
57
57
|
*/
|
|
58
|
-
readByte(address) {
|
|
59
|
-
return this.
|
|
58
|
+
async readByte(address) {
|
|
59
|
+
return (await this._conn.writeRead(Buffer.from([address & 0xFF]), 1))[0];
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
@@ -68,16 +68,17 @@ class EEPROM24AA02UIDMinimal {
|
|
|
68
68
|
*
|
|
69
69
|
* @param {number} address - Memory address 0-255.
|
|
70
70
|
* @param {number} value - Byte value 0-255.
|
|
71
|
+
* @returns {Promise<void>}
|
|
71
72
|
*/
|
|
72
|
-
writeByte(address, value) {
|
|
73
|
-
this.
|
|
74
|
-
this._ackPoll();
|
|
73
|
+
async writeByte(address, value) {
|
|
74
|
+
await this._conn.write(Buffer.from([address & 0xFF, value & 0xFF]));
|
|
75
|
+
await this._ackPoll();
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
_ackPoll() {
|
|
78
|
+
async _ackPoll() {
|
|
78
79
|
for (let i = 0; i < ACK_POLL_MAX; i++) {
|
|
79
80
|
try {
|
|
80
|
-
this.
|
|
81
|
+
await this._conn.writeRead(Buffer.from([0x00]), 1);
|
|
81
82
|
return;
|
|
82
83
|
} catch (e) {
|
|
83
84
|
_sleep(1);
|
|
@@ -95,10 +96,10 @@ class EEPROM24AA02UIDMinimal {
|
|
|
95
96
|
*/
|
|
96
97
|
class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
97
98
|
/**
|
|
98
|
-
* @param {
|
|
99
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
|
|
99
100
|
*/
|
|
100
|
-
constructor(
|
|
101
|
-
super(
|
|
101
|
+
constructor(connection) {
|
|
102
|
+
super(connection);
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
/**
|
|
@@ -109,10 +110,10 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
|
109
110
|
*
|
|
110
111
|
* @param {number} address - Starting address 0-255.
|
|
111
112
|
* @param {number} length - Number of bytes to read.
|
|
112
|
-
* @returns {Buffer} `length` bytes from the device.
|
|
113
|
+
* @returns {Promise<Buffer>} `length` bytes from the device.
|
|
113
114
|
*/
|
|
114
|
-
read(address, length) {
|
|
115
|
-
return this.
|
|
115
|
+
async read(address, length) {
|
|
116
|
+
return this._conn.writeRead(Buffer.from([address & 0xFF]), length);
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
/**
|
|
@@ -125,14 +126,15 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
|
125
126
|
*
|
|
126
127
|
* @param {number} address - Start address within an 8-byte page (0, 8, 16, …).
|
|
127
128
|
* @param {Buffer|Uint8Array} data - Bytes to write (1 to 8 bytes).
|
|
129
|
+
* @returns {Promise<void>}
|
|
128
130
|
*/
|
|
129
|
-
writePage(address, data) {
|
|
131
|
+
async writePage(address, data) {
|
|
130
132
|
if (data.length === 0) return;
|
|
131
133
|
const buf = Buffer.alloc(1 + data.length);
|
|
132
134
|
buf[0] = address & 0xFF;
|
|
133
135
|
for (let i = 0; i < data.length; i++) buf[1 + i] = data[i];
|
|
134
|
-
this.
|
|
135
|
-
this._ackPoll();
|
|
136
|
+
await this._conn.write(buf);
|
|
137
|
+
await this._ackPoll();
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
/**
|
|
@@ -143,8 +145,9 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
|
143
145
|
*
|
|
144
146
|
* @param {number} address - Starting address 0-255.
|
|
145
147
|
* @param {Buffer|Uint8Array} data - Bytes to write.
|
|
148
|
+
* @returns {Promise<void>}
|
|
146
149
|
*/
|
|
147
|
-
write(address, data) {
|
|
150
|
+
async write(address, data) {
|
|
148
151
|
let offset = 0;
|
|
149
152
|
let remaining = data.length;
|
|
150
153
|
let current = address;
|
|
@@ -152,7 +155,7 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
|
152
155
|
const pageOffset = current & (PAGE_SIZE - 1);
|
|
153
156
|
let chunk = PAGE_SIZE - pageOffset;
|
|
154
157
|
if (chunk > remaining) chunk = remaining;
|
|
155
|
-
this.writePage(current, data.slice(offset, offset + chunk));
|
|
158
|
+
await this.writePage(current, data.slice(offset, offset + chunk));
|
|
156
159
|
offset += chunk;
|
|
157
160
|
current += chunk;
|
|
158
161
|
remaining -= chunk;
|
|
@@ -161,17 +164,17 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
|
|
|
161
164
|
|
|
162
165
|
/**
|
|
163
166
|
* Read the manufacturer code at 0xFA.
|
|
164
|
-
* @returns {number} Manufacturer code; expect 0x29 (Microchip).
|
|
167
|
+
* @returns {Promise<number>} Manufacturer code; expect 0x29 (Microchip).
|
|
165
168
|
*/
|
|
166
|
-
readManufacturerCode() {
|
|
169
|
+
async readManufacturerCode() {
|
|
167
170
|
return this.readByte(ADDR_MFR_CODE);
|
|
168
171
|
}
|
|
169
172
|
|
|
170
173
|
/**
|
|
171
174
|
* Read the device code at 0xFB.
|
|
172
|
-
* @returns {number} Device code; expect 0x41 (I2C 2-Kbit EEPROM).
|
|
175
|
+
* @returns {Promise<number>} Device code; expect 0x41 (I2C 2-Kbit EEPROM).
|
|
173
176
|
*/
|
|
174
|
-
readDeviceCode() {
|
|
177
|
+
async readDeviceCode() {
|
|
175
178
|
return this.readByte(ADDR_DEV_CODE);
|
|
176
179
|
}
|
|
177
180
|
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONTROL = 0x00;
|
|
4
|
+
const _REG_FAULT = 0x01;
|
|
5
|
+
|
|
6
|
+
const _CTRL_IN1 = 0x01;
|
|
7
|
+
const _CTRL_IN2 = 0x02;
|
|
8
|
+
|
|
9
|
+
const _FAULT_FAULT = 0x01;
|
|
10
|
+
const _FAULT_OCP = 0x02;
|
|
11
|
+
const _FAULT_UVLO = 0x04;
|
|
12
|
+
const _FAULT_OTS = 0x08;
|
|
13
|
+
const _FAULT_ILIMIT = 0x10;
|
|
14
|
+
const _FAULT_CLEAR = 0x80;
|
|
15
|
+
|
|
16
|
+
const _VREF = 1.285;
|
|
17
|
+
const _VSET_MIN = 6;
|
|
18
|
+
const _VSET_MAX = 63;
|
|
19
|
+
|
|
20
|
+
const _DIRECTIONS = ['coast', 'forward', 'reverse', 'brake'];
|
|
21
|
+
|
|
22
|
+
/** Map |voltage| to a VSET code; 0 means coast (below the vset=6 floor). */
|
|
23
|
+
function _voltageToVset(voltage) {
|
|
24
|
+
const vset = Math.floor(Math.abs(voltage) * 16 / _VREF + 0.5);
|
|
25
|
+
if (vset < _VSET_MIN) return 0;
|
|
26
|
+
if (vset > _VSET_MAX) return _VSET_MAX;
|
|
27
|
+
return vset;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _vsetToVoltage(vset) {
|
|
31
|
+
if (vset < _VSET_MIN) return 0;
|
|
32
|
+
return _VREF * vset / 16;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* DRV8830 low-voltage motor driver with I²C interface (Texas Instruments)
|
|
37
|
+
* — minimal interface.
|
|
38
|
+
*
|
|
39
|
+
* H-bridge driver for a single brushed DC motor, controlled entirely over
|
|
40
|
+
* I²C. The host commands a target output *voltage*; the chip PWM-regulates
|
|
41
|
+
* the bridge to hold that average voltage regardless of supply sag. Nine
|
|
42
|
+
* selectable addresses (0x60–0x68) via the tri-state A0/A1 strap pins.
|
|
43
|
+
*
|
|
44
|
+
* No register writes are needed at construction — the chip's POR default
|
|
45
|
+
* already leaves the motor in standby/coast.
|
|
46
|
+
*/
|
|
47
|
+
class DRV8830Minimal {
|
|
48
|
+
/**
|
|
49
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x60–0x68).
|
|
50
|
+
*/
|
|
51
|
+
constructor(connection) {
|
|
52
|
+
this._conn = connection;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async _writeReg(reg, value) {
|
|
56
|
+
await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async _readReg(reg) {
|
|
60
|
+
const buf = await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1);
|
|
61
|
+
return buf[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Confirm the device answers on the bus. The DRV8830 has no
|
|
66
|
+
* WHO_AM_I/identity register, so this is a plain CONTROL read; it makes
|
|
67
|
+
* no register writes.
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
async init() {
|
|
71
|
+
await this._readReg(_REG_CONTROL);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Drive the motor at a regulated output voltage. Writes VSET and
|
|
76
|
+
* IN1/IN2 together in one CONTROL write. A magnitude below the ~0.48 V
|
|
77
|
+
* floor is treated as 0 V (coast); above ~5.06 V it is clamped.
|
|
78
|
+
* @param {number} voltage - Signed target voltage in V: positive = forward, negative = reverse, 0 = coast.
|
|
79
|
+
* @returns {Promise<void>}
|
|
80
|
+
*/
|
|
81
|
+
async drive(voltage) {
|
|
82
|
+
const vset = _voltageToVset(voltage);
|
|
83
|
+
if (vset === 0) {
|
|
84
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
85
|
+
} else if (voltage > 0) {
|
|
86
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN1);
|
|
87
|
+
} else {
|
|
88
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN2);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Short-brake the motor (IN1 = IN2 = 1, both outputs high).
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
async brake() {
|
|
97
|
+
await this._writeReg(_REG_CONTROL, _CTRL_IN1 | _CTRL_IN2);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Put the bridge in standby/coast (IN1 = IN2 = 0) — same as `drive(0)`.
|
|
102
|
+
* @returns {Promise<void>}
|
|
103
|
+
*/
|
|
104
|
+
async stop() {
|
|
105
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* DRV8830 full interface — extends Minimal with raw CONTROL access, output
|
|
111
|
+
* read-back, fault reporting/clearing, and the FAULTn interrupt API.
|
|
112
|
+
*
|
|
113
|
+
* Faults are never cleared implicitly: a latched OCP/ILIMIT fault also
|
|
114
|
+
* disables the H-bridge, so clearing is always an explicit `clearFault()`.
|
|
115
|
+
*/
|
|
116
|
+
class DRV8830Full extends DRV8830Minimal {
|
|
117
|
+
/**
|
|
118
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x60–0x68).
|
|
119
|
+
*/
|
|
120
|
+
constructor(connection) {
|
|
121
|
+
super(connection);
|
|
122
|
+
this._callback = null;
|
|
123
|
+
this._edgeHandler = null;
|
|
124
|
+
this._pollTimer = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Write the CONTROL register from raw fields.
|
|
129
|
+
* @param {number} vset - VSET DAC code, 6–63 (0–5 are reserved).
|
|
130
|
+
* @param {boolean} in1 - H-bridge input 1.
|
|
131
|
+
* @param {boolean} in2 - H-bridge input 2.
|
|
132
|
+
* @returns {Promise<void>}
|
|
133
|
+
* @throws {RangeError} If vset is outside 6–63.
|
|
134
|
+
*/
|
|
135
|
+
async setOutput(vset, in1, in2) {
|
|
136
|
+
if (!Number.isInteger(vset) || vset < _VSET_MIN || vset > _VSET_MAX) {
|
|
137
|
+
throw new RangeError('vset must be an integer 6-63');
|
|
138
|
+
}
|
|
139
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | (in1 ? _CTRL_IN1 : 0) | (in2 ? _CTRL_IN2 : 0));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read back and decode the CONTROL register.
|
|
144
|
+
* @returns {Promise<{voltage: number, direction: ('forward'|'reverse'|'coast'|'brake')}>}
|
|
145
|
+
* voltage is the commanded magnitude in V (0 for a reserved VSET code).
|
|
146
|
+
*/
|
|
147
|
+
async readOutput() {
|
|
148
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
149
|
+
return { voltage: _vsetToVoltage(ctrl >> 2), direction: _DIRECTIONS[ctrl & 0x03] };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Read the FAULT register without clearing it.
|
|
154
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
155
|
+
*/
|
|
156
|
+
async readFault() {
|
|
157
|
+
const f = await this._readReg(_REG_FAULT);
|
|
158
|
+
return {
|
|
159
|
+
fault: (f & _FAULT_FAULT) !== 0,
|
|
160
|
+
ocp: (f & _FAULT_OCP) !== 0,
|
|
161
|
+
uvlo: (f & _FAULT_UVLO) !== 0,
|
|
162
|
+
ots: (f & _FAULT_OTS) !== 0,
|
|
163
|
+
ilimit: (f & _FAULT_ILIMIT) !== 0,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Clear all fault status bits (CLEAR = 1); re-enables the H-bridge if an
|
|
169
|
+
* OCP/ILIMIT fault had latched it off.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
*/
|
|
172
|
+
async clearFault() {
|
|
173
|
+
await this._writeReg(_REG_FAULT, _FAULT_CLEAR);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// -- Interrupt API (Level 1: FAULTn) ------------------------------------
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Read the fault status — equivalent to `readFault()`, does not clear.
|
|
180
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
181
|
+
*/
|
|
182
|
+
async pollInterrupt() {
|
|
183
|
+
return this.readFault();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Subscribe to fault interrupts. Uses `connection.intPin.onEdge()` (FAULTn
|
|
188
|
+
* falling edge) when an INT-line InputPin is wired; otherwise falls back
|
|
189
|
+
* to a 5 ms polling loop that fires on each new fault (FAULT bit rising).
|
|
190
|
+
* The fault is not cleared.
|
|
191
|
+
* @param {function({fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}): void} callback
|
|
192
|
+
* @returns {Promise<void>}
|
|
193
|
+
*/
|
|
194
|
+
async onInterrupt(callback) {
|
|
195
|
+
this._callback = callback;
|
|
196
|
+
if (this._conn.intPin) {
|
|
197
|
+
this._edgeHandler = async () => {
|
|
198
|
+
const status = await this.pollInterrupt();
|
|
199
|
+
if (status.fault && this._callback) this._callback(status);
|
|
200
|
+
};
|
|
201
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
202
|
+
} else {
|
|
203
|
+
let wasFault = false;
|
|
204
|
+
let busy = false;
|
|
205
|
+
this._pollTimer = setInterval(async () => {
|
|
206
|
+
if (busy) return;
|
|
207
|
+
busy = true;
|
|
208
|
+
try {
|
|
209
|
+
const status = await this.pollInterrupt();
|
|
210
|
+
if (status.fault && !wasFault && this._callback) this._callback(status);
|
|
211
|
+
wasFault = status.fault;
|
|
212
|
+
} finally {
|
|
213
|
+
busy = false;
|
|
214
|
+
}
|
|
215
|
+
}, 5);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe from fault interrupts.
|
|
221
|
+
* @returns {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async offInterrupt() {
|
|
224
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
225
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
226
|
+
this._edgeHandler = null;
|
|
227
|
+
}
|
|
228
|
+
if (this._pollTimer) {
|
|
229
|
+
clearInterval(this._pollTimer);
|
|
230
|
+
this._pollTimer = null;
|
|
231
|
+
}
|
|
232
|
+
this._callback = null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Default 7-bit I²C address (A0 = A1 = GND). Valid range 0x60–0x68. */
|
|
237
|
+
DRV8830Minimal.I2C_ADDRESS = 0x60;
|
|
238
|
+
|
|
239
|
+
module.exports = { DRV8830Minimal, DRV8830Full };
|