Ejemplo n.º 1
0
def main():
    def handle_button(pin):
        index = BUTTONS.index(pin)
        label = LABELS[index]

        if label == "A":  # Select View
            viewcontroller.button_a()

        if label == "B":  # Sleep Alarm
            if not viewcontroller.button_b():
                if viewcontroller.home:
                    if alarm.sleeping():
                        alarm.cancel_sleep()
                    else:
                        alarm.sleep()

        if label == "X":
            viewcontroller.button_x()

        if label == "Y":
            viewcontroller.button_y()

    # Set up the ST7735 SPI Display
    display = ST7735.ST7735(port=0,
                            cs=1,
                            dc=9,
                            backlight=12,
                            rotation=270,
                            spi_speed_hz=80000000)
    display.begin()

    # Set up light sensor
    light = ltr559.LTR559()

    # Set up our canvas and prepare for drawing
    image = Image.new("RGBA", (DISPLAY_WIDTH, DISPLAY_HEIGHT),
                      color=(255, 255, 255))

    # Pick a random selection of plant icons to display on screen
    channels = [
        Channel(1, 1, 1),
        Channel(2, 2, 2),
        Channel(3, 3, 3),
    ]

    alarm = Alarm(image)

    config = Config()

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    for pin in BUTTONS:
        GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=200)

    config.load()

    for channel in channels:
        channel.update_from_yml(config.get_channel(channel.channel))

    alarm.update_from_yml(config.get_general())

    print("Channels:")
    for channel in channels:
        print(channel)

    print("""Settings:
Alarm Enabled: {}
Alarm Interval: {:.2f}s
""".format(alarm.enabled, alarm.interval))

    main_options = [
        {
            "title": "Alarm Interval",
            "prop": "interval",
            "inc": 1,
            "min": 1,
            "max": 60,
            "format": lambda value: f"{value:02.0f}sec",
            "object": alarm,
            "help": "Time between alarm beeps.",
        },
        {
            "title": "Alarm Enable",
            "prop": "enabled",
            "mode": "bool",
            "format": lambda value: "Yes" if value else "No",
            "object": alarm,
            "help": "Enable the piezo alarm beep.",
        },
    ]

    viewcontroller = ViewController([
        (
            MainView(image, channels=channels, alarm=alarm),
            SettingsView(image, options=main_options),
        ),
        (
            DetailView(image, channel=channels[0]),
            ChannelEditView(image, channel=channels[0]),
        ),
        (
            DetailView(image, channel=channels[1]),
            ChannelEditView(image, channel=channels[1]),
        ),
        (
            DetailView(image, channel=channels[2]),
            ChannelEditView(image, channel=channels[2]),
        ),
    ])

    while True:
        for channel in channels:
            config.set_channel(channel.channel, channel)
            channel.update()
            if channel.alarm:
                alarm.trigger()

        alarm.update(light.get_lux() < 4.0)

        viewcontroller.update()
        viewcontroller.render()
        display.display(image.convert("RGB"))

        config.set_general({
            "alarm_enable": alarm.enabled,
            "alarm_interval": alarm.interval,
        })

        config.save()

        time.sleep(1.0 / FPS)
Ejemplo n.º 2
0
 def __init__(self, **kwargs):
     import ltr559
     super().__init__(**kwargs)
     self.ltr = ltr559.LTR559()
Ejemplo n.º 3
0
def main():
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('grow.ui')

    logger.info(
        f'{time.strftime("%Y-%m-%d %H:%M:%S")} Set up the ST7735 SPI Display')
    display = ST7735.ST7735(port=0,
                            cs=1,
                            dc=9,
                            backlight=12,
                            rotation=270,
                            spi_speed_hz=80000000)
    display.begin()

    logger.info(f'{time.strftime("%Y-%m-%d %H:%M:%S")} Set up light sensor')
    light = ltr559.LTR559()

    logger.info(
        f'{time.strftime("%Y-%m-%d %H:%M:%S")} Set up our canvas and prepare for drawing'
    )
    image = Image.new("RGBA", (DISPLAY_WIDTH, DISPLAY_HEIGHT),
                      color=(255, 255, 255))

    logger.info(f'{time.strftime("%Y-%m-%d %H:%M:%S")} Loading config')
    config = Config()
    config_file = sys.argv[1]
    config.load(config_file)

    channels = []
    for i in range(1, 4):
        channels.append(
            Channel.channel_from_config(i, config.get_channel(i), logger))

    alarm = Alarm(image)
    alarm.update_from_yml(config.get_general())

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    def handle_button(pin):
        index = BUTTONS.index(pin)
        label = LABELS[index]

        if label == "A":  # Select View
            view_controller.button_a()

        if label == "B":  # Sleep Alarm
            if not view_controller.button_b():
                if view_controller.home:
                    if alarm.sleeping():
                        alarm.cancel_sleep()
                    else:
                        alarm.sleep()

        if label == "X":
            view_controller.button_x()

        if label == "Y":
            view_controller.button_y()

    for pin in BUTTONS:
        GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=200)

    main_options = [
        {
            "title": "Alarm Interval",
            "prop": "interval",
            "inc": 1,
            "min": 1,
            "max": 60,
            "format": lambda value: f"{value:02.0f}sec",
            "object": alarm,
            "help": "Time between alarm beeps.",
        },
        {
            "title": "Alarm Enable",
            "prop": "enabled",
            "mode": "bool",
            "format": lambda value: "Yes" if value else "No",
            "object": alarm,
            "help": "Enable the piezo alarm beep.",
        },
        {
            "title": "Watering Delay",
            "prop": "watering_delay",
            "inc": 1,
            "min": 10,
            "max": 1200,
            "mode": "int",
            "format": lambda value: f"{value:0.0f}sec",
            "help": "Delay between waterings"
        },
    ]

    view_controller = ViewController([
        (MainView(image, channels=channels,
                  alarm=alarm), SettingsView(image, options=main_options)),
        (DetailView(image, channel=channels[0]),
         ChannelEditView(image, channel=channels[0])),
        (DetailView(image, channel=channels[1]),
         ChannelEditView(image, channel=channels[1])),
        (DetailView(image, channel=channels[2]),
         ChannelEditView(image, channel=channels[2])),
    ])

    while True:
        for channel in channels:
            channel.update_from_yml(config.get_channel(channel.channel))
            if channel.alarm:
                alarm.trigger()

        alarm.update(light.get_lux() < 4.0)

        view_controller.update()
        view_controller.render()
        display.display(image.convert("RGB"))

        config.set_general({
            "alarm_enable": alarm.enabled,
            "alarm_interval": alarm.interval
        })
        config.save(config_file)

        time.sleep(1.0 / FPS)