Source

arduino_hack_day / lightning_talk / lightning.py

Full commit
#!/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)

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)

def start_talk():
    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()