Browse Source

Remove dependency on scheduler

Pete Shadbolt 2 months ago
parent
commit
e5a08d3551
2 changed files with 24 additions and 22 deletions
  1. 1
    1
      README.md
  2. 23
    21
      xmas.py

+ 1
- 1
README.md View File

@@ -1,6 +1,6 @@
1 1
 # Christmas tree
2 2
 
3
-Scheduler for our christmas tree lights. Uses PiFace and [schedule](https://github.com/dbader/schedule).
3
+Scheduler for our christmas tree lights. Uses [PiFace](http://www.piface.org.uk/).
4 4
 
5 5
 Use `tmux` or `supervisor` to run `xmas.py`
6 6
 

+ 23
- 21
xmas.py View File

@@ -1,8 +1,11 @@
1
-import schedule
2 1
 import time
3 2
 import datetime
4 3
 import pifacedigitalio as p
5 4
 
5
+# Configures interval
6
+START = datetime.time(17, 0, 0)
7
+END = datetime.time(23, 59, 0)
8
+
6 9
 TREE = """
7 10
            *             ,
8 11
                        _/^\_
@@ -20,9 +23,9 @@ TREE = """
20 23
      *          /`,@ ;+& () o*`;-';\    *
21 24
                (`""--.,_0 +% @' &()\   *
22 25
                /-.,_    ``''--....-'`)  *
23
-          *    /@%;o`:;'--,.__   __.'\ 
26
+          *    /@%;o`:;'--,.__   __.'\
24 27
               ;*,&(); @ % &^;~`"`o;@();         *
25
-              /(); o^~; & ().o@*&`;&%O\  
28
+              /(); o^~; & ().o@*&`;&%O\
26 29
         jgs   `"="==""==,,,.,="=="==="`
27 30
            __.----.(\-''#####---...___...-----._
28 31
          '`         \)_`'''''`
@@ -31,23 +34,31 @@ TREE = """
31 34
                  `'''` `
32 35
 """
33 36
 
34
-START = datetime.time(17, 0, 0)
35
-END = datetime.time(23, 59, 0)
36 37
 
37 38
 def timestamp():
39
+    """ Get a timestamp """
38 40
     return datetime.datetime.now()
39 41
 
40 42
 
41 43
 def turn_on():
42 44
     """ Turn on the christmas tree """
43 45
     print("{} ON".format(timestamp()))
44
-    p.digital_write(0, 1) 
46
+    p.digital_write(0, 1)
45 47
 
46 48
 
47 49
 def turn_off():
48 50
     """ Turn off the christmas tree """
49 51
     print("{} OFF".format(timestamp()))
50
-    p.digital_write(0, 0) 
52
+    p.digital_write(0, 0)
53
+
54
+
55
+def update():
56
+    """ Updates the state """
57
+    now = datetime.datetime.now().time()
58
+    if START <= now <= END:
59
+        turn_on()
60
+    else:
61
+        turn_off()
51 62
 
52 63
 
53 64
 def check_connectivity():
@@ -60,12 +71,6 @@ def check_connectivity():
60 71
         time.sleep(2)
61 72
 
62 73
 
63
-    # Turn on if we are in the correct interval
64
-    now = datetime.datetime.now().time()
65
-    if START <= now <= END:
66
-        turn_on()
67
-
68
-
69 74
 if __name__ == "__main__":
70 75
     # Connect to PiFace
71 76
     print(TREE)
@@ -77,13 +82,10 @@ if __name__ == "__main__":
77 82
     # Message
78 83
     print()
79 84
     print("Starting scheduler:")
80
-    print("Xmas enabled between {} and {}.".format(START.strftime("%H:%M"), END.strftime("%H:%M")))
81
-
82
-    # Set up the schedule
83
-    schedule.every().day.at(START.strftime("%H:%M")).do(turn_on)
84
-    schedule.every().day.at(END.strftime("%H:%M")).do(turn_off)
85
+    print("Xmas enabled between {} and {}.".format(
86
+        START.strftime("%H:%M"), END.strftime("%H:%M")))
85 87
 
86 88
     # Run the event loop
87
-    while(True):
88
-        schedule.run_pending()
89
-        time.sleep(1)
89
+    while (True):
90
+        update()
91
+        time.sleep(60)