@typecad/hal 1.0.0-alpha.6 → 1.0.0-alpha.7

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/src/rmt.ts ADDED
@@ -0,0 +1,125 @@
1
+ // ---------------------------------------------------------------------------
2
+ // @typecad/hal — RMT (Remote Control Transceiver) ergonomic API
3
+ // ---------------------------------------------------------------------------
4
+ // Exposes RMT via a RmtChannel class, mirroring the I2CBus/I2CDevice pattern.
5
+ // The transpiler only resolves semantic calls made inside class-method bodies
6
+ // (free top-level calls emit verbatim), so every peripheral exposes its API
7
+ // through a class whose methods call the positional semantic primitives in
8
+ // emit.ts. The resolver collapses object literals, so HALOpIR fields must be
9
+ // scalars; these methods destructure `opts` at the call site before forwarding.
10
+ //
11
+ // Channel identity is the GPIO pin (like pwm.ts's LEDC channel-per-pin model).
12
+
13
+ import type { Pin } from './gpio.js';
14
+ import {
15
+ rmtTxInit,
16
+ rmtRxInit,
17
+ rmtTxWriteBytes,
18
+ rmtTxWriteSymbols,
19
+ rmtTxWaitDone,
20
+ rmtTxDeinit,
21
+ rmtRxOnReceived,
22
+ rmtRxStart,
23
+ rmtRxStop,
24
+ rmtRxRead,
25
+ rmtRxDeinit,
26
+ } from './emit.js';
27
+
28
+ /** Pin identifier accepted by RmtChannel: a Pin object, raw GPIO number, or port name. */
29
+ type RmtPin = Pin | number | string;
30
+
31
+ /**
32
+ * RMT transceiver channel bound to a GPIO pin. One channel per pin; TX and RX
33
+ * are independent roles on the same channel object.
34
+ *
35
+ * ```ts
36
+ * const led = new RmtChannel(LED);
37
+ * led.txInit({ resolutionHz: 10_000_000, bit0: [4, 9], bit1: [9, 4] });
38
+ * led.txWriteBytes([0, 16, 0]);
39
+ * led.txWaitDone();
40
+ * ```
41
+ */
42
+ export class RmtChannel {
43
+ private _pin: RmtPin;
44
+
45
+ constructor(pin: RmtPin) {
46
+ this._pin = pin;
47
+ }
48
+
49
+ // ── TX ─────────────────────────────────────────────────────────────────────
50
+ /** Initialize the channel for TX. Idempotent per pin. Bit timings are fixed
51
+ * at init — ESP-IDF bakes them into the bytes-encoder and exposes no public
52
+ * mutation API. Tick units are 1/resolutionHz seconds.
53
+ *
54
+ * Args are positional scalars (not an opts object) because the transpiler's
55
+ * method-body renderer folds scalar params but not property accesses on an
56
+ * object param (opts.resolutionHz would render as a collapsed object + dead
57
+ * property access). Mirrors how I2CBus.begin(address) takes a scalar. */
58
+ txInit(
59
+ resolutionHz: number,
60
+ bit0Hi: number, bit0Lo: number,
61
+ bit1Hi: number, bit1Lo: number,
62
+ msbFirst: boolean = false,
63
+ queueDepth: number = 4,
64
+ ): void {
65
+ rmtTxInit(
66
+ this._pin,
67
+ resolutionHz,
68
+ bit0Hi, bit0Lo,
69
+ bit1Hi, bit1Lo,
70
+ msbFirst,
71
+ queueDepth,
72
+ );
73
+ }
74
+
75
+ /** Write bytes via the channel's bytes-encoder (timings fixed at txInit). */
76
+ txWriteBytes(bytes: number[] | Uint8Array): void {
77
+ rmtTxWriteBytes(this._pin, bytes);
78
+ }
79
+
80
+ /** Write raw RMT symbols — arbitrary [hiTicks, loTicks] pairs. */
81
+ txWriteSymbols(symbols: [number, number][]): void {
82
+ rmtTxWriteSymbols(this._pin, symbols);
83
+ }
84
+
85
+ /** Block until the queued TX completes. */
86
+ txWaitDone(timeoutMs?: number): void {
87
+ rmtTxWaitDone(this._pin, timeoutMs);
88
+ }
89
+
90
+ /** Tear down the TX channel and release its slot. */
91
+ txDeinit(): void {
92
+ rmtTxDeinit(this._pin);
93
+ }
94
+
95
+ // ── RX ─────────────────────────────────────────────────────────────────────
96
+ /** Initialize the channel for RX. Idempotent per pin. */
97
+ rxInit(resolutionHz: number): void {
98
+ rmtRxInit(this._pin, resolutionHz);
99
+ }
100
+
101
+ /** Register a callback-by-name invoked when an RX burst completes. */
102
+ rxOnReceived(handler: string): void {
103
+ rmtRxOnReceived(this._pin, handler);
104
+ }
105
+
106
+ /** Start receiving. */
107
+ rxStart(): void {
108
+ rmtRxStart(this._pin);
109
+ }
110
+
111
+ /** Stop receiving. */
112
+ rxStop(): void {
113
+ rmtRxStop(this._pin);
114
+ }
115
+
116
+ /** Blocking read — returns flattened symbols [d0,l0,d1,l1,…] in ticks. */
117
+ rxRead(maxCount: number): number[] {
118
+ return rmtRxRead(this._pin, maxCount);
119
+ }
120
+
121
+ /** Tear down the RX channel and release its slot. */
122
+ rxDeinit(): void {
123
+ rmtRxDeinit(this._pin);
124
+ }
125
+ }
package/src/wifi.ts ADDED
@@ -0,0 +1,225 @@
1
+ import {
2
+ wifiConnect,
3
+ wifiConnectStart,
4
+ wifiDisconnect,
5
+ wifiStatus,
6
+ wifiIsConnected,
7
+ wifiLocalIp,
8
+ wifiRssi,
9
+ wifiMac,
10
+ wifiSetHostname,
11
+ wifiSetStaticIp,
12
+ wifiSetAutoReconnect,
13
+ wifiSetPowerSave,
14
+ wifiSetTxPower,
15
+ wifiOnEvent,
16
+ wifiApStart,
17
+ wifiApStop,
18
+ wifiApClientCount,
19
+ wifiApIp,
20
+ wifiApSetChannel,
21
+ wifiApSetHidden,
22
+ wifiApSetMaxClients,
23
+ wifiScan,
24
+ wifiScanStart,
25
+ wifiScanCount,
26
+ wifiScanSsid,
27
+ wifiScanRssi,
28
+ wifiScanEncryption,
29
+ wifiScanChannel,
30
+ wifiSaveCredentials,
31
+ wifiConnectSaved,
32
+ wifiClearCredentials,
33
+ wifiWaitConnected,
34
+ wifiWaitDisconnected,
35
+ } from './emit.js';
36
+ import { callback } from './callback.js';
37
+
38
+ /** Normalized WiFi link status (mapped from esp_wifi events by the runtime shim). */
39
+ export enum WiFiStatus {
40
+ Idle = 0,
41
+ Connecting = 1,
42
+ Connected = 2,
43
+ ConnectFailed = 3,
44
+ Disconnected = 4,
45
+ }
46
+
47
+ export enum WiFiEncryption {
48
+ Open = 0,
49
+ WEP = 1,
50
+ WPA = 2,
51
+ WPA2 = 3,
52
+ WPA3 = 4,
53
+ Enterprise = 5,
54
+ }
55
+
56
+ /**
57
+ * WiFi radio / link control, lowered to native ESP-IDF (`esp_wifi` /
58
+ * `esp_netif` / `esp_event` / `nvs_flash`) by framework-esp32.
59
+ *
60
+ * No `include()` calls here — ESP-IDF headers are framework-owned and added
61
+ * via forcedIncludes when the program uses wifi.* ops (the Preferences
62
+ * lesson: HAL files must not carry platform headers). Frameworks without a
63
+ * wifi lowering reject these ops with a diagnostic.
64
+ */
65
+ export class WiFiClass {
66
+ static readonly __instance_name = "WiFi";
67
+
68
+ /** Blocking at top level; cooperatively awaitable inside async functions. */
69
+ connect(ssid: string, password?: string, timeoutMs: number = 15000): Promise<boolean> {
70
+ wifiConnect(ssid, password, timeoutMs);
71
+ return Promise.resolve(false);
72
+ }
73
+
74
+ /** Fire-and-forget STA begin; poll status() / untilConnected(). */
75
+ connectAsync(ssid: string, password?: string): void {
76
+ wifiConnectStart(ssid, password);
77
+ }
78
+
79
+ untilConnected(timeoutMs: number = 15000): Promise<boolean> {
80
+ wifiWaitConnected(timeoutMs);
81
+ return Promise.resolve(false);
82
+ }
83
+
84
+ untilDisconnected(): Promise<void> {
85
+ wifiWaitDisconnected();
86
+ return Promise.resolve();
87
+ }
88
+
89
+ disconnect(): void {
90
+ wifiDisconnect();
91
+ }
92
+
93
+ isConnected(): boolean {
94
+ return wifiIsConnected();
95
+ }
96
+
97
+ status(): WiFiStatus {
98
+ return wifiStatus() as WiFiStatus;
99
+ }
100
+
101
+ localIP(): string {
102
+ return wifiLocalIp();
103
+ }
104
+
105
+ rssi(): number {
106
+ return wifiRssi();
107
+ }
108
+
109
+ macAddress(): string {
110
+ return wifiMac();
111
+ }
112
+
113
+ hostname(name: string): this {
114
+ wifiSetHostname(name);
115
+ return this;
116
+ }
117
+
118
+ staticIP(ip: string, gateway: string, subnet: string, dns?: string): this {
119
+ wifiSetStaticIp(ip, gateway, subnet, dns);
120
+ return this;
121
+ }
122
+
123
+ autoReconnect(enabled: boolean): this {
124
+ wifiSetAutoReconnect(enabled);
125
+ return this;
126
+ }
127
+
128
+ powerSave(mode: "default" | "none"): this {
129
+ wifiSetPowerSave(mode);
130
+ return this;
131
+ }
132
+
133
+ /** Cap TX power in dBm (roughly 2–20). Safe to call before connect() —
134
+ * the value is applied after the radio starts. */
135
+ txPower(dbm: number): this {
136
+ wifiSetTxPower(dbm);
137
+ return this;
138
+ }
139
+
140
+ saveCredentials(ssid: string, password: string): void {
141
+ wifiSaveCredentials(ssid, password);
142
+ }
143
+
144
+ connectSaved(timeoutMs: number = 15000): boolean {
145
+ return wifiConnectSaved(timeoutMs);
146
+ }
147
+
148
+ clearCredentials(): void {
149
+ wifiClearCredentials();
150
+ }
151
+
152
+ onConnect(handler: () => void): void {
153
+ wifiOnEvent("connect", callback(handler));
154
+ }
155
+
156
+ onDisconnect(handler: () => void): void {
157
+ wifiOnEvent("disconnect", callback(handler));
158
+ }
159
+
160
+ onGotIP(handler: () => void): void {
161
+ wifiOnEvent("got_ip", callback(handler));
162
+ }
163
+
164
+ startAP(ssid: string, password?: string): boolean {
165
+ return wifiApStart(ssid, password);
166
+ }
167
+
168
+ apChannel(ch: number): this {
169
+ wifiApSetChannel(ch);
170
+ return this;
171
+ }
172
+
173
+ apHidden(hidden: boolean): this {
174
+ wifiApSetHidden(hidden);
175
+ return this;
176
+ }
177
+
178
+ apMaxClients(n: number): this {
179
+ wifiApSetMaxClients(n);
180
+ return this;
181
+ }
182
+
183
+ stopAP(): void {
184
+ wifiApStop();
185
+ }
186
+
187
+ apClientCount(): number {
188
+ return wifiApClientCount();
189
+ }
190
+
191
+ apIP(): string {
192
+ return wifiApIp();
193
+ }
194
+
195
+ scan(): number {
196
+ return wifiScan();
197
+ }
198
+
199
+ scanAsync(): Promise<void> {
200
+ wifiScanStart();
201
+ return Promise.resolve();
202
+ }
203
+
204
+ scanCount(): number {
205
+ return wifiScanCount();
206
+ }
207
+
208
+ scanSSID(i: number): string {
209
+ return wifiScanSsid(i);
210
+ }
211
+
212
+ scanRSSI(i: number): number {
213
+ return wifiScanRssi(i);
214
+ }
215
+
216
+ scanEncryption(i: number): WiFiEncryption {
217
+ return wifiScanEncryption(i) as WiFiEncryption;
218
+ }
219
+
220
+ scanChannel(i: number): number {
221
+ return wifiScanChannel(i);
222
+ }
223
+ }
224
+
225
+ export const WiFi = new WiFiClass();