periph 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/README.md +18 -0
  2. package/examples/accelerometer/bma150/complete.js +62 -0
  3. package/examples/accelerometer/bma150/demo.js +65 -0
  4. package/examples/accelerometer/bma150/minimal.js +18 -0
  5. package/examples/adc_dac/hy3131/complete.js +66 -0
  6. package/examples/adc_dac/hy3131/demo.js +37 -0
  7. package/examples/adc_dac/hy3131/minimal.js +18 -0
  8. package/examples/discovery/complete.js +25 -0
  9. package/examples/discovery/demo.js +30 -0
  10. package/examples/discovery/minimal.js +12 -0
  11. package/examples/display/il0373/complete.js +71 -0
  12. package/examples/display/il0373/demo.js +49 -0
  13. package/examples/display/il0373/minimal.js +26 -0
  14. package/examples/display/ssd1331/complete.js +83 -0
  15. package/examples/display/ssd1331/demo.js +49 -0
  16. package/examples/display/ssd1331/minimal.js +27 -0
  17. package/examples/display/st7036/complete.js +71 -0
  18. package/examples/display/st7036/demo.js +56 -0
  19. package/examples/display/st7036/minimal.js +21 -0
  20. package/examples/display/uc1611s/complete.js +135 -0
  21. package/examples/display/uc1611s/complete_spi.js +128 -0
  22. package/examples/display/uc1611s/demo.js +90 -0
  23. package/examples/display/uc1611s/demo_spi.js +91 -0
  24. package/examples/display/uc1611s/minimal.js +28 -0
  25. package/examples/display/uc1611s/minimal_spi.js +30 -0
  26. package/examples/display/uc8159/complete.js +64 -0
  27. package/examples/display/uc8159/demo.js +41 -0
  28. package/examples/display/uc8159/minimal.js +28 -0
  29. package/examples/display/uc8276c/complete.js +73 -0
  30. package/examples/display/uc8276c/demo.js +49 -0
  31. package/examples/display/uc8276c/minimal.js +26 -0
  32. package/examples/environmental/bme280/complete.js +1 -1
  33. package/examples/environmental/bme280/demo.js +1 -1
  34. package/examples/environmental/bme280/minimal.js +1 -1
  35. package/examples/gyroscope/l3g4200d/complete.js +1 -1
  36. package/examples/gyroscope/l3g4200d/demo.js +1 -1
  37. package/examples/gyroscope/l3g4200d/minimal.js +1 -1
  38. package/examples/memory/23x1024/complete.js +54 -0
  39. package/examples/memory/23x1024/demo.js +51 -0
  40. package/examples/memory/23x1024/minimal.js +22 -0
  41. package/examples/memory/mx25l12845e/complete.js +66 -0
  42. package/examples/memory/mx25l12845e/demo.js +66 -0
  43. package/examples/memory/mx25l12845e/minimal.js +27 -0
  44. package/examples/memory/sd_spi/complete.js +55 -0
  45. package/examples/memory/sd_spi/demo.js +67 -0
  46. package/examples/memory/sd_spi/minimal.js +32 -0
  47. package/examples/motor/drv8830/complete.js +50 -0
  48. package/examples/motor/drv8830/demo.js +57 -0
  49. package/examples/motor/drv8830/minimal.js +23 -0
  50. package/examples/pressure/bmp280/complete.js +1 -1
  51. package/examples/pressure/bmp280/demo.js +1 -1
  52. package/examples/pressure/bmp280/minimal.js +1 -1
  53. package/examples/pressure/bmp581/complete.js +1 -1
  54. package/examples/pressure/bmp581/demo.js +1 -1
  55. package/examples/pressure/bmp581/minimal.js +1 -1
  56. package/examples/pressure/lps22df/complete.js +1 -1
  57. package/examples/pressure/lps22df/demo.js +1 -1
  58. package/examples/pressure/lps22df/minimal.js +1 -1
  59. package/examples/rtc/ds3231/complete.js +77 -0
  60. package/examples/rtc/ds3231/demo.js +60 -0
  61. package/examples/rtc/ds3231/minimal.js +23 -0
  62. package/examples/rtc/pcf8523/complete.js +66 -0
  63. package/examples/rtc/pcf8523/demo.js +65 -0
  64. package/examples/rtc/pcf8523/minimal.js +20 -0
  65. package/examples/temperature/mcp9808/complete.js +89 -0
  66. package/examples/temperature/mcp9808/demo.js +62 -0
  67. package/examples/temperature/mcp9808/minimal.js +21 -0
  68. package/examples/temperature/tmp117/complete.js +90 -0
  69. package/examples/temperature/tmp117/demo.js +73 -0
  70. package/examples/temperature/tmp117/minimal.js +21 -0
  71. package/examples/tof/vl53l0x/complete.js +118 -0
  72. package/examples/tof/vl53l0x/demo.js +83 -0
  73. package/examples/tof/vl53l0x/minimal.js +25 -0
  74. package/examples/tof/vl53l1x/complete.js +151 -0
  75. package/examples/tof/vl53l1x/demo.js +100 -0
  76. package/examples/tof/vl53l1x/minimal.js +25 -0
  77. package/package.json +1 -1
  78. package/src/chips/accelerometer/adxl345.js +10 -31
  79. package/src/chips/accelerometer/adxl362.js +7 -7
  80. package/src/chips/accelerometer/bma150.js +419 -0
  81. package/src/chips/adc_dac/ad7705.js +36 -9
  82. package/src/chips/adc_dac/ad7706.js +39 -9
  83. package/src/chips/adc_dac/hy3131.js +311 -0
  84. package/src/chips/display/il0373.js +607 -0
  85. package/src/chips/display/ssd1331.js +412 -0
  86. package/src/chips/display/st7036.js +401 -0
  87. package/src/chips/display/uc1611s.js +648 -0
  88. package/src/chips/display/uc8159.js +394 -0
  89. package/src/chips/display/uc8276c.js +658 -0
  90. package/src/chips/environmental/bme280.js +31 -46
  91. package/src/chips/environmental/bme680.js +38 -43
  92. package/src/chips/gas/ens160.js +18 -26
  93. package/src/chips/gyroscope/l3g4200d.js +34 -50
  94. package/src/chips/gyroscope/l3gd20h.js +24 -40
  95. package/src/chips/imu/mpu6050.js +29 -41
  96. package/src/chips/imu/mpu9250.js +45 -69
  97. package/src/chips/imu/mpu9255.js +54 -78
  98. package/src/chips/io_expander/mcp23017.js +24 -33
  99. package/src/chips/light/apds-9930.js +52 -60
  100. package/src/chips/light/apds9960.js +69 -77
  101. package/src/chips/magnetometer/as5600.js +13 -27
  102. package/src/chips/magnetometer/hmc5883l.js +32 -29
  103. package/src/chips/memory/_23x1024.js +228 -0
  104. package/src/chips/memory/_24aa02uid.js +8 -11
  105. package/src/chips/memory/mx25l12845e.js +467 -0
  106. package/src/chips/memory/sd_spi.js +486 -0
  107. package/src/chips/motor/drv8830.js +239 -0
  108. package/src/chips/other/mpr121.js +7 -7
  109. package/src/chips/power/ina219.js +5 -8
  110. package/src/chips/power/ina226.js +5 -8
  111. package/src/chips/power/ina3221.js +5 -8
  112. package/src/chips/pressure/bmp085.js +17 -11
  113. package/src/chips/pressure/bmp180.js +7 -7
  114. package/src/chips/pressure/bmp280.js +21 -33
  115. package/src/chips/pressure/bmp384.js +28 -39
  116. package/src/chips/pressure/bmp581.js +59 -70
  117. package/src/chips/pressure/lps22df.js +50 -52
  118. package/src/chips/pressure/lps28dfw.js +15 -13
  119. package/src/chips/pressure/lps33hw.js +7 -5
  120. package/src/chips/rtc/ds3231.js +504 -0
  121. package/src/chips/rtc/pcf8523.js +533 -0
  122. package/src/chips/temperature/mcp9808.js +490 -0
  123. package/src/chips/temperature/tmp117.js +471 -0
  124. package/src/chips/tof/_vl53_base.js +160 -0
  125. package/src/chips/tof/vl53l0x.js +816 -0
  126. package/src/chips/tof/vl53l1x.js +816 -0
  127. package/src/connection/connection.js +6 -4
  128. package/src/connection/dhtxx.js +5 -3
  129. package/src/connection/display.js +278 -0
  130. package/src/connection/display_mock.js +77 -0
  131. package/src/connection/hx711.js +5 -3
  132. package/src/connection/i2c.js +6 -4
  133. package/src/connection/i2c_mock.js +47 -0
  134. package/src/connection/input_pin.js +36 -5
  135. package/src/connection/register.js +14 -0
  136. package/src/connection/register_connection.js +65 -0
  137. package/src/connection/sipo.js +4 -2
  138. package/src/connection/sipo_mock.js +49 -0
  139. package/src/connection/smbus.js +6 -4
  140. package/src/connection/softspi.js +73 -0
  141. package/src/connection/spi.js +110 -23
  142. package/src/connection/spi_mock.js +50 -0
  143. package/src/connection/uart.js +2 -1
  144. package/src/discovery/discovery.js +218 -0
  145. package/src/discovery/registry.js +85 -0
  146. package/src/storage/block_device.js +95 -0
  147. package/src/storage/ram_block_device.js +72 -0
  148. package/examples/motor/.gitkeep +0 -0
  149. package/examples/temperature/.gitkeep +0 -0
  150. package/examples/tof/.gitkeep +0 -0
  151. package/src/chips/motor/.gitkeep +0 -0
  152. package/src/chips/temperature/.gitkeep +0 -0
  153. package/src/chips/tof/.gitkeep +0 -0
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
6
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
7
 
8
8
  const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
- const bmp = new BMP280Minimal(connection); // Create BMP280 driver, (connection, busType='i2c')
9
+ const bmp = new BMP280Minimal(connection); // Create BMP280 driver, (connection)
10
10
 
11
11
  (async () => {
12
12
  for (let i = 0; i < 5; i++) {
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
6
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x46', 16);
7
7
 
8
8
  const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
- const bmp = new BMP581Full(connection); // Create BMP581 driver, (connection, busType='i2c')
9
+ const bmp = new BMP581Full(connection); // Create BMP581 driver, (connection)
10
10
 
11
11
  (async () => {
12
12
  const cid = await bmp.chipId(); // Read chip ID, () → number
@@ -9,7 +9,7 @@ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
9
 
10
10
  (async () => {
11
11
  // --- Precision altimeter: 10 Hz NORMAL mode for 30 seconds ---
12
- const bmp = new BMP581Full(connection); // Create BMP581 driver, (connection, busType='i2c')
12
+ const bmp = new BMP581Full(connection); // Create BMP581 driver, (connection)
13
13
  await bmp.configure(0x17, BMP581Full.OSR_16X, BMP581Full.OSR_4X, true); // Configure chip, (odr=10Hz, osr_p=×16, osr_t=×4, press_en) → Promise<void>
14
14
 
15
15
  const pressures = [], temps = [], alts = [];
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
6
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x46', 16);
7
7
 
8
8
  const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
- const bmp = new BMP581Minimal(connection); // Create BMP581 driver, (connection, busType='i2c')
9
+ const bmp = new BMP581Minimal(connection); // Create BMP581 driver, (connection)
10
10
 
11
11
  (async () => {
12
12
  for (let i = 0; i < 5; i++) {
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
6
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5C', 16);
7
7
 
8
8
  const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
- const lps = new LPS22DFFull(connection); // Create LPS22DF driver, (connection, busType='i2c')
9
+ const lps = new LPS22DFFull(connection); // Create LPS22DF driver, (connection)
10
10
 
11
11
  (async () => {
12
12
  await lps.configure(3, 0, false, 0, true); // Configure chip, (odr=10 Hz, avg=4, enLpfp=false, lfpfCfg=0, bdu=true) → undefined
@@ -11,7 +11,7 @@ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
11
11
  // --- Indoor altimeter preset: 25 Hz, 4-sample average, low-pass filter ---
12
12
  // Low-pass at ODR/9 smooths short-term pressure noise (door slams, fans);
13
13
  // 4-sample averaging trims noise without adding visible lag.
14
- const lps = new LPS22DFFull(connection); // Create LPS22DF driver, (connection, busType='i2c')
14
+ const lps = new LPS22DFFull(connection); // Create LPS22DF driver, (connection)
15
15
  await lps.configure(4, 0, true, 1, true); // Configure chip, (odr=25 Hz, avg=4, enLpfp=true, lfpfCfg=ODR/9, bdu=true) → undefined
16
16
 
17
17
  // --- Baseline capture: 2-second stabilization then zero the altimeter ---
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
6
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5C', 16);
7
7
 
8
8
  const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
- const lps = new LPS22DFMinimal(connection); // Create LPS22DF driver, (connection, busType='i2c')
9
+ const lps = new LPS22DFMinimal(connection); // Create LPS22DF driver, (connection)
10
10
 
11
11
  (async () => {
12
12
  for (let i = 0; i < 5; i++) {
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS3231Full } = require('periph/src/chips/rtc/ds3231');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new DS3231Full(connection); // Create DS3231 Full driver, (connection)
10
+ await rtc.init(); // Confirm device presence, () → None
11
+
12
+ if (await rtc.oscillatorStopped()) { // Query oscillator-stop flag, () → bool
13
+ // true means timekeeping data may be invalid since the last check
14
+ await rtc.setDatetime(2026, 9, 22, 2, 14, 30, 0); // Set the calendar clock, (year, month, day, weekday, hour, minute, second) → None
15
+ // also clears OSF, since the time is now known-good
16
+ }
17
+
18
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, ..., second}
19
+ // inherited from DS3231Minimal
20
+ console.log(dt);
21
+
22
+ const tempC = await rtc.readTemperature(); // Read cached temperature, () → float C
23
+ // may be up to 64 s stale (autonomous conversion cycle)
24
+ console.log('cached temp:', tempC);
25
+
26
+ const freshTempC = await rtc.forceTemperatureConversion(); // Force a fresh conversion, () → float C
27
+ // sets CONV and polls BSY, max 200 ms
28
+ console.log('fresh temp:', freshTempC);
29
+
30
+ await rtc.setAlarm1(0, 0, 0, 1, false, DS3231Full.ALARM1_EVERY_SECOND); // Configure alarm 1, (second, minute, hour, day_or_date, is_day_of_week, match_mode) → None
31
+ // once-per-second match; day_or_date/is_day_of_week ignored for this mode
32
+ const alarm1 = await rtc.getAlarm1(); // Read alarm 1 config, () → {second, minute, ..., matchMode}
33
+ console.log('alarm1:', alarm1);
34
+
35
+ await rtc.setAlarm2(30, 0, 1, false, DS3231Full.ALARM2_MATCH_MINUTES); // Configure alarm 2, (minute, hour, day_or_date, is_day_of_week, match_mode) → None
36
+ // fires when minutes register reads 30
37
+ const alarm2 = await rtc.getAlarm2(); // Read alarm 2 config, () → {minute, hour, ..., matchMode}
38
+ console.log('alarm2:', alarm2);
39
+
40
+ await rtc.enableInterrupt(DS3231Full.SOURCE_ALARM1 | DS3231Full.SOURCE_ALARM2); // Enable alarm interrupts, (source) → None
41
+ // sets A1IE/A2IE and INTCN=1 (INT/SQW now carries alarm interrupts)
42
+
43
+ await rtc.onInterrupt((status) => { // Subscribe to alarm interrupts, (callback) → None
44
+ if (status & DS3231Full.SOURCE_ALARM1) console.log('alarm 1 fired');
45
+ if (status & DS3231Full.SOURCE_ALARM2) console.log('alarm 2 fired');
46
+ });
47
+ await new Promise((r) => setTimeout(r, 3000));
48
+ await rtc.offInterrupt(); // Unsubscribe, () → None
49
+
50
+ const status = await rtc.pollInterrupt(); // Poll & clear alarm flags, () → int
51
+ console.log('leftover status:', status);
52
+
53
+ await rtc.disableInterrupt(DS3231Full.SOURCE_ALARM1 | DS3231Full.SOURCE_ALARM2); // Disable alarm interrupts, (source) → None
54
+
55
+ await rtc.enableSquareWave(1024, false); // Enable square wave, (rate_hz=8192, battery_backed=false) → None
56
+ // 1.024 kHz on INT/SQW; mutually exclusive with alarm interrupts
57
+ await rtc.disableSquareWave(); // Disable square wave, () → None
58
+ // returns INT/SQW to interrupt mode
59
+
60
+ console.log('32kHz enabled:', await rtc.is32khzEnabled()); // Query 32kHz output, () → bool
61
+ await rtc.disable32khzOutput(); // Disable 32kHz output, () → None
62
+ await rtc.enable32khzOutput(); // Enable 32kHz output, () → None
63
+
64
+ await rtc.disableBatteryOscillator(); // Disable oscillator on VBAT, () → None
65
+ // saves battery current; oscillator resumes once VCC returns
66
+ await rtc.enableBatteryOscillator(); // Enable oscillator on VBAT, () → None
67
+ // power-on default
68
+
69
+ await rtc.clearOscillatorStopped(); // Clear oscillator-stop flag, () → None
70
+
71
+ const aging = await rtc.getAgingOffset(); // Read aging offset, () → int
72
+ // raw signed 8-bit trim code, no fixed physical unit
73
+ console.log('aging offset:', aging);
74
+ await rtc.setAgingOffset(0); // Set aging offset, (offset) → None
75
+
76
+ await connection.close();
77
+ })();
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS3231Full } = require('periph/src/chips/rtc/ds3231');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new DS3231Full(connection); // Create DS3231 Full driver, (connection)
10
+ await rtc.init(); // Confirm device presence, () → None
11
+
12
+ // --- Backup-clock module for a data logger ---
13
+ // A fresh chip (or one whose coin cell just died and was replaced) reports
14
+ // OSF=1: its timekeeping registers cannot be trusted. Reseed from a known
15
+ // reference timestamp rather than logging garbage — setDatetime() also
16
+ // clears OSF, so this only runs once per real power/battery loss.
17
+ if (await rtc.oscillatorStopped()) {
18
+ const now = new Date();
19
+ const weekday = ((now.getUTCDay() + 6) % 7) + 1; // JS Sunday=0 -> ISO 8601 Monday=1..Sunday=7
20
+ await rtc.setDatetime(now.getUTCFullYear(), now.getUTCMonth() + 1, now.getUTCDate(),
21
+ weekday, now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
22
+ console.log('oscillator was stopped - reseeded clock from host time');
23
+ }
24
+
25
+ // --- Arm both alarms as periodic wake sources ---
26
+ // Alarm 1 matches once per minute (at :00 seconds); Alarm 2 matches once
27
+ // per hour (at :00 minutes). Neither register is ever rewritten, so once
28
+ // armed they repeat indefinitely without host intervention.
29
+ await rtc.setAlarm1(0, 0, 0, 1, false, DS3231Full.ALARM1_MATCH_SECONDS); // seconds==0 -> once per minute
30
+ await rtc.setAlarm2(0, 0, 1, false, DS3231Full.ALARM2_EVERY_MINUTE);
31
+ await rtc.enableInterrupt(DS3231Full.SOURCE_ALARM1 | DS3231Full.SOURCE_ALARM2);
32
+
33
+ let logEntries = 0;
34
+ const MAX_ENTRIES = 5;
35
+ let done;
36
+ const finished = new Promise((resolve) => { done = resolve; });
37
+
38
+ // --- Log one line per alarm match, tagged with which alarm(s) fired ---
39
+ await rtc.onInterrupt(async (status) => {
40
+ const dt = await rtc.getDatetime();
41
+ const tempC = await rtc.readTemperature();
42
+ const tags = [];
43
+ if (status & DS3231Full.SOURCE_ALARM1) tags.push('minute');
44
+ if (status & DS3231Full.SOURCE_ALARM2) tags.push('hour');
45
+ const stamp = `${dt.year}-${String(dt.month).padStart(2, '0')}-${String(dt.day).padStart(2, '0')} ` +
46
+ `${String(dt.hour).padStart(2, '0')}:${String(dt.minute).padStart(2, '0')}:${String(dt.second).padStart(2, '0')}`;
47
+ console.log(`[${tags.join('+')}] ${stamp} ${tempC.toFixed(2)} C`);
48
+
49
+ logEntries++;
50
+ if (logEntries >= MAX_ENTRIES) done();
51
+ });
52
+
53
+ await finished;
54
+
55
+ // --- Clean up: silence both alarms and unsubscribe ---
56
+ await rtc.disableInterrupt(DS3231Full.SOURCE_ALARM1 | DS3231Full.SOURCE_ALARM2);
57
+ await rtc.offInterrupt();
58
+
59
+ await connection.close();
60
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS3231Minimal } = require('periph/src/chips/rtc/ds3231');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new DS3231Minimal(connection); // Create DS3231 driver, (connection)
10
+ await rtc.init(); // Confirm device presence, () → None
11
+
12
+ await rtc.setDatetime(2026, 9, 22, 2, 14, 30, 0); // Set the calendar clock, (year, month, day, weekday, hour, minute, second) → None
13
+
14
+ for (let i = 0; i < 10; i++) {
15
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, ..., second}
16
+ const tempC = await rtc.readTemperature(); // Read temperature, () → float C
17
+ console.log(`${dt.year}-${String(dt.month).padStart(2, '0')}-${String(dt.day).padStart(2, '0')} ` +
18
+ `${String(dt.hour).padStart(2, '0')}:${String(dt.minute).padStart(2, '0')}:${String(dt.second).padStart(2, '0')} ${tempC.toFixed(2)} C`);
19
+ await new Promise((r) => setTimeout(r, 1000));
20
+ }
21
+
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { PCF8523Full } = require('periph/src/chips/rtc/pcf8523');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new PCF8523Full(connection); // Create PCF8523 Full driver, (connection)
10
+ await rtc.init(); // Confirm presence + enable battery backup, () → None
11
+ // writes PM=000: standard switch-over, low detection on
12
+
13
+ await rtc.setDatetime(2026, 9, 23, 3, 14, 30, 0); // Set the calendar clock, (year, month, day, weekday 0=Sun, hour, minute, second) → None
14
+ // STOP-bit precision start; forces 24-hour mode and clears OS
15
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
16
+ // decodes the seven BCD clock/calendar registers
17
+ const stopped = await rtc.oscillatorStopped(); // Query oscillator-stop flag, () → bool
18
+ // true means the time may be invalid until setDatetime
19
+
20
+ await rtc.setAlarm({ minute: 0, hour: 9 }); // Configure alarm, ({minute, hour, day, weekday}) → None
21
+ // fires daily at 09:00; omitted fields are ignored in the match
22
+ const alarm = await rtc.getAlarm(); // Read alarm, () → {minute, hour, day, weekday}
23
+ // disabled fields decode as null
24
+
25
+ await rtc.configureTimerA('countdown', 10, '1hz'); // Start Timer A, (mode, value 0–255, sourceClock, pulsed=false) → None
26
+ // counts down 10 s, then sets CTAF
27
+ const remainingA = await rtc.readTimerA(); // Read Timer A counter, () → int
28
+ // live value, not the loaded one
29
+ await rtc.disableTimerA(); // Stop Timer A, () → None
30
+
31
+ await rtc.configureTimerB(30, '1hz', 62.5, true); // Start Timer B, (value 0–255, sourceClock, pulseWidthMs=46.875 ms, pulsed=false) → None
32
+ // 30 s countdown, pulsed 62.5 ms low on INT1 and INT2
33
+ const remainingB = await rtc.readTimerB(); // Read Timer B counter, () → int
34
+ await rtc.disableTimerB(); // Stop Timer B, () → None
35
+
36
+ await rtc.setClockOutput(1); // Drive CLKOUT, (frequencyHz) → None
37
+ // 1 Hz square wave on the shared INT1/CLKOUT pin
38
+ await rtc.disableClockOutput(); // Disable CLKOUT, () → None
39
+ // frees INT1 for interrupts
40
+
41
+ await rtc.setOffset(-3, 'every_two_hours'); // Write offset calibration, (offset −64–63, mode='every_two_hours') → None
42
+ // −3 LSB × 4.34 ppm = −13.02 ppm correction
43
+ const offset = await rtc.getOffset(); // Read offset calibration, () → {offset, mode}
44
+
45
+ await rtc.configureBatteryBackup('standard', true); // Select battery switch-over, (mode, lowDetection=true) → None
46
+ // switches to VBAT when VDD < VBAT and VDD < 2.5 V
47
+ const switched = await rtc.isBatterySwitchedOver(); // Query switch-over flag, () → bool
48
+ await rtc.clearBatterySwitchover(); // Clear switch-over flag, () → None
49
+ const low = await rtc.isBatteryLow(); // Query battery-low flag, () → bool
50
+ // read-only; clears itself once the cell is replaced
51
+
52
+ await rtc.onInterrupt((status) => console.log('interrupt status=0x' + status.toString(16))); // Subscribe to interrupts, (callback) → None
53
+ // falls back to 5 ms polling when no INT pin is wired
54
+ await rtc.enableInterrupt(PCF8523Full.SOURCE_ALARM | PCF8523Full.SOURCE_TIMER_B | PCF8523Full.SOURCE_BATTERY_LOW); // Enable sources, (source) → None
55
+ // sets AIE, CTBIE and BLIE
56
+ const status = await rtc.pollInterrupt(); // Poll & clear flags, () → int
57
+ // clears CTAF/CTBF/SF/AF/BSF, returns the pre-clear mask
58
+ await rtc.disableInterrupt(PCF8523Full.SOURCE_ALARM | PCF8523Full.SOURCE_TIMER_A | PCF8523Full.SOURCE_TIMER_B | PCF8523Full.SOURCE_BATTERY_LOW); // Disable sources, (source) → None
59
+ await rtc.offInterrupt(); // Unsubscribe, () → None
60
+
61
+ await rtc.softwareReset(); // Software reset, () → None
62
+ // control registers back to POR (PM=111); time is kept
63
+
64
+ console.log(dt, { stopped }, alarm, { remainingA, remainingB }, offset, { switched, low, status });
65
+ await connection.close();
66
+ })();
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ // Scheduling core of a battery-backed logger: reseeds the clock after a
4
+ // power loss, checks the coin cell, then wakes on an hourly alarm to print
5
+ // a timestamp while Timer B pulses a 30-second "still running" heartbeat
6
+ // on INT2 that toggles an LED.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { PCF8523Full } = require('periph/src/chips/rtc/pcf8523');
10
+
11
+ (async () => {
12
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
13
+ const connection = new I2CConnection(bus, 0x68);
14
+ const rtc = new PCF8523Full(connection); // Create PCF8523 Full driver, (connection)
15
+ await rtc.init(); // Confirm presence + enable battery backup, () → None
16
+
17
+ // --- Detect a lost time reference and reseed if needed ---
18
+ // A fresh chip, or one whose backup cell was disconnected too long,
19
+ // reports the OS flag set: its calendar cannot be trusted until reseeded.
20
+ if (await rtc.oscillatorStopped()) { // Query oscillator-stop flag, () → bool
21
+ await rtc.setDatetime(2026, 1, 1, 4, 0, 0, 0); // Set the calendar clock, (year, month, day, weekday 0=Sun, hour, minute, second) → None
22
+ console.log('oscillator was stopped - reseeded from reference timestamp');
23
+ }
24
+
25
+ // --- Keep the clock alive through power cuts ---
26
+ // Standard switch-over is already the driver default; it is repeated here
27
+ // so the logger's power policy is explicit. A low coin cell is reported
28
+ // once so it can be replaced before the next outage.
29
+ await rtc.configureBatteryBackup('standard'); // Select battery switch-over, (mode, lowDetection=true) → None
30
+ if (await rtc.isBatteryLow()) { // Query battery-low flag, () → bool
31
+ console.log('warning: backup battery low - replace the coin cell');
32
+ }
33
+
34
+ // --- Hourly wake-up plus a 30 s heartbeat ---
35
+ // Only the minute field is enabled, so the alarm matches at hh:00 every
36
+ // hour. Timer B reloads automatically and has its own INT2 pin, so the
37
+ // heartbeat keeps running independently of the hourly alarm.
38
+ await rtc.disableClockOutput(); // Disable CLKOUT, () → None
39
+ await rtc.setAlarm({ minute: 0 }); // Configure alarm, ({minute, hour, day, weekday}) → None
40
+ await rtc.configureTimerB(30, '1hz'); // Start Timer B, (value 0–255, sourceClock, pulseWidthMs=46.875 ms, pulsed=false) → None
41
+
42
+ // --- Dispatch by source: log on the alarm, blink on the heartbeat ---
43
+ let alarms = 0;
44
+ let ledOn = false;
45
+ await new Promise((resolve) => {
46
+ rtc.onInterrupt(async (status) => { // Subscribe to interrupts, (callback) → None
47
+ if (status & PCF8523Full.SOURCE_ALARM) {
48
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
49
+ console.log('[hourly]', dt);
50
+ if (++alarms >= 3) resolve();
51
+ }
52
+ if (status & PCF8523Full.SOURCE_TIMER_B) {
53
+ ledOn = !ledOn;
54
+ console.log(`[heartbeat] LED ${ledOn ? 'on' : 'off'}`);
55
+ }
56
+ });
57
+ rtc.enableInterrupt(PCF8523Full.SOURCE_ALARM | PCF8523Full.SOURCE_TIMER_B); // Enable sources, (source) → None
58
+ });
59
+
60
+ // --- Leave the chip quiet on exit ---
61
+ await rtc.disableInterrupt(PCF8523Full.SOURCE_ALARM | PCF8523Full.SOURCE_TIMER_B); // Disable sources, (source) → None
62
+ await rtc.disableTimerB(); // Stop Timer B, () → None
63
+ await rtc.offInterrupt(); // Unsubscribe, () → None
64
+ await connection.close();
65
+ })();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { PCF8523Minimal } = require('periph/src/chips/rtc/pcf8523');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new PCF8523Minimal(connection); // Create PCF8523 driver, (connection)
10
+ await rtc.init(); // Confirm presence + enable battery backup, () → None
11
+
12
+ const pad = (n) => String(n).padStart(2, '0');
13
+ for (let i = 0; i < 10; i++) {
14
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
15
+ console.log(`${dt.year}-${pad(dt.month)}-${pad(dt.day)} ${pad(dt.hour)}:${pad(dt.minute)}:${pad(dt.second)}`);
16
+ await new Promise((r) => setTimeout(r, 1000));
17
+ }
18
+
19
+ await connection.close();
20
+ })();
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ // Exercises every method in the MCP9808 public API: temperature, resolution,
4
+ // Shutdown mode, the three boundaries and hysteresis, the Alert output, and
5
+ // the boundary-status interrupt API. The one-way lock methods are shown but
6
+ // left commented out — they cannot be undone without a power-on reset.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { MCP9808Full } = require('periph/src/chips/temperature/mcp9808');
10
+
11
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
12
+
13
+ (async () => {
14
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
15
+ const connection = new I2CConnection(bus, MCP9808Full.I2C_ADDRESS);
16
+ const sensor = new MCP9808Full(connection); // Create MCP9808 Full driver, (connection)
17
+ // starts the MANUFACTURER_ID / DEVICE_ID check
18
+ await sensor.init(); // Confirm device identity, () → None
19
+ // rejects if the IDs are not 0x0054 / 0x04
20
+
21
+ const t = await sensor.readTemperature(); // Read ambient temperature, () → number °C
22
+ // masks TA's 3 status bits, decodes 1/16 °C two's complement
23
+ console.log(`temperature ${t.toFixed(4)} °C`);
24
+
25
+ await sensor.setResolution(0.25); // Set resolution, (celsius 0.5|0.25|0.125|0.0625) → None
26
+ // 0.25 °C step converts in ~65 ms instead of 250 ms
27
+ console.log('resolution', await sensor.getResolution(), '°C'); // Read resolution, () → number °C
28
+ // decodes the RESOLUTION register code
29
+
30
+ await sensor.shutdown(); // Enter Shutdown mode, () → None
31
+ // stops conversion; TA keeps its last value
32
+ console.log('shutdown', await sensor.isShutdown()); // Check Shutdown mode, () → boolean
33
+ // reads CONFIG.SHDN
34
+ await sensor.wake(); // Leave Shutdown mode, () → None
35
+ // resumes continuous conversion
36
+ await sleep(100);
37
+
38
+ await sensor.setUpperLimit(30.0); // Set TUPPER, (celsius °C) → None
39
+ // rounded to the nearest 0.25 °C step
40
+ await sensor.setLowerLimit(10.0); // Set TLOWER, (celsius °C) → None
41
+ // rounded to the nearest 0.25 °C step
42
+ await sensor.setCriticalLimit(45.0); // Set TCRIT, (celsius °C) → None
43
+ // rounded to the nearest 0.25 °C step
44
+ console.log('upper', await sensor.getUpperLimit()); // Read TUPPER, () → number °C
45
+ // decodes the 0.25 °C two's-complement boundary
46
+ console.log('lower', await sensor.getLowerLimit()); // Read TLOWER, () → number °C
47
+ // decodes the 0.25 °C two's-complement boundary
48
+ console.log('critical', await sensor.getCriticalLimit()); // Read TCRIT, () → number °C
49
+ // decodes the 0.25 °C two's-complement boundary
50
+
51
+ await sensor.setHysteresis(1.5); // Set hysteresis, (celsius 0|1.5|3.0|6.0) → None
52
+ // applied on the cooling edge of each boundary only
53
+ console.log('hysteresis', await sensor.getHysteresis()); // Read hysteresis, () → number °C
54
+ // decodes CONFIG.THYST
55
+
56
+ // await sensor.lockCriticalLimit(); // Lock TCRIT, () → None
57
+ // // irreversible until power-on reset
58
+ // await sensor.lockWindowLimits(); // Lock TUPPER/TLOWER, () → None
59
+ // // irreversible until power-on reset
60
+ console.log('crit locked', await sensor.isCriticalLimitLocked()); // Check TCRIT lock, () → boolean
61
+ // reads CONFIG.CRIT_LOCK
62
+ console.log('win locked', await sensor.isWindowLimitsLocked()); // Check TUPPER/TLOWER lock, () → boolean
63
+ // reads CONFIG.WIN_LOCK
64
+
65
+ await sensor.configureAlert('all', 'interrupt', 'active_low'); // Configure Alert, (mode='all', output='comparator', polarity='active_low') → None
66
+ // sets ALERT_SEL, ALERT_MOD and ALERT_POL together
67
+ await sensor.enableAlert(); // Enable Alert output, () → None
68
+ // sets CONFIG.ALERT_CNT
69
+ console.log('alert asserted', await sensor.isAlertAsserted()); // Check Alert output, () → boolean
70
+ // reads the read-only CONFIG.ALERT_STAT
71
+
72
+ const status = await sensor.pollInterrupt(); // Read boundary status, () → number mask
73
+ // TA's live bits: SOURCE_LOWER/UPPER/CRITICAL, nothing cleared
74
+ console.log('below lower', (status & MCP9808Full.SOURCE_LOWER) !== 0,
75
+ 'above upper', (status & MCP9808Full.SOURCE_UPPER) !== 0,
76
+ 'critical', (status & MCP9808Full.SOURCE_CRITICAL) !== 0);
77
+ await sensor.clearInterrupt(); // Clear interrupt-mode Alert, () → None
78
+ // writes CONFIG.INT_CLEAR=1; no effect in comparator mode
79
+
80
+ await sensor.onInterrupt((s) => console.log('alert, status mask', s)); // Subscribe to Alert, (callback) → None
81
+ // callback receives the pollInterrupt() mask
82
+ await sleep(5000);
83
+ await sensor.offInterrupt(); // Unsubscribe, () → None
84
+ // detaches the edge handler or stops the polling timer
85
+ await sensor.disableAlert(); // Disable Alert output, () → None
86
+ // clears CONFIG.ALERT_CNT
87
+
88
+ await connection.close();
89
+ })();
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ // Industrial freezer monitor: the healthy range is -25 °C to -15 °C, and
4
+ // -5 °C means the door has been left open too long. The Alert output fires
5
+ // in interrupt mode each time the temperature leaves or re-enters the
6
+ // window; the callback reports which boundary tripped. After a fixed number
7
+ // of alerts the monitor disables the Alert output and unsubscribes.
8
+
9
+ const { I2CConnection } = require('periph/src/connection/i2c');
10
+ const { MCP9808Full } = require('periph/src/chips/temperature/mcp9808');
11
+
12
+ const MAX_ALERTS = 10;
13
+
14
+ (async () => {
15
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
16
+ const connection = new I2CConnection(bus, MCP9808Full.I2C_ADDRESS);
17
+ const sensor = new MCP9808Full(connection); // Create MCP9808 Full driver, (connection)
18
+ await sensor.init(); // Confirm device identity, () → None
19
+
20
+ // --- Trade resolution for faster sampling ---
21
+ // 0.25 °C is plenty for a freezer and converts in ~65 ms instead of 250 ms,
22
+ // so a door opening shows up in the next reading almost immediately.
23
+ await sensor.setResolution(0.25); // Set resolution, (celsius 0.5|0.25|0.125|0.0625) → None
24
+
25
+ // --- Program the healthy window and the door-open threshold ---
26
+ // TLOWER/TUPPER bracket normal operation; TCRIT flags a door left open.
27
+ // 3 °C of hysteresis stops the Alert chattering while the compressor cycles.
28
+ await sensor.setLowerLimit(-25.0); // Set TLOWER, (celsius °C) → None
29
+ await sensor.setUpperLimit(-15.0); // Set TUPPER, (celsius °C) → None
30
+ await sensor.setCriticalLimit(-5.0); // Set TCRIT, (celsius °C) → None
31
+ await sensor.setHysteresis(3.0); // Set hysteresis, (celsius 0|1.5|3.0|6.0) → None
32
+
33
+ // --- Route every boundary to the Alert pin as a latched interrupt ---
34
+ // Interrupt mode latches each crossing until clearInterrupt(), so a short
35
+ // excursion is never missed between two reads.
36
+ await sensor.configureAlert('all', 'interrupt', 'active_low'); // Configure Alert, (mode='all', output='comparator', polarity='active_low') → None
37
+ await sensor.enableAlert(); // Enable Alert output, () → None
38
+
39
+ let alerts = 0;
40
+ let done;
41
+ const finished = new Promise((r) => { done = r; });
42
+
43
+ // --- Report which boundary tripped, then re-arm ---
44
+ // The status mask is a live read of TA's boundary bits; an empty mask
45
+ // means the temperature has come back inside the healthy window.
46
+ await sensor.onInterrupt(async (status) => { // Subscribe to Alert, (callback) → None
47
+ const t = await sensor.readTemperature(); // Read ambient temperature, () → number °C
48
+ if (status & MCP9808Full.SOURCE_CRITICAL) console.log(`${t.toFixed(2)} °C CRITICAL — door open?`);
49
+ else if (status & MCP9808Full.SOURCE_UPPER) console.log(`${t.toFixed(2)} °C too warm`);
50
+ else if (status & MCP9808Full.SOURCE_LOWER) console.log(`${t.toFixed(2)} °C too cold`);
51
+ else console.log(`${t.toFixed(2)} °C back in range`);
52
+ await sensor.clearInterrupt(); // Clear interrupt-mode Alert, () → None
53
+ if (++alerts >= MAX_ALERTS) done();
54
+ });
55
+ console.log(`monitoring, ${(await sensor.readTemperature()).toFixed(2)} °C now`); // Read ambient temperature, () → number °C
56
+ await finished;
57
+
58
+ // --- Shut down cleanly after the demo run ---
59
+ await sensor.disableAlert(); // Disable Alert output, () → None
60
+ await sensor.offInterrupt(); // Unsubscribe, () → None
61
+ await connection.close();
62
+ })();
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { MCP9808Minimal } = require('periph/src/chips/temperature/mcp9808');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ (async () => {
9
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
10
+ const connection = new I2CConnection(bus, MCP9808Minimal.I2C_ADDRESS);
11
+ const sensor = new MCP9808Minimal(connection); // Create MCP9808 driver, (connection)
12
+ await sensor.init(); // Confirm device identity, () → None
13
+
14
+ for (let i = 0; i < 10; i++) {
15
+ const t = await sensor.readTemperature(); // Read ambient temperature, () → number °C
16
+ console.log(`${t.toFixed(4)} °C`);
17
+ await sleep(1000);
18
+ }
19
+
20
+ await connection.close();
21
+ })();