@typecad/hal 0.1.0-alpha.1
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/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/adc.d.ts +13 -0
- package/dist/adc.js +22 -0
- package/dist/async.d.ts +30 -0
- package/dist/async.js +56 -0
- package/dist/board.d.ts +3 -0
- package/dist/board.js +5 -0
- package/dist/callback.d.ts +4 -0
- package/dist/callback.js +6 -0
- package/dist/constants.d.ts +21 -0
- package/dist/constants.js +23 -0
- package/dist/dac.d.ts +12 -0
- package/dist/dac.js +17 -0
- package/dist/eeprom.d.ts +16 -0
- package/dist/eeprom.js +21 -0
- package/dist/emit.d.ts +121 -0
- package/dist/emit.js +177 -0
- package/dist/fs.d.ts +13 -0
- package/dist/fs.js +39 -0
- package/dist/gpio.d.ts +108 -0
- package/dist/gpio.js +230 -0
- package/dist/i2c.d.ts +40 -0
- package/dist/i2c.js +126 -0
- package/dist/include.d.ts +5 -0
- package/dist/include.js +5 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +30 -0
- package/dist/interrupts.d.ts +17 -0
- package/dist/interrupts.js +25 -0
- package/dist/math.d.ts +22 -0
- package/dist/math.js +28 -0
- package/dist/power.d.ts +14 -0
- package/dist/power.js +21 -0
- package/dist/preferences.d.ts +18 -0
- package/dist/preferences.js +55 -0
- package/dist/pulse.d.ts +24 -0
- package/dist/pulse.js +53 -0
- package/dist/random.d.ts +12 -0
- package/dist/random.js +26 -0
- package/dist/register.d.ts +11 -0
- package/dist/register.js +21 -0
- package/dist/shift.d.ts +31 -0
- package/dist/shift.js +71 -0
- package/dist/spi.d.ts +31 -0
- package/dist/spi.js +90 -0
- package/dist/timer.d.ts +35 -0
- package/dist/timer.js +50 -0
- package/dist/timing.d.ts +29 -0
- package/dist/timing.js +53 -0
- package/dist/types.d.ts +48 -0
- package/dist/types.js +67 -0
- package/dist/uart.d.ts +21 -0
- package/dist/uart.js +58 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +14 -0
- package/dist/wdt.d.ts +7 -0
- package/dist/wdt.js +14 -0
- package/package.json +64 -0
- package/src/adc.ts +30 -0
- package/src/async.ts +63 -0
- package/src/board.ts +5 -0
- package/src/callback.ts +6 -0
- package/src/constants.ts +23 -0
- package/src/dac.ts +21 -0
- package/src/eeprom.ts +26 -0
- package/src/emit.ts +210 -0
- package/src/fs.ts +46 -0
- package/src/gpio.ts +298 -0
- package/src/i2c.ts +155 -0
- package/src/include.ts +5 -0
- package/src/index.ts +48 -0
- package/src/interrupts.ts +35 -0
- package/src/math.ts +32 -0
- package/src/power.ts +26 -0
- package/src/preferences.ts +58 -0
- package/src/pulse.ts +63 -0
- package/src/random.ts +31 -0
- package/src/register.ts +35 -0
- package/src/shift.ts +88 -0
- package/src/spi.ts +118 -0
- package/src/timer.ts +59 -0
- package/src/timing.ts +67 -0
- package/src/types.ts +144 -0
- package/src/uart.ts +77 -0
- package/src/utils.ts +17 -0
- package/src/wdt.ts +17 -0
package/dist/i2c.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { i2cBegin, i2cEnd, i2cSetClock, i2cBeginTx, i2cWrite, i2cWriteBuffer, i2cEndTx, i2cRequestFrom, i2cAvailable, i2cRead, rawCpp } from './emit.js';
|
|
2
|
+
import { include } from './include.js';
|
|
3
|
+
export class I2CDevice {
|
|
4
|
+
constructor(bus, address) {
|
|
5
|
+
this._bus = bus;
|
|
6
|
+
this._address = address;
|
|
7
|
+
}
|
|
8
|
+
writeByte(register, value) {
|
|
9
|
+
include("<Wire.h>");
|
|
10
|
+
i2cBeginTx(this._bus, this._address);
|
|
11
|
+
i2cWrite(this._bus, register);
|
|
12
|
+
i2cWrite(this._bus, value);
|
|
13
|
+
i2cEndTx(this._bus, true);
|
|
14
|
+
}
|
|
15
|
+
readByte(register) {
|
|
16
|
+
include("<Wire.h>");
|
|
17
|
+
i2cBeginTx(this._bus, this._address);
|
|
18
|
+
i2cWrite(this._bus, register);
|
|
19
|
+
i2cEndTx(this._bus, false);
|
|
20
|
+
i2cRequestFrom(this._bus, this._address, 1);
|
|
21
|
+
return i2cRead(this._bus);
|
|
22
|
+
}
|
|
23
|
+
writeBytes(register, data) {
|
|
24
|
+
include("<Wire.h>");
|
|
25
|
+
i2cBeginTx(this._bus, this._address);
|
|
26
|
+
i2cWrite(this._bus, register);
|
|
27
|
+
i2cWriteBuffer(this._bus, data);
|
|
28
|
+
i2cEndTx(this._bus, true);
|
|
29
|
+
}
|
|
30
|
+
readBytes(register, count) {
|
|
31
|
+
include("<Wire.h>");
|
|
32
|
+
i2cBeginTx(this._bus, this._address);
|
|
33
|
+
i2cWrite(this._bus, register);
|
|
34
|
+
i2cEndTx(this._bus, false);
|
|
35
|
+
i2cRequestFrom(this._bus, this._address, count, true);
|
|
36
|
+
rawCpp(`static uint8_t __buf[${count}];`);
|
|
37
|
+
rawCpp(`for (int __i = 0; __i < ${count}; __i++) __buf[__i] = ${this._bus}.read();`);
|
|
38
|
+
rawCpp(`return __buf;`);
|
|
39
|
+
return new Uint8Array(count);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class I2CBus {
|
|
43
|
+
constructor(bus) {
|
|
44
|
+
this._bus = bus;
|
|
45
|
+
}
|
|
46
|
+
device(address) {
|
|
47
|
+
return new I2CDevice(this._bus, address);
|
|
48
|
+
}
|
|
49
|
+
begin() {
|
|
50
|
+
include("<Wire.h>");
|
|
51
|
+
i2cBegin(this._bus);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
beginSlave(address) {
|
|
55
|
+
i2cBegin(this._bus, address);
|
|
56
|
+
}
|
|
57
|
+
end() {
|
|
58
|
+
i2cEnd(this._bus);
|
|
59
|
+
}
|
|
60
|
+
setClock(hz) {
|
|
61
|
+
i2cSetClock(this._bus, hz);
|
|
62
|
+
}
|
|
63
|
+
/** Recover a locked I2C bus by clocking SCL until the stuck slave releases SDA.
|
|
64
|
+
*
|
|
65
|
+
* NOTE: This references the board-defined `SCL` pin macro. All current MCU
|
|
66
|
+
* packages (atmega328p, esp32, esp32c3, esp32c6, esp32s3) export SCL, so
|
|
67
|
+
* this works in practice. A fully portable version would resolve the SCL
|
|
68
|
+
* pin number via a board constant (e.g. peripherals.i2c.0.sclPin), but that
|
|
69
|
+
* requires adding resolved numeric pin fields to the board-constants
|
|
70
|
+
* pipeline — deferred for now. */
|
|
71
|
+
recover() {
|
|
72
|
+
include("<Wire.h>");
|
|
73
|
+
rawCpp(`pinMode(SCL, OUTPUT);`);
|
|
74
|
+
rawCpp(`for (int i = 0; i < 16; i++) {`);
|
|
75
|
+
rawCpp(` digitalWrite(SCL, LOW);`);
|
|
76
|
+
rawCpp(` delayMicroseconds(10);`);
|
|
77
|
+
rawCpp(` digitalWrite(SCL, HIGH);`);
|
|
78
|
+
rawCpp(` delayMicroseconds(10);`);
|
|
79
|
+
rawCpp(`}`);
|
|
80
|
+
rawCpp(`${this._bus}.begin();`);
|
|
81
|
+
}
|
|
82
|
+
take() {
|
|
83
|
+
include("<Wire.h>");
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
release() {
|
|
87
|
+
// No-op for standard Arduino.
|
|
88
|
+
}
|
|
89
|
+
writeByte(address, register, value) {
|
|
90
|
+
i2cBeginTx(this._bus, address);
|
|
91
|
+
i2cWrite(this._bus, register);
|
|
92
|
+
i2cWrite(this._bus, value);
|
|
93
|
+
i2cEndTx(this._bus, true);
|
|
94
|
+
}
|
|
95
|
+
readByte(address, register) {
|
|
96
|
+
i2cBeginTx(this._bus, address);
|
|
97
|
+
i2cWrite(this._bus, register);
|
|
98
|
+
i2cEndTx(this._bus, false);
|
|
99
|
+
i2cRequestFrom(this._bus, address, 1);
|
|
100
|
+
return i2cRead(this._bus);
|
|
101
|
+
}
|
|
102
|
+
// Low-level Wire API pass-through methods
|
|
103
|
+
beginTransmission(address) {
|
|
104
|
+
i2cBeginTx(this._bus, address);
|
|
105
|
+
}
|
|
106
|
+
write(data) {
|
|
107
|
+
i2cWrite(this._bus, data);
|
|
108
|
+
}
|
|
109
|
+
endTransmission(stop) {
|
|
110
|
+
return i2cEndTx(this._bus, stop ?? true);
|
|
111
|
+
}
|
|
112
|
+
requestFrom(address, quantity, stop) {
|
|
113
|
+
return i2cRequestFrom(this._bus, address, quantity, stop ?? true);
|
|
114
|
+
}
|
|
115
|
+
available() {
|
|
116
|
+
return i2cAvailable(this._bus);
|
|
117
|
+
}
|
|
118
|
+
read() {
|
|
119
|
+
return i2cRead(this._bus);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
I2CBus.__includes = ["<Wire.h>"];
|
|
123
|
+
/** Map TypeCAD I2C instance number to Arduino C++ object name. I2C0→Wire, I2C1→Wire1 */
|
|
124
|
+
export function i2cName(instance) {
|
|
125
|
+
return instance === 0 ? "Wire" : `Wire${instance}`;
|
|
126
|
+
}
|
package/dist/include.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type { DigitalValue, AnalogValue } from './types.js';
|
|
2
|
+
export { PinMode, InterruptMode } from './types.js';
|
|
3
|
+
export type { IPinGroup, PinGroupMember } from './types.js';
|
|
4
|
+
export { createPinGroup } from './types.js';
|
|
5
|
+
export type { InterruptHandler } from './types.js';
|
|
6
|
+
export type { ArchitectureIdentifier } from './types.js';
|
|
7
|
+
export type { I2CAddress } from './types.js';
|
|
8
|
+
export type { SPIBitOrder, SPIMode, SPISettings } from './types.js';
|
|
9
|
+
export { include } from './include.js';
|
|
10
|
+
export { board } from './board.js';
|
|
11
|
+
export { callback } from './callback.js';
|
|
12
|
+
export { rawCpp, boardResolve } from './emit.js';
|
|
13
|
+
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';
|
|
14
|
+
export { delay, millis, micros, delayMicroseconds, map, constrain, TimingClass, Timing } from './timing.js';
|
|
15
|
+
export { freeHeap, setInterval, setTimeout, clearInterval, clearTimeout } from './timing.js';
|
|
16
|
+
export { abs, min, max, NumClass, Num, MapChain, ConstrainChain } from './math.js';
|
|
17
|
+
export { Pulse, pulseIn, pulseInLong } from './pulse.js';
|
|
18
|
+
export { Shift, shiftIn, shiftOut } from './shift.js';
|
|
19
|
+
export { Random } from './random.js';
|
|
20
|
+
export { randomSeed, random } from './random.js';
|
|
21
|
+
export { noInterrupts, interrupts, attachInterrupt, detachInterrupt } from './interrupts.js';
|
|
22
|
+
export { Pin, OutputPin, InputPin, ToneChain } from './gpio.js';
|
|
23
|
+
export { I2CBus, I2CDevice, i2cName } from './i2c.js';
|
|
24
|
+
export { SPIBus, SPIDevice, spiName } from './spi.js';
|
|
25
|
+
export { SerialPort, serialName } from './uart.js';
|
|
26
|
+
export { createHALInstances } from './utils.js';
|
|
27
|
+
export type { Bit, Bits } from './register.js';
|
|
28
|
+
export { register, bits } from './register.js';
|
|
29
|
+
export { EEPROMClass, EEPROM } from './eeprom.js';
|
|
30
|
+
export { WDTClass, WDT } from './wdt.js';
|
|
31
|
+
export { ADCClass, ADC } from './adc.js';
|
|
32
|
+
export { DACClass, DAC } from './dac.js';
|
|
33
|
+
export { PreferencesClass, Preferences } from './preferences.js';
|
|
34
|
+
export { HardwareTimer, Timer0, Timer1, Timer2 } from './timer.js';
|
|
35
|
+
export { FSClass, FS } from './fs.js';
|
|
36
|
+
export { PowerClass, Power } from './power.js';
|
|
37
|
+
export { AsyncClass, Async } from './async.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export { PinMode, InterruptMode } from './types.js';
|
|
2
|
+
export { createPinGroup } from './types.js';
|
|
3
|
+
export { include } from './include.js';
|
|
4
|
+
export { board } from './board.js';
|
|
5
|
+
export { callback } from './callback.js';
|
|
6
|
+
export { rawCpp, boardResolve } from './emit.js';
|
|
7
|
+
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';
|
|
8
|
+
export { delay, millis, micros, delayMicroseconds, map, constrain, TimingClass, Timing } from './timing.js';
|
|
9
|
+
export { freeHeap, setInterval, setTimeout, clearInterval, clearTimeout } from './timing.js';
|
|
10
|
+
export { abs, min, max, NumClass, Num, MapChain, ConstrainChain } from './math.js';
|
|
11
|
+
export { Pulse, pulseIn, pulseInLong } from './pulse.js';
|
|
12
|
+
export { Shift, shiftIn, shiftOut } from './shift.js';
|
|
13
|
+
export { Random } from './random.js';
|
|
14
|
+
export { randomSeed, random } from './random.js';
|
|
15
|
+
export { noInterrupts, interrupts, attachInterrupt, detachInterrupt } from './interrupts.js';
|
|
16
|
+
export { Pin, OutputPin, InputPin, ToneChain } from './gpio.js';
|
|
17
|
+
export { I2CBus, I2CDevice, i2cName } from './i2c.js';
|
|
18
|
+
export { SPIBus, SPIDevice, spiName } from './spi.js';
|
|
19
|
+
export { SerialPort, serialName } from './uart.js';
|
|
20
|
+
export { createHALInstances } from './utils.js';
|
|
21
|
+
export { register, bits } from './register.js';
|
|
22
|
+
export { EEPROMClass, EEPROM } from './eeprom.js';
|
|
23
|
+
export { WDTClass, WDT } from './wdt.js';
|
|
24
|
+
export { ADCClass, ADC } from './adc.js';
|
|
25
|
+
export { DACClass, DAC } from './dac.js';
|
|
26
|
+
export { PreferencesClass, Preferences } from './preferences.js';
|
|
27
|
+
export { HardwareTimer, Timer0, Timer1, Timer2 } from './timer.js';
|
|
28
|
+
export { FSClass, FS } from './fs.js';
|
|
29
|
+
export { PowerClass, Power } from './power.js';
|
|
30
|
+
export { AsyncClass, Async } from './async.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { InterruptHandler, InterruptMode } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Globally disable interrupts.
|
|
4
|
+
*/
|
|
5
|
+
export declare function noInterrupts(): void;
|
|
6
|
+
/**
|
|
7
|
+
* Re-enable interrupts after `noInterrupts()`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function interrupts(): void;
|
|
10
|
+
/**
|
|
11
|
+
* Attach an interrupt handler to a pin.
|
|
12
|
+
*/
|
|
13
|
+
export declare function attachInterrupt(pin: number, handler: InterruptHandler, mode: InterruptMode): void;
|
|
14
|
+
/**
|
|
15
|
+
* Detach a previously attached interrupt.
|
|
16
|
+
*/
|
|
17
|
+
export declare function detachInterrupt(pin: number): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/hal — Interrupt helpers
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
import { interruptAttach, interruptDetach } from './emit.js';
|
|
5
|
+
import { callback } from './callback.js';
|
|
6
|
+
/**
|
|
7
|
+
* Globally disable interrupts.
|
|
8
|
+
*/
|
|
9
|
+
export function noInterrupts() { }
|
|
10
|
+
/**
|
|
11
|
+
* Re-enable interrupts after `noInterrupts()`.
|
|
12
|
+
*/
|
|
13
|
+
export function interrupts() { }
|
|
14
|
+
/**
|
|
15
|
+
* Attach an interrupt handler to a pin.
|
|
16
|
+
*/
|
|
17
|
+
export function attachInterrupt(pin, handler, mode) {
|
|
18
|
+
interruptAttach(pin, callback(handler), mode);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Detach a previously attached interrupt.
|
|
22
|
+
*/
|
|
23
|
+
export function detachInterrupt(pin) {
|
|
24
|
+
interruptDetach(pin);
|
|
25
|
+
}
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare class MapChain {
|
|
2
|
+
constructor(value: number);
|
|
3
|
+
from(low: number, high: number): MapChain;
|
|
4
|
+
to(low: number, high: number): number;
|
|
5
|
+
toPercent(): number;
|
|
6
|
+
toByte(): number;
|
|
7
|
+
}
|
|
8
|
+
export declare class ConstrainChain {
|
|
9
|
+
constructor(value: number);
|
|
10
|
+
between(low: number, high: number): number;
|
|
11
|
+
}
|
|
12
|
+
export declare class NumClass {
|
|
13
|
+
abs(x: number): number;
|
|
14
|
+
min(a: number, b: number): number;
|
|
15
|
+
max(a: number, b: number): number;
|
|
16
|
+
constrain(value: number): ConstrainChain;
|
|
17
|
+
map(value: number): MapChain;
|
|
18
|
+
}
|
|
19
|
+
export declare const Num: NumClass;
|
|
20
|
+
export declare function abs(x: number): number;
|
|
21
|
+
export declare function min(a: number, b: number): number;
|
|
22
|
+
export declare function max(a: number, b: number): number;
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class MapChain {
|
|
2
|
+
constructor(value) { }
|
|
3
|
+
from(low, high) { return this; }
|
|
4
|
+
to(low, high) { return 0; }
|
|
5
|
+
toPercent() { return 0; }
|
|
6
|
+
toByte() { return 0; }
|
|
7
|
+
}
|
|
8
|
+
export class ConstrainChain {
|
|
9
|
+
constructor(value) { }
|
|
10
|
+
between(low, high) { return 0; }
|
|
11
|
+
}
|
|
12
|
+
export class NumClass {
|
|
13
|
+
// No __instance_name — Num methods must NOT be intercepted by the HAL
|
|
14
|
+
// resolver. They pass through as bare C++ calls (Num.abs(-7)), which the
|
|
15
|
+
// strategy's __tc_Num polyfill struct provides. The strategy post-processes
|
|
16
|
+
// the output to rename Num.abs( → Num._abs( to match the polyfill's
|
|
17
|
+
// parenthesized method names (which avoid macro collisions with Arduino's
|
|
18
|
+
// abs/min/max macros). See strategy.ts __tc_Num struct + the rewrite regex.
|
|
19
|
+
abs(x) { return 0; }
|
|
20
|
+
min(a, b) { return 0; }
|
|
21
|
+
max(a, b) { return 0; }
|
|
22
|
+
constrain(value) { return new ConstrainChain(value); }
|
|
23
|
+
map(value) { return new MapChain(value); }
|
|
24
|
+
}
|
|
25
|
+
export const Num = new NumClass();
|
|
26
|
+
export function abs(x) { return 0; }
|
|
27
|
+
export function min(a, b) { return 0; }
|
|
28
|
+
export function max(a, b) { return 0; }
|
package/dist/power.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PowerClass provides control over MCU power states and clock frequencies.
|
|
3
|
+
*
|
|
4
|
+
* The arch guard (ESP32 family vs AVR) lives in the strategy, not here — it
|
|
5
|
+
* relies on the FQBN-derived architecture which is always available at
|
|
6
|
+
* transpile time, unlike `board("architecture")` which needs a board package.
|
|
7
|
+
*/
|
|
8
|
+
export declare class PowerClass {
|
|
9
|
+
static readonly __instance_name = "Power";
|
|
10
|
+
deepSleep(ms: number): void;
|
|
11
|
+
lightSleep(): void;
|
|
12
|
+
setCpuFrequency(mhz: number): void;
|
|
13
|
+
}
|
|
14
|
+
export declare const Power: PowerClass;
|
package/dist/power.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { powerDeepSleep, powerLightSleep, powerSetCpuFrequency } from './emit.js';
|
|
2
|
+
/**
|
|
3
|
+
* PowerClass provides control over MCU power states and clock frequencies.
|
|
4
|
+
*
|
|
5
|
+
* The arch guard (ESP32 family vs AVR) lives in the strategy, not here — it
|
|
6
|
+
* relies on the FQBN-derived architecture which is always available at
|
|
7
|
+
* transpile time, unlike `board("architecture")` which needs a board package.
|
|
8
|
+
*/
|
|
9
|
+
export class PowerClass {
|
|
10
|
+
deepSleep(ms) {
|
|
11
|
+
powerDeepSleep(ms);
|
|
12
|
+
}
|
|
13
|
+
lightSleep() {
|
|
14
|
+
powerLightSleep();
|
|
15
|
+
}
|
|
16
|
+
setCpuFrequency(mhz) {
|
|
17
|
+
powerSetCpuFrequency(mhz);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
PowerClass.__instance_name = "Power";
|
|
21
|
+
export const Power = new PowerClass();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class PreferencesClass {
|
|
2
|
+
static readonly __instance_name = "Preferences";
|
|
3
|
+
begin(name: string, readOnly?: boolean): void;
|
|
4
|
+
end(): void;
|
|
5
|
+
clear(): void;
|
|
6
|
+
remove(key: string): void;
|
|
7
|
+
putInt(key: string, value: number): void;
|
|
8
|
+
getInt(key: string, defaultValue?: number): number;
|
|
9
|
+
putUInt(key: string, value: number): void;
|
|
10
|
+
getUInt(key: string, defaultValue?: number): number;
|
|
11
|
+
putFloat(key: string, value: number): void;
|
|
12
|
+
getFloat(key: string, defaultValue?: number): number;
|
|
13
|
+
putBool(key: string, value: boolean): void;
|
|
14
|
+
getBool(key: string, defaultValue?: boolean): boolean;
|
|
15
|
+
putString(key: string, value: string): void;
|
|
16
|
+
getString(key: string, defaultValue?: string): string;
|
|
17
|
+
}
|
|
18
|
+
export declare const Preferences: PreferencesClass;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { rawCpp } from './emit.js';
|
|
2
|
+
export class PreferencesClass {
|
|
3
|
+
// NOTE: no __includes here. The transpiler emits singleton __includes on
|
|
4
|
+
// every architecture with no filtering, and <Preferences.h> is ESP32-only —
|
|
5
|
+
// adding it would break AVR builds. The class is ESP32-only by convention.
|
|
6
|
+
begin(name, readOnly = false) {
|
|
7
|
+
rawCpp(`Preferences.begin(${name}.c_str(), ${readOnly});`);
|
|
8
|
+
}
|
|
9
|
+
end() {
|
|
10
|
+
rawCpp(`Preferences.end();`);
|
|
11
|
+
}
|
|
12
|
+
clear() {
|
|
13
|
+
rawCpp(`Preferences.clear();`);
|
|
14
|
+
}
|
|
15
|
+
remove(key) {
|
|
16
|
+
rawCpp(`Preferences.remove(${key}.c_str());`);
|
|
17
|
+
}
|
|
18
|
+
putInt(key, value) {
|
|
19
|
+
rawCpp(`Preferences.putInt(${key}.c_str(), ${value});`);
|
|
20
|
+
}
|
|
21
|
+
getInt(key, defaultValue = 0) {
|
|
22
|
+
rawCpp(`return Preferences.getInt(${key}.c_str(), ${defaultValue});`);
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
putUInt(key, value) {
|
|
26
|
+
rawCpp(`Preferences.putUInt(${key}.c_str(), ${value});`);
|
|
27
|
+
}
|
|
28
|
+
getUInt(key, defaultValue = 0) {
|
|
29
|
+
rawCpp(`return Preferences.getUInt(${key}.c_str(), ${defaultValue});`);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
putFloat(key, value) {
|
|
33
|
+
rawCpp(`Preferences.putFloat(${key}.c_str(), ${value});`);
|
|
34
|
+
}
|
|
35
|
+
getFloat(key, defaultValue = 0) {
|
|
36
|
+
rawCpp(`return Preferences.getFloat(${key}.c_str(), ${defaultValue});`);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
putBool(key, value) {
|
|
40
|
+
rawCpp(`Preferences.putBool(${key}.c_str(), ${value});`);
|
|
41
|
+
}
|
|
42
|
+
getBool(key, defaultValue = false) {
|
|
43
|
+
rawCpp(`return Preferences.getBool(${key}.c_str(), ${defaultValue});`);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
putString(key, value) {
|
|
47
|
+
rawCpp(`Preferences.putString(${key}.c_str(), ${value}.c_str());`);
|
|
48
|
+
}
|
|
49
|
+
getString(key, defaultValue = "") {
|
|
50
|
+
rawCpp(`return Preferences.getString(${key}.c_str(), ${defaultValue}.c_str());`);
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
PreferencesClass.__instance_name = "Preferences";
|
|
55
|
+
export const Preferences = new PreferencesClass();
|
package/dist/pulse.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Pin, InputPin } from './gpio.js';
|
|
2
|
+
export declare function pulseIn(pin: number, value: number, timeout?: number): number;
|
|
3
|
+
export declare function pulseInLong(pin: number, value: number, timeout?: number): number;
|
|
4
|
+
export declare class Pulse {
|
|
5
|
+
/**
|
|
6
|
+
* Functional entrypoint for measuring pulses.
|
|
7
|
+
* @example Pulse.on(D7).high()
|
|
8
|
+
*/
|
|
9
|
+
static on(pin: Pin | InputPin): PulseMeasurement;
|
|
10
|
+
/** Measure long pulses using high-precision 64-bit timers. */
|
|
11
|
+
static long(pin: Pin | InputPin, value: number): number;
|
|
12
|
+
}
|
|
13
|
+
declare class PulseMeasurement {
|
|
14
|
+
private _pin;
|
|
15
|
+
private _timeout;
|
|
16
|
+
constructor(pin: Pin | InputPin);
|
|
17
|
+
/** Set the maximum wait time for a pulse (in microseconds). */
|
|
18
|
+
timeout(us: number): this;
|
|
19
|
+
/** Measures the next HIGH pulse duration in microseconds. */
|
|
20
|
+
high(): number;
|
|
21
|
+
/** Measures the next LOW pulse duration in microseconds. */
|
|
22
|
+
low(): number;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
package/dist/pulse.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { rawCpp } from './emit.js';
|
|
2
|
+
export function pulseIn(pin, value, timeout) {
|
|
3
|
+
if (timeout !== undefined) {
|
|
4
|
+
rawCpp(`return pulseIn(${pin}, ${value}, ${timeout});`);
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
rawCpp(`return pulseIn(${pin}, ${value});`);
|
|
8
|
+
}
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
export function pulseInLong(pin, value, timeout) {
|
|
12
|
+
if (timeout !== undefined) {
|
|
13
|
+
rawCpp(`return pulseInLong(${pin}, ${value}, ${timeout});`);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
rawCpp(`return pulseInLong(${pin}, ${value});`);
|
|
17
|
+
}
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
export class Pulse {
|
|
21
|
+
/**
|
|
22
|
+
* Functional entrypoint for measuring pulses.
|
|
23
|
+
* @example Pulse.on(D7).high()
|
|
24
|
+
*/
|
|
25
|
+
static on(pin) {
|
|
26
|
+
return new PulseMeasurement(pin);
|
|
27
|
+
}
|
|
28
|
+
/** Measure long pulses using high-precision 64-bit timers. */
|
|
29
|
+
static long(pin, value) {
|
|
30
|
+
rawCpp(`return pulseInLong(${pin.number}, ${value});`);
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class PulseMeasurement {
|
|
35
|
+
constructor(pin) {
|
|
36
|
+
this._pin = pin.number;
|
|
37
|
+
}
|
|
38
|
+
/** Set the maximum wait time for a pulse (in microseconds). */
|
|
39
|
+
timeout(us) {
|
|
40
|
+
this._timeout = us;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/** Measures the next HIGH pulse duration in microseconds. */
|
|
44
|
+
high() {
|
|
45
|
+
rawCpp(`return pulseIn(${this._pin}, HIGH${this._timeout !== undefined ? `, ${this._timeout}` : ""});`);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
/** Measures the next LOW pulse duration in microseconds. */
|
|
49
|
+
low() {
|
|
50
|
+
rawCpp(`return pulseIn(${this._pin}, LOW${this._timeout !== undefined ? `, ${this._timeout}` : ""});`);
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function randomSeed(seed: number): void;
|
|
2
|
+
export declare function random(minOrMax: number, max?: number): number;
|
|
3
|
+
export declare class Random {
|
|
4
|
+
/** Seeds the PRNG with a starting value. */
|
|
5
|
+
static seed(val: number): void;
|
|
6
|
+
/** Returns a random number in range [0, max-1]. */
|
|
7
|
+
static upTo(max: number): number;
|
|
8
|
+
/** Returns a random number in range [min, max-1]. */
|
|
9
|
+
static between(min: number, max: number): number;
|
|
10
|
+
/** Returns a random non-negative 31-bit integer [0, 2147483646]. */
|
|
11
|
+
static int(): number;
|
|
12
|
+
}
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { rawCpp } from './emit.js';
|
|
2
|
+
export function randomSeed(seed) { }
|
|
3
|
+
export function random(minOrMax, max) { return 0; }
|
|
4
|
+
export class Random {
|
|
5
|
+
/** Seeds the PRNG with a starting value. */
|
|
6
|
+
static seed(val) {
|
|
7
|
+
rawCpp(`randomSeed(${val});`);
|
|
8
|
+
}
|
|
9
|
+
/** Returns a random number in range [0, max-1]. */
|
|
10
|
+
static upTo(max) {
|
|
11
|
+
rawCpp(`return random(${max});`);
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
/** Returns a random number in range [min, max-1]. */
|
|
15
|
+
static between(min, max) {
|
|
16
|
+
rawCpp(`return random(${min}, ${max});`);
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
/** Returns a random non-negative 31-bit integer [0, 2147483646]. */
|
|
20
|
+
static int() {
|
|
21
|
+
// Arduino random(max) returns [0, max-1]; 2147483647 = INT_MAX gives a
|
|
22
|
+
// non-negative 31-bit range (sign bit always 0).
|
|
23
|
+
rawCpp(`return random(2147483647);`);
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A single-bit register field (0 or 1). */
|
|
2
|
+
export type Bit = 0 | 1;
|
|
3
|
+
/** A multi-bit register field spanning N bits. The numeric type arg carries
|
|
4
|
+
* the bit width for documentation only; the value is the raw field contents. */
|
|
5
|
+
export type Bits<N extends number = number> = number;
|
|
6
|
+
/** Class decorator marking a struct as a memory-mapped register at `address`.
|
|
7
|
+
* Erased at transpile time — the class becomes a `volatile uint32_t*`. */
|
|
8
|
+
export declare function register(address: number): ClassDecorator;
|
|
9
|
+
/** Property decorator carrying the bit range [lo, hi] (inclusive) of a field
|
|
10
|
+
* within its register. Erased at transpile time. */
|
|
11
|
+
export declare function bits(hi: number, lo: number): PropertyDecorator;
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Register-mapped struct decorators — compile-time markers
|
|
3
|
+
//
|
|
4
|
+
// `@register(address)` and `@bits(hi, lo)` are recognized by-name by the
|
|
5
|
+
// cuttlefish transpiler (packages/cuttlefish/src/ir/register-decorators.ts).
|
|
6
|
+
// The transpiler intercepts a class carrying @register, lowers it to a
|
|
7
|
+
// `volatile uint32_t*` pointer at the given address, and rewrites field
|
|
8
|
+
// reads/writes to shift/mask arithmetic. The decorators themselves are erased
|
|
9
|
+
// and never execute, so these are ambient stubs that exist purely so user code
|
|
10
|
+
// type-checks under tsc before transpilation.
|
|
11
|
+
//
|
|
12
|
+
// @register(0x40011000)
|
|
13
|
+
// class USART1 {
|
|
14
|
+
// @bits(0, 0) static UE: Bit = 0;
|
|
15
|
+
// @bits(9, 8) static PS: Bits<2> = 0;
|
|
16
|
+
// @bits(15, 8) static BAUD: Bits<8> = 0;
|
|
17
|
+
// }
|
|
18
|
+
// USART1.UE = 1; // (*USART1 & ~1UL) | ((1 & 1UL) << 0)
|
|
19
|
+
// const parity = USART1.PS; // ((*USART1 >> 8) & ((1UL << 2) - 1))
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
export {};
|
package/dist/shift.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Pin, OutputPin, InputPin } from './gpio.js';
|
|
2
|
+
export declare function shiftIn(dataPin: number, clockPin: number, bitOrder: number): number;
|
|
3
|
+
export declare function shiftOut(dataPin: number, clockPin: number, bitOrder: number, value: number): void;
|
|
4
|
+
export declare class Shift {
|
|
5
|
+
/** Directly shifts a byte out to a pin. */
|
|
6
|
+
static out(dataPin: Pin | OutputPin, clockPin: Pin | OutputPin, order: 'lsb' | 'msb', value: number): void;
|
|
7
|
+
/** Directly shifts a byte in from a pin. */
|
|
8
|
+
static in(dataPin: Pin | InputPin, clockPin: Pin | OutputPin, order: 'lsb' | 'msb'): number;
|
|
9
|
+
/** Fluent builder for shifting out. */
|
|
10
|
+
static write(dataPin: Pin | OutputPin, value: number): ShiftOutBuilder;
|
|
11
|
+
/** Fluent builder for shifting in. */
|
|
12
|
+
static read(dataPin: Pin | InputPin): ShiftInBuilder;
|
|
13
|
+
}
|
|
14
|
+
declare class ShiftOutBuilder {
|
|
15
|
+
private _dataPin;
|
|
16
|
+
private _value;
|
|
17
|
+
private _clockPin;
|
|
18
|
+
constructor(dataPin: Pin | OutputPin, value: number);
|
|
19
|
+
clock(clk: Pin | OutputPin): this;
|
|
20
|
+
msbFirst(): void;
|
|
21
|
+
lsbFirst(): void;
|
|
22
|
+
}
|
|
23
|
+
declare class ShiftInBuilder {
|
|
24
|
+
private _dataPin;
|
|
25
|
+
private _clockPin;
|
|
26
|
+
constructor(dataPin: Pin | InputPin);
|
|
27
|
+
clock(clk: Pin | OutputPin): this;
|
|
28
|
+
msbFirst(): number;
|
|
29
|
+
lsbFirst(): number;
|
|
30
|
+
}
|
|
31
|
+
export {};
|