Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import datetime
- 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','off']
- self.mode = 0
- self.binary = True
- self.running = False
- self.option_time = 0
- self.day_hour = 8
- self.night_hour = 20
- self.off_hour = 22
- self.is_setup = False
- MenuOption.__init__(self)
- 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('Clock', 'day', str(self.day_hour))
- self.set_option('Clock', 'night', str(self.night_hour))
- self.set_option('Clock', 'binary', str(self.binary))
- self.set_option('Clock', 'off', str(self.off_hour))
- def load_options(self):
- self.day_hour = int(self.get_option('Clock', 'day', str(self.day_hour)))
- self.night_hour = int(self.get_option('Clock', 'night', str(self.night_hour)))
- self.off = int(self.get_option('Clock', 'off', str(self.off_hour)))
- self.binary = self.get_option('Clock', 'binary', str(self.binary)) == '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] == 'off':
- self.off_hour = (self.off_hour - 1) % 24
- 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] == 'off':
- self.off_hour = (self.off_hour + 1) % 24
- 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)
- def nightlights_on(self):
- GPIO.output(13,0)
- GPIO.output(5,1)
- GPIO.output(13,0)
- def lights_off(self):
- GPIO.output(13,0)
- GPIO.output(5,0)
- def get_day_cycle(self, daylights_on, lights_off, nightlights_on):
- current_hour = datetime.datetime.now().hour
- lights_on %= 24
- lights_off %= 24
- nightlights_on %= 24
- if np.intersect1d(lights_on, lights_off).size or np.intersect1d(lights_on, nightlights_on).size or np.intersect1d(lights_off, nightlights_on).size:
- return 'ERROR: OVERLAP IN TIME RANGES'
- if current_hour in daylights_on:
- return 'lights on'
- elif current_hour in lights_off:
- return 'lights off'
- elif current_hour in nightlights_on:
- return 'nightlights on'
- else:
- return 'ERROR: RANGE NOT FILLED'
- def redraw(self, menu):
- self.get_day_cycle(self.daylights_on, self.lights_off, self.nightlights_on)
- 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'))
- if self.get_day_cycle() == "morning":
- self.daylights_on()
- elif self.get_day_cycle() == "night":
- self.nightlights_on()
- 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] == '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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement