[{"data":1,"prerenderedAt":422},["ShallowReactive",2],{"example-page-messenger":3},{"id":4,"info":5,"parts":6,"wired":35,"refs":36,"nets":37,"moreNets":416,"code":417},"messenger","Two devices with nRF52840, ST7735 TFT and a 50 kHz radio link (loop antennas, OOK 2 kbit\u002Fs), 20 cm apart. Firmware RadioOS (Zephyr 4.4, source firmware\u002Fradioos): A greets, B acknowledges and replies. The ADC measures signal strength at the envelope detector: mV and dB above the switching threshold, plus a bar on the TFT. Key SW1A\u002FSW1B = send PING. \"Scene\" view: pull the nodes apart: 20 cm ≈ 650 mV (+16 dB), 27 cm ≈ 210 mV (+6 dB), at 30 cm the link drops (field ∝ 1\u002Fr³, plus the diode curve). Shell: send \u003Ctext>, rssi, ps, info.",[7,10,13,16,18,20,22,25,27,29,31,33],[8,9],"resistor",22,[11,12],"capacitor",12,[14,15],"diode",4,[17,15],"led",[19,15],"loop_antenna",[21,15],"nmos",[23,24],"button",2,[26,24],"comparator",[28,24],"ldo",[30,24],"liion_cell",[32,24],"nrf52840",[34,24],"st7735_tft",true,{"BT1A":30,"BT1B":30,"C1A":11,"C1B":11,"C2A":11,"C2B":11,"C3A":11,"C3B":11,"C5A":11,"C5B":11,"C6A":11,"C6B":11,"D1A":17,"D1B":17,"D2A":17,"D2B":17,"DS1A":34,"DS1B":34,"L1A":19,"L1B":19,"L2A":19,"L2B":19,"Q2A":21,"Q2B":21,"R4A":8,"R4B":8,"R5A":8,"R5B":8,"R7A":8,"R7B":8,"R11A":8,"R11B":8,"SW1A":23,"SW1B":23,"U1A":32,"U1B":32,"U2A":28,"U2B":28,"U3A":26,"U3B":26,"D4A":14,"R3A":8,"R6A":8,"R8A":8,"R10A":8,"D3A":14,"C4A":11,"Q1A":21,"R9A":8,"R2A":8,"R1A":8,"D4B":14,"R3B":8,"R6B":8,"R8B":8,"R10B":8,"D3B":14,"C4B":11,"Q1B":21,"R9B":8,"R2B":8,"R1B":8},[38,129,154,160,167,178,189,195,202,209,214,221,226,233,238,244,251,258,265,272,279,286,291,297,306,324,329,335,344,353,358,365,371,376,382,387,392,397,403,410],{"name":39,"pins":40},"0",[41,44,46,49,51,53,55,57,59,61,63,65,67,70,72,74,76,79,81,83,85,87,89,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,127],[42,43],"BT1A","−",[45,43],"BT1B",[47,48],"C1A","2",[50,48],"C1B",[52,48],"C2A",[54,48],"C2B",[56,48],"C3A",[58,48],"C3B",[60,48],"C5A",[62,48],"C5B",[64,48],"C6A",[66,48],"C6B",[68,69],"D1A","K",[71,69],"D1B",[73,69],"D2A",[75,69],"D2B",[77,78],"DS1A","GND",[80,78],"DS1B",[82,48],"L1A",[84,48],"L1B",[86,48],"L2A",[88,48],"L2B",[90,91],"Q2A","S",[93,91],"Q2B",[95,48],"R4A",[97,48],"R4B",[99,48],"R5A",[101,48],"R5B",[103,48],"R7A",[105,48],"R7B",[107,48],"R11A",[109,48],"R11B",[111,48],"SW1A",[113,48],"SW1B",[115,78],"U1A",[117,78],"U1B",[117,119],"P0.31",[121,78],"U2A",[123,78],"U2B",[125,126],"U3A","V−",[128,126],"U3B",{"name":130,"pins":131},"A_3V3",[132,134,135,137,139,141,143,145,147,149,150,152],[52,133],"1",[56,133],[136,69],"D4A",[77,138],"LED",[77,140],"VCC",[142,133],"R3A",[144,133],"R6A",[146,133],"R8A",[115,148],"VDD",[115,119],[121,151],"OUT",[125,153],"V+",{"name":155,"pins":156},"A_BTN",[157,158],[111,133],[115,159],"P0.11",{"name":161,"pins":162},"A_CARRIER",[163,165],[164,133],"R10A",[115,166],"P0.26",{"name":168,"pins":169},"A_DET",[170,171,173,174,176],[64,133],[172,69],"D3A",[99,133],[115,175],"P0.02",[125,177],"+",{"name":179,"pins":180},"A_DRV",[181,183,185,188],[182,133],"C4A",[136,184],"A",[186,187],"Q1A","D",[142,48],{"name":190,"pins":191},"A_G_CAR",[192,194],[90,193],"G",[164,48],{"name":196,"pins":197},"A_G_TX",[198,199,201],[186,193],[200,48],"R9A",[107,133],{"name":203,"pins":204},"A_LED_RX",[205,207],[206,133],"R2A",[115,208],"P0.14",{"name":210,"pins":211},"A_LED_RX_K",[212,213],[73,184],[206,48],{"name":215,"pins":216},"A_LED_TX",[217,219],[218,133],"R1A",[115,220],"P0.13",{"name":222,"pins":223},"A_LED_TX_K",[224,225],[68,184],[218,48],{"name":227,"pins":228},"A_MOSI",[229,231],[77,230],"SDA\u002FMOSI",[115,232],"P0.20",{"name":234,"pins":235},"A_Q_MID",[236,237],[186,91],[90,187],{"name":239,"pins":240},"A_REF",[241,242,243],[144,48],[103,133],[125,43],{"name":245,"pins":246},"A_RX_ANT",[247,248,249,250],[60,133],[172,184],[86,133],[95,133],{"name":252,"pins":253},"A_RX_DATA",[254,255,257],[146,48],[115,256],"P0.28",[125,151],{"name":259,"pins":260},"A_SCK",[261,263],[77,262],"SCK",[115,264],"P0.19",{"name":266,"pins":267},"A_TFT_CS",[268,270],[77,269],"CS",[115,271],"P0.21",{"name":273,"pins":274},"A_TFT_DC",[275,277],[77,276],"DC",[115,278],"P0.22",{"name":280,"pins":281},"A_TFT_RES",[282,284],[77,283],"RES",[115,285],"P0.23",{"name":287,"pins":288},"A_TX_ANT",[289,290],[182,48],[82,133],{"name":292,"pins":293},"A_TX_EN",[294,295],[200,133],[115,296],"P0.27",{"name":298,"pins":299},"A_VBAT",[300,301,302,304],[42,177],[47,133],[121,303],"EN",[121,305],"IN",{"name":307,"pins":308},"B_3V3",[309,310,311,313,314,315,317,319,321,322,323],[54,133],[58,133],[312,69],"D4B",[80,138],[80,140],[316,133],"R3B",[318,133],"R6B",[320,133],"R8B",[117,148],[123,151],[128,153],{"name":325,"pins":326},"B_BTN",[327,328],[113,133],[117,159],{"name":330,"pins":331},"B_CARRIER",[332,334],[333,133],"R10B",[117,166],{"name":336,"pins":337},"B_DET",[338,339,341,342,343],[66,133],[340,69],"D3B",[101,133],[117,175],[128,177],{"name":345,"pins":346},"B_DRV",[347,349,350,352],[348,133],"C4B",[312,184],[351,187],"Q1B",[316,48],{"name":354,"pins":355},"B_G_CAR",[356,357],[93,193],[333,48],{"name":359,"pins":360},"B_G_TX",[361,362,364],[351,193],[363,48],"R9B",[109,133],{"name":366,"pins":367},"B_LED_RX",[368,370],[369,133],"R2B",[117,208],{"name":372,"pins":373},"B_LED_RX_K",[374,375],[75,184],[369,48],{"name":377,"pins":378},"B_LED_TX",[379,381],[380,133],"R1B",[117,220],{"name":383,"pins":384},"B_LED_TX_K",[385,386],[71,184],[380,48],{"name":388,"pins":389},"B_MOSI",[390,391],[80,230],[117,232],{"name":393,"pins":394},"B_Q_MID",[395,396],[351,91],[93,187],{"name":398,"pins":399},"B_REF",[400,401,402],[318,48],[105,133],[128,43],{"name":404,"pins":405},"B_RX_ANT",[406,407,408,409],[62,133],[340,184],[88,133],[97,133],{"name":411,"pins":412},"B_RX_DATA",[413,414,415],[320,48],[117,256],[128,151],7,{"file":418,"lang":419,"text":420,"lines":421,"more":35},"src\u002Fmain.c","c","\u002F*\n * RadioOS - Zephyr-based firmware for a two-node text radio link (Circuit Lab).\n *\n * Hardware per node (nRF52840):\n *   ST7735S 128x160 TFT on SPIM2: SCK P0.19, MOSI P0.20, CS P0.21, D\u002FC P0.22, RES P0.23\n *   Carrier 50 kHz square on PWM0 -> P0.26 (enabled while a frame is sent)\n *   TX_EN P0.27: keys the carrier into the transmit loop (two NMOS in series = AND)\n *   RX_IN P0.28: comparator output, high = carrier detected\n *   RSSI  P0.02 (AIN0): envelope detector voltage (the comparator's + input)\n *   Button P0.11 (to GND, internal pull-up), LED TX P0.13, LED RX P0.14 (active high)\n *   Node ID strap P0.31: open = node A, tied to GND = node B\n *   Console\u002Fshell: UART0 TX P0.06, RX P0.08\n *\n * Radio link: on-off keying of the 50 kHz carrier, 2000 bit\u002Fs, 8N1 framing\n * (bit 0 \u002F start bit = carrier on, bit 1 \u002F idle = carrier off).\n * Frame: STX(0x02) src len payload[len] xor-checksum\n *\n * Signal strength: while a frame comes in, the detector voltage is sampled every\n * millisecond; its peak is the carrier level. Shown as millivolts and as the margin\n * above the comparator threshold (R6\u002FR7 divider: VDD * 3.3k \u002F 103.3k = 105 mV),\n * i.e. how many dB the signal may still drop before the link fails.\n *\u002F\n#include \u003Czephyr\u002Fkernel.h>\n#include \u003Czephyr\u002Fversion.h>\n#include \u003Czephyr\u002Fdrivers\u002Fgpio.h>\n#include \u003Czephyr\u002Fdrivers\u002Fspi.h>\n#include \u003Czephyr\u002Fdrivers\u002Fpwm.h>\n#include \u003Czephyr\u002Fdrivers\u002Fuart.h>\n#include \u003Czephyr\u002Fdrivers\u002Fadc.h>\n#include \u003Cmath.h>\n#include \u003Czephyr\u002Fsys\u002Fprintk.h>\n#include \u003Cstring.h>\n#include \u003Cstdio.h>\n#include \u003Cstdarg.h>\n\nextern const uint8_t font5x7[95][5];\n\n#define PIN_BTN    11\n#define PIN_LED_TX 13\n#define PIN_LED_RX 14\n#define PIN_DC     22\n#define PIN_RES    23\n#define PIN_TXEN   27\n#define PIN_RXIN   28\n#define PIN_ID     31\n\n#define BIT_US     500          \u002F* 2000 bit\u002Fs *\u002F\n#define CARRIER_NS 20000        \u002F* 50 kHz *\u002F\n#define MAXMSG     40\n\nstatic const struct device *const gpio0 = DEVICE_DT_GET(DT_NODELABEL(gpio0));\nstatic const struct device *const spi = DEVICE_DT_GET(DT_NODELABEL(spi2));\nstatic const struct device *const pwm = DEVICE_DT_GET(DT_NODELABEL(pwm0));\nstatic const struct device *const con = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));\nstatic const struct spi_config cfg = {\n\t.frequency = 8000000,\n\t.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8),\n\t.cs = { .gpio = { .port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin = 21, .dt_flags = GPIO_ACTIVE_LOW }, .delay = 0, .cs_is_gpio = true },\n};\n\nstatic const struct adc_dt_spec rssi_adc = ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);\n#define THRESH_MV 105           \u002F* comparator reference: 3300 mV * 3.3k \u002F 103.3k *\u002F\n\nstatic char node = 'A';\nstatic uint32_t n_tx, n_rx, n_err;\n\n\u002F* ------------------------------------------------------------------ display *\u002F\n#define W 128\n#define H 160\n#define COLS 21\n#define ROWS 20\n#define RGB(r, g, b) ((((r) & 0xF8) \u003C\u003C 8) | (((g) & 0xFC) \u003C\u003C 3) | ((b) >> 3))\n#define C_BLACK  0x0000\n#define C_WHITE  0xFFFF\n#define C_GREY   RGB(150, 150, 150)\n#define C_BLUE   RGB(20, 60, 160)\n#define C_DKBLUE RGB(10, 20, 50)\n#define C_YELLOW RGB(255, 220, 0)\n#define C_GREEN  RGB(60, 230, 90)\n#define C_CYAN   RGB(80, 220, 255)\n#define C_RED    RGB(255, 70, 70)\n#define C_GREY_DK RGB(60, 60, 60)\n\n\u002F* bars: -1 = plain text row, 0..5 = signal bars drawn at the right edge *\u002F\nstruct row { char t[COLS + 1]; uint16_t fg, bg; int8_t bars; bool dirty; };\nstatic struct row rows[ROWS];\nstatic K_MUTEX_DEFINE(ui_lock);\nstatic K_SEM_DEFINE(ui_wake, 0, 1);\n\n#define LOG_FIRST 5\n#define LOG_LAST  17\n\nstatic void spi_out(const uint8_t *b, size_t n)\n{\n\tconst struct spi_buf sb = {.buf = (void *)b, .len = n};\n\tconst struct spi_buf_set ss = {&sb, 1};\n\tspi_write(spi, &cfg, &ss);\n}\nstatic void cmd(uint8_t c, const uint8_t *p, size_t n)\n{\n\tgpio_pin_set_raw(gpio0, PIN_DC, 0);\n\tspi_out(&c, 1);\n\tif (n) { gpio_pin_set_raw(gpio0, PIN_DC, 1); spi_out(p, n); }\n}\nstatic void window(int x, int y, int w, int h)\n{\n\tuint8_t ca[4] = {0, x, 0, x + w - 1}, ra[4] = {0, y, 0, y + h - 1};\n\tcmd(0x2A, ca, 4);\n\tcmd(0x2B, ra, 4);\n\tcmd(0x2C, NULL, 0);\n\tgpio_pin_set_raw(gpio0, PIN_DC, 1);\n}\n\nstatic uint8_t line_px[W * 8 * 2];\nstatic void draw_row(int r, const struct row *rw)\n{\n\tsize_t len = strlen(rw->t);\n\tfor (int y = 0; y \u003C 8; y++) {\n\t\tfor (int x = 0; x \u003C W; x++) {\n\t\t\tint cx = (x - 1) \u002F 6, col = (x - 1) % 6;",540,1796304401152]