forked from abelectronicsuk/AlarmPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarmpi.py
182 lines (147 loc) · 5.01 KB
/
alarmpi.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
#!/usr/bin/env python3
#read PCF8563 real time clock model on Alarm Pi board from AB Electronics UK
# www.abelectronics.co.uk
# uses quick2wire from http://quick2wire.com/ github: https://github.com/quick2wire/quick2wire-python-api
# Requries Python 3
# GPIO API depends on Quick2Wire GPIO Admin. To install Quick2Wire GPIO Admin, follow instructions at http://github.com/quick2wire/quick2wire-gpio-admin
# I2C API depends on I2C support in the kernel
# Version 1.0 - 26/04/2013
# Version History:
# 1.0 - Initial Release
#
import quick2wire.i2c as i2c
import time
import sys, math, struct
rtc_address1 = 0x51
with i2c.I2CMaster(1) as bus:
# general functions for conversion
def fromBCDtoDecimal(x):
return x - 6 * (x >> 4)
#return (newvalue /16 * 10) + (newvalue%16)
def decToBcd(x):
bcdstring = ''
if x < 10:
bcdstring = '0000'
for i in str(x):
bcdstring += bcddigit(i)
return int(bcdstring,2)
return hex(int(bcdstring,2))
def bcddigit(sn):
n = int(sn)
bin_nr = bin(n)[2:]
return ('0000' + bin_nr)[-4:]
def bin2bcd(x):
return x + 6 * (x /10)
# end general functions for conversion
def initClock():
# reset all clock values to factory default
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0, 0x0, 0x0, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x80, 0x80, 0x80, 0x80, 0x0, 0x0))
def setClkOutput():
# enable clock output on CLKOUT at 32.768 kHz
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0D, 0x80))
def clearClkOutput():
# reset clock output
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0, 0x0, 0x0))
def clearStatus():
#reset status bytes
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0D, 0x80))
def getDate():
# get date and return value
#get day
day, day2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x05),
i2c.reading(rtc_address1,2))[0]
timeday1 = ((day >> 4) & 0x03)
timeday2 = (day & 0x0F)
#get month
month, month2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x07),
i2c.reading(rtc_address1,2))[0]
timemonth1 = ((month >> 4) & 0x01)
timemonth2 = (month & 0x0F)
#get year
year, year2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x08),
i2c.reading(rtc_address1,2))[0]
timeyear1 = ((year >> 4))
timeyear2 = (year & 0x0F)
return str(timeday1) + str(timeday2) + "/" + str(timemonth1) + str(timemonth2) + "/" + str(timeyear1) + str(timeyear2)
def getTime():
#get time and return value
# get seconds
seconds, seconds2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x02),
i2c.reading(rtc_address1,2))[0]
timeseconds1 = ((seconds >> 4) & 0x07)
timeseconds2 = (seconds & 0x0F)
#get minute
minute, minute2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x03),
i2c.reading(rtc_address1,2))[0]
timeminute1 = ((minute >> 4) & 0x07)
timeminute2 = (minute & 0x0F)
#get hour
hour, hour2 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x04),
i2c.reading(rtc_address1,2))[0]
timehour1 = ((hour >> 4) & 0x03)
timehour2 = (hour & 0x0F)
return str(timehour1) + str(timehour2) + ":" + str(timeminute1) + str(timeminute2) + ":" + str(timeseconds1) + str(timeseconds2)
def setDate(day, weekday, month, century, year):
# set the date to the clock
# year is 00-99
# high bit of month = century
# 0 = 20xx
# 1 = 19xx
savemonth = decToBcd(month)
if (century == 1):
savemonth |= 0x80
else:
savemonth &= ~0x80
bus.transaction(i2c.writing_bytes(rtc_address1, 0x05,
decToBcd(day), decToBcd(weekday), savemonth, decToBcd(year)))
def setTime(hour, minute, second):
# set the time to the clock
bus.transaction(i2c.writing_bytes(rtc_address1, 0x02,
decToBcd(second), decToBcd(minute), decToBcd(hour)))
def setAlarm(minuite, hour, day, weekday):
# set alarm data to clock
enableAlarm()
bus.transaction(i2c.writing_bytes(rtc_address1, 0x09,
decToBcd(minuite), decToBcd(hour), decToBcd(day), decToBcd(weekday)))
def enableAlarm():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x02))
def clearAlarm():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0D, 0x80))
def resetAlarm():
# clear alarm flag leaving interupt unchanged
bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x00))
#bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x08))
def readAlarm():
alarmstatus, status22 = bus.transaction(
i2c.writing_bytes(rtc_address1, 0x01),
i2c.reading(rtc_address1,2))[0]
return alarmstatus
# now we start communication with the clock and enable clock pin out and alarm
initClock()
setClkOutput()
enableAlarm()
# set time at 22:59:58
setTime(22,59,58)
#setDate(day, weekday, month, century, year)
# set the date to be 17th April 2013, 3rd weekday
setDate(17, 3, 4, 1, 13)
setAlarm(0, 23, 17, 3)
while True:
print(getDate())
print(getTime())
print(readAlarm() )
if (readAlarm() == 66):
print ("not active")
if (readAlarm() == 10):
print ("Active")
clearAlarm()
resetAlarm()
if (readAlarm() == 2):
print ("Reset")
time.sleep(1)