forked from KenT2/pipresents-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp_gpio.py
479 lines (397 loc) · 17.3 KB
/
pp_gpio.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import time
import datetime
import copy
from Tkinter import *
import Tkinter as tk
import os
import ConfigParser
from pp_utils import Monitor
from pp_options import command_options
class PPIO:
"""
PPIO provides some IO facilties for Pi presents
- configures GPIO pins from data in gpio.cfg
- reads and debounces inputs pins, provides callbacks on state changes which are used to trigger mediashows
- for output pins allows players to put events, which request the change of state of pins, into a queue. Events are executed at the required time.
"""
# constants for buttons
# cofiguration from gpio.cfg
PIN=0 # pin on RPi board GPIO connector e.g. P1-11
DIRECTION = 1 # IN/OUT/NONE (None is not used)
NAME = 2 # name for output
RISING_NAME=3 # name for rising edge callback
FALLING_NAME=4 # name ofr falling edge callback
ONE_NAME=5 # name for one state callback
ZERO_NAME = 6 # name for zero state callback
REPEAT = 7 #reperat interval for state callbacks (mS)
THRESHOLD = 8 # threshold of debounce count for state change to be considered
PULL = 9 # pull up or down or none
# dynamic data
COUNT=10 # variable - count of the number of times the input has been 0 (limited to threshold)
PRESSED = 11 # variable - debounced state
LAST = 12 # varible - last state - used to detect edge
REPEAT_COUNT = 13
TEMPLATE = ['', #pin
'', # direction
'', #name
'','','','', #input names
0, # repeat
0, #threshold
'', #pull
0,False,False,0] #dynamics
PINLIST = ('P1-03','P1-05','P1-07','P1-08',
'P1-10','P1-11','P1-12','P1-13','P1-15','P1-16','P1-18','P1-19',
'P1-21','P1-22','P1-23','P1-24','P1-26')
# index of shutdown pin
SHUTDOWN_INDEX=0
# constants for sequencer
SEQUENCER_PIN = 0 # GPIO pin number, the xx in P1-xx
SEQUENCER_TO_STATE = 1 # False = off , True =on
SEQUENCER_TIME = 2 # time since the epoch in seconds
SEQUENCER_TAG = 3 # tag used to delete all matching event, usually a track reference.
# CLASS VARIABLES
events=[]
pins=[]
last_poll_time=0
options=None
# gpio_enabled=False
EVENT_TEMPLATE=[0,False,0,None]
#executed by main program and by each object using gpio
def __init__(self):
self.mon=Monitor()
self.mon.on()
self.options=command_options()
# executed once from main program
def init(self,pp_dir,pp_home,pp_profile,widget,button_tick,callback=None):
# instantiate arguments
self.widget=widget
self.pp_dir=pp_dir
self.pp_profile=pp_profile
self.pp_home=pp_home
self.button_tick=button_tick
self.callback=callback
PPIO.SHUTDOWN_INDEX=0
# read gpio.cfg file.
if self.read(self.pp_dir,self.pp_home,self.pp_profile)==False:
return False
import RPi.GPIO as GPIO
self.GPIO = GPIO
#construct the GPIO control list from the configuration
for index, pin_def in enumerate(PPIO.PINLIST):
pin=copy.deepcopy(PPIO.TEMPLATE)
pin_bits = pin_def.split('-')
pin_num=pin_bits[1:]
pin[PPIO.PIN]=int(pin_num[0])
if self.config.has_section(pin_def)==False:
self.mon.log(self, "no pin definition for "+ pin_def)
pin[PPIO.DIRECTION]='None'
else:
# unused pin
if self.config.get(pin_def,'direction')=='none':
pin[PPIO.DIRECTION]='none'
else:
pin[PPIO.DIRECTION]=self.config.get(pin_def,'direction')
if pin[PPIO.DIRECTION]=='in':
# input pin
pin[PPIO.RISING_NAME]=self.config.get(pin_def,'rising-name')
pin[PPIO.FALLING_NAME]=self.config.get(pin_def,'falling-name')
pin[PPIO.ONE_NAME]=self.config.get(pin_def,'one-name')
pin[PPIO.ZERO_NAME]=self.config.get(pin_def,'zero-name')
if pin[PPIO.FALLING_NAME]=='pp-shutdown':
PPIO.SHUTDOWN_INDEX=index
if self.config.get(pin_def,'repeat')<>'':
pin[PPIO.REPEAT]=int(self.config.get(pin_def,'repeat'))
else:
pin[PPIO.REPEAT]=-1
pin[PPIO.THRESHOLD]=int(self.config.get(pin_def,'threshold'))
if self.config.get(pin_def,'pull-up-down')=='up':
pin[PPIO.PULL]=GPIO.PUD_UP
elif self.config.get(pin_def,'pull-up-down')=='down':
pin[PPIO.PULL]=GPIO.PUD_DOWN
else:
pin[PPIO.PULL]=GPIO.PUD_OFF
else:
# output pin
pin[PPIO.NAME]=self.config.get(pin_def,'name')
# print pin
PPIO.pins.append(copy.deepcopy(pin))
# setup GPIO
self.GPIO.setwarnings(False)
self.GPIO.setmode(self.GPIO.BOARD)
# set up the GPIO inputs and outputs
for index, pin in enumerate(PPIO.pins):
num = pin[PPIO.PIN]
if pin[PPIO.DIRECTION]=='in':
self.GPIO.setup(num,self.GPIO.IN,pull_up_down=pin[PPIO.PULL])
elif pin[PPIO.DIRECTION]=='out':
self.GPIO.setup(num,self.GPIO.OUT)
self.GPIO.setup(num,False)
self.reset_inputs()
PPIO.gpio_enabled=True
#init timer
self.button_tick_timer=None
PPIO.last_scheduler_time=long(time.time())
return True
# called by main program only
def poll(self):
# look at the buttons
self.do_buttons()
# kick off output pin sequencer
poll_time=long(time.time())
# is current time greater than last time the sceduler was run (previous second or more)
# run in a loop to catch up because root.after can get behind when images are being rendered etc.
while PPIO.last_scheduler_time<=poll_time:
self.do_sequencer(PPIO.last_scheduler_time)
PPIO.last_scheduler_time +=1
# and loop
self.button_tick_timer=self.widget.after(self.button_tick,self.poll)
# called by main program only
def terminate(self):
if self.button_tick_timer<>None:
self.widget.after_cancel(self.button_tick_timer)
self.clear_events_list(None)
self.reset_outputs()
self.GPIO.cleanup()
# ************************************************
# gpio input functions
# called by main program only
# ************************************************
def reset_inputs(self):
for pin in PPIO.pins:
pin[PPIO.COUNT]=0
pin[PPIO.PRESSED]==False
pin[PPIO.LAST]==False
pin[PPIO.REPEAT_COUNT]=pin[PPIO.REPEAT]
# index is of the pins array, provided by the callback ***** needs to be name
def shutdown_pressed(self):
if PPIO.SHUTDOWN_INDEX<>0:
return PPIO.pins[PPIO.SHUTDOWN_INDEX][PPIO.PRESSED]
else:
return False
def do_buttons(self):
for index, pin in enumerate(PPIO.pins):
if pin[PPIO.DIRECTION]=='in':
# debounce
if self.GPIO.input(pin[PPIO.PIN])==0:
if pin[PPIO.COUNT]<pin[PPIO.THRESHOLD]:
pin[PPIO.COUNT]+=1
if pin[PPIO.COUNT]==pin[PPIO.THRESHOLD]:
pin[PPIO.PRESSED]=True
else: # input us 1
if pin[PPIO.COUNT]>0:
pin[PPIO.COUNT]-=1
if pin[PPIO.COUNT]==0:
pin[PPIO.PRESSED]=False
#detect edges
# falling edge
if pin[PPIO.PRESSED]==True and pin[PPIO.LAST]==False:
pin[PPIO.LAST]=pin[PPIO.PRESSED]
pin[PPIO.REPEAT_COUNT]=pin[PPIO.REPEAT]
if pin[PPIO.FALLING_NAME]<>'' and self.callback <> None:
self.callback(index, pin[PPIO.FALLING_NAME],"falling")
#rising edge
if pin[PPIO.PRESSED]==False and pin[PPIO.LAST]==True:
pin[PPIO.LAST]=pin[PPIO.PRESSED]
pin[PPIO.REPEAT_COUNT]=pin[PPIO.REPEAT]
if pin[PPIO.RISING_NAME]<>'' and self.callback <> None:
self.callback(index, pin[PPIO.RISING_NAME],"rising")
# do state callbacks
if pin[PPIO.REPEAT_COUNT]==0:
if pin[PPIO.ZERO_NAME]<>'' and pin[PPIO.PRESSED]==True and self.callback<>None:
self.callback(index, pin[PPIO.ZERO_NAME],"zero")
if pin[PPIO.ONE_NAME]<>'' and pin[PPIO.PRESSED]==False and self.callback<>None:
self.callback(index, pin[PPIO.ONE_NAME],"zero")
pin[PPIO.REPEAT_COUNT]=pin[PPIO.REPEAT]
else:
if pin[PPIO.REPEAT]<>-1:
pin[PPIO.REPEAT_COUNT]-=1
# ************************************************
# gpio output sequencer functions
# ************************************************
# execute events at the appropriate time and remove from list (runs from main program only)
# runs through list a number of times because of problems with pop messing up list
def do_sequencer(self,schedule_time):
# print 'sequencer run for: ' + str(schedule_time) + ' at ' + str(long(time.time()))
while True:
event_found=False
for index, item in enumerate(PPIO.events):
if item[PPIO.SEQUENCER_TIME]<=schedule_time:
event=PPIO.events.pop(index)
event_found=True
self.do_event(event[PPIO.SEQUENCER_PIN],event[PPIO.SEQUENCER_TO_STATE],item[PPIO.SEQUENCER_TIME])
break
if event_found==False: break
# execute an event
def do_event(self,pin,to_state,req_time):
self.mon.log (self,'pin P1-'+ str(pin)+ ' set '+ str(to_state) + ' required: ' + str(req_time)+ ' actual: ' + str(long(time.time())))
# print 'pin P1-'+ str(pin)+ ' set '+ str(to_state) + ' required: ' + str(req_time)+ ' actual: ' + str(long(time.time()))
self.GPIO.output(pin,to_state)
# ************************************************
# gpio output sequencer interface methods
# these can be called from many classes so need to operate on class variables
# ************************************************
def animate(self,text,tag):
if self.options['gpio']==True:
lines = text.split("\n")
for line in lines:
error_text=self.parse_animate_fields(line,tag)
if error_text <>'':
return 'error',error_text
return 'normal',''
return 'normal',''
# clear event list
def clear_events_list(self,tag):
if self.options['gpio']==True:
self.mon.log(self,'clear events list ')
# empty event list
if tag==None:
PPIO.events=[]
else:
self.remove_events(tag)
def reset_outputs(self):
if self.options['gpio']==True:
self.mon.log(self,'reset outputs')
for index, pin in enumerate(PPIO.pins):
num = pin[PPIO.PIN]
if pin[PPIO.DIRECTION]=='out':
self.GPIO.output(num,False)
# ************************************************
# internal functions
# these can be called from many classes so need to operate on class variables
# ************************************************
def parse_animate_fields(self,line,tag):
fields= line.split()
if len(fields)==0:
return ''
name=fields[0]
pin= self.pin_of(name)
if pin ==-1:
return 'Unknown gpio logical output in: ' + line
to_state_text=fields[1]
if not (to_state_text in ('on','off')):
return 'Illegal to-state in : '+ line
if to_state_text == 'on':
to_state=True
else:
to_state=False
if len(fields)==2:
delay_text='0'
else:
delay_text=fields[2]
if not delay_text.isdigit():
return 'Delay is not an integer in : '+ line
delay=int(delay_text)
self.add_event(pin,to_state,delay,tag)
# self.print_events()
return ''
def pin_of(self,name):
for pin in PPIO.pins:
# print " in list" + pin[PPIO.NAME] + str(pin[PPIO.PIN] )
if pin[PPIO.NAME]==name and pin[PPIO.DIRECTION]=='out':
return pin[PPIO.PIN]
return -1
def print_events(self):
print
for i in PPIO.events:
print i
def add_event(self,sequencer_pin,sequencer_to_state,sequencer_time,sequencer_tag):
poll_time=long(time.time())
# delay is 0 so just do it, don't queue it.
#if sequencer_time == 0:
#print "firing now",poll_time
#self.do_event(sequencer_pin,sequencer_to_state,poll_time)
#return
# prepare the event
event=PPIO.EVENT_TEMPLATE
event[PPIO.SEQUENCER_PIN]=sequencer_pin
event[PPIO.SEQUENCER_TO_STATE]=sequencer_to_state
event[PPIO.SEQUENCER_TIME]=sequencer_time+poll_time+1
event[PPIO.SEQUENCER_TAG]=sequencer_tag
# print event
# find the place in the events list and insert
# first item in the list is earliest, if two have the same time then last to be added is fired last.
abs_time=sequencer_time+poll_time
copy_event= copy.deepcopy(event)
for index, item in enumerate(PPIO.events):
if abs_time<item[PPIO.SEQUENCER_TIME]:
PPIO.events.insert(index,copy_event)
return copy_event
PPIO.events.append(copy_event)
return copy_event
# remove an event not used and does not work
def remove_event(self,event):
for index, item in enumerate(PPIO.events):
if event==item:
del PPIO.events[index]
return True
return False
# remove all the events with the same tag, usually a track reference
def remove_events(self,tag):
left=[]
for item in PPIO.events:
if tag<>item[PPIO.SEQUENCER_TAG]:
left.append(item)
PPIO.events= left
#self.print_events()
# ***********************************
# reading gpio.cfg functions
# ************************************
def read(self,pp_dir,pp_home,pp_profile):
# try inside profile
tryfile=pp_profile+os.sep+"gpio.cfg"
# self.mon.log(self,"Trying gpio.cfg in profile at: "+ tryfile)
if os.path.exists(tryfile):
filename=tryfile
else:
# try inside pp_home
# self.mon.log(self,"gpio.cfg not found at "+ tryfile+ " trying pp_home")
tryfile=pp_home+os.sep+"gpio.cfg"
if os.path.exists(tryfile):
filename=tryfile
else:
# try inside pipresents
# self.mon.log(self,"gpio.cfg not found at "+ tryfile + " trying inside pipresents")
tryfile=pp_dir+os.sep+'pp_home'+os.sep+"gpio.cfg"
if os.path.exists(tryfile):
filename=tryfile
else:
self.mon.log(self,"gpio.cfg not found at "+ tryfile)
self.mon.err(self,"gpio.cfg not found")
return False
self.config = ConfigParser.ConfigParser()
self.config.read(filename)
self.mon.log(self,"gpio.cfg read from "+ filename)
return True
# ******************************
# test harness
# ******************************
if __name__ == '__main__':
def callback(index,name,edge):
global pevent
if name == 'play':
#print name, edge
# event with 0 delay is executed immeadiately and cannot be removed.
# pin, state, time, tag
ppio.add_event(0,1,0,1)
ppio.add_event(2,1,2,1)
ppio.add_event(3,1,3,1)
ppio.add_event(4,1,3,2)
ppio.add_event(1,1,1,1)
ppio.add_event(5,1,10,2)
pevent=ppio.add_event(6,1,11,2)
elif name=='pause':
ppio.remove_events(2)
ppio.remove_event(pevent)
pevent=None
pp_dir='/home/pi/pipresents'
pp_profile='/home/pi/pp_home/pp_profiles/trigger_test'
Monitor.log_path=pp_dir
Monitor.global_enable=True
print "runnning"
my_window=Tk()
my_window.title("PPIO Test Harness")
ppio=PPIO()
ppio.init(pp_dir,pp_profile,my_window,50,callback)
ppio.read()
ppio.poll()
my_window.mainloop()