rushabhcodes/wireless-split-keyboard

Wireless Split Keyboard — custom routed nRF52840 split PCBs with a printable enclosure

Version
0.4.5
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone rushabhcodes/wireless-split-keyboard  # download the package files into ./wireless-split-keyboard
tsci add rushabhcodes/wireless-split-keyboard    # add it to your project as @tsci/rushabhcodes.wireless-split-keyboard

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

scripts/generate-firmware.ts

import { mkdir, writeFile } from 'node:fs/promises'
import { pinout,gpioNumber } from '../src/pinout'
import { geometryFor } from '../src/mechanicalGeometry'
const root='firmware/boards/custom/custom_sofle'
await mkdir(root,{recursive:true});await mkdir('firmware/zephyr',{recursive:true});await mkdir('firmware/config',{recursive:true})
const write=(p:string,t:string)=>writeFile(`${root}/${p}`,t)
const gpio=(pin:string,flags:string)=>{const {port,index}=gpioNumber(pin);return `<&gpio${port} ${index} (${flags})>`}
const rows=pinout.rows.map(p=>gpio(p,'GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN')).join(',\n            ')
const cols=pinout.columns.map(p=>gpio(p,'GPIO_ACTIVE_HIGH')).join(',\n            ')
const transform=Array.from({length:5},(_,r)=>[5,4,3,2,1,0,6,7,8,9,10,11].map(c=>`RC(${r},${c})`).join(' ')).join('\n            ')
await write('board.yml',`boards:\n  - name: custom_sofle_left\n    vendor: custom\n    socs:\n      - name: nrf52840\n  - name: custom_sofle_right\n    vendor: custom\n    socs:\n      - name: nrf52840\n`)
for(const half of ['left','right'] as const) {
 const upper=half.toUpperCase()
 await write(`Kconfig.custom_sofle_${half}`,`config BOARD_CUSTOM_SOFLE_${upper}\n    select SOC_NRF52840_QIAA\n    select ZMK_BOARD_COMPAT\n`)
 await write(`custom_sofle_${half}_nrf52840.dts`,`#include "custom_sofle.dtsi"\n/ { model = "Custom Sofle ${half}"; };\n`+(half==='right'?'&default_transform { col-offset = <6>; };\n':''))
 await write(`custom_sofle_${half}_nrf52840_defconfig`,`CONFIG_GPIO=y\nCONFIG_USE_DT_CODE_PARTITION=y\nCONFIG_MINIMAL_LIBC=y\nCONFIG_LV_USE_CUSTOM_SPRINTF=y\nCONFIG_BT_CTLR_TX_PWR_PLUS_4=y\nCONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL=y\nCONFIG_PINCTRL=y\nCONFIG_BT=y\nCONFIG_USB_DEVICE_STACK=y\nCONFIG_ZMK_BLE=y\nCONFIG_ZMK_SPLIT=y\nCONFIG_ZMK_SPLIT_ROLE_CENTRAL=${half==='left'?'y':'n'}\nCONFIG_ZMK_USB=${half==='left'?'y':'n'}\nCONFIG_ZMK_SLEEP=n\nCONFIG_ZMK_POINTING=y\nCONFIG_SPI=y\nCONFIG_ADC=y\nCONFIG_SENSOR=y\nCONFIG_EC11=y\nCONFIG_EC11_TRIGGER_GLOBAL_THREAD=y\nCONFIG_ZMK_DISPLAY=y\nCONFIG_LS0XX=y\nCONFIG_ZMK_WIDGET_BATTERY_STATUS=y\nCONFIG_ZMK_WIDGET_BATTERY_STATUS_SHOW_PERCENTAGE=y\n${half==='left'?'CONFIG_ZMK_WIDGET_OUTPUT_STATUS=y\nCONFIG_ZMK_WIDGET_LAYER_STATUS=y\nCONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING=y\n':'CONFIG_ZMK_WIDGET_PERIPHERAL_STATUS=y\n'}`)
 await write(`custom_sofle_${half}.yaml`,`identifier: custom_sofle_${half}/nrf52840\nname: Custom Sofle ${half}\ntype: mcu\narch: arm\ntoolchain:\n  - zephyr\nram: 256\nflash: 1024\nsupported:\n  - gpio\n  - spi\n  - adc\n  - usb_device\n  - ble\n`)
}
await write('custom_sofle.dtsi',`// Generated from src/pinout.ts. Application at 0: SWD only, no borrowed bootloader.
/dts-v1/;
#include <nordic/nrf52840_qiaa.dtsi>
#include <zephyr/dt-bindings/gpio/gpio.h>
#include <zephyr/dt-bindings/pinctrl/nrf-pinctrl.h>
#include <zephyr/dt-bindings/adc/adc.h>
#include <dt-bindings/zmk/matrix_transform.h>
/ {
    model = "Custom Sofle Wireless";
    compatible = "custom,sofle-wireless";
    chosen {
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
        zephyr,code-partition = &code_partition;
        zmk,kscan = &kscan0;
        zmk,matrix-transform = &default_transform;
        zmk,battery = &battery;
        zephyr,display = &lcd;
    };
    kscan0: kscan {
        compatible = "zmk,kscan-gpio-matrix";
        diode-direction = "col2row";
        row-gpios = ${rows};
        col-gpios = ${cols};
    };
    default_transform: matrix_transform {
        compatible = "zmk,matrix-transform";
        rows = <5>; columns = <12>;
        map = <${transform}>;
    };
    encoder_left: encoder_left {
        compatible = "alps,ec11";
        a-gpios = ${gpio(pinout.encoderA,'GPIO_ACTIVE_LOW | GPIO_PULL_UP')};
        b-gpios = ${gpio(pinout.encoderB,'GPIO_ACTIVE_LOW | GPIO_PULL_UP')};
        steps = <48>;
        status = "disabled";
    };
    encoder_right: encoder_right {
        compatible = "alps,ec11";
        a-gpios = ${gpio(pinout.encoderA,'GPIO_ACTIVE_LOW | GPIO_PULL_UP')};
        b-gpios = ${gpio(pinout.encoderB,'GPIO_ACTIVE_LOW | GPIO_PULL_UP')};
        steps = <48>;
        status = "disabled";
    };
    sensors: sensors {
        compatible = "zmk,keymap-sensors";
        sensors = <&encoder_left &encoder_right>;
        triggers-per-rotation = <24>;
    };
    battery: battery {
        compatible = "zmk,battery-voltage-divider";
        io-channels = <&adc 0>;
        output-ohms = <1000000>;
        full-ohms = <2000000>;
        power-gpios = ${gpio(pinout.batterySenseEnable,'GPIO_ACTIVE_HIGH')};
    };
};
&gpio0 { status = "okay"; };
&gpio1 { status = "okay"; };
&gpiote { status = "okay"; };
&reg1 { regulator-initial-mode = <NRF5X_REG_MODE_DCDC>; };
&adc {
    status = "okay";
    #address-cells = <1>; #size-cells = <0>;
    channel@0 {
        reg = <0>; zephyr,gain = "ADC_GAIN_1_6";
        zephyr,reference = "ADC_REF_INTERNAL";
        zephyr,acquisition-time = <ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40)>;
        zephyr,input-positive = <NRF_SAADC_AIN2>;
        zephyr,resolution = <12>;
    };
};
&pinctrl {
    spi1_default: spi1_default { group1 {
        psels = <NRF_PSEL(SPIM_SCK,0,26)>, <NRF_PSEL(SPIM_MOSI,0,27)>;
    }; };
    spi1_sleep: spi1_sleep { group1 {
        psels = <NRF_PSEL(SPIM_SCK,0,26)>, <NRF_PSEL(SPIM_MOSI,0,27)>;
        low-power-enable;
    }; };
};
&spi1 {
    compatible = "nordic,nrf-spim"; status = "okay";
    pinctrl-0 = <&spi1_default>; pinctrl-1 = <&spi1_sleep>;
    pinctrl-names = "default", "sleep";
    cs-gpios = ${gpio(pinout.displayCs,'GPIO_ACTIVE_HIGH')};
    lcd: ls011b7dh03@0 {
        compatible = "sharp,ls0xx"; reg = <0>;
        spi-max-frequency = <1000000>;
        width = <160>; height = <68>;
        extcomin-gpios = ${gpio(pinout.displayVcom,'GPIO_ACTIVE_HIGH')};
        extcomin-frequency = <60>;
        disp-en-gpios = ${gpio(pinout.displayDisp,'GPIO_ACTIVE_HIGH')};
    };
};
zephyr_udc0: &usbd { status = "okay"; };
&flash0 {
    partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>; #size-cells = <1>;
        code_partition: partition@0 { reg = <0x0 0xF0000>; };
        storage_partition: partition@f0000 { reg = <0xF0000 0x10000>; };
    };
};
`)
for(const half of ['left','right'] as const) await writeFile(`${root}/custom_sofle_${half}_nrf52840.dts`,(await Bun.file(`${root}/custom_sofle_${half}_nrf52840.dts`).text())+`&encoder_${half} { status = "okay"; };\n`)
await write('board.cmake','board_runner_args(jlink "--device=nRF52840_xxAA" "--speed=4000")\ninclude(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)\ninclude(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)\n')
await write('Kconfig.defconfig',`if BOARD_CUSTOM_SOFLE_LEFT || BOARD_CUSTOM_SOFLE_RIGHT\nconfig ZMK_KEYBOARD_NAME\n    default "Custom Sofle"\nconfig ZMK_SPLIT\n    default y\nconfig ZMK_SPLIT_ROLE_CENTRAL\n    default y if BOARD_CUSTOM_SOFLE_LEFT\nendif\n`)
const keyRows=[
 ['ESC','N1','N2','N3','N4','N5','N6','N7','N8','N9','N0','BSPC'],
 ['TAB','Q','W','E','R','T','Y','U','I','O','P','BSLH'],
 ['CAPS','A','S','D','F','G','H','J','K','L','SEMI','SQT'],
 ['LSHFT','Z','X','C','V','B','N','M','COMMA','DOT','FSLH','RSHFT'],
 ['LCTRL','LGUI','LALT','SPACE','MO_1','C_MUTE','C_PP','MO_1','SPACE','RALT','RGUI','RCTRL'],
]
await write('custom_sofle.keymap',`#include <behaviors.dtsi>\n#include <dt-bindings/zmk/keys.h>\n#include <dt-bindings/zmk/bt.h>\n#include <dt-bindings/zmk/pointing.h>\n/ {\n    behaviors {\n        scroll: scroll_encoder {\n            compatible = "zmk,behavior-sensor-rotate";\n            #sensor-binding-cells = <0>;\n            bindings = <&msc SCRL_UP>, <&msc SCRL_DOWN>;\n        };\n    };\n    keymap {\n        compatible = "zmk,keymap";\n        base_layer {\n            bindings = <\n${keyRows.map(row=>'                '+row.map(k=>k==='MO_1'?'&mo 1':`&kp ${k}`).join(' ')).join('\n')}\n            >;\n            sensor-bindings = <&inc_dec_kp C_VOL_UP C_VOL_DN &scroll>;\n        };\n        function_layer {\n            bindings = <\n                &bt BT_CLR &bt BT_SEL 0 &bt BT_SEL 1 &bt BT_SEL 2 &bt BT_SEL 3 &bt BT_SEL 4 &kp F6 &kp F7 &kp F8 &kp F9 &kp F10 &kp F11\n                &trans &trans &kp UP &trans &trans &trans &trans &trans &trans &trans &trans &trans\n                &trans &kp LEFT &kp DOWN &kp RIGHT &trans &trans &trans &trans &trans &trans &trans &trans\n                &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans\n                &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans &trans\n            >;\n            sensor-bindings = <&inc_dec_kp C_VOL_UP C_VOL_DN &scroll>;\n        };\n    };\n};\n`)
await writeFile('firmware/zephyr/module.yml','name: custom-sofle\nbuild:\n  cmake: zephyr\n  settings:\n    board_root: .\n')
await writeFile('firmware/zephyr/CMakeLists.txt','zephyr_library()\nzephyr_library_sources(../src/display_power.c)\n')
await mkdir('firmware/src',{recursive:true})
const {port,index}=gpioNumber(pinout.displayPower)
await writeFile('firmware/src/display_power.c',`/* Power raw panel before the display driver initializes; no deep sleep yet.
 * 60Hz EXTCOMIN thread remains running during normal system-on idle.
 */\n#include <zephyr/device.h>\n#include <zephyr/drivers/gpio.h>\n#include <zephyr/init.h>\n#include <zephyr/kernel.h>\nstatic int display_power_init(void) {\n    const struct device *gpio = DEVICE_DT_GET(DT_NODELABEL(gpio${port}));\n    if (!device_is_ready(gpio)) return -ENODEV;\n    int ret = gpio_pin_configure(gpio, ${index}, GPIO_OUTPUT_ACTIVE);\n    k_msleep(10);\n    return ret;\n}\nSYS_INIT(display_power_init, POST_KERNEL, 70);\n`)
await writeFile('firmware/config/custom_sofle.keymap',await Bun.file(`${root}/custom_sofle.keymap`).text())
await writeFile('firmware/build.yaml','include:\n  - board: custom_sofle_left/nrf52840\n  - board: custom_sofle_right/nrf52840\n')
await writeFile('firmware/config/west.yml',`manifest:\n  remotes:\n    - name: zmkfirmware\n      url-base: https://github.com/zmkfirmware\n  projects:\n    - name: zmk\n      remote: zmkfirmware\n      revision: 4de313367288aad5aeaa1243890e40dc813f7008\n      clone-depth: 1\n      import: app/west.yml\n  self:\n    path: config\n`)
await writeFile('firmware/physical-layout.json',JSON.stringify({left:geometryFor('left').keys,right:geometryFor('right').keys,pinout},null,2)+'\n')
console.log('Generated native ZMK boards and pin assignments')