-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhal.py
More file actions
309 lines (252 loc) · 10.1 KB
/
hal.py
File metadata and controls
309 lines (252 loc) · 10.1 KB
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
'''
Hardware Abstraction Layer for Mice Touchscreen Project
'''
import time
import logging
import gpiozero
from arch import isRaspberryPI
logger = logging.getLogger('touchHAL')
'''
gpio pins used by different hardware
audio:
buzzer (flexible): 12 (+), GND (-)
speakers (fixed): (3.5 mm audio jack)
audiohat (fixed): 2 (i2c sda), 3 (i2c scl), 25 (led), 19 (i2s lrclk), 18 (i2s clk), 20 (i2s adc), 21 (i2s dac), 23 (button)
sensors:
adafruit/sparkfun (flexible): 3.3V, GND, 4 (input)
battery:
UPSV3P2 (fixed/flexible): 5V, GND, 8 (UART txd), 10 (uart rxd) / 17 (shutdown input)
liquid reward:
valve/pump (flexible): 5V, GND, 26 (output)
food reward:
(flexible): 16 (output)
'''
########################################
## Sound
########################################
from sound import SparkFunBuzzer, CustomSpeaker
class Sound():
__instance = None
__variant = None
__items = [('No Audio', 'None'),('Buzzer', 'spkfbuzzer'),('Speaker','custspk')]
def __init__(self, variant=None):
'''
if there is no instance create one.
if no variant specified or using PC, use a dummy
if already is an instance and specifying a new variant
(only should happen during hardware configuration)
close old instance and create the new one.
'''
if Sound.__instance and variant is not None:
logger.debug('Closing Sound instance {}'.format(Sound.__variant))
Sound.__instance._close()
Sound.__instance = None
Sound.__variant = None
if Sound.__instance is None:
if variant is not None:
if variant == 'spkfbuzzer':
Sound.__instance = SparkFunBuzzer()
elif variant == 'custspk':
Sound.__instance = CustomSpeaker()
else:
raise ValueError('Audio device "{}" not recognized'.format(variant))
Sound.__variant = variant
else:
raise ValueError('Audio device not specified')
logger.debug('New sound instance {}'.format(Sound.__variant))
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
def get_type(self):
return self.__variant
def get_items():
return Sound.__items
########################################
## IR Reward Sensor
########################################
from sensor import AdafruitSensor, SparkfunCustomIrSensor
class IRSensor(object):
__instance = None
__variant = None
__items = [('No Sensor', 'None'),('AdaFruit', 'adafruit'),('SparkfunCustom','sparkfuncustom')]
def __init__(self,handler_in=None,handler_out=None,variant=None):
'''
if there is already an instance and specifying a (new) variant (only should happen during hardware configuration) close old instance and create the new one.
if there is no instance create one.
if no variant specified or using PC, use a dummy
if no variant specified and there is already an instance, just set or release handler
'''
if IRSensor.__instance and variant is not None:
logger.debug('Closing IRSensor instance {}'.format(IRSensor.__variant))
IRSensor.__instance._close()
IRSensor.__instance = None
IRSensor.__variant = None
if IRSensor.__instance is None:
if variant is not None:
if variant == 'adafruit':
IRSensor.__instance=AdafruitSensor(handler_in,handler_out)
elif variant == 'sparkfuncustom':
IRSensor.__instance=SparkfunCustomIrSensor(handler_in,handler_out)
else:
raise ValueError('Sensor device "{}" not recognized'.format(variant))
IRSensor.__variant = variant
else:
raise ValueError('Sensor device not specified')
logger.debug('New IRSensor instance {}'.format(IRSensor.__variant))
else:
if handler_in is not None:
IRSensor.__instance.set_handler_in(handler_in)
else:
IRSensor.__instance.release_handler_out()
if handler_out is not None:
IRSensor.__instance.set_handler_out(handler_out)
else:
IRSensor.__instance.release_handler_out()
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
def is_activated(self):
if IRSensor.__instance:
return IRSensor.__instance.is_activated()
else:
return False
def get_type(self):
return self.__variant
def get_items():
return IRSensor.__items
########################################
## Liquid Reward
########################################
from liqrew import LeeValve, LeePump
class LiqReward(object):
__instance = None
__variant = None
__items = [('No Liquid Reward', 'None'),('Valve', 'leevalve'),('Pump','leepump')]
def __init__(self, drop_amount: int = 1, variant: str = None):
''' if there is no instance create one.
if no variant specified or using PC, use a dummy
if already is an instance and specifying a new variant
(only should happen during hardware configuration)
close old instance and create the new one.
'''
if LiqReward.__instance and variant is not None:
logger.debug('Closing Liquid Reward instance {}'.format(LiqReward.__variant))
LiqReward.__instance._close()
LiqReward.__instance = None
LiqReward.__variant = None
if LiqReward.__instance is None:
if variant is not None:
if variant == 'leevalve':
LiqReward.__instance = LeeValve(drop_amount)
elif variant == 'leepump':
LiqReward.__instance = LeePump(drop_amount)
else:
raise ValueError('Liquid Reward Device "{}" not recognized'.format(variant))
LiqReward.__variant = variant
else:
raise ValueError('Liquid Reward Variant needs to be specified')
logger.debug('New Liquid Reward instance {}'.format(LiqReward.__variant))
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
def __str__(self):
if LiqReward.__instance:
return self.__instance.__str__()
else:
return 'No liqreward instance'
def set_drop_amount(self, drop_amount: int):
if LiqReward.__instance:
logger.info('Liquid Reward drop amount {:d}'.format(drop_amount))
return LiqReward.__instance.set_drop_amount(drop_amount)
def get_drop_amount(self):
if LiqReward.__instance:
return LiqReward.__instance.get_drop_amount()
def is_open(self):
if LiqReward.__instance:
return LiqReward.__instance.is_open()
def open(self):
if LiqReward.__instance:
logger.info('Valve open')
return LiqReward.__instance.open()
def close(self):
if LiqReward.__instance:
logger.info('Valve closed')
return LiqReward.__instance.close()
def drop(self, N=None):
if LiqReward.__instance:
logger.info('Valve drop amount {}'.format(repr(LiqReward.__instance)))
return LiqReward.__instance.drop(N)
def get_type(self):
return LiqReward.__variant
def get_items():
return LiqReward.__items
############
# Battery
############
from battery import UPSV3P2Bat
class Battery(object):
__instance = None
__variant = None
def __init__(self, variant = None):
if Battery.__instance is None:
Battery.__instance = UPSV3P2Bat()
logger.debug('New battery instance created')
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
def disconnect(self):
if Battery.__instance:
Battery.__instance._close()
Battery.__instance = None
if __name__=='__main__':
logging.basicConfig(filename='logs/haltest.log', filemode='w+', level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d@@%(name)s@@%(levelname)s@@%(message)s',
datefmt='%Y/%m/%d||%H:%M:%S'
)
logger.info('HAL Test')
val = LiqReward()
val.open()
time.sleep(.5)
val.close()
val.setOpenTime(.2)
val.drop()
buzz = Sound()
buzz.play(440,0.5)
tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2),
('Eb4', 0.2), ('E4', 0.2), (None, 0.6),
('F#4', 0.2), ('G4', 0.2), (None, 0.6),
('Eb4', 0.2), ('E4', 0.2), (None, 0.2),
('F#4', 0.2), ('G4', 0.2), (None, 0.2),
('C4', 0.2), ('B4', 0.2), (None, 0.2),
('F#4', 0.2), ('G4', 0.2), (None, 0.2),
('B4', 0.2), ('Bb4', 0.5), (None, 0.6),
('A4', 0.2), ('G4', 0.2), ('E4', 0.2),
('D4', 0.2), ('E4', 0.2)]
buzz.playTune(tune)
import pygame
pygame.init()
screen = pygame.display.set_mode((300,300))
time.sleep(1)
def testHandler():
print('IR handler')
logger.info('testing handler')
irbeam = IRSensor()
irbeam.set_handler(testHandler)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()