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,393 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BAND_BASE_KHZ = [87000, 76000, 76000, 65000];
|
|
4
|
+
const SPACE_KHZ = [100, 200, 50, 25];
|
|
5
|
+
|
|
6
|
+
const STC_TIMEOUT_MS = 500;
|
|
7
|
+
const STC_POLL_MS = 1;
|
|
8
|
+
|
|
9
|
+
// Undocumented, measured on real hardware: after standby wake-up or a soft
|
|
10
|
+
// reset, the chip needs this long before it will lock onto a subsequent TUNE
|
|
11
|
+
// (FM_READY otherwise never asserts, even after minutes). Same requirement as
|
|
12
|
+
// the datasheet's power-up sequencing, just not called out for these two cases.
|
|
13
|
+
const RESET_RECOVERY_MS = 250;
|
|
14
|
+
// Undocumented, measured on real hardware: FM_READY lags STC by up to ~20 ms
|
|
15
|
+
// after any register write.
|
|
16
|
+
const READY_SETTLE_MS = 30;
|
|
17
|
+
|
|
18
|
+
const DHIZ = 0x8000;
|
|
19
|
+
const DMUTE = 0x4000;
|
|
20
|
+
const MONO = 0x2000;
|
|
21
|
+
const BASS = 0x1000;
|
|
22
|
+
const SEEKUP = 0x0200;
|
|
23
|
+
const SEEK = 0x0100;
|
|
24
|
+
const SKMODE = 0x0080;
|
|
25
|
+
const RDS_EN = 0x0008;
|
|
26
|
+
const NEW_METHOD = 0x0004;
|
|
27
|
+
const SOFT_RESET = 0x0002;
|
|
28
|
+
const ENABLE = 0x0001;
|
|
29
|
+
|
|
30
|
+
const TUNE = 0x0010;
|
|
31
|
+
|
|
32
|
+
const DE = 0x0800;
|
|
33
|
+
const SOFTMUTE_EN = 0x0200;
|
|
34
|
+
const AFCD = 0x0100;
|
|
35
|
+
|
|
36
|
+
const INT_MODE = 0x8000;
|
|
37
|
+
|
|
38
|
+
const BAND_65M_50M = 0x0200;
|
|
39
|
+
|
|
40
|
+
const RDSR = 0x8000;
|
|
41
|
+
const STC = 0x4000;
|
|
42
|
+
const SF = 0x2000;
|
|
43
|
+
const ST = 0x0400;
|
|
44
|
+
|
|
45
|
+
const FM_TRUE = 0x0100;
|
|
46
|
+
const FM_READY = 0x0080;
|
|
47
|
+
|
|
48
|
+
function sleep(ms) {
|
|
49
|
+
const end = Date.now() + ms;
|
|
50
|
+
while (Date.now() < end) {}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function freqToChan(band, space, eastEurope50m, frequencyMhz) {
|
|
54
|
+
const base = (band === 3 && eastEurope50m) ? 50000 : BAND_BASE_KHZ[band];
|
|
55
|
+
const freqKhz = Math.round(frequencyMhz * 1000);
|
|
56
|
+
let chan = Math.round((freqKhz - base) / SPACE_KHZ[space]);
|
|
57
|
+
if (chan < 0) chan = 0;
|
|
58
|
+
if (chan > 1023) chan = 1023;
|
|
59
|
+
return chan;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function chanToFreq(band, space, eastEurope50m, chan) {
|
|
63
|
+
const base = (band === 3 && eastEurope50m) ? 50000 : BAND_BASE_KHZ[band];
|
|
64
|
+
return (base + chan * SPACE_KHZ[space]) / 1000.0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* RDA5807M single-chip FM stereo radio tuner — minimal interface.
|
|
69
|
+
*
|
|
70
|
+
* Tunes to a station, adjusts volume, mutes, and seeks the next station.
|
|
71
|
+
* No configuration required beyond the connection.
|
|
72
|
+
*
|
|
73
|
+
* Unlike most chips in this project, the RDA5807M has no register-pointer
|
|
74
|
+
* byte: writes always start at the fixed register 0x02 and reads always
|
|
75
|
+
* start at the fixed register 0x0A. This driver keeps an in-memory shadow
|
|
76
|
+
* of registers 0x02-0x07 (6 big-endian 16-bit words) and rewrites all of
|
|
77
|
+
* them on every change, since the chip cannot be told to start a write
|
|
78
|
+
* anywhere else.
|
|
79
|
+
*
|
|
80
|
+
* Constructor caveat: JS constructors cannot be async, but the initial tune
|
|
81
|
+
* needs to poll STC (Seek/Tune Complete) before the TUNE bit can be safely
|
|
82
|
+
* cleared. The constructor fires this off unawaited (same fire-and-forget
|
|
83
|
+
* convention used for every other constructor-time register write in this
|
|
84
|
+
* port) — in practice the promise settles before the constructor's next
|
|
85
|
+
* synchronous statement runs, since I2C/SMBus connections are synchronous
|
|
86
|
+
* under the hood, but this is not guaranteed the way a real blocking wait
|
|
87
|
+
* was in the pre-async version. Call `await radio.frequency()` (or any other
|
|
88
|
+
* async method) once before relying on tuned state if this matters for your
|
|
89
|
+
* use case.
|
|
90
|
+
*/
|
|
91
|
+
class RDA5807MMinimal {
|
|
92
|
+
static BAND_US_EUROPE = 0;
|
|
93
|
+
static BAND_JAPAN = 1;
|
|
94
|
+
static BAND_WORLD = 2;
|
|
95
|
+
static BAND_EAST_EUROPE = 3;
|
|
96
|
+
|
|
97
|
+
static SPACE_100K = 0;
|
|
98
|
+
static SPACE_200K = 1;
|
|
99
|
+
static SPACE_50K = 2;
|
|
100
|
+
static SPACE_25K = 3;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @param {import('../../connection/connection').Connection} connection - I²C connection bound to address 0x10.
|
|
104
|
+
* @param {number} [frequencyMhz=100.0] - Initial frequency in MHz.
|
|
105
|
+
* @param {number} [volume=8] - Initial volume, 0 (mute) to 15 (max).
|
|
106
|
+
*/
|
|
107
|
+
constructor(connection, frequencyMhz = 100.0, volume = 8) {
|
|
108
|
+
this._conn = connection;
|
|
109
|
+
this._band = RDA5807MMinimal.BAND_WORLD;
|
|
110
|
+
this._space = RDA5807MMinimal.SPACE_100K;
|
|
111
|
+
this._eastEurope50m = false;
|
|
112
|
+
|
|
113
|
+
const ctrl = DHIZ | DMUTE | SKMODE | NEW_METHOD | ENABLE;
|
|
114
|
+
const chan = freqToChan(this._band, this._space, this._eastEurope50m, frequencyMhz);
|
|
115
|
+
const chanReg = (chan << 6) | TUNE | (this._band << 2) | this._space;
|
|
116
|
+
const r4 = SOFTMUTE_EN | DE;
|
|
117
|
+
const r5 = INT_MODE | (8 << 8) | (volume & 0x0F);
|
|
118
|
+
const r6 = 0x0000;
|
|
119
|
+
const r7 = (16 << 10) | BAND_65M_50M | 0x0002;
|
|
120
|
+
|
|
121
|
+
this._regs = [ctrl, chanReg, r4, r5, r6, r7];
|
|
122
|
+
this._currentFreq = frequencyMhz;
|
|
123
|
+
this._writeRegs();
|
|
124
|
+
this._waitStc().then(() => { this._regs[1] &= ~TUNE; });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async _writeRegs() {
|
|
128
|
+
const buf = Buffer.alloc(12);
|
|
129
|
+
for (let i = 0; i < 6; i++) buf.writeUInt16BE(this._regs[i] & 0xFFFF, i * 2);
|
|
130
|
+
await this._conn.write(buf);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async _readStatus(n) {
|
|
134
|
+
const buf = await this._conn.read(n);
|
|
135
|
+
const words = [];
|
|
136
|
+
for (let i = 0; i < n / 2; i++) words.push(buf.readUInt16BE(i * 2));
|
|
137
|
+
return words;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async _waitStc() {
|
|
141
|
+
let elapsed = 0;
|
|
142
|
+
while (elapsed < STC_TIMEOUT_MS) {
|
|
143
|
+
const [statusA] = await this._readStatus(2);
|
|
144
|
+
if (statusA & STC) return statusA;
|
|
145
|
+
sleep(STC_POLL_MS);
|
|
146
|
+
elapsed += STC_POLL_MS;
|
|
147
|
+
}
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Tune to a frequency, blocking until the tune completes.
|
|
153
|
+
* @param {number} frequencyMhz - Target frequency in MHz.
|
|
154
|
+
* @returns {Promise<void>}
|
|
155
|
+
*/
|
|
156
|
+
async setFrequency(frequencyMhz) {
|
|
157
|
+
const chan = freqToChan(this._band, this._space, this._eastEurope50m, frequencyMhz);
|
|
158
|
+
this._regs[1] = (chan << 6) | TUNE | (this._band << 2) | this._space;
|
|
159
|
+
this._currentFreq = frequencyMhz;
|
|
160
|
+
await this._writeRegs();
|
|
161
|
+
await this._waitStc();
|
|
162
|
+
this._regs[1] &= ~TUNE;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @returns {Promise<number>} Currently tuned frequency in MHz, derived from READCHAN. */
|
|
166
|
+
async frequency() {
|
|
167
|
+
const [statusA] = await this._readStatus(2);
|
|
168
|
+
const readchan = statusA & 0x03FF;
|
|
169
|
+
return chanToFreq(this._band, this._space, this._eastEurope50m, readchan);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Set the output volume.
|
|
174
|
+
* @param {number} level - Volume 0 (mute) to 15 (max), logarithmic scale.
|
|
175
|
+
* @returns {Promise<void>}
|
|
176
|
+
*/
|
|
177
|
+
async setVolume(level) {
|
|
178
|
+
this._regs[3] = (this._regs[3] & ~0x000F) | (level & 0x0F);
|
|
179
|
+
await this._writeRegs();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Mute or unmute the audio output.
|
|
184
|
+
* @param {boolean} enable - True to mute, false for normal operation.
|
|
185
|
+
* @returns {Promise<void>}
|
|
186
|
+
*/
|
|
187
|
+
async mute(enable) {
|
|
188
|
+
if (enable) this._regs[0] &= ~DMUTE;
|
|
189
|
+
else this._regs[0] |= DMUTE;
|
|
190
|
+
await this._writeRegs();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Seek to the next station, blocking until the seek completes.
|
|
195
|
+
* @param {boolean} [up=true] - True to seek upward, false to seek downward.
|
|
196
|
+
* @returns {Promise<number|null>} New frequency in MHz, or null if the seek failed.
|
|
197
|
+
*/
|
|
198
|
+
async seek(up = true) {
|
|
199
|
+
if (up) this._regs[0] |= SEEKUP;
|
|
200
|
+
else this._regs[0] &= ~SEEKUP;
|
|
201
|
+
this._regs[0] |= SEEK;
|
|
202
|
+
await this._writeRegs();
|
|
203
|
+
const statusA = await this._waitStc();
|
|
204
|
+
this._regs[0] &= ~SEEK;
|
|
205
|
+
await this._writeRegs();
|
|
206
|
+
|
|
207
|
+
if (statusA & SF) return null;
|
|
208
|
+
const readchan = statusA & 0x03FF;
|
|
209
|
+
const freq = chanToFreq(this._band, this._space, this._eastEurope50m, readchan);
|
|
210
|
+
this._currentFreq = freq;
|
|
211
|
+
return freq;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* RDA5807M full interface — extends RDA5807MMinimal with band/spacing
|
|
217
|
+
* configuration, RDS, status, and power management.
|
|
218
|
+
*/
|
|
219
|
+
class RDA5807MFull extends RDA5807MMinimal {
|
|
220
|
+
/**
|
|
221
|
+
* Reconfigure tuner-level settings. Only options that are provided are changed.
|
|
222
|
+
* Changing band or space re-tunes to the current frequency.
|
|
223
|
+
* @param {object} [options]
|
|
224
|
+
* @param {number} [options.band] - BAND_US_EUROPE, BAND_JAPAN, BAND_WORLD, or BAND_EAST_EUROPE.
|
|
225
|
+
* @param {number} [options.space] - SPACE_100K, SPACE_200K, SPACE_50K, or SPACE_25K.
|
|
226
|
+
* @param {boolean} [options.deEmphasis] - True for 50 µs, false for 75 µs.
|
|
227
|
+
* @param {number} [options.seekThreshold] - Seek SNR threshold, 0-15.
|
|
228
|
+
* @param {boolean} [options.seekMode] - True to stop seeking at the band limit.
|
|
229
|
+
* @param {number} [options.clkMode] - Reference clock select, 0-7.
|
|
230
|
+
* @param {boolean} [options.afcDisable] - True to disable AFC.
|
|
231
|
+
* @param {boolean} [options.eastEurope50m] - Sub-band select when band is BAND_EAST_EUROPE.
|
|
232
|
+
* @returns {Promise<void>}
|
|
233
|
+
*/
|
|
234
|
+
async configure(options = {}) {
|
|
235
|
+
const { band, space, deEmphasis, seekThreshold, seekMode, clkMode, afcDisable, eastEurope50m } = options;
|
|
236
|
+
let retune = false;
|
|
237
|
+
const currentFreq = await this.frequency();
|
|
238
|
+
|
|
239
|
+
if (band !== undefined && band !== this._band) {
|
|
240
|
+
this._band = band;
|
|
241
|
+
retune = true;
|
|
242
|
+
}
|
|
243
|
+
if (space !== undefined && space !== this._space) {
|
|
244
|
+
this._space = space;
|
|
245
|
+
retune = true;
|
|
246
|
+
}
|
|
247
|
+
if (eastEurope50m !== undefined && eastEurope50m !== this._eastEurope50m) {
|
|
248
|
+
this._eastEurope50m = eastEurope50m;
|
|
249
|
+
retune = true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this._regs[1] = (this._regs[1] & ~0x000F) | (this._band << 2) | this._space;
|
|
253
|
+
|
|
254
|
+
if (deEmphasis !== undefined) {
|
|
255
|
+
if (deEmphasis) this._regs[2] |= DE;
|
|
256
|
+
else this._regs[2] &= ~DE;
|
|
257
|
+
}
|
|
258
|
+
if (afcDisable !== undefined) {
|
|
259
|
+
if (afcDisable) this._regs[2] |= AFCD;
|
|
260
|
+
else this._regs[2] &= ~AFCD;
|
|
261
|
+
}
|
|
262
|
+
if (seekThreshold !== undefined) {
|
|
263
|
+
this._regs[3] = (this._regs[3] & ~0x0F00) | ((seekThreshold & 0x0F) << 8);
|
|
264
|
+
}
|
|
265
|
+
if (seekMode !== undefined) {
|
|
266
|
+
if (seekMode) this._regs[0] |= SKMODE;
|
|
267
|
+
else this._regs[0] &= ~SKMODE;
|
|
268
|
+
}
|
|
269
|
+
if (clkMode !== undefined) {
|
|
270
|
+
this._regs[0] = (this._regs[0] & ~0x0070) | ((clkMode & 0x07) << 4);
|
|
271
|
+
}
|
|
272
|
+
if (eastEurope50m !== undefined) {
|
|
273
|
+
if (eastEurope50m) this._regs[5] &= ~BAND_65M_50M;
|
|
274
|
+
else this._regs[5] |= BAND_65M_50M;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (retune) await this.setFrequency(currentFreq);
|
|
278
|
+
else await this._writeRegs();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** @param {boolean} enable - True to enable bass boost.
|
|
282
|
+
* @returns {Promise<void>} */
|
|
283
|
+
async setBassBoost(enable) {
|
|
284
|
+
if (enable) this._regs[0] |= BASS;
|
|
285
|
+
else this._regs[0] &= ~BASS;
|
|
286
|
+
await this._writeRegs();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** @param {boolean} enable - True to force mono, false to allow stereo.
|
|
290
|
+
* @returns {Promise<void>} */
|
|
291
|
+
async setMono(enable) {
|
|
292
|
+
if (enable) this._regs[0] |= MONO;
|
|
293
|
+
else this._regs[0] &= ~MONO;
|
|
294
|
+
await this._writeRegs();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** @param {boolean} enable - True to enable soft mute (chip default).
|
|
298
|
+
* @returns {Promise<void>} */
|
|
299
|
+
async setSoftmute(enable) {
|
|
300
|
+
if (enable) this._regs[2] |= SOFTMUTE_EN;
|
|
301
|
+
else this._regs[2] &= ~SOFTMUTE_EN;
|
|
302
|
+
await this._writeRegs();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** @param {boolean} enable - True to enable RDS/RBDS.
|
|
306
|
+
* @returns {Promise<void>} */
|
|
307
|
+
async enableRds(enable) {
|
|
308
|
+
if (enable) this._regs[0] |= RDS_EN;
|
|
309
|
+
else this._regs[0] &= ~RDS_EN;
|
|
310
|
+
await this._writeRegs();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** @returns {Promise<boolean>} True if a new RDS/RBDS group is available. */
|
|
314
|
+
async rdsReady() {
|
|
315
|
+
const [statusA] = await this._readStatus(2);
|
|
316
|
+
return !!(statusA & RDSR);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Read the four raw RDS/RBDS blocks, if a new group is ready. Does not
|
|
321
|
+
* decode group content — the caller interprets the raw blocks.
|
|
322
|
+
* @returns {Promise<number[]|null>} [blockA, blockB, blockC, blockD], or null.
|
|
323
|
+
*/
|
|
324
|
+
async readRdsGroup() {
|
|
325
|
+
const [statusA, , blockA, blockB, blockC, blockD] = await this._readStatus(12);
|
|
326
|
+
if (!(statusA & RDSR)) return null;
|
|
327
|
+
return [blockA, blockB, blockC, blockD];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** @returns {Promise<boolean>} True if the current station is being received in stereo. */
|
|
331
|
+
async isStereo() {
|
|
332
|
+
const [statusA] = await this._readStatus(2);
|
|
333
|
+
return !!(statusA & ST);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** @returns {Promise<boolean>} True if the current channel is a real station. */
|
|
337
|
+
async isStation() {
|
|
338
|
+
const [, statusB] = await this._readStatus(4);
|
|
339
|
+
return !!(statusB & FM_TRUE);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** @returns {Promise<boolean>} True if the tuner is ready. */
|
|
343
|
+
async isReady() {
|
|
344
|
+
const [, statusB] = await this._readStatus(4);
|
|
345
|
+
return !!(statusB & FM_READY);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** @returns {Promise<number>} Raw RSSI, 0 (weakest) to 127 (strongest), logarithmic. */
|
|
349
|
+
async signalStrength() {
|
|
350
|
+
const [, statusB] = await this._readStatus(4);
|
|
351
|
+
return (statusB >> 9) & 0x7F;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Power the chip down or up. Powering back up clears the tuner's PLL
|
|
356
|
+
* lock, so waking from standby blocks briefly for the chip to recover,
|
|
357
|
+
* then re-tunes to the last known frequency (mirroring the datasheet's
|
|
358
|
+
* power-up sequencing, which the chip otherwise never recovers from on
|
|
359
|
+
* its own).
|
|
360
|
+
* @param {boolean} enable - True to power down, false to power up.
|
|
361
|
+
* @returns {Promise<void>}
|
|
362
|
+
*/
|
|
363
|
+
async standby(enable) {
|
|
364
|
+
if (enable) this._regs[0] &= ~ENABLE;
|
|
365
|
+
else this._regs[0] |= ENABLE;
|
|
366
|
+
await this._writeRegs();
|
|
367
|
+
if (!enable) {
|
|
368
|
+
sleep(RESET_RECOVERY_MS);
|
|
369
|
+
await this.setFrequency(this._currentFreq);
|
|
370
|
+
sleep(READY_SETTLE_MS);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Pulse the soft-reset bit, then re-apply the current configuration.
|
|
376
|
+
* A soft reset restores the chip's power-on register defaults and
|
|
377
|
+
* clears the tuner's PLL lock, so this blocks briefly for the chip to
|
|
378
|
+
* recover, then re-tunes to the last known frequency (the chip never
|
|
379
|
+
* reacquires lock on its own otherwise).
|
|
380
|
+
* @returns {Promise<void>}
|
|
381
|
+
*/
|
|
382
|
+
async softReset() {
|
|
383
|
+
this._regs[0] |= SOFT_RESET;
|
|
384
|
+
await this._writeRegs();
|
|
385
|
+
this._regs[0] &= ~SOFT_RESET;
|
|
386
|
+
await this._writeRegs();
|
|
387
|
+
sleep(RESET_RECOVERY_MS);
|
|
388
|
+
await this.setFrequency(this._currentFreq);
|
|
389
|
+
sleep(READY_SETTLE_MS);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
module.exports = { RDA5807MMinimal, RDA5807MFull };
|