periph 1.0.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/examples/accelerometer/.gitkeep +0 -0
- package/examples/adc_dac/.gitkeep +0 -0
- package/examples/adc_dac/mcp4725/complete.js +34 -0
- package/examples/adc_dac/mcp4725/demo.js +43 -0
- package/examples/adc_dac/mcp4725/minimal.js +14 -0
- package/examples/color/.gitkeep +0 -0
- package/examples/comms/.gitkeep +0 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gas/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/gpio/.gitkeep +0 -0
- package/examples/gyroscope/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/io_expander/mcp23017/complete.js +80 -0
- package/examples/io_expander/mcp23017/demo.js +59 -0
- package/examples/io_expander/mcp23017/minimal.js +16 -0
- package/examples/io_expander/pcf8574/complete.js +68 -0
- package/examples/io_expander/pcf8574/demo.js +41 -0
- package/examples/io_expander/pcf8574/minimal.js +16 -0
- package/examples/io_expander/pcf8575/complete.js +31 -0
- package/examples/io_expander/pcf8575/demo.js +33 -0
- package/examples/io_expander/pcf8575/minimal.js +20 -0
- package/examples/led/.gitkeep +0 -0
- package/examples/led/sk6812rgbw/complete.js +69 -0
- package/examples/led/sk6812rgbw/demo.js +85 -0
- package/examples/led/sk6812rgbw/minimal.js +25 -0
- package/examples/led/ws2812b/complete.js +64 -0
- package/examples/led/ws2812b/demo.js +101 -0
- package/examples/led/ws2812b/minimal.js +22 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/magnetometer/.gitkeep +0 -0
- package/examples/magnetometer/as5600/complete.js +43 -0
- package/examples/magnetometer/as5600/demo.js +49 -0
- package/examples/magnetometer/as5600/minimal.js +16 -0
- package/examples/memory/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/other/.gitkeep +0 -0
- package/examples/power/ina219/complete.js +30 -0
- package/examples/power/ina219/demo.js +26 -0
- package/examples/power/ina219/minimal.js +17 -0
- package/examples/power/ina226/complete.js +33 -0
- package/examples/power/ina226/demo.js +30 -0
- package/examples/power/ina226/minimal.js +11 -0
- package/examples/power/ina3221/complete.js +65 -0
- package/examples/power/ina3221/demo.js +56 -0
- package/examples/power/ina3221/minimal.js +16 -0
- package/examples/pressure/.gitkeep +0 -0
- package/examples/pressure/bmp180/complete.js +26 -0
- package/examples/pressure/bmp180/demo.js +32 -0
- package/examples/pressure/bmp180/minimal.js +17 -0
- package/examples/pressure/bmp280/complete.js +28 -0
- package/examples/pressure/bmp280/demo.js +34 -0
- package/examples/pressure/bmp280/minimal.js +17 -0
- package/examples/rfid/.gitkeep +0 -0
- package/examples/rtc/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/package.json +20 -0
- package/src/chips/accelerometer/.gitkeep +0 -0
- package/src/chips/adc_dac/.gitkeep +0 -0
- package/src/chips/adc_dac/mcp4725.js +161 -0
- package/src/chips/color/.gitkeep +0 -0
- package/src/chips/comms/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gas/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
- package/src/chips/gyroscope/.gitkeep +0 -0
- package/src/chips/humidity/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/io_expander/mcp23017.js +396 -0
- package/src/chips/io_expander/pcf8574.js +298 -0
- package/src/chips/io_expander/pcf8575.js +295 -0
- package/src/chips/led/.gitkeep +0 -0
- package/src/chips/led/_color.js +25 -0
- package/src/chips/led/sk6812rgbw.js +183 -0
- package/src/chips/led/ws2812b.js +171 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/magnetometer/as5600.js +293 -0
- package/src/chips/memory/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/other/.gitkeep +0 -0
- package/src/chips/power/ina219.js +154 -0
- package/src/chips/power/ina226.js +208 -0
- package/src/chips/power/ina3221.js +341 -0
- package/src/chips/pressure/.gitkeep +0 -0
- package/src/chips/pressure/bmp180.js +234 -0
- package/src/chips/pressure/bmp280.js +328 -0
- package/src/chips/rfid/.gitkeep +0 -0
- package/src/chips/rtc/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
- package/src/index.js +2 -0
- package/src/transport/i2c.js +67 -0
- package/src/transport/neopixel.js +37 -0
- package/src/transport/smbus.js +117 -0
- package/src/transport/spi.js +72 -0
- package/src/transport/uart.js +145 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { SK6812RGBWFull } = require('../../../src/chips/led/sk6812rgbw');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
const N_PIXELS = parseInt(process.env.N_PIXELS || '30', 10);
|
|
9
|
+
|
|
10
|
+
const FRAME_MS = 33;
|
|
11
|
+
const RAINBOW_MS = 10000;
|
|
12
|
+
const WARM_MS = 2000;
|
|
13
|
+
const WARM_HALF = 100;
|
|
14
|
+
|
|
15
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
16
|
+
const strip = new SK6812RGBWFull(transport, N_PIXELS); // Create SK6812RGBW full driver, (transport, n=N_PIXELS pixels)
|
|
17
|
+
strip.brightness = 180; // Set global brightness, (value=0–255)
|
|
18
|
+
|
|
19
|
+
const { _hsvToRgb } = require('../../../src/chips/led/_color');
|
|
20
|
+
|
|
21
|
+
// --- Rainbow rotation: each pixel is assigned a hue offset by its position;
|
|
22
|
+
// the offset advances each frame so the rainbow rotates around the strip.
|
|
23
|
+
// RGB channels only (w=0) for 10 seconds at ~30 fps. ---
|
|
24
|
+
let hueOffset = 0;
|
|
25
|
+
let lastPrint = Date.now();
|
|
26
|
+
|
|
27
|
+
function rainbowFrame() {
|
|
28
|
+
for (let i = 0; i < N_PIXELS; i++) {
|
|
29
|
+
const h = (hueOffset + i / N_PIXELS) % 1.0;
|
|
30
|
+
const [r, g, b] = _hsvToRgb(h, 1.0, 1.0);
|
|
31
|
+
strip.set_pixel(i, r, g, b, 0); // Set pixel i to rainbow hue (w=0), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
32
|
+
}
|
|
33
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
34
|
+
hueOffset = (hueOffset + 1.0 / (N_PIXELS * 2)) % 1.0;
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
if (now - lastPrint >= 1000) {
|
|
37
|
+
console.log('rainbow hue_offset=' + hueOffset.toFixed(3));
|
|
38
|
+
lastPrint = now;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const rainbowStart = Date.now();
|
|
43
|
+
const rainbowTimer = setInterval(() => {
|
|
44
|
+
rainbowFrame();
|
|
45
|
+
if (Date.now() - rainbowStart >= RAINBOW_MS) {
|
|
46
|
+
clearInterval(rainbowTimer);
|
|
47
|
+
startWarm();
|
|
48
|
+
}
|
|
49
|
+
}, FRAME_MS);
|
|
50
|
+
|
|
51
|
+
// --- Warm-white strobe: showcases the dedicated white element.
|
|
52
|
+
// All four channels active (r=255, g=200, b=150, w=255) gives a warm,
|
|
53
|
+
// high-CRI white; toggling at 5 Hz for 2 seconds draws the eye to the
|
|
54
|
+
// difference between mixed-RGB white and the native W element. ---
|
|
55
|
+
function startWarm() {
|
|
56
|
+
strip.brightness = 255; // Set global brightness, (value=0–255)
|
|
57
|
+
strip.fill(255, 200, 150, 255); // Pre-load warm white (RGB+W) into buffer, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
58
|
+
let state = true;
|
|
59
|
+
const warmStart = Date.now();
|
|
60
|
+
const warmTimer = setInterval(() => {
|
|
61
|
+
strip.brightness = state ? 255 : 0; // Toggle brightness on/off, (value=0–255)
|
|
62
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
63
|
+
state = !state;
|
|
64
|
+
if (Date.now() - warmStart >= WARM_MS) {
|
|
65
|
+
clearInterval(warmTimer);
|
|
66
|
+
startRainbowContinuous();
|
|
67
|
+
}
|
|
68
|
+
}, WARM_HALF);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// --- Return to continuous rainbow ---
|
|
72
|
+
function startRainbowContinuous() {
|
|
73
|
+
strip.brightness = 180; // Set global brightness, (value=0–255)
|
|
74
|
+
hueOffset = 0;
|
|
75
|
+
lastPrint = Date.now();
|
|
76
|
+
setInterval(() => {
|
|
77
|
+
rainbowFrame();
|
|
78
|
+
}, FRAME_MS);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
process.on('SIGINT', () => {
|
|
82
|
+
strip.off();
|
|
83
|
+
transport.close();
|
|
84
|
+
process.exit(0);
|
|
85
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { SK6812RGBWMinimal } = require('../../../src/chips/led/sk6812rgbw');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
|
|
9
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
10
|
+
const strip = new SK6812RGBWMinimal(transport, 30); // Create SK6812RGBW driver, (transport, n=30 pixels)
|
|
11
|
+
|
|
12
|
+
strip.fill(255, 0, 0); // Fill all pixels red, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
strip.fill(0, 255, 0); // Fill all pixels green, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
strip.fill(0, 0, 255); // Fill all pixels blue, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
strip.fill(0, 0, 0, 255); // Fill all pixels white (W channel), (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
strip.off(); // Turn off all pixels, () → void
|
|
21
|
+
transport.close();
|
|
22
|
+
}, 1000);
|
|
23
|
+
}, 1000);
|
|
24
|
+
}, 1000);
|
|
25
|
+
}, 1000);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { WS2812BFull } = require('../../../src/chips/led/ws2812b');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
|
|
9
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
10
|
+
const strip = new WS2812BFull(transport, 8); // Create WS2812B full driver, (transport, n=8 pixels)
|
|
11
|
+
|
|
12
|
+
// fill — set all pixels and send immediately
|
|
13
|
+
strip.fill(255, 0, 0); // Fill all pixels with one colour, (r=0–255, g=0–255, b=0–255) → void
|
|
14
|
+
// stores GRB in buffer and calls transport.write()
|
|
15
|
+
|
|
16
|
+
// set individual pixels then show
|
|
17
|
+
strip.set_pixel(0, 255, 0, 0); // Set pixel 0 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255) → void
|
|
18
|
+
// writes G,R,B bytes into internal buffer at position index*3
|
|
19
|
+
strip.set_pixel(1, 0, 255, 0); // Set pixel 1 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255) → void
|
|
20
|
+
// writes G,R,B bytes into internal buffer at position index*3
|
|
21
|
+
strip.set_pixel(2, 0, 0, 255); // Set pixel 2 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255) → void
|
|
22
|
+
// writes G,R,B bytes into internal buffer at position index*3
|
|
23
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
24
|
+
// applies brightness scaling then calls transport.write()
|
|
25
|
+
|
|
26
|
+
// set_pixels — write multiple pixels at once
|
|
27
|
+
strip.set_pixels([[255,0,0],[0,255,0],[0,0,255], // Set pixels from array of [r,g,b], (colors=Array<[r,g,b]>) → void
|
|
28
|
+
[255,255,0],[0,255,255],[255,0,255],
|
|
29
|
+
[128,128,128],[255,255,255]]);
|
|
30
|
+
// writes entries sequentially from pixel 0; ignores extras beyond strip length
|
|
31
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
32
|
+
// applies brightness scaling then calls transport.write()
|
|
33
|
+
|
|
34
|
+
// brightness — global scale applied at show() time
|
|
35
|
+
strip.brightness = 64; // Set global brightness, (value=0–255)
|
|
36
|
+
// stored value is scaled: sent = stored * brightness / 255
|
|
37
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
38
|
+
// applies brightness scaling then calls transport.write()
|
|
39
|
+
strip.brightness = 255; // Set global brightness, (value=0–255)
|
|
40
|
+
// stored value is scaled: sent = stored * brightness / 255
|
|
41
|
+
|
|
42
|
+
// fill_hsv — fill all pixels from HSV colour
|
|
43
|
+
strip.fill_hsv(0.0, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
44
|
+
// converts HSV to RGB then calls fill(); hue 0.0 = red
|
|
45
|
+
strip.fill_hsv(0.333, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
46
|
+
// converts HSV to RGB then calls fill(); hue 0.333 = green
|
|
47
|
+
strip.fill_hsv(0.667, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
48
|
+
// converts HSV to RGB then calls fill(); hue 0.667 = blue
|
|
49
|
+
|
|
50
|
+
// rotate — shift pixel buffer left, then show
|
|
51
|
+
strip.set_pixels([[255, 0, 0], ...Array(7).fill([0, 0, 0])]); // Set pixels from array of [r,g,b], (colors=Array<[r,g,b]>) → void
|
|
52
|
+
// writes entries sequentially from pixel 0; ignores extras beyond strip length
|
|
53
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
54
|
+
// applies brightness scaling then calls transport.write()
|
|
55
|
+
for (let i = 0; i < 7; i++) {
|
|
56
|
+
strip.rotate(1); // Rotate pixel buffer left, (steps=1) → void
|
|
57
|
+
// shifts buffer by steps pixel positions; wraps around; does not send
|
|
58
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
59
|
+
// applies brightness scaling then calls transport.write()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
strip.off(); // Turn off all pixels, () → void
|
|
63
|
+
// equivalent to fill(0, 0, 0)
|
|
64
|
+
transport.close();
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { WS2812BFull } = require('../../../src/chips/led/ws2812b');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
const N_PIXELS = parseInt(process.env.N_PIXELS || '30', 10);
|
|
9
|
+
|
|
10
|
+
const FRAME_MS = 33;
|
|
11
|
+
const RAINBOW_DURATION = 10000;
|
|
12
|
+
const STROBE_DURATION = 2000;
|
|
13
|
+
const STROBE_HALF = 50;
|
|
14
|
+
|
|
15
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
16
|
+
const strip = new WS2812BFull(transport, N_PIXELS); // Create WS2812B full driver, (transport, n=N_PIXELS pixels)
|
|
17
|
+
strip.brightness = 180; // Set global brightness, (value=0–255)
|
|
18
|
+
|
|
19
|
+
function hsvToRgb(h, s, v) {
|
|
20
|
+
if (s === 0) { const c = Math.round(v * 255); return [c, c, c]; }
|
|
21
|
+
const i = Math.floor(h * 6);
|
|
22
|
+
const f = h * 6 - i;
|
|
23
|
+
const p = Math.round(v * (1 - s) * 255);
|
|
24
|
+
const q = Math.round(v * (1 - s * f) * 255);
|
|
25
|
+
const t = Math.round(v * (1 - s * (1 - f)) * 255);
|
|
26
|
+
const vv = Math.round(v * 255);
|
|
27
|
+
switch (i % 6) {
|
|
28
|
+
case 0: return [vv, t, p];
|
|
29
|
+
case 1: return [q, vv, p];
|
|
30
|
+
case 2: return [p, vv, t];
|
|
31
|
+
case 3: return [p, q, vv];
|
|
32
|
+
case 4: return [t, p, vv];
|
|
33
|
+
default: return [vv, p, q];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// --- Rainbow rotation: each pixel is assigned a hue offset by its position;
|
|
38
|
+
// the offset is advanced each frame so the rainbow rotates around the strip.
|
|
39
|
+
// Running at ~30 fps for 10 seconds gives a smooth continuous animation. ---
|
|
40
|
+
let hueOffset = 0;
|
|
41
|
+
let lastPrint = Date.now();
|
|
42
|
+
|
|
43
|
+
function rainbowFrame() {
|
|
44
|
+
for (let i = 0; i < N_PIXELS; i++) {
|
|
45
|
+
const h = (hueOffset + i / N_PIXELS) % 1.0;
|
|
46
|
+
const [r, g, b] = hsvToRgb(h, 1.0, 1.0);
|
|
47
|
+
strip.set_pixel(i, r, g, b); // Set pixel i to rainbow hue, (index=0–n-1, r=0–255, g=0–255, b=0–255) → void
|
|
48
|
+
}
|
|
49
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
50
|
+
hueOffset = (hueOffset + 1.0 / (N_PIXELS * 2)) % 1.0;
|
|
51
|
+
const now = Date.now();
|
|
52
|
+
if (now - lastPrint >= 1000) {
|
|
53
|
+
console.log('rainbow hue_offset=' + hueOffset.toFixed(3));
|
|
54
|
+
lastPrint = now;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const rainbowStart = Date.now();
|
|
59
|
+
const rainbowTimer = setInterval(() => {
|
|
60
|
+
rainbowFrame();
|
|
61
|
+
if (Date.now() - rainbowStart >= RAINBOW_DURATION) {
|
|
62
|
+
clearInterval(rainbowTimer);
|
|
63
|
+
startStrobe();
|
|
64
|
+
}
|
|
65
|
+
}, FRAME_MS);
|
|
66
|
+
|
|
67
|
+
// --- Strobe effect: alternate full white and off at 10 Hz for 2 seconds.
|
|
68
|
+
// Uses brightness=255 for maximum intensity then brightness=0 for off,
|
|
69
|
+
// demonstrating non-destructive brightness scaling — pixel values in the
|
|
70
|
+
// buffer are never zeroed. ---
|
|
71
|
+
function startStrobe() {
|
|
72
|
+
strip.brightness = 255; // Set global brightness, (value=0–255)
|
|
73
|
+
strip.fill(255, 255, 255); // Pre-load white into buffer, (r=0–255, g=0–255, b=0–255) → void
|
|
74
|
+
let state = true;
|
|
75
|
+
const strobeStart = Date.now();
|
|
76
|
+
const strobeTimer = setInterval(() => {
|
|
77
|
+
strip.brightness = state ? 255 : 0; // Set global brightness, (value=0–255)
|
|
78
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
79
|
+
state = !state;
|
|
80
|
+
if (Date.now() - strobeStart >= STROBE_DURATION) {
|
|
81
|
+
clearInterval(strobeTimer);
|
|
82
|
+
startRainbowContinuous();
|
|
83
|
+
}
|
|
84
|
+
}, STROBE_HALF);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// --- Return to continuous rainbow ---
|
|
88
|
+
function startRainbowContinuous() {
|
|
89
|
+
strip.brightness = 180; // Set global brightness, (value=0–255)
|
|
90
|
+
hueOffset = 0;
|
|
91
|
+
lastPrint = Date.now();
|
|
92
|
+
setInterval(() => {
|
|
93
|
+
rainbowFrame();
|
|
94
|
+
}, FRAME_MS);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
process.on('SIGINT', () => {
|
|
98
|
+
strip.off();
|
|
99
|
+
transport.close();
|
|
100
|
+
process.exit(0);
|
|
101
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { WS2812BMinimal } = require('../../../src/chips/led/ws2812b');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
|
|
9
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
10
|
+
const strip = new WS2812BMinimal(transport, 30); // Create WS2812B driver, (transport, n=30 pixels)
|
|
11
|
+
|
|
12
|
+
strip.fill(255, 0, 0); // Fill all pixels red, (r=0–255, g=0–255, b=0–255) → void
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
strip.fill(0, 255, 0); // Fill all pixels green, (r=0–255, g=0–255, b=0–255) → void
|
|
15
|
+
setTimeout(() => {
|
|
16
|
+
strip.fill(0, 0, 255); // Fill all pixels blue, (r=0–255, g=0–255, b=0–255) → void
|
|
17
|
+
setTimeout(() => {
|
|
18
|
+
strip.off(); // Turn off all pixels, () → void
|
|
19
|
+
transport.close();
|
|
20
|
+
}, 1000);
|
|
21
|
+
}, 1000);
|
|
22
|
+
}, 1000);
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../src/transport/i2c');
|
|
4
|
+
const { AS5600Full } = require('../../src/chips/magnetometer/as5600');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Full(transport); // Create AS5600 driver, (transport) → AS5600Full
|
|
11
|
+
|
|
12
|
+
// --- Status and magnet checks ---
|
|
13
|
+
console.log(as5600.isMagnetDetected()); // Check magnet present, () → bool
|
|
14
|
+
console.log(as5600.isMagnetTooStrong()); // Check magnet too strong, () → bool
|
|
15
|
+
console.log(as5600.isMagnetTooWeak()); // Check magnet too weak, () → bool
|
|
16
|
+
console.log(as5600.statusByte().toString(16)); // Read raw status, () → int
|
|
17
|
+
|
|
18
|
+
// --- Angle readings ---
|
|
19
|
+
console.log(as5600.angle()); // Read absolute angle, () → float degrees
|
|
20
|
+
console.log(as5600.angleRaw()); // Read scaled angle count, () → int 0-4095
|
|
21
|
+
console.log(as5600.rawAngle()); // Read raw unscaled angle, () → int 0-4095
|
|
22
|
+
console.log(as5600.rawAngleDegrees()); // Read raw angle in degrees, () → float degrees
|
|
23
|
+
|
|
24
|
+
// --- Diagnostics ---
|
|
25
|
+
console.log(as5600.agc()); // Read AGC value, () → int
|
|
26
|
+
console.log(as5600.magnitude()); // Read CORDIC magnitude, () → int
|
|
27
|
+
|
|
28
|
+
// --- Position configuration (volatile) ---
|
|
29
|
+
console.log(as5600.zeroPosition()); // Read ZPOS, () → int 0-4095
|
|
30
|
+
console.log(as5600.maxPosition()); // Read MPOS, () → int 0-4095
|
|
31
|
+
console.log(as5600.maxAngle()); // Read MANG, () → int 0-4095
|
|
32
|
+
|
|
33
|
+
as5600.setZeroPosition(0); // Set zero position, (pos 0-4095) → None
|
|
34
|
+
as5600.setMaxPosition(4095); // Set max position, (pos 0-4095) → None
|
|
35
|
+
as5600.setMaxAngle(2048); // Set max angle span, (span 0-4095) → None
|
|
36
|
+
|
|
37
|
+
// --- Configure power mode and output ---
|
|
38
|
+
as5600.configure(0, 0, 0, 0, 0, 0, false); // Configure chip, (pm 0-3, hyst 0-3, outs 0-2, pwmf 0-3, sf 0-3, fth 0-7, wd bool) → None
|
|
39
|
+
|
|
40
|
+
// --- Burn count ---
|
|
41
|
+
console.log(as5600.burnCount()); // Read burn count, () → int 0-3
|
|
42
|
+
|
|
43
|
+
transport.close();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../src/transport/i2c');
|
|
4
|
+
const { AS5600Full } = require('../../src/chips/magnetometer/as5600');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Full(transport);
|
|
11
|
+
|
|
12
|
+
// --- Motor feedback monitor: read angle 10 times per second ---
|
|
13
|
+
// AGC monitoring detects magnet distance drift; status changes alert to magnet removal.
|
|
14
|
+
// In 5 V mode, target AGC ≈ 128; in 3.3 V mode, target AGC ≈ 64.
|
|
15
|
+
|
|
16
|
+
let prevStatus = as5600.statusByte();
|
|
17
|
+
|
|
18
|
+
for (let n = 0; n < 10; n++) {
|
|
19
|
+
const a = as5600.angle(); // Read absolute angle, () → float degrees
|
|
20
|
+
const r = as5600.rawAngle(); // Read raw unscaled angle, () → int 0-4095
|
|
21
|
+
const g = as5600.agc(); // Read AGC value, () → int
|
|
22
|
+
|
|
23
|
+
// --- Check for status changes (magnet inserted/removed) ---
|
|
24
|
+
const status = as5600.statusByte();
|
|
25
|
+
if (status !== prevStatus) {
|
|
26
|
+
if (!as5600.isMagnetDetected()) {
|
|
27
|
+
console.log('[MAGNET REMOVED] MD=0');
|
|
28
|
+
} else {
|
|
29
|
+
console.log('[MAGNET DETECTED] MD=1 MH=%d ML=%d',
|
|
30
|
+
as5600.isMagnetTooStrong(), as5600.isMagnetTooWeak());
|
|
31
|
+
}
|
|
32
|
+
prevStatus = status;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// --- AGC health check ---
|
|
36
|
+
if (as5600.isMagnetDetected()) {
|
|
37
|
+
let tag = '[OK]';
|
|
38
|
+
if (g < 64 || g > 192) {
|
|
39
|
+
tag = '[AGC low — magnet weak or too far]';
|
|
40
|
+
}
|
|
41
|
+
console.log('angle=%.2f° raw=%d agc=%d %s', a, r, g, tag);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 100 ms synchronous delay
|
|
45
|
+
const end = Date.now() + 100;
|
|
46
|
+
while (Date.now() < end) {}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
transport.close();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../src/transport/i2c');
|
|
4
|
+
const { AS5600Minimal } = require('../../src/chips/magnetometer/as5600');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Minimal(transport); // Create AS5600 driver, (transport) → AS5600Minimal
|
|
11
|
+
|
|
12
|
+
setInterval(() => {
|
|
13
|
+
const a = as5600.angle(); // Read absolute angle, () → float degrees
|
|
14
|
+
const r = as5600.angleRaw(); // Read scaled angle count, () → int 0-4095
|
|
15
|
+
console.log('angle=%.2f° raw=%d', a, r);
|
|
16
|
+
}, 1000);
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA219Full } = require('../../../src/chips/power/ina219');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const ina = new INA219Full(transport);
|
|
11
|
+
|
|
12
|
+
console.log(ina.voltage()); // Read bus voltage, () → float V
|
|
13
|
+
console.log(ina.shuntVoltage()); // Read shunt voltage, () → float V
|
|
14
|
+
console.log(ina.current()); // Read load current, () → float A
|
|
15
|
+
console.log(ina.power()); // Read power, () → float W
|
|
16
|
+
console.log(ina.conversionReady()); // Check conversion done, () → bool
|
|
17
|
+
console.log(ina.overflow()); // Check math overflow, () → bool
|
|
18
|
+
|
|
19
|
+
ina.configure(1, 3, 0x03, 0x03, 7);
|
|
20
|
+
// Configure ADC, (brng 0–1, pga 0–3, badc 0x0F, sadc 0x0F, mode 0–7) → None
|
|
21
|
+
// sets bus range, PGA gain, ADC resolution, and operating mode
|
|
22
|
+
|
|
23
|
+
ina.shutdown(); // Put chip into power-down mode, () → None
|
|
24
|
+
const end = Date.now() + 1;
|
|
25
|
+
while (Date.now() < end) {}
|
|
26
|
+
ina.wake(); // Restore previous operating mode, () → None
|
|
27
|
+
|
|
28
|
+
ina.reset(); // Reset all registers and re-write calibration, () → None
|
|
29
|
+
|
|
30
|
+
transport.close();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA219Full } = require('../../../src/chips/power/ina219');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const ina = new INA219Full(transport);
|
|
11
|
+
|
|
12
|
+
// --- Configure for noise-sensitive power rail monitoring ---
|
|
13
|
+
// 128-sample averaging suppresses switching noise on a noisy 5 V rail;
|
|
14
|
+
// continuous mode avoids re-triggering overhead between measurements.
|
|
15
|
+
ina.configure(1, 3, 0x0F, 0x0F, 7);
|
|
16
|
+
// Configure ADC, (brng 0–1, pga 0–3, badc 0x0F, sadc 0x0F, mode 0–7) → None
|
|
17
|
+
|
|
18
|
+
console.log('V A W');
|
|
19
|
+
|
|
20
|
+
setInterval(() => {
|
|
21
|
+
while (!ina.conversionReady()) {} // Check conversion done, () → bool
|
|
22
|
+
const v = ina.voltage(); // Read bus voltage, () → float V
|
|
23
|
+
const i = ina.current(); // Read load current, () → float A
|
|
24
|
+
const p = ina.power(); // Read power, () → float W
|
|
25
|
+
console.log(`${v.toFixed(3)} V ${i.toFixed(4)} A ${p.toFixed(4)} W`);
|
|
26
|
+
}, 1000);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA219Minimal } = require('../../../src/chips/power/ina219');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const ina = new INA219Minimal(transport);
|
|
11
|
+
|
|
12
|
+
setInterval(() => {
|
|
13
|
+
const v = ina.voltage(); // Read bus voltage, () → float V
|
|
14
|
+
const i = ina.current(); // Read load current, () → float A
|
|
15
|
+
const p = ina.power(); // Read power, () → float W
|
|
16
|
+
console.log(`${v.toFixed(3)} V ${i.toFixed(4)} A ${p.toFixed(4)} W`);
|
|
17
|
+
}, 1000);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA226Full } = require('../../../src/chips/power/ina226');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x40);
|
|
7
|
+
const ina = new INA226Full(transport);
|
|
8
|
+
|
|
9
|
+
console.log('0x' + ina.manufacturerId().toString(16));
|
|
10
|
+
console.log('0x' + ina.dieId().toString(16));
|
|
11
|
+
|
|
12
|
+
console.log(ina.voltage());
|
|
13
|
+
console.log(ina.shuntVoltage());
|
|
14
|
+
console.log(ina.current());
|
|
15
|
+
console.log(ina.power());
|
|
16
|
+
console.log(ina.conversionReady());
|
|
17
|
+
console.log(ina.overflow());
|
|
18
|
+
|
|
19
|
+
ina.configure(3, 4, 4, 7);
|
|
20
|
+
|
|
21
|
+
ina.setAlert(INA226Full.POL, 1.0, false, true);
|
|
22
|
+
console.log('0x' + ina.alertFlags().toString(16));
|
|
23
|
+
|
|
24
|
+
ina.setAlert(INA226Full.BOL, 5.5);
|
|
25
|
+
ina.setAlert(INA226Full.BUL, 4.5);
|
|
26
|
+
ina.setAlert(INA226Full.SOL, 0.05);
|
|
27
|
+
ina.setAlert(INA226Full.CNVR);
|
|
28
|
+
|
|
29
|
+
ina.shutdown();
|
|
30
|
+
ina.wake();
|
|
31
|
+
ina.reset();
|
|
32
|
+
|
|
33
|
+
transport.close();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA226Full } = require('../../../src/chips/power/ina226');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x40);
|
|
7
|
+
const ina = new INA226Full(transport);
|
|
8
|
+
|
|
9
|
+
// 64-sample averaging smooths out switching noise from DC/DC converters
|
|
10
|
+
ina.configure(3, 4, 4, 7);
|
|
11
|
+
|
|
12
|
+
// latch the alert so a brief spike is not missed between loop iterations
|
|
13
|
+
ina.setAlert(INA226Full.POL, 1.0, false, true);
|
|
14
|
+
|
|
15
|
+
console.log('V A W');
|
|
16
|
+
|
|
17
|
+
setInterval(() => {
|
|
18
|
+
// wait for a fresh conversion to avoid reading stale register values
|
|
19
|
+
while (!ina.conversionReady()) {}
|
|
20
|
+
|
|
21
|
+
const v = ina.voltage();
|
|
22
|
+
const i = ina.current();
|
|
23
|
+
const p = ina.power();
|
|
24
|
+
console.log(v.toFixed(3) + 'V ' + i.toFixed(4) + 'A ' + p.toFixed(4) + 'W');
|
|
25
|
+
|
|
26
|
+
// reading alertFlags clears the latch — do this after printing measurements
|
|
27
|
+
if (ina.alertFlags() & INA226Full.AFF) {
|
|
28
|
+
console.log('ALERT: power limit exceeded');
|
|
29
|
+
}
|
|
30
|
+
}, 1000);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA226Minimal } = require('../../../src/chips/power/ina226');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x40);
|
|
7
|
+
const ina = new INA226Minimal(transport);
|
|
8
|
+
|
|
9
|
+
setInterval(() => {
|
|
10
|
+
console.log(ina.voltage(), ina.current(), ina.power());
|
|
11
|
+
}, 1000);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { INA3221Full } = require('../../../src/chips/power/ina3221');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x40);
|
|
7
|
+
const ina = new INA3221Full(transport); // Create INA3221 driver, (transport, r_shunt=0.1 Ω)
|
|
8
|
+
|
|
9
|
+
console.log('0x' + ina.manufacturerId().toString(16)); // Read Manufacturer ID, () → int 0x5449
|
|
10
|
+
// Texas Instruments ID
|
|
11
|
+
console.log('0x' + ina.dieId().toString(16)); // Read Die ID, () → int 0x3220
|
|
12
|
+
// INA3221 die revision
|
|
13
|
+
|
|
14
|
+
for (const ch of [1, 2, 3]) {
|
|
15
|
+
console.log(ina.voltage(ch)); // Read bus voltage, (channel) → float V
|
|
16
|
+
// left-aligned 12-bit bus register, 8 mV LSB
|
|
17
|
+
console.log(ina.shuntVoltage(ch)); // Read shunt voltage, (channel) → float V
|
|
18
|
+
// left-aligned 13-bit signed shunt, 40 µV LSB
|
|
19
|
+
console.log(ina.current(ch)); // Read load current, (channel) → float A
|
|
20
|
+
// computed from shunt voltage / r_shunt
|
|
21
|
+
console.log(ina.power(ch)); // Read power, (channel) → float W
|
|
22
|
+
// computed from voltage × current
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
console.log(ina.conversionReady()); // Check conversion done, () → bool
|
|
26
|
+
// reads CVRF bit from Mask/Enable register
|
|
27
|
+
|
|
28
|
+
ina.configure(4, 4, 4, 7); // Configure ADC, (avg 0–7, vbus_ct 0–7, vsh_ct 0–7, mode 0–7) → None
|
|
29
|
+
// sets averaging count, conversion time, and operating mode
|
|
30
|
+
|
|
31
|
+
ina.enableChannel(1, true); // Enable channel, (channel, enabled) → None
|
|
32
|
+
// modifies CH1en bit in Configuration register
|
|
33
|
+
const ena = ina.channelEnabled(1); // Read channel enabled, (channel) → bool
|
|
34
|
+
// reads CH1en bit
|
|
35
|
+
|
|
36
|
+
ina.setCriticalAlert(1, 0.1); // Set critical alert, (channel, limit_v, latch=False) → None
|
|
37
|
+
// per-conversion threshold on shunt voltage
|
|
38
|
+
ina.setWarningAlert(2, 0.05); // Set warning alert, (channel, limit_v, latch=False) → None
|
|
39
|
+
// per-average threshold on shunt voltage
|
|
40
|
+
|
|
41
|
+
const flags = ina.alertFlags(); // Read alert flags, () → int
|
|
42
|
+
// reads Mask/Enable register, clears latched flags
|
|
43
|
+
|
|
44
|
+
ina.setSummationChannels([1, 2], 0.2); // Set summation channels, (channels, limit_v) → None
|
|
45
|
+
// enables SCC bits and sets sum limit register
|
|
46
|
+
const svSum = ina.summationValue(); // Read summation value, () → float V
|
|
47
|
+
// reads Shunt-Voltage Sum register
|
|
48
|
+
|
|
49
|
+
ina.setPowerValidLimits(5.5, 4.5); // Set PV limits, (upper_v, lower_v) → None
|
|
50
|
+
// sets PV Upper/Lower Limit registers
|
|
51
|
+
const pv = ina.powerValid(); // Read power valid, () → bool
|
|
52
|
+
// reads PVF bit from Mask/Enable
|
|
53
|
+
|
|
54
|
+
ina.shutdown(); // Put chip into power-down mode, () → None
|
|
55
|
+
// saves current mode for wake()
|
|
56
|
+
// 1 ms synchronous delay
|
|
57
|
+
const end = Date.now() + 1;
|
|
58
|
+
while (Date.now() < end) {}
|
|
59
|
+
ina.wake(); // Restore operating mode, () → None
|
|
60
|
+
// restores the mode saved by shutdown()
|
|
61
|
+
|
|
62
|
+
ina.reset(); // Reset all registers, () → None
|
|
63
|
+
// sets RST bit, chip re-initializes to defaults
|
|
64
|
+
|
|
65
|
+
transport.close();
|