@typecad/hal 1.0.0-alpha.13 → 1.0.0-alpha.15

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 (121) hide show
  1. package/README.md +145 -87
  2. package/dist/adc-pin.d.ts +53 -0
  3. package/dist/adc-pin.js +69 -0
  4. package/dist/async.d.ts +3 -11
  5. package/dist/async.js +3 -15
  6. package/dist/ble.d.ts +42 -78
  7. package/dist/ble.js +79 -98
  8. package/dist/counter.d.ts +16 -0
  9. package/dist/counter.js +32 -0
  10. package/dist/dac-pin.d.ts +13 -0
  11. package/dist/dac-pin.js +21 -0
  12. package/dist/emit.d.ts +123 -196
  13. package/dist/emit.js +146 -208
  14. package/dist/fs.d.ts +12 -31
  15. package/dist/fs.js +30 -40
  16. package/dist/gpio-pin.d.ts +46 -0
  17. package/dist/gpio-pin.js +77 -0
  18. package/dist/gpio.d.ts +0 -103
  19. package/dist/gpio.js +16 -227
  20. package/dist/http.d.ts +38 -39
  21. package/dist/http.js +53 -78
  22. package/dist/i2c-target.d.ts +28 -0
  23. package/dist/i2c-target.js +52 -0
  24. package/dist/i2c.d.ts +5 -43
  25. package/dist/i2c.js +17 -130
  26. package/dist/index.d.ts +30 -42
  27. package/dist/index.js +27 -39
  28. package/dist/math.d.ts +0 -13
  29. package/dist/math.js +9 -13
  30. package/dist/mqtt.d.ts +15 -25
  31. package/dist/mqtt.js +32 -28
  32. package/dist/power.d.ts +9 -17
  33. package/dist/power.js +22 -20
  34. package/dist/preferences.d.ts +17 -25
  35. package/dist/preferences.js +54 -52
  36. package/dist/pulse.d.ts +4 -4
  37. package/dist/pulse.js +6 -2
  38. package/dist/pwm-pin.d.ts +24 -0
  39. package/dist/pwm-pin.js +38 -0
  40. package/dist/random.d.ts +0 -2
  41. package/dist/random.js +14 -20
  42. package/dist/sensor-catalog.generated.d.ts +997 -0
  43. package/dist/sensor-catalog.generated.js +764 -0
  44. package/dist/sensor.d.ts +46 -0
  45. package/dist/sensor.js +52 -0
  46. package/dist/shift-pin.d.ts +2 -0
  47. package/dist/shift-pin.js +13 -0
  48. package/dist/spi-target.d.ts +25 -0
  49. package/dist/spi-target.js +42 -0
  50. package/dist/spi.d.ts +8 -26
  51. package/dist/spi.js +15 -87
  52. package/dist/thread.d.ts +18 -0
  53. package/dist/thread.js +36 -0
  54. package/dist/time.d.ts +23 -0
  55. package/dist/time.js +43 -0
  56. package/dist/timing.d.ts +17 -7
  57. package/dist/timing.js +17 -7
  58. package/dist/types.d.ts +4 -34
  59. package/dist/types.js +5 -47
  60. package/dist/uart-port.d.ts +26 -0
  61. package/dist/uart-port.js +52 -0
  62. package/dist/uart.d.ts +0 -17
  63. package/dist/uart.js +10 -53
  64. package/dist/usb.d.ts +26 -0
  65. package/dist/usb.js +66 -0
  66. package/dist/watchdog.d.ts +12 -0
  67. package/dist/watchdog.js +28 -0
  68. package/dist/wifi.d.ts +88 -64
  69. package/dist/wifi.js +112 -126
  70. package/dist/zephyr-tokens.generated.d.ts +8 -0
  71. package/dist/zephyr-tokens.generated.js +73 -0
  72. package/package.json +7 -8
  73. package/src/adc-pin.ts +89 -0
  74. package/src/async.ts +3 -16
  75. package/src/ble.ts +84 -111
  76. package/src/counter.ts +40 -0
  77. package/src/dac-pin.ts +28 -0
  78. package/src/emit.ts +157 -238
  79. package/src/fs.ts +47 -54
  80. package/src/gpio-pin.ts +91 -0
  81. package/src/gpio.ts +49 -320
  82. package/src/http.ts +75 -95
  83. package/src/i2c-target.ts +63 -0
  84. package/src/i2c.ts +29 -167
  85. package/src/index.ts +32 -46
  86. package/src/math.ts +9 -14
  87. package/src/mqtt.ts +39 -31
  88. package/src/preferences.ts +52 -61
  89. package/src/pwm-pin.ts +54 -0
  90. package/src/random.ts +14 -21
  91. package/src/sensor-catalog.generated.ts +1238 -0
  92. package/src/sensor.ts +80 -0
  93. package/src/shift-pin.ts +16 -0
  94. package/src/spi-target.ts +53 -0
  95. package/src/spi.ts +27 -121
  96. package/src/thread.ts +44 -0
  97. package/src/time.ts +50 -0
  98. package/src/types.ts +14 -89
  99. package/src/uart-port.ts +64 -0
  100. package/src/usb.ts +82 -0
  101. package/src/watchdog.ts +35 -0
  102. package/src/wifi.ts +147 -179
  103. package/src/zephyr-tokens.generated.ts +77 -0
  104. package/src/adc.ts +0 -30
  105. package/src/capacitive.ts +0 -31
  106. package/src/constants.ts +0 -23
  107. package/src/dac.ts +0 -21
  108. package/src/eeprom.ts +0 -26
  109. package/src/interrupts.ts +0 -35
  110. package/src/mdns.ts +0 -50
  111. package/src/ota.ts +0 -44
  112. package/src/power.ts +0 -36
  113. package/src/pulse.ts +0 -63
  114. package/src/rmt.ts +0 -125
  115. package/src/shift.ts +0 -88
  116. package/src/temperature.ts +0 -20
  117. package/src/timer.ts +0 -58
  118. package/src/timing.ts +0 -67
  119. package/src/uart.ts +0 -77
  120. package/src/utils.ts +0 -17
  121. package/src/wdt.ts +0 -17
package/src/ble.ts CHANGED
@@ -1,3 +1,22 @@
1
+ // ---------------------------------------------------------------------------
2
+ // BLE — the thin Zephyr-shaped GATT peripheral
3
+ //
4
+ // The advertised identity is a CONSTRUCTION fact: `new BLE('TempSensor')`.
5
+ // The GATT database is declared through service()/char() chains (each char's
6
+ // uuid/type/perms ride its add op — the sensor discipline), handlers attach
7
+ // with onRead/onWrite, and the verbs map 1:1 onto Zephyr's bt_* GATT surface:
8
+ //
9
+ // start() → register the deferred service table + bt_enable + advertise
10
+ // stop() → bt_le_adv_stop
11
+ // linked() → a central is connected
12
+ // onConnect()/onDrop() → bt_conn callbacks
13
+ // notify(i, v) → bt_gatt_notify on the declared characteristic i
14
+ //
15
+ // Ordering contract (same as the runtime shim's current_char keying):
16
+ // onRead/onWrite/notify bind to the char() they immediately follow in the
17
+ // chain. Declare handlers right where the characteristic is declared.
18
+ // ----------------------------------------------------------------------------
19
+
1
20
  import {
2
21
  bleServerBegin,
3
22
  bleAdvertiseStart,
@@ -11,11 +30,6 @@ import {
11
30
  bleNotify,
12
31
  bleIsConnected,
13
32
  bleClientCount,
14
- bleSetName,
15
- bleUntilConnected,
16
- bleUntilConnectedStart,
17
- bleSetTxPower,
18
- bleStatus,
19
33
  } from './emit.js';
20
34
  import { callback } from './callback.js';
21
35
 
@@ -40,20 +54,6 @@ export enum BlePerm {
40
54
  Notify = 4,
41
55
  }
42
56
 
43
- /** BLE peripheral status (mirrored by the runtime shim). */
44
- export enum BleStatus {
45
- Idle = 0,
46
- Initializing = 1,
47
- Advertising = 2,
48
- Connected = 3,
49
- Error = 4,
50
- }
51
-
52
- export enum BleAdvertisingMode {
53
- Connectable = 'connectable',
54
- NonConnectable = 'non_connectable',
55
- }
56
-
57
57
  /** A well-known GATT characteristic entry in the catalog. */
58
58
  export interface GattCharacteristicDef {
59
59
  readonly uuid: string;
@@ -64,9 +64,10 @@ export interface GattCharacteristicDef {
64
64
  }
65
65
 
66
66
  /**
67
- * Standard GATT services/characteristics. Autocomplete walks the hierarchy:
68
- * GATT.ENVIRONMENTAL. -> TEMPERATURE, HUMIDITY, ...
69
- * Pass the .uuid, .type, and computed perms to BleServer.characteristic().
67
+ * Standard GATT services/characteristics uuid/type/perms reference data.
68
+ * Pass the pieces explicitly to char():
69
+ * GATT.ENVIRONMENTAL.TEMPERATURE
70
+ * char('2A6E', BleValueType.Int16, BlePerm.Read | BlePerm.Notify)
70
71
  */
71
72
  export const GATT = {
72
73
  DEVICE_INFO: {
@@ -88,122 +89,94 @@ export const GATT = {
88
89
  export type CharValue = number | string | boolean | Uint8Array;
89
90
 
90
91
  /**
91
- * BLE GATT peripheral control, lowered to native ESP-IDF NimBLE
92
- * (`nimble_host` / `ble_gap` / `ble_gatts`) by framework-esp32.
93
- *
94
- * No `include()` calls here — NimBLE headers are framework-owned and added via
95
- * forcedIncludes when the program uses ble.* ops.
96
- *
97
- * Transpiler note: method bodies pass parameters directly into semantic calls
98
- * (no local consts / module counters) so the resolver can statically track every
99
- * argument. The characteristic index is carried through the chain via
100
- * `this._charCount` fieldValues, mirroring how HttpRequest carries _method/_url.
92
+ * The GATT declaration chain returned by BLE.service()/BLE.char(). Each char()
93
+ * appends a characteristic; onRead/onWrite/notify bind to the most recent one.
101
94
  */
102
- export class BleClass {
103
- static readonly __instance_name = "Ble";
104
-
105
- /** Begin building a GATT server with the given advertised device name. */
106
- server(name: string): BleServer {
107
- bleSetName(name);
108
- return new BleServer(name, 0, 1);
95
+ export class BleChain {
96
+ /** Begin a new service grouping; subsequent char() calls attach to it. */
97
+ service(uuid: string): BleChain {
98
+ bleAddService(uuid);
99
+ return this;
109
100
  }
110
101
 
111
- /** Initialize NimBLE, register services, and start advertising. */
112
- begin(): void {
113
- bleServerBegin("TypeCAD");
114
- bleAdvertiseStart();
102
+ /** Append a characteristic by uuid, value type, and permissions
103
+ * (`BlePerm.Read | BlePerm.Notify`). Returns this for chaining. */
104
+ char(uuid: string, type: BleValueType, perms: number): BleChain {
105
+ bleAddChar(0, uuid, type, perms, 0);
106
+ return this;
115
107
  }
116
108
 
117
- advertise(): void { bleAdvertiseStart(); }
118
- stopAdvertising(): void { bleAdvertiseStop(); }
109
+ /** Read handler for the most recently declared characteristic. */
110
+ onRead(handler: () => CharValue): BleChain {
111
+ bleOnRead(0, callback(handler));
112
+ return this;
113
+ }
119
114
 
120
- /** Blocking at top level; cooperatively awaitable inside async functions. */
121
- untilConnected(timeoutMs: number = 0): Promise<boolean> {
122
- bleUntilConnected(timeoutMs);
123
- return Promise.resolve(false);
115
+ /** Write handler for the most recently declared characteristic. */
116
+ onWrite(handler: (value: number) => void): BleChain {
117
+ bleOnWrite(0, callback(handler));
118
+ return this;
124
119
  }
125
120
 
126
- untilConnectedStart(): void { bleUntilConnectedStart(); }
127
- isConnected(): boolean { return bleIsConnected(); }
128
- status(): BleStatus { return bleStatus() as BleStatus; }
129
- clientCount(): number { return bleClientCount(); }
130
- txPower(dbm: number): this { bleSetTxPower(dbm); return this; }
131
- notify(index: number, value: number): void { bleNotify(index, value); }
132
121
  }
133
122
 
134
- /**
135
- * Fluent GATT server builder. Returned by `Ble.server()`.
136
- *
137
- * Single-class fluent chain (like HttpRequest): characteristic() returns `this`,
138
- * so onRead/onWrite/onSubscribe chain directly. The _charCount field tracks
139
- * which characteristic slot the callbacks attach to.
140
- *
141
- * Field tracking (read by the transpiler resolver via ctor field assignment):
142
- * _name — advertised device name
143
- * _charCount — current characteristic index (the last characteristic() target)
144
- * _svcCount — current service index
145
- */
146
- export class BleServer {
147
- private _name: string;
148
- private _charCount: number;
149
- private _lastChar: number;
150
- private _svcCount: number;
123
+ /** A GATT peripheral. The advertised device name is the construction fact. */
124
+ export class BLE {
125
+ private readonly _name: string;
151
126
 
152
- constructor(name: string, charCount: number, svcCount: number) {
127
+ constructor(name: string) {
153
128
  this._name = name;
154
- this._charCount = charCount;
155
- this._lastChar = charCount;
156
- this._svcCount = svcCount;
157
129
  }
158
130
 
159
- /** Add a characteristic by UUID, value type, and permissions.
160
- * Combine permissions with `|`: `BlePerm.Read | BlePerm.Notify`.
161
- * Returns this for chaining. */
162
- characteristic(uuid: string, type: BleValueType, perms: number): this {
163
- bleAddChar(this._charCount, uuid, type, perms, this._svcCount);
164
- return this;
131
+ /** Begin a service grouping; chain char() declarations off it. */
132
+ service(uuid: string): BleChain {
133
+ bleAddService(uuid);
134
+ return new BleChain();
165
135
  }
166
136
 
167
- /** Begin a new service grouping. Subsequent characteristics attach to it. */
168
- service(uuid: string): this {
169
- bleAddService(uuid);
170
- return this;
137
+ /** Append a characteristic under the default (Environmental Sensing)
138
+ * service the common single-service case. */
139
+ char(uuid: string, type: BleValueType, perms: number): BleChain {
140
+ bleAddChar(0, uuid, type, perms, 0);
141
+ return new BleChain();
171
142
  }
172
143
 
173
- /** Register a read handler for the most recently added characteristic. */
174
- onRead(handler: () => CharValue): this {
175
- bleOnRead(this._lastChar, callback(handler));
176
- return this;
144
+ /** Register the declared GATT database, enable the stack, and start
145
+ * advertising the construction name. */
146
+ start(): void {
147
+ bleServerBegin(this._name);
148
+ bleAdvertiseStart();
177
149
  }
178
150
 
179
- /** Register a write handler for the most recently added characteristic. */
180
- onWrite(handler: (value: number) => void): this {
181
- bleOnWrite(this._lastChar, callback(handler));
182
- return this;
151
+ /** Stop advertising (the GATT database and any link stay up). */
152
+ stop(): void {
153
+ bleAdvertiseStop();
183
154
  }
184
155
 
185
- /** Register a connect handler (called when a central connects). */
186
- onConnect(handler: () => void): this {
187
- bleOnConnect(callback(handler));
188
- return this;
156
+ /** True while a central is connected. */
157
+ linked(): boolean {
158
+ return bleIsConnected();
189
159
  }
190
160
 
191
- /** Register a disconnect handler (called when a central disconnects). */
192
- onDisconnect(handler: () => void): this {
193
- bleOnDisconnect(callback(handler));
194
- return this;
161
+ /** Number of connected centrals (0 or 1 the shim is single-connection). */
162
+ clients(): number {
163
+ return bleClientCount();
195
164
  }
196
165
 
197
- /** Push a new value to subscribed clients on the most recently added characteristic. */
198
- notify(value: number): void {
199
- bleNotify(this._lastChar, value);
166
+ /** Fires when a central connects. */
167
+ onConnect(handler: () => void): void {
168
+ bleOnConnect(callback(handler));
200
169
  }
201
170
 
202
- /** Initialize NimBLE, register services, and start advertising. */
203
- begin(): void {
204
- bleServerBegin(this._name);
205
- bleAdvertiseStart();
171
+ /** Fires when the central disconnects. */
172
+ onDrop(handler: () => void): void {
173
+ bleOnDisconnect(callback(handler));
206
174
  }
207
- }
208
175
 
209
- export const Ble = new BleClass();
176
+ /** Push a value to subscribed clients on a characteristic
177
+ * (bt_gatt_notify). `index` is the characteristic's declaration order
178
+ * (0-based — the first char() is 0). */
179
+ notify(index: number, value: number): void {
180
+ bleNotify(index, value);
181
+ }
182
+ }
package/src/counter.ts ADDED
@@ -0,0 +1,40 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Counter — the thin Zephyr-shaped hardware counter/timer
3
+ //
4
+ // Zephyr's counter driver with Zephyr's verbs. The instance index selects
5
+ // one of the chip's declared free counters (instance 0 = the first, e.g.
6
+ // RTC1 on the nRF52840; the build errors name the available ones). The alarm
7
+ // frequency is a construction fact: start() applies hz as the top value
8
+ // (counter_freq / hz) and arms the alarm callback.
9
+ // ----------------------------------------------------------------------------
10
+
11
+ import { counterOnAlarm, counterStart, counterStop } from './emit.js';
12
+ import { callback } from './callback.js';
13
+
14
+ export class Counter {
15
+ private readonly _instance: number;
16
+ private readonly _hz: number;
17
+
18
+ /** Construct a counter handle. `hz` is the alarm frequency — start()
19
+ * realizes it as a top value of counter_freq / hz. */
20
+ constructor(instance: number, opts: { hz: number }) {
21
+ this._instance = instance;
22
+ this._hz = opts.hz;
23
+ }
24
+
25
+ /** Register the alarm handler (fires once per top-value wrap). */
26
+ onAlarm(handler: () => void): void {
27
+ counterOnAlarm(this._instance, callback(handler));
28
+ }
29
+
30
+ /** Apply hz as the top value, arm the alarm, and start counting
31
+ * (counter_set_top_value + counter_start). */
32
+ start(): void {
33
+ counterStart(this._instance, this._hz);
34
+ }
35
+
36
+ /** Stop counting (counter_stop). */
37
+ stop(): void {
38
+ counterStop(this._instance);
39
+ }
40
+ }
package/src/dac-pin.ts ADDED
@@ -0,0 +1,28 @@
1
+ // ---------------------------------------------------------------------------
2
+ // DAC — the thin Zephyr-shaped analog output
3
+ //
4
+ // Construction carries the resolution (default: the chip descriptor's channel
5
+ // resolution); write() lowers to a lazy dac_channel_setup (once) +
6
+ // dac_write_value with the RAW code — no 0–255 Arduino range.
7
+ // ----------------------------------------------------------------------------
8
+
9
+ import { dacWriteValue } from './emit.js';
10
+ import type { Pin } from './gpio.js';
11
+
12
+ export class DAC {
13
+ private readonly _pin: number;
14
+ private readonly _resolution: number;
15
+
16
+ /** Construct a DAC channel. `resolution` in bits; 0/omitted = the chip
17
+ * descriptor's channel resolution (e.g. 8 on the ESP32's two channels). */
18
+ constructor(pin: number | Pin, opts?: { resolution?: number }) {
19
+ this._pin = typeof pin === 'number' ? pin : pin.number;
20
+ this._resolution = opts?.resolution ?? 0;
21
+ }
22
+
23
+ /** Write the raw code (dac_write_value). For an 8-bit channel the range
24
+ * is 0–255; for 12-bit, 0–4095 — the channel's resolution decides. */
25
+ write(value: number): void {
26
+ dacWriteValue(this._pin, value, this._resolution);
27
+ }
28
+ }