@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.
- package/README.md +145 -87
- package/dist/adc-pin.d.ts +53 -0
- package/dist/adc-pin.js +69 -0
- package/dist/async.d.ts +3 -11
- package/dist/async.js +3 -15
- package/dist/ble.d.ts +42 -78
- package/dist/ble.js +79 -98
- package/dist/counter.d.ts +16 -0
- package/dist/counter.js +32 -0
- package/dist/dac-pin.d.ts +13 -0
- package/dist/dac-pin.js +21 -0
- package/dist/emit.d.ts +123 -196
- package/dist/emit.js +146 -208
- package/dist/fs.d.ts +12 -31
- package/dist/fs.js +30 -40
- package/dist/gpio-pin.d.ts +46 -0
- package/dist/gpio-pin.js +77 -0
- package/dist/gpio.d.ts +0 -103
- package/dist/gpio.js +16 -227
- package/dist/http.d.ts +38 -39
- package/dist/http.js +53 -78
- package/dist/i2c-target.d.ts +28 -0
- package/dist/i2c-target.js +52 -0
- package/dist/i2c.d.ts +5 -43
- package/dist/i2c.js +17 -130
- package/dist/index.d.ts +30 -42
- package/dist/index.js +27 -39
- package/dist/math.d.ts +0 -13
- package/dist/math.js +9 -13
- package/dist/mqtt.d.ts +15 -25
- package/dist/mqtt.js +32 -28
- package/dist/power.d.ts +9 -17
- package/dist/power.js +22 -20
- package/dist/preferences.d.ts +17 -25
- package/dist/preferences.js +54 -52
- package/dist/pulse.d.ts +4 -4
- package/dist/pulse.js +6 -2
- package/dist/pwm-pin.d.ts +24 -0
- package/dist/pwm-pin.js +38 -0
- package/dist/random.d.ts +0 -2
- package/dist/random.js +14 -20
- package/dist/sensor-catalog.generated.d.ts +997 -0
- package/dist/sensor-catalog.generated.js +764 -0
- package/dist/sensor.d.ts +46 -0
- package/dist/sensor.js +52 -0
- package/dist/shift-pin.d.ts +2 -0
- package/dist/shift-pin.js +13 -0
- package/dist/spi-target.d.ts +25 -0
- package/dist/spi-target.js +42 -0
- package/dist/spi.d.ts +8 -26
- package/dist/spi.js +15 -87
- package/dist/thread.d.ts +18 -0
- package/dist/thread.js +36 -0
- package/dist/time.d.ts +23 -0
- package/dist/time.js +43 -0
- package/dist/timing.d.ts +17 -7
- package/dist/timing.js +17 -7
- package/dist/types.d.ts +4 -34
- package/dist/types.js +5 -47
- package/dist/uart-port.d.ts +26 -0
- package/dist/uart-port.js +52 -0
- package/dist/uart.d.ts +0 -17
- package/dist/uart.js +10 -53
- package/dist/usb.d.ts +26 -0
- package/dist/usb.js +66 -0
- package/dist/watchdog.d.ts +12 -0
- package/dist/watchdog.js +28 -0
- package/dist/wifi.d.ts +88 -64
- package/dist/wifi.js +112 -126
- package/dist/zephyr-tokens.generated.d.ts +8 -0
- package/dist/zephyr-tokens.generated.js +73 -0
- package/package.json +7 -8
- package/src/adc-pin.ts +89 -0
- package/src/async.ts +3 -16
- package/src/ble.ts +84 -111
- package/src/counter.ts +40 -0
- package/src/dac-pin.ts +28 -0
- package/src/emit.ts +157 -238
- package/src/fs.ts +47 -54
- package/src/gpio-pin.ts +91 -0
- package/src/gpio.ts +49 -320
- package/src/http.ts +75 -95
- package/src/i2c-target.ts +63 -0
- package/src/i2c.ts +29 -167
- package/src/index.ts +32 -46
- package/src/math.ts +9 -14
- package/src/mqtt.ts +39 -31
- package/src/preferences.ts +52 -61
- package/src/pwm-pin.ts +54 -0
- package/src/random.ts +14 -21
- package/src/sensor-catalog.generated.ts +1238 -0
- package/src/sensor.ts +80 -0
- package/src/shift-pin.ts +16 -0
- package/src/spi-target.ts +53 -0
- package/src/spi.ts +27 -121
- package/src/thread.ts +44 -0
- package/src/time.ts +50 -0
- package/src/types.ts +14 -89
- package/src/uart-port.ts +64 -0
- package/src/usb.ts +82 -0
- package/src/watchdog.ts +35 -0
- package/src/wifi.ts +147 -179
- package/src/zephyr-tokens.generated.ts +77 -0
- package/src/adc.ts +0 -30
- package/src/capacitive.ts +0 -31
- package/src/constants.ts +0 -23
- package/src/dac.ts +0 -21
- package/src/eeprom.ts +0 -26
- package/src/interrupts.ts +0 -35
- package/src/mdns.ts +0 -50
- package/src/ota.ts +0 -44
- package/src/power.ts +0 -36
- package/src/pulse.ts +0 -63
- package/src/rmt.ts +0 -125
- package/src/shift.ts +0 -88
- package/src/temperature.ts +0 -20
- package/src/timer.ts +0 -58
- package/src/timing.ts +0 -67
- package/src/uart.ts +0 -77
- package/src/utils.ts +0 -17
- 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
|
|
68
|
-
*
|
|
69
|
-
*
|
|
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
|
-
*
|
|
92
|
-
*
|
|
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
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
118
|
-
|
|
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
|
-
/**
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
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
|
-
|
|
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
|
|
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
|
-
/**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
/**
|
|
168
|
-
service
|
|
169
|
-
|
|
170
|
-
|
|
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
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
/**
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return this;
|
|
151
|
+
/** Stop advertising (the GATT database and any link stay up). */
|
|
152
|
+
stop(): void {
|
|
153
|
+
bleAdvertiseStop();
|
|
183
154
|
}
|
|
184
155
|
|
|
185
|
-
/**
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
return this;
|
|
156
|
+
/** True while a central is connected. */
|
|
157
|
+
linked(): boolean {
|
|
158
|
+
return bleIsConnected();
|
|
189
159
|
}
|
|
190
160
|
|
|
191
|
-
/**
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
/**
|
|
198
|
-
|
|
199
|
-
|
|
166
|
+
/** Fires when a central connects. */
|
|
167
|
+
onConnect(handler: () => void): void {
|
|
168
|
+
bleOnConnect(callback(handler));
|
|
200
169
|
}
|
|
201
170
|
|
|
202
|
-
/**
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
bleAdvertiseStart();
|
|
171
|
+
/** Fires when the central disconnects. */
|
|
172
|
+
onDrop(handler: () => void): void {
|
|
173
|
+
bleOnDisconnect(callback(handler));
|
|
206
174
|
}
|
|
207
|
-
}
|
|
208
175
|
|
|
209
|
-
|
|
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
|
+
}
|