-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
147 lines (105 loc) · 3.48 KB
/
control.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
from machine import Pin, ADC, I2C
import utime
from libs import mma7660
xAxis: ADC
yAxis: ADC
buttonB: Pin
buttonA: Pin
buttonStart: Pin
buttonSelect: Pin
def x_value():
return xAxis.read_u16()
def y_value():
return yAxis.read_u16()
def button_a():
return buttonA.value()
def button_b():
return buttonB.value()
def button_start():
return buttonStart.value()
def button_select():
return buttonSelect.value()
class DelayEvent:
def __init__(self, time_threshold, callback_handle):
self.start_time = utime.ticks_ms()
self.time_threshold = time_threshold
self.callback_handle = callback_handle
def tick(self):
if utime.ticks_diff(utime.ticks_ms(), self.start_time) > self.time_threshold:
res = self.callback_handle()
self.start_time = utime.ticks_ms()
return res
return True
class JoystickEvent:
def __init__(self, stick_pos_handle, great_than: bool, pos_threshold: int,
timer_threshold: int, callback_handle):
self.counter = 0
self.stick_pos_handle = stick_pos_handle
self.stick_pos = self.stick_pos_handle()
self.pos_threshold = pos_threshold
self.great_than = great_than
self.delay_event = DelayEvent(timer_threshold, callback_handle)
def event(self):
self.stick_pos = self.stick_pos_handle()
trigger = False
if self.great_than:
if self.stick_pos > self.pos_threshold:
trigger = True
else:
if self.stick_pos < self.pos_threshold:
trigger = True
if trigger:
self.delay_event.tick()
class Joystick:
def __init__(self, *args):
self.controls = []
for arg in args:
self.controls.append(JoystickEvent(*arg))
def run(self):
for control in self.controls:
control.event()
class ButtonEvent:
def __init__(self, pin: Pin, trigger_event, time_threshold, callback_handle):
self.irq = pin.irq(self.event, trigger_event)
self.callback_handle = callback_handle
self.delay_event = DelayEvent(time_threshold, callback_handle)
def event(self, pin):
self.delay_event.tick()
class Button:
def __init__(self, *args):
self.buttons = []
for arg in args:
self.buttons.append(ButtonEvent(*arg))
class AccelEvent:
def __init__(self, get_data_handle, region: tuple, timer_threshold: int, callback_handle):
self.get_data_handle = get_data_handle
self.region = region
self.delay_event = DelayEvent(timer_threshold, callback_handle)
def run(self):
if self.region[0] <= self.get_data_handle() <= self.region[1]:
self.delay_event.tick()
class Accelerometer:
def __init__(self, i2c: I2C):
self.accel = mma7660.Accelerometer(i2c)
self.events = []
def set_events(self, *args):
for arg in args:
self.events.append(AccelEvent(*arg))
def run(self):
for event in self.events:
event.run()
class Controller:
joystick: Joystick
button: Button
accelerometer: Accelerometer
def __init__(self):
pass
def set_button(self, button: Button):
self.button = button
def set_joystick(self, joystick):
self.joystick = joystick
def set_accelerometer(self, accelerometer):
self.accelerometer = accelerometer
def run(self):
self.joystick.run()
self.accelerometer.run()