@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/dist/ble.js CHANGED
@@ -1,4 +1,22 @@
1
- import { bleServerBegin, bleAdvertiseStart, bleAdvertiseStop, bleAddService, bleAddChar, bleOnRead, bleOnWrite, bleOnConnect, bleOnDisconnect, bleNotify, bleIsConnected, bleClientCount, bleSetName, bleUntilConnected, bleUntilConnectedStart, bleSetTxPower, bleStatus, } from './emit.js';
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
+ import { bleServerBegin, bleAdvertiseStart, bleAdvertiseStop, bleAddService, bleAddChar, bleOnRead, bleOnWrite, bleOnConnect, bleOnDisconnect, bleNotify, bleIsConnected, bleClientCount, } from './emit.js';
2
20
  import { callback } from './callback.js';
3
21
  /** Characteristic value encoding — drives both TS callback types and C++ marshalling. */
4
22
  export var BleValueType;
@@ -21,24 +39,11 @@ export var BlePerm;
21
39
  BlePerm[BlePerm["Write"] = 2] = "Write";
22
40
  BlePerm[BlePerm["Notify"] = 4] = "Notify";
23
41
  })(BlePerm || (BlePerm = {}));
24
- /** BLE peripheral status (mirrored by the runtime shim). */
25
- export var BleStatus;
26
- (function (BleStatus) {
27
- BleStatus[BleStatus["Idle"] = 0] = "Idle";
28
- BleStatus[BleStatus["Initializing"] = 1] = "Initializing";
29
- BleStatus[BleStatus["Advertising"] = 2] = "Advertising";
30
- BleStatus[BleStatus["Connected"] = 3] = "Connected";
31
- BleStatus[BleStatus["Error"] = 4] = "Error";
32
- })(BleStatus || (BleStatus = {}));
33
- export var BleAdvertisingMode;
34
- (function (BleAdvertisingMode) {
35
- BleAdvertisingMode["Connectable"] = "connectable";
36
- BleAdvertisingMode["NonConnectable"] = "non_connectable";
37
- })(BleAdvertisingMode || (BleAdvertisingMode = {}));
38
42
  /**
39
- * Standard GATT services/characteristics. Autocomplete walks the hierarchy:
40
- * GATT.ENVIRONMENTAL. -> TEMPERATURE, HUMIDITY, ...
41
- * Pass the .uuid, .type, and computed perms to BleServer.characteristic().
43
+ * Standard GATT services/characteristics uuid/type/perms reference data.
44
+ * Pass the pieces explicitly to char():
45
+ * GATT.ENVIRONMENTAL.TEMPERATURE
46
+ * char('2A6E', BleValueType.Int16, BlePerm.Read | BlePerm.Notify)
42
47
  */
43
48
  export const GATT = {
44
49
  DEVICE_INFO: {
@@ -56,102 +61,78 @@ export const GATT = {
56
61
  },
57
62
  };
58
63
  /**
59
- * BLE GATT peripheral control, lowered to native ESP-IDF NimBLE
60
- * (`nimble_host` / `ble_gap` / `ble_gatts`) by framework-esp32.
61
- *
62
- * No `include()` calls here — NimBLE headers are framework-owned and added via
63
- * forcedIncludes when the program uses ble.* ops.
64
- *
65
- * Transpiler note: method bodies pass parameters directly into semantic calls
66
- * (no local consts / module counters) so the resolver can statically track every
67
- * argument. The characteristic index is carried through the chain via
68
- * `this._charCount` fieldValues, mirroring how HttpRequest carries _method/_url.
64
+ * The GATT declaration chain returned by BLE.service()/BLE.char(). Each char()
65
+ * appends a characteristic; onRead/onWrite/notify bind to the most recent one.
69
66
  */
70
- export class BleClass {
71
- /** Begin building a GATT server with the given advertised device name. */
72
- server(name) {
73
- bleSetName(name);
74
- return new BleServer(name, 0, 1);
67
+ export class BleChain {
68
+ /** Begin a new service grouping; subsequent char() calls attach to it. */
69
+ service(uuid) {
70
+ bleAddService(uuid);
71
+ return this;
75
72
  }
76
- /** Initialize NimBLE, register services, and start advertising. */
77
- begin() {
78
- bleServerBegin("TypeCAD");
79
- bleAdvertiseStart();
73
+ /** Append a characteristic by uuid, value type, and permissions
74
+ * (`BlePerm.Read | BlePerm.Notify`). Returns this for chaining. */
75
+ char(uuid, type, perms) {
76
+ bleAddChar(0, uuid, type, perms, 0);
77
+ return this;
80
78
  }
81
- advertise() { bleAdvertiseStart(); }
82
- stopAdvertising() { bleAdvertiseStop(); }
83
- /** Blocking at top level; cooperatively awaitable inside async functions. */
84
- untilConnected(timeoutMs = 0) {
85
- bleUntilConnected(timeoutMs);
86
- return Promise.resolve(false);
79
+ /** Read handler for the most recently declared characteristic. */
80
+ onRead(handler) {
81
+ bleOnRead(0, callback(handler));
82
+ return this;
83
+ }
84
+ /** Write handler for the most recently declared characteristic. */
85
+ onWrite(handler) {
86
+ bleOnWrite(0, callback(handler));
87
+ return this;
87
88
  }
88
- untilConnectedStart() { bleUntilConnectedStart(); }
89
- isConnected() { return bleIsConnected(); }
90
- status() { return bleStatus(); }
91
- clientCount() { return bleClientCount(); }
92
- txPower(dbm) { bleSetTxPower(dbm); return this; }
93
- notify(index, value) { bleNotify(index, value); }
94
89
  }
95
- BleClass.__instance_name = "Ble";
96
- /**
97
- * Fluent GATT server builder. Returned by `Ble.server()`.
98
- *
99
- * Single-class fluent chain (like HttpRequest): characteristic() returns `this`,
100
- * so onRead/onWrite/onSubscribe chain directly. The _charCount field tracks
101
- * which characteristic slot the callbacks attach to.
102
- *
103
- * Field tracking (read by the transpiler resolver via ctor field assignment):
104
- * _name — advertised device name
105
- * _charCount — current characteristic index (the last characteristic() target)
106
- * _svcCount — current service index
107
- */
108
- export class BleServer {
109
- constructor(name, charCount, svcCount) {
90
+ /** A GATT peripheral. The advertised device name is the construction fact. */
91
+ export class BLE {
92
+ constructor(name) {
110
93
  this._name = name;
111
- this._charCount = charCount;
112
- this._lastChar = charCount;
113
- this._svcCount = svcCount;
114
94
  }
115
- /** Add a characteristic by UUID, value type, and permissions.
116
- * Combine permissions with `|`: `BlePerm.Read | BlePerm.Notify`.
117
- * Returns this for chaining. */
118
- characteristic(uuid, type, perms) {
119
- bleAddChar(this._charCount, uuid, type, perms, this._svcCount);
120
- return this;
121
- }
122
- /** Begin a new service grouping. Subsequent characteristics attach to it. */
95
+ /** Begin a service grouping; chain char() declarations off it. */
123
96
  service(uuid) {
124
97
  bleAddService(uuid);
125
- return this;
98
+ return new BleChain();
126
99
  }
127
- /** Register a read handler for the most recently added characteristic. */
128
- onRead(handler) {
129
- bleOnRead(this._lastChar, callback(handler));
130
- return this;
100
+ /** Append a characteristic under the default (Environmental Sensing)
101
+ * service — the common single-service case. */
102
+ char(uuid, type, perms) {
103
+ bleAddChar(0, uuid, type, perms, 0);
104
+ return new BleChain();
131
105
  }
132
- /** Register a write handler for the most recently added characteristic. */
133
- onWrite(handler) {
134
- bleOnWrite(this._lastChar, callback(handler));
135
- return this;
106
+ /** Register the declared GATT database, enable the stack, and start
107
+ * advertising the construction name. */
108
+ start() {
109
+ bleServerBegin(this._name);
110
+ bleAdvertiseStart();
111
+ }
112
+ /** Stop advertising (the GATT database and any link stay up). */
113
+ stop() {
114
+ bleAdvertiseStop();
136
115
  }
137
- /** Register a connect handler (called when a central connects). */
116
+ /** True while a central is connected. */
117
+ linked() {
118
+ return bleIsConnected();
119
+ }
120
+ /** Number of connected centrals (0 or 1 — the shim is single-connection). */
121
+ clients() {
122
+ return bleClientCount();
123
+ }
124
+ /** Fires when a central connects. */
138
125
  onConnect(handler) {
139
126
  bleOnConnect(callback(handler));
140
- return this;
141
127
  }
142
- /** Register a disconnect handler (called when a central disconnects). */
143
- onDisconnect(handler) {
128
+ /** Fires when the central disconnects. */
129
+ onDrop(handler) {
144
130
  bleOnDisconnect(callback(handler));
145
- return this;
146
131
  }
147
- /** Push a new value to subscribed clients on the most recently added characteristic. */
148
- notify(value) {
149
- bleNotify(this._lastChar, value);
150
- }
151
- /** Initialize NimBLE, register services, and start advertising. */
152
- begin() {
153
- bleServerBegin(this._name);
154
- bleAdvertiseStart();
132
+ /** Push a value to subscribed clients on a characteristic
133
+ * (bt_gatt_notify). `index` is the characteristic's declaration order
134
+ * (0-based — the first char() is 0). */
135
+ notify(index, value) {
136
+ bleNotify(index, value);
155
137
  }
156
138
  }
157
- export const Ble = new BleClass();
@@ -0,0 +1,16 @@
1
+ export declare class Counter {
2
+ private readonly _instance;
3
+ private readonly _hz;
4
+ /** Construct a counter handle. `hz` is the alarm frequency — start()
5
+ * realizes it as a top value of counter_freq / hz. */
6
+ constructor(instance: number, opts: {
7
+ hz: number;
8
+ });
9
+ /** Register the alarm handler (fires once per top-value wrap). */
10
+ onAlarm(handler: () => void): void;
11
+ /** Apply hz as the top value, arm the alarm, and start counting
12
+ * (counter_set_top_value + counter_start). */
13
+ start(): void;
14
+ /** Stop counting (counter_stop). */
15
+ stop(): void;
16
+ }
@@ -0,0 +1,32 @@
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
+ import { counterOnAlarm, counterStart, counterStop } from './emit.js';
11
+ import { callback } from './callback.js';
12
+ export class Counter {
13
+ /** Construct a counter handle. `hz` is the alarm frequency — start()
14
+ * realizes it as a top value of counter_freq / hz. */
15
+ constructor(instance, opts) {
16
+ this._instance = instance;
17
+ this._hz = opts.hz;
18
+ }
19
+ /** Register the alarm handler (fires once per top-value wrap). */
20
+ onAlarm(handler) {
21
+ counterOnAlarm(this._instance, callback(handler));
22
+ }
23
+ /** Apply hz as the top value, arm the alarm, and start counting
24
+ * (counter_set_top_value + counter_start). */
25
+ start() {
26
+ counterStart(this._instance, this._hz);
27
+ }
28
+ /** Stop counting (counter_stop). */
29
+ stop() {
30
+ counterStop(this._instance);
31
+ }
32
+ }
@@ -0,0 +1,13 @@
1
+ import type { Pin } from './gpio.js';
2
+ export declare class DAC {
3
+ private readonly _pin;
4
+ private readonly _resolution;
5
+ /** Construct a DAC channel. `resolution` in bits; 0/omitted = the chip
6
+ * descriptor's channel resolution (e.g. 8 on the ESP32's two channels). */
7
+ constructor(pin: number | Pin, opts?: {
8
+ resolution?: number;
9
+ });
10
+ /** Write the raw code (dac_write_value). For an 8-bit channel the range
11
+ * is 0–255; for 12-bit, 0–4095 — the channel's resolution decides. */
12
+ write(value: number): void;
13
+ }
@@ -0,0 +1,21 @@
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
+ import { dacWriteValue } from './emit.js';
9
+ export class DAC {
10
+ /** Construct a DAC channel. `resolution` in bits; 0/omitted = the chip
11
+ * descriptor's channel resolution (e.g. 8 on the ESP32's two channels). */
12
+ constructor(pin, opts) {
13
+ this._pin = typeof pin === 'number' ? pin : pin.number;
14
+ this._resolution = opts?.resolution ?? 0;
15
+ }
16
+ /** Write the raw code (dac_write_value). For an 8-bit channel the range
17
+ * is 0–255; for 12-bit, 0–4095 — the channel's resolution decides. */
18
+ write(value) {
19
+ dacWriteValue(this._pin, value, this._resolution);
20
+ }
21
+ }