#!/bin/bash #---------------------------------------------------------------------- # A firmware Sketch for the Keyboardio Model 01 -- apply color maps # Copyright © 2019 Guilhem Moulin # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . #---------------------------------------------------------------------- set -eu PATH=/usr/bin:/bin export PATH PALETTE="$1" COLORMAP="$2" DEFAULT_DEVICE_PATH=( /dev/serial/by-id/usb-keyboardio_Model_01_* ) DEVICE_PATH="${DEVICE_PATH:-${DEFAULT_DEVICE_PATH[0]}}" stty -F "$DEVICE_PATH" 9600 raw -echo # cf. key_led_map in # ./lib/hardware/keyboardio/avr/libraries/Kaleidoscope/src/Kaleidoscope-Hardware-Model01.h declare -a LED_MAP=( 3 4 11 12 19 20 26 27 36 37 43 44 51 52 59 60 2 5 10 13 18 21 25 28 35 38 42 45 50 53 58 61 1 6 9 14 17 22 24 29 34 39 41 46 49 54 57 62 0 7 8 15 16 23 31 30 33 32 40 47 48 55 56 63 ) declare -a LED_RMAP_LEFT=() LED_RMAP_RIGHT=() LED_RMAP=() for ((r = 0; r < 4; r++)); do for ((c = 0; c < 8; c++)); do LED_RMAP_LEFT[${LED_MAP[r*16+c]}]=$((r*8+c)) LED_RMAP_RIGHT[${LED_MAP[r*16+8+c]}-32]=$((r*8+c)) done done transpose() { local i offset=$(($1 + 1 - ${#LED_MAP[@]})) MAPFILE[$1]="$2" for ((i = 0; i < ${#LED_RMAP_LEFT[@]}; i++)); do printf " %d" "${MAPFILE[offset+${LED_RMAP_LEFT[i]}]}" done for ((i = 0; i < ${#LED_RMAP_RIGHT[@]}; i++)); do printf " %d" "${MAPFILE[offset+32+${LED_RMAP_RIGHT[i]}]}" done } colormap() { local colormap="$(mktemp --tmpdir)" fd declare -a MAPFILE # ugly workaround for the lack of O_TMPFILE exec {fd}>"$colormap" rm -f -- "$colormap" sed -r "/^#/d; s/\\s+/\\n/g" <"$COLORMAP" | sed "/^\\s*$/d" >&$fd printf "colormap.map" mapfile -t -C transpose -c ${#LED_MAP[@]} <"/proc/self/fd/$fd" printf "\\n" {fd}>&- } palette() { printf "palette" sed "/^\\s*#/d; s/\\s*#.*//" <"$PALETTE" \ | sed -nr "1h;1!H; \${x; s/^\\s*/ /; s/\s+/ /g; p}" } serial_send() { local fd old_IFS="$IFS" exec {fd}<"$DEVICE_PATH" "$@" >"$DEVICE_PATH" while IFS= read -r -u "$fd" x; do x="${x%$'\r'}" [ "$x" != "." ] || break printf "%s\\n" "$x" done IFS="$old_IFS" exec {fd}<&- } serial_send palette serial_send colormap