periph 1.3.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/es8311/complete.js +80 -0
- package/examples/adc_dac/es8311/demo.js +59 -0
- package/examples/adc_dac/es8311/minimal.js +18 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/adc_dac/mcp4xxx/complete.js +97 -0
- package/examples/adc_dac/mcp4xxx/demo.js +76 -0
- package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/imu/qmi8658a/complete.js +134 -0
- package/examples/imu/qmi8658a/demo.js +81 -0
- package/examples/imu/qmi8658a/minimal.js +22 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/motor/tmc2130/complete.js +138 -0
- package/examples/motor/tmc2130/demo.js +96 -0
- package/examples/motor/tmc2130/minimal.js +30 -0
- package/examples/motor/tmc2208/complete.js +92 -0
- package/examples/motor/tmc2208/demo.js +85 -0
- package/examples/motor/tmc2208/minimal.js +25 -0
- package/examples/motor/tmc2209/complete.js +106 -0
- package/examples/motor/tmc2209/demo.js +97 -0
- package/examples/motor/tmc2209/minimal.js +25 -0
- package/examples/motor/tmc5160/complete.js +205 -0
- package/examples/motor/tmc5160/demo.js +104 -0
- package/examples/motor/tmc5160/minimal.js +35 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/power/axp2101/complete.js +135 -0
- package/examples/power/axp2101/demo.js +68 -0
- package/examples/power/axp2101/minimal.js +24 -0
- package/examples/rtc/pcf85063a/complete.js +68 -0
- package/examples/rtc/pcf85063a/demo.js +76 -0
- package/examples/rtc/pcf85063a/minimal.js +20 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/es8311.js +621 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/adc_dac/mcp4xxx.js +982 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/imu/qmi8658a.js +1011 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/motor/tmc2130.js +537 -0
- package/src/chips/motor/tmc2208.js +490 -0
- package/src/chips/motor/tmc2209.js +565 -0
- package/src/chips/motor/tmc5160.js +900 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/power/axp2101.js +968 -0
- package/src/chips/rtc/pcf85063a.js +482 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/connection/uart.js +9 -0
- package/src/connection/uart_mock.js +65 -0
- package/src/discovery/registry.js +85 -8
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca9536Full } = require('periph/src/chips/io_expander/pca9536');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x41); // Create I2C connection, (bus, addr=0x41)
|
|
7
|
+
const chip = new Pca9536Full(connection); // Create PCA9536 full driver, (connection, addr=0x41)
|
|
8
|
+
// writes the reset values; all pins start as inputs
|
|
9
|
+
await chip.ready;
|
|
10
|
+
|
|
11
|
+
// --- Pin proxy — async API ---
|
|
12
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
13
|
+
// P0 as output; all four pins are bidirectional
|
|
14
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
15
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
// clears the OUTPUT bit via the shadow
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads INPUT (actual pin level)
|
|
19
|
+
console.log('P0 level:', v);
|
|
20
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
21
|
+
// writes CONFIG; OUTPUT is latched first for outputs
|
|
22
|
+
|
|
23
|
+
// --- Port-level operations ---
|
|
24
|
+
await chip.writePort(0, 0b00000101); // Write all 4 pins, (port=0, mask) → Promise<void>
|
|
25
|
+
// whole-port OUTPUT write
|
|
26
|
+
const port = await chip.readPort(); // Read all 4 pins, (port=0) → Promise<int bitmask>
|
|
27
|
+
// INPUT register
|
|
28
|
+
console.log('PORT=0x' + port.toString(16).padStart(1, '0'));
|
|
29
|
+
|
|
30
|
+
// --- Configuration ---
|
|
31
|
+
await chip.configurePolarity(0x00); // Configure input polarity, (mask) → Promise<void>
|
|
32
|
+
// 1 bits invert the INPUT read
|
|
33
|
+
await chip.setDirection(0x0C); // Set whole-port direction, (mask) → Promise<void>
|
|
34
|
+
// P0–P1 outputs, P2–P3 inputs; OUTPUT is written first
|
|
35
|
+
const dir = chip.getDirection(); // Read direction shadow, () → int
|
|
36
|
+
// 1 = input, 0 = output
|
|
37
|
+
console.log('CONFIG=0x' + dir.toString(16).padStart(1, '0'));
|
|
38
|
+
const config = await chip.readRegister(0x03); // Read raw register, (reg) → Promise<int>
|
|
39
|
+
// escape hatch for debugging
|
|
40
|
+
console.log('CONFIG=0x' + config.toString(16).padStart(1, '0'));
|
|
41
|
+
await chip.writeRegister(0x02, 0x04); // Write raw register, (reg, value) → Promise<void>
|
|
42
|
+
// POLARITY: invert P2
|
|
43
|
+
|
|
44
|
+
// --- Change detection (polling) ---
|
|
45
|
+
chip.watchChanges((changed) => { // Start polling for changes, (callback, intervalS=0.005) → void
|
|
46
|
+
console.log('changed=0x' + changed.toString(16)); // no INT pin exists; a timer polls INPUT
|
|
47
|
+
});
|
|
48
|
+
const p2 = chip.pin(2, 'in');
|
|
49
|
+
await p2.watch((pin) => console.log('P2 changed'), 'change'); // Watch a pin, (handler, trigger='change') → Promise<void>
|
|
50
|
+
// edge filtered in software; all pins share one poll
|
|
51
|
+
const changed = await chip.pollChanges(); // Read INPUT, get changed mask, () → Promise<int>
|
|
52
|
+
// XOR with the last INPUT value
|
|
53
|
+
console.log('changed=0x' + changed.toString(16));
|
|
54
|
+
await p2.unwatch(); // Stop watching, () → Promise<void>
|
|
55
|
+
// drops the handler
|
|
56
|
+
chip.unwatchChanges(); // Stop polling, () → void
|
|
57
|
+
// clears the timer
|
|
58
|
+
await chip.reset(); // Restore reset values, () → Promise<void>
|
|
59
|
+
// OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF
|
|
60
|
+
await connection.close();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
main();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* PCA9536 demo — blinking LEDs with polled button input.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* P0–P1: two active-low LEDs (anode → VCC via 220 Ω, cathode → P; 0 lights the LED, sinks up to 25 mA)
|
|
7
|
+
* P2–P3: two push buttons (pin → GND when pressed); the chip's built-in weak
|
|
8
|
+
* pull-up holds an open input high, so no external resistor is needed
|
|
9
|
+
*
|
|
10
|
+
* The LEDs blink alternately. The chip has no INT pin, so a 5 ms polling
|
|
11
|
+
* timer reports button changes; the callback prints the changed-pin mask and
|
|
12
|
+
* the port state and holds the LED matching the pressed button (P2→P0, P3→P1)
|
|
13
|
+
* for one second.
|
|
14
|
+
*/
|
|
15
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
16
|
+
const { Pca9536Full } = require('periph/src/chips/io_expander/pca9536');
|
|
17
|
+
|
|
18
|
+
async function main() {
|
|
19
|
+
// --- Initialise bus and driver ---
|
|
20
|
+
const connection = new I2CConnection(1, 0x41); // Create I2C connection, (bus, addr=0x41)
|
|
21
|
+
const chip = new Pca9536Full(connection); // Create PCA9536 full driver, (connection, addr=0x41)
|
|
22
|
+
await chip.ready;
|
|
23
|
+
|
|
24
|
+
// --- Configure LEDs and buttons ---
|
|
25
|
+
// P0–P1 are outputs (LED off = 1); P2–P3 stay inputs, a pressed button reads 0.
|
|
26
|
+
await chip.writePort(0, 0x0F); // Turn all LEDs off, (port=0, mask) → Promise<void>
|
|
27
|
+
await chip.setDirection(0x0C); // P0–P1 outputs, P2–P3 inputs, (mask) → Promise<void>
|
|
28
|
+
|
|
29
|
+
// --- Watch input changes ---
|
|
30
|
+
// The polling timer compares INPUT with the previous value; the callback
|
|
31
|
+
// reports the changed mask, then holds the matching LED.
|
|
32
|
+
let holdUntil = 0;
|
|
33
|
+
let holdMask = 0x03;
|
|
34
|
+
chip.watchChanges(async (changed) => { // Start polling for changes, (callback, intervalS=0.005) → void
|
|
35
|
+
const port = await chip.readPort(); // Read all 4 pins, (port=0) → Promise<int bitmask>
|
|
36
|
+
console.log('button changed=0x' + changed.toString(16) + ' port=0x' + port.toString(16));
|
|
37
|
+
holdMask = ~((changed >> 2) & 0x03) & 0x03;
|
|
38
|
+
holdUntil = Date.now() + 1000;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// --- Blink loop ---
|
|
42
|
+
let step = 0;
|
|
43
|
+
console.log('Running — press buttons P2–P3 to hold the matching LED');
|
|
44
|
+
const timer = setInterval(async () => {
|
|
45
|
+
const leds = Date.now() < holdUntil ? holdMask : (step % 2 === 0 ? 0x02 : 0x01);
|
|
46
|
+
await chip.writePort(0, 0x0C | leds); // Write all 4 pins, (port=0, mask) → Promise<void>
|
|
47
|
+
step++;
|
|
48
|
+
if (step >= 40) {
|
|
49
|
+
clearInterval(timer);
|
|
50
|
+
chip.unwatchChanges(); // Stop polling, () → void
|
|
51
|
+
await chip.writePort(0, 0x0F); // Turn all LEDs off, (port=0, mask) → Promise<void>
|
|
52
|
+
await connection.close();
|
|
53
|
+
}
|
|
54
|
+
}, 200);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
main();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca9536Minimal } = require('periph/src/chips/io_expander/pca9536');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x41); // Create I2C connection, (bus, addr=0x41)
|
|
7
|
+
const chip = new Pca9536Minimal(connection); // Create PCA9536 driver, (connection, addr=0x41)
|
|
8
|
+
|
|
9
|
+
const p2 = chip.pin(2, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
10
|
+
const p1 = chip.pin(1, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(); // Read all 4 pins, (port=0) → Promise<int bitmask>
|
|
14
|
+
const level = await p2.read(); // Read pin, () → Promise<int 0|1>
|
|
15
|
+
await p1.write(level); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('PORT=0x' + port.toString(16).padStart(1, '0') + ' P2=' + level);
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca9539Full } = require('periph/src/chips/io_expander/pca9539');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x74); // Create I2C connection, (bus, addr=0x74)
|
|
7
|
+
const chip = new Pca9539Full(connection); // Create PCA9539 full driver, (connection, addr=0x74)
|
|
8
|
+
// writes the reset values; all 16 pins start as inputs
|
|
9
|
+
await chip.ready;
|
|
10
|
+
|
|
11
|
+
// --- Pin proxy — async API ---
|
|
12
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
13
|
+
// P00 as output; every pin is bidirectional
|
|
14
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
15
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
// clears the OUTPUT bit via the shadow
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads INPUT (actual pin level)
|
|
19
|
+
console.log('P0 level:', v);
|
|
20
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
21
|
+
// writes CONFIG; OUTPUT is latched first for outputs
|
|
22
|
+
|
|
23
|
+
// --- Port-level operations ---
|
|
24
|
+
await chip.writePort(0, 0b00001111); // Write port 0, (port, mask) → Promise<void>
|
|
25
|
+
// single-port OUTPUT write
|
|
26
|
+
await chip.writeAll(0xA50F); // Write all 16 pins, (value) → Promise<void>
|
|
27
|
+
// one two-byte write at OUTPUT0 (pair toggling)
|
|
28
|
+
const port = await chip.readPort(1); // Read port 1, (port) → Promise<int bitmask>
|
|
29
|
+
// INPUT1 register
|
|
30
|
+
console.log('PORT1=0x' + port.toString(16).padStart(2, '0'));
|
|
31
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
32
|
+
// one two-byte read at INPUT0
|
|
33
|
+
console.log('ALL=0x' + all.toString(16).padStart(4, '0'));
|
|
34
|
+
|
|
35
|
+
// --- Configuration ---
|
|
36
|
+
await chip.configurePolarity(0x0000); // Configure input polarity, (mask) → Promise<void>
|
|
37
|
+
// 1 bits invert the INPUT read
|
|
38
|
+
await chip.setDirection(0xFF00); // Set whole-device direction, (mask) → Promise<void>
|
|
39
|
+
// port 0 outputs, port 1 inputs; OUTPUT is written first
|
|
40
|
+
const dir = chip.getDirection(); // Read direction shadow, () → int
|
|
41
|
+
// 1 = input, 0 = output
|
|
42
|
+
console.log('CONFIG=0x' + dir.toString(16).padStart(4, '0'));
|
|
43
|
+
const config = await chip.readRegister(0x07); // Read raw register, (reg) → Promise<int>
|
|
44
|
+
// escape hatch for debugging
|
|
45
|
+
console.log('CONFIG1=0x' + config.toString(16).padStart(2, '0'));
|
|
46
|
+
await chip.writeRegister(0x05, 0x10); // Write raw register, (reg, value) → Promise<void>
|
|
47
|
+
// POLARITY1: invert P14
|
|
48
|
+
|
|
49
|
+
// --- Interrupts ---
|
|
50
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
51
|
+
console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
|
|
52
|
+
});
|
|
53
|
+
const p12 = chip.pin(12, 'in');
|
|
54
|
+
await p12.watch((pin) => console.log('P14 changed'), 'change');// Watch a pin, (handler, trigger='change') → Promise<void>
|
|
55
|
+
// edge filtered in software; all pins share INT
|
|
56
|
+
const changed = await chip.pollInterrupt(); // Read INPUT0/1, get changed mask, () → Promise<int>
|
|
57
|
+
// XOR with the last INPUT values
|
|
58
|
+
console.log('changed=0x' + changed.toString(16));
|
|
59
|
+
await p12.unwatch(); // Stop watching, () → Promise<void>
|
|
60
|
+
// drops the handler
|
|
61
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
62
|
+
// stops delivery
|
|
63
|
+
await chip.reset(); // Restore reset values, () → Promise<void>
|
|
64
|
+
// OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF (both ports)
|
|
65
|
+
await connection.close();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
main();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* PCA9539 demo — LED scanner with interrupt-driven button input.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* P00–P07: eight active-low LEDs (anode → VCC via 220 Ω, cathode → P; 0 lights the LED)
|
|
7
|
+
* P10–P17: eight push buttons (pin → GND when pressed) with external pull-ups
|
|
8
|
+
* (the chip has no internal pull-ups)
|
|
9
|
+
*
|
|
10
|
+
* Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
|
|
11
|
+
* callback prints the 16-bit changed-pin mask and the pin state and holds the
|
|
12
|
+
* LED matching the pressed button (P10→P00, …) for one second.
|
|
13
|
+
*/
|
|
14
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
15
|
+
const { Pca9539Full } = require('periph/src/chips/io_expander/pca9539');
|
|
16
|
+
|
|
17
|
+
async function main() {
|
|
18
|
+
// --- Initialise bus and driver ---
|
|
19
|
+
const connection = new I2CConnection(1, 0x74); // Create I2C connection, (bus, addr=0x74)
|
|
20
|
+
const chip = new Pca9539Full(connection); // Create PCA9539 full driver, (connection, addr=0x74)
|
|
21
|
+
await chip.ready;
|
|
22
|
+
|
|
23
|
+
// --- Configure LEDs and buttons ---
|
|
24
|
+
// Port 0 holds the LED outputs (all LEDs off = 1); port 1 stays input, a pressed button
|
|
25
|
+
// reads 0 — polarity inversion makes it read 1.
|
|
26
|
+
await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
|
|
27
|
+
await chip.setDirection(0xFF00); // Port 0 outputs, port 1 inputs, (mask) → Promise<void>
|
|
28
|
+
await chip.configurePolarity(0xFF00); // Invert port 1 inputs, (mask) → Promise<void>
|
|
29
|
+
|
|
30
|
+
// --- Subscribe to input changes ---
|
|
31
|
+
// Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
|
|
32
|
+
// mask, then holds the matching LED.
|
|
33
|
+
let holdUntil = 0;
|
|
34
|
+
let holdMask = 0xFF;
|
|
35
|
+
await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
36
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
37
|
+
console.log('button changed=0x' + changed.toString(16) + ' all=0x' + all.toString(16));
|
|
38
|
+
holdMask = ~((changed >> 8) & 0xFF) & 0xFF;
|
|
39
|
+
holdUntil = Date.now() + 1000;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// --- Scanner loop ---
|
|
43
|
+
let step = 0;
|
|
44
|
+
console.log('Running — press buttons P10–P17 to hold the matching LED');
|
|
45
|
+
const timer = setInterval(async () => {
|
|
46
|
+
const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 8)) & 0xFF);
|
|
47
|
+
await chip.writeAll(0xFF00 | leds); // Write all 16 pins, (value) → Promise<void>
|
|
48
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
49
|
+
console.log('pos=' + (step % 8) + ' all=0x' + all.toString(16).padStart(4, '0'));
|
|
50
|
+
step++;
|
|
51
|
+
if (step >= 80) {
|
|
52
|
+
clearInterval(timer);
|
|
53
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
54
|
+
await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
|
|
55
|
+
await connection.close();
|
|
56
|
+
}
|
|
57
|
+
}, 100);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
main();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca9539Minimal } = require('periph/src/chips/io_expander/pca9539');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x74); // Create I2C connection, (bus, addr=0x74)
|
|
7
|
+
const chip = new Pca9539Minimal(connection); // Create PCA9539 driver, (connection, addr=0x74)
|
|
8
|
+
|
|
9
|
+
const p0 = chip.pin(0, 'in'); // Get pin proxy P00, (n, direction='in') → Pin
|
|
10
|
+
const p15 = chip.pin(15, 'out'); // Get pin proxy P17, (n, direction='out') → Pin
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(1); // Read the 8 pins of port 1, (port) → Promise<int bitmask>
|
|
14
|
+
const level = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
15
|
+
await p15.write(level); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('PORT1=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Complete example: exercise every method in the public API.
|
|
4
|
+
// Reads the illuminance and the raw exponent / mantissa, switches between
|
|
5
|
+
// automatic and manual ranging and between the measurement modes, programs
|
|
6
|
+
// the interrupt window and delay and handles one interrupt.
|
|
7
|
+
|
|
8
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
9
|
+
const { MAX44009Full } = require('periph/src/chips/light/max44009');
|
|
10
|
+
|
|
11
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
15
|
+
const connection = new I2CConnection(bus, MAX44009Full.I2C_ADDRESS);
|
|
16
|
+
const sensor = new MAX44009Full(connection); // Create MAX44009 Full driver, (connection)
|
|
17
|
+
await sensor.init(); // Run initialization sequence, () → void (writes the reset state, waits 0.9 s)
|
|
18
|
+
|
|
19
|
+
console.log(await sensor.lux()); // Read illuminance, () → float lux
|
|
20
|
+
// 2^E × M × 0.045; 188006.4 on overrange
|
|
21
|
+
console.log(await sensor.readRaw()); // Read exponent and mantissa, () → {exponent, mantissa, overrange}
|
|
22
|
+
// one consistent sample: LUX_HIGH, LUX_LOW, LUX_HIGH
|
|
23
|
+
console.log(sensor.isOverrange()); // Query the last reading, () → bool
|
|
24
|
+
// true if the last exponent field was 15
|
|
25
|
+
console.log(sensor.resolution()); // Lux per LSB, () → float lux
|
|
26
|
+
// 2^E × 0.045 for the last reading
|
|
27
|
+
|
|
28
|
+
await sensor.setContinuous(true); // Select measurement mode, (continuous bool) → void
|
|
29
|
+
// true: a new measurement as soon as one finishes
|
|
30
|
+
await sensor.setManual(100, false); // Select manual range, (integrationMs, divideBy8=false) → void
|
|
31
|
+
// nearest of 800/400/200/100/50/25/12.5/6.25 ms
|
|
32
|
+
console.log(await sensor.getConfig()); // Read CONFIG, () → {continuous, manual, divideBy8, integrationMs}
|
|
33
|
+
await sensor.setAutomatic(); // Select automatic range, () → void
|
|
34
|
+
// the chip chooses the current divider and integration time
|
|
35
|
+
await sensor.setContinuous(false); // Back to one measurement per 800 ms, (continuous bool) → void
|
|
36
|
+
|
|
37
|
+
await sensor.setThresholds(50.0, 500.0); // Set the interrupt window, (lowLux, highLux) → void
|
|
38
|
+
// 4 mantissa MSBs: about 1/16 to 1/8 of the value
|
|
39
|
+
console.log(await sensor.getThresholds()); // Read the window, () → {lowLux, highLux}
|
|
40
|
+
await sensor.setThresholdDelay(2.0); // Set the threshold delay, (seconds) → void
|
|
41
|
+
// 0–25.5 s in 0.1 s steps
|
|
42
|
+
console.log(await sensor.getThresholdDelay()); // Read the delay, () → float s
|
|
43
|
+
|
|
44
|
+
await sensor.enableInterrupt(); // Enable the interrupt, () → void
|
|
45
|
+
// INT_ENABLE = 1
|
|
46
|
+
await sensor.onInterrupt((status) => console.log('interrupt', status)); // Subscribe, (callback(status), {intPin}) → void
|
|
47
|
+
// falling edge on INT, or a 100 ms poll without a pin
|
|
48
|
+
await sleep(10000);
|
|
49
|
+
console.log(await sensor.pollInterrupt()); // Read and clear INT_STATUS, () → int
|
|
50
|
+
// bit 0 = SOURCE_THRESHOLD
|
|
51
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
52
|
+
await sensor.disableInterrupt(); // Disable the interrupt, () → void
|
|
53
|
+
// INT_ENABLE = 0; also clears a pending status
|
|
54
|
+
|
|
55
|
+
await connection.close();
|
|
56
|
+
})();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Demo: an adaptive backlight controller. Arms an interrupt window of +-30 %
|
|
4
|
+
// around the current illuminance; whenever the light leaves the window for 2 s
|
|
5
|
+
// the callback maps the new level logarithmically to a backlight level of
|
|
6
|
+
// 10-100 %, re-centres the window and re-arms it. Prints the exponent / mantissa
|
|
7
|
+
// once a second and runs for 60 s - no polling of the lux registers.
|
|
8
|
+
|
|
9
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
10
|
+
const { MAX44009Full } = require('periph/src/chips/light/max44009');
|
|
11
|
+
|
|
12
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
13
|
+
|
|
14
|
+
(async () => {
|
|
15
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
16
|
+
const connection = new I2CConnection(bus, MAX44009Full.I2C_ADDRESS);
|
|
17
|
+
const sensor = new MAX44009Full(connection); // Create MAX44009 Full driver, (connection)
|
|
18
|
+
await sensor.init(); // Run initialization sequence, () → void (writes the reset state, waits 0.9 s)
|
|
19
|
+
|
|
20
|
+
const backlight = (lx) => 10 + 90 * (Math.log10(Math.min(10000, Math.max(0.1, lx))) + 1) / 5;
|
|
21
|
+
|
|
22
|
+
console.log('config', await sensor.getConfig()); // Read CONFIG, () → {continuous, manual, divideBy8, integrationMs}
|
|
23
|
+
const lux = await sensor.lux(); // Read illuminance, () → float lux
|
|
24
|
+
console.log('lux', lux.toFixed(2), 'resolution', sensor.resolution().toFixed(3), 'lx/LSB');// Lux per LSB, () → float lux
|
|
25
|
+
|
|
26
|
+
const arm = async (center) => {
|
|
27
|
+
await sensor.setThresholds(center * 0.7, center * 1.3); // Set the interrupt window, (lowLux, highLux) → void
|
|
28
|
+
await sensor.enableInterrupt(); // Enable the interrupt, () → void
|
|
29
|
+
};
|
|
30
|
+
await sensor.setThresholdDelay(2.0); // Set the threshold delay, (seconds) → void
|
|
31
|
+
await arm(lux);
|
|
32
|
+
await sensor.onInterrupt(async () => { // Subscribe, (callback(status), {intPin}) → void
|
|
33
|
+
const now = await sensor.lux();
|
|
34
|
+
console.log('lux', now.toFixed(2), '-> backlight', backlight(now).toFixed(0), '%');
|
|
35
|
+
await arm(now); // re-centre the window around the new value
|
|
36
|
+
});
|
|
37
|
+
console.log('backlight', backlight(lux).toFixed(0), '%');
|
|
38
|
+
|
|
39
|
+
for (let s = 0; s < 60; s++) {
|
|
40
|
+
const raw = await sensor.readRaw(); // Read exponent and mantissa, () → {exponent, mantissa, overrange}
|
|
41
|
+
console.log('E=' + raw.exponent, 'M=' + raw.mantissa, 'overrange=' + sensor.isOverrange());// Query the last reading, () → bool
|
|
42
|
+
await sleep(1000);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await sensor.disableInterrupt(); // Disable the interrupt, () → void
|
|
46
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
47
|
+
|
|
48
|
+
await connection.close();
|
|
49
|
+
})();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Minimal example: read the ambient illuminance once a second.
|
|
4
|
+
|
|
5
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
6
|
+
const { MAX44009Minimal } = require('periph/src/chips/light/max44009');
|
|
7
|
+
|
|
8
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
(async () => {
|
|
11
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
12
|
+
const connection = new I2CConnection(bus, MAX44009Minimal.I2C_ADDRESS);
|
|
13
|
+
const sensor = new MAX44009Minimal(connection); // Create MAX44009 driver, (connection)
|
|
14
|
+
await sensor.init(); // Run initialization sequence, () → void (writes the reset state, waits 0.9 s)
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < 10; i++) {
|
|
17
|
+
console.log((await sensor.lux()).toFixed(2), 'lx'); // Read illuminance, () → float lux
|
|
18
|
+
await sleep(1000);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
await connection.close();
|
|
22
|
+
})();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C02NFull } = require('../../../src/chips/memory/at24c02n');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50); // Create I2C connection, (bus=1, addr=0x50) → Connection
|
|
8
|
+
const ee = new AT24C02NFull(connection); // Create AT24C02N driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
console.log('Size:', ee.size(), 'bytes'); // Get array size, () → number
|
|
11
|
+
// always 256
|
|
12
|
+
console.log('Page:', ee.pageSize(), 'bytes'); // Get write page size, () → number
|
|
13
|
+
// always 8
|
|
14
|
+
ee.maxChunk = 16; // Set max bytes per bus transaction, (maxChunk=30) → void
|
|
15
|
+
console.log('maxChunk:', ee.maxChunk); // Get max bytes per bus transaction, () → number
|
|
16
|
+
|
|
17
|
+
await ee.writeByte(0x0010, 0xA5); // Write a single byte, (address 0–255, value 0–255) → void
|
|
18
|
+
// ACK-polled until the write cycle ends
|
|
19
|
+
console.log('Byte:', (await ee.readByte(0x0010)).toString(16)); // Read a single byte, (address 0–255) → number
|
|
20
|
+
// random read
|
|
21
|
+
|
|
22
|
+
await ee.write(0x0040, Buffer.from(Array.from({ length: 40 }, (_, i) => i))); // Write data, (address, data) → void
|
|
23
|
+
// split at 8-byte page boundaries and maxChunk, ACK-polled per chunk
|
|
24
|
+
const data = await ee.read(0x0040, 40); // Sequential read, (address, length) → Buffer
|
|
25
|
+
// split into maxChunk-byte transactions
|
|
26
|
+
console.log('Block:', data.toString('hex'));
|
|
27
|
+
|
|
28
|
+
await ee.writePage(0x00B8, Buffer.from(Array.from({ length: 8 }, (_, i) => i))); // Write within one page, (address, data) → void
|
|
29
|
+
// rejects with RangeError if the data crosses a page boundary
|
|
30
|
+
await ee.fill(0x0040, 40, 0xFF); // Fill a range, (address, length, value=0) → void
|
|
31
|
+
// page-wise writes of value
|
|
32
|
+
console.log('After fill:', (await ee.read(0x0040, 8)).toString('hex'));
|
|
33
|
+
|
|
34
|
+
console.log('Ready:', await ee.waitReady(10)); // ACK-poll the chip, (timeoutMs) → boolean
|
|
35
|
+
// true once the chip answers its address
|
|
36
|
+
connection.close();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C02NFull } = require('../../../src/chips/memory/at24c02n');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50); // Create I2C connection, (bus=1, addr=0x50) → Connection
|
|
8
|
+
const ee = new AT24C02NFull(connection); // Create AT24C02N driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
// --- Persistent boot counter: 4-byte big-endian value at address 0 ---
|
|
11
|
+
const counter = ((await ee.read(0x0000, 4)).readUInt32BE(0) + 1) >>> 0; // Sequential read, (address, length) → Buffer
|
|
12
|
+
const cb = Buffer.alloc(4);
|
|
13
|
+
cb.writeUInt32BE(counter, 0);
|
|
14
|
+
await ee.write(0x0000, cb); // Write data, (address, data) → void
|
|
15
|
+
console.log('Boot counter:', counter);
|
|
16
|
+
|
|
17
|
+
// --- Page-write benchmark: 3 pages, 8-byte aligned ---
|
|
18
|
+
const size = 3 * ee.pageSize(); // Get write page size, () → number
|
|
19
|
+
const pattern = Buffer.from(Array.from({ length: size }, (_, i) => (i * 7) & 0xFF));
|
|
20
|
+
const t0 = process.hrtime.bigint();
|
|
21
|
+
await ee.write(0x0080, pattern); // Write data, (address, data) → void
|
|
22
|
+
const ms = Number(process.hrtime.bigint() - t0) / 1e6;
|
|
23
|
+
console.log('Wrote %d bytes in %s ms (%s B/s incl. ACK-polled write cycles)', size, ms.toFixed(1), Math.round(size / (ms / 1000)));
|
|
24
|
+
console.log('Read-back matches:', (await ee.read(0x0080, size)).equals(pattern)); // Sequential read, (address, length) → Buffer
|
|
25
|
+
|
|
26
|
+
// --- A write that crosses a page boundary is refused ---
|
|
27
|
+
try {
|
|
28
|
+
await ee.writePage(0x00F4, Buffer.alloc(8)); // Write within one page, (address, data) → void
|
|
29
|
+
} catch (e) {
|
|
30
|
+
console.log('writePage refused:', e.message);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await ee.fill(0x0080, size); // Fill a range, (address, length, value=0) → void
|
|
34
|
+
console.log('Zeroed:', (await ee.read(0x0080, size)).equals(Buffer.alloc(size)));
|
|
35
|
+
console.log('Size', ee.size(), 'ready', await ee.waitReady(10)); // Get array size, () → number ; ACK-poll the chip, (timeoutMs) → boolean
|
|
36
|
+
connection.close();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C02NMinimal } = require('../../../src/chips/memory/at24c02n');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50); // Create I2C connection, (bus=1, addr=0x50) → Connection
|
|
8
|
+
const ee = new AT24C02NMinimal(connection); // Create AT24C02N driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
await ee.writeByte(0x0000, 0xA5); // Write a single byte, (address 0–255, value 0–255) → void
|
|
11
|
+
console.log('Byte:', (await ee.readByte(0x0000)).toString(16)); // Read a single byte, (address 0–255) → number
|
|
12
|
+
connection.close();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C64Full } = require('../../../src/chips/memory/at24c64');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
8
|
+
const ee = new AT24C64Full(connection); // Create AT24C64 driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
console.log('Size:', ee.size(), 'bytes'); // Get array size, () → number
|
|
11
|
+
// always 8192
|
|
12
|
+
console.log('Page:', ee.pageSize(), 'bytes'); // Get write page size, () → number
|
|
13
|
+
// always 32
|
|
14
|
+
ee.maxChunk = 16; // Set max bytes per bus transaction, (maxChunk=30) → void
|
|
15
|
+
console.log('maxChunk:', ee.maxChunk); // Get max bytes per bus transaction, () → number
|
|
16
|
+
|
|
17
|
+
await ee.writeByte(0x0010, 0xA5); // Write a single byte, (address 0–8191, value 0–255) → void
|
|
18
|
+
// ACK-polled until the write cycle ends
|
|
19
|
+
console.log('Byte:', (await ee.readByte(0x0010)).toString(16)); // Read a single byte, (address 0–8191) → number
|
|
20
|
+
// random read
|
|
21
|
+
|
|
22
|
+
await ee.write(0x0100, Buffer.from(Array.from({ length: 40 }, (_, i) => i))); // Write data, (address, data) → void
|
|
23
|
+
// split at 32-byte page boundaries and maxChunk, ACK-polled per chunk
|
|
24
|
+
const data = await ee.read(0x0100, 40); // Sequential read, (address, length) → Buffer
|
|
25
|
+
// split into maxChunk-byte transactions
|
|
26
|
+
console.log('Block:', data.toString('hex'));
|
|
27
|
+
|
|
28
|
+
await ee.writePage(0x0180, Buffer.from(Array.from({ length: 16 }, (_, i) => i))); // Write within one page, (address, data) → void
|
|
29
|
+
// rejects with RangeError if the data crosses a page boundary
|
|
30
|
+
await ee.fill(0x0100, 40, 0xFF); // Fill a range, (address, length, value=0) → void
|
|
31
|
+
// page-wise writes of value
|
|
32
|
+
console.log('After fill:', (await ee.read(0x0100, 8)).toString('hex'));
|
|
33
|
+
|
|
34
|
+
console.log('Ready:', await ee.waitReady(10)); // ACK-poll the chip, (timeoutMs) → boolean
|
|
35
|
+
// true once the chip answers its address
|
|
36
|
+
connection.close();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C64Full } = require('../../../src/chips/memory/at24c64');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
8
|
+
const ee = new AT24C64Full(connection); // Create AT24C64 driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
// --- Persistent boot counter: 4-byte big-endian value at address 0 ---
|
|
11
|
+
const counter = ((await ee.read(0x0000, 4)).readUInt32BE(0) + 1) >>> 0; // Sequential read, (address, length) → Buffer
|
|
12
|
+
const cb = Buffer.alloc(4);
|
|
13
|
+
cb.writeUInt32BE(counter, 0);
|
|
14
|
+
await ee.write(0x0000, cb); // Write data, (address, data) → void
|
|
15
|
+
console.log('Boot counter:', counter);
|
|
16
|
+
|
|
17
|
+
// --- Page-write benchmark: 3 pages, 32-byte aligned ---
|
|
18
|
+
const size = 3 * ee.pageSize(); // Get write page size, () → number
|
|
19
|
+
const pattern = Buffer.from(Array.from({ length: size }, (_, i) => (i * 7) & 0xFF));
|
|
20
|
+
const t0 = process.hrtime.bigint();
|
|
21
|
+
await ee.write(0x0080, pattern); // Write data, (address, data) → void
|
|
22
|
+
const ms = Number(process.hrtime.bigint() - t0) / 1e6;
|
|
23
|
+
console.log('Wrote %d bytes in %s ms (%s B/s incl. ACK-polled write cycles)', size, ms.toFixed(1), Math.round(size / (ms / 1000)));
|
|
24
|
+
console.log('Read-back matches:', (await ee.read(0x0080, size)).equals(pattern)); // Sequential read, (address, length) → Buffer
|
|
25
|
+
|
|
26
|
+
// --- A write that crosses a page boundary is refused ---
|
|
27
|
+
try {
|
|
28
|
+
await ee.writePage(0x00FC, Buffer.alloc(8)); // Write within one page, (address, data) → void
|
|
29
|
+
} catch (e) {
|
|
30
|
+
console.log('writePage refused:', e.message);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await ee.fill(0x0080, size); // Fill a range, (address, length, value=0) → void
|
|
34
|
+
console.log('Zeroed:', (await ee.read(0x0080, size)).equals(Buffer.alloc(size)));
|
|
35
|
+
console.log('Size', ee.size(), 'ready', await ee.waitReady(10)); // Get array size, () → number ; ACK-poll the chip, (timeoutMs) → boolean
|
|
36
|
+
connection.close();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AT24C64Minimal } = require('../../../src/chips/memory/at24c64');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
8
|
+
const ee = new AT24C64Minimal(connection); // Create AT24C64 driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
await ee.writeByte(0x0000, 0xA5); // Write a single byte, (address 0–8191, value 0–255) → void
|
|
11
|
+
console.log('Byte:', (await ee.readByte(0x0000)).toString(16)); // Read a single byte, (address 0–8191) → number
|
|
12
|
+
connection.close();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
main().catch(err => { console.error(err); process.exit(1); });
|