periph 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelConnection } = require('../../../src/connection/neopixel');
|
|
4
|
+
const { WS2814Full } = require('../../../src/chips/led/ws2814');
|
|
5
|
+
const { _hsvToRgb } = require('../../../src/chips/led/_color');
|
|
6
|
+
|
|
7
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
8
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
9
|
+
const N_PIXELS = parseInt(process.env.N_PIXELS || '30', 10);
|
|
10
|
+
|
|
11
|
+
const FRAME_MS = 33; // ~30 fps
|
|
12
|
+
const RAINBOW_MS = 5000;
|
|
13
|
+
const FLASH_MS = 2000;
|
|
14
|
+
const DIM_MS = 2000;
|
|
15
|
+
|
|
16
|
+
const connection = new NeoPixelConnection(SPI_BUS, SPI_DEVICE); // Create NeoPixel connection, (busNumber, deviceNumber)
|
|
17
|
+
const strip = new WS2814Full(connection, N_PIXELS); // Create WS2814 full driver, (connection, n=N_PIXELS pixels)
|
|
18
|
+
|
|
19
|
+
function sleep(ms) {
|
|
20
|
+
return new Promise(r => setTimeout(r, ms));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function main() {
|
|
24
|
+
// --- Rainbow rotation using RGB channels (white=0). Each pixel is
|
|
25
|
+
// assigned a hue offset by its position; the offset advances each
|
|
26
|
+
// frame so the rainbow rotates continuously around the strip.
|
|
27
|
+
// Demonstrates that WS2814's RGBW wire order is identity (R, G, B, W
|
|
28
|
+
// with no reorder), unlike the SK6812RGBW's GRBW order. Runs at
|
|
29
|
+
// ~30 fps for 5 seconds. ---
|
|
30
|
+
let hueOffset = 0;
|
|
31
|
+
const rainbowStart = Date.now();
|
|
32
|
+
let lastPrint = rainbowStart;
|
|
33
|
+
while (Date.now() - rainbowStart < RAINBOW_MS) {
|
|
34
|
+
for (let i = 0; i < N_PIXELS; i++) {
|
|
35
|
+
const h = (hueOffset + i / N_PIXELS) % 1.0;
|
|
36
|
+
const [r, g, b] = _hsvToRgb(h, 1.0, 1.0);
|
|
37
|
+
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
|
|
38
|
+
}
|
|
39
|
+
await strip.show(); // Transmit buffer to strip, () → void
|
|
40
|
+
hueOffset = (hueOffset + 1.0 / (N_PIXELS * 2)) % 1.0;
|
|
41
|
+
const now = Date.now();
|
|
42
|
+
if (now - lastPrint >= 1000) {
|
|
43
|
+
console.log(`mode=rainbow brightness=${strip.brightness}`);
|
|
44
|
+
lastPrint = now;
|
|
45
|
+
}
|
|
46
|
+
await sleep(FRAME_MS);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// --- Warm white at full brightness for 2 seconds. r=255, g=200, b=150,
|
|
50
|
+
// w=255 blends the dedicated white element with amber-tinted RGB,
|
|
51
|
+
// exercising the white channel and the 32-bit RGBW pixel word at
|
|
52
|
+
// full brightness. ---
|
|
53
|
+
await strip.fill(255, 200, 150, 255); // Fill all pixels warm white, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
54
|
+
const flashStart = Date.now();
|
|
55
|
+
while (Date.now() - flashStart < FLASH_MS) {
|
|
56
|
+
console.log(`mode=warm-white brightness=${strip.brightness}`);
|
|
57
|
+
await sleep(100);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// --- Dim warm white to 50% using the brightness property and hold for
|
|
61
|
+
// 2 seconds. Demonstrates that brightness scaling is non-destructive:
|
|
62
|
+
// the stored RGBW values are unchanged, only the scale factor applied
|
|
63
|
+
// at show() time changes. ---
|
|
64
|
+
strip.brightness = 128; // Set global brightness, (value=0–255)
|
|
65
|
+
await strip.show(); // Transmit buffer to strip, () → void
|
|
66
|
+
const dimStart = Date.now();
|
|
67
|
+
while (Date.now() - dimStart < DIM_MS) {
|
|
68
|
+
console.log(`mode=warm-white-dimmed brightness=${strip.brightness}`);
|
|
69
|
+
await sleep(100);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// --- Cycle to cool white at full brightness. r=200, g=210, b=255,
|
|
73
|
+
// w=255 shifts the blend toward blue, showcasing the dedicated
|
|
74
|
+
// white element paired with a cool-tinted RGB base. ---
|
|
75
|
+
strip.brightness = 255; // Set global brightness, (value=0–255)
|
|
76
|
+
await strip.fill(200, 210, 255, 255); // Fill all pixels cool white, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
77
|
+
console.log(`mode=cool-white brightness=${strip.brightness}`);
|
|
78
|
+
|
|
79
|
+
await connection.close();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelConnection } = require('../../../src/connection/neopixel');
|
|
4
|
+
const { WS2814Minimal } = require('../../../src/chips/led/ws2814');
|
|
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 connection = new NeoPixelConnection(SPI_BUS, SPI_DEVICE); // Create NeoPixel connection, (busNumber, deviceNumber)
|
|
10
|
+
const strip = new WS2814Minimal(connection, 30); // Create WS2814 driver, (connection, n=30 pixels)
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await strip.fill(255, 0, 0); // Fill all pixels red, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
14
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
15
|
+
await strip.fill(0, 255, 0); // Fill all pixels green, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
16
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
17
|
+
await strip.fill(0, 0, 255); // Fill all pixels blue, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
18
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
19
|
+
await 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
|
|
20
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
21
|
+
await strip.off(); // Turn off all pixels, () → void
|
|
22
|
+
await connection.close();
|
|
23
|
+
})();
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Complete example for the APDS-9930 — exercises every Full-class method.
|
|
2
|
+
|
|
3
|
+
const { APDS9930Full } = require('periph/src/chips/light/apds-9930');
|
|
4
|
+
const { I2CConnection } = require('periph/src/connection/i2c_auto');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
|
|
8
|
+
const apds = new APDS9930Full(connection); // Construct APDS-9930 Full, (connection) → APDS9930Full
|
|
9
|
+
// exposes ALS and proximity configuration methods
|
|
10
|
+
|
|
11
|
+
await new Promise(r => setTimeout(r, 110));
|
|
12
|
+
|
|
13
|
+
await apds.configureAls(0xDB, 0, false); // Configure ALS, (atime=0xDB, again=0, agl=false) → Promise<void>
|
|
14
|
+
// sets ALS integration time to 101 ms with 1x gain
|
|
15
|
+
await apds.configureProximity(8, 0, 0, false, 0xFF); // Configure proximity, (ppulse=8, pgain=0, pdrive=0, pdl=false, ptime=0xFF) → Promise<void>
|
|
16
|
+
// 8 LED pulses at 100 mA, 1x gain, no reduced drive
|
|
17
|
+
await apds.disableWait(); // Disable wait timer, () → Promise<void>
|
|
18
|
+
// clears WEN in ENABLE
|
|
19
|
+
await apds.setAlsThresholds(100, 60000, 1); // Set ALS thresholds, (low=100, high=60000, persistence=1) → Promise<void>
|
|
20
|
+
// fires after 1 consecutive out-of-range Ch0 count
|
|
21
|
+
await apds.setProximityThresholds(10, 200, 1); // Set proximity thresholds, (low=10, high=200, persistence=1) → Promise<void>
|
|
22
|
+
// fires on a single proximity reading outside [10, 200]
|
|
23
|
+
await apds.setProximityOffset(0); // Set proximity offset, (offset=0) → Promise<void>
|
|
24
|
+
// clears any prior offset
|
|
25
|
+
await apds.sleepAfterInterrupt(false); // Configure SAI, (enable=false) → Promise<void>
|
|
26
|
+
// chip stays in normal operation after an interrupt
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < 10; i++) {
|
|
29
|
+
await new Promise(r => setTimeout(r, 110));
|
|
30
|
+
const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
|
|
31
|
+
// combines Ch0 and Ch1 with IR-compensation coefficients
|
|
32
|
+
const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
|
|
33
|
+
// 16-bit ADC value
|
|
34
|
+
const c0 = await apds.ch0(); // Read Ch0 raw, () → Promise<number> count
|
|
35
|
+
// 16-bit ADC value of visible + IR channel
|
|
36
|
+
const c1 = await apds.ch1(); // Read Ch1 raw, () → Promise<number> count
|
|
37
|
+
// 16-bit ADC value of IR-only channel
|
|
38
|
+
const st = await apds.status(); // Read STATUS decoded, () → Promise<object>
|
|
39
|
+
// {avalid, pvalid, psat, aint, pint} booleans
|
|
40
|
+
console.log(`lux=${lx.toFixed(1)} prox=${p} ch0=${c0} ch1=${c1} status=${JSON.stringify(st)}`);
|
|
41
|
+
}
|
|
42
|
+
await apds.clearInterrupt('both'); // Clear interrupts, (channel='both') → Promise<void>
|
|
43
|
+
// issues special-function command 0xE7 to clear both ALS and proximity INT flags
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Demo for the APDS-9930 — adaptive backlight + screen-lock scenario.
|
|
2
|
+
|
|
3
|
+
const { APDS9930Full } = require('periph/src/chips/light/apds-9930');
|
|
4
|
+
const { I2CConnection } = require('periph/src/connection/i2c_auto');
|
|
5
|
+
|
|
6
|
+
const DIM_LUX_THRESHOLD = 10.0;
|
|
7
|
+
const PROX_SCREEN_OFF = 400;
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
// --- Configure for adaptive backlight + screen-lock monitoring ---
|
|
11
|
+
// Defaults (ATIME=0xDB, 1x AGAIN, 8-pulse proximity, 100 mA drive)
|
|
12
|
+
// already give stable readings; we just print lux and proximity.
|
|
13
|
+
const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
|
|
14
|
+
const apds = new APDS9930Full(connection); // Construct APDS-9930 Full, (connection) → APDS9930Full
|
|
15
|
+
// default 101 ms ALS integration, 8-pulse proximity, 100 mA drive
|
|
16
|
+
|
|
17
|
+
await new Promise(r => setTimeout(r, 110));
|
|
18
|
+
// --- Sample lux and proximity once per second for 30 cycles ---
|
|
19
|
+
for (let i = 0; i < 30; i++) {
|
|
20
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
21
|
+
const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
|
|
22
|
+
// IR-compensated lux via Ch0/Ch1 difference
|
|
23
|
+
const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
|
|
24
|
+
// 16-bit ADC value; higher = closer
|
|
25
|
+
console.log(`lux=${lx.toFixed(1)} lx proximity=${p}`);
|
|
26
|
+
if (lx < DIM_LUX_THRESHOLD) console.log(' -> dim backlight');
|
|
27
|
+
if (p > PROX_SCREEN_OFF) console.log(' -> disable screen');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Minimal example for the APDS-9930 ambient light + proximity sensor.
|
|
2
|
+
|
|
3
|
+
const { APDS9930Minimal } = require('periph/src/chips/light/apds-9930');
|
|
4
|
+
const { I2CConnection } = require('periph/src/connection/i2c_auto');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
|
|
8
|
+
const apds = new APDS9930Minimal(connection); // Construct APDS-9930 Minimal, (connection) → APDS9930Minimal
|
|
9
|
+
// initialises with ATIME=0xDB, PTIME=0xFF, PPULSE=8, CONTROL=0x20
|
|
10
|
+
await new Promise(r => setTimeout(r, 110));
|
|
11
|
+
setInterval(async () => {
|
|
12
|
+
const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
|
|
13
|
+
// IR-compensated lux via Ch0/Ch1 difference
|
|
14
|
+
const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
|
|
15
|
+
// 16-bit ADC value; higher = closer object
|
|
16
|
+
console.log(`lux=${lx.toFixed(1)} proximity=${p}`);
|
|
17
|
+
}, 1000);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { APDS9960Full } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Full(connection); // Create APDS9960 driver, (connection) → APDS9960Full
|
|
8
|
+
|
|
9
|
+
(async () => {
|
|
10
|
+
console.log('0x' + (await apds.chipId()).toString(16)); // Read device ID, () → number
|
|
11
|
+
|
|
12
|
+
const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
13
|
+
// burst read 0x94-0x9B latches all channels atomically
|
|
14
|
+
console.log(await apds.colorClear()); // Read clear channel, () → number 0-65535
|
|
15
|
+
console.log(await apds.colorRed()); // Read red channel, () → number 0-65535
|
|
16
|
+
console.log(await apds.colorGreen()); // Read green channel, () → number 0-65535
|
|
17
|
+
console.log(await apds.colorBlue()); // Read blue channel, () → number 0-65535
|
|
18
|
+
|
|
19
|
+
await apds.configureAls(0xB6, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
20
|
+
// sets integration time and gain for the ALS/color engine
|
|
21
|
+
await apds.configureWait(0xFF, false); // Configure wait, (wtime 0-255, wlong=false) → void
|
|
22
|
+
// sets idle period between measurement cycles
|
|
23
|
+
await apds.enableWait(true); // Enable wait engine, (enabled) → void
|
|
24
|
+
|
|
25
|
+
await apds.enableProximity(true); // Enable proximity engine, (enabled) → void
|
|
26
|
+
await apds.configureProximityLed(0, 0, 0, 1); // Configure proximity LED, (ldrive 0-3, pgain 0-3, ppulse 0-63, pplen 0-3) → void
|
|
27
|
+
// sets LED drive strength, gain, pulse count and length
|
|
28
|
+
await apds.setLedBoost(0); // Set LED boost, (boost 0-3) → void
|
|
29
|
+
// multiplies LED current: 0=100%, 1=150%, 2=200%, 3=300%
|
|
30
|
+
console.log(await apds.proximity()); // Read proximity count, () → number 0-255
|
|
31
|
+
|
|
32
|
+
await apds.alsThreshold(100, 60000); // Set ALS thresholds, (low 0-65535, high 0-65535) → void
|
|
33
|
+
await apds.proximityThreshold(10, 200); // Set proximity thresholds, (low 0-255, high 0-255) → void
|
|
34
|
+
await apds.setPersistence(0, 1); // Set persistence, (ppers 0-15, apers 0-15) → void
|
|
35
|
+
|
|
36
|
+
await apds.enableAlsInterrupt(true); // Enable ALS interrupt, (enabled) → void
|
|
37
|
+
await apds.enableProximityInterrupt(true); // Enable proximity interrupt, (enabled) → void
|
|
38
|
+
await apds.clearAlsInterrupt(); // Clear ALS interrupt, () → void
|
|
39
|
+
await apds.clearProximityInterrupt(); // Clear proximity interrupt, () → void
|
|
40
|
+
await apds.clearAllInterrupts(); // Clear all interrupts, () → void
|
|
41
|
+
|
|
42
|
+
await apds.setProximityOffset(10, -5); // Set proximity offset, (ur -127..127, dl -127..127) → void
|
|
43
|
+
// sign-magnitude encoding compensates for optical crosstalk
|
|
44
|
+
await apds.setProximityMask(false, false, false, false); // Set proximity mask, (u, d, l, r) → void
|
|
45
|
+
|
|
46
|
+
await apds.enableGesture(true); // Enable gesture engine, (enabled) → void
|
|
47
|
+
await apds.configureGesture(1, 0, 0, 1, 1, 50, 20); // Configure gesture, (ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) → void
|
|
48
|
+
// sets gain, LED drive, pulse, wait time, entry/exit thresholds
|
|
49
|
+
console.log(await apds.gestureAvailable()); // Check gesture data, () → boolean
|
|
50
|
+
console.log(await apds.gestureFifoLevel()); // Read FIFO level, () → number
|
|
51
|
+
console.log(await apds.readGestureFifo()); // Read gesture FIFO, () → Array<{u,d,l,r}>
|
|
52
|
+
await apds.clearGestureFifo(); // Clear gesture FIFO, () → void
|
|
53
|
+
await apds.enableGestureInterrupt(false); // Enable gesture interrupt, (enabled) → void
|
|
54
|
+
await apds.enableGesture(false); // Disable gesture engine, (enabled) → void
|
|
55
|
+
|
|
56
|
+
console.log(await apds.status()); // Read STATUS register, () → number
|
|
57
|
+
console.log(await apds.isAlsValid()); // Check ALS data valid, () → boolean
|
|
58
|
+
console.log(await apds.isProximityValid()); // Check proximity valid, () → boolean
|
|
59
|
+
console.log(await apds.isAlsSaturated()); // Check ALS saturated, () → boolean
|
|
60
|
+
console.log(await apds.isProximitySaturated()); // Check proximity saturated, () → boolean
|
|
61
|
+
|
|
62
|
+
await apds.enableProximity(false);
|
|
63
|
+
await connection.close();
|
|
64
|
+
})();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { APDS9960Full } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Full(connection); // Create APDS9960 driver, (connection) → APDS9960Full
|
|
8
|
+
|
|
9
|
+
// --- Monitor ambient light with adaptive integration time ---
|
|
10
|
+
// Start with the default 200 ms integration (ATIME=0xB6). When the clear
|
|
11
|
+
// channel approaches saturation (>90% of max count), halve the integration
|
|
12
|
+
// time by doubling ATIME to prevent overflow.
|
|
13
|
+
let atime = 0xB6;
|
|
14
|
+
(async () => {
|
|
15
|
+
await apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
16
|
+
})();
|
|
17
|
+
|
|
18
|
+
setInterval(async () => {
|
|
19
|
+
while (!(await apds.isAlsValid())) {} // Check ALS data valid, () → boolean
|
|
20
|
+
|
|
21
|
+
const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
22
|
+
const lux = -0.32466 * red + 1.57837 * green + -0.73191 * blue;
|
|
23
|
+
console.log(`C=${clear} R=${red} G=${green} B=${blue} lux~${lux.toFixed(0)}`);
|
|
24
|
+
|
|
25
|
+
// --- Adaptive integration: reduce time when saturated ---
|
|
26
|
+
// At saturation the sensor clips; shortening integration recovers
|
|
27
|
+
// headroom at the cost of reduced sensitivity in low light.
|
|
28
|
+
if ((await apds.isAlsSaturated()) && atime < 0xFE) { // Check ALS saturated, () → boolean
|
|
29
|
+
atime = atime + Math.floor((256 - atime) / 2);
|
|
30
|
+
if (atime > 0xFE) atime = 0xFE;
|
|
31
|
+
await apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
32
|
+
console.log(`[SATURATED — reducing integration time, ATIME=0x${atime.toString(16)}]`);
|
|
33
|
+
}
|
|
34
|
+
}, 1000);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { APDS9960Minimal } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Minimal(connection); // Create APDS9960 driver, (connection) → APDS9960Minimal
|
|
8
|
+
|
|
9
|
+
setInterval(async () => {
|
|
10
|
+
const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
11
|
+
console.log(`C=${clear} R=${red} G=${green} B=${blue}`);
|
|
12
|
+
}, 1000);
|
|
@@ -1,43 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
const { AS5600Full } = require('
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AS5600Full } = require('../../../src/chips/magnetometer/as5600');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const as5600 = new AS5600Full(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Full(connection); // Create AS5600 driver, (connection) → AS5600Full
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
console.log(as5600.
|
|
15
|
-
console.log(as5600.
|
|
16
|
-
console.log(as5600.
|
|
12
|
+
(async () => {
|
|
13
|
+
// --- Status and magnet checks ---
|
|
14
|
+
console.log(await as5600.isMagnetDetected()); // Check magnet present, () → bool
|
|
15
|
+
console.log(await as5600.isMagnetTooStrong()); // Check magnet too strong, () → bool
|
|
16
|
+
console.log(await as5600.isMagnetTooWeak()); // Check magnet too weak, () → bool
|
|
17
|
+
console.log((await as5600.statusByte()).toString(16)); // Read raw status, () → int
|
|
17
18
|
|
|
18
|
-
// --- Angle readings ---
|
|
19
|
-
console.log(as5600.angle());
|
|
20
|
-
console.log(as5600.angleRaw());
|
|
21
|
-
console.log(as5600.rawAngle());
|
|
22
|
-
console.log(as5600.rawAngleDegrees());
|
|
19
|
+
// --- Angle readings ---
|
|
20
|
+
console.log(await as5600.angle()); // Read absolute angle, () → float degrees
|
|
21
|
+
console.log(await as5600.angleRaw()); // Read scaled angle count, () → int 0-4095
|
|
22
|
+
console.log(await as5600.rawAngle()); // Read raw unscaled angle, () → int 0-4095
|
|
23
|
+
console.log(await as5600.rawAngleDegrees()); // Read raw angle in degrees, () → float degrees
|
|
23
24
|
|
|
24
|
-
// --- Diagnostics ---
|
|
25
|
-
console.log(as5600.agc());
|
|
26
|
-
console.log(as5600.magnitude());
|
|
25
|
+
// --- Diagnostics ---
|
|
26
|
+
console.log(await as5600.agc()); // Read AGC value, () → int
|
|
27
|
+
console.log(await as5600.magnitude()); // Read CORDIC magnitude, () → int
|
|
27
28
|
|
|
28
|
-
// --- Position configuration (volatile) ---
|
|
29
|
-
console.log(as5600.zeroPosition());
|
|
30
|
-
console.log(as5600.maxPosition());
|
|
31
|
-
console.log(as5600.maxAngle());
|
|
29
|
+
// --- Position configuration (volatile) ---
|
|
30
|
+
console.log(await as5600.zeroPosition()); // Read ZPOS, () → int 0-4095
|
|
31
|
+
console.log(await as5600.maxPosition()); // Read MPOS, () → int 0-4095
|
|
32
|
+
console.log(await as5600.maxAngle()); // Read MANG, () → int 0-4095
|
|
32
33
|
|
|
33
|
-
as5600.setZeroPosition(0);
|
|
34
|
-
as5600.setMaxPosition(4095);
|
|
35
|
-
as5600.setMaxAngle(2048);
|
|
34
|
+
await as5600.setZeroPosition(0); // Set zero position, (pos 0-4095) → None
|
|
35
|
+
await as5600.setMaxPosition(4095); // Set max position, (pos 0-4095) → None
|
|
36
|
+
await as5600.setMaxAngle(2048); // Set max angle span, (span 0-4095) → None
|
|
36
37
|
|
|
37
|
-
// --- Configure power mode and output ---
|
|
38
|
-
as5600.configure(0, 0, 0, 0, 0, 0, false);
|
|
38
|
+
// --- Configure power mode and output ---
|
|
39
|
+
await 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
|
|
|
40
|
-
// --- Burn count ---
|
|
41
|
-
console.log(as5600.burnCount());
|
|
41
|
+
// --- Burn count ---
|
|
42
|
+
console.log(await as5600.burnCount()); // Read burn count, () → int 0-3
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
await connection.close();
|
|
45
|
+
})();
|
|
@@ -1,49 +1,51 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
const { AS5600Full } = require('
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AS5600Full } = require('../../../src/chips/magnetometer/as5600');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const as5600 = new AS5600Full(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Full(connection);
|
|
11
11
|
|
|
12
12
|
// --- Motor feedback monitor: read angle 10 times per second ---
|
|
13
13
|
// AGC monitoring detects magnet distance drift; status changes alert to magnet removal.
|
|
14
14
|
// In 5 V mode, target AGC ≈ 128; in 3.3 V mode, target AGC ≈ 64.
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
(async () => {
|
|
17
|
+
let prevStatus = await as5600.statusByte();
|
|
18
|
+
|
|
19
|
+
for (let n = 0; n < 10; n++) {
|
|
20
|
+
const a = await as5600.angle(); // Read absolute angle, () → float degrees
|
|
21
|
+
const r = await as5600.rawAngle(); // Read raw unscaled angle, () → int 0-4095
|
|
22
|
+
const g = await as5600.agc(); // Read AGC value, () → int
|
|
23
|
+
|
|
24
|
+
// --- Check for status changes (magnet inserted/removed) ---
|
|
25
|
+
const status = await as5600.statusByte();
|
|
26
|
+
if (status !== prevStatus) {
|
|
27
|
+
if (!(await as5600.isMagnetDetected())) {
|
|
28
|
+
console.log('[MAGNET REMOVED] MD=0');
|
|
29
|
+
} else {
|
|
30
|
+
console.log('[MAGNET DETECTED] MD=1 MH=%d ML=%d',
|
|
31
|
+
await as5600.isMagnetTooStrong(), await as5600.isMagnetTooWeak());
|
|
32
|
+
}
|
|
33
|
+
prevStatus = status;
|
|
31
34
|
}
|
|
32
|
-
prevStatus = status;
|
|
33
|
-
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
// --- AGC health check ---
|
|
37
|
+
if (await as5600.isMagnetDetected()) {
|
|
38
|
+
let tag = '[OK]';
|
|
39
|
+
if (g < 64 || g > 192) {
|
|
40
|
+
tag = '[AGC low — magnet weak or too far]';
|
|
41
|
+
}
|
|
42
|
+
console.log('angle=%.2f° raw=%d agc=%d %s', a, r, g, tag);
|
|
40
43
|
}
|
|
41
|
-
console.log('angle=%.2f° raw=%d agc=%d %s', a, r, g, tag);
|
|
42
|
-
}
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
45
|
+
// 100 ms synchronous delay
|
|
46
|
+
const end = Date.now() + 100;
|
|
47
|
+
while (Date.now() < end) {}
|
|
48
|
+
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
await connection.close();
|
|
51
|
+
})();
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
const { AS5600Minimal } = require('
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { AS5600Minimal } = require('../../../src/chips/magnetometer/as5600');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const as5600 = new AS5600Minimal(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const as5600 = new AS5600Minimal(connection); // Create AS5600 driver, (connection) → AS5600Minimal
|
|
11
11
|
|
|
12
|
-
setInterval(() => {
|
|
13
|
-
const a = as5600.angle();
|
|
14
|
-
const r = as5600.angleRaw();
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const a = await as5600.angle(); // Read absolute angle, () → float degrees
|
|
14
|
+
const r = await as5600.angleRaw(); // Read scaled angle count, () → int 0-4095
|
|
15
15
|
console.log('angle=%.2f° raw=%d', a, r);
|
|
16
16
|
}, 1000);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { HMC5883LFull } = require('../../../src/chips/magnetometer/hmc5883l');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const hmc5883l = new HMC5883LFull(connection); // Create HMC5883L driver, (connection) → HMC5883LFull
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
// --- Identification ---
|
|
14
|
+
const [idA, idB, idC] = await hmc5883l.identify(); // Read ID registers, () → (int, int, int)
|
|
15
|
+
console.log('ID: 0x%s 0x%s 0x%s', idA.toString(16), idB.toString(16), idC.toString(16));
|
|
16
|
+
|
|
17
|
+
// --- Status ---
|
|
18
|
+
console.log('Status: 0x%s', (await hmc5883l.status()).toString(16)); // Read raw status, () → int
|
|
19
|
+
console.log('Data ready:', await hmc5883l.dataReady()); // Check data ready, () → bool
|
|
20
|
+
|
|
21
|
+
// --- Magnetic field readings ---
|
|
22
|
+
const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
|
|
23
|
+
console.log('X=%.6f T Y=%.6f T Z=%.6f T', x, y, z);
|
|
24
|
+
|
|
25
|
+
// --- Configuration ---
|
|
26
|
+
await hmc5883l.configure(15, 8, 1); // Configure chip, (odr 0.75-75 Hz, averaging 1/2/4/8, gain 0-7) → None
|
|
27
|
+
// writes Config A and B registers
|
|
28
|
+
await hmc5883l.setGain(2); // Set gain, (gain 0-7) → None
|
|
29
|
+
// updates GN bits in Config B
|
|
30
|
+
await hmc5883l.setMode('single'); // Set operating mode, ('continuous'|'single'|'idle') → None
|
|
31
|
+
// writes MD bits in Mode Register
|
|
32
|
+
|
|
33
|
+
// --- Single-shot measurement ---
|
|
34
|
+
await new Promise(r => setTimeout(r, 6));
|
|
35
|
+
const single = await hmc5883l.singleMeasurement(); // Single-shot measurement, () → { x: float T, y: float T, z: float T }
|
|
36
|
+
// writes single-measurement mode, waits 6 ms, reads all axes
|
|
37
|
+
console.log('Single: X=%.6f T Y=%.6f T Z=%.6f T', single.x, single.y, single.z);
|
|
38
|
+
|
|
39
|
+
await hmc5883l.setMode('continuous'); // Set operating mode, ('continuous'|'single'|'idle') → None
|
|
40
|
+
|
|
41
|
+
// --- Self-test ---
|
|
42
|
+
const selfTest = await hmc5883l.selfTest(true); // Self-test with positive bias, (positive=bool) → { x: float T, y: float T, z: float T }
|
|
43
|
+
// configures bias, takes measurement, restores normal mode
|
|
44
|
+
console.log('Self-test: X=%.6f T Y=%.6f T Z=%.6f T', selfTest.x, selfTest.y, selfTest.z);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { HMC5883LFull } = require('../../../src/chips/magnetometer/hmc5883l');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const hmc5883l = new HMC5883LFull(connection);
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
// --- Configure for electronic compass ---
|
|
14
|
+
// 8-sample averaging at 15 Hz suppresses noise; ±1.3 Ga gain covers Earth's field (~0.5 Ga).
|
|
15
|
+
await hmc5883l.configure(15, 8, 1); // Configure chip, (odr 0.75-75 Hz, averaging 1/2/4/8, gain 0-7) → None
|
|
16
|
+
|
|
17
|
+
console.log('Electronic compass demo — hold sensor flat, rotate horizontally');
|
|
18
|
+
console.log('Vertical mount warning: |Z| > 30 uT indicates tilt compensation needed');
|
|
19
|
+
console.log('');
|
|
20
|
+
|
|
21
|
+
// --- Sample and compute heading ---
|
|
22
|
+
// User rotates the sensor horizontally; we compute heading from X/Y axes.
|
|
23
|
+
// At n=5, user is prompted to tilt vertically to demonstrate Z-axis detection.
|
|
24
|
+
for (let n = 0; n < 10; n++) {
|
|
25
|
+
while (!(await hmc5883l.dataReady())) { // Check data ready, () → bool
|
|
26
|
+
await new Promise(r => setTimeout(r, 1));
|
|
27
|
+
}
|
|
28
|
+
const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
|
|
29
|
+
|
|
30
|
+
// --- Compute heading from X and Y ---
|
|
31
|
+
if (x !== null && y !== null) {
|
|
32
|
+
const heading = Math.atan2(y, x) * 180 / Math.PI;
|
|
33
|
+
const h = heading < 0 ? heading + 360 : heading;
|
|
34
|
+
console.log('Heading: %.1f°', h);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// --- Vertical mount detection ---
|
|
38
|
+
if (z !== null && Math.abs(z) > 30e-6) {
|
|
39
|
+
console.log('[TILT WARNING] Z=%.1f uT — tilt compensation needed', z * 1e6);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (n === 4) {
|
|
43
|
+
console.log('>>> Now tilt sensor vertically <<<');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await new Promise(r => setTimeout(r, 500));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { HMC5883LMinimal } = require('../../../src/chips/magnetometer/hmc5883l');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const hmc5883l = new HMC5883LMinimal(connection); // Create HMC5883L driver, (connection) → HMC5883LMinimal
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
|
|
14
|
+
console.log('X=%.6f T Y=%.6f T Z=%.6f T', x, y, z);
|
|
15
|
+
}, 1000);
|