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.
Files changed (241) hide show
  1. package/README.md +54 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  116. package/examples/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +289 -0
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +499 -0
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +182 -0
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +188 -206
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/connection/hx711.js +132 -0
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,242 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * TPIC6B595 8-bit power SIPO shift register — minimal interface.
5
+ *
6
+ * Drives up to {@code numDevices} cascaded TPIC6B595s through a SiPo
7
+ * (serial-in/parallel-out) connection. Each device exposes 8 open-drain
8
+ * outputs (DRAIN0–DRAIN7); every write shifts the entire cascade MSB-first
9
+ * and pulses RCK to latch all outputs atomically. Outputs only sink current
10
+ * — they never source it; an external pull-up or load supply is required
11
+ * for the "off"/high state.
12
+ *
13
+ * The driver owns a {@code numDevices}-byte shadow register so single-pin
14
+ * updates work without re-reading the bus. Every write (pin, port, fill)
15
+ * rebuilds and retransmits the entire reversed cascade — see
16
+ * {@code specs/io_expander/tpic6b595.md} for the wire-order reversal that
17
+ * cascading requires.
18
+ *
19
+ * Initialises every output to OFF at construction (shadow zero, latched
20
+ * once). If the SiPo connection has SRCLR wired, the constructor pulses
21
+ * it to clear the shift register before the all-zero latch.
22
+ */
23
+ class Tpic6b595Minimal {
24
+ /**
25
+ * @param {import('../../connection/sipo').SiPoConnection} connection - Configured SiPo connection.
26
+ * @param {number} [numDevices=1] - Number of cascaded TPIC6B595s on the wire.
27
+ */
28
+ constructor(connection, numDevices = 1) {
29
+ this._conn = connection;
30
+ this._numDevices = numDevices;
31
+ this._shadow = new Uint8Array(numDevices);
32
+ try { this._conn.clear(); } catch (_) { /* SRCLR optional */ }
33
+ this._flush();
34
+ }
35
+
36
+ _flush() {
37
+ const wire = Buffer.alloc(this._numDevices);
38
+ for (let i = 0; i < this._numDevices; i++) {
39
+ wire[i] = this._shadow[this._numDevices - 1 - i];
40
+ }
41
+ this._conn.write(wire);
42
+ }
43
+
44
+ async _setPin(n, value) {
45
+ const port = Math.floor(n / 8);
46
+ const bit = n % 8;
47
+ if (value) this._shadow[port] |= (1 << bit);
48
+ else this._shadow[port] &= ~(1 << bit);
49
+ this._flush();
50
+ }
51
+
52
+ /**
53
+ * Return a Pin proxy object for global pin number {@code n}.
54
+ * @param {number} n - Pin index, 0 (DRAIN0 of the device nearest the controller) to {@code numDevices * 8 - 1}.
55
+ * @returns {_Pin} Pin proxy.
56
+ */
57
+ pin(n) {
58
+ return new _Pin(this, n);
59
+ }
60
+
61
+ /**
62
+ * Write all 8 outputs of cascaded device {@code port} from {@code mask}.
63
+ *
64
+ * Updates the shadow register for the targeted port, rebuilds the
65
+ * reversed cascade, shifts it out, and pulses RCK to latch every
66
+ * cascaded device's outputs.
67
+ *
68
+ * @param {number} port - Cascaded device index (0 = nearest the controller).
69
+ * @param {number} mask - 8-bit output mask. Bit 0 = DRAIN0, bit 7 = DRAIN7.
70
+ * 1 = ON (DMOS conducting, sinks current); 0 = OFF (high-impedance).
71
+ * @returns {Promise<void>}
72
+ */
73
+ async writePort(port, mask) {
74
+ this._shadow[port] = mask & 0xFF;
75
+ this._flush();
76
+ }
77
+
78
+ /**
79
+ * Set every pin on every cascaded device to {@code value}.
80
+ *
81
+ * Sends immediately — the fast path for "all on"/"all off".
82
+ *
83
+ * @param {boolean} value - true to turn every output ON, false to turn them OFF.
84
+ * @returns {Promise<void>}
85
+ */
86
+ async fill(value) {
87
+ const b = value ? 0xFF : 0x00;
88
+ for (let i = 0; i < this._numDevices; i++) this._shadow[i] = b;
89
+ this._flush();
90
+ }
91
+
92
+ /**
93
+ * Turn every output off (equivalent to {@code fill(false)}).
94
+ * @returns {Promise<void>}
95
+ */
96
+ async off() { return this.fill(false); }
97
+ }
98
+
99
+ /**
100
+ * GPIO proxy for a single TPIC6B595 pin — output-only.
101
+ *
102
+ * Obtain via {@code Tpic6b595Minimal.pin(n)}. Do not instantiate directly.
103
+ */
104
+ class _Pin {
105
+ /**
106
+ * @param {Tpic6b595Minimal} chip - Parent driver instance.
107
+ * @param {number} n - Pin index (0 to {@code numDevices * 8 - 1}).
108
+ */
109
+ constructor(chip, n) {
110
+ this._chip = chip;
111
+ this._n = n;
112
+ }
113
+
114
+ /**
115
+ * Read pin shadow bit (NOT a bus read — SiPo is write-only).
116
+ * @returns {Promise<number>} 0 or 1.
117
+ */
118
+ async read() {
119
+ const port = Math.floor(this._n / 8);
120
+ const bit = this._n % 8;
121
+ return (this._chip._shadow[port] >> bit) & 1;
122
+ }
123
+
124
+ /**
125
+ * Write pin.
126
+ * @param {number} value - 0 (DMOS OFF, high-impedance) or 1 (DMOS ON, sinks current).
127
+ * @returns {Promise<void>}
128
+ */
129
+ async write(value) {
130
+ await this._chip._setPin(this._n, value ? 1 : 0);
131
+ }
132
+
133
+ /** Set DMOS output ON (sink current through the external load). */
134
+ async on() { await this._chip._setPin(this._n, 1); }
135
+
136
+ /** Set DMOS output OFF (high-impedance). */
137
+ async off() { await this._chip._setPin(this._n, 0); }
138
+
139
+ /** Invert the shadow bit for this pin. */
140
+ async toggle() {
141
+ const port = Math.floor(this._n / 8);
142
+ const bit = this._n % 8;
143
+ const cur = (this._chip._shadow[port] >> bit) & 1;
144
+ await this._chip._setPin(this._n, 1 - cur);
145
+ }
146
+
147
+ /**
148
+ * OutputPin interface — satisfies the project's OutputPin contract.
149
+ * @param {boolean} high - true = ON, false = OFF.
150
+ */
151
+ async set(high) { await this._chip._setPin(this._n, high ? 1 : 0); }
152
+
153
+ /**
154
+ * Return a synchronous opengpio-shaped Output facade for this pin —
155
+ * a boolean `.value` getter/setter and `stop()` — so it can be passed
156
+ * anywhere real opengpio GPIO is expected.
157
+ *
158
+ * The facade reads back the shadow register directly (authoritative —
159
+ * there is no bus round-trip to be eventually-consistent about).
160
+ *
161
+ * @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
162
+ */
163
+ asGpio() {
164
+ const pin = this;
165
+ const port = Math.floor(this._n / 8);
166
+ const bit = this._n % 8;
167
+ return {
168
+ get direction() { return 'out'; },
169
+ get value() { return ((pin._chip._shadow[port] >> bit) & 1) === 1; },
170
+ set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
171
+ stop() {},
172
+ };
173
+ }
174
+
175
+ /** Release the pin (no-op; shadow state preserved). */
176
+ stop() {}
177
+ }
178
+
179
+ /**
180
+ * TPIC6B595 full interface — extends Tpic6b595Minimal with hardware features.
181
+ *
182
+ * Adds {@code clear()}, {@code setOutputEnable()}, and {@code writeAll()} for
183
+ * the shift-register-clear and output-enable hardware lines, plus a bulk
184
+ * multi-device write. Pin API surface stays exactly Minimal's — every
185
+ * TPIC6B595 pin is a fixed, capability-less output.
186
+ */
187
+ class Tpic6b595Full extends Tpic6b595Minimal {
188
+ /**
189
+ * @param {import('../../connection/sipo').SiPoConnection} connection - Configured SiPo connection.
190
+ * @param {number} [numDevices=1] - Number of cascaded TPIC6B595s on the wire.
191
+ */
192
+ constructor(connection, numDevices = 1) {
193
+ super(connection, numDevices);
194
+ }
195
+
196
+ /**
197
+ * Pulse SRCLR to clear the shift register only.
198
+ *
199
+ * The storage register (and therefore the DRAIN outputs) keeps its
200
+ * last-latched value until the next RCK pulse. To actually blank the
201
+ * outputs, call {@code off()} or {@code fill(false)} afterwards.
202
+ *
203
+ * @returns {void}
204
+ * @throws {Error} If SRCLR was not wired on the SiPo connection.
205
+ */
206
+ clear() { this._conn.clear(); }
207
+
208
+ /**
209
+ * Drive G LOW ({@code enabled=true}) or HIGH ({@code enabled=false}).
210
+ *
211
+ * {@code enabled=true} lets the storage register drive the outputs.
212
+ * {@code enabled=false} forces every output off without disturbing
213
+ * the shadow register or the storage register's contents — the
214
+ * datasheet's documented use case for global PWM-style dimming.
215
+ *
216
+ * @param {boolean} enabled - true to enable, false to disable.
217
+ * @returns {void}
218
+ * @throws {Error} If G was not wired on the SiPo connection.
219
+ */
220
+ setOutputEnable(enabled) { this._conn.setOutputEnable(enabled); }
221
+
222
+ /**
223
+ * Write every cascaded device's byte in one call.
224
+ *
225
+ * Updates the whole shadow register and performs exactly one transmit
226
+ * + latch. {@code values} is length-truncated or zero-extended to
227
+ * {@code numDevices} as needed.
228
+ *
229
+ * @param {number[]|Buffer|Uint8Array} values - One byte per cascaded device.
230
+ * Index 0 is the byte for cascaded device 0 (nearest the controller).
231
+ * @returns {void}
232
+ */
233
+ writeAll(values) {
234
+ for (let i = 0; i < this._numDevices; i++) {
235
+ const v = i < values.length ? values[i] : 0;
236
+ this._shadow[i] = v & 0xFF;
237
+ }
238
+ this._flush();
239
+ }
240
+ }
241
+
242
+ module.exports = { Tpic6b595Minimal, Tpic6b595Full };
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+
3
+ const { _hsvToRgb } = require('./_color');
4
+
5
+ /**
6
+ * Shared minimal-tier logic for 3-channel (RGB) NeoPixel-protocol LED drivers.
7
+ *
8
+ * Drives a chain of n pixels over a NeoPixel connection. Maintains an
9
+ * internal buffer in wire order; fill() writes every pixel and transmits
10
+ * immediately. Subclasses fix channelOrder and resetBytes for their
11
+ * specific chip and expose a public constructor(connection, n) that calls
12
+ * this one with those fixed values.
13
+ *
14
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
15
+ * @param {number} n - Number of pixels in the strip.
16
+ * @param {number[]} channelOrder - [iR, iG, iB]; wire[k] = [r,g,b][channelOrder[k]].
17
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
18
+ */
19
+ class NeoPixelRGBMinimal {
20
+ /**
21
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
22
+ * @param {number} n - Number of pixels in the strip.
23
+ * @param {number[]} channelOrder - [iR, iG, iB] wire channel order.
24
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
25
+ */
26
+ constructor(connection, n, channelOrder, resetBytes) {
27
+ this._conn = connection;
28
+ this._n = n;
29
+ this._buf = Buffer.alloc(n * 3);
30
+ this._channelOrder = channelOrder;
31
+ this._reset = Buffer.alloc(resetBytes);
32
+ }
33
+
34
+ /**
35
+ * Fill every pixel with one colour and send to the strip immediately.
36
+ *
37
+ * Clamps each channel to [0, 255]. Stores the three channels using
38
+ * this chip's wire channel order, then calls connection.write().
39
+ *
40
+ * @param {number} r - Red channel (0–255).
41
+ * @param {number} g - Green channel (0–255).
42
+ * @param {number} b - Blue channel (0–255).
43
+ * @returns {Promise<void>}
44
+ */
45
+ async fill(r, g, b) {
46
+ const vals = [
47
+ Math.max(0, Math.min(255, r | 0)),
48
+ Math.max(0, Math.min(255, g | 0)),
49
+ Math.max(0, Math.min(255, b | 0)),
50
+ ];
51
+ const [i0, i1, i2] = this._channelOrder;
52
+ const w0 = vals[i0], w1 = vals[i1], w2 = vals[i2];
53
+ for (let i = 0; i < this._n; i++) {
54
+ this._buf[i * 3] = w0;
55
+ this._buf[i * 3 + 1] = w1;
56
+ this._buf[i * 3 + 2] = w2;
57
+ }
58
+ await this._conn.write(Buffer.concat([this._buf, this._reset]));
59
+ }
60
+
61
+ /**
62
+ * Turn off all pixels (fill with black and send).
63
+ *
64
+ * Equivalent to fill(0, 0, 0).
65
+ * @returns {Promise<void>}
66
+ */
67
+ async off() {
68
+ await this.fill(0, 0, 0);
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Shared full-tier logic for 3-channel (RGB) NeoPixel-protocol LED drivers.
74
+ *
75
+ * Adds individual pixel addressing, explicit show(), global brightness
76
+ * scaling, buffer rotation, and HSV fill on top of NeoPixelRGBMinimal.
77
+ * Call set_pixel() / set_pixels() to update the buffer, then show() to
78
+ * transmit; or use the inherited fill() for an immediate all-same-colour
79
+ * update.
80
+ *
81
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
82
+ * @param {number} n - Number of pixels in the strip.
83
+ * @param {number[]} channelOrder - [iR, iG, iB] wire channel order.
84
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
85
+ */
86
+ class NeoPixelRGBFull extends NeoPixelRGBMinimal {
87
+ /**
88
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
89
+ * @param {number} n - Number of pixels in the strip.
90
+ * @param {number[]} channelOrder - [iR, iG, iB] wire channel order.
91
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
92
+ */
93
+ constructor(connection, n, channelOrder, resetBytes) {
94
+ super(connection, n, channelOrder, resetBytes);
95
+ this._brightness = 255;
96
+ }
97
+
98
+ /**
99
+ * Global brightness scalar applied at show() time (0–255).
100
+ * @type {number}
101
+ */
102
+ get brightness() { return this._brightness; }
103
+ set brightness(value) {
104
+ this._brightness = Math.max(0, Math.min(255, value | 0));
105
+ }
106
+
107
+ /**
108
+ * Set one pixel in the buffer without sending.
109
+ *
110
+ * Index is clamped to [0, n-1]; each channel to [0, 255].
111
+ * Call show() to transmit.
112
+ *
113
+ * @param {number} index - Zero-based pixel index.
114
+ * @param {number} r - Red channel (0–255).
115
+ * @param {number} g - Green channel (0–255).
116
+ * @param {number} b - Blue channel (0–255).
117
+ */
118
+ set_pixel(index, r, g, b) {
119
+ index = Math.max(0, Math.min(this._n - 1, index | 0));
120
+ const vals = [
121
+ Math.max(0, Math.min(255, r | 0)),
122
+ Math.max(0, Math.min(255, g | 0)),
123
+ Math.max(0, Math.min(255, b | 0)),
124
+ ];
125
+ const [i0, i1, i2] = this._channelOrder;
126
+ this._buf[index * 3] = vals[i0];
127
+ this._buf[index * 3 + 1] = vals[i1];
128
+ this._buf[index * 3 + 2] = vals[i2];
129
+ }
130
+
131
+ /**
132
+ * Write a sequence of [r, g, b] arrays into the buffer starting at pixel 0.
133
+ *
134
+ * Extra entries beyond the strip length are ignored. Call show() to transmit.
135
+ *
136
+ * @param {Array<number[]>} colors - Array of [r, g, b] arrays (0–255 each).
137
+ */
138
+ set_pixels(colors) {
139
+ const [i0, i1, i2] = this._channelOrder;
140
+ for (let i = 0; i < colors.length && i < this._n; i++) {
141
+ const [r, g, b] = colors[i];
142
+ const vals = [
143
+ Math.max(0, Math.min(255, r | 0)),
144
+ Math.max(0, Math.min(255, g | 0)),
145
+ Math.max(0, Math.min(255, b | 0)),
146
+ ];
147
+ this._buf[i * 3] = vals[i0];
148
+ this._buf[i * 3 + 1] = vals[i1];
149
+ this._buf[i * 3 + 2] = vals[i2];
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Transmit the current buffer to the strip, applying brightness scaling.
155
+ *
156
+ * Each channel is scaled: sent = stored * brightness / 255.
157
+ * @returns {Promise<void>}
158
+ */
159
+ async show() {
160
+ const bri = this._brightness;
161
+ if (bri === 255) {
162
+ await this._conn.write(Buffer.concat([this._buf, this._reset]));
163
+ return;
164
+ }
165
+ const scaled = Buffer.alloc(this._buf.length);
166
+ for (let i = 0; i < this._buf.length; i++) {
167
+ scaled[i] = (this._buf[i] * bri / 255) | 0;
168
+ }
169
+ await this._conn.write(Buffer.concat([scaled, this._reset]));
170
+ }
171
+
172
+ /**
173
+ * Shift the pixel buffer left by steps positions (wraps around).
174
+ *
175
+ * Does not transmit — call show() afterwards.
176
+ *
177
+ * @param {number} [steps=1] - Number of pixel positions to shift left.
178
+ */
179
+ rotate(steps = 1) {
180
+ steps = ((steps % this._n) + this._n) % this._n;
181
+ if (steps === 0) return;
182
+ const bytes = steps * 3;
183
+ const head = Buffer.from(this._buf.slice(0, bytes));
184
+ this._buf.copyWithin(0, bytes);
185
+ head.copy(this._buf, this._buf.length - bytes);
186
+ }
187
+
188
+ /**
189
+ * Fill every pixel with one HSV colour and send to the strip immediately.
190
+ *
191
+ * Converts HSV to RGB then calls fill().
192
+ *
193
+ * @param {number} h - Hue (0.0–1.0).
194
+ * @param {number} s - Saturation (0.0–1.0).
195
+ * @param {number} v - Value/brightness (0.0–1.0).
196
+ * @returns {Promise<void>}
197
+ */
198
+ async fill_hsv(h, s, v) {
199
+ const [r, g, b] = _hsvToRgb(h, s, v);
200
+ await this.fill(r, g, b);
201
+ }
202
+ }
203
+
204
+ module.exports = { NeoPixelRGBMinimal, NeoPixelRGBFull };
@@ -0,0 +1,218 @@
1
+ 'use strict';
2
+
3
+ const { _hsvToRgb } = require('./_color');
4
+
5
+ /**
6
+ * Shared minimal-tier logic for 4-channel (RGBW) NeoPixel-protocol LED drivers.
7
+ *
8
+ * Drives a chain of n pixels over a NeoPixel connection. Maintains an
9
+ * internal buffer in wire order; fill() writes every pixel and transmits
10
+ * immediately. Each pixel has four channels: red, green, blue, and white.
11
+ * Subclasses fix channelOrder and resetBytes for their specific chip and
12
+ * expose a public constructor(connection, n) that calls this one with
13
+ * those fixed values.
14
+ *
15
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
16
+ * @param {number} n - Number of pixels in the strip.
17
+ * @param {number[]} channelOrder - [iR, iG, iB, iW]; wire[k] = [r,g,b,w][channelOrder[k]].
18
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
19
+ */
20
+ class NeoPixelRGBWMinimal {
21
+ /**
22
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
23
+ * @param {number} n - Number of pixels in the strip.
24
+ * @param {number[]} channelOrder - [iR, iG, iB, iW] wire channel order.
25
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
26
+ */
27
+ constructor(connection, n, channelOrder, resetBytes) {
28
+ this._conn = connection;
29
+ this._n = n;
30
+ this._buf = Buffer.alloc(n * 4);
31
+ this._channelOrder = channelOrder;
32
+ this._reset = Buffer.alloc(resetBytes);
33
+ }
34
+
35
+ /**
36
+ * Fill every pixel with one colour and send to the strip immediately.
37
+ *
38
+ * Clamps each channel to [0, 255]. Stores the four channels using
39
+ * this chip's wire channel order, then appends the reset tail and
40
+ * calls connection.write(). The white channel defaults to 0.
41
+ *
42
+ * @param {number} r - Red channel (0–255).
43
+ * @param {number} g - Green channel (0–255).
44
+ * @param {number} b - Blue channel (0–255).
45
+ * @param {number} [w=0] - White channel (0–255).
46
+ * @returns {Promise<void>}
47
+ */
48
+ async fill(r, g = 0, b = 0, w = 0) {
49
+ const vals = [
50
+ Math.max(0, Math.min(255, r | 0)),
51
+ Math.max(0, Math.min(255, g | 0)),
52
+ Math.max(0, Math.min(255, b | 0)),
53
+ Math.max(0, Math.min(255, w | 0)),
54
+ ];
55
+ const [i0, i1, i2, i3] = this._channelOrder;
56
+ const w0 = vals[i0], w1 = vals[i1], w2 = vals[i2], w3 = vals[i3];
57
+ for (let i = 0; i < this._n; i++) {
58
+ this._buf[i * 4] = w0;
59
+ this._buf[i * 4 + 1] = w1;
60
+ this._buf[i * 4 + 2] = w2;
61
+ this._buf[i * 4 + 3] = w3;
62
+ }
63
+ await this._conn.write(Buffer.concat([this._buf, this._reset]));
64
+ }
65
+
66
+ /**
67
+ * Turn off all pixels (fill with all zeros and send).
68
+ *
69
+ * Equivalent to fill(0, 0, 0, 0).
70
+ * @returns {Promise<void>}
71
+ */
72
+ async off() {
73
+ await this.fill(0, 0, 0, 0);
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Shared full-tier logic for 4-channel (RGBW) NeoPixel-protocol LED drivers.
79
+ *
80
+ * Adds individual pixel addressing, explicit show(), global brightness
81
+ * scaling, buffer rotation, and HSV fill on top of NeoPixelRGBWMinimal.
82
+ * Call set_pixel() / set_pixels() to update the buffer, then show() to
83
+ * transmit; or use the inherited fill() for an immediate all-same-colour
84
+ * update.
85
+ *
86
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
87
+ * @param {number} n - Number of pixels in the strip.
88
+ * @param {number[]} channelOrder - [iR, iG, iB, iW] wire channel order.
89
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
90
+ */
91
+ class NeoPixelRGBWFull extends NeoPixelRGBWMinimal {
92
+ /**
93
+ * @param {import('../../connection/neopixel').NeoPixelConnection} connection - Configured NeoPixel connection.
94
+ * @param {number} n - Number of pixels in the strip.
95
+ * @param {number[]} channelOrder - [iR, iG, iB, iW] wire channel order.
96
+ * @param {number} resetBytes - Extra zero-bytes appended after pixel data.
97
+ */
98
+ constructor(connection, n, channelOrder, resetBytes) {
99
+ super(connection, n, channelOrder, resetBytes);
100
+ this._brightness = 255;
101
+ }
102
+
103
+ /**
104
+ * Global brightness scalar applied at show() time (0–255).
105
+ * @type {number}
106
+ */
107
+ get brightness() { return this._brightness; }
108
+ set brightness(value) {
109
+ this._brightness = Math.max(0, Math.min(255, value | 0));
110
+ }
111
+
112
+ /**
113
+ * Set one pixel in the buffer without sending.
114
+ *
115
+ * Index is clamped to [0, n-1]; each channel to [0, 255].
116
+ * Call show() to transmit. White channel defaults to 0.
117
+ *
118
+ * @param {number} index - Zero-based pixel index.
119
+ * @param {number} r - Red channel (0–255).
120
+ * @param {number} g - Green channel (0–255).
121
+ * @param {number} b - Blue channel (0–255).
122
+ * @param {number} [w=0] - White channel (0–255).
123
+ */
124
+ set_pixel(index, r, g, b, w = 0) {
125
+ index = Math.max(0, Math.min(this._n - 1, index | 0));
126
+ const vals = [
127
+ Math.max(0, Math.min(255, r | 0)),
128
+ Math.max(0, Math.min(255, g | 0)),
129
+ Math.max(0, Math.min(255, b | 0)),
130
+ Math.max(0, Math.min(255, w | 0)),
131
+ ];
132
+ const [i0, i1, i2, i3] = this._channelOrder;
133
+ this._buf[index * 4] = vals[i0];
134
+ this._buf[index * 4 + 1] = vals[i1];
135
+ this._buf[index * 4 + 2] = vals[i2];
136
+ this._buf[index * 4 + 3] = vals[i3];
137
+ }
138
+
139
+ /**
140
+ * Write a sequence of [r, g, b] or [r, g, b, w] arrays into the buffer
141
+ * starting at pixel 0.
142
+ *
143
+ * Extra entries beyond the strip length are ignored. Call show() to transmit.
144
+ * White channel defaults to 0 if arrays have only 3 elements.
145
+ *
146
+ * @param {Array<number[]>} colors - Array of [r, g, b] or [r, g, b, w] arrays.
147
+ */
148
+ set_pixels(colors) {
149
+ const [i0, i1, i2, i3] = this._channelOrder;
150
+ for (let i = 0; i < colors.length && i < this._n; i++) {
151
+ const [r, g, b, w = 0] = colors[i];
152
+ const vals = [
153
+ Math.max(0, Math.min(255, r | 0)),
154
+ Math.max(0, Math.min(255, g | 0)),
155
+ Math.max(0, Math.min(255, b | 0)),
156
+ Math.max(0, Math.min(255, w | 0)),
157
+ ];
158
+ this._buf[i * 4] = vals[i0];
159
+ this._buf[i * 4 + 1] = vals[i1];
160
+ this._buf[i * 4 + 2] = vals[i2];
161
+ this._buf[i * 4 + 3] = vals[i3];
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Transmit the current buffer to the strip, applying brightness scaling.
167
+ *
168
+ * Each channel is scaled: sent = stored * brightness / 255.
169
+ * Appends this chip's reset-byte tail before transmission.
170
+ * @returns {Promise<void>}
171
+ */
172
+ async show() {
173
+ const bri = this._brightness;
174
+ let pixels;
175
+ if (bri === 255) {
176
+ pixels = this._buf;
177
+ } else {
178
+ pixels = Buffer.alloc(this._buf.length);
179
+ for (let i = 0; i < this._buf.length; i++) {
180
+ pixels[i] = (this._buf[i] * bri / 255) | 0;
181
+ }
182
+ }
183
+ await this._conn.write(Buffer.concat([pixels, this._reset]));
184
+ }
185
+
186
+ /**
187
+ * Shift the pixel buffer left by steps positions (wraps around).
188
+ *
189
+ * Does not transmit — call show() afterwards.
190
+ *
191
+ * @param {number} [steps=1] - Number of pixel positions to shift left.
192
+ */
193
+ rotate(steps = 1) {
194
+ steps = ((steps % this._n) + this._n) % this._n;
195
+ if (steps === 0) return;
196
+ const bytes = steps * 4;
197
+ const head = Buffer.from(this._buf.slice(0, bytes));
198
+ this._buf.copyWithin(0, bytes);
199
+ head.copy(this._buf, this._buf.length - bytes);
200
+ }
201
+
202
+ /**
203
+ * Fill every pixel with one HSV colour and send to the strip immediately.
204
+ *
205
+ * Converts HSV to RGB (white=0) then calls fill().
206
+ *
207
+ * @param {number} h - Hue (0.0–1.0).
208
+ * @param {number} s - Saturation (0.0–1.0).
209
+ * @param {number} v - Value/brightness (0.0–1.0).
210
+ * @returns {Promise<void>}
211
+ */
212
+ async fill_hsv(h, s, v) {
213
+ const [r, g, b] = _hsvToRgb(h, s, v);
214
+ await this.fill(r, g, b, 0);
215
+ }
216
+ }
217
+
218
+ module.exports = { NeoPixelRGBWMinimal, NeoPixelRGBWFull };