Advertisement
trippyt

1.3

Jun 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. import time
  2. import datetime
  3. from dot3k.menu import MenuOption
  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. class Myclock(MenuOption):
  11. def __init__(self, backlight=None):
  12. self.modes = ['date', 'week', 'binary', 'day', 'night','off']
  13. self.mode = 0
  14. self.binary = True
  15. self.running = False
  16. self.option_time = 0
  17. self.day_hour = 8
  18. self.night_hour = 20
  19. self.off_hour = 22
  20. self.is_setup = False
  21. MenuOption.__init__(self)
  22.  
  23. def begin(self):
  24. self.is_setup = False
  25. self.running = True
  26.  
  27. def setup(self, config):
  28. MenuOption.setup(self, config)
  29. self.load_options()
  30.  
  31. def update_options(self):
  32. self.set_option('Clock', 'day', str(self.day_hour))
  33. self.set_option('Clock', 'night', str(self.night_hour))
  34. self.set_option('Clock', 'binary', str(self.binary))
  35. self.set_option('Clock', 'off', str(self.off_hour))
  36.  
  37. def load_options(self):
  38. self.day_hour = int(self.get_option('Clock', 'day', str(self.day_hour)))
  39. self.night_hour = int(self.get_option('Clock', 'night', str(self.night_hour)))
  40. self.off = int(self.get_option('Clock', 'off', str(self.off_hour)))
  41. self.binary = self.get_option('Clock', 'binary', str(self.binary)) == 'True'
  42. def cleanup(self):
  43. self.running = False
  44. time.sleep(0.01)
  45. self.is_setup = False
  46.  
  47. def left(self):
  48. if self.modes[self.mode] == 'binary':
  49. self.binary = False
  50. elif self.modes[self.mode] == 'day':
  51. self.day_hour = (self.day_hour - 1) % 24
  52. elif self.modes[self.mode] == 'night':
  53. self.night_hour = (self.night_hour - 1) % 24
  54. elif self.modes[self.mode] == 'off':
  55. self.off_hour = (self.off_hour - 1) % 24
  56. else:
  57. return False
  58. self.update_options()
  59. self.option_time = self.millis()
  60. return True
  61.  
  62. def right(self):
  63. if self.modes[self.mode] == 'binary':
  64. self.binary = True
  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] == 'off':
  70. self.off_hour = (self.off_hour + 1) % 24
  71. self.update_options()
  72. self.option_time = self.millis()
  73. return True
  74.  
  75. def up(self):
  76. self.mode = (self.mode - 1) % len(self.modes)
  77. self.option_time = self.millis()
  78. return True
  79.  
  80. def down(self):
  81. self.mode = (self.mode + 1) % len(self.modes)
  82. self.option_time = self.millis()
  83. return True
  84.  
  85. def daylights_on(self):
  86. GPIO.output(13,0)
  87. GPIO.output(5,1)
  88. GPIO.output(13,1)
  89.  
  90. def nightlights_on(self):
  91. GPIO.output(13,0)
  92. GPIO.output(5,1)
  93. GPIO.output(13,0)
  94.  
  95. def lights_off(self):
  96. GPIO.output(13,0)
  97. GPIO.output(5,0)
  98.  
  99. def get_day_cycle(self, daylights_on, lights_off, nightlights_on):
  100. current_hour = datetime.datetime.now().hour
  101. lights_on %= 24
  102. lights_off %= 24
  103. nightlights_on %= 24
  104. if np.intersect1d(lights_on, lights_off).size or np.intersect1d(lights_on, nightlights_on).size or np.intersect1d(lights_off, nightlights_on).size:
  105. return 'ERROR: OVERLAP IN TIME RANGES'
  106. if current_hour in daylights_on:
  107. return 'lights on'
  108. elif current_hour in lights_off:
  109. return 'lights off'
  110. elif current_hour in nightlights_on:
  111. return 'nightlights on'
  112. else:
  113. return 'ERROR: RANGE NOT FILLED'
  114.  
  115. def redraw(self, menu):
  116. self.get_day_cycle(self.daylights_on, self.lights_off, self.nightlights_on)
  117. if not self.running:
  118. return False
  119.  
  120. if self.millis() - self.option_time > 5000 and self.option_time > 0:
  121. self.option_time = 0
  122. self.mode = 0
  123.  
  124. if not self.is_setup:
  125. menu.lcd.create_char(0, [0, 0, 0, 14, 17, 17, 14, 0])
  126. menu.lcd.create_char(1, [0, 0, 0, 14, 31, 31, 14, 0])
  127. menu.lcd.create_char(2, [0, 14, 17, 17, 17, 14, 0, 0])
  128. menu.lcd.create_char(3, [0, 14, 31, 31, 31, 14, 0, 0])
  129. menu.lcd.create_char(4, [0, 4, 14, 0, 0, 14, 4, 0]) # Up down arrow
  130. menu.lcd.create_char(5, [0, 0, 10, 27, 10, 0, 0, 0]) # Left right arrow
  131. self.is_setup = True
  132.  
  133. hour = float(time.strftime('%H'))
  134.  
  135. if self.get_day_cycle() == "morning":
  136. self.daylights_on()
  137. elif self.get_day_cycle() == "night":
  138. self.nightlights_on()
  139.  
  140. menu.write_row(0, time.strftime(' %a %H:%M:%S '))
  141.  
  142. if self.binary:
  143. binary_hour = str(bin(int(time.strftime('%I'))))[2:].zfill(4).replace('0', chr(0)).replace('1', chr(1))
  144. binary_min = str(bin(int(time.strftime('%M'))))[2:].zfill(6).replace('0', chr(2)).replace('1', chr(3))
  145. binary_sec = str(bin(int(time.strftime('%S'))))[2:].zfill(6).replace('0', chr(0)).replace('1', chr(1))
  146. menu.write_row(1, binary_hour + binary_min + binary_sec)
  147. else:
  148. menu.write_row(1, '-' * 16)
  149.  
  150. if self.idling:
  151. menu.clear_row(2)
  152. return True
  153.  
  154. bottom_row = ''
  155.  
  156. if self.modes[self.mode] == 'date':
  157. bottom_row = time.strftime('%b %Y:%m:%d ')
  158. elif self.modes[self.mode] == 'week':
  159. bottom_row = time.strftime(' Week: %W')
  160. elif self.modes[self.mode] == 'binary':
  161. bottom_row = ' Binary ' + chr(5) + ('Y' if self.binary else 'N')
  162. elif self.modes[self.mode] == 'day':
  163. bottom_row = ' Day at ' + chr(5) + str(self.day_hour).zfill(2)
  164. elif self.modes[self.mode] == 'night':
  165. bottom_row = ' Night at ' + chr(5) + str(self.night_hour).zfill(2)
  166. elif self.modes[self.mode] == 'off':
  167. bottom_row = ' Off at ' + chr(5) + str(self.off_hour).zfill(2)
  168.  
  169. menu.write_row(2, chr(4) + bottom_row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement