forked from caru/StenoFW
-
Notifications
You must be signed in to change notification settings - Fork 2
/
genHeader.py
executable file
·89 lines (71 loc) · 2.95 KB
/
genHeader.py
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
#!/usr/bin/python3
import sys
#board = __import__("Stenoboard")
board = __import__(sys.argv[1])
from gemini import *
from txbolt import *
ROWS = len(board.qwertyMapping)
COLS = len(board.qwertyMapping[0])
def myrepr(c):
if c=="'": return r"'\''"
return repr(c)
print ('// this is the StenoFW header for the %s.' % board.boardName)
print (' ')
for sym in board.defines:
print ('#define %s'% sym)
print (' ')
print ('#define ROWS %d' % ROWS)
print ('#define COLS %d' % COLS)
print ('#define ledPin %d' % board.ledPin)
print (' ')
print ('int rowPins[ROWS] = {', ', '.join([str(pin) for pin in board.rowPins]), '};')
print ('int colPins[COLS] = {', ', '.join([str(pin) for pin in board.colPins]), '};')
print (' ')
print ('char qwertyMapping[ROWS][COLS] = {')
print (' {', '},\n {'.join([', '.join([myrepr(c) for c in row]) for row in board.qwertyMapping]), '}\n};')
print (' ')
print ('/* this is for reference only -- the Fn keys are not valid C characters.')
print ('char stenoKeys[ROWS][COLS] = {')
print (' {', '},\n { '.join([', '.join([myrepr(c) for c in row]) for row in board.stenoKeys]), '}\n};')
print ('*/')
print (' ')
geminiBytes = [ [geminiMap.get(stenoChar,(None,None))[0] for stenoChar in row] for row in board.stenoKeys ]
geminiBits = [ [geminiMap.get(stenoChar,(None,None))[1] for stenoChar in row] for row in board.stenoKeys ]
def intRepr(b):
if b==None: return str(0)
else: return str(b)
print ('byte geminiBytes[ROWS][COLS] = {')
print (' {', '},\n {'.join([', '.join([intRepr(b) for b in row]) for row in geminiBytes]), '}\n};')
print (' ')
def bitRepr(b):
if b==None: return "B00000000"
else: return 'B' + "{0:08b}".format(1<<b)
print ('byte geminiBits[ROWS][COLS] = {')
print (' {', '},\n {'.join([', '.join([bitRepr(b) for b in row]) for row in geminiBits]), '}\n};')
print (' ')
txboltBytes = [ [txboltMap.get(stenoChar,(None,None))[0] for stenoChar in row] for row in board.stenoKeys ]
txboltBits = [ [txboltMap.get(stenoChar,(None,None))[1] for stenoChar in row] for row in board.stenoKeys ]
print ('byte txboltBytes[ROWS][COLS] = {')
print (' {', '},\n {'.join([', '.join([intRepr(b) for b in row]) for row in txboltBytes]), '}\n};')
print (' ')
print ('byte txboltBits[ROWS][COLS] = {')
print (' {', '},\n {'.join([', '.join([bitRepr(b) for b in row]) for row in txboltBits]), '}\n};')
print (' ')
# generate legitimate C identifiers for key names
def keyID(s):
if s==None: return None
if s=='#': return "Key_Num"
if s=='*': return "Key_Asterisk"
if s[0]=='-': return "Key__"+s[1]
return "Key_"+s
keydefs = {}
for (i,row) in enumerate(board.stenoKeys):
for (j,k) in enumerate(row):
if k:
entry = keydefs.setdefault(k, [])
entry.append((i,j))
def keytest(l):
asStrings = ["currentChord[%d][%d]" % (i,j) for (i,j) in l]
return "(%s)" % " || ".join(asStrings)
for k in sorted(keydefs):
print ("#define %s %s" % (keyID(k), keytest(keydefs[k])))