Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from dot3k.menu import MenuOption
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- GPIO.setup(5,GPIO.OUT)
- class Myclock(MenuOption):
- def __init__(self, backlight=None):
- self.modes = ['date', 'week', 'binary', 'day', 'night','manual', 'auto']
- self.mode = 0
- self.binary = True
- self.manual = True
- self.auto = True
- self.running = False
- self.option_time = 0
- self.day_hour = 20
- self.night_hour = 8
- self.is_setup = False
- self._status = False
- MenuOption.__init__(self)
- def get_status(self):
- return self._current_status
- def is_on(self):
- return self._status
- def is_auto(self):
- return self._auto
- def begin(self):
- self.is_setup = False
- self.running = True
- def setup(self, config):
- MenuOption.setup(self, config)
- self.load_options()
- def update_options(self):
- self.set_option('myclock', 'day', str(self.day_hour))
- self.set_option('myclock', 'night', str(self.night_hour))
- self.set_option('myclock', 'binary', str(self.binary))
- self.set_option('myclock', 'manual', str(self.manual))
- self.set_option('myclock', 'auto', str(self.auto))
- def load_options(self):
- self.day_hour = int(self.get_option('myclock', 'day', str(self.day_hour)))
- self.night_hour = int(self.get_option('myclock', 'night', str(self.night_hour)))
- self.binary = self.get_option('myclock', 'binary', str(self.binary)) == 'True'
- self.manual = self.get_option('myclock', 'manual', str(self.manual)) == 'False'
- self.auto = self.get_option('myclock', 'auto', str(self.auto)) == 'True'
- def cleanup(self):
- self.running = False
- time.sleep(0.01)
- self.is_setup = False
- def left(self):
- if self.modes[self.mode] == 'binary':
- self.binary = False
- elif self.modes[self.mode] == 'day':
- self.day_hour = (self.day_hour - 1) % 24
- elif self.modes[self.mode] == 'night':
- self.night_hour = (self.night_hour - 1) % 24
- elif self.modes[self.mode] == 'manual':
- self.manual = False
- elif self.modes[self.mode] == 'auto':
- self.auto = False
- else:
- return False
- self.update_options()
- self.option_time = self.millis()
- return True
- def right(self):
- if self.modes[self.mode] == 'binary':
- self.binary = True
- elif self.modes[self.mode] == 'day':
- self.day_hour = (self.day_hour + 1) % 24
- elif self.modes[self.mode] == 'night':
- self.night_hour = (self.night_hour + 1) % 24
- elif self.modes[self.mode] == 'manual':
- self.manual = True
- elif self.modes[self.mode] == 'auto':
- self.auto =True
- self.update_options()
- self.option_time = self.millis()
- return True
- def up(self):
- self.mode = (self.mode - 1) % len(self.modes)
- self.option_time = self.millis()
- return True
- def down(self):
- self.mode = (self.mode + 1) % len(self.modes)
- self.option_time = self.millis()
- return True
- def daylights_on(self):
- GPIO.output(13,0)
- GPIO.output(5,1)
- GPIO.output(13,1)
- self._status = True
- def nightlights_on(self):
- GPIO.output(13,0)
- GPIO.output(5,1)
- GPIO.output(13,0)
- self._status = True
- def auto_on(self): # Sets an automatic timer to activate lights
- self._auto = True
- def auto_off(self): # Sets an automatic timer to dectivate lights
- self._auto = False
- required_status = 'off'
- def sched_nightlights():
- global required_status
- required_status = 'night'
- def sched_daylights():
- global required_status
- required_status = 'day'
- def sched_off():
- global required_status
- required_status = 'off'
- def redraw(self, menu):
- 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'))
- brightness = 1.0
- 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 Myclock.is_auto():
- if Myclock.get_status() != required_status:
- if required_status == 'day':
- Myclock.daylights_on()
- if required_status == 'night':
- Myclock.nightlights_on()
- if required_status == 'off':
- Myclock.lights_off()
- 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] == '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] == 'manual':
- bottom_row = ' Manual ' + chr(5) + ('Y' if self.manual else 'N')
- elif self.modes[self.mode] == 'auto':
- bottom_row = ' Auto ' + chr(5) + ('Y' if self.auto else 'N')
- menu.write_row(2, chr(4) + bottom_row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement