Advertisement
trippyt

test2

Jun 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. # ... all your import/setup stuff ...
  2. import sys
  3. import time
  4. import dothat.backlight as backlight
  5. import dothat.lcd as lcd
  6. import dothat.touch as nav
  7. from dot3k.menu import Menu, MenuOption
  8. from time import sleep
  9. sys.path.append ('/usr/local/lib/python2.7/dist-packages')
  10. sys.path.append('/home/pi/Pimoroni/displayotron/examples')
  11. sys.path.append('/home/pi/.local/lib/python2.7/site-packages')
  12. sys.path.append('/home/pi/Aquarium/')
  13. from plugins.clock import Clock
  14. from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
  15. from plugins.text import Text
  16. from plugins.utils import Backlight, Contrast
  17. import RPi.GPIO as GPIO
  18. GPIO.setwarnings(False)
  19. GPIO.setup(13,GPIO.OUT)
  20. GPIO.setup(5,GPIO.OUT)
  21. class CustomMenu(Menu):
  22.  
  23. def __init__( self, *args, **kwargs ):
  24. self.auto = None
  25. self.toggle = False
  26. self.day_hour = 8
  27. self.night_hour = 20
  28. self.off_hour = 23
  29. Menu.__init__(self, *args, **kwargs)
  30.  
  31. def light_logic( self ):
  32. # your light logic stuff
  33. hour = float(time.strftime('%H'))
  34. if (self.off_hour <= self.day_hour) and (self.off_hour <= self.night_hour): #A
  35. day_range = range(self.day_hour, self.night_hour)
  36. night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
  37.  
  38. elif (self.off_hour >= self.day_hour) and (self.off_hour >= self.night_hour): #B
  39. day_range = range(self.day_hour, self.night_hour)
  40. night_range = range(self.night_hour, self.off_hour)
  41.  
  42. elif (self.off_hour >= self.day_hour) and (self.off_hour <= self.night_hour): #C
  43. day_range = range(self.day_hour, self.off_hour)
  44. night_range = list(range(0, self.day_hour)) + list(range(self.night_hour, 24))
  45. elif self.off_hour == self.day_hour: #D
  46. day_range = range(self.day_hour, self.night_hour)
  47. night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
  48. elif (self.off_hour >= self.night_hour) and (self.off_hour < self.day_hour):
  49. day_range = range(self.day_hour, self.night_hour)
  50. night_range = list(range(0, self.night_hour)) + list(range(self.off_hour, 24))
  51.  
  52. if hour in day_range:
  53. self.daylights_on()
  54. elif hour in night_range:
  55. self.nightlights_on()
  56. else:
  57. self.lights_off()
  58. print('Hour not found in any range!')
  59.  
  60. def set_lighting_options( self, auto, day_hour, night_hour, off_hour ):
  61. self.auto = auto
  62. self.day_hour = day_hour
  63. self.night_hour = night_hour
  64. self.off_hour = off_hour
  65.  
  66. def daylights_on(self): #Activates Daylights
  67. self._current_status = 'day'
  68. GPIO.output(13,0)
  69. GPIO.output(5,1)
  70. GPIO.output(13,1)
  71. print("DayLights: On")
  72.  
  73. def nightlights_on(self): #Activates Nightlights
  74. self._current_status = 'night'
  75. GPIO.output(13,0)
  76. GPIO.output(5,1)
  77. GPIO.output(13,0)
  78. print("NightLights: On")
  79.  
  80. def lights_off(self): # Deactivates any light mode
  81. self._current_status = 'off'
  82. GPIO.output(5,0)
  83. print("Lights: Off")
  84.  
  85. def redraw( self ):
  86. # override redraw function
  87. if self.auto:
  88. self.light_logic()
  89. elif self.toggle == True:
  90. self.daylights_on()
  91. else:
  92. self.nightlights_on()
  93.  
  94. Menu.redraw( self )
  95.  
  96.  
  97. class LightingControl( MenuOption ):
  98. # this is your menuoption class with
  99. def light_logic( self ):
  100. # your light logic stuff
  101. hour = float(time.strftime('%H'))
  102. if (self.off_hour <= self.day_hour) and (self.off_hour <= self.night_hour): #A
  103. day_range = range(self.day_hour, self.night_hour)
  104. night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
  105.  
  106. elif (self.off_hour >= self.day_hour) and (self.off_hour >= self.night_hour): #B
  107. day_range = range(self.day_hour, self.night_hour)
  108. night_range = range(self.night_hour, self.off_hour)
  109.  
  110. elif (self.off_hour >= self.day_hour) and (self.off_hour <= self.night_hour): #C
  111. day_range = range(self.day_hour, self.off_hour)
  112. night_range = list(range(0, self.day_hour)) + list(range(self.night_hour, 24))
  113. elif self.off_hour == self.day_hour: #D
  114. day_range = range(self.day_hour, self.night_hour)
  115. night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
  116. elif (self.off_hour >= self.night_hour) and (self.off_hour < self.day_hour):
  117. day_range = range(self.day_hour, self.night_hour)
  118. night_range = list(range(0, self.night_hour)) + list(range(self.off_hour, 24))
  119.  
  120. if hour in day_range:
  121. self.daylights_on()
  122. elif hour in night_range:
  123. self.nightlights_on()
  124. else:
  125. self.lights_off()
  126. print('Hour not found in any range!')
  127. # ... other functions you need to turn nightlights on/off etc.
  128. # begin, setup, update_options, load_options, right, left, down, up, etc...
  129.  
  130. def redraw( self, menu ):
  131. # here is where you can update the menu's light options
  132. menu.set_lighting_options( self.auto, self.day_hour, self.night_hour, self.off_hour )
  133. # ... everything else that should be in your menuoption's redraw()
  134. if not self.running:
  135. return False
  136.  
  137. if self.millis() - self.option_time > 5000 and self.option_time > 0:
  138. self.option_time = 0
  139. self.mode = 0
  140.  
  141. if not self.is_setup:
  142. menu.lcd.create_char(0, [0, 0, 0, 14, 17, 17, 14, 0])
  143. menu.lcd.create_char(1, [0, 0, 0, 14, 31, 31, 14, 0])
  144. menu.lcd.create_char(2, [0, 14, 17, 17, 17, 14, 0, 0])
  145. menu.lcd.create_char(3, [0, 14, 31, 31, 31, 14, 0, 0])
  146. menu.lcd.create_char(4, [0, 4, 14, 0, 0, 14, 4, 0]) # Up down arrow
  147. menu.lcd.create_char(5, [0, 0, 10, 27, 10, 0, 0, 0]) # Left right arrow
  148. self.is_setup = True
  149.  
  150. hour = float(time.strftime('%H'))
  151. print(hour)
  152. menu.write_row(0, time.strftime(' %a %H:%M:%S '))
  153.  
  154. if self.binary:
  155. binary_hour = str(bin(int(time.strftime('%I'))))[2:].zfill(4).replace('0', chr(0)).replace('1', chr(1))
  156. binary_min = str(bin(int(time.strftime('%M'))))[2:].zfill(6).replace('0', chr(2)).replace('1', chr(3))
  157. binary_sec = str(bin(int(time.strftime('%S'))))[2:].zfill(6).replace('0', chr(0)).replace('1', chr(1))
  158. menu.write_row(1, binary_hour + binary_min + binary_sec)
  159. else:
  160. menu.write_row(1, '-' * 16)
  161.  
  162. if self.idling:
  163. menu.clear_row(2)
  164. return True
  165.  
  166. bottom_row = ''
  167.  
  168. if self.modes[self.mode] == 'date':
  169. bottom_row = time.strftime('%b %Y:%m:%d ')
  170. elif self.modes[self.mode] == 'week':
  171. bottom_row = time.strftime(' Week: %W')
  172. elif self.modes[self.mode] == 'binary':
  173. bottom_row = ' Binary ' + chr(5) + ('Y' if self.binary else 'N')
  174. elif self.modes[self.mode] == 'auto':
  175. bottom_row = ' Auto ' + chr(5) + ('Y' if self.auto else 'N')
  176. elif self.modes[self.mode] == 'toggle':
  177. bottom_row = ' Toggle ' + chr(5) + ('Day' if self.toggle else 'Night')
  178. elif self.modes[self.mode] == 'day':
  179. bottom_row = ' Day at ' + chr(5) + str(self.day_hour).zfill(2)
  180. elif self.modes[self.mode] == 'night':
  181. bottom_row = ' Night at ' + chr(5) + str(self.night_hour).zfill(2)
  182. elif self.modes[self.mode] == 'off':
  183. bottom_row = ' Off at ' + chr(5) + str(self.off_hour).zfill(2)
  184.  
  185. menu.write_row(2, chr(4) + bottom_row)
  186.  
  187. #Unordered menu
  188. menu = CustomMenu(
  189. structure={
  190. 'Power Options': {
  191. 'Reboot':GraphSysReboot(),
  192. 'Shutdown':GraphSysShutdown(),
  193. },
  194. 'Aquarium': {
  195. 'Lighting': {
  196. 'Control': LightingControl(),
  197. }
  198. },
  199. 'Clock': Clock(backlight),
  200. 'Status': {
  201. 'IP': IPAddress(),
  202. 'CPU': GraphCPU(backlight),
  203. 'Temp': GraphTemp()
  204. },
  205. 'Settings': {
  206. 'Display': {
  207. 'Contrast': Contrast(lcd),
  208. 'Backlight': Backlight(backlight)
  209. }
  210. }
  211. },
  212. lcd=lcd,
  213.  
  214. input_handler=Text())
  215.  
  216. nav.bind_defaults(menu)
  217.  
  218. while True:
  219. menu.redraw()
  220. time.sleep(1.0 / 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement