-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandyGPIOwatcher.py
More file actions
executable file
·166 lines (140 loc) · 5.03 KB
/
Copy pathcandyGPIOwatcher.py
File metadata and controls
executable file
·166 lines (140 loc) · 5.03 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import random
import psutil
import datetime
import dothat.backlight as backlight
import dothat.lcd as lcd
import threading
import createCandyDashboard as DASHboard
import pymysql
from selenium import webdriver
#Set up GPIO for button press event:
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
delay_in_seconds=30
def lcdRainbowBackgroundThread():
lcd_thread = threading.Thread(name="lcdRainbow", target=lcdRainbow, args=(delay_in_seconds,))
lcd_thread.start()
def firefoxBackgroundThread():
web_thread = threading.Thread(name="webDisplay", target=runWebBrowser)
web_thread.start()
def dashboardBackgroundThread():
dashboard_thread = threading.Thread(name="dashboard", target=runDashboard)
dashboard_thread.start()
def lcdScreenUpdatesThread(backlight):
lcd_update_thread = threading.Thread(name="lcdScreenUpdatesThread", target=continuousLCDUpdate)
lcd_update_thread.start()
def lcdWriteHeader(lcd):
lcd.set_cursor_position(1, 1)
lcd.write("*CANDY TRACKER* ")
lcd.set_cursor_position(0, 0)
lcd.write("!!!!- BETA -!!!!")
lcd.set_cursor_position(2, 2)
def resetScreen(lcdBacklight, lcd):
lcd.clear()
randomBlue = random.randint(0,255)
randomRed = random.randint(0,255)
randomGreen = random.randint(0,255)
backlight.rgb(randomRed,randomGreen,randomBlue)
lcdWriteHeader(lcd)
CPUusage = psutil.cpu_percent(interval=None)
meminfo = psutil.virtual_memory()
cpuUsageString = ("CPU " + str(round(CPUusage)) + "%")
memUsageString = ("RAM " + str(round(meminfo.percent)) + "%")
lcdWriteHeader(lcd)
lcd.set_cursor_position(0, 2)
lcd.write(memUsageString)
lcd_cursor_x = (16 - len(cpuUsageString))
lcd.set_cursor_position(lcd_cursor_x, 2)
lcd.write(cpuUsageString)
def lcdRainbow(delay_in_seconds):
lcd.clear()
start_time = datetime.datetime.now()
s = 1
while s > 0:
current_time = datetime.datetime.now()
delta = (current_time - start_time)
s = delta.total_seconds()
lcd.set_cursor_position(0, 0)
lcd.write("IT'S CANDY TIME!")
lcd.set_cursor_position(0, 1)
lcd.write("You have " + str((delay_in_seconds - s)))
lcd.set_cursor_position(0, 2)
lcd.write("seconds left-->")
for x in range(0,360):
backlight.sweep((x % 360) / 360.0)
lcd.clear()
print(str(delta))
if s > delay_in_seconds:
s = -1 #exit loop
lcd.set_cursor_position(1, 1)
lcd.write("CANDY TRACKER")
resetScreen(backlight,lcd)
def candyButtonPressed(channel):
print("Candy activity detected!! randint():" + str(random.randint(0,50)))
activeCandyButtonThreads = threading.active_count()
current_threads = threading.enumerate()
lcdRainbowActive = False
for x in current_threads:
if x.name == 'lcdRainbow':
lcdRainbowActive = True
if lcdRainbowActive == False:
#This is used to keep the LCD from freaking out if the button is pressed multiple times
lcdRainbowBackgroundThread()
mysql = connectToDB()
current_timestamp = datetime.datetime.now()
with mysql.cursor() as cursor:
currentEntrySQL = "INSERT INTO candydb.candycounts (candyconsumption_date_ik,logged_date) VALUES (" + current_timestamp.strftime("%Y%m%d") + ",STR_TO_DATE('" + current_timestamp.strftime("%Y,%m,%d, %H:%M:%S") + "', '%Y,%m,%d,%T'));"
print(currentEntrySQL)
cursor.execute(currentEntrySQL)
mysql.commit()
mysql.close()
def continuousLCDUpdate():
while 1==1:
time.sleep(1)
lcdRainbowActive = False
current_threads = threading.enumerate()
for x in current_threads:
if x.name == 'lcdRainbow':
lcdRainbowActive = True
if lcdRainbowActive == False:
lcd.clear()
resetScreen(backlight,lcd)
def runDashboard():
DASHboard.setup_app()
def runWebBrowser():
driver = webdriver.Firefox()
driver.get("http://127.0.0.1:8050")
while 1==1:
for x in range(0,600):
x = x + 1
time.sleep(1)
#print("Firefox refresh loop second " + str(x) + " of 600")
driver.refresh()
def connectToDB():
connection = pymysql.connect(
host = 'localhost'
,user = 'cobicandy'
,password = 'cobi'
,charset = 'utf8mb4'
,cursorclass=pymysql.cursors.DictCursor
)
connection.connect()
return connection
#This has all the code needed to detect when the button has been pressed and released
GPIO.add_event_detect(6, GPIO.RISING, callback=candyButtonPressed, bouncetime=(delay_in_seconds*1000))
lcd.set_contrast(50)
resetScreen(backlight,lcd)
dashboardBackgroundThread()
firefoxBackgroundThread()
lcdScreenUpdatesThread(backlight)
while True:
time.sleep(0.1)
GPIO.cleanup()
##while True:
## input_state = GPIO.input(6)
## print(str(input_state))
## time.sleep(0.2)