swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 1 | # Generate scan_codes.h from scan_codes.tsv |
| 2 | |
| 3 | from functools import reduce |
| 4 | |
| 5 | def symbolify(s): |
| 6 | if not s.startswith("'"): |
| 7 | return s.replace('+', 'PLUS').replace('*', 'TIMES').replace('/', 'DIV').replace('-', 'MINUS').replace('.', 'DOT') |
| 8 | else: return s |
| 9 | |
| 10 | def gen_set_entry(set, line): |
| 11 | parts = line.split('\t') |
| 12 | |
| 13 | make_break = parts[set] |
| 14 | [make, brk] = [[int(y, 16) for y in x.split(' ')] for x in make_break.replace('(base)', '').strip().split('/')] |
| 15 | brk_pfx = brk[0] |
| 16 | if not set == 1: |
| 17 | brk = brk[1:] |
| 18 | |
| 19 | if make[0] == 0xe0 or make[0] == 0xe1: |
| 20 | return |
| 21 | |
| 22 | [make, brk] = [reduce(lambda o, n: o << 8 | n, x, 0) for x in [make, brk]] |
| 23 | |
| 24 | ascii = str(parts[6] == 'yes').lower() |
| 25 | symbol = symbolify(parts[7]) |
| 26 | upper_symbol = symbolify(parts[8]) |
| 27 | |
| 28 | print(f"""\t[{make}] = {'{'} .ascii = {ascii}, .symbol = {symbol}, |
| 29 | \t\t.up_symbol = {upper_symbol}, .prefix = {brk_pfx} {'}'},""") |
| 30 | |
| 31 | if set == 1: |
| 32 | print(f"""\t[{brk}] = {'{'} .ascii = {ascii}, .symbol = {symbol}, |
| 33 | \t\t.up_symbol = {upper_symbol}, .prefix = {brk_pfx}, .brk = true {'}'},""") |
| 34 | |
| 35 | def gen_set(lines, set): |
| 36 | # NOTE will only work with scan code set 3 |
| 37 | print("const struct kbd_scan_codes scan_code_set_" + str(set) + "[0xff] =\n{") |
| 38 | for l in lines: |
| 39 | gen_set_entry(set, l) |
| 40 | print("};") |
| 41 | |
| 42 | with open('scan_codes.tsv', 'r') as f: |
| 43 | lines = f.read().split('\n')[1:] |
| 44 | print('#include "kbd.h"\n\n') |
| 45 | gen_set(lines, 1) |
| 46 | print('\n') |
| 47 | gen_set(lines, 3) |
| 48 | |