-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
319 lines (253 loc) · 8.62 KB
/
code.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import board
import digitalio
import time
import usb_hid
import random
import busio
import displayio
import terminalio
from adafruit_displayio import adafruit_displayio_sh1106
from adafruit_display_text import label
from adafruit_display_text import scrolling_label
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
MODE_INIT = "Start"
MODE_NONE = "None"
MODE_FORTNITE = "Fortnite"
MODE_TEAMS = "Teams"
SUBMODE_F_CIRCLE = "Fortnite Circle"
SUBMODE_F_FORWARD = "Fortnite Forward"
SUBMODE_F_RANDOM = "Fortnite Random" # not in use
SUBMODE_T_MIC = "Teams Microphone"
SUBMODE_T_VIDEO = "Teams Video"
keyboard = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
button1 = digitalio.DigitalInOut(board.GP0)
button1.direction = digitalio.Direction.INPUT
button1.pull = digitalio.Pull.UP
button2 = digitalio.DigitalInOut(board.GP1)
button2.direction = digitalio.Direction.INPUT
button2.pull = digitalio.Pull.UP
button3 = digitalio.DigitalInOut(board.GP2)
button3.direction = digitalio.Direction.INPUT
button3.pull = digitalio.Pull.UP
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
nextDirection = 0
ignoreButtonState = False
prevMode = MODE_INIT
currMode = MODE_NONE
prevSubMode = MODE_INIT
currSubMode = MODE_NONE
def showDebugInfo(blinkNo):
print(f'Mode: {currMode}')
print(f'Submode: {currSubMode}')
print(f'Button1: {button1.value}')
print(f'Button2: {button2.value}')
print(f'Button3: {button3.value}')
blink(blinkNo)
def blink(blinks):
ignoreButtonState = True
led.value = False
i = 0
while i < blinks:
led.value = True
time.sleep(0.1)
led.value = False
time.sleep(0.1)
i += 1
ignoreButtonState = False
def modeSwitch():
global sprite
if (currMode != prevMode) or (currSubMode != prevSubMode):
print(f'Switching to Mode {currMode} with Submode {currSubMode}.')
if currMode == MODE_FORTNITE:
sprite[0] = 4
sprite[1] = 5
sprite[2] = 3
elif currMode == MODE_TEAMS:
sprite[0] = 6
sprite[1] = 7
sprite[2] = 3
else:
sprite[0] = 1
sprite[1] = 2
sprite[2] = 3
def clearSubMode():
global prevSubMode
global currSubMode
prevSubMode = MODE_INIT
currSubMode = MODE_NONE
clearModeFinal()
def clearMainMode():
global currMode
clearSubMode()
currMode = MODE_INIT
clearModeFinal()
def clearModeFinal():
keyboard.release_all()
modeSwitch()
# Teams Microphone
def teamsSwitchMicrophone():
keyboard.release_all()
keyboard.send(Keycode.LEFT_CONTROL, Keycode.SHIFT, Keycode.M)
time.sleep(0.5)
# Teams Microphone
def teamsSwitchVideo():
keyboard.release_all()
keyboard.send(Keycode.LEFT_CONTROL, Keycode.SHIFT, Keycode.O)
time.sleep(0.5)
# Fortnite moves with cancel possibility
def moveAndWaitForCancel(sec):
for i in range(sec * 10):
time.sleep(sec / 100)
if button3.value:
showDebugInfo(3)
clearSubMode()
return
# Fortnite random movement. Not used yet.
def fortniteRandom():
keyboard.release_all()
nextDirection = random.randint(0, 8)
if nextDirection == 0:
print("do nothing")
elif nextDirection == 1:
print("go forward")
keyboard.press(Keycode.W)
elif nextDirection == 2:
print("go right")
keyboard.press(Keycode.D)
elif nextDirection == 3:
print("go back")
keyboard.press(Keycode.S)
elif nextDirection == 4:
print("go left")
keyboard.press(Keycode.A)
elif nextDirection == 5:
print("go left")
keyboard.press(Keycode.SPACEBAR)
elif nextDirection == 6:
print("emote")
keyboard.press(Keycode.B)
time.sleep(2)
keyboard.release(Keycode.B)
elif nextDirection == 7:
print("left click")
mouse.move(0,0,random.randint(1,10))
mouse.press(Mouse.LEFT_BUTTON)
time.sleep(2)
mouse.release(Mouse.LEFT_BUTTON)
elif nextDirection == 8:
print("mouse move")
mouse.move(random.randint(-500,500), random.randint(-50,50),0)
time.sleep(random.randint(2,8))
keyboard.release_all()
# Fortnite circle movement
def fortniteCircle():
keyboard.release_all()
sec = random.randint(2,8)
keyboard.press(Keycode.W)
moveAndWaitForCancel(sec)
keyboard.release_all()
keyboard.press(Keycode.D)
moveAndWaitForCancel(sec)
keyboard.release_all()
keyboard.press(Keycode.S)
moveAndWaitForCancel(sec)
keyboard.release_all()
keyboard.press(Keycode.A)
moveAndWaitForCancel(sec)
keyboard.release_all()
keyboard.release_all()
# Fortnite straight forward
def fortniteStraightForward():
keyboard.release_all()
keyboard.press(Keycode.W, Keycode.SHIFT)
moveAndWaitForCancel(5)
keyboard.release(Keycode.SHIFT)
keyboard.release_all()
time.sleep(1)
keyboard.press(Keycode.W, Keycode.SHIFT)
moveAndWaitForCancel(8)
keyboard.release(Keycode.SHIFT)
keyboard.release_all()
# Init Display
displayio.release_displays()
i2c = busio.I2C(scl=board.GP17, sda=board.GP16)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
WIDTH = 132
HEIGHT = 64
BORDER = 5
display = adafruit_displayio_sh1106.SH1106(display_bus, width=WIDTH, height=HEIGHT)
sprite_fname = "spritesheet.bmp"
sprite_cnt = 3*1
sprite_w,sprite_h = 40,40
sprite_sheet = displayio.OnDiskBitmap(open(sprite_fname, "rb"))
sprite_palette = sprite_sheet.pixel_shader
sprite_palette.make_transparent(0)
sprite = displayio.TileGrid(sprite_sheet, pixel_shader=sprite_palette,
width = 3, height = 1,
tile_width = sprite_w, tile_height = sprite_h)
maingroup = displayio.Group(scale=1)
maingroup.append(sprite)
display.show(maingroup)
sprite[0] = 1
sprite[1] = 2
sprite[2] = 3
maingroup.x = 6
maingroup.y = ((HEIGHT - sprite_h) // 2 - 1) + 3 # I added 3 because my display line 1 and 3 is broken. You can remove it.
# Start main loop
print("Wait for button press.")
while True:
prevSubMode = currSubMode
prevMode = currMode
if not ignoreButtonState:
led.value = False
if currMode == MODE_FORTNITE:
if button1.value:
showDebugInfo(1)
currSubMode = SUBMODE_F_CIRCLE
elif button2.value:
showDebugInfo(2)
currSubMode = SUBMODE_F_FORWARD
elif button3.value:
showDebugInfo(3)
clearMainMode()
elif currMode == MODE_TEAMS:
if button1.value:
showDebugInfo(1)
currSubMode = SUBMODE_T_MIC
elif button2.value:
showDebugInfo(2)
currSubMode = SUBMODE_T_VIDEO
elif button3.value:
showDebugInfo(3)
clearMainMode()
else: # main menu
if button1.value:
showDebugInfo(1)
currMode = MODE_FORTNITE
elif button2.value:
showDebugInfo(2)
currMode = MODE_TEAMS
elif button3.value:
showDebugInfo(3)
clearMainMode()
modeSwitch()
# Repeating SubMode runs
if currSubMode == SUBMODE_F_CIRCLE:
sprite[1] = 0
fortniteCircle()
elif currSubMode == SUBMODE_F_FORWARD:
sprite[0] = 0
fortniteStraightForward()
elif currSubMode == SUBMODE_T_MIC:
sprite[0] = 8
teamsSwitchMicrophone()
clearSubMode()
elif currSubMode == SUBMODE_T_VIDEO:
sprite[1] = 8
teamsSwitchVideo()
clearSubMode()
time.sleep(0.1)