arduino_hack_day / lightning_talk / lightning.py

#!/usr/bin/env python
# coding: utf-8

import firmata
import glob
import time
import logging

class Sinal(object):
    def __init__(self, port, red_pin, green_pin):
        self.port = port
        self.red_pin = red_pin
        self.green_pin = green_pin
        self.arduino = firmata.Arduino(port)
        self.arduino.pin_mode(red_pin, firmata.OUTPUT)
        self.arduino.pin_mode(green_pin, firmata.OUTPUT)
    
    def turn_on_green(self):
        self.arduino.digital_write(self.green_pin, firmata.HIGH)

    def turn_off_green(self):
        self.arduino.digital_write(self.green_pin, firmata.LOW)

    def turn_on_red(self):
        self.arduino.digital_write(self.red_pin, firmata.HIGH)

    def turn_off_red(self):
        self.arduino.digital_write(self.red_pin, firmata.LOW)

    def turn_off_all(self):
        self.arduino.digital_write(self.red_pin, firmata.LOW)
        self.arduino.digital_write(self.green_pin, firmata.LOW)

    def turn_on_both(self):
        self.arduino.digital_write(self.red_pin, firmata.HIGH)
        self.arduino.digital_write(self.green_pin, firmata.HIGH)


def start_talk():
    handler = logging.Handler()
    def print_log(log_object):
        print time.time(), log_object.msg
    handler.emit = print_log
    log = logging.Logger('sinal')
    log.addHandler(handler)

    sinal = Sinal(port=glob.glob('/dev/ttyUSB*')[0], red_pin=7, green_pin=8)
    sinal.turn_off_all()
    log.debug('starting...')

    try:
        for i in range(4):
            sinal.turn_on_green()
            time.sleep(59)
            sinal.turn_off_green()
            time.sleep(1)
            log.debug('%d minute%c' % (i + 1, 's' if i > 0 else ' '))
        sinal.turn_on_green()
        time.sleep(30)
        log.debug('04:30')

        for i in range(20):
            sinal.turn_on_green()
            time.sleep(0.5)
            sinal.turn_off_green()
            time.sleep(0.5)

        log.debug('04:50')
        for i in range(10):
            sinal.turn_on_red()
            sinal.turn_on_green()
            time.sleep(0.5)
            sinal.turn_off_red()
            sinal.turn_off_green()
            time.sleep(0.5)
        
        sinal.turn_on_red()
        log.debug('05:00!')
    except KeyboardInterrupt:
        sinal.turn_off_all()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.