-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_guitar_python.py
112 lines (76 loc) · 2.65 KB
/
arduino_guitar_python.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
#dependencies:
# !pip install pyserial
# !pip install pynput
import threading
import time
import serial
import sys
import numpy as np
import time
from threading import Thread
from pynput.keyboard import Key, Controller
# sudo chmod 666 /dev/ttys0
NUM_BUTTONS = 9
pressed = np.zeros(NUM_BUTTONS, dtype=bool)
#TIME = np.zeros(NUM_BUTTONS);
RUNNING = True
ser = serial.Serial('COM6', baudrate = 115200, timeout = 20) # Replace the port here with Arduino's one
keyboard = Controller()
ENABLE_AUTO_WHAMMY = False
def autowhammy():
keyboard2 = Controller()
FRETS = [0, 1, 2, 3, 4] # button idsgt related to frets
WHAMMY_TASK_TIME = 100 / 1000; #s
WHAMMY_KEY = "w"
WHAMMY_PRESSED = False
while True:
if(WHAMMY_PRESSED):
WHAMMY_PRESSED = False
keyboard2.release(WHAMMY_KEY)
elif(np.any(pressed[FRETS])):
keyboard2.press(WHAMMY_KEY)
WHAMMY_PRESSED = True
time.sleep(WHAMMY_TASK_TIME)
if(ENABLE_AUTO_WHAMMY):
autowhammy_thread = Thread(target=autowhammy, args=())
autowhammy_thread.daemon = True
autowhammy_thread.start()
while True:
try:
code = ser.read(3)
if(len(code) > 0): # sanety check implemented
if(code[2] & (~(code[0] | code[1]))):
for i in range(NUM_BUTTONS):
ispressed = 0
if(i < 8):
ispressed = code[0] & 1 << i
else:
ispressed = code[1] & 1 << (i - 8)
if(ispressed and not pressed[i]):
pressed[i] = 1;
keyboard.press(str(i));
# TIME[i] = time.time()
elif(pressed[i] and not ispressed):
pressed[i] = 0;
keyboard.release(str(i));
# delay = (time.time() - TIME[i]) * 1000
# print(delay)
else:
print('Warning: Invalid serial code')
print(code[0])
print(code[1])
print(code[2])
else:
print('Warning: empty serial code')
except KeyboardInterrupt:
ser.close()
print('Interrupt')
RUNNING = False
if(ENABLE_AUTO_WHAMMY):
autowhammy_thread.stop()
break
except:
ser.close()
RUNNING = False
if(ENABLE_AUTO_WHAMMY):
autowhammy_thread.stop()