@typecad/hal 0.1.0-alpha.2 → 1.0.0-alpha.10

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 (57) hide show
  1. package/README.md +8 -7
  2. package/dist/ble.d.ts +160 -0
  3. package/dist/ble.js +157 -0
  4. package/dist/capacitive.d.ts +22 -0
  5. package/dist/capacitive.js +27 -0
  6. package/dist/emit.d.ts +139 -0
  7. package/dist/emit.js +152 -0
  8. package/dist/fs.d.ts +21 -1
  9. package/dist/fs.js +29 -21
  10. package/dist/http.d.ts +48 -0
  11. package/dist/http.js +104 -0
  12. package/dist/i2c.d.ts +7 -0
  13. package/dist/i2c.js +15 -4
  14. package/dist/index.d.ts +18 -1
  15. package/dist/index.js +17 -1
  16. package/dist/mdns.d.ts +31 -0
  17. package/dist/mdns.js +43 -0
  18. package/dist/mqtt.d.ts +33 -0
  19. package/dist/mqtt.js +48 -0
  20. package/dist/ota.d.ts +29 -0
  21. package/dist/ota.js +38 -0
  22. package/dist/power.d.ts +7 -0
  23. package/dist/power.js +10 -1
  24. package/dist/preferences.d.ts +11 -2
  25. package/dist/preferences.js +28 -27
  26. package/dist/register.d.ts +12 -4
  27. package/dist/register.js +18 -1
  28. package/dist/rmt.d.ts +48 -0
  29. package/dist/rmt.js +81 -0
  30. package/dist/spi.js +10 -7
  31. package/dist/temperature.d.ts +14 -0
  32. package/dist/temperature.js +17 -0
  33. package/dist/timer.d.ts +14 -17
  34. package/dist/timer.js +20 -22
  35. package/dist/types.d.ts +1 -1
  36. package/dist/wifi.d.ts +67 -0
  37. package/dist/wifi.js +151 -0
  38. package/package.json +6 -5
  39. package/src/ble.ts +209 -0
  40. package/src/capacitive.ts +31 -0
  41. package/src/emit.ts +180 -0
  42. package/src/fs.ts +30 -22
  43. package/src/http.ts +144 -0
  44. package/src/i2c.ts +16 -4
  45. package/src/index.ts +21 -1
  46. package/src/mdns.ts +50 -0
  47. package/src/mqtt.ts +57 -0
  48. package/src/ota.ts +44 -0
  49. package/src/power.ts +11 -1
  50. package/src/preferences.ts +56 -27
  51. package/src/register.ts +16 -4
  52. package/src/rmt.ts +125 -0
  53. package/src/spi.ts +10 -7
  54. package/src/temperature.ts +20 -0
  55. package/src/timer.ts +22 -23
  56. package/src/types.ts +1 -0
  57. package/src/wifi.ts +221 -0
package/src/power.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { powerDeepSleep, powerLightSleep, powerSetCpuFrequency } from './emit.js';
1
+ import { powerDeepSleep, powerLightSleep, powerSetCpuFrequency, powerDeepSleepPin } from './emit.js';
2
2
 
3
3
  /**
4
4
  * PowerClass provides control over MCU power states and clock frequencies.
@@ -14,6 +14,16 @@ export class PowerClass {
14
14
  powerDeepSleep(ms);
15
15
  }
16
16
 
17
+ /** Enter deep sleep until `pin` reaches `level` (0 = low, 1 = high).
18
+ *
19
+ * Lowers to ext0 wakeup (Xtensa ESP32/S3, RTC pins only) or the gpio-wakeup
20
+ * variant (RISC-V C3/C6) depending on the target. `pin` must be RTC-capable;
21
+ * the framework flags non-RTC pins at compile time. Wakeup resets the chip,
22
+ * so this call never returns. */
23
+ deepSleepPin(pin: number, level: 0 | 1): void {
24
+ powerDeepSleepPin(pin, level);
25
+ }
26
+
17
27
  lightSleep(): void {
18
28
  powerLightSleep();
19
29
  }
@@ -1,57 +1,86 @@
1
- import { rawCpp } from './emit.js';
1
+ import {
2
+ preferencesBegin,
3
+ preferencesEnd,
4
+ preferencesClear,
5
+ preferencesRemove,
6
+ preferencesPutInt,
7
+ preferencesGetInt,
8
+ preferencesPutUInt,
9
+ preferencesGetUInt,
10
+ preferencesPutBool,
11
+ preferencesGetBool,
12
+ preferencesPutFloat,
13
+ preferencesGetFloat,
14
+ preferencesPutString,
15
+ preferencesGetString,
16
+ } from './emit.js';
2
17
 
18
+ /**
19
+ * Preferences — a persistent key/value store, lowered to native NVS
20
+ * (nvs_flash / nvs_open / nvs_set_* / nvs_get_*) by framework-esp32.
21
+ *
22
+ * No include() calls here — NVS headers are framework-owned and added via
23
+ * forcedIncludes when the program uses preferences.* ops. Method bodies pass
24
+ * parameters directly into semantic calls so the resolver can statically track
25
+ * every argument (matching the WiFi/HTTP HAL pattern).
26
+ */
3
27
  export class PreferencesClass {
4
28
  static readonly __instance_name = "Preferences";
5
- // NOTE: no __includes here. The transpiler emits singleton __includes on
6
- // every architecture with no filtering, and <Preferences.h> is ESP32-only —
7
- // adding it would break AVR builds. The class is ESP32-only by convention.
8
29
 
9
30
  begin(name: string, readOnly: boolean = false): void {
10
- rawCpp(`Preferences.begin(${name}.c_str(), ${readOnly});`);
31
+ preferencesBegin(name, readOnly);
11
32
  }
33
+
12
34
  end(): void {
13
- rawCpp(`Preferences.end();`);
35
+ preferencesEnd();
14
36
  }
37
+
15
38
  clear(): void {
16
- rawCpp(`Preferences.clear();`);
39
+ preferencesClear();
17
40
  }
41
+
18
42
  remove(key: string): void {
19
- rawCpp(`Preferences.remove(${key}.c_str());`);
43
+ preferencesRemove(key);
20
44
  }
45
+
21
46
  putInt(key: string, value: number): void {
22
- rawCpp(`Preferences.putInt(${key}.c_str(), ${value});`);
47
+ preferencesPutInt(key, value);
23
48
  }
49
+
24
50
  getInt(key: string, defaultValue: number = 0): number {
25
- rawCpp(`return Preferences.getInt(${key}.c_str(), ${defaultValue});`);
26
- return 0;
51
+ return preferencesGetInt(key, defaultValue);
27
52
  }
53
+
28
54
  putUInt(key: string, value: number): void {
29
- rawCpp(`Preferences.putUInt(${key}.c_str(), ${value});`);
55
+ preferencesPutUInt(key, value);
30
56
  }
57
+
31
58
  getUInt(key: string, defaultValue: number = 0): number {
32
- rawCpp(`return Preferences.getUInt(${key}.c_str(), ${defaultValue});`);
33
- return 0;
34
- }
35
- putFloat(key: string, value: number): void {
36
- rawCpp(`Preferences.putFloat(${key}.c_str(), ${value});`);
37
- }
38
- getFloat(key: string, defaultValue: number = 0): number {
39
- rawCpp(`return Preferences.getFloat(${key}.c_str(), ${defaultValue});`);
40
- return 0;
59
+ return preferencesGetUInt(key, defaultValue);
41
60
  }
61
+
42
62
  putBool(key: string, value: boolean): void {
43
- rawCpp(`Preferences.putBool(${key}.c_str(), ${value});`);
63
+ preferencesPutBool(key, value);
44
64
  }
65
+
45
66
  getBool(key: string, defaultValue: boolean = false): boolean {
46
- rawCpp(`return Preferences.getBool(${key}.c_str(), ${defaultValue});`);
47
- return false;
67
+ return preferencesGetBool(key, defaultValue);
68
+ }
69
+
70
+ putFloat(key: string, value: number): void {
71
+ preferencesPutFloat(key, value);
48
72
  }
73
+
74
+ getFloat(key: string, defaultValue: number = 0): number {
75
+ return preferencesGetFloat(key, defaultValue);
76
+ }
77
+
49
78
  putString(key: string, value: string): void {
50
- rawCpp(`Preferences.putString(${key}.c_str(), ${value}.c_str());`);
79
+ preferencesPutString(key, value);
51
80
  }
81
+
52
82
  getString(key: string, defaultValue: string = ""): string {
53
- rawCpp(`return Preferences.getString(${key}.c_str(), ${defaultValue}.c_str());`);
54
- return "";
83
+ return preferencesGetString(key, defaultValue);
55
84
  }
56
85
  }
57
86
 
package/src/register.ts CHANGED
@@ -27,9 +27,21 @@ export type Bit = 0 | 1;
27
27
  export type Bits<N extends number = number> = number;
28
28
 
29
29
  /** Class decorator marking a struct as a memory-mapped register at `address`.
30
- * Erased at transpile time — the class becomes a `volatile uint32_t*`. */
31
- export declare function register(address: number): ClassDecorator;
30
+ * Erased at transpile time — the class becomes a `volatile uint32_t*`.
31
+ *
32
+ * These carry real (inert) runtime bodies rather than `declare`, so the
33
+ * `export { register, bits }` re-export in index.ts resolves under Node's ESM
34
+ * loader, which validates that re-exported bindings exist at runtime. They are
35
+ * never invoked: the cuttlefish transpiler detects them by name and lowers the
36
+ * decorated struct away, so these stubs are only reached when the decorator
37
+ * source is imported without transpilation (e.g. host-side tests). */
38
+ export function register(_address: number): ClassDecorator {
39
+ return () => {};
40
+ }
32
41
 
33
42
  /** Property decorator carrying the bit range [lo, hi] (inclusive) of a field
34
- * within its register. Erased at transpile time. */
35
- export declare function bits(hi: number, lo: number): PropertyDecorator;
43
+ * within its register. Erased at transpile time. See `register` for why these
44
+ * have runtime bodies. */
45
+ export function bits(_hi: number, _lo: number): PropertyDecorator {
46
+ return () => {};
47
+ }
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/spi.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { spiBegin, spiEnd, spiTransfer, spiBeginTx, spiEndTx, spiCsLow, spiCsHigh, spiSetMode, spiSetBitOrder, rawCpp } from './emit.js';
1
+ import { spiBegin, spiEnd, spiTransfer, spiBeginTx, spiEndTx, spiCsLow, spiCsHigh, spiSetMode, spiSetBitOrder, spiReadBuffer, rawCpp } from './emit.js';
2
2
  import { include } from './include.js';
3
3
  import type { Pin } from './gpio.js';
4
4
  import type { SPIMode, SPISettings } from './types.js';
@@ -30,12 +30,15 @@ export class SPIDevice {
30
30
 
31
31
  readRegister(register: number, count: number): Uint8Array {
32
32
  include("<SPI.h>");
33
- rawCpp(`digitalWrite(${this._cs}, LOW);`);
34
- rawCpp(`${this._bus}.transfer(${register});`);
35
- rawCpp(`static uint8_t __spi_buf[${count}];`);
36
- rawCpp(`for (int i=0; i<${count}; i++) __spi_buf[i] = ${this._bus}.transfer(0x00);`);
37
- rawCpp(`digitalWrite(${this._cs}, HIGH);`);
38
- rawCpp(`return __spi_buf;`);
33
+ spiCsLow(this._cs);
34
+ spiTransfer(this._bus, register);
35
+ // Clock `count` dummy bytes and drain them into the caller's buffer
36
+ // (declared by the Uint8Array return marker as `uint8_t data[count]`).
37
+ // Using the semantic primitive — NOT rawCpp — keeps the buffer in user
38
+ // scope so it survives the return (no decayed pointer) and `data.length`
39
+ // / `data[i]` work, mirroring I2CDevice.readBytes.
40
+ spiReadBuffer(this._bus, count, new Uint8Array(count));
41
+ spiCsHigh(this._cs);
39
42
  return new Uint8Array(count);
40
43
  }
41
44
 
@@ -0,0 +1,20 @@
1
+ /**
2
+ * TemperatureClass — on-chip die temperature sensor.
3
+ *
4
+ * Lowered to the temp.* HAL op: ESP-IDF's temperature_sensor driver reads the
5
+ * internal die temperature in °C. Useful for thermal monitoring and
6
+ * compensation. No external components required.
7
+ */
8
+ export class TemperatureClass {
9
+ static readonly __instance_name = "Temperature";
10
+
11
+ /** Read the on-chip die temperature in degrees Celsius. */
12
+ read(): number {
13
+ return tempRead();
14
+ }
15
+ }
16
+
17
+ export const Temperature = new TemperatureClass();
18
+
19
+ // ── Semantic primitive (resolved to temp.* HAL op by the transpiler) ──
20
+ export function tempRead(): number { return 0; }
package/src/timer.ts CHANGED
@@ -1,12 +1,16 @@
1
- import { rawCpp, boardResolve } from './emit.js';
1
+ import { boardResolve } from './emit.js';
2
2
  import { callback } from './callback.js';
3
3
 
4
4
  /**
5
5
  * HardwareTimer provides direct control over the board's hardware timers.
6
- *
6
+ *
7
7
  * Hardware timers are distinct from the software-based setInterval/setTimeout
8
8
  * and are typically used for high-precision timing, PWM generation, or
9
- * interrupt-driven tasks.
9
+ * interrupt-driven tasks. Lowered to hwtimer.* HAL ops: ESP-IDF's GPTimer
10
+ * driver (driver/gptimer.h); Arduino's HardwareTimer (Timer0/1/2 on STM32).
11
+ *
12
+ * The instance index maps to the platform's timer numbering (ESP-IDF GPTimer
13
+ * unit 0..n, STM32 TIM0..n).
10
14
  */
11
15
  export class HardwareTimer {
12
16
  private _instance: number;
@@ -15,38 +19,27 @@ export class HardwareTimer {
15
19
  this._instance = instance;
16
20
  }
17
21
 
18
- /**
19
- * Sets the timer frequency in Hertz.
20
- * Note: The actual frequency may be limited by the hardware's clock dividers.
21
- */
22
+ /** Sets the timer frequency in Hertz. */
22
23
  setFrequency(hz: number): void {
23
- rawCpp(`Timer${this._instance}.setFrequency(${hz});`);
24
+ hwtimerSetFrequency(this._instance, hz);
24
25
  }
25
26
 
26
- /**
27
- * Attaches an interrupt handler that executes when the timer overflows.
28
- */
27
+ /** Attaches an interrupt handler that executes when the timer overflows. */
29
28
  onOverflow(handler: () => void): void {
30
- rawCpp(`Timer${this._instance}.onOverflow(${callback(handler)});`);
29
+ hwtimerOnOverflow(this._instance, callback(handler));
31
30
  }
32
31
 
33
- /**
34
- * Starts the timer.
35
- */
32
+ /** Starts the timer. */
36
33
  start(): void {
37
- rawCpp(`Timer${this._instance}.start();`);
34
+ hwtimerStart(this._instance);
38
35
  }
39
36
 
40
- /**
41
- * Stops the timer.
42
- */
37
+ /** Stops the timer. */
43
38
  stop(): void {
44
- rawCpp(`Timer${this._instance}.stop();`);
39
+ hwtimerStop(this._instance);
45
40
  }
46
41
 
47
- /**
48
- * Returns the bit resolution of the timer (e.g., 8, 16, 32).
49
- */
42
+ /** Returns the bit resolution of the timer (e.g., 8, 16, 32). */
50
43
  getBits(): number {
51
44
  // "peripherals.timer" (singular) matches the board-resolver's array-key
52
45
  // derivation (TIMER_INSTANCES → peripherals.timer.<index>.*).
@@ -57,3 +50,9 @@ export class HardwareTimer {
57
50
  export const Timer0 = new HardwareTimer(0);
58
51
  export const Timer1 = new HardwareTimer(1);
59
52
  export const Timer2 = new HardwareTimer(2);
53
+
54
+ // ── Semantic primitives (resolved to hwtimer.* HAL ops by the transpiler) ──
55
+ export function hwtimerSetFrequency(instance: number, hz: number): void {}
56
+ export function hwtimerOnOverflow(instance: number, handler: string): void {}
57
+ export function hwtimerStart(instance: number): void {}
58
+ export function hwtimerStop(instance: number): void {}
package/src/types.ts CHANGED
@@ -123,6 +123,7 @@ export type ArchitectureIdentifier =
123
123
  | 'esp32c3'
124
124
  | 'esp32c6'
125
125
  | 'rp2040'
126
+ | 'rp2350'
126
127
  | 'samd'
127
128
  | 'stm32'
128
129
  | 'nrf52'
package/src/wifi.ts ADDED
@@ -0,0 +1,221 @@
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
+ startAP(ssid: string, password?: string): boolean {
161
+ return wifiApStart(ssid, password);
162
+ }
163
+
164
+ apChannel(ch: number): this {
165
+ wifiApSetChannel(ch);
166
+ return this;
167
+ }
168
+
169
+ apHidden(hidden: boolean): this {
170
+ wifiApSetHidden(hidden);
171
+ return this;
172
+ }
173
+
174
+ apMaxClients(n: number): this {
175
+ wifiApSetMaxClients(n);
176
+ return this;
177
+ }
178
+
179
+ stopAP(): void {
180
+ wifiApStop();
181
+ }
182
+
183
+ apClientCount(): number {
184
+ return wifiApClientCount();
185
+ }
186
+
187
+ apIP(): string {
188
+ return wifiApIp();
189
+ }
190
+
191
+ scan(): number {
192
+ return wifiScan();
193
+ }
194
+
195
+ scanAsync(): Promise<void> {
196
+ wifiScanStart();
197
+ return Promise.resolve();
198
+ }
199
+
200
+ scanCount(): number {
201
+ return wifiScanCount();
202
+ }
203
+
204
+ scanSSID(i: number): string {
205
+ return wifiScanSsid(i);
206
+ }
207
+
208
+ scanRSSI(i: number): number {
209
+ return wifiScanRssi(i);
210
+ }
211
+
212
+ scanEncryption(i: number): WiFiEncryption {
213
+ return wifiScanEncryption(i) as WiFiEncryption;
214
+ }
215
+
216
+ scanChannel(i: number): number {
217
+ return wifiScanChannel(i);
218
+ }
219
+ }
220
+
221
+ export const WiFi = new WiFiClass();