-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetalarm.py
More file actions
682 lines (534 loc) · 16.8 KB
/
Copy pathsetalarm.py
File metadata and controls
682 lines (534 loc) · 16.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#!/usr/bin/env python3
#
#-----------------------------------------------------------------------------
# set a wakeup time on AB Electronics PCF8563 board for Raspberry Pi.
# uses quick2wire see
# https://github.com/quick2wire/quick2wire-python-api
#
# Requries Python 3
#
# GPIO API depends on Quick2Wire GPIO Admin.
# I2C API depends on I2C support in the kernel
#
# Version 1.00 - 26/04/2013
# Version 2.00 - 18/02/2014
#
# Version History:
#
# 1.00 - Initial Release
# 2.00 - Re-written by M.A. O'Neill <mao@tumblingdice.co.uk>
#----------------------------------------------------------------------------
import quick2wire.i2c as i2c
import time
import sys, math, struct
#-------------------------------
# Address of PCF8563 RTC device
#-------------------------------
rtc_address1 = 0x51
#----------------------------
# Use i2c as operational bus
#----------------------------
with i2c.I2CMaster(1) as bus:
#-----------------------------------
# general functions for conversion
#-----------------------------------
#---------------------------------------------
# Binary packed decmal to decimal conversion
#---------------------------------------------
def fromBCDtoDecimal(x):
return x - 6 * (x >> 4)
#---------------------------------------------
# Decimal to binary packed decimal conversion
#---------------------------------------------
def decToBcd(x):
bcdstring = ''
if x < 10:
bcdstring = '0000'
for i in str(x):
bcdstring += bcddigit(i)
return int(bcdstring,2)
#----------------
# Get BCD digit
#----------------
def bcddigit(sn):
n = int(sn)
bin_nr = bin(n)[2:]
return ('0000' + bin_nr)[-4:]
#------------------------------------------
# Convert binary number to BCD equivalent
#------------------------------------------
def bin2bcd(x):
return x + 6 * (x /10)
#---------------------------------------
# end general functions for conversion
# PCF8563 specific functions follow
#---------------------------------------
#---------------------------------------------
# enable clock output on CLKOUT at 32.768 kHz
#---------------------------------------------
def setClkOutput():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0D, 0x80))
#--------------------
# reset clock output
#--------------------
def clearClkOutput():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0, 0x0, 0x0))
#--------------------
# reset status bytes
#--------------------
def clearStatus():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0D, 0x80))
#-----------------------------
# get minute and return value
#-----------------------------
def getMinute():
minute, minute2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x03),
i2c.reading(rtc_address1,2))[0]
timeminute1 = ((minute >> 4) & 0x07)
timeminute2 = (minute & 0x0F)
return int(str(timeminute1) + str(timeminute2))
#----------------------------
# get hour and return value
#----------------------------
def getHour():
hour, hour2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x04),
i2c.reading(rtc_address1,2))[0]
timehour1 = ((hour >> 4) & 0x03)
timehour2 = (hour & 0x0F)
return int(str(timehour1) + str(timehour2))
#--------------------------
# get day and return value
#--------------------------
def getDay():
#------
# day
#------
day, day2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x05),
i2c.reading(rtc_address1,2))[0]
timeday1 = ((day >> 4) & 0x03)
timeday2 = (day & 0x0F)
return int(str(timeday1) + str(timeday2))
#----------------------------
# get month and return value
#----------------------------
def getMonth():
month, month2 = bus.transaction(i2c.writing_bytes(rtc_address1, 0x07),
i2c.reading(rtc_address1,2))[0]
timemonth1 = ((month >> 4) & 0x01)
timemonth2 = (month & 0x0F)
#----------------------
# Return month by name
#----------------------
monthno = int(str(timemonth1) + str(timemonth2))
if monthno == 1:
monthname = "Jan"
if monthno == 2:
monthname = "Feb"
if monthno == 3:
monthname = "Mar"
if monthno == 4:
monthname = "Apr"
if monthno == 5:
monthname = "May"
if monthno == 6:
monthname = "Jun"
if monthno == 7:
monthname = "Jul"
if monthno == 8:
monthname = "Aug"
if monthno == 9:
monthname = "Sep"
if monthno == 10:
monthname = "Oct"
if monthno == 11:
monthname = "Nov"
if monthno == 12:
monthname = "Dec"
#------------------------
# Return month by number
#------------------------
return monthno, monthname
#---------------------------
# get year and return value
#---------------------------
def getYear():
year, year2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x08),
i2c.reading(rtc_address1,2))[0]
timeyear1 = ((year >> 4))
timeyear2 = (year & 0x0F)
#------------------------
# We're in 21st Century!
#------------------------
return int("20" + str(timeyear1) + str(timeyear2))
#-------------------------------------------------------------
# Is the PCF8563 device present (note only works between 2001
# and 2099!
#-------------------------------------------------------------
def pcf8563OnI2cBus():
if getYear() == 0:
return False
return True
#-------------------------
# set alarm data to clock
#-------------------------
def setAlarm(minute, hour, day, weekday):
enableAlarm()
bus.transaction(i2c.writing_bytes(rtc_address1, 0x09, decToBcd(minute), decToBcd(hour), decToBcd(day), decToBcd(weekday)))
#-------------------------------
# Return day of week given date
#-------------------------------
def weekDay(year, month, day):
#---------------------------------------------
# Sanity check - make sure day given does
# not exceed the number of days in the month!
#---------------------------------------------
if month == 1 and day > 31:
print(" setalarm ERROR: only 31 days in January")
print(" ")
exit()
if year % 4 == 0:
if month == 2 and day > 29:
print(" setalarm ERROR: only 29 days in February")
print("")
exit()
else:
if month == 2 and day > 28:
print(" setalarm ERROR: only 28 days in February")
print(" ")
exit()
if month == 3 and day > 31:
print(" setalarm ERROR: only 31 days in March")
print(" ")
exit()
if month == 4 and day > 30:
print(" setalarm ERROR: only 30 days in April")
print(" ")
exit()
if month == 5 and day > 31:
print(" setalarm ERROR: only 31 days in May")
print(" ")
exit()
if month == 6 and day > 30:
print(" setalarm ERROR: only 30 days in June")
print(" ")
exit()
if month == 7 and day > 31:
print(" setalarm ERROR: only 31 days in July")
print(" ")
exit()
if month == 8 and day > 31:
print(" setalarm ERROR: only 31 days in August")
print(" ")
exit()
if month == 9 and day > 30:
print(" setalarm ERROR: only 30 days in September")
print(" ")
exit()
if month == 10 and day > 31:
print(" setalarm ERROR: only 31 days in October")
print(" ")
exit()
if month == 11 and day > 30:
print(" setalarm ERROR: only 30 days in November")
print(" ")
exit()
if month == 12 and day > 31:
print(" setalarm ERROR: only 31 days in December")
print(" ")
exit()
offset = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
week = {0:'Sun',
1:'Mon',
2:'Tue',
3:'Wed',
4:'Thu',
5:'Fri',
6:'Sat'}
afterFeb = 1
if month > 2:
afterFeb = 0
aux = year - 1700 - afterFeb
#------------------------------------
# dayOfWeek for 1700/1/1 = 5, Friday
#------------------------------------
dayOfWeek = 5
#-------------------------------------------------------
# partial sum of days betweem current date and 1700/1/1
#-------------------------------------------------------
dayOfWeek += (aux + afterFeb) * 365
#----------------------
# leap year correction
#----------------------
dayOfWeek += int(aux / 4 - aux / 100 + (aux + 100) / 400)
#-----------------------------
# sum monthly and day offsets
#-----------------------------
dayOfWeek += offset[month - 1] + (day - 1)
dayOfWeek %= 7
return dayOfWeek, week[dayOfWeek]
#--------------
# enable alarm
#--------------
def enableAlarm():
bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x02))
#----------------------------------
# clear status and alarm registers
#----------------------------------
def resetAlarm():
#-----------------------
# Clear status register
#-----------------------
bus.transaction(i2c.writing_bytes(rtc_address1, 0x01, 0x00))
#-----------------------
# Clear alarm registers
#-----------------------
bus.transaction(i2c.writing_bytes(rtc_address1, 0x09, 0x00))
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0A, 0x00))
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0B, 0x00))
bus.transaction(i2c.writing_bytes(rtc_address1, 0x0C, 0x00))
#-------------------
# read alarm status
#-------------------
def readAlarm():
alarmstatus, status22 = bus.transaction(i2c.writing_bytes(rtc_address1, 0x01),
i2c.reading(rtc_address1,2))[0]
#----------------------------------
# Mask alarm interrupt enable flag
#----------------------------------
aie = alarmstatus & 0x02
#---------------------
# Mask alarm set flag
#---------------------
af = alarmstatus & 0x08
return af, aie
#-----------------
# Read alarm time
#-----------------
def readAlarmTime(month, year):
#---------
# minute
#--------
minute, minute2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x09),
i2c.reading(rtc_address1,2))[0]
timeminute1 = ((minute >> 4) & 0x07)
timeminute2 = (minute & 0x0F)
#------
# hour
#------
hour, hour2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0A),
i2c.reading(rtc_address1,2))[0]
timehour1 = ((hour >> 4) & 0x03)
timehour2 = (hour & 0x0F)
#------
# Day
#-----
day, day2 = bus.transaction( i2c.writing_bytes(rtc_address1, 0x0B),
i2c.reading(rtc_address1,2))[0]
timeday1 = ((day >> 4) & 0x03)
timeday2 = (day & 0x0F)
alminute = int(str(timeminute1)+str(timeminute2))
alhour = int(str(timehour1)+str(timehour2))
alday = int(str(timeday1)+str(timeday2))
weekday,dayname = weekDay(year,month,alday)
return alminute, alhour, alday, weekday, dayname
#-----------------
# Show alarm time
#-----------------
def showAlarmTime():
year = getYear()
month, monthname = getMonth()
alminute, alhour, alday, weekday, dayname = readAlarmTime(month, year)
af, aie = readAlarm()
if af == 1:
print(" setalarm: alarm went off at %02d:%02d %s %02d %s %4d" % (alhour, alminute, dayname, alday, monthname, year))
else:
if alminute > 0 or alhour > 0 or alday > 0:
print(" setalarm: alarm scehduled at %02d:%02d %s %02d %s %4d" % (alhour, alminute, dayname, alday, monthname, year))
else:
print(" setalarm: no alarm scheduled")
#--------------
# Display help
#--------------
def showHelp():
print("")
print(" PCF8563 RTC setalarm for Raspberry Pi version 2.00")
print(" (C) M.A. O'Neill, TumblingDice 2014")
print("")
print(" Usage: setalarm [--usage | --help]")
print(" |")
print(" [-v | --verbose:False]")
print(" [-s | --status] | [-c | --cancel] | [-o | --off] |")
print(" [-m <minute of hour> | --minute <minute of hour>]")
print(" [-h <hour of day> | --minute <hour of day>]")
print(" [-d <day of month> | --day <day of month>]")
print("")
print(" The clock is a 24 hour clock: e.g. hour of day is 0..24")
print("")
exit()
#------------------------
# Main entry point here
#------------------------
argp = 1
argd = 0
do_cancel = False
do_status = False
do_verbose = False
argc = len(sys.argv)
if argc == 1:
showHelp()
#--------------------
# Parse command tail
#--------------------
while(argp < argc):
#-------------------------------
# Help if user has requested it
#-------------------------------
if sys.argv[argp] == "--usage" or sys.argv[argp] == "--help":
showHelp()
#--------------------
# Is output verbose?
#--------------------
if sys.argv[argp] == "-v" or sys.argv[argp] == "--verbose":
do_verbose = True
argd = argd + 1
#----------------
# Cancel alarm
#----------------
if sys.argv[argp] == "-c" or sys.argv[argp] == "--cancel":
do_cancel = True
argd = argd + 1
else:
#---------------------
# Report alarm status
#---------------------
if sys.argv[argp] == "-s" or sys.argv[argp] == "--status":
do_status = True
argd = argd + 1
if do_cancel == False and do_status == False:
#------------------------
# Get hour to set alarm
#------------------------
if sys.argv[argp] == "-h" or sys.argv[argp] == "--hour":
if argp == argc - 1 or sys.argv[argp+1][0] == '-':
print(" setalarm ERROR: expecting integer value hour")
print("")
exit()
else:
try:
hour = int(sys.argv[argp+1])
except ValueError:
print(" setalarm ERROR: integer between 0 and 23 expected")
exit()
if hour < 0 or hour > 23:
print(" setalarm ERROR: expecting integer between 0 and 23")
print("")
exit()
argd = argd + 2
argp = argp + 1
#-------------------------
# Get minute to set alarm
#-------------------------
if sys.argv[argp] == "-m" or sys.argv[argp] == "--minute":
if argp == argc - 1 or sys.argv[argp+1][0] == '-':
print(" setalarm ERROR: expecting integer value minute")
print("")
exit()
else:
try:
minute = int(sys.argv[argp+1])
except ValueError:
print(" setalarm ERROR: integer between 0 and 59 expected")
exit()
if minute < 0 or minute > 59:
print(" setalarm ERROR: expecting integer between 0 and 59")
print("")
exit()
argd = argd + 2
argp = argp + 1
#-------------------------------
# Get day of month to set alarm
#-------------------------------
if sys.argv[argp] == "-d" or sys.argv[argp] == "--day":
if argp == argc - 1 or sys.argv[argp+1][0] == '-':
print(" setalarm ERROR: expecting integer value day")
print("")
exit()
else:
try:
day = int(sys.argv[argp+1])
except ValueError:
print(" setalarm ERROR: integer between 0 and 31 expected")
exit()
if day < 0 or day > 31:
print(" setalarm ERROR: expecting integer between 0 and 31")
print("")
exit()
argd = argd + 2
argp = argp + 1
argp = argp + 1
#-------------------------------------------
# Check for unparsed command tail arguments
#-------------------------------------------
if argd < argc - 1:
print(" setalarm ERROR: unparsed command tail arguments")
print("")
exit()
if do_verbose:
print("")
print(" PCF8563 RTC setalarm for Raspberry Pi version 2.00")
print(" (C) M.A. O'Neill, TumblingDice 2014")
print("")
if pcf8563OnI2cBus() == False:
print(" setalarm ERROR: cannot find PCF8563 RTC on I2C bus")
printf(" ")
exit()
#-------------------------------------------
# now we start communication with the clock
# and enable clock pin out and alarm
#-------------------------------------------
setClkOutput()
enableAlarm()
#----------------------------
# Show alarm Time and status
#----------------------------
if do_status == True:
showAlarmTime()
else:
#--------------
# Cancel alarm
#--------------
if do_cancel == True:
resetAlarm()
if do_verbose:
print(" setalarm: alarm cancelled")
else:
#--------------------
# Schedule an alarm
#--------------------
year = getYear()
month,monthname = getMonth()
weekday,dayname = weekDay(year,month,day)
#-----------------------------------------------
# Sanity check the alarm must be in the future!
#-----------------------------------------------
nowDay = getDay()
if day < nowDay:
print(" setalarm ERROR1: you cannot set the alarm to go off in the past!")
print("")
exit()
else:
nowHour = getHour()
nowMinute = getMinute()
nowMinutes = 60*nowHour + nowMinute
minutes = 60*hour + minute
if day < nowDay and minutes < nowMinutes:
print(" setalarm ERROR2: you cannot set the alarm to go off in the past!")
print("")
exit()
if do_verbose:
print(" setalarm: alarm scheduled at: %02d:%02d %s %02d %s %4d" % (hour, minute, dayname, day, monthname, year))
setAlarm(minute, hour, day, weekday)
if do_verbose == True:
print(" setalarm: finished")
print("")
exit()