periph 1.0.1 → 1.1.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 (56) hide show
  1. package/examples/adc_dac/hx711/complete.js +42 -0
  2. package/examples/adc_dac/hx711/demo.js +35 -0
  3. package/examples/adc_dac/hx711/minimal.js +16 -0
  4. package/examples/adc_dac/mcp4728/complete.js +41 -0
  5. package/examples/adc_dac/mcp4728/demo.js +41 -0
  6. package/examples/adc_dac/mcp4728/minimal.js +11 -0
  7. package/examples/display/pcf8576/complete.js +32 -0
  8. package/examples/display/pcf8576/demo.js +48 -0
  9. package/examples/display/pcf8576/minimal.js +18 -0
  10. package/examples/environmental/aht21/complete.js +30 -0
  11. package/examples/environmental/aht21/demo.js +44 -0
  12. package/examples/environmental/aht21/minimal.js +12 -0
  13. package/examples/environmental/bme280/complete.js +34 -0
  14. package/examples/environmental/bme280/demo.js +67 -0
  15. package/examples/environmental/bme280/minimal.js +18 -0
  16. package/examples/environmental/bme680/complete.js +37 -0
  17. package/examples/environmental/bme680/demo.js +52 -0
  18. package/examples/environmental/bme680/minimal.js +19 -0
  19. package/examples/gas/ens160/complete.js +52 -0
  20. package/examples/gas/ens160/demo.js +52 -0
  21. package/examples/gas/ens160/minimal.js +27 -0
  22. package/examples/imu/mpu6050/complete.js +56 -0
  23. package/examples/imu/mpu6050/demo.js +34 -0
  24. package/examples/imu/mpu6050/minimal.js +14 -0
  25. package/examples/light/apds9960/complete.js +62 -0
  26. package/examples/light/apds9960/demo.js +32 -0
  27. package/examples/light/apds9960/minimal.js +12 -0
  28. package/examples/memory/24aa02uid/complete.js +40 -0
  29. package/examples/memory/24aa02uid/demo.js +40 -0
  30. package/examples/memory/24aa02uid/minimal.js +12 -0
  31. package/examples/pressure/bmp280/complete.js +22 -18
  32. package/examples/pressure/bmp280/demo.js +34 -18
  33. package/examples/pressure/bmp280/minimal.js +4 -4
  34. package/package.json +1 -1
  35. package/src/chips/adc_dac/hx711.js +165 -0
  36. package/src/chips/adc_dac/mcp4728.js +284 -0
  37. package/src/chips/display/pcf8576.js +223 -0
  38. package/src/chips/environmental/aht21.js +170 -0
  39. package/src/chips/environmental/bme280.js +366 -0
  40. package/src/chips/environmental/bme680.js +489 -0
  41. package/src/chips/gas/ens160.js +280 -0
  42. package/src/chips/imu/mpu6050.js +266 -0
  43. package/src/chips/light/apds9960.js +464 -0
  44. package/src/chips/memory/_24aa02uid.js +179 -0
  45. package/src/chips/pressure/bmp280.js +154 -191
  46. package/src/transport/hx711.js +105 -0
  47. package/examples/display/.gitkeep +0 -0
  48. package/examples/environmental/.gitkeep +0 -0
  49. package/examples/imu/.gitkeep +0 -0
  50. package/examples/light/.gitkeep +0 -0
  51. package/examples/pressure/.gitkeep +0 -0
  52. package/src/chips/display/.gitkeep +0 -0
  53. package/src/chips/environmental/.gitkeep +0 -0
  54. package/src/chips/imu/.gitkeep +0 -0
  55. package/src/chips/light/.gitkeep +0 -0
  56. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,165 @@
1
+ 'use strict';
2
+
3
+ const GAIN_TO_PULSES = { 128: 25, 32: 26, 64: 27 };
4
+
5
+ /**
6
+ * HX711 24-bit ADC — minimal interface.
7
+ *
8
+ * Reads signed 24-bit ADC values using Channel A, Gain 128. No configuration
9
+ * beyond the transport is required. The first post-power-up conversion is
10
+ * discarded during construction.
11
+ */
12
+ class HX711Minimal {
13
+ /**
14
+ * @param {object} transport - Configured HX711 transport (HX711Transport).
15
+ */
16
+ constructor(transport) {
17
+ this._transport = transport;
18
+ this._transport.readRaw(25);
19
+ }
20
+
21
+ /**
22
+ * Return true if a conversion result is available (DOUT is LOW).
23
+ *
24
+ * Non-blocking.
25
+ *
26
+ * @returns {boolean} True when DOUT is LOW (data ready).
27
+ */
28
+ isReady() {
29
+ return this._transport.isReady();
30
+ }
31
+
32
+ /**
33
+ * Block until data is ready and return a signed 24-bit ADC value.
34
+ *
35
+ * Reads Channel A at Gain 128.
36
+ *
37
+ * @returns {number} Signed 24-bit ADC value (-8 388 608 to +8 388 607).
38
+ */
39
+ readRaw() {
40
+ return this._transport.readRaw(25);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * HX711 full interface — extends HX711Minimal with gain, tare, and calibration.
46
+ *
47
+ * Adds gain selection (Channel A Gain 128/64, Channel B Gain 32), multi-sample
48
+ * averaging, tare offset capture, scale factor calibration, and power management.
49
+ */
50
+ class HX711Full extends HX711Minimal {
51
+ /**
52
+ * @param {object} transport - Configured HX711 transport (HX711Transport).
53
+ */
54
+ constructor(transport) {
55
+ super(transport);
56
+ this._pulses = 25;
57
+ this._offset = 0;
58
+ this._scale = 1.0;
59
+ }
60
+
61
+ /**
62
+ * Block until data is ready and return a signed 24-bit ADC value.
63
+ *
64
+ * Uses the currently selected channel and gain.
65
+ *
66
+ * @returns {number} Signed 24-bit ADC value.
67
+ */
68
+ readRaw() {
69
+ return this._transport.readRaw(this._pulses);
70
+ }
71
+
72
+ /**
73
+ * Select the input channel and gain.
74
+ *
75
+ * Issues one dummy read to apply the new gain before returning.
76
+ *
77
+ * @param {number} gain - 128 (Channel A), 64 (Channel A), or 32 (Channel B).
78
+ * @throws {Error} If gain is not 128, 64, or 32.
79
+ */
80
+ setGain(gain) {
81
+ if (!(gain in GAIN_TO_PULSES))
82
+ throw new Error('gain must be 128, 64, or 32');
83
+ this._pulses = GAIN_TO_PULSES[gain];
84
+ this._transport.readRaw(this._pulses);
85
+ }
86
+
87
+ /**
88
+ * Return the average of multiple raw ADC readings.
89
+ * @param {number} times - Number of readings to average (default 10).
90
+ * @returns {number} Average signed 24-bit ADC value.
91
+ */
92
+ readAverage(times = 10) {
93
+ let total = 0;
94
+ for (let i = 0; i < times; i++)
95
+ total += this.readRaw();
96
+ return Math.trunc(total / times);
97
+ }
98
+
99
+ /**
100
+ * Capture the current average reading as the zero offset.
101
+ * @param {number} times - Number of readings to average (default 10).
102
+ */
103
+ tare(times = 10) {
104
+ this._offset = this.readAverage(times);
105
+ }
106
+
107
+ /**
108
+ * Return the stored tare offset.
109
+ * @returns {number} Offset captured by the last tare() call.
110
+ */
111
+ getOffset() {
112
+ return this._offset;
113
+ }
114
+
115
+ /**
116
+ * Set the calibration scale factor.
117
+ *
118
+ * Calibrate: factor = (readAverage() - getOffset()) / known_weight.
119
+ *
120
+ * @param {number} factor - Scale factor (ADC counts per unit weight).
121
+ */
122
+ setScale(factor) {
123
+ this._scale = factor;
124
+ }
125
+
126
+ /**
127
+ * Return the current calibration scale factor.
128
+ * @returns {number} Scale factor set by the last setScale() call.
129
+ */
130
+ getScale() {
131
+ return this._scale;
132
+ }
133
+
134
+ /**
135
+ * Return the calibrated weight in the units defined by the scale factor.
136
+ *
137
+ * Computes (readAverage(times) - offset) / scale.
138
+ *
139
+ * @param {number} times - Number of readings to average (default 1).
140
+ * @returns {number} Calibrated weight value.
141
+ */
142
+ readWeight(times = 1) {
143
+ return (this.readAverage(times) - this._offset) / this._scale;
144
+ }
145
+
146
+ /**
147
+ * Enter power-down mode (PD_SCK held HIGH for >60 µs).
148
+ */
149
+ powerDown() {
150
+ this._transport.powerDown();
151
+ }
152
+
153
+ /**
154
+ * Exit power-down, reset chip, discard settling conversion.
155
+ *
156
+ * Resets to Channel A, Gain 128 and discards the first post-reset conversion.
157
+ */
158
+ powerUp() {
159
+ this._transport.powerUp();
160
+ this._pulses = 25;
161
+ this._transport.readRaw(25);
162
+ }
163
+ }
164
+
165
+ module.exports = { HX711Minimal, HX711Full };
@@ -0,0 +1,284 @@
1
+ 'use strict';
2
+
3
+ const CMD_MULTI_WRITE_BASE = 0x40; // [0 1 0 0 0 DAC1 DAC0 UDAC]
4
+ const CMD_SINGLE_WRITE = 0x58; // [0 1 0 1 1 DAC1 DAC0 UDAC]
5
+ const CMD_SEQUENTIAL_BASE = 0x50; // [0 1 0 1 0 DAC1 DAC0 UDAC]
6
+ const CMD_WRITE_VREF = 0x80; // [1 0 0 X Vref_A Vref_B Vref_C Vref_D]
7
+ const CMD_WRITE_GAIN = 0xC0; // [1 1 0 X Gx_A Gx_B Gx_C Gx_D]
8
+ const CMD_WRITE_POWERDOWN = 0xA0; // [1 0 1 X ...]
9
+ const ADDR_GENERAL_CALL = 0x00;
10
+ const GC_RESET = 0x06;
11
+ const GC_SOFTWARE_UPD = 0x08;
12
+ const GC_WAKE = 0x09;
13
+
14
+ /**
15
+ * MCP4728 quad-channel 12-bit voltage-output DAC — minimal interface.
16
+ *
17
+ * Provides simple voltage output as a fraction of V_DD for any of the four
18
+ * channels (A–D) plus a convenience method to update all four channels
19
+ * simultaneously. No configuration required beyond the transport. V_REF is
20
+ * fixed at external (V_DD), gain is fixed at ×1, and power-down is off.
21
+ * EEPROM is never written by this class.
22
+ *
23
+ * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
24
+ */
25
+ class MCP4728Minimal {
26
+ /**
27
+ * @param {object} transport - Configured I²C transport pointing at the device.
28
+ */
29
+ constructor(transport) {
30
+ this._transport = transport;
31
+ }
32
+
33
+ /**
34
+ * Set one channel's DAC output as a fraction of V_DD.
35
+ * @param {number} channel - Channel index 0 (A) – 3 (D).
36
+ * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
37
+ */
38
+ set_voltage(channel, fraction) {
39
+ let f = Math.max(0.0, Math.min(1.0, fraction));
40
+ const code = Math.round(f * 4095);
41
+ this.set_raw(channel, code);
42
+ }
43
+
44
+ /**
45
+ * Set one channel's raw 12-bit DAC code.
46
+ * @param {number} channel - Channel index 0 (A) – 3 (D).
47
+ * @param {number} code - Raw 12-bit DAC code (0–4095).
48
+ */
49
+ set_raw(channel, code) {
50
+ const ch = Math.max(0, Math.min(3, channel | 0));
51
+ const c = Math.max(0, Math.min(4095, code | 0));
52
+ this._multi_write(ch, c, 0, 0, 0, 0);
53
+ }
54
+
55
+ /**
56
+ * Update all four channels simultaneously using Fast Write.
57
+ * @param {number[]} fractions - Array of 4 fractions (0.0–1.0), index 0 = A.
58
+ */
59
+ set_all(fractions) {
60
+ if (!Array.isArray(fractions) || fractions.length !== 4) {
61
+ throw new Error('fractions must have exactly 4 elements');
62
+ }
63
+ const buf = Buffer.alloc(8);
64
+ for (let i = 0; i < 4; i++) {
65
+ let f = Math.max(0.0, Math.min(1.0, fractions[i]));
66
+ const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
67
+ // Fast Write: byte1=[0 0 PD1 PD0 D11-D8] (PD=00), byte2=[D7-D0]
68
+ buf[i * 2] = (code >> 8) & 0x0F;
69
+ buf[i * 2 + 1] = code & 0xFF;
70
+ }
71
+ this._transport.write(buf);
72
+ }
73
+
74
+ _multi_write(channel, code, vref, pd, gain, udac) {
75
+ const buf = Buffer.alloc(3);
76
+ buf[0] = CMD_MULTI_WRITE_BASE | ((channel & 0x03) << 1) | (udac & 0x01);
77
+ buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
78
+ ((gain & 0x01) << 4) | ((code >> 8) & 0x0F);
79
+ buf[2] = code & 0xFF;
80
+ this._transport.write(buf);
81
+ }
82
+ }
83
+
84
+ /**
85
+ * MCP4728 full interface — extends MCP4728Minimal with EEPROM, V_REF, gain, power-down, and read-back.
86
+ *
87
+ * Adds per-channel V_REF and gain configuration, all-channel V_REF/gain/
88
+ * power-down commands, write-with-EEPROM persistence (Single and Sequential
89
+ * Write), General Call reset/wake-up/software-update, and full 24-byte
90
+ * read-back of all channel DAC input registers and EEPROM contents.
91
+ *
92
+ * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
93
+ */
94
+ class MCP4728Full extends MCP4728Minimal {
95
+ /** @type {number} Normal operation (power-down mode 0). */
96
+ static PD_NORMAL = 0;
97
+ /** @type {number} Power-down with 1 kΩ to GND (mode 1). */
98
+ static PD_1K_GND = 1;
99
+ /** @type {number} Power-down with 100 kΩ to GND (mode 2). */
100
+ static PD_100K_GND = 2;
101
+ /** @type {number} Power-down with 500 kΩ to GND (mode 3). */
102
+ static PD_500K_GND = 3;
103
+
104
+ /** @type {number} External V_DD reference. */
105
+ static VREF_EXTERNAL = 0;
106
+ /** @type {number} Internal 2.048 V reference. */
107
+ static VREF_INTERNAL = 1;
108
+
109
+ /** @type {number} Gain ×1. */
110
+ static GAIN_X1 = 0;
111
+ /** @type {number} Gain ×2. */
112
+ static GAIN_X2 = 1;
113
+
114
+ /**
115
+ * @param {object} transport - Configured I²C transport pointing at the device.
116
+ */
117
+ constructor(transport) {
118
+ super(transport);
119
+ }
120
+
121
+ /**
122
+ * Set one channel's output and persist to EEPROM (Single Write).
123
+ * @param {number} channel - Channel index 0 (A) – 3 (D).
124
+ * @param {number} fraction - Output as a fraction of the configured full-scale.
125
+ * @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
126
+ * @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
127
+ */
128
+ set_voltage_eeprom(channel, fraction, vref, gain) {
129
+ let f = Math.max(0.0, Math.min(1.0, fraction));
130
+ const code = Math.round(f * 4095);
131
+ this._single_write(channel, code, vref, 0, gain, 0);
132
+ }
133
+
134
+ /**
135
+ * Set one channel's raw 12-bit code and persist to EEPROM.
136
+ * @param {number} channel - Channel index 0 (A) – 3 (D).
137
+ * @param {number} code - Raw 12-bit DAC code (0–4095).
138
+ * @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
139
+ * @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
140
+ */
141
+ set_raw_eeprom(channel, code, vref, gain) {
142
+ const c = Math.max(0, Math.min(4095, code | 0));
143
+ this._single_write(channel, c, vref, 0, gain, 0);
144
+ }
145
+
146
+ /**
147
+ * Update all four channels and EEPROM (Sequential Write from A to D).
148
+ * @param {number[]} fractions - Array of 4 fractions (0.0–1.0).
149
+ * @param {number[]} vrefs - Array of 4 V_REF values (0/1).
150
+ * @param {number[]} gains - Array of 4 gain values (1/2).
151
+ */
152
+ set_all_eeprom(fractions, vrefs, gains) {
153
+ if (fractions.length !== 4 || vrefs.length !== 4 || gains.length !== 4) {
154
+ throw new Error('fractions, vrefs, gains must each have 4 elements');
155
+ }
156
+ const buf = Buffer.alloc(9);
157
+ // Sequential Write starting at channel 0 (A): [0 1 0 1 0 0 0 UDAC] = 0x50
158
+ buf[0] = CMD_SEQUENTIAL_BASE | 0x00;
159
+ for (let i = 0; i < 4; i++) {
160
+ let f = Math.max(0.0, Math.min(1.0, fractions[i]));
161
+ const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
162
+ const v = vrefs[i] ? 1 : 0;
163
+ const g = (gains[i] === 2) ? 1 : 0;
164
+ // Per-channel byte layout (Multi-Write format): [V_REF PD1 PD0 Gx D11-D8]
165
+ // PD bits are always 0 here (no power-down is set in this command).
166
+ buf[1 + i * 2] = ((v & 0x01) << 7) | ((g & 0x01) << 4) | ((code >> 8) & 0x0F);
167
+ buf[1 + i * 2 + 1] = code & 0xFF;
168
+ }
169
+ this._transport.write(buf);
170
+ }
171
+
172
+ /**
173
+ * Set V_REF for all four channels (volatile register only).
174
+ * @param {number} vref_a - 0 = external V_DD, 1 = internal 2.048 V.
175
+ * @param {number} vref_b
176
+ * @param {number} vref_c
177
+ * @param {number} vref_d
178
+ */
179
+ set_vref(vref_a, vref_b, vref_c, vref_d) {
180
+ const byte1 = CMD_WRITE_VREF |
181
+ ((vref_a ? 1 : 0) << 3) | ((vref_b ? 1 : 0) << 2) |
182
+ ((vref_c ? 1 : 0) << 1) | (vref_d ? 1 : 0);
183
+ this._transport.write(Buffer.from([byte1]));
184
+ }
185
+
186
+ /**
187
+ * Set gain for all four channels (volatile register only).
188
+ * @param {number} gain_a - 1 = ×1, 2 = ×2.
189
+ * @param {number} gain_b
190
+ * @param {number} gain_c
191
+ * @param {number} gain_d
192
+ */
193
+ set_gain(gain_a, gain_b, gain_c, gain_d) {
194
+ const byte1 = CMD_WRITE_GAIN |
195
+ ((gain_a === 2 ? 1 : 0) << 3) | ((gain_b === 2 ? 1 : 0) << 2) |
196
+ ((gain_c === 2 ? 1 : 0) << 1) | (gain_d === 2 ? 1 : 0);
197
+ this._transport.write(Buffer.from([byte1]));
198
+ }
199
+
200
+ /**
201
+ * Set power-down mode for all four channels (volatile register only).
202
+ * @param {number} pd_a - 0–3.
203
+ * @param {number} pd_b
204
+ * @param {number} pd_c
205
+ * @param {number} pd_d
206
+ */
207
+ set_power_down(pd_a, pd_b, pd_c, pd_d) {
208
+ const byte1 = CMD_WRITE_POWERDOWN |
209
+ (((pd_a >> 1) & 0x01) << 4) | ((pd_a & 0x01) << 3) |
210
+ (((pd_b >> 1) & 0x01) << 2) | ((pd_b & 0x01) << 1);
211
+ const byte2 = (((pd_c >> 1) & 0x01) << 6) | ((pd_c & 0x01) << 5) |
212
+ (((pd_d >> 1) & 0x01) << 4) | ((pd_d & 0x01) << 3);
213
+ this._transport.write(Buffer.from([byte1, byte2]));
214
+ }
215
+
216
+ /**
217
+ * Read all four channels' DAC input registers and EEPROM contents.
218
+ * @returns {{channel: object[], eeprom_ready: boolean}} Per-channel code, vref, gain, power_down, eeprom_*.
219
+ */
220
+ read() {
221
+ const buf = this._transport.read(24);
222
+ const result = { channel: [], eeprom_ready: !!(buf[0] & 0x80) };
223
+ for (let i = 0; i < 4; i++) {
224
+ const b = i * 3;
225
+ result.channel.push({
226
+ code: ((buf[b + 1] & 0x0F) << 8) | buf[b + 2],
227
+ vref: (buf[b + 1] >> 7) & 0x01,
228
+ gain: ((buf[b + 1] >> 4) & 0x01) ? 2 : 1,
229
+ power_down: (buf[b + 1] >> 5) & 0x03,
230
+ });
231
+ }
232
+ for (let i = 0; i < 4; i++) {
233
+ const b = 12 + i * 3;
234
+ result.channel[i].eeprom_code = ((buf[b + 1] & 0x0F) << 8) | buf[b + 2];
235
+ result.channel[i].eeprom_vref = (buf[b + 1] >> 7) & 0x01;
236
+ result.channel[i].eeprom_gain = ((buf[b + 1] >> 4) & 0x01) ? 2 : 1;
237
+ result.channel[i].eeprom_power_down = (buf[b + 1] >> 5) & 0x03;
238
+ }
239
+ return result;
240
+ }
241
+
242
+ /**
243
+ * Check if the EEPROM write is complete (RDY/BSY = 1).
244
+ * @returns {boolean}
245
+ */
246
+ is_eeprom_ready() {
247
+ const buf = this._transport.read(1);
248
+ return !!(buf[0] & 0x80);
249
+ }
250
+
251
+ /**
252
+ * Send General Call Software Update (0x00, 0x08) to latch all V_OUT.
253
+ */
254
+ software_update() {
255
+ this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_SOFTWARE_UPD]));
256
+ }
257
+
258
+ /**
259
+ * Send General Call Wake-Up (0x00, 0x09) to clear all PD bits.
260
+ */
261
+ wake_up() {
262
+ this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
263
+ }
264
+
265
+ /**
266
+ * Send General Call Reset (0x00, 0x06) to reload EEPROM into all DAC registers.
267
+ */
268
+ reset() {
269
+ this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
270
+ }
271
+
272
+ _single_write(channel, code, vref, pd, gain, udac) {
273
+ const ch = Math.max(0, Math.min(3, channel | 0));
274
+ const c = Math.max(0, Math.min(4095, code | 0));
275
+ const buf = Buffer.alloc(3);
276
+ buf[0] = CMD_SINGLE_WRITE | ((ch & 0x03) << 1) | (udac & 0x01);
277
+ buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
278
+ ((gain & 0x01) << 4) | ((c >> 8) & 0x0F);
279
+ buf[2] = c & 0xFF;
280
+ this._transport.write(buf);
281
+ }
282
+ }
283
+
284
+ module.exports = { MCP4728Minimal, MCP4728Full };
@@ -0,0 +1,223 @@
1
+ 'use strict';
2
+
3
+ const _ADDR_SA0_LOW = 0x38;
4
+ const _ADDR_SA0_HIGH = 0x39;
5
+
6
+ const _CMD_MODE_SET = 0x40;
7
+ const _CMD_LOAD_PTR = 0x00;
8
+ const _CMD_DEVICE_SELECT = 0x60;
9
+ const _CMD_BANK_SELECT = 0x78;
10
+ const _CMD_BLINK_SELECT = 0x70;
11
+
12
+ const _MODE_1_4 = 0x00;
13
+ const _MODE_STATIC = 0x01;
14
+ const _MODE_1_2 = 0x02;
15
+ const _MODE_1_3 = 0x03;
16
+
17
+ const _BIAS_1_3 = 0x00;
18
+ const _BIAS_1_2 = 0x04;
19
+
20
+ const _DISPLAY_OFF = 0x00;
21
+ const _DISPLAY_ON = 0x08;
22
+
23
+ const _SEVEN_SEG = [
24
+ 0xED, 0x60, 0xA7, 0xE3, 0x6A,
25
+ 0xCB, 0xCF, 0xE0, 0xEF, 0xEB,
26
+ ];
27
+
28
+ /**
29
+ * PCF8576 40x4 universal LCD segment driver — minimal interface.
30
+ *
31
+ * Drives a single 7-segment LCD display (static or 1:4 multiplex) out of
32
+ * the box. The chip is write-only — the host never reads back. I2C address
33
+ * is 0x38 (SA0 = VSS) or 0x39 (SA0 = VDD).
34
+ *
35
+ * Default: 1:4 multiplex drive mode, 1/3 bias, display enabled, and a
36
+ * 7-segment digit lookup table for the default multiplex mode.
37
+ *
38
+ * @param {object} transport - Configured I2C transport pointing at the device.
39
+ */
40
+ class PCF8576Minimal {
41
+ constructor(transport) {
42
+ this._transport = transport;
43
+ this._backplanes = 4;
44
+ this._clear();
45
+ }
46
+
47
+ _cmdMode(enable, bias, mode) {
48
+ return _CMD_MODE_SET | (enable ? _DISPLAY_ON : _DISPLAY_OFF) | bias | mode;
49
+ }
50
+
51
+ _sendCommands(cmds) {
52
+ const out = [];
53
+ for (let i = 0; i < cmds.length - 1; i++) {
54
+ out.push(0x80 | (cmds[i] & 0x7F));
55
+ }
56
+ out.push(cmds[cmds.length - 1] & 0x7F);
57
+ this._transport.write(Buffer.from(out));
58
+ }
59
+
60
+ _sendCommandsWithData(cmd, data) {
61
+ const out = [cmd & 0x7F, ...data];
62
+ this._transport.write(Buffer.from(out));
63
+ }
64
+
65
+ _clear() {
66
+ this._sendCommands([this._cmdMode(true, _BIAS_1_3, _MODE_1_4)]);
67
+ const zeros = new Array(20).fill(0);
68
+ this._sendCommandsWithData(_CMD_LOAD_PTR, zeros);
69
+ }
70
+
71
+ /**
72
+ * Zero all 40 columns of display RAM; all segments off.
73
+ */
74
+ clear() {
75
+ this._clear();
76
+ }
77
+
78
+ /**
79
+ * Set the data pointer to {@param address} and write raw data bytes.
80
+ *
81
+ * @param {number} address - RAM column address, 0-39.
82
+ * @param {Buffer|Uint8Array|number[]} data - Bytes to write to display RAM.
83
+ */
84
+ writeRaw(address, data) {
85
+ if (address < 0 || address > 39) {
86
+ throw new RangeError('address must be in 0..39');
87
+ }
88
+ if (!data || data.length === 0) return;
89
+ this._sendCommandsWithData(_CMD_LOAD_PTR | (address & 0x3F), Array.from(data));
90
+ }
91
+
92
+ /**
93
+ * Write one 7-segment byte at column {@param position} * 2.
94
+ *
95
+ * @param {number} position - Digit index, 0-19. Maps to RAM address position * 2.
96
+ * @param {number} segments - 7-segment byte (a/c/b/DP/f/e/g/d packed, MSB-first).
97
+ * Add 0x10 to set the decimal point.
98
+ */
99
+ setDigit7seg(position, segments) {
100
+ if (position < 0 || position > 19) {
101
+ throw new RangeError('position must be in 0..19');
102
+ }
103
+ this.writeRaw(position * 2, [segments & 0xFF]);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * PCF8576 full interface — extends PCF8576Minimal with drive mode, bias, and blink control.
109
+ *
110
+ * Adds the ability to switch drive modes (static, 1:2, 1:3, 1:4 multiplex),
111
+ * change bias (1:2 or 1/3), configure blinking, select RAM banks for
112
+ * static and 1:2 multiplex use, and change the device subaddress counter
113
+ * for cascaded displays.
114
+ *
115
+ * @param {object} transport - Configured I2C transport pointing at the device.
116
+ */
117
+ class PCF8576Full extends PCF8576Minimal {
118
+ static BLINK_OFF = 0;
119
+ static BLINK_2_HZ = 1;
120
+ static BLINK_1_HZ = 2;
121
+ static BLINK_0_5_HZ = 3;
122
+
123
+ static BIAS_1_3 = 0;
124
+ static BIAS_1_2 = 1;
125
+
126
+ static BACKPLANES_1 = 1;
127
+ static BACKPLANES_2 = 2;
128
+ static BACKPLANES_3 = 3;
129
+ static BACKPLANES_4 = 4;
130
+
131
+ static BANK_0 = 0;
132
+ static BANK_1 = 1;
133
+
134
+ constructor(transport) {
135
+ super(transport);
136
+ this._enabled = true;
137
+ this._bias = PCF8576Full.BIAS_1_3;
138
+ }
139
+
140
+ _modeCode(backplanes) {
141
+ switch (backplanes) {
142
+ case PCF8576Full.BACKPLANES_1: return _MODE_STATIC;
143
+ case PCF8576Full.BACKPLANES_2: return _MODE_1_2;
144
+ case PCF8576Full.BACKPLANES_3: return _MODE_1_3;
145
+ case PCF8576Full.BACKPLANES_4: return _MODE_1_4;
146
+ default: return _MODE_1_4;
147
+ }
148
+ }
149
+
150
+ _applyMode() {
151
+ const biasBits = (this._bias === PCF8576Full.BIAS_1_2) ? _BIAS_1_2 : _BIAS_1_3;
152
+ this._sendCommands([this._cmdMode(this._enabled, biasBits, this._modeCode(this._backplanes))]);
153
+ }
154
+
155
+ /**
156
+ * Turn the display on (E = 1). RAM contents are preserved.
157
+ */
158
+ enable() {
159
+ this._enabled = true;
160
+ this._applyMode();
161
+ }
162
+
163
+ /**
164
+ * Blank the display output (E = 0). RAM contents are preserved.
165
+ */
166
+ disable() {
167
+ this._enabled = false;
168
+ this._applyMode();
169
+ }
170
+
171
+ /**
172
+ * Reconfigure drive mode and bias at runtime.
173
+ *
174
+ * @param {number} backplanes - Number of backplanes: 1 (static), 2 (1:2), 3 (1:3), 4 (1:4).
175
+ * @param {number} [bias=0] - 0 = 1/3 bias, 1 = 1/2 bias.
176
+ */
177
+ setMode(backplanes, bias = 0) {
178
+ this._backplanes = backplanes;
179
+ this._bias = bias;
180
+ this._applyMode();
181
+ }
182
+
183
+ /**
184
+ * Set the blink frequency.
185
+ *
186
+ * @param {number} frequency - 0 = off, 1 = ~2 Hz, 2 = ~1 Hz, 3 = ~0.5 Hz.
187
+ * @param {boolean} [alternateBank=false] - Alternate-RAM-bank blinking (static/1:2 only).
188
+ */
189
+ setBlink(frequency, alternateBank = false) {
190
+ if (frequency < 0 || frequency > 3) {
191
+ throw new RangeError('frequency must be in 0..3');
192
+ }
193
+ const ab = alternateBank ? 0x04 : 0x00;
194
+ this._sendCommands([_CMD_BLINK_SELECT | ab | (frequency & 0x03)]);
195
+ }
196
+
197
+ /**
198
+ * Select the active RAM bank.
199
+ *
200
+ * @param {number} inputBank - 0 (rows 0-1) or 1 (rows 2-3).
201
+ * @param {number} outputBank - 0 (rows 0-1) or 1 (rows 2-3).
202
+ */
203
+ setBank(inputBank, outputBank) {
204
+ this._sendCommands([_CMD_BANK_SELECT | ((inputBank & 1) << 1) | (outputBank & 1)]);
205
+ }
206
+
207
+ /**
208
+ * Change the subaddress counter for cascaded displays.
209
+ *
210
+ * @param {number} subaddress - 0-7; must match the A0/A1/A2 pin state.
211
+ */
212
+ deviceSelect(subaddress) {
213
+ if (subaddress < 0 || subaddress > 7) {
214
+ throw new RangeError('subaddress must be in 0..7');
215
+ }
216
+ this._sendCommands([_CMD_DEVICE_SELECT | (subaddress & 0x07)]);
217
+ }
218
+ }
219
+
220
+ PCF8576Minimal.SEVEN_SEG = _SEVEN_SEG;
221
+ PCF8576Full.SEVEN_SEG = _SEVEN_SEG;
222
+
223
+ module.exports = { PCF8576Minimal, PCF8576Full };