forked from b0tting/escapemachine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathescape_library.py
55 lines (45 loc) · 1.43 KB
/
escape_library.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
from collections import OrderedDict
import logging
import imp
try:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
except Exception:
GPIO = False
import datetime
import time
class CaravanLoggingHandler(logging.StreamHandler):
def __init__(self):
logging.StreamHandler.__init__(self)
self.last_entries = OrderedDict()
def emit(self, record):
msg = self.format(record)
if len(self.last_entries) > 30:
self.last_entries.popitem(last=False)
self.last_entries[datetime.datetime.now()] = msg
def get_last_entries(self):
nowsecs = time.time()
returns = []
for dateentry in self.last_entries:
thensecs = time.mktime(dateentry.timetuple())
diff = thensecs - nowsecs
diffstring = str(int(diff)) + " secs"
returns.insert(0, diffstring.ljust(9) + " " + self.last_entries[dateentry])
return returns
class OutputPin:
def __init__(self, pin, name):
self.name = name
self.pin = pin
if GPIO:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
self.is_on = False
def flip_switch(self, to_high):
if GPIO:
state = GPIO.HIGH if to_high else GPIO.LOW
GPIO.output(self.pin, state)
self.is_on = not to_high
def turn_on(self):
self.flip_switch(False)
def turn_off(self):
self.flip_switch(True)