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

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/http.ts ADDED
@@ -0,0 +1,144 @@
1
+ import {
2
+ httpBegin,
3
+ httpReset,
4
+ httpSetHeader,
5
+ httpSetTimeout,
6
+ httpSetMaxBody,
7
+ httpSetBody,
8
+ httpSetInsecure,
9
+ httpSetCaCert,
10
+ httpSend,
11
+ httpStatus,
12
+ httpOk,
13
+ httpBody,
14
+ httpContentLength,
15
+ httpResponseHeader,
16
+ } from './emit.js';
17
+
18
+ export enum HttpMethod {
19
+ GET = 0,
20
+ POST = 1,
21
+ PUT = 2,
22
+ DELETE = 3,
23
+ HEAD = 4,
24
+ PATCH = 5,
25
+ }
26
+
27
+ /**
28
+ * Fluent HTTP/S request builder, lowered to native ESP-IDF
29
+ * `esp_http_client` by framework-esp32 (TLS via esp-tls / mbedTLS bundle).
30
+ * Response fields are read from this object after send() — mirrors
31
+ * `await WiFi.connect(); WiFi.localIP()`.
32
+ *
33
+ * No `include()` calls here — ESP-IDF headers are framework-owned and added
34
+ * via forcedIncludes when the program uses http.* ops.
35
+ */
36
+ export class HttpRequest {
37
+ private _method: string;
38
+ private _url: string;
39
+
40
+ constructor(method: string, url: string) {
41
+ this._method = method;
42
+ this._url = url;
43
+ }
44
+
45
+ header(name: string, value: string): this {
46
+ httpSetHeader(name, value);
47
+ return this;
48
+ }
49
+
50
+ timeout(ms: number): this {
51
+ httpSetTimeout(ms);
52
+ return this;
53
+ }
54
+
55
+ maxBody(bytes: number): this {
56
+ httpSetMaxBody(bytes);
57
+ return this;
58
+ }
59
+
60
+ body(data: string): this {
61
+ httpSetBody(data, false);
62
+ return this;
63
+ }
64
+
65
+ jsonBody(json: string): this {
66
+ httpSetBody(json, true);
67
+ return this;
68
+ }
69
+
70
+ /** Skip TLS certificate verification (development only). */
71
+ insecure(): this {
72
+ httpSetInsecure();
73
+ return this;
74
+ }
75
+
76
+ caCert(pem: string): this {
77
+ httpSetCaCert(pem);
78
+ return this;
79
+ }
80
+
81
+ /** Blocking at top level; cooperatively awaitable inside async functions. */
82
+ send(): Promise<boolean> {
83
+ httpBegin(this._method, this._url);
84
+ httpSend();
85
+ return Promise.resolve(false);
86
+ }
87
+
88
+ status(): number {
89
+ return httpStatus();
90
+ }
91
+
92
+ ok(): boolean {
93
+ return httpOk();
94
+ }
95
+
96
+ /** Response body as a C string (valid until the next request). */
97
+ text(): string {
98
+ return httpBody();
99
+ }
100
+
101
+ contentLength(): number {
102
+ return httpContentLength();
103
+ }
104
+
105
+ responseHeader(name: string): string {
106
+ return httpResponseHeader(name);
107
+ }
108
+ }
109
+
110
+ export class HttpClass {
111
+ static readonly __instance_name = "Http";
112
+
113
+ get(url: string): HttpRequest {
114
+ httpReset();
115
+ return new HttpRequest("GET", url);
116
+ }
117
+
118
+ post(url: string): HttpRequest {
119
+ httpReset();
120
+ return new HttpRequest("POST", url);
121
+ }
122
+
123
+ put(url: string): HttpRequest {
124
+ httpReset();
125
+ return new HttpRequest("PUT", url);
126
+ }
127
+
128
+ del(url: string): HttpRequest {
129
+ httpReset();
130
+ return new HttpRequest("DELETE", url);
131
+ }
132
+
133
+ head(url: string): HttpRequest {
134
+ httpReset();
135
+ return new HttpRequest("HEAD", url);
136
+ }
137
+
138
+ patch(url: string): HttpRequest {
139
+ httpReset();
140
+ return new HttpRequest("PATCH", url);
141
+ }
142
+ }
143
+
144
+ export const Http = new HttpClass();
package/src/index.ts CHANGED
@@ -19,7 +19,7 @@ export type { SPIBitOrder, SPIMode, SPISettings } from './types.js';
19
19
  export { include } from './include.js';
20
20
  export { board } from './board.js';
21
21
  export { callback } from './callback.js';
22
- export { rawCpp, boardResolve } from './emit.js';
22
+ export { rawCpp, rawCppExpr, boardResolve } from './emit.js';
23
23
  export { HIGH, LOW, OUTPUT, INPUT, INPUT_PULLUP, INPUT_PULLDOWN, OUTPUT_OPEN_DRAIN, ANALOG, LED_BUILTIN, LSBFIRST, MSBFIRST, WDTO_15MS, WDTO_30MS, WDTO_60MS, WDTO_120MS, WDTO_250MS, WDTO_500MS, WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S } from './constants.js';
24
24
  export { delay, millis, micros, delayMicroseconds, map, constrain, TimingClass, Timing } from './timing.js';
25
25
  export { freeHeap, setInterval, setTimeout, clearInterval, clearTimeout } from './timing.js';
@@ -44,5 +44,25 @@ export { DACClass, DAC } from './dac.js';
44
44
  export { PreferencesClass, Preferences } from './preferences.js';
45
45
  export { HardwareTimer, Timer0, Timer1, Timer2 } from './timer.js';
46
46
  export { FSClass, FS } from './fs.js';
47
+ export { fsBegin, fsReadText, fsWriteText, fsExists, fsRemove } from './fs.js';
48
+ export { MdnsClass, MDNS } from './mdns.js';
49
+ export { mdnsStart, mdnsSetHostname, mdnsAddService, mdnsAnnounce, mdnsStop } from './mdns.js';
50
+ export { MqttClass, MQTT } from './mqtt.js';
51
+ export { mqttConnect, mqttOnMessage, mqttSubscribe, mqttPublish, mqttConnected, mqttDisconnect } from './mqtt.js';
52
+ export { OtaClass, OTA } from './ota.js';
53
+ export { otaFromUrl, otaBegin, otaWrite, otaApply } from './ota.js';
54
+ export { TemperatureClass, Temperature } from './temperature.js';
55
+ export { tempRead } from './temperature.js';
56
+ export { hwtimerSetFrequency, hwtimerOnOverflow, hwtimerStart, hwtimerStop } from './timer.js';
57
+ export { CapacitiveClass, Capacitive } from './capacitive.js';
58
+ export { capacitiveRead } from './capacitive.js';
47
59
  export { PowerClass, Power } from './power.js';
48
60
  export { AsyncClass, Async } from './async.js';
61
+ export { WiFiClass, WiFi, WiFiStatus, WiFiEncryption } from './wifi.js';
62
+ export { HttpClass, Http, HttpRequest, HttpMethod } from './http.js';
63
+ export {
64
+ BleClass, Ble, BleServer,
65
+ BleValueType, BlePerm, BleStatus, BleAdvertisingMode, GATT,
66
+ } from './ble.js';
67
+ export type { GattCharacteristicDef, CharValue } from './ble.js';
68
+ export { RmtChannel } from './rmt.js';
package/src/mdns.ts ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * MdnsClass — mDNS service discovery (ESP-IDF esp_mdns).
3
+ *
4
+ * Lowered to native mDNS HAL ops (mdns.*): ESP-IDF's esp_mdns component
5
+ * advertises the device on the local network as `<hostname>.local` and
6
+ * publishes services (e.g. `_http._tcp`) for discovery by Bonjour/Avahi.
7
+ *
8
+ * The semantic primitives (mdnsStart / mdnsAddService / ...) are resolved to
9
+ * mdns.* HAL ops by the transpiler; this class is the ergonomic surface.
10
+ *
11
+ * Requires WiFi to be connected (mDNS rides on the station interface).
12
+ */
13
+ export class MdnsClass {
14
+ static readonly __instance_name = "MDNS";
15
+
16
+ /** Initialize mDNS and set the host name (advertised as <name>.local). */
17
+ start(hostname: string): boolean {
18
+ mdnsStart(hostname);
19
+ return true;
20
+ }
21
+
22
+ /** Set/override the host name after start(). */
23
+ setHostname(name: string): void {
24
+ mdnsSetHostname(name);
25
+ }
26
+
27
+ /** Publish a service instance. proto is "_tcp" or "_udp". */
28
+ addService(instance: string, proto: string, port: number): void {
29
+ mdnsAddService(instance, proto, port);
30
+ }
31
+
32
+ /** Advertise that the device is reachable (sends a probe/announce). */
33
+ announce(): void {
34
+ mdnsAnnounce();
35
+ }
36
+
37
+ /** Tear down the mDNS responder. */
38
+ stop(): void {
39
+ mdnsStop();
40
+ }
41
+ }
42
+
43
+ export const MDNS = new MdnsClass();
44
+
45
+ // ── Semantic primitives (resolved to mdns.* HAL ops by the transpiler) ──
46
+ export function mdnsStart(hostname: string): void {}
47
+ export function mdnsSetHostname(name: string): void {}
48
+ export function mdnsAddService(instance: string, proto: string, port: number): void {}
49
+ export function mdnsAnnounce(): void {}
50
+ export function mdnsStop(): void {}
package/src/mqtt.ts ADDED
@@ -0,0 +1,57 @@
1
+ import { callback } from './callback.js';
2
+
3
+ /**
4
+ * MqttClass — MQTT 3.1.1 pub/sub client (ESP-IDF esp_mqtt).
5
+ *
6
+ * Lowered to native MQTT HAL ops (mqtt.*): ESP-IDF's esp_mqtt_client_* API.
7
+ * Covers the common IoT pub/sub path: connect to a broker, publish, subscribe
8
+ * with an onMessage callback, and disconnect. The runtime shim owns the event
9
+ * loop translation (ESP-IDF's MQTT event handler → the user's TS callback).
10
+ *
11
+ * Requires a network connection (WiFi) before connect().
12
+ */
13
+ export class MqttClass {
14
+ static readonly __instance_name = "MQTT";
15
+
16
+ /** Connect to a broker URI (e.g. "mqtt://broker.local" or "mqtts://..."). */
17
+ connect(brokerUri: string, clientId: string): boolean {
18
+ mqttConnect(brokerUri, clientId);
19
+ return true;
20
+ }
21
+
22
+ /** Set a handler invoked for every received PUBLISH on a subscribed topic.
23
+ * The handler receives (topic, payload). */
24
+ onMessage(handler: (topic: string, payload: string) => void): void {
25
+ mqttOnMessage(callback(handler));
26
+ }
27
+
28
+ /** Subscribe to a topic filter (e.g. "sensors/#"). */
29
+ subscribe(topic: string): void {
30
+ mqttSubscribe(topic);
31
+ }
32
+
33
+ /** Publish a message to a topic. */
34
+ publish(topic: string, data: string): void {
35
+ mqttPublish(topic, data);
36
+ }
37
+
38
+ /** True if the client is currently connected to the broker. */
39
+ connected(): boolean {
40
+ return mqttConnected();
41
+ }
42
+
43
+ /** Disconnect from the broker and free the client. */
44
+ disconnect(): void {
45
+ mqttDisconnect();
46
+ }
47
+ }
48
+
49
+ export const MQTT = new MqttClass();
50
+
51
+ // ── Semantic primitives (resolved to mqtt.* HAL ops by the transpiler) ──
52
+ export function mqttConnect(brokerUri: string, clientId: string): void {}
53
+ export function mqttOnMessage(handler: string): void {}
54
+ export function mqttSubscribe(topic: string): void {}
55
+ export function mqttPublish(topic: string, data: string): void {}
56
+ export function mqttConnected(): boolean { return false; }
57
+ export function mqttDisconnect(): void {}
package/src/ota.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * OtaClass — over-the-air firmware update (ESP-IDF esp_https_ota / esp_app_format).
3
+ *
4
+ * Lowered to native OTA HAL ops (ota.*): ESP-IDF's esp_https_ota downloads a
5
+ * firmware image over HTTPS and writes it to the OTA partition, then reboots
6
+ * into it. The runtime shim owns the session/progress/verification dance.
7
+ *
8
+ * Requires a network connection (WiFi) and that the partition table defines
9
+ * at least two OTA app slots (the framework's default partitions.csv does).
10
+ */
11
+ export class OtaClass {
12
+ static readonly __instance_name = "OTA";
13
+
14
+ /** Download and apply a firmware image from an HTTPS URL, then reboot.
15
+ * Blocks until the update is written and verified. Returns true on success
16
+ * (the reboot happens inside the shim on success, so a true return means
17
+ * the new firmware is about to boot). */
18
+ fromUrl(url: string): boolean {
19
+ return otaFromUrl(url);
20
+ }
21
+
22
+ /** Begin a manual update session (caller writes chunks via write()). */
23
+ begin(): boolean {
24
+ return otaBegin();
25
+ }
26
+
27
+ /** Write a chunk of firmware data to the OTA partition. */
28
+ write(chunk: string): void {
29
+ otaWrite(chunk);
30
+ }
31
+
32
+ /** Finalize the update: verify, set the boot partition, and reboot. */
33
+ apply(): void {
34
+ otaApply();
35
+ }
36
+ }
37
+
38
+ export const OTA = new OtaClass();
39
+
40
+ // ── Semantic primitives (resolved to ota.* HAL ops by the transpiler) ──
41
+ export function otaFromUrl(url: string): boolean { return false; }
42
+ export function otaBegin(): boolean { return false; }
43
+ export function otaWrite(chunk: string): void {}
44
+ export function otaApply(): void {}
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/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
+ }
@@ -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 {}