コード例 #1
0
ファイル: lcd.py プロジェクト: jarulsamy/Rpi-Clock
def main():
    global BACKLIGHT_STATUS
    ALARM_TIME, TONE, DURATION, WEEKDAYS_ONLY, ENABLED = load_config(SETTINGS)

    # Initialize the buttons
    GPIO.setmode(GPIO.BCM)

    GPIO.setup(GREEN_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(BLUE_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(RED_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

    # Backlight Toggle
    GPIO.setup(BACKLIGHT, GPIO.OUT)
    GPIO.output(BACKLIGHT, BACKLIGHT_STATUS)
    GPIO.add_event_detect(GREEN_BUTTON, GPIO.RISING, callback=toggle_backlight)

    # Initialize the lcd object
    lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5,
                                          lcd_d6, lcd_d7, LCD_COLUMNS,
                                          LCD_ROWS)
    lcd.clear()

    # Initialize logger
    logging.basicConfig(filename="clock.log",
                        format='%(asctime)s - %(message)s',
                        datefmt='%d-%b-%y %H:%M:%S',
                        level=logging.INFO)

    alarm = Alarm(ALARM_TIME, lcd, TONE, DURATION)
    if ENABLED:
        alarm.start()
        logging.info("Started - Alarm ENABLED")

    # Setup the logger
    logging.basicConfig(filename="Rpi-Clock.log",
                        level=LOGGING_LEVEL,
                        format='%(asctime)s - %(message)s',
                        datefmt='%d-%b-%y %H:%M:%S')
    logging.info(START_TEXT)

    try:
        while True:
            # Don't overwrite alarm text with stock times
            # Ensure backlight is on during alarm
            while alarm.alarming:
                # Stop the alarm with blue button press.
                while GPIO.input(BLUE_BUTTON) == GPIO.HIGH:
                    alarm.stop_alarm()
                    message(lcd, "STOPPED ALARM")
                    logging.info("Stopped alarm from BLUE button.")
                    BACKLIGHT_STATUS = True
                    GPIO.output(BACKLIGHT, BACKLIGHT_STATUS)
                sleep(1)

            # Grab and display time
            lcd.message = get_formatted_times(ALARM_TIME.strftime("%H:%M"),
                                              weekdays_only=WEEKDAYS_ONLY,
                                              enabled=ENABLED)
            logging.debug("Updated time")

            # Power off if red button is held for 3 seconds
            while GPIO.input(RED_BUTTON) == GPIO.HIGH:
                for i in range(3):
                    if i == 0:
                        lcd.clear()
                    lcd.message = f"Shutting down {3 - i}"
                    if i == 3:
                        logging.info("Poweroff from RED button.")
                        safe_exit(lcd, alarm)
                    sleep(1)

            # Reload the config on blue button press.
            while GPIO.input(BLUE_BUTTON) == GPIO.HIGH and not alarm.alarming:
                ALARM_TIME, TONE, DURATION, WEEKDAYS_ONLY, ENABLED = load_config(
                    SETTINGS)
                alarm.kill()

                if ENABLED:
                    logging.info("Config Reloaded - Alarm Enabled")
                    alarm = Alarm(ALARM_TIME, lcd, TONE, DURATION)
                    alarm.start()
                else:
                    logging.info("Config Reloaded - Alarm Disabled")

                message(lcd, "Reloaded Config")
            sleep(1)

    except KeyboardInterrupt:
        safe_exit(lcd, alarm)

    except Exception:
        logging.fatal("Exception occured", exc_info=True)