-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program
182 lines (162 loc) · 5.02 KB
/
Program
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
###############################################
# Minecraft Gesture Controller
#
#Written by <[email protected]>
# MIT License V.asof2018
# Also coffee/beer ware license :)
#Super awesome thanks to:
# Richard Albritton, Tony DiCola, John Parker
# All the awesome people who wrote the libraries
################################################
##########################
## Libraries ##
##########################
import touchio
import board
import busio
import time
#Libraries for HID Keyboard & Mouse
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.mouse import Mouse
#Libraries for accelerometer
import adafruit_lis3dh
#######################
# CPX Setup #
#######################
pin1 = board.A1
pin2 = board.A2
pin3 = board.A3
pin4 = board.A4
############################
# Keyboard & Mouse Setup #
############################
# The keyboard object!
kbd = Keyboard()
# we're americans :)
layout = KeyboardLayoutUS(kbd)
#The mouse object!
mouse = Mouse()
#######################
# Accelerometer Setup #
#######################
#Initialize Accelerometer
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=25)
# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
lis3dh.range = adafruit_lis3dh.RANGE_8_G
###########################
## Controller Functions ##
###########################
def Map(x, in_min, in_max, out_min, out_max):
return int((x - in_min)*(out_max - out_min) / (in_max - in_min) + out_min)
def Move(upDown_axis, isBackward):
axis_new = abs(upDown_axis)
#If you are touching A4, walk backwards, else walk forwards
if isBackward:
print('backwards') #Debugging
if (axis_new > 1.2): #walk threshold
if(axis_new > 2.5): #run threshold
kbd.press(Keycode.LEFT_CONTROL, Keycode.S)
time.sleep(0.1)
kbd.release_all()
else:
kbd.press(Keycode.S)
time.sleep(0.1)
kbd.release_all()
else:
if (axis_new > 1.2): #walk threshold
if(axis_new > 2.5): #run threshold
kbd.press(Keycode.LEFT_CONTROL)
time.sleep(0.1)
kbd.release_all()
else:
kbd.press(Keycode.W)
time.sleep(0.1)
kbd.release_all()
def Turn(upDown_axis, frontBack_axis, leftRight_axis, lookUp):
leftRight_adj = int(leftRight_axis) #currently z_axis
upDown_adj = int(upDown_axis) #currently y_axis
leftRight_new = Map(leftRight_adj, -3, 3, -100, 100)
if(lookUp and abs(upDown_adj) < 1.2):
upDown_new = Map(upDown_adj, -1, 1, -100, 100)
else:
upDown_new = 0
if abs(leftRight_new) < 127:
mouse.move(-leftRight_new, upDown_new)
else:
mouse.move(0, 0)
def Jump(upDown_axis):
upDown = abs(upDown_axis)
if(upDown > 3):
kbd.press(Keycode.SPACE, Keycode.W)
kbd.release_all()
def Give(upDown_axis, frontBack_axis):
frontBack_new = abs(frontBack_axis)
if abs(upDown_axis) < 1:
if(frontBack_new > 2):
print('give')
mouse.click(Mouse.RIGHT_BUTTON)
mouse.release_all()
def Attack():
print('attack')
mouse.click(Mouse.LEFT_BUTTON)
time.sleep(0.1)
mouse.release_all()
def Inventory():
print('inventory') #Debugging -- view in serial monitor
kbd.press(Keycode.E)
time.sleep(0.01)
kbd.release_all()
def ESC():
print('ESC') #Debugging -- view in serial monitor
kbd.press(Keycode.ESCAPE)
time.sleep(0.01)
kbd.release_all()
def touch(pin):
touch = touchio.TouchIn(pin)
touch.threshold = 2000
rawValue = touch.raw_value
if touch.value:
touchTrig = True
else:
touchTrig = False
return touchTrig
touch.deinit()
def readAxes():
x_axis, y_axis, z_axis = lis3dh.acceleration
x = x_axis / 9.806
y = y_axis / 9.806
z = z_axis / 9.806
time.sleep(0.01)
return x, y, z
###########################
## Main Function ##
###########################
def __main__():
while True:
# Read accelerometer values (in m / s ^ 2). Returns a 3-tuple of x, y, x axis
x, y, z = readAxes()
#Read finger pads and act accordingly
if touch(pin1):
Attack()
if touch(pin2):
Inventory()
if touch(pin3):
ESC()
isBackward = touch(pin4)
lookUp = touch(pin4)
#Run through the motions! .. literally :)
Move(y, isBackward)
Turn(y, x, z, lookUp)
Jump(y)
Give(y,x)
# Small delay to keep things responsive but give time for interrupt processing.
time.sleep(0.01)
## Debugging Ahead!! ##
#Use the following 2 lines to figure out which axis is upDown, frontBack, or LeftRight
# and also for debugging!
#print('x = {}G, y = {}G, z = {}G'.format(x, y, z))
#time.sleep(0.3)
__main__()