def configure(schedule_list): global state config.verbose('Loading state') load_state() state['schedule']['names'] = schedule_list schedule_list.sort() config.verbose('Selected schedule is \'%s\'' % schedule()) config.verbose('Configuring Raspberry PI') GPIO.setwarnings(False) GPIO.cleanup() GPIO.setmode(GPIO.BCM) GPIO.setup(config.get('raspberry', 'led_power'), GPIO.OUT) GPIO.output(config.get('raspberry', 'led_power'), state['active']) for led_pin in config.get('raspberry', 'led_array'): GPIO.setup(led_pin, GPIO.OUT) set_led_array(state['led']) GPIO.setup(config.get('raspberry', 'btn_power'), GPIO.IN) GPIO.setup(config.get('raspberry', 'btn_sched'), GPIO.IN) GPIO.setup(config.get('raspberry', 'btn_macro'), GPIO.IN) button_reader = threading.Thread(target=button_read, daemon=True) button_reader.run() config.verbose('GPIO is active') config.verbose('Executing macro configure.') macro.configure(set_led_array, config.verbose,) save_state()
def start(): global state if state is None: raise ValueError('GPIO is not initialized') config.verbose('Raspberry Pi mode is active') global gpio_thread gpio_thread = threading.Thread(target=gpio_cycle, args=(True, ), daemon=True) gpio_thread.start() while True: if state._active: if not bell_thread.is_alive(): today = datetime.datetime.now().weekday() schedule_queue = None if state._saturday and today == 5: schedule_queue = schedule.today(state._schedule_saturday, force=True) else: schedule_queue = schedule.today(state._schedule) import calendar if not schedule_queue.empty(): config.verbose('Started bell with the schedule for %s' % calendar.day_name[today]) bell_start(schedule_queue) time.sleep(0.5)
def __init__(self): self._active = None self._schedule = None self._saturday = None self._schedule_saturday = None self._led = { 'power': False, 'sched': [0, 0, 0], 'ssche': [0, 0, 0], 'satur': False } if not self.load(): self.active(False) self.saturday(False) self.schedule(name=config.get('bell', 'schedule')) self.schedule(name=config.get('raspberry', 'sch_satur'), saturday=True) self.save() config.verbose('Using default GPIO state values')
def __init__(self): self._active = None self._schedule = None self._saturday = None self._schedule_saturday = None self._led = { 'power': False, 'sched': [0, 0, 0], 'ssche': [0, 0, 0], 'satur': False } if not self.load(): self.active(False) self.saturday(False) self.schedule(name=config.get('bell', 'schedule')) self.schedule(name=config.get( 'raspberry', 'sch_satur'), saturday=True) self.save() config.verbose('Using default GPIO state values')
def start(): global state if state is None: raise ValueError('GPIO is not initialized') config.verbose('Raspberry Pi mode is active') global gpio_thread gpio_thread = threading.Thread( target=gpio_cycle, args=(True,), daemon=True) gpio_thread.start() while True: if state._active: if not bell_thread.is_alive(): today = datetime.datetime.now().weekday() schedule_queue = None if state._saturday and today == 5: schedule_queue = schedule.today( state._schedule_saturday, force=True) else: schedule_queue = schedule.today(state._schedule) import calendar if not schedule_queue.empty(): config.verbose('Started bell with the schedule for %s' % calendar.day_name[today]) bell_start(schedule_queue) time.sleep(0.5)
def action_macro(): config.verbose('Macro button pressed, executing macro.') macro.button( set_led_array, config.verbose, ) save_state()
def gpio_button_ring(): if not state._active: sound_file = schedule.schedules[state._schedule_saturday if state. _saturday else state. _schedule]['default sound'] ringer.ring(sound_file) config.verbose('Ring button pressed!')
def gpio_button_ring(): if not state._active: sound_file = schedule.schedules[ state._schedule_saturday if state._saturday else state._schedule ]['default sound'] ringer.ring(sound_file) config.verbose('Ring button pressed!')
def active(self, value=None): if value is not None: self._active = value else: self._active = not self._active self.led('power', value) self.save() config.verbose('School bell is now %sactive' % ('' if value else 'in'))
def saturday(self, value=None): if value is not None: self._saturday = value else: self._saturday = not self._saturday self.led('satur', value) self.save() config.verbose('School bell is now %sactive on Saturdays' % ('' if self._saturday else 'in'))
def action_schedule(): global state if state['active']: config.verbose('Schedule button pressed, ignoring.') blink(config.get('raspberry', 'led_power'), 0.1, 2) else: state['schedule']['active'] = (state['schedule']['active'] + 1)\ % len(state['schedule']['names']) config.verbose( 'Schedule button pressed, changing active schedule to \'%s\'.' % schedule()) set_led_array(number=state['schedule']['active'] + 1) save_state()
def schedule(self, name=None, next=False, saturday=False): if name is not None and next is True: raise ValueError('Schedule is double defined') schedule_list = schedule.list_names() schedule_list.sort() if name is not None: if saturday: self._schedule_saturday = name self.led(name='ssche', value=schedule_list.index(name) + 1) config.verbose( 'Current Saturday schedule is set to \'%s\'' % name) else: self._schedule = name self.led(name='sched', value=schedule_list.index(name) + 1) config.verbose('Current schedule is set to \'%s\'' % name) if next is True: if saturday: current = schedule_list.index(self._schedule_saturday) name = schedule_list[(current + 1) % len(schedule_list)] self._schedule_saturday = name self.led(name='ssche', value=schedule_list.index(name) + 1) config.verbose( 'Current Saturday schedule is set to \'%s\'' % name) else: current = schedule_list.index(self._schedule) name = schedule_list[(current + 1) % len(schedule_list)] self._schedule = name self.led(name='sched', value=schedule_list.index(name) + 1) config.verbose('Current schedule is set to \'%s\'' % name)
def action_active(): global state state['active'] = not state['active'] config.verbose('Power button pressed, turning %s.' % ('on' if state['active'] else 'off')) if state['active']: set_led_array(values=(False, False, False)) else: set_led_array(number=state['schedule']['active'] + 1) GPIO.output(config.get('raspberry', 'led_power'), state['active']) config.verbose('Executing macro configure.') macro.configure(set_led_array, config.verbose,) save_state()
def schedule(self, name=None, next=False, saturday=False): if name is not None and next is True: raise ValueError('Schedule is double defined') schedule_list = schedule.list_names() schedule_list.sort() if name is not None: if saturday: self._schedule_saturday = name self.led(name='ssche', value=schedule_list.index(name) + 1) config.verbose('Current Saturday schedule is set to \'%s\'' % name) else: self._schedule = name self.led(name='sched', value=schedule_list.index(name) + 1) config.verbose('Current schedule is set to \'%s\'' % name) if next is True: if saturday: current = schedule_list.index(self._schedule_saturday) name = schedule_list[(current + 1) % len(schedule_list)] self._schedule_saturday = name self.led(name='ssche', value=schedule_list.index(name) + 1) config.verbose('Current Saturday schedule is set to \'%s\'' % name) else: current = schedule_list.index(self._schedule) name = schedule_list[(current + 1) % len(schedule_list)] self._schedule = name self.led(name='sched', value=schedule_list.index(name) + 1) config.verbose('Current schedule is set to \'%s\'' % name)
def gpio_connected(): check_state_new_1 = bool(GPIO.input(config.get('raspberry', 'pin_check'))) time.sleep(0.05) check_state_new_2 = bool(GPIO.input(config.get('raspberry', 'pin_check'))) global connected connected_new = check_state_new_1 and check_state_new_2 if connected_new != connected: config.verbose('Control board %sconnected' % ('' if connected_new else 'dis')) time.sleep(1) connected = connected_new return connected
def gpio_setup(): config.verbose('Setting up GPIO pins') GPIO.setwarnings(False) GPIO.cleanup() GPIO.setmode(GPIO.BCM) GPIO.setup(gpio['led']['power'], GPIO.OUT) for pin in gpio['led']['sched']: GPIO.setup(pin, GPIO.OUT) GPIO.setup(gpio['led']['satur'], GPIO.OUT) for button in gpio['button']: GPIO.setup(gpio['button'][button], GPIO.IN) GPIO.setup(gpio['check'], GPIO.IN)
def action_active(): global state state['active'] = not state['active'] config.verbose('Power button pressed, turning %s.' % ('on' if state['active'] else 'off')) if state['active']: set_led_array(values=(False, False, False)) else: set_led_array(number=state['schedule']['active'] + 1) GPIO.output(config.get('raspberry', 'led_power'), state['active']) config.verbose('Executing macro configure.') macro.configure( set_led_array, config.verbose, ) save_state()
def load_state(): use_default = True global state if os.path.isfile(config.directory + '.gpio_state'): try: with open(config.directory + '.gpio_state', 'rb') as pickle_file: state = pickle.load(pickle_file) use_default = False except Exception: config.verbose( 'Previous GPIO state could not be restored, using defaults') if use_default: config.verbose('No GPIO state file found, using defaults') state = { 'active': True, 'led': (False, False, False), 'schedule': { 'active': 0, 'names': ['default'] }, 'macro': None }
def configure(schedule_list): global state config.verbose('Loading state') load_state() state['schedule']['names'] = schedule_list schedule_list.sort() config.verbose('Selected schedule is \'%s\'' % schedule()) config.verbose('Configuring Raspberry PI') GPIO.setwarnings(False) GPIO.cleanup() GPIO.setmode(GPIO.BCM) GPIO.setup(config.get('raspberry', 'led_power'), GPIO.OUT) GPIO.output(config.get('raspberry', 'led_power'), state['active']) for led_pin in config.get('raspberry', 'led_array'): GPIO.setup(led_pin, GPIO.OUT) set_led_array(state['led']) GPIO.setup(config.get('raspberry', 'btn_power'), GPIO.IN) GPIO.setup(config.get('raspberry', 'btn_sched'), GPIO.IN) GPIO.setup(config.get('raspberry', 'btn_macro'), GPIO.IN) button_reader = threading.Thread(target=button_read, daemon=True) button_reader.run() config.verbose('GPIO is active') config.verbose('Executing macro configure.') macro.configure( set_led_array, config.verbose, ) save_state()
def start(schedule, active): while not schedule.empty() and active(): next_time = schedule.get() config.verbose('Next bell is at %s:%s:%s' % (next_time[0], next_time[1], next_time[2])) while next_time > current_time(): time.sleep(0.1) if not active(): config.verbose('Terminating ringer') return ring(next_time[3]) if not active(): config.verbose('Terminating ringer') return
def main(): if config.get('raspberry', 'gpio'): config.verbose('Running in Raspberry Pi mode') import raspberry raspberry.initialize() raspberry.start() else: config.verbose('Running in regular mode') while True: schedule_name = config.get('bell', 'schedule') schedule_queue = schedule.today(schedule_name) if not schedule_queue.empty(): config.verbose('Schedule: %s' % schedule_name) ringer.start(schedule_queue, lambda: True) schedule.wait_tomorrow()
def load(self): if os.path.isfile(config.directory + '.gpio_state'): try: with open(config.directory + '.gpio_state', 'rb') as pickle_file: loaded_state = pickle.load(pickle_file) self._active = loaded_state._active self._schedule = loaded_state._schedule self._saturday = loaded_state._saturday self._schedule_saturday = loaded_state._schedule_saturday self._led = loaded_state._led config.verbose('Loaded previous GPIO state') config.verbose('Active: %s\n' % self._active + 'Schedule: %s\n' % self._schedule + 'Saturday: %s\n' % self._saturday + 'Schedule saturday: %s\n' % self._schedule_saturday + 'LED: %s' % self._led) return True except Exception: config.verbose('Previous GPIO state could not be restored') else: return False
def ring(sound_file): sound_file_target = config.directory + 'sounds' + sep + sound_file Popen([config.get('bell', 'player'), sound_file_target]) config.verbose('Playing %s with %s' % (sound_file_target, config.get('bell', 'player')))
def action_macro(): config.verbose('Macro button pressed, executing macro.') macro.button(set_led_array, config.verbose,) save_state()
def gpio_button_power(): state.active(not state._active) config.verbose('Power button pressed!')
def gpio_button_schedule(): if not state._active: state.schedule(next=True, saturday=state._saturday) config.verbose('Schedule button pressed!')
def gpio_button_saturday(): if not state._active: state.saturday(not state._saturday) config.verbose('Saturday button pressed!')