aboutsummaryrefslogtreecommitdiffstats
path: root/tools/apply-colormap
blob: dd3584433ca761590784ce5496ebf13cf3bf9cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

#----------------------------------------------------------------------
# A firmware Sketch for the Keyboardio Model 01 -- apply color maps
# Copyright © 2019 Guilhem Moulin <guilhem@fripost.org>
#
# 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 <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------

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
    exec {fd}<"$DEVICE_PATH"
    "$@" >"$DEVICE_PATH"
    while IFS= read -r -u "$fd" x; do
        x="${x%$'\r'}"
        [ "$x" != "." ] || break
        printf "%s\\n" "$x"
    done
    {fd}<&-
}

serial_send palette
serial_send colormap