This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClock.py
187 lines (159 loc) · 4.87 KB
/
Clock.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Raspberry Pi Wecker Class
# $Id: Clock.py, 2014/09/19
#
# Author : Florian Miess
#
# Main Class with all clock funcionality
#
#
import time
import RPi.GPIO as GPIO
from Display import Display
from NTPTime import NTPTime
from Properties import Properties
from rotary_class import RotaryEncoder
from Alarm import Alarm
class Clock():
# Setup der Tasten
wheelButton = 21
pauseButton = 20
stopButton = 16
schalter = 25
scrollUp = 6
scrollDown = 5
GPIO.setmode(GPIO.BCM)
GPIO.setup(pauseButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(stopButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(schalter, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Klassen laden
ntpTime = NTPTime()
display = Display()
prop = Properties()
alarm = Alarm()
# ausgewaehltes Menue
menuItem = 0
# Menü aktiv
menuActive = False
menuMinuten = False
waittime = 2
# Einstellungen einlesen
wakeHour = int(prop.getProperty("Weckzeit").split(":")[0])
wakeMinute = int(prop.getProperty("Weckzeit").split(":")[1])
volume = int(prop.getProperty("Lautstaerke"))
alarmActive = (prop.getProperty("Alarm") == "True")
# Konstruktor
def __init__(self):
RotaryEncoder(self.scrollUp, self.scrollDown, self.wheelButton, callback=self.changeEvent)
GPIO.add_event_detect(self.schalter, GPIO.BOTH, callback=self.changeAlarm)
GPIO.add_event_detect(self.pauseButton, GPIO.BOTH, callback=self.pauseAlarm)
GPIO.add_event_detect(self.stopButton, GPIO.BOTH, callback=self.stopAlarm)
# verschiedene Menueeinträge durchschalten
def changeEvent(self, event):
# ein Menü ist aktiviert, also Funktion des aktiven Menüpunktes ausführen
if self.menuActive:
if event == RotaryEncoder.CLOCKWISE:
self.menuUp()
elif event == RotaryEncoder.ANTICLOCKWISE:
self.menuDown()
elif event == RotaryEncoder.BUTTONDOWN:
# falls wir uns im Weckzeitmenü befinden, durch Stunden und Minuten schalten
if self.menuItem == 1 and not self.menuMinuten:
print "aktiviert"
self.menuMinuten = True
else:
print "deaktiviert"
self.menuActive = False
self.menuMinuten = False
self.prop.setProperty("Weckzeit", str(self.wakeHour) + ":" + str(self.wakeMinute))
self.prop.setProperty("Lautstaerke", self.volume)
# kein Menü aktiv, also durch die Menüs navigieren
else:
if event == RotaryEncoder.CLOCKWISE:
self.menuItem += 1
elif event == RotaryEncoder.ANTICLOCKWISE:
self.menuItem -= 1
elif event == RotaryEncoder.BUTTONDOWN:
self.menuActive = True
self.display.showMenu(self.menuItem)
if self.menuItem == 0:
pass
elif self.menuItem == 1:
# Weckzeit einstellen
self.display.showTime(self.wakeHour, self.wakeMinute, self.alarmActive, self.menuItem)
elif self.menuItem == 2:
# Lautstärke einstellen
self.display.showNumber(self.volume, self.menuItem)
elif self.menuItem >= 3:
self.menuItem = -1
elif self.menuItem <= -1:
self.menuItem = 3
# Alarm de-/aktivieren
def changeAlarm(self, event):
self.alarmActive = not self.alarmActive
self.display.showAlarm(self.alarmActive)
self.prop.setProperty("Alarm", self.alarmActive)
def pauseAlarm(self, event):
self.alarm.pauseAlarm(self.waittime)
if self.waittime > 1:
self.waittime -= 1
def stopAlarm(self, event):
self.alarm.stopAlarm()
waittime = 10
# Eintrag erhöhen
def menuUp(self):
if self.menuItem == 1:
# Stunden
if not self.menuMinuten:
self.wakeHour += 1
self.wakeHour %= 24
# Minuten
else:
self.wakeMinute += 1
self.wakeMinute %= 60
self.display.showTime(self.wakeHour, self.wakeMinute, self.alarmActive, self.menuItem)
elif self.menuItem == 2:
self.volume += 1
self.display.showNumber(self.volume, self.menuItem)
# Eintrag erniedrigen
def menuDown(self):
if self.menuItem == 1:
# Stunden
if not self.menuMinuten:
self.wakeHour -= 1
self.wakeHour %= 24
# Minuten
else:
self.wakeMinute -= 1
self.wakeMinute %= 60
self.display.showTime(self.wakeHour, self.wakeMinute, self.alarmActive, self.menuItem)
elif self.menuItem == 2:
self.volume -= 1
self.display.showNumber(self.volume, self.menuItem)
def main(self):
try:
lastminute = 0
while True:
# normale Zeit anzeigen
currentTime = self.ntpTime.getTime()
hour = currentTime.hour
minute = currentTime.minute
second = currentTime.second
if self.menuItem == 0:
# blinken
self.display.showTime(hour, minute, self.alarmActive, self.menuItem)
self.display.blink(second)
# Alarm 1x pro Minute prüfen
if not minute == lastminute:
if self.alarm.isAlarm(currentTime, self.wakeHour, self.wakeMinute, self.alarmActive, self.menuItem):
self.alarm.startAlarm()
lastminute = minute
time.sleep(0.5)
except KeyboardInterrupt:
print "Programm abgebrochen"
finally:
GPIO.cleanup()
if __name__ == '__main__':
objName = Clock()
objName.main()