-
Notifications
You must be signed in to change notification settings - Fork 6
/
adscompiler.py
228 lines (181 loc) · 5.34 KB
/
adscompiler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
# Script vars
ads_data = []
ads_consts = []
# Compiler vars
labels = {}
jumps = {}
# System vars
sysvars = {
"caps": 0,
"num": 1,
"scroll": 2
}
# Keycodes
keyCodes = {
" ": "KEY_SPACE",
"enter": "KEY_ENTER",
"space": "KEY_SPACE",
"ctrl": "KEY_LEFT_CTRL",
"shift": "KEY_LEFT_SHIFT",
"alt": "KEY_LEFT_ALT",
"gui": "KEY_LEFT_GUI",
"right": "KEY_RIGHT_ARROW",
"left": "KEY_LEFT_ARROW",
"down": "KEY_DOWN_ARROW",
"up": "KEY_UP_ARROW",
"esc": "KEY_ESC",
"back": "KEY_BACKSPACE",
"tab": "KEY_TAB",
"ins": "KEY_INSERT",
"home": "KEY_HOME",
"pgup": "KEY_PAGE_UP",
"del": "KEY_DELETE",
"end": "KEY_END",
"pgdn": "KEY_PAGE_DOWN",
"caps": "KEY_CAPS_LOCK",
"f1": "KEY_F1",
"f2": "KEY_F2",
"f3": "KEY_F3",
"f4": "KEY_F4",
"f5": "KEY_F5",
"f6": "KEY_F6",
"f7": "KEY_F7",
"f8": "KEY_F8",
"f9": "KEY_F9",
"f10": "KEY_F10",
"f11": "KEY_F11",
"f12": "KEY_F12"
}
# Functions
def keyCode(key, warn = False):
if key in keyCodes:
return keyCodes[key]
elif (key >= 'A' and key <= 'Z') or\
(key >= 'a' and key <= 'z') or\
(key >= '0' and key <= '9'):
return "KEY_" + key.upper()
else:
if warn:
print("]]] WARNING: Unknown key '" + key + "'")
return 0
def pushConst(num):
# Push number into constants if it doesn't exist already
if not num in ads_consts:
ads_consts.append(num)
idx = ads_consts.index(num)
if idx > 255:
print("ERROR: Too many constants!")
exit()
return idx
# Check args
if len(sys.argv) != 2:
print("Usage: adscompiler.py <script>")
exit()
# Open ardoducky script
inf = open(sys.argv[1], "r")
# Read lines
for line in inf:
# Remove newlines
line = line.replace("\n", "").replace("\r", "")
# Line too short?
if len(line) < 1:
continue
# Split
f = line.find(' ')
if f < 0:
cmd = line
arg = ""
else:
cmd = line[:f]
arg = line[f + 1:]
# Write string
if cmd == "write":
for c in arg:
kc = keyCode(c)
if kc == 0:
ads_data.append(5) # Alt code
ads_data.append("'" + c.replace('\\', '\\\\') + "'")
else:
# If uppercase
if c != c.lower():
ads_data.append(3) # Hold
ads_data.append(keyCode("shift", True))
ads_data.append(1) # Press key
ads_data.append(kc)
# If uppercase
if c != c.lower():
ads_data.append(4) # Release
ads_data.append(keyCode("shift", True))
# Wait
elif cmd == "wait":
# Push command
ads_data.append(2) # Wait
ads_data.append(pushConst(int(arg))) # Const index
# Press key
elif cmd == "press":
keys = arg.split('+')
for key in keys[:-1]:
ads_data.append(3) # Hold key
ads_data.append(keyCode(key, True)) # Keycode
ads_data.append(1) # Press key
ads_data.append(keyCode(keys[-1], True)) # Keycode
for key in reversed(keys[:-1]):
ads_data.append(4) # Release key
ads_data.append(keyCode(key, True)) # Keycode
# Label
elif cmd[0] == ':':
lbl = cmd[1:]
if lbl in labels:
print("ERROR: Label '" + lbl + "' already exists!")
exit()
labels[lbl] = len(ads_data)
# Goto label
elif cmd == "goto":
# Don't put in the real jump. We're not sure the label exist yet.
ads_data.append(6) # Jump
jumps[len(ads_data)] = arg
ads_data.append(0) # Dummy target
# Jump if sysvar is true
elif cmd == "ifj":
args = arg.split(' ');
var = args[0]
lbl = args[1]
if var[0] != "$":
print("ERROR: '" + var + "' is not a valid variable!")
exit()
if not var[1:] in sysvars:
print("ERROR: Sysvar '" + var[1:] + "' not found!")
exit()
ads_data.append(7) # Jump if sysvar true
ads_data.append(sysvars[var[1:]])
# Don't put in the real jump. We're not sure the label exist yet.
jumps[len(ads_data)] = lbl
ads_data.append(0) # Dummy target
# Unknown
else:
print("]]] WARNING: Unhandled command '" + cmd + "'")
# Close input file
inf.close()
# Handle jumps
for i in jumps:
if not jumps[i] in labels:
print("ERROR: Label '" + jumps[i] + "' doesn't exist!")
exit()
ads_data[i] = pushConst(labels[jumps[i]])
# Turn data into strings
for i in range(len(ads_data)):
ads_data[i] = str(ads_data[i])
for i in range(len(ads_consts)):
ads_consts[i] = str(ads_consts[i])
# Create header
outsrc = "#include <avr/pgmspace.h>\n\n";
outsrc += "const uint8_t ads_data[] PROGMEM = { " + ", ".join(ads_data) + " };\n"
outsrc += "const int ads_len = " + str(len(ads_data)) + ";\n"
outsrc += "const int ads_consts[] = { " + ", ".join(ads_consts) + " };\n"
# Write file
outf = open("script.h", "w")
outf.write(outsrc)
outf.close()
# Success!
print("Wrote to 'script.h' successfully!")