@typecad/framework-zephyr 1.0.0-alpha.18 → 1.0.0-alpha.19

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 (114) hide show
  1. package/dist/audit.d.ts +111 -0
  2. package/dist/audit.js +416 -0
  3. package/dist/boardgen.js +63 -5
  4. package/dist/chips/resolve.js +20 -0
  5. package/dist/chips/types.d.ts +21 -0
  6. package/dist/display/bindings.d.ts +55 -0
  7. package/dist/display/bindings.js +316 -0
  8. package/dist/display/gfx.d.ts +2 -3
  9. package/dist/display/gfx.js +166 -154
  10. package/dist/display/index.js +20 -1
  11. package/dist/display/mipi-dbi-host.d.ts +9 -0
  12. package/dist/display/mipi-dbi-host.js +174 -0
  13. package/dist/display/profiles.d.ts +109 -4
  14. package/dist/display/profiles.js +270 -7
  15. package/dist/display/touch-adapter.js +118 -48
  16. package/dist/display/ui-adapter-eink.d.ts +2 -0
  17. package/dist/display/ui-adapter-eink.js +4 -0
  18. package/dist/display/ui-adapter-gray.d.ts +8 -0
  19. package/dist/display/ui-adapter-gray.js +170 -0
  20. package/dist/display/ui-adapter-mono.d.ts +13 -0
  21. package/dist/display/ui-adapter-mono.js +230 -0
  22. package/dist/display/ui-adapter-native.d.ts +10 -0
  23. package/dist/display/ui-adapter-native.js +295 -0
  24. package/dist/display/ui-adapter-shared.d.ts +11 -0
  25. package/dist/display/ui-adapter-shared.js +122 -0
  26. package/dist/display/ui-adapter.js +53 -101
  27. package/dist/dt-config/kconfig.d.ts +62 -1
  28. package/dist/dt-config/kconfig.js +123 -26
  29. package/dist/dt-config/overlay.d.ts +13 -0
  30. package/dist/dt-config/overlay.js +431 -16
  31. package/dist/framework.manifest.d.ts +9 -3
  32. package/dist/framework.manifest.js +123 -10
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.js +9 -1
  35. package/dist/licenses.js +5 -84
  36. package/dist/lowering/can.d.ts +25 -0
  37. package/dist/lowering/can.js +97 -0
  38. package/dist/lowering/clock.d.ts +17 -0
  39. package/dist/lowering/clock.js +58 -0
  40. package/dist/lowering/hid.d.ts +27 -0
  41. package/dist/lowering/hid.js +244 -0
  42. package/dist/lowering/i2c.d.ts +8 -0
  43. package/dist/lowering/i2c.js +140 -0
  44. package/dist/lowering/i2s.d.ts +27 -0
  45. package/dist/lowering/i2s.js +98 -0
  46. package/dist/lowering/index.d.ts +9 -1
  47. package/dist/lowering/index.js +25 -1
  48. package/dist/lowering/matrix.d.ts +15 -0
  49. package/dist/lowering/matrix.js +63 -0
  50. package/dist/lowering/power.d.ts +10 -0
  51. package/dist/lowering/power.js +35 -0
  52. package/dist/lowering/pwm.js +25 -0
  53. package/dist/lowering/sensor.d.ts +2 -2
  54. package/dist/lowering/sensor.js +8 -4
  55. package/dist/lowering/strip.d.ts +16 -0
  56. package/dist/lowering/strip.js +70 -0
  57. package/dist/lowering/thread.js +5 -1
  58. package/dist/lowering/trace.d.ts +44 -0
  59. package/dist/lowering/trace.js +239 -0
  60. package/dist/lowering/uart.js +6 -1
  61. package/dist/lowering/usb.d.ts +3 -1
  62. package/dist/lowering/usb.js +6 -3
  63. package/dist/sbom.d.ts +181 -0
  64. package/dist/sbom.js +901 -0
  65. package/dist/strategy.d.ts +60 -2
  66. package/dist/strategy.js +485 -25
  67. package/dist/toolchain/index.d.ts +12 -1
  68. package/dist/toolchain/index.js +115 -27
  69. package/dist/toolchain/scaffold.d.ts +4 -1
  70. package/dist/toolchain/scaffold.js +67 -3
  71. package/dist/west-inventory.d.ts +25 -0
  72. package/dist/west-inventory.js +97 -0
  73. package/package.json +3 -3
  74. package/src/audit.ts +529 -0
  75. package/src/boardgen.ts +54 -5
  76. package/src/chips/resolve.ts +21 -0
  77. package/src/chips/types.ts +576 -567
  78. package/src/display/bindings.ts +347 -0
  79. package/src/display/gfx.ts +318 -306
  80. package/src/display/index.ts +87 -70
  81. package/src/display/mipi-dbi-host.ts +183 -0
  82. package/src/display/profiles.ts +458 -139
  83. package/src/display/touch-adapter.ts +274 -204
  84. package/src/display/ui-adapter-eink.ts +13 -0
  85. package/src/display/ui-adapter-gray.ts +178 -0
  86. package/src/display/ui-adapter-mono.ts +238 -0
  87. package/src/display/ui-adapter-native.ts +304 -0
  88. package/src/display/ui-adapter-shared.ts +125 -0
  89. package/src/display/ui-adapter.ts +51 -100
  90. package/src/dt-config/kconfig.ts +647 -511
  91. package/src/dt-config/overlay.ts +433 -16
  92. package/src/framework.manifest.ts +131 -10
  93. package/src/index.ts +11 -1
  94. package/src/licenses.ts +5 -84
  95. package/src/lowering/can.ts +140 -0
  96. package/src/lowering/clock.ts +91 -0
  97. package/src/lowering/hid.ts +261 -0
  98. package/src/lowering/i2c.ts +146 -0
  99. package/src/lowering/i2s.ts +143 -0
  100. package/src/lowering/index.ts +18 -1
  101. package/src/lowering/matrix.ts +70 -0
  102. package/src/lowering/power.ts +41 -0
  103. package/src/lowering/pwm.ts +192 -167
  104. package/src/lowering/sensor.ts +159 -155
  105. package/src/lowering/strip.ts +81 -0
  106. package/src/lowering/thread.ts +5 -1
  107. package/src/lowering/trace.ts +270 -0
  108. package/src/lowering/uart.ts +6 -1
  109. package/src/lowering/usb.ts +224 -221
  110. package/src/sbom.ts +1117 -0
  111. package/src/strategy.ts +440 -25
  112. package/src/toolchain/index.ts +117 -28
  113. package/src/toolchain/scaffold.ts +62 -3
  114. package/src/west-inventory.ts +102 -0
@@ -1,204 +1,274 @@
1
- // ---------------------------------------------------------------------------
2
- // Zephyr touch adapters for the TypeCAD UI rendering pipeline.
3
- //
4
- // Two controllers, both driven directly (polling, no in-tree driver):
5
- // - FT6336U capacitive over I2C (i2c_write_read_dt)
6
- // - XPT2046 resistive over SPI (spi_transceive_dt) — the Zephyr analog of
7
- // Arduino's XPT2046_Touchscreen adapter. The DT node uses the in-tree
8
- // xptek,xpt2046 binding; CONFIG_INPUT stays off, so the in-tree input
9
- // driver does not build and this adapter owns the chip.
10
- //
11
- // The adapters emit touch_init / touch_isTouched / touch_readRaw — the three
12
- // symbols the runtime's ui_poll_touch body calls.
13
- //
14
- // EMIT BOUNDARY: emitted bytes land in user firmware. Covered by the TypeCAD
15
- // Runtime Exception (RUNTIME_EXCEPTION.md at the repo root).
16
- // ---------------------------------------------------------------------------
17
-
18
- import type { TouchAdapterCodegen } from "@typecad/cuttlefish/api/shared";
19
- import type { TouchProfile } from "@typecad/cuttlefish/api/shared";
20
-
21
- /**
22
- * Generate a Zephyr touch adapter for the profile's library. Returns
23
- * undefined for libraries this framework doesn't handle so the strategy can
24
- * decline and cuttlefish surfaces a clear error.
25
- */
26
- export function zephyrTouchAdapter(touch: TouchProfile): TouchAdapterCodegen | undefined {
27
- if (touch.library === "XPT2046_Touchscreen") return xpt2046Adapter(touch);
28
- if (touch.library === "FT6336U") return ft6336uAdapter(touch);
29
- return undefined;
30
- }
31
-
32
- /** XPT2046 control bytes (12-bit differential mode, auto power-down). */
33
- const XPT2046_CMD_X = 0x90;
34
- const XPT2046_CMD_Y = 0xd0;
35
- const XPT2046_CMD_Z1 = 0xb0;
36
- const hex = (v: number): string => `0x${v.toString(16).toUpperCase()}`;
37
-
38
- function xpt2046Adapter(touch: TouchProfile): TouchAdapterCodegen {
39
- // Pen-detect Z1 threshold (raw 12-bit counts). Resistive panels need a few
40
- // hundred counts above noise; the TouchProfile default (10) is far too low
41
- // for raw Z1, so the adapter defaults to 400 (the Arduino XPT2046
42
- // library's Z_THRESHOLD) unless the config sets one explicitly.
43
- const zThreshold = touch.minPressure ?? 400;
44
-
45
- return {
46
- includes: [
47
- `#include <zephyr/kernel.h>`,
48
- `#include <zephyr/drivers/spi.h>`,
49
- `#include <zephyr/drivers/gpio.h>`,
50
- ],
51
- declaration: [
52
- `// Zephyr XPT2046 resistive touch — SPI device resolved via devicetree.`,
53
- `// The overlay defines the 'xpt2046' nodelabel on the panel's SPI bus`,
54
- `// (CS index 1) with the xptek,xpt2046 binding; the bus + CS + 2.5MHz`,
55
- `// ceiling come from that node. Raw X/Y/Z1 values are reported — the`,
56
- `// runtime applies the profile calibration + rotation.`,
57
- `static const struct spi_dt_spec __tc_touch =`,
58
- ` SPI_DT_SPEC_GET(DT_NODELABEL(xpt2046), SPI_OP_MODE_MASTER | SPI_WORD_SET(8), 0U);`,
59
- `static int16_t __tc_touch_cached_x = 0;`,
60
- `static int16_t __tc_touch_cached_y = 0;`,
61
- `static int16_t __tc_touch_cached_z = 0;`,
62
- `static uint8_t __tc_touch_cached_valid = 0;`,
63
- `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
64
- `static const struct gpio_dt_spec __tc_touch_irq = GPIO_DT_SPEC_GET(DT_NODELABEL(xpt2046), int_gpios);`,
65
- `static uint8_t __tc_touch_irq_ready = 0;`,
66
- `#endif`,
67
- ].join("\n"),
68
- functions: [
69
- `// One XPT2046 conversion: clock out a control byte, read the 12-bit`,
70
- `// result. The rx byte during the command phase is a dummy; the value is`,
71
- `// MSB-aligned across the following 16 bits ((b1<<8 | b2) >> 3). The`,
72
- `// control byte's PD bits are 00 (auto power-down between transactions),`,
73
- `// so the bus stays shareable with the panel. Touch is polled every frame`,
74
- `// from ui_poll_touch, so a failed transfer is non-fatal — returns 0 and`,
75
- `// touch_isTouched() reports false.`,
76
- `static uint16_t __tc_xpt_read(uint8_t cmd) {`,
77
- ` if (!device_is_ready(__tc_touch.bus)) return 0U;`,
78
- ` uint8_t __tx[3] = { cmd, 0U, 0U };`,
79
- ` uint8_t __rx[3] = { 0U, 0U, 0U };`,
80
- ` struct spi_buf __bt = { __tx, sizeof(__tx) };`,
81
- ` struct spi_buf __br = { __rx, sizeof(__rx) };`,
82
- ` struct spi_buf_set __st = { &__bt, 1 };`,
83
- ` struct spi_buf_set __sr = { &__br, 1 };`,
84
- ` if (spi_transceive_dt(&__tc_touch, &__st, &__sr) != 0) return 0U;`,
85
- ` return static_cast<uint16_t>(((static_cast<uint16_t>(__rx[1]) << 8) | __rx[2]) >> 3);`,
86
- `}`,
87
- ``,
88
- `static inline void touch_init() {`,
89
- ` // The SPI bus + CS are configured by devicetree. Configure the pen IRQ`,
90
- ` // input (active low) when the overlay wired it — the cheap detect path.`,
91
- `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
92
- ` if (device_is_ready(__tc_touch_irq.port)) {`,
93
- ` gpio_pin_configure_dt(&__tc_touch_irq, GPIO_INPUT);`,
94
- ` __tc_touch_irq_ready = 1U;`,
95
- ` }`,
96
- `#endif`,
97
- ` (void)device_is_ready(__tc_touch.bus);`,
98
- `}`,
99
- ``,
100
- `static inline bool touch_isTouched() {`,
101
- ` __tc_touch_cached_valid = 0;`,
102
- ` __tc_touch_cached_z = 0;`,
103
- `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
104
- ` if (__tc_touch_irq_ready != 0U) {`,
105
- ` // PENIRQ asserts (active low) while the panel is pressed — no SPI`,
106
- ` // traffic needed to answer the per-frame poll.`,
107
- ` if (gpio_pin_get_dt(&__tc_touch_irq) <= 0) return false;`,
108
- ` }`,
109
- `#endif`,
110
- ` // No IRQ (or it fired): confirm via Z1 pressure before reading coords.`,
111
- ` uint16_t __z1 = __tc_xpt_read(${hex(XPT2046_CMD_Z1)});`,
112
- ` if (__z1 <= ${zThreshold}) return false;`,
113
- ` __tc_touch_cached_x = static_cast<int16_t>(__tc_xpt_read(${hex(XPT2046_CMD_X)}));`,
114
- ` __tc_touch_cached_y = static_cast<int16_t>(__tc_xpt_read(${hex(XPT2046_CMD_Y)}));`,
115
- ` __tc_touch_cached_z = static_cast<int16_t>(__z1);`,
116
- ` __tc_touch_cached_valid = 1;`,
117
- ` return true;`,
118
- `}`,
119
- ``,
120
- `static inline void touch_readRaw(int16_t* x, int16_t* y, int16_t* z) {`,
121
- ` // touch_isTouched() is polled each frame by ui_poll_touch; the cached`,
122
- ` // values are fresh. If something calls readRaw before isTouched, probe now.`,
123
- ` if (!__tc_touch_cached_valid) {`,
124
- ` (void)touch_isTouched();`,
125
- ` }`,
126
- ` if (x) *x = __tc_touch_cached_x;`,
127
- ` if (y) *y = __tc_touch_cached_y;`,
128
- ` if (z) *z = __tc_touch_cached_z;`,
129
- ` __tc_touch_cached_valid = 0;`,
130
- `}`,
131
- ].join("\n"),
132
- };
133
- }
134
-
135
- function ft6336uAdapter(_touch: TouchProfile): TouchAdapterCodegen {
136
- return {
137
- includes: [
138
- `#include <zephyr/drivers/i2c.h>`,
139
- ],
140
- declaration: [
141
- `// Zephyr FT6336U touch — I2C device resolved via devicetree.`,
142
- `// The board DT must define an 'ft6336u' nodelabel on an I2C bus.`,
143
- `static const struct i2c_dt_spec __tc_touch = I2C_DT_SPEC_GET(DT_NODELABEL(ft6336u));`,
144
- `static int16_t __tc_touch_cached_x = 0;`,
145
- `static int16_t __tc_touch_cached_y = 0;`,
146
- `static int16_t __tc_touch_cached_z = 0;`,
147
- `static uint8_t __tc_touch_cached_valid = 0;`,
148
- ].join("\n"),
149
- functions: [
150
- `// Read N bytes starting from a register address. Returns 1 on success.`,
151
- `// Uses i2c_write_read_dt for a combined write-read transaction (register`,
152
- `// address write, then repeated-start read). The FT6336U is polled every`,
153
- `// frame from ui_poll_touch, so a failed read (stuck SDA, no controller) is`,
154
- `// non-fatal — returns 0 and touch_isTreated() returns false.`,
155
- `static inline uint8_t __tc_touch_read_block(uint8_t reg, uint8_t* buf, uint8_t len) {`,
156
- ` if (!device_is_ready(__tc_touch.bus)) return 0U;`,
157
- ` int rc = i2c_write_read_dt(&__tc_touch, &reg, 1U, buf, len);`,
158
- ` return (rc == 0) ? 1U : 0U;`,
159
- `}`,
160
- ``,
161
- `static inline void touch_init() {`,
162
- ` // The I2C bus + device are configured by devicetree. The FT6336U needs a`,
163
- ` // hardware reset (reset pin low → high) to enter normal mode after power-on.`,
164
- `#if DT_NODE_HAS_STATUS(DT_NODELABEL(ft6336u), okay) && DT_NODE_HAS_PROP(DT_NODELABEL(ft6336u), reset_gpios)`,
165
- ` static const struct gpio_dt_spec __tc_touch_rst = GPIO_DT_SPEC_GET(DT_NODELABEL(ft6336u), reset_gpios);`,
166
- ` if (device_is_ready(__tc_touch_rst.port)) {`,
167
- ` gpio_pin_configure_dt(&__tc_touch_rst, GPIO_OUTPUT_ACTIVE); // assert reset (active=low)`,
168
- ` k_msleep(10);`,
169
- ` gpio_pin_set_dt(&__tc_touch_rst, 0); // release reset (inactive=high)`,
170
- ` k_msleep(200); // FT6336U needs ~50ms after reset before responding`,
171
- ` }`,
172
- `#endif`,
173
- ` (void)device_is_ready(__tc_touch.bus);`,
174
- `}`,
175
- ``,
176
- `static inline bool touch_isTouched() {`,
177
- ` __tc_touch_cached_valid = 0;`,
178
- ` __tc_touch_cached_z = 0;`,
179
- ` // Read TD_STATUS (0x02) + P1_XH/XL/YH/YL — 5 bytes in one transaction.`,
180
- ` uint8_t buf[5] = {0, 0, 0, 0, 0};`,
181
- ` if (!__tc_touch_read_block(0x02, buf, 5U)) return false;`,
182
- ` uint8_t count = buf[0] & 0x0F;`,
183
- ` if (count == 0U) return false;`,
184
- ` __tc_touch_cached_x = static_cast<int16_t>((static_cast<uint16_t>(buf[1] & 0x0F) << 8) | buf[2]);`,
185
- ` __tc_touch_cached_y = static_cast<int16_t>((static_cast<uint16_t>(buf[3] & 0x0F) << 8) | buf[4]);`,
186
- ` __tc_touch_cached_z = 255;`,
187
- ` __tc_touch_cached_valid = 1;`,
188
- ` return true;`,
189
- `}`,
190
- ``,
191
- `static inline void touch_readRaw(int16_t* x, int16_t* y, int16_t* z) {`,
192
- ` // touch_isTouched() is polled each frame by ui_poll_touch; the cached`,
193
- ` // values are fresh. If something calls readRaw before isTouched, probe now.`,
194
- ` if (!__tc_touch_cached_valid) {`,
195
- ` (void)touch_isTouched();`,
196
- ` }`,
197
- ` if (x) *x = __tc_touch_cached_x;`,
198
- ` if (y) *y = __tc_touch_cached_y;`,
199
- ` if (z) *z = __tc_touch_cached_z;`,
200
- ` __tc_touch_cached_valid = 0;`,
201
- `}`,
202
- ].join("\n"),
203
- };
204
- }
1
+ // ---------------------------------------------------------------------------
2
+ // Zephyr touch adapters for the TypeCAD UI rendering pipeline.
3
+ //
4
+ // - FT6336U capacitive: through Zephyr's INPUT subsystem — the in-tree
5
+ // focaltech,ft5336 driver owns the controller (polling mode: the node
6
+ // carries no int-gpios, so the driver samples on a k_timer/work cycle
7
+ // and no GPIO IRQ is registered — sidestepping the ESP32
8
+ // VECDESC_FL_SHARED crash the direct-IRQ path hit). The adapter listens
9
+ // for the device's INPUT events and serves the runtime's
10
+ // touch_init/isTouched/readRaw contract from the latest event state.
11
+ // Hardware-verified on the demo rig (ESP32-S3 + FT6336U, SDA8/SCL9).
12
+ // - XPT2046 resistive: raw SPI polling (spi_transceive_dt) — the Zephyr
13
+ // analog of Arduino's XPT2046_Touchscreen adapter. NOT yet on the input
14
+ // subsystem: the in-tree xpt2046 driver scales coordinates through its
15
+ // DT min-x/max-x props, which would double-apply the runtime's
16
+ // calibration math (touch_readRaw must stay in raw controller units for
17
+ // ui_poll_touch's calibration + rotation). Converting needs the DT
18
+ // scaling reconciled with the config calibration — and hardware to
19
+ // verify it on. CONFIG_INPUT stays off for xpt2046-only builds.
20
+ //
21
+ // The adapters emit touch_init / touch_isTouched / touch_readRaw — the three
22
+ // symbols the runtime's ui_poll_touch body calls. For FT6336U the raw
23
+ // coordinate space is unchanged (X/Y controller registers, native portrait
24
+ // panel units): the overlay's swapped-xy property makes the driver's ABS_X/
25
+ // ABS_Y events carry the register values verbatim, so ui_poll_touch's
26
+ // calibration + rotation math applies as before.
27
+ //
28
+ // EMIT BOUNDARY: emitted bytes land in user firmware. Covered by the TypeCAD
29
+ // Runtime Exception (RUNTIME_EXCEPTION.md at the repo root).
30
+ // ---------------------------------------------------------------------------
31
+
32
+ import type { TouchAdapterCodegen } from "@typecad/cuttlefish/api/shared";
33
+ import type { TouchProfile } from "@typecad/cuttlefish/api/shared";
34
+
35
+ /**
36
+ * Generate a Zephyr touch adapter for the profile's library. Returns
37
+ * undefined for libraries this framework doesn't handle so the strategy can
38
+ * decline and cuttlefish surfaces a clear error.
39
+ */
40
+ export function zephyrTouchAdapter(touch: TouchProfile): TouchAdapterCodegen | undefined {
41
+ if (touch.library === "XPT2046_Touchscreen") return xpt2046Adapter(touch);
42
+ if (touch.library === "FT6336U") return ft6336uAdapter(touch);
43
+ return undefined;
44
+ }
45
+
46
+ /** XPT2046 control bytes (12-bit differential mode, auto power-down). */
47
+ const XPT2046_CMD_X = 0x90;
48
+ const XPT2046_CMD_Y = 0xd0;
49
+ const XPT2046_CMD_Z1 = 0xb0;
50
+ const hex = (v: number): string => `0x${v.toString(16).toUpperCase()}`;
51
+
52
+ function xpt2046Adapter(touch: TouchProfile): TouchAdapterCodegen {
53
+ // Pen-detect Z1 threshold (raw 12-bit counts). Resistive panels need a few
54
+ // hundred counts above noise; the TouchProfile default (10) is far too low
55
+ // for raw Z1, so the adapter defaults to 400 (the Arduino XPT2046
56
+ // library's Z_THRESHOLD) unless the config sets one explicitly.
57
+ const zThreshold = touch.minPressure ?? 400;
58
+
59
+ return {
60
+ includes: [
61
+ `#include <zephyr/kernel.h>`,
62
+ `#include <zephyr/drivers/spi.h>`,
63
+ `#include <zephyr/drivers/gpio.h>`,
64
+ ],
65
+ declaration: [
66
+ `// Zephyr XPT2046 resistive touch — SPI device resolved via devicetree.`,
67
+ `// The overlay defines the 'xpt2046' nodelabel on the panel's SPI bus`,
68
+ `// (CS index 1) with the xptek,xpt2046 binding; the bus + CS + 2.5MHz`,
69
+ `// ceiling come from that node. Raw X/Y/Z1 values are reported — the`,
70
+ `// runtime applies the profile calibration + rotation.`,
71
+ `static const struct spi_dt_spec __tc_touch =`,
72
+ ` SPI_DT_SPEC_GET(DT_NODELABEL(xpt2046), SPI_OP_MODE_MASTER | SPI_WORD_SET(8), 0U);`,
73
+ `static int16_t __tc_touch_cached_x = 0;`,
74
+ `static int16_t __tc_touch_cached_y = 0;`,
75
+ `static int16_t __tc_touch_cached_z = 0;`,
76
+ `static uint8_t __tc_touch_cached_valid = 0;`,
77
+ `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
78
+ `static const struct gpio_dt_spec __tc_touch_irq = GPIO_DT_SPEC_GET(DT_NODELABEL(xpt2046), int_gpios);`,
79
+ `static uint8_t __tc_touch_irq_ready = 0;`,
80
+ `#endif`,
81
+ ].join("\n"),
82
+ functions: [
83
+ `// One XPT2046 conversion: clock out a control byte, read the 12-bit`,
84
+ `// result. The rx byte during the command phase is a dummy; the value is`,
85
+ `// MSB-aligned across the following 16 bits ((b1<<8 | b2) >> 3). The`,
86
+ `// control byte's PD bits are 00 (auto power-down between transactions),`,
87
+ `// so the bus stays shareable with the panel. Touch is polled every frame`,
88
+ `// from ui_poll_touch, so a failed transfer is non-fatal — returns 0 and`,
89
+ `// touch_isTouched() reports false.`,
90
+ `static uint16_t __tc_xpt_read(uint8_t cmd) {`,
91
+ ` if (!device_is_ready(__tc_touch.bus)) return 0U;`,
92
+ ` uint8_t __tx[3] = { cmd, 0U, 0U };`,
93
+ ` uint8_t __rx[3] = { 0U, 0U, 0U };`,
94
+ ` struct spi_buf __bt = { __tx, sizeof(__tx) };`,
95
+ ` struct spi_buf __br = { __rx, sizeof(__rx) };`,
96
+ ` struct spi_buf_set __st = { &__bt, 1 };`,
97
+ ` struct spi_buf_set __sr = { &__br, 1 };`,
98
+ ` if (spi_transceive_dt(&__tc_touch, &__st, &__sr) != 0) return 0U;`,
99
+ ` return static_cast<uint16_t>(((static_cast<uint16_t>(__rx[1]) << 8) | __rx[2]) >> 3);`,
100
+ `}`,
101
+ ``,
102
+ `static inline void touch_init() {`,
103
+ ` // The SPI bus + CS are configured by devicetree. Configure the pen IRQ`,
104
+ ` // input (active low) when the overlay wired it — the cheap detect path.`,
105
+ `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
106
+ ` if (device_is_ready(__tc_touch_irq.port)) {`,
107
+ ` gpio_pin_configure_dt(&__tc_touch_irq, GPIO_INPUT);`,
108
+ ` __tc_touch_irq_ready = 1U;`,
109
+ ` }`,
110
+ `#endif`,
111
+ ` (void)device_is_ready(__tc_touch.bus);`,
112
+ `}`,
113
+ ``,
114
+ `static inline bool touch_isTouched() {`,
115
+ ` __tc_touch_cached_valid = 0;`,
116
+ ` __tc_touch_cached_z = 0;`,
117
+ `#if DT_NODE_HAS_PROP(DT_NODELABEL(xpt2046), int_gpios)`,
118
+ ` if (__tc_touch_irq_ready != 0U) {`,
119
+ ` // PENIRQ asserts (active low) while the panel is pressed — no SPI`,
120
+ ` // traffic needed to answer the per-frame poll.`,
121
+ ` if (gpio_pin_get_dt(&__tc_touch_irq) <= 0) return false;`,
122
+ ` }`,
123
+ `#endif`,
124
+ ` // No IRQ (or it fired): confirm via Z1 pressure before reading coords.`,
125
+ ` uint16_t __z1 = __tc_xpt_read(${hex(XPT2046_CMD_Z1)});`,
126
+ ` if (__z1 <= ${zThreshold}) return false;`,
127
+ ` __tc_touch_cached_x = static_cast<int16_t>(__tc_xpt_read(${hex(XPT2046_CMD_X)}));`,
128
+ ` __tc_touch_cached_y = static_cast<int16_t>(__tc_xpt_read(${hex(XPT2046_CMD_Y)}));`,
129
+ ` __tc_touch_cached_z = static_cast<int16_t>(__z1);`,
130
+ ` __tc_touch_cached_valid = 1;`,
131
+ ` return true;`,
132
+ `}`,
133
+ ``,
134
+ `static inline void touch_readRaw(int16_t* x, int16_t* y, int16_t* z) {`,
135
+ ` // touch_isTouched() is polled each frame by ui_poll_touch; the cached`,
136
+ ` // values are fresh. If something calls readRaw before isTouched, probe now.`,
137
+ ` if (!__tc_touch_cached_valid) {`,
138
+ ` (void)touch_isTouched();`,
139
+ ` }`,
140
+ ` if (x) *x = __tc_touch_cached_x;`,
141
+ ` if (y) *y = __tc_touch_cached_y;`,
142
+ ` if (z) *z = __tc_touch_cached_z;`,
143
+ ` __tc_touch_cached_valid = 0;`,
144
+ `}`,
145
+ ].join("\n"),
146
+ };
147
+ }
148
+
149
+ function ft6336uAdapter(_touch: TouchProfile): TouchAdapterCodegen {
150
+ return {
151
+ includes: [
152
+ `#include <zephyr/kernel.h>`,
153
+ `#include <zephyr/input/input.h>`,
154
+ // TEMPORARY DIAGNOSTIC (with __tc_touch_diag above).
155
+ `#include <zephyr/drivers/i2c.h>`,
156
+ ],
157
+ declaration: [
158
+ `// Zephyr FT6336U touch — INPUT subsystem listener. The in-tree`,
159
+ `// focaltech,ft5336 driver (CONFIG_INPUT_FT5336, polling mode — no`,
160
+ `// int-gpios on the node) owns the controller and reports events; this`,
161
+ `// adapter keeps the latest state and serves the runtime's per-frame`,
162
+ `// touch_isTouched/touch_readRaw poll. The overlay's swapped-xy DT prop`,
163
+ `// makes the driver's ABS_X/ABS_Y carry the controller's X/Y registers`,
164
+ `// verbatim — the same raw space the register-polling adapter reported,`,
165
+ `// so the runtime's calibration + rotation math is unchanged.`,
166
+ `static int16_t __tc_touch_cached_x = 0;`,
167
+ `static int16_t __tc_touch_cached_y = 0;`,
168
+ `static volatile bool __tc_touch_down = false;`,
169
+ ].join("\n"),
170
+ functions: [
171
+ `// Input listener: runs in the driver's work-handler context (poll mode`,
172
+ `// samples every CONFIG_INPUT_FT5336_PERIOD ms). Latest-value semantics —`,
173
+ `// plain int16 stores with no locking; the UI loop reads them once per`,
174
+ `// frame and a torn mid-write read only ever costs one sample of one axis.`,
175
+ `// The first events are logged once — a diagnostic that costs ten lines`,
176
+ `// ever and immediately answers "are events arriving, and in which space".`,
177
+ `static uint8_t __tc_touch_evt_logged = 0U;`,
178
+ `static void __tc_touch_input_cb(struct input_event* evt, void* /*user_data*/) {`,
179
+ ` if (__tc_touch_evt_logged < 10U) {`,
180
+ ` __tc_touch_evt_logged++;`,
181
+ ` printk("TC_TOUCH: evt type=%u code=%u value=%d sync=%u\\n",`,
182
+ ` static_cast<unsigned int>(evt->type),`,
183
+ ` static_cast<unsigned int>(evt->code),`,
184
+ ` static_cast<int>(evt->value),`,
185
+ ` static_cast<unsigned int>(evt->sync));`,
186
+ ` }`,
187
+ ` if (evt->type == INPUT_EV_ABS) {`,
188
+ ` if (evt->code == INPUT_ABS_X) {`,
189
+ ` __tc_touch_cached_x = static_cast<int16_t>(evt->value);`,
190
+ ` } else if (evt->code == INPUT_ABS_Y) {`,
191
+ ` __tc_touch_cached_y = static_cast<int16_t>(evt->value);`,
192
+ ` }`,
193
+ ` } else if ((evt->type == INPUT_EV_KEY) && (evt->code == INPUT_BTN_TOUCH)) {`,
194
+ ` __tc_touch_down = (evt->value != 0);`,
195
+ ` }`,
196
+ `}`,
197
+ ``,
198
+ `// Registered for the ft6336u device's events only (the _dev filter) —`,
199
+ `// other input devices on the system never reach the UI touch state.`,
200
+ `INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_NODELABEL(ft6336u)), __tc_touch_input_cb, NULL);`,
201
+ ``,
202
+ `static inline void touch_init() {`,
203
+ ` // Power/reset enable — rig-verified sequence, matching the Arduino`,
204
+ ` // pin order this module needs (GPIO driven LOW 10ms, then HIGH, then`,
205
+ ` // 500ms settle before the controller answers). Without the drive the`,
206
+ ` // enable floats: the controller half-powers, ACKs briefly, then`,
207
+ ` // browns out — the 0xFF-then-NAK bus death reproduced identically on`,
208
+ ` // every firmware variant until this was wired. The in-tree driver`,
209
+ ` // pulses its own (shorter) reset at init; this re-drive with the`,
210
+ ` // verified timing runs from main before the UI starts polling.`,
211
+ `#if DT_NODE_HAS_STATUS(DT_NODELABEL(ft6336u), okay) && DT_NODE_HAS_PROP(DT_NODELABEL(ft6336u), reset_gpios)`,
212
+ ` static const struct gpio_dt_spec __tc_touch_en = GPIO_DT_SPEC_GET(DT_NODELABEL(ft6336u), reset_gpios);`,
213
+ ` if (device_is_ready(__tc_touch_en.port)) {`,
214
+ ` gpio_pin_configure_dt(&__tc_touch_en, GPIO_OUTPUT_ACTIVE); // drive LOW (active-low spec)`,
215
+ ` k_msleep(10);`,
216
+ ` gpio_pin_set_dt(&__tc_touch_en, 0); // drive HIGH`,
217
+ ` k_msleep(500);`,
218
+ ` }`,
219
+ `#endif`,
220
+ ` const struct device* __tc_touch_dev = DEVICE_DT_GET(DT_NODELABEL(ft6336u));`,
221
+ ` if (!device_is_ready(__tc_touch_dev)) {`,
222
+ ` printk("TC_TOUCH: ft6336u device not ready\\n");`,
223
+ ` return;`,
224
+ ` }`,
225
+ ` // Self-test: dispatch one synthetic release event through the input`,
226
+ ` // core. If the listener is registered and dispatch works, the event`,
227
+ ` // logger above prints it; rc=-EAGAIN means the input queue is full`,
228
+ ` // (input thread wedged). K_NO_WAIT so a dead queue can never block`,
229
+ ` // boot.`,
230
+ ` int __rc = input_report_key(__tc_touch_dev, INPUT_BTN_TOUCH, 0, true, K_NO_WAIT);`,
231
+ ` printk("TC_TOUCH: ft6336u ready, self-test report rc=%d\\n", __rc);`,
232
+ `}`,
233
+ ``,
234
+ `// Boot-time bus diagnostic (self-limiting): reads TD_STATUS directly`,
235
+ `// over I2C — the register path the raw adapter proved on this rig —`,
236
+ `// dense (300ms) for the first 5s, sparse (3s) until 60s, then silent.`,
237
+ `// On the demo rig this caught a failing touch module: the controller`,
238
+ `// ACKs for the first ~700ms after every reset, returns 0xFF once, then`,
239
+ `// NAKs permanently — a power/connection fault, independent of firmware`,
240
+ `// (reproduced identically on the committed pre-migration build).`,
241
+ `static const struct i2c_dt_spec __tc_touch_probe = I2C_DT_SPEC_GET(DT_NODELABEL(ft6336u));`,
242
+ `static int32_t __tc_touch_probe_next = 0;`,
243
+ `static void __tc_touch_diag() {`,
244
+ ` int32_t __now = static_cast<int32_t>(k_uptime_get_32());`,
245
+ ` if (__now >= 60000) return;`,
246
+ ` int32_t __gap = (__now < 5000) ? 300 : 3000;`,
247
+ ` if (__now < __tc_touch_probe_next) return;`,
248
+ ` __tc_touch_probe_next = __now + __gap;`,
249
+ ` uint8_t __td = 0U;`,
250
+ ` uint8_t __reg = 0x02U;`,
251
+ ` int __rc = i2c_write_read_dt(&__tc_touch_probe, &__reg, 1U, &__td, 1U);`,
252
+ ` if ((__rc != 0) || (__td != 0U)) {`,
253
+ ` printk("TC_TOUCH: diag td_status=0x%02x rc=%d down=%d\\n",`,
254
+ ` static_cast<unsigned int>(__td), __rc,`,
255
+ ` __tc_touch_down ? 1 : 0);`,
256
+ ` }`,
257
+ `}`,
258
+ ``,
259
+ `static inline bool touch_isTouched() {`,
260
+ ` __tc_touch_diag();`,
261
+ ` return __tc_touch_down;`,
262
+ `}`,
263
+ ``,
264
+ `static inline void touch_readRaw(int16_t* x, int16_t* y, int16_t* z) {`,
265
+ ` // Latest reported position in controller register units (native panel`,
266
+ ` // space); the runtime applies calibration + rotation. z is saturation`,
267
+ ` // for capacitive (the driver reports press/release, not pressure).`,
268
+ ` if (x) *x = __tc_touch_cached_x;`,
269
+ ` if (y) *y = __tc_touch_cached_y;`,
270
+ ` if (z) *z = 255;`,
271
+ `}`,
272
+ ].join("\n"),
273
+ };
274
+ }
@@ -0,0 +1,13 @@
1
+ // E-ink UI adapter (Stage 4) — the refresh-model proof. Same 1bpp format as
2
+ // mono (the parameterized mono adapter: vtiled MONO01 backing store, always-
3
+ // complemented MONO10 pushes — the ssd16xx/uc81xx families accept nothing
4
+ // else); the NEW axis is the refresh model: the runtime's UI_REFRESH_DEFERRED
5
+ // accumulator flushes on change, the adapter rate-limits pushes to the panel's
6
+ // flash-cycle economics (TC_EINK_MIN_REFRESH_MS), and the panel sleeps between
7
+ // flashes (the driver blocks through BUSY inside display_write).
8
+ import type { ZephyrDisplayProfile } from "./profiles.js";
9
+ import { zephyrMonoDisplayAdapter } from "./ui-adapter-mono.js";
10
+
11
+ export function zephyrEinkDisplayAdapter(profile: ZephyrDisplayProfile) {
12
+ return zephyrMonoDisplayAdapter(profile, { eink: true });
13
+ }