periph 1.3.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +36 -0
  2. package/examples/adc_dac/ad5251/complete.js +66 -0
  3. package/examples/adc_dac/ad5251/demo.js +69 -0
  4. package/examples/adc_dac/ad5251/minimal.js +23 -0
  5. package/examples/adc_dac/ad5252/complete.js +66 -0
  6. package/examples/adc_dac/ad5252/demo.js +69 -0
  7. package/examples/adc_dac/ad5252/minimal.js +23 -0
  8. package/examples/adc_dac/ads7828/complete.js +46 -0
  9. package/examples/adc_dac/ads7828/demo.js +45 -0
  10. package/examples/adc_dac/ads7828/minimal.js +17 -0
  11. package/examples/adc_dac/cat5171/complete.js +52 -0
  12. package/examples/adc_dac/cat5171/demo.js +59 -0
  13. package/examples/adc_dac/cat5171/minimal.js +23 -0
  14. package/examples/adc_dac/cs43l22/complete.js +88 -0
  15. package/examples/adc_dac/cs43l22/demo.js +56 -0
  16. package/examples/adc_dac/cs43l22/minimal.js +20 -0
  17. package/examples/adc_dac/ds1881/complete.js +62 -0
  18. package/examples/adc_dac/ds1881/demo.js +61 -0
  19. package/examples/adc_dac/ds1881/minimal.js +25 -0
  20. package/examples/adc_dac/ds3502/complete.js +46 -0
  21. package/examples/adc_dac/ds3502/demo.js +54 -0
  22. package/examples/adc_dac/ds3502/minimal.js +28 -0
  23. package/examples/adc_dac/es8311/complete.js +80 -0
  24. package/examples/adc_dac/es8311/demo.js +59 -0
  25. package/examples/adc_dac/es8311/minimal.js +18 -0
  26. package/examples/adc_dac/mcp3422/complete.js +48 -0
  27. package/examples/adc_dac/mcp3422/demo.js +49 -0
  28. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  29. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  30. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  31. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  32. package/examples/display/sh1106/complete.js +101 -0
  33. package/examples/display/sh1106/demo.js +68 -0
  34. package/examples/display/sh1106/minimal.js +25 -0
  35. package/examples/display/ssd1305/complete.js +129 -0
  36. package/examples/display/ssd1305/demo.js +71 -0
  37. package/examples/display/ssd1305/minimal.js +25 -0
  38. package/examples/display/ssd1306/complete.js +115 -0
  39. package/examples/display/ssd1306/demo.js +70 -0
  40. package/examples/display/ssd1306/minimal.js +25 -0
  41. package/examples/environmental/am2315/complete.js +34 -0
  42. package/examples/environmental/am2315/demo.js +46 -0
  43. package/examples/environmental/am2315/minimal.js +21 -0
  44. package/examples/humidity/htu21df/complete.js +49 -0
  45. package/examples/humidity/htu21df/demo.js +50 -0
  46. package/examples/humidity/htu21df/minimal.js +22 -0
  47. package/examples/humidity/sht31/complete.js +53 -0
  48. package/examples/humidity/sht31/demo.js +53 -0
  49. package/examples/humidity/sht31/minimal.js +21 -0
  50. package/examples/humidity/si7021/complete.js +55 -0
  51. package/examples/humidity/si7021/demo.js +58 -0
  52. package/examples/humidity/si7021/minimal.js +21 -0
  53. package/examples/imu/bno055/complete.js +85 -0
  54. package/examples/imu/bno055/demo.js +70 -0
  55. package/examples/imu/bno055/minimal.js +20 -0
  56. package/examples/imu/qmi8658a/complete.js +134 -0
  57. package/examples/imu/qmi8658a/demo.js +81 -0
  58. package/examples/imu/qmi8658a/minimal.js +22 -0
  59. package/examples/io_expander/pca6408a/complete.js +63 -0
  60. package/examples/io_expander/pca6408a/demo.js +58 -0
  61. package/examples/io_expander/pca6408a/minimal.js +20 -0
  62. package/examples/io_expander/pca6416a/complete.js +68 -0
  63. package/examples/io_expander/pca6416a/demo.js +60 -0
  64. package/examples/io_expander/pca6416a/minimal.js +20 -0
  65. package/examples/io_expander/pca9536/complete.js +63 -0
  66. package/examples/io_expander/pca9536/demo.js +57 -0
  67. package/examples/io_expander/pca9536/minimal.js +20 -0
  68. package/examples/io_expander/pca9539/complete.js +68 -0
  69. package/examples/io_expander/pca9539/demo.js +60 -0
  70. package/examples/io_expander/pca9539/minimal.js +20 -0
  71. package/examples/light/max44009/complete.js +56 -0
  72. package/examples/light/max44009/demo.js +49 -0
  73. package/examples/light/max44009/minimal.js +22 -0
  74. package/examples/memory/at24c02n/complete.js +39 -0
  75. package/examples/memory/at24c02n/demo.js +39 -0
  76. package/examples/memory/at24c02n/minimal.js +15 -0
  77. package/examples/memory/at24c64/complete.js +39 -0
  78. package/examples/memory/at24c64/demo.js +39 -0
  79. package/examples/memory/at24c64/minimal.js +15 -0
  80. package/examples/memory/cat24c512/complete.js +39 -0
  81. package/examples/memory/cat24c512/demo.js +39 -0
  82. package/examples/memory/cat24c512/minimal.js +15 -0
  83. package/examples/motor/tmc2130/complete.js +138 -0
  84. package/examples/motor/tmc2130/demo.js +96 -0
  85. package/examples/motor/tmc2130/minimal.js +30 -0
  86. package/examples/motor/tmc2208/complete.js +92 -0
  87. package/examples/motor/tmc2208/demo.js +85 -0
  88. package/examples/motor/tmc2208/minimal.js +25 -0
  89. package/examples/motor/tmc2209/complete.js +106 -0
  90. package/examples/motor/tmc2209/demo.js +97 -0
  91. package/examples/motor/tmc2209/minimal.js +25 -0
  92. package/examples/motor/tmc5160/complete.js +205 -0
  93. package/examples/motor/tmc5160/demo.js +104 -0
  94. package/examples/motor/tmc5160/minimal.js +35 -0
  95. package/examples/other/pca9541/complete.js +52 -0
  96. package/examples/other/pca9541/demo.js +61 -0
  97. package/examples/other/pca9541/minimal.js +23 -0
  98. package/examples/power/axp2101/complete.js +135 -0
  99. package/examples/power/axp2101/demo.js +68 -0
  100. package/examples/power/axp2101/minimal.js +24 -0
  101. package/examples/rtc/pcf85063a/complete.js +68 -0
  102. package/examples/rtc/pcf85063a/demo.js +76 -0
  103. package/examples/rtc/pcf85063a/minimal.js +20 -0
  104. package/examples/temperature/tmp102/complete.js +74 -0
  105. package/examples/temperature/tmp102/demo.js +71 -0
  106. package/examples/temperature/tmp102/minimal.js +20 -0
  107. package/examples/tof/vl6180x/complete.js +62 -0
  108. package/examples/tof/vl6180x/demo.js +77 -0
  109. package/examples/tof/vl6180x/minimal.js +23 -0
  110. package/package.json +1 -1
  111. package/src/chips/adc_dac/ad5251.js +345 -0
  112. package/src/chips/adc_dac/ad5252.js +345 -0
  113. package/src/chips/adc_dac/ads7828.js +225 -0
  114. package/src/chips/adc_dac/cat5171.js +213 -0
  115. package/src/chips/adc_dac/cs43l22.js +645 -0
  116. package/src/chips/adc_dac/ds1881.js +357 -0
  117. package/src/chips/adc_dac/ds3502.js +241 -0
  118. package/src/chips/adc_dac/es8311.js +621 -0
  119. package/src/chips/adc_dac/mcp3422.js +312 -0
  120. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  121. package/src/chips/display/mono_oled.js +182 -0
  122. package/src/chips/display/sh1106.js +496 -0
  123. package/src/chips/display/ssd1305.js +655 -0
  124. package/src/chips/display/ssd1306.js +458 -0
  125. package/src/chips/environmental/am2315.js +237 -0
  126. package/src/chips/humidity/htu21df.js +222 -0
  127. package/src/chips/humidity/sht31.js +284 -0
  128. package/src/chips/humidity/si7021.js +282 -0
  129. package/src/chips/imu/bno055.js +496 -0
  130. package/src/chips/imu/qmi8658a.js +1011 -0
  131. package/src/chips/io_expander/pca6408a.js +407 -0
  132. package/src/chips/io_expander/pca6416a.js +455 -0
  133. package/src/chips/io_expander/pca9536.js +398 -0
  134. package/src/chips/io_expander/pca9539.js +457 -0
  135. package/src/chips/light/max44009.js +336 -0
  136. package/src/chips/memory/at24c02n.js +220 -0
  137. package/src/chips/memory/at24c64.js +220 -0
  138. package/src/chips/memory/cat24c512.js +221 -0
  139. package/src/chips/motor/tmc2130.js +537 -0
  140. package/src/chips/motor/tmc2208.js +490 -0
  141. package/src/chips/motor/tmc2209.js +565 -0
  142. package/src/chips/motor/tmc5160.js +900 -0
  143. package/src/chips/other/pca9541.js +315 -0
  144. package/src/chips/power/axp2101.js +968 -0
  145. package/src/chips/rtc/pcf85063a.js +482 -0
  146. package/src/chips/temperature/tmp102.js +361 -0
  147. package/src/chips/tof/vl6180x.js +580 -0
  148. package/src/connection/uart.js +9 -0
  149. package/src/connection/uart_mock.js +65 -0
  150. package/src/discovery/registry.js +85 -8
@@ -0,0 +1,213 @@
1
+ 'use strict';
2
+
3
+ const _INSTR_RS = 0x40;
4
+ const _INSTR_SD = 0x20;
5
+ const _MIDSCALE = 0x80;
6
+
7
+ function _clampCode(code) {
8
+ code = Math.trunc(code);
9
+ if (code < 0) return 0;
10
+ if (code > 255) return 255;
11
+ return code;
12
+ }
13
+
14
+ /**
15
+ * CAT5171 256-position I²C digital potentiometer (ON Semiconductor) —
16
+ * minimal interface.
17
+ *
18
+ * A single 3-terminal potentiometer (A, W, B). The chip has no register map:
19
+ * a write is `[instruction, data]` where the instruction byte carries the
20
+ * midscale-reset flag (RS, bit 6) and the shutdown flag (SD, bit 5); a read is
21
+ * a bare one-byte read returning the wiper register. The driver therefore
22
+ * uses the plain Connection `write()` / `read()` calls. The I²C address is
23
+ * 0x2C (AD0 = GND) or 0x2D (AD0 = VDD). The wiper is volatile (power-up
24
+ * midscale 0x80).
25
+ */
26
+ class CAT5171Minimal {
27
+ /**
28
+ * @param {import('../../connection/connection').Connection} connection - I²C connection (0x2C or 0x2D).
29
+ * @param {number} [rAb=100000] - End-to-end resistance RAB in ohms.
30
+ * @param {number} [rWiper=50] - Wiper resistance RW in ohms.
31
+ */
32
+ constructor(connection, rAb = 100000.0, rWiper = 50.0) {
33
+ this._conn = connection;
34
+ this._rAb = rAb;
35
+ this._rW = rWiper;
36
+ this._code = _MIDSCALE;
37
+ this._sd = false;
38
+ }
39
+
40
+ async _write(instruction, code) {
41
+ await this._conn.write(Buffer.from([instruction, code]));
42
+ }
43
+
44
+ async _writeCode(code) {
45
+ code = _clampCode(code);
46
+ await this._write(this._sd ? _INSTR_SD : 0, code);
47
+ this._code = code;
48
+ }
49
+
50
+ async _readCode() {
51
+ const buf = await this._conn.read(1);
52
+ this._code = buf[0];
53
+ return buf[0];
54
+ }
55
+
56
+ /**
57
+ * Prepare the driver. The CAT5171 has no identity register and needs no
58
+ * initialisation writes, so this performs no I²C traffic.
59
+ * @returns {Promise<void>}
60
+ */
61
+ async init() {}
62
+
63
+ /**
64
+ * Set the wiper position.
65
+ * @param {number} code - Wiper position 0–255 (clamped).
66
+ * @returns {Promise<void>}
67
+ */
68
+ async setWiper(code) {
69
+ await this._writeCode(code);
70
+ }
71
+
72
+ /**
73
+ * Read back the wiper position with a single read transaction.
74
+ * @returns {Promise<number>} Wiper position 0–255.
75
+ */
76
+ async getWiper() {
77
+ return this._readCode();
78
+ }
79
+
80
+ /**
81
+ * Set the W–B resistance to the nearest reachable code.
82
+ * @param {number} ohms - Target W–B resistance in ohms (clamped to the reachable range).
83
+ * @returns {Promise<void>}
84
+ */
85
+ async setResistance(ohms) {
86
+ await this._writeCode(Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5));
87
+ }
88
+
89
+ /**
90
+ * Read the W–B resistance for the current wiper code.
91
+ * @returns {Promise<number>} Resistance in ohms, `D / 256 * RAB + RW`.
92
+ */
93
+ async getResistance() {
94
+ return (await this._readCode()) / 256 * this._rAb + this._rW;
95
+ }
96
+ }
97
+
98
+ /** 7-bit I²C address with AD0 = GND. */
99
+ CAT5171Minimal.I2C_ADDRESS = 0x2C;
100
+ /** 7-bit I²C address with AD0 = VDD. */
101
+ CAT5171Minimal.I2C_ADDRESS_AD0_HIGH = 0x2D;
102
+
103
+ /**
104
+ * CAT5171 full interface — extends Minimal with ratio and W–A helpers,
105
+ * increment/decrement, midscale, shutdown and reset. The shutdown state is
106
+ * write-only on the chip, so the driver tracks it.
107
+ */
108
+ class CAT5171Full extends CAT5171Minimal {
109
+ /**
110
+ * Set the wiper by divider ratio V(W−B)/V(A−B).
111
+ * @param {number} ratio - 0.0–1.0; code is round(ratio × 256) clamped to 255.
112
+ * @returns {Promise<void>}
113
+ */
114
+ async setRatio(ratio) {
115
+ await this._writeCode(Math.floor(ratio * 256 + 0.5));
116
+ }
117
+
118
+ /**
119
+ * Read the divider ratio.
120
+ * @returns {Promise<number>} `code / 256`.
121
+ */
122
+ async getRatio() {
123
+ return (await this._readCode()) / 256;
124
+ }
125
+
126
+ /**
127
+ * Set the W–A resistance (rheostat mode, B open) to the nearest code.
128
+ * @param {number} ohms - Target W–A resistance in ohms.
129
+ * @returns {Promise<void>}
130
+ */
131
+ async setResistanceWa(ohms) {
132
+ const steps = Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5);
133
+ await this._writeCode(256 - steps);
134
+ }
135
+
136
+ /**
137
+ * Read the W–A resistance for the current wiper code.
138
+ * @returns {Promise<number>} Resistance in ohms, `(256 − D) / 256 * RAB + RW`.
139
+ */
140
+ async getResistanceWa() {
141
+ return (256 - (await this._readCode())) / 256 * this._rAb + this._rW;
142
+ }
143
+
144
+ /**
145
+ * Move the wiper relative to its current position.
146
+ * @param {number} [steps=1] - Signed step count (negative to decrement).
147
+ * @returns {Promise<number>} The new wiper code, clamped to 0–255.
148
+ */
149
+ async increment(steps = 1) {
150
+ const code = _clampCode((await this._readCode()) + Math.trunc(steps));
151
+ await this._writeCode(code);
152
+ return code;
153
+ }
154
+
155
+ /**
156
+ * Move the wiper to midscale (0x80) with the RS instruction bit; SD is
157
+ * kept if the device is shut down.
158
+ * @returns {Promise<void>}
159
+ */
160
+ async midscale() {
161
+ await this._write(_INSTR_RS | (this._sd ? _INSTR_SD : 0), _MIDSCALE);
162
+ this._code = _MIDSCALE;
163
+ }
164
+
165
+ /**
166
+ * Shut down: terminal A opens and the wiper shorts to B. The wiper
167
+ * register is kept.
168
+ * @returns {Promise<void>}
169
+ */
170
+ async shutdown() {
171
+ this._sd = true;
172
+ await this._write(_INSTR_SD, this._code);
173
+ }
174
+
175
+ /**
176
+ * Leave shutdown (SD = 0), rewriting the tracked current code.
177
+ * @returns {Promise<void>}
178
+ */
179
+ async resume() {
180
+ this._sd = false;
181
+ await this._write(0, this._code);
182
+ }
183
+
184
+ /**
185
+ * Driver-tracked shutdown state (the chip cannot report it).
186
+ * @returns {boolean} True if the device was shut down by this driver.
187
+ */
188
+ isShutdown() {
189
+ return this._sd;
190
+ }
191
+
192
+ /**
193
+ * Midscale reset (RS = 1, SD = 0): wiper to 0x80, shutdown cleared.
194
+ * @returns {Promise<void>}
195
+ */
196
+ async reset() {
197
+ this._sd = false;
198
+ await this._write(_INSTR_RS, _MIDSCALE);
199
+ this._code = _MIDSCALE;
200
+ }
201
+
202
+ /**
203
+ * Change the resistance parameters used by conversions (no I²C traffic).
204
+ * @param {number} rAb - End-to-end resistance RAB in ohms.
205
+ * @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
206
+ */
207
+ setRab(rAb, rWiper) {
208
+ this._rAb = rAb;
209
+ if (rWiper !== undefined && rWiper !== null) this._rW = rWiper;
210
+ }
211
+ }
212
+
213
+ module.exports = { CAT5171Minimal, CAT5171Full };