Advertisement
trippyt

Myclock3.py

Jun 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. import time
  2. from dot3k.menu import MenuOption
  3.  
  4. import RPi.GPIO as GPIO
  5. GPIO.setmode(GPIO.BCM)
  6. GPIO.setwarnings(False)
  7. GPIO.setup(13,GPIO.OUT)
  8. GPIO.setup(5,GPIO.OUT)
  9.  
  10.  
  11. class Myclock(MenuOption):
  12. def __init__(self, backlight=None):
  13. self.modes = ['date', 'week', 'binary', 'day', 'night','manual', 'auto']
  14. self.mode = 0
  15. self.binary = True
  16. self.manual = True
  17. self.auto = True
  18. self.running = False
  19. self.option_time = 0
  20. self.day_hour = 20
  21. self.night_hour = 8
  22. self.is_setup = False
  23. self._status = False
  24. MenuOption.__init__(self)
  25.  
  26. def get_status(self):
  27. return self._current_status
  28.  
  29. def is_on(self):
  30. return self._status
  31.  
  32. def is_auto(self):
  33. return self._auto
  34.  
  35. def begin(self):
  36. self.is_setup = False
  37. self.running = True
  38.  
  39. def setup(self, config):
  40. MenuOption.setup(self, config)
  41. self.load_options()
  42.  
  43. def update_options(self):
  44. self.set_option('myclock', 'day', str(self.day_hour))
  45. self.set_option('myclock', 'night', str(self.night_hour))
  46. self.set_option('myclock', 'binary', str(self.binary))
  47. self.set_option('myclock', 'manual', str(self.manual))
  48. self.set_option('myclock', 'auto', str(self.auto))
  49.  
  50. def load_options(self):
  51. self.day_hour = int(self.get_option('myclock', 'day', str(self.day_hour)))
  52. self.night_hour = int(self.get_option('myclock', 'night', str(self.night_hour)))
  53. self.binary = self.get_option('myclock', 'binary', str(self.binary)) == 'True'
  54. self.manual = self.get_option('myclock', 'manual', str(self.manual)) == 'False'
  55. self.auto = self.get_option('myclock', 'auto', str(self.auto)) == 'True'
  56.  
  57. def cleanup(self):
  58. self.running = False
  59. time.sleep(0.01)
  60. self.is_setup = False
  61.  
  62. def left(self):
  63. if self.modes[self.mode] == 'binary':
  64. self.binary = False
  65. elif self.modes[self.mode] == 'day':
  66. self.day_hour = (self.day_hour - 1) % 24
  67. elif self.modes[self.mode] == 'night':
  68. self.night_hour = (self.night_hour - 1) % 24
  69. elif self.modes[self.mode] == 'manual':
  70. self.manual = False
  71. elif self.modes[self.mode] == 'auto':
  72. self.auto = False
  73. else:
  74. return False
  75. self.update_options()
  76. self.option_time = self.millis()
  77. return True
  78.  
  79. def right(self):
  80. if self.modes[self.mode] == 'binary':
  81. self.binary = True
  82. elif self.modes[self.mode] == 'day':
  83. self.day_hour = (self.day_hour + 1) % 24
  84. elif self.modes[self.mode] == 'night':
  85. self.night_hour = (self.night_hour + 1) % 24
  86. elif self.modes[self.mode] == 'manual':
  87. self.manual = True
  88. elif self.modes[self.mode] == 'auto':
  89. self.auto =True
  90. self.update_options()
  91. self.option_time = self.millis()
  92. return True
  93.  
  94. def up(self):
  95. self.mode = (self.mode - 1) % len(self.modes)
  96. self.option_time = self.millis()
  97. return True
  98.  
  99. def down(self):
  100. self.mode = (self.mode + 1) % len(self.modes)
  101. self.option_time = self.millis()
  102. return True
  103.  
  104. def daylights_on(self):
  105. GPIO.output(13,0)
  106. GPIO.output(5,1)
  107. GPIO.output(13,1)
  108. self._status = True
  109.  
  110. def nightlights_on(self):
  111. GPIO.output(13,0)
  112. GPIO.output(5,1)
  113. GPIO.output(13,0)
  114. self._status = True
  115.  
  116. def auto_on(self): # Sets an automatic timer to activate lights
  117. self._auto = True
  118.  
  119. def auto_off(self): # Sets an automatic timer to dectivate lights
  120. self._auto = False
  121.  
  122. required_status = 'off'
  123.  
  124. def sched_nightlights():
  125. global required_status
  126. required_status = 'night'
  127.  
  128. def sched_daylights():
  129. global required_status
  130. required_status = 'day'
  131.  
  132. def sched_off():
  133. global required_status
  134. required_status = 'off'
  135.  
  136.  
  137. def redraw(self, menu):
  138. if not self.running:
  139. return False
  140.  
  141. if self.millis() - self.option_time > 5000 and self.option_time > 0:
  142. self.option_time = 0
  143. self.mode = 0
  144.  
  145. if not self.is_setup:
  146. menu.lcd.create_char(0, [0, 0, 0, 14, 17, 17, 14, 0])
  147. menu.lcd.create_char(1, [0, 0, 0, 14, 31, 31, 14, 0])
  148. menu.lcd.create_char(2, [0, 14, 17, 17, 17, 14, 0, 0])
  149. menu.lcd.create_char(3, [0, 14, 31, 31, 31, 14, 0, 0])
  150. menu.lcd.create_char(4, [0, 4, 14, 0, 0, 14, 4, 0]) # Up down arrow
  151. menu.lcd.create_char(5, [0, 0, 10, 27, 10, 0, 0, 0]) # Left right arrow
  152. self.is_setup = True
  153.  
  154. hour = float(time.strftime('%H'))
  155. brightness = 1.0
  156.  
  157. menu.write_row(0, time.strftime(' %a %H:%M:%S '))
  158.  
  159.  
  160. if self.binary:
  161. binary_hour = str(bin(int(time.strftime('%I'))))[2:].zfill(4).replace('0', chr(0)).replace('1', chr(1))
  162. binary_min = str(bin(int(time.strftime('%M'))))[2:].zfill(6).replace('0', chr(2)).replace('1', chr(3))
  163. binary_sec = str(bin(int(time.strftime('%S'))))[2:].zfill(6).replace('0', chr(0)).replace('1', chr(1))
  164. menu.write_row(1, binary_hour + binary_min + binary_sec)
  165. else:
  166. menu.write_row(1, '-' * 16)
  167.  
  168. if Myclock.is_auto():
  169. if Myclock.get_status() != required_status:
  170. if required_status == 'day':
  171. Myclock.daylights_on()
  172. if required_status == 'night':
  173. Myclock.nightlights_on()
  174. if required_status == 'off':
  175. Myclock.lights_off()
  176.  
  177. if self.idling:
  178. menu.clear_row(2)
  179. return True
  180.  
  181. bottom_row = ''
  182.  
  183. if self.modes[self.mode] == 'date':
  184. bottom_row = time.strftime('%b %Y:%m:%d ')
  185. elif self.modes[self.mode] == 'week':
  186. bottom_row = time.strftime(' Week: %W')
  187. elif self.modes[self.mode] == 'binary':
  188. bottom_row = ' Binary ' + chr(5) + ('Y' if self.binary else 'N')
  189. elif self.modes[self.mode] == 'day':
  190. bottom_row = ' day at ' + chr(5) + str(self.day_hour).zfill(2)
  191. elif self.modes[self.mode] == 'night':
  192. bottom_row = ' Night at ' + chr(5) + str(self.night_hour).zfill(2)
  193. elif self.modes[self.mode] == 'manual':
  194. bottom_row = ' Manual ' + chr(5) + ('Y' if self.manual else 'N')
  195. elif self.modes[self.mode] == 'auto':
  196. bottom_row = ' Auto ' + chr(5) + ('Y' if self.auto else 'N')
  197.  
  198. menu.write_row(2, chr(4) + bottom_row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement