Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ... all your import/setup stuff ...
- import sys
- import time
- import dothat.backlight as backlight
- import dothat.lcd as lcd
- import dothat.touch as nav
- from dot3k.menu import Menu, MenuOption
- from time import sleep
- sys.path.append ('/usr/local/lib/python2.7/dist-packages')
- sys.path.append('/home/pi/Pimoroni/displayotron/examples')
- sys.path.append('/home/pi/.local/lib/python2.7/site-packages')
- sys.path.append('/home/pi/Aquarium/')
- from plugins.clock import Clock
- from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
- from plugins.text import Text
- from plugins.utils import Backlight, Contrast
- import RPi.GPIO as GPIO
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- GPIO.setup(5,GPIO.OUT)
- class CustomMenu(Menu):
- def __init__( self, *args, **kwargs ):
- self.auto = None
- self.toggle = False
- self.day_hour = 8
- self.night_hour = 20
- self.off_hour = 23
- Menu.__init__(self, *args, **kwargs)
- def light_logic( self ):
- # your light logic stuff
- hour = float(time.strftime('%H'))
- if (self.off_hour <= self.day_hour) and (self.off_hour <= self.night_hour): #A
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
- elif (self.off_hour >= self.day_hour) and (self.off_hour >= self.night_hour): #B
- day_range = range(self.day_hour, self.night_hour)
- night_range = range(self.night_hour, self.off_hour)
- elif (self.off_hour >= self.day_hour) and (self.off_hour <= self.night_hour): #C
- day_range = range(self.day_hour, self.off_hour)
- night_range = list(range(0, self.day_hour)) + list(range(self.night_hour, 24))
- elif self.off_hour == self.day_hour: #D
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
- elif (self.off_hour >= self.night_hour) and (self.off_hour < self.day_hour):
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.night_hour)) + list(range(self.off_hour, 24))
- if hour in day_range:
- self.daylights_on()
- elif hour in night_range:
- self.nightlights_on()
- else:
- self.lights_off()
- print('Hour not found in any range!')
- def set_lighting_options( self, auto, day_hour, night_hour, off_hour ):
- self.auto = auto
- self.day_hour = day_hour
- self.night_hour = night_hour
- self.off_hour = off_hour
- def daylights_on(self): #Activates Daylights
- self._current_status = 'day'
- GPIO.output(13,0)
- GPIO.output(5,1)
- GPIO.output(13,1)
- print("DayLights: On")
- def nightlights_on(self): #Activates Nightlights
- self._current_status = 'night'
- GPIO.output(13,0)
- GPIO.output(5,1)
- GPIO.output(13,0)
- print("NightLights: On")
- def lights_off(self): # Deactivates any light mode
- self._current_status = 'off'
- GPIO.output(5,0)
- print("Lights: Off")
- def redraw( self ):
- # override redraw function
- if self.auto:
- self.light_logic()
- elif self.toggle == True:
- self.daylights_on()
- else:
- self.nightlights_on()
- Menu.redraw( self )
- class LightingControl( MenuOption ):
- # this is your menuoption class with
- def light_logic( self ):
- # your light logic stuff
- hour = float(time.strftime('%H'))
- if (self.off_hour <= self.day_hour) and (self.off_hour <= self.night_hour): #A
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
- elif (self.off_hour >= self.day_hour) and (self.off_hour >= self.night_hour): #B
- day_range = range(self.day_hour, self.night_hour)
- night_range = range(self.night_hour, self.off_hour)
- elif (self.off_hour >= self.day_hour) and (self.off_hour <= self.night_hour): #C
- day_range = range(self.day_hour, self.off_hour)
- night_range = list(range(0, self.day_hour)) + list(range(self.night_hour, 24))
- elif self.off_hour == self.day_hour: #D
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.off_hour)) + list(range(self.night_hour, 24))
- elif (self.off_hour >= self.night_hour) and (self.off_hour < self.day_hour):
- day_range = range(self.day_hour, self.night_hour)
- night_range = list(range(0, self.night_hour)) + list(range(self.off_hour, 24))
- if hour in day_range:
- self.daylights_on()
- elif hour in night_range:
- self.nightlights_on()
- else:
- self.lights_off()
- print('Hour not found in any range!')
- # ... other functions you need to turn nightlights on/off etc.
- # begin, setup, update_options, load_options, right, left, down, up, etc...
- def redraw( self, menu ):
- # here is where you can update the menu's light options
- menu.set_lighting_options( self.auto, self.day_hour, self.night_hour, self.off_hour )
- # ... everything else that should be in your menuoption's redraw()
- if not self.running:
- return False
- if self.millis() - self.option_time > 5000 and self.option_time > 0:
- self.option_time = 0
- self.mode = 0
- if not self.is_setup:
- menu.lcd.create_char(0, [0, 0, 0, 14, 17, 17, 14, 0])
- menu.lcd.create_char(1, [0, 0, 0, 14, 31, 31, 14, 0])
- menu.lcd.create_char(2, [0, 14, 17, 17, 17, 14, 0, 0])
- menu.lcd.create_char(3, [0, 14, 31, 31, 31, 14, 0, 0])
- menu.lcd.create_char(4, [0, 4, 14, 0, 0, 14, 4, 0]) # Up down arrow
- menu.lcd.create_char(5, [0, 0, 10, 27, 10, 0, 0, 0]) # Left right arrow
- self.is_setup = True
- hour = float(time.strftime('%H'))
- print(hour)
- menu.write_row(0, time.strftime(' %a %H:%M:%S '))
- if self.binary:
- binary_hour = str(bin(int(time.strftime('%I'))))[2:].zfill(4).replace('0', chr(0)).replace('1', chr(1))
- binary_min = str(bin(int(time.strftime('%M'))))[2:].zfill(6).replace('0', chr(2)).replace('1', chr(3))
- binary_sec = str(bin(int(time.strftime('%S'))))[2:].zfill(6).replace('0', chr(0)).replace('1', chr(1))
- menu.write_row(1, binary_hour + binary_min + binary_sec)
- else:
- menu.write_row(1, '-' * 16)
- if self.idling:
- menu.clear_row(2)
- return True
- bottom_row = ''
- if self.modes[self.mode] == 'date':
- bottom_row = time.strftime('%b %Y:%m:%d ')
- elif self.modes[self.mode] == 'week':
- bottom_row = time.strftime(' Week: %W')
- elif self.modes[self.mode] == 'binary':
- bottom_row = ' Binary ' + chr(5) + ('Y' if self.binary else 'N')
- elif self.modes[self.mode] == 'auto':
- bottom_row = ' Auto ' + chr(5) + ('Y' if self.auto else 'N')
- elif self.modes[self.mode] == 'toggle':
- bottom_row = ' Toggle ' + chr(5) + ('Day' if self.toggle else 'Night')
- elif self.modes[self.mode] == 'day':
- bottom_row = ' Day at ' + chr(5) + str(self.day_hour).zfill(2)
- elif self.modes[self.mode] == 'night':
- bottom_row = ' Night at ' + chr(5) + str(self.night_hour).zfill(2)
- elif self.modes[self.mode] == 'off':
- bottom_row = ' Off at ' + chr(5) + str(self.off_hour).zfill(2)
- menu.write_row(2, chr(4) + bottom_row)
- #Unordered menu
- menu = CustomMenu(
- structure={
- 'Power Options': {
- 'Reboot':GraphSysReboot(),
- 'Shutdown':GraphSysShutdown(),
- },
- 'Aquarium': {
- 'Lighting': {
- 'Control': LightingControl(),
- }
- },
- 'Clock': Clock(backlight),
- 'Status': {
- 'IP': IPAddress(),
- 'CPU': GraphCPU(backlight),
- 'Temp': GraphTemp()
- },
- 'Settings': {
- 'Display': {
- 'Contrast': Contrast(lcd),
- 'Backlight': Backlight(backlight)
- }
- }
- },
- lcd=lcd,
- input_handler=Text())
- nav.bind_defaults(menu)
- while True:
- menu.redraw()
- time.sleep(1.0 / 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement