-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGPIOServerSide.py
More file actions
126 lines (113 loc) · 3.71 KB
/
GPIOServerSide.py
File metadata and controls
126 lines (113 loc) · 3.71 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
import Adafruit_CharLCD as LCD
import time
import importlib.util
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
import FakeRPi.GPIO as GPIO
# Pin Config:
ledGroen = 2
ledRood = 3
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledRood, GPIO.OUT)
GPIO.setup(ledGroen, GPIO.OUT)
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 1
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)
#list of client ID's that are in 'alarming' state. Used for LCD displaying purposes.
triggered = set()
#used to prevent the LCD screen from flickering and constant updating.
ShowedMessageArmed = False
ShowedMessageDisarmed = False
ShowedMessageAlarm = False
def ClearLCD():
"""
clears the LCD screen...
"""
lcd.clear()
def armed(helper):
"""
helper = instance of csn_server_helper. Has no real function anymore...
"""
global ShowedMessageArmed,ShowedMessageAlarm,ShowedMessageDisarmed
try:
triggered.remove(helper.cID) #client re-armed. Should not be in the list at all, so double-checking.
except:
pass
helper.disarmed_lcd_showed = False #not used anymore...
if len(triggered) > 0 and ShowedMessageArmed==False: #If the LCD message hasn't been updated yet: update.
ShowedMessageArmed = True
ShowedMessageDisarmed = False
ShowedMessageAlarm = False
lcd.clear()
s = ""
clientlist = list(triggered)
for cID in clientlist:
#print(cID)
s+=str(cID)+" "
lcd.message("Clients\nBreached:"+s) #sets the LCD text
elif ShowedMessageArmed==False: #no alarms are triggered, all clients are OK.
ShowedMessageArmed = True
ShowedMessageDisarmed = False
ShowedMessageAlarm = False
lcd.clear()
lcd.message('All clients\nOK')
GPIO.output(ledRood,False)
GPIO.output(ledGroen,True)
def lcd_text(text):
lcd.clear()
lcd.message(text)
def disarm(helper):
"""
Updates the flags and prevents updates from other functions by blocking this sub-thread for 2secs.
Turns on the green LED and turns the red LED off.
"""
global ShowedMessageArmed,ShowedMessageAlarm,ShowedMessageDisarmed
if ShowedMessageDisarmed == False:
GPIO.output(ledRood,False)
GPIO.output(ledGroen,True)
try:
triggered.remove(helper.cID)
except:
pass
lcd.clear()
lcd.message("Client "+str(helper.cID)+"\nDisarmed")
helper.disarmed_lcd_showed = True
ShowedMessageArmed = False
ShowedMessageDisarmed = True
ShowedMessageAlarm = False
time.sleep(2)
def alarm(helper):
"""
Adds the client to the list of triggered alarms. Sets the flags to alarm state as well if not done already.
Makes the red LED blink as well
"""
global ShowedMessageArmed,ShowedMessageAlarm,ShowedMessageDisarmed
if ShowedMessageAlarm == False:
triggered.add(helper.cID)
lcd.clear()
s = ""
clientlist = list(triggered)
for cID in clientlist:
#print(cID)
s+=str(cID)+" "
lcd.message('Alarm triggerd:\nClient: {}'.format(s))
ShowedMessageArmed = False
ShowedMessageDisarmed = False
ShowedMessageAlarm = True
GPIO.output(ledGroen,False)
GPIO.output(ledRood, True)
time.sleep(1)
GPIO.output(ledRood, False)
time.sleep(1)