@typecad/framework-arduino 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 +192 -0
- package/dist/arduino/i2c-arduino.d.ts +47 -0
- package/dist/arduino/i2c-arduino.js +6 -0
- package/dist/arduino/index.d.ts +6 -0
- package/dist/arduino/index.js +8 -0
- package/dist/arduino/spi-arduino.d.ts +42 -0
- package/dist/arduino/spi-arduino.js +6 -0
- package/dist/arduino/stubs-uno.d.ts +9 -0
- package/dist/arduino/stubs-uno.js +65 -0
- package/dist/arduino/uart-arduino.d.ts +41 -0
- package/dist/arduino/uart-arduino.js +6 -0
- package/dist/arduino-class-map.d.ts +5 -0
- package/dist/arduino-class-map.js +21 -0
- package/dist/arduino-compile.d.ts +24 -0
- package/dist/arduino-compile.js +316 -0
- package/dist/arduino-libs.d.ts +6 -0
- package/dist/arduino-libs.js +11 -0
- package/dist/cli-metadata.d.ts +12 -0
- package/dist/cli-metadata.js +196 -0
- package/dist/cpp-parser.d.ts +47 -0
- package/dist/cpp-parser.js +292 -0
- package/dist/debug-codegen.d.ts +20 -0
- package/dist/debug-codegen.js +115 -0
- package/dist/displays/ili9341-spi.d.ts +6 -0
- package/dist/displays/ili9341-spi.js +28 -0
- package/dist/displays/ssd1309-i2c.d.ts +2 -0
- package/dist/displays/ssd1309-i2c.js +16 -0
- package/dist/displays/st7796-spi.d.ts +2 -0
- package/dist/displays/st7796-spi.js +20 -0
- package/dist/graphics/ili9341.d.ts +24 -0
- package/dist/graphics/ili9341.js +59 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +27 -0
- package/dist/lib-declaration.d.ts +35 -0
- package/dist/lib-declaration.js +399 -0
- package/dist/lib-discovery.d.ts +38 -0
- package/dist/lib-discovery.js +189 -0
- package/dist/profile.d.ts +8 -0
- package/dist/profile.js +584 -0
- package/dist/strategy.d.ts +174 -0
- package/dist/strategy.js +2032 -0
- package/package.json +73 -0
- package/src/arduino/i2c-arduino.ts +72 -0
- package/src/arduino/index.ts +18 -0
- package/src/arduino/spi-arduino.ts +63 -0
- package/src/arduino/stubs-uno.ts +80 -0
- package/src/arduino/uart-arduino.ts +59 -0
- package/src/arduino-class-map.ts +32 -0
- package/src/arduino-compile.ts +359 -0
- package/src/arduino-libs.ts +33 -0
- package/src/cli-metadata.ts +250 -0
- package/src/cpp-parser.ts +361 -0
- package/src/debug-codegen.ts +155 -0
- package/src/displays/ili9341-spi.ts +33 -0
- package/src/displays/ssd1309-i2c.ts +19 -0
- package/src/displays/st7796-spi.ts +23 -0
- package/src/graphics/ili9341.ts +80 -0
- package/src/index.ts +37 -0
- package/src/lib-declaration.ts +493 -0
- package/src/lib-discovery.ts +267 -0
- package/src/profile.ts +676 -0
- package/src/strategy.ts +2126 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 typecad0
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# @typecad/framework-arduino
|
|
2
|
+
|
|
3
|
+
Arduino framework strategy for TypeCAD code generation.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@typecad/framework-arduino` implements the Arduino-compatible code generation strategy used by TypeCAD. It emits Arduino-style C++ calls such as `pinMode()`, `digitalWrite()`, and `Serial` operations, and provides helper utilities for Arduino CLI metadata, library discovery, and runtime polyfills.
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
Add the package to your `cuttlefish.config.ts`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import type { CuttlefishConfig } from '@typecad/cuttlefish/api';
|
|
15
|
+
|
|
16
|
+
const config: CuttlefishConfig = {
|
|
17
|
+
entry: './src/main.ts',
|
|
18
|
+
target: 'avr',
|
|
19
|
+
mcu: '@typecad/mcu-atmega328p',
|
|
20
|
+
board: '@typecad/board-arduino-uno',
|
|
21
|
+
framework: '@typecad/framework-arduino',
|
|
22
|
+
output: { framework: 'arduino', optimize: 'size' },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default config;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then run the CLI:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx cuttlefish src/main.ts --compile --upload --port COM4
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## How to use
|
|
35
|
+
|
|
36
|
+
### Framework strategy
|
|
37
|
+
|
|
38
|
+
When `framework` is set to `@typecad/framework-arduino`, TypeCAD generates code compatible with the Arduino runtime and Arduino CLI toolchain.
|
|
39
|
+
|
|
40
|
+
### Key exports
|
|
41
|
+
|
|
42
|
+
This package exposes the main Arduino strategy and helpers for advanced workflows:
|
|
43
|
+
|
|
44
|
+
- `ArduinoStrategy`
|
|
45
|
+
- `FrameworkStrategy` (alias for `ArduinoStrategy`)
|
|
46
|
+
- `compileArduinoSketch`
|
|
47
|
+
- `uploadArduinoSketch`
|
|
48
|
+
- `monitorArduinoSketch`
|
|
49
|
+
- `loadArduinoCliMetadata`
|
|
50
|
+
- `getInstalledLibraries`
|
|
51
|
+
- `generateArduinoLibDecl`
|
|
52
|
+
- `buildArduinoClassNameMap`
|
|
53
|
+
- `arduinoAsyncPolyfill`
|
|
54
|
+
- `generateStdStringPolyfill`
|
|
55
|
+
|
|
56
|
+
### Arduino library support
|
|
57
|
+
|
|
58
|
+
The package can discover installed Arduino libraries and generate TypeScript declaration stubs for library imports. This is useful when using third-party Arduino libraries from TypeScript firmware.
|
|
59
|
+
|
|
60
|
+
### Serial and console polyfills
|
|
61
|
+
|
|
62
|
+
`@typecad/framework-arduino` includes helpers for detecting `Serial.begin()` usage and injecting Arduino console support automatically when needed.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## AVR safety features
|
|
67
|
+
|
|
68
|
+
The following features are automatically applied when targeting AVR (Arduino Uno, Mega, etc.) or ESP32.
|
|
69
|
+
|
|
70
|
+
### Panic handler — `cuttlefish_halt`
|
|
71
|
+
|
|
72
|
+
TypeScript `throw` statements compile to a `cuttlefish_halt("PANIC")` macro call instead of a bare `for(;;){}` loop. The macro prints the message over Serial (using `F()` for flash storage) then halts:
|
|
73
|
+
|
|
74
|
+
```cpp
|
|
75
|
+
#ifndef cuttlefish_halt
|
|
76
|
+
#define cuttlefish_halt(msg) do { Serial.println(F(msg)); for (;;) {} } while (0)
|
|
77
|
+
#endif
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Flash-storage string literals — `F()`
|
|
81
|
+
|
|
82
|
+
Bare string literals passed to `console.log` / `console.error` / `console.warn` are automatically wrapped in the Arduino `F()` macro so they reside in program flash rather than precious SRAM:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// TypeScript
|
|
86
|
+
console.log("Hello AVR");
|
|
87
|
+
console.error("bad state");
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```cpp
|
|
91
|
+
// Generated C++
|
|
92
|
+
Serial.println(F("Hello AVR"));
|
|
93
|
+
Serial.print(F("[ERROR] ")); Serial.println(F("bad state"));
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Variable expressions are passed through without wrapping.
|
|
97
|
+
|
|
98
|
+
### C-stdlib string predicates (no `String` heap allocation)
|
|
99
|
+
|
|
100
|
+
`.includes()`, `.startsWith()`, and `.endsWith()` on `const char*` values are lowered to C standard-library calls instead of constructing Arduino `String` objects:
|
|
101
|
+
|
|
102
|
+
| TypeScript | Generated C++ |
|
|
103
|
+
|--------------------------|-------------------------------------------|
|
|
104
|
+
| `s.includes(sub)` | `(strstr(s, sub) != NULL)` |
|
|
105
|
+
| `s.startsWith(prefix)` | `(strncmp(s, prefix, strlen(prefix)) == 0)` |
|
|
106
|
+
| `s.endsWith(suffix)` | `__tc_endsWith(s, suffix)` |
|
|
107
|
+
|
|
108
|
+
### Configurable string buffer size — `CUTTLEFISH_STR_BUF_SIZE`
|
|
109
|
+
|
|
110
|
+
String-method polyfills (`.toUpperCase()`, `.toLowerCase()`, `.trim()`, `.replace()`, etc.) use a stack-allocated buffer whose size is controlled by a compile-time macro. Override it in your sketch or build flags:
|
|
111
|
+
|
|
112
|
+
```cpp
|
|
113
|
+
#define CUTTLEFISH_STR_BUF_SIZE 128 // default is 64
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Heap-allocation validator
|
|
117
|
+
|
|
118
|
+
On AVR targets, `new ClassName(args)` in variable declarations produces a compile-time `heap-allocation-avr` error diagnostic. AVR has only 2 KB of SRAM and no heap manager — use stack or global objects instead:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
// ❌ Error on AVR:
|
|
122
|
+
const sensor = new BME280(0x76);
|
|
123
|
+
|
|
124
|
+
// ✅ Correct — stack/global allocation:
|
|
125
|
+
let sensor: BME280; // emits: BME280 sensor;
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### `IRAM_ATTR` for ESP32 ISR functions
|
|
129
|
+
|
|
130
|
+
On ESP32 targets, functions registered as interrupt service routines (ISR callbacks) are automatically prefixed with `IRAM_ATTR` to ensure they run from fast internal RAM:
|
|
131
|
+
|
|
132
|
+
```cpp
|
|
133
|
+
// Generated on ESP32:
|
|
134
|
+
IRAM_ATTR void myButton_isr_0();
|
|
135
|
+
IRAM_ATTR void myButton_isr_0() { ... }
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## New namespaces
|
|
141
|
+
|
|
142
|
+
### `Timing` — timing utilities
|
|
143
|
+
|
|
144
|
+
| TypeCAD | Generated C++ |
|
|
145
|
+
|-------------------------------|--------------------------------|
|
|
146
|
+
| `Timing.millis()` | `millis()` |
|
|
147
|
+
| `Timing.micros()` | `micros()` |
|
|
148
|
+
| `Timing.delay(ms)` | `delay(ms)` |
|
|
149
|
+
| `Timing.delayMicroseconds(us)`| `delayMicroseconds(us)` |
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
const t0 = Timing.millis();
|
|
153
|
+
Timing.delay(500);
|
|
154
|
+
const elapsed = Timing.millis() - t0;
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### `EEPROM` — non-volatile storage
|
|
158
|
+
|
|
159
|
+
Requires `<EEPROM.h>` (included automatically by the board framework).
|
|
160
|
+
|
|
161
|
+
| TypeCAD | Generated C++ |
|
|
162
|
+
|-------------------------------|--------------------------------|
|
|
163
|
+
| `EEPROM.read(addr)` | `EEPROM.read(addr)` |
|
|
164
|
+
| `EEPROM.write(addr, val)` | `EEPROM.write(addr, val)` |
|
|
165
|
+
| `EEPROM.update(addr, val)` | `EEPROM.update(addr, val)` |
|
|
166
|
+
| `EEPROM.length()` | `EEPROM.length()` |
|
|
167
|
+
| `EEPROM.get(addr, ref)` | `EEPROM.get(addr, ref)` |
|
|
168
|
+
| `EEPROM.put(addr, ref)` | `EEPROM.put(addr, ref)` |
|
|
169
|
+
|
|
170
|
+
`EEPROM.update()` is preferred over `EEPROM.write()` when the value may not have changed — it skips the write cycle and extends EEPROM lifetime.
|
|
171
|
+
|
|
172
|
+
### `WDT` — watchdog timer
|
|
173
|
+
|
|
174
|
+
Controls the AVR hardware watchdog. Requires `<avr/wdt.h>`.
|
|
175
|
+
|
|
176
|
+
| TypeCAD | Generated C++ |
|
|
177
|
+
|-------------------------------|----------------------------------|
|
|
178
|
+
| `WDT.enable('2s')` | `wdt_enable(WDTO_2S)` |
|
|
179
|
+
| `WDT.reset()` | `wdt_reset()` |
|
|
180
|
+
| `WDT.disable()` | `wdt_disable()` |
|
|
181
|
+
|
|
182
|
+
Supported timeout strings: `'15ms'`, `'30ms'`, `'60ms'`, `'120ms'`, `'250ms'`, `'500ms'`, `'1s'`, `'2s'`, `'4s'`, `'8s'`. Calling `WDT.enable()` without an argument defaults to `WDTO_2S`.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
WDT.enable('1s'); // reset within 1 second or the board reboots
|
|
186
|
+
|
|
187
|
+
function loop(): void {
|
|
188
|
+
WDT.reset(); // pet the watchdog each loop iteration
|
|
189
|
+
doWork();
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { I2CAddress } from '@typecad/hal';
|
|
2
|
+
/**
|
|
3
|
+
* Arduino Wire-compatible I2C bus interface.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { I2C0 } from '@typecad/framework-arduino/arduino';
|
|
8
|
+
*
|
|
9
|
+
* I2C0.begin();
|
|
10
|
+
* I2C0.setClock(400000);
|
|
11
|
+
* I2C0.beginTransmission(0x76);
|
|
12
|
+
* I2C0.write(0xFA);
|
|
13
|
+
* const status = I2C0.endTransmission();
|
|
14
|
+
* const bytesAvailable = I2C0.requestFrom(0x76, 2);
|
|
15
|
+
* const data = I2C0.read();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export interface II2CArduino {
|
|
19
|
+
readonly busNumber: number;
|
|
20
|
+
readonly isInitialized: boolean;
|
|
21
|
+
/** Initialize as I2C master. */
|
|
22
|
+
begin(): void;
|
|
23
|
+
/** Initialize as I2C slave with given address. */
|
|
24
|
+
begin(address: I2CAddress): void;
|
|
25
|
+
/** Begin a transmission to the given address. */
|
|
26
|
+
beginTransmission(address: I2CAddress): void;
|
|
27
|
+
/** Write data to the transmission buffer. Returns number of bytes written. */
|
|
28
|
+
write(data: number | Uint8Array | string): number;
|
|
29
|
+
/** End the transmission and send data. Returns status code. */
|
|
30
|
+
endTransmission(stop?: boolean): number;
|
|
31
|
+
/** Request bytes from a slave. Returns number of bytes available. */
|
|
32
|
+
requestFrom(address: I2CAddress, quantity: number, stop?: boolean): number;
|
|
33
|
+
/** Number of bytes available to read. */
|
|
34
|
+
available(): number;
|
|
35
|
+
/** Read a single byte from the buffer. Returns -1 if no data available. */
|
|
36
|
+
read(): number;
|
|
37
|
+
/** Set the I2C clock frequency in Hz. */
|
|
38
|
+
setClock(clock: number): void;
|
|
39
|
+
/** Register a callback for when data is received as slave. */
|
|
40
|
+
onReceive(handler: (howMany: number) => void): void;
|
|
41
|
+
/** Register a callback for when data is requested from slave. */
|
|
42
|
+
onRequest(handler: () => void): void;
|
|
43
|
+
/** Disable the Wire peripheral. */
|
|
44
|
+
end(): void;
|
|
45
|
+
/** Attempt to recover a stuck bus (toggles SCL to release stuck slaves). */
|
|
46
|
+
recover(): boolean;
|
|
47
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/framework-arduino — I2C Arduino-compatible API interface
|
|
3
|
+
//
|
|
4
|
+
// Arduino Wire-compatible methods for I2C communication.
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { II2CArduino } from './i2c-arduino.js';
|
|
2
|
+
export type { ISPIArduino } from './spi-arduino.js';
|
|
3
|
+
export type { ISerialArduino } from './uart-arduino.js';
|
|
4
|
+
export type { I2CAddress } from '@typecad/hal';
|
|
5
|
+
export type { SPIMode, SPIBitOrder, SPISettings } from '@typecad/hal';
|
|
6
|
+
export { I2C0, SPI0, UART0 } from './stubs-uno.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/framework-arduino/arduino — Arduino-compatible API types and stubs
|
|
3
|
+
//
|
|
4
|
+
// Import from '@typecad/framework-arduino/arduino' to use Arduino-style APIs
|
|
5
|
+
// (Wire, SPI, Serial) with full TypeScript type checking.
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Arduino Uno stubs
|
|
8
|
+
export { I2C0, SPI0, UART0 } from './stubs-uno.js';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { SPIMode, SPIBitOrder, SPISettings } from '@typecad/hal';
|
|
2
|
+
/**
|
|
3
|
+
* Arduino SPI-compatible bus interface.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { SPI0 } from '@typecad/framework-arduino/arduino';
|
|
8
|
+
*
|
|
9
|
+
* SPI0.begin();
|
|
10
|
+
* SPI0.setMode(0);
|
|
11
|
+
* SPI0.setBitOrder('msb');
|
|
12
|
+
* SPI0.setFrequency(1_000_000);
|
|
13
|
+
*
|
|
14
|
+
* const response = SPI0.transfer(0xAA);
|
|
15
|
+
* SPI0.transferBuffer(buffer);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export interface ISPIArduino {
|
|
19
|
+
readonly isInitialized: boolean;
|
|
20
|
+
/** Initialize the SPI bus. */
|
|
21
|
+
begin(): void;
|
|
22
|
+
/** Disable the SPI bus. */
|
|
23
|
+
end(): void;
|
|
24
|
+
/** Begin a transaction with specific settings. */
|
|
25
|
+
beginTransaction(settings: SPISettings): void;
|
|
26
|
+
/** End the current transaction. */
|
|
27
|
+
endTransaction(): void;
|
|
28
|
+
/** Transfer a single byte (full-duplex). Returns received byte. */
|
|
29
|
+
transfer(data: number): number;
|
|
30
|
+
/** Transfer a buffer (full-duplex). Returns received bytes. */
|
|
31
|
+
transferBuffer(buffer: Uint8Array): Uint8Array;
|
|
32
|
+
/** Write a single byte. */
|
|
33
|
+
write(data: number): void;
|
|
34
|
+
/** Write a 16-bit value. */
|
|
35
|
+
write16(data: number): void;
|
|
36
|
+
/** Set clock frequency in Hz. */
|
|
37
|
+
setFrequency(hz: number): void;
|
|
38
|
+
/** Set SPI mode (0-3). */
|
|
39
|
+
setMode(mode: SPIMode): void;
|
|
40
|
+
/** Set bit order. */
|
|
41
|
+
setBitOrder(order: SPIBitOrder): void;
|
|
42
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/framework-arduino — SPI Arduino-compatible API interface
|
|
3
|
+
//
|
|
4
|
+
// Arduino SPI-compatible methods for SPI communication.
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { II2CArduino } from './i2c-arduino.js';
|
|
2
|
+
import type { ISPIArduino } from './spi-arduino.js';
|
|
3
|
+
import type { ISerialArduino } from './uart-arduino.js';
|
|
4
|
+
/** Arduino Uno I2C bus 0 (Wire library, pins A4=SDA / A5=SCL). */
|
|
5
|
+
export declare const I2C0: II2CArduino;
|
|
6
|
+
/** Arduino Uno SPI bus 0 (pins D11=MOSI, D12=MISO, D13=SCK, D10=SS). */
|
|
7
|
+
export declare const SPI0: ISPIArduino;
|
|
8
|
+
/** Arduino Uno hardware serial (UART 0, pins D0/RX, D1/TX, + USB). */
|
|
9
|
+
export declare const UART0: ISerialArduino;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Arduino Uno — Arduino-compatible API stub implementations
|
|
3
|
+
//
|
|
4
|
+
// Empty stubs for type checking. The transpiler replaces these with Arduino
|
|
5
|
+
// C++ calls via framework-arduino handlers.
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// I2C — Wire (bus 0) - Arduino API
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
/** Arduino Uno I2C bus 0 (Wire library, pins A4=SDA / A5=SCL). */
|
|
11
|
+
export const I2C0 = {
|
|
12
|
+
busNumber: 0,
|
|
13
|
+
isInitialized: false,
|
|
14
|
+
begin(_address) { },
|
|
15
|
+
beginTransmission(_address) { },
|
|
16
|
+
write(_data) { return 0; },
|
|
17
|
+
endTransmission(_stop) { return 0; },
|
|
18
|
+
requestFrom(_address, _quantity, _stop) { return 0; },
|
|
19
|
+
available() { return 0; },
|
|
20
|
+
read() { return -1; },
|
|
21
|
+
setClock(_clock) { },
|
|
22
|
+
onReceive(_handler) { },
|
|
23
|
+
onRequest(_handler) { },
|
|
24
|
+
end() { },
|
|
25
|
+
recover() { return true; },
|
|
26
|
+
};
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// SPI — SPI (bus 0) - Arduino API
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
/** Arduino Uno SPI bus 0 (pins D11=MOSI, D12=MISO, D13=SCK, D10=SS). */
|
|
31
|
+
export const SPI0 = {
|
|
32
|
+
isInitialized: false,
|
|
33
|
+
begin() { },
|
|
34
|
+
end() { },
|
|
35
|
+
beginTransaction(_settings) {
|
|
36
|
+
/* transpiler: SPI.beginTransaction(SPISettings(freq, bitOrder, mode)); */
|
|
37
|
+
},
|
|
38
|
+
endTransaction() { },
|
|
39
|
+
transfer(_data) { return 0; },
|
|
40
|
+
transferBuffer(_buffer) { return new Uint8Array(0); },
|
|
41
|
+
write(_data) { },
|
|
42
|
+
write16(_data) { },
|
|
43
|
+
setFrequency(_hz) { },
|
|
44
|
+
setMode(_mode) { },
|
|
45
|
+
setBitOrder(_order) { },
|
|
46
|
+
};
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// UART0 — UART 0 (USB / pins D0=RX, D1=TX) - Arduino API
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
/** Arduino Uno hardware serial (UART 0, pins D0/RX, D1/TX, + USB). */
|
|
51
|
+
export const UART0 = {
|
|
52
|
+
uartNumber: 0,
|
|
53
|
+
baudRate: 9600,
|
|
54
|
+
isInitialized: false,
|
|
55
|
+
begin(_baud) { },
|
|
56
|
+
end() { },
|
|
57
|
+
available() { return 0; },
|
|
58
|
+
peek() { return -1; },
|
|
59
|
+
read() { return -1; },
|
|
60
|
+
write(_data) { return 0; },
|
|
61
|
+
flush() { },
|
|
62
|
+
print(..._args) { },
|
|
63
|
+
println(..._args) { },
|
|
64
|
+
printf(_format, ..._args) { },
|
|
65
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arduino Serial-compatible UART interface.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { UART0 } from '@typecad/framework-arduino/arduino';
|
|
7
|
+
*
|
|
8
|
+
* UART0.begin(9600);
|
|
9
|
+
* UART0.println("Hello, World!");
|
|
10
|
+
*
|
|
11
|
+
* if (UART0.available() > 0) {
|
|
12
|
+
* const data = UART0.read();
|
|
13
|
+
* UART0.write(data);
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface ISerialArduino {
|
|
18
|
+
readonly uartNumber: number;
|
|
19
|
+
readonly baudRate: number;
|
|
20
|
+
readonly isInitialized: boolean;
|
|
21
|
+
/** Initialize with baud rate. */
|
|
22
|
+
begin(baud: number): void;
|
|
23
|
+
/** Deinitialize the UART. */
|
|
24
|
+
end(): void;
|
|
25
|
+
/** Number of bytes available to read. */
|
|
26
|
+
available(): number;
|
|
27
|
+
/** Peek at next byte without consuming. Returns -1 if no data. */
|
|
28
|
+
peek(): number;
|
|
29
|
+
/** Read a single byte. Returns -1 if no data available. */
|
|
30
|
+
read(): number;
|
|
31
|
+
/** Write a single byte or buffer. Returns number of bytes written. */
|
|
32
|
+
write(data: number | Uint8Array | string): number;
|
|
33
|
+
/** Wait for transmission to complete. */
|
|
34
|
+
flush(): void;
|
|
35
|
+
/** Print values without newline. */
|
|
36
|
+
print(...args: unknown[]): void;
|
|
37
|
+
/** Print values with newline. */
|
|
38
|
+
println(...args: unknown[]): void;
|
|
39
|
+
/** Printf-style formatted print. */
|
|
40
|
+
printf(format: string, ...args: unknown[]): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/framework-arduino — UART/Serial Arduino-compatible API interface
|
|
3
|
+
//
|
|
4
|
+
// Arduino Serial-compatible methods for UART communication.
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getArduinoLibraryClassNames, isArduinoLibraryImport } from "./arduino-libs.js";
|
|
2
|
+
// Module-level cache for Arduino library class name mappings.
|
|
3
|
+
// Maps module specifier -> (simple class name -> fully qualified name)
|
|
4
|
+
const arduinoClassNameCache = new Map();
|
|
5
|
+
export function buildArduinoClassNameMap(imports) {
|
|
6
|
+
const result = new Map();
|
|
7
|
+
for (const importedModule of imports) {
|
|
8
|
+
if (!isArduinoLibraryImport(importedModule.moduleSpecifier)) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
let classMap = arduinoClassNameCache.get(importedModule.moduleSpecifier);
|
|
12
|
+
if (!classMap) {
|
|
13
|
+
classMap = getArduinoLibraryClassNames(importedModule.moduleSpecifier);
|
|
14
|
+
arduinoClassNameCache.set(importedModule.moduleSpecifier, classMap);
|
|
15
|
+
}
|
|
16
|
+
for (const [simpleName, fullName] of classMap) {
|
|
17
|
+
result.set(simpleName, fullName);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CompileResult, UploadResult } from "@typecad/cuttlefish/api/shared";
|
|
2
|
+
export type ArduinoCompileResult = CompileResult;
|
|
3
|
+
export type ArduinoUploadResult = UploadResult;
|
|
4
|
+
/**
|
|
5
|
+
* Parse memory usage from arduino-cli output.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseArduinoMemoryUsage(output: string): {
|
|
8
|
+
flashUsed?: number;
|
|
9
|
+
flashTotal?: number;
|
|
10
|
+
ramUsed?: number;
|
|
11
|
+
ramTotal?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Extract architecture from FQBN string.
|
|
15
|
+
* FQBN format: vendor:arch:board[:config]
|
|
16
|
+
*/
|
|
17
|
+
export declare function toArchitectureFromFqbn(buildTarget?: string): string | undefined;
|
|
18
|
+
export declare function flattenGeneratedModulesIntoSketch(sketchDir: string, sketchPath: string): void;
|
|
19
|
+
export declare function compileArduinoSketch(sketchFilePath: string, buildTarget: string, options?: {
|
|
20
|
+
extraFlags?: string[];
|
|
21
|
+
defines?: Record<string, string>;
|
|
22
|
+
}): ArduinoCompileResult;
|
|
23
|
+
export declare function uploadArduinoSketch(sketchDir: string, buildTarget: string, port: string): ArduinoUploadResult;
|
|
24
|
+
export declare function monitorArduinoSketch(port: string, baud: number): void;
|