Anonymous avatar Anonymous committed c7c8649

Adicionado código do sinal para lightning talks! #PythonBrasil6; senha -> senha/senha.pde

Comments (0)

Files changed (4)

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()

lightning_talk/old_firmata/old_firmata.pde

+/*
+  Copyright (C) 2006-2008 Hans-Christoph Steiner.  All rights reserved.
+ 
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+ 
+  See file LICENSE.txt for further informations on licensing terms.
+ */
+
+/* 
+ * This is an old version of StandardFirmata (v2.0).  It is kept here because
+ * its the last version that works on an ATMEGA8 chip.  Also, it can be used
+ * for host software that has not been updated to a newer version of the
+ * protocol.  It also uses the old baud rate of 115200 rather than 57600.
+ */
+
+#include <EEPROM.h>
+#include <Firmata.h>
+
+/*==============================================================================
+ * GLOBAL VARIABLES
+ *============================================================================*/
+
+/* analog inputs */
+int analogInputsToReport = 0; // bitwise array to store pin reporting
+int analogPin = 0; // counter for reading analog pins
+
+/* digital pins */
+byte reportPINs[TOTAL_PORTS];   // PIN == input port
+byte previousPINs[TOTAL_PORTS]; // PIN == input port
+byte pinStatus[TOTAL_DIGITAL_PINS]; // store pin status, default OUTPUT
+byte portStatus[TOTAL_PORTS];
+
+/* timer variables */
+unsigned long currentMillis;     // store the current value from millis()
+unsigned long nextExecuteMillis; // for comparison with currentMillis
+
+
+/*==============================================================================
+ * FUNCTIONS                                                                
+ *============================================================================*/
+
+void outputPort(byte portNumber, byte portValue)
+{
+  portValue = portValue &~ portStatus[portNumber];
+  if(previousPINs[portNumber] != portValue) {
+        Firmata.sendDigitalPort(portNumber, portValue); 
+        previousPINs[portNumber] = portValue;
+        Firmata.sendDigitalPort(portNumber, portValue); 
+    }
+}
+
+/* -----------------------------------------------------------------------------
+ * check all the active digital inputs for change of state, then add any events
+ * to the Serial output queue using Serial.print() */
+void checkDigitalInputs(void) 
+{
+    byte i, tmp;
+    for(i=0; i < TOTAL_PORTS; i++) {
+        if(reportPINs[i]) {
+            switch(i) {
+            case 0: outputPort(0, PIND &~ B00000011); break; // ignore Rx/Tx 0/1
+            case 1: outputPort(1, PINB); break;
+            case ANALOG_PORT: outputPort(ANALOG_PORT, PINC); break;
+            }
+        }
+    }
+}
+
+// -----------------------------------------------------------------------------
+/* sets the pin mode to the correct state and sets the relevant bits in the
+ * two bit-arrays that track Digital I/O and PWM status
+ */
+void setPinModeCallback(byte pin, int mode) {
+    byte port = 0;
+    byte offset = 0;
+
+    if (pin < 8) {
+      port = 0;
+      offset = 0;
+    } else if (pin < 14) {
+      port = 1;
+      offset = 8;     
+    } else if (pin < 22) {
+      port = 2;
+      offset = 14;
+    }
+
+    if(pin > 1) { // ignore RxTx (pins 0 and 1)
+        pinStatus[pin] = mode;
+        switch(mode) {
+        case INPUT:
+            pinMode(pin, INPUT);
+            portStatus[port] = portStatus[port] &~ (1 << (pin - offset));
+            break;
+        case OUTPUT:
+            digitalWrite(pin, LOW); // disable PWM
+        case PWM:
+            pinMode(pin, OUTPUT);
+            portStatus[port] = portStatus[port] | (1 << (pin - offset));
+            break;
+        //case ANALOG: // TODO figure this out
+        default:
+            Firmata.sendString("");
+        }
+        // TODO: save status to EEPROM here, if changed
+    }
+}
+
+void analogWriteCallback(byte pin, int value)
+{
+    setPinModeCallback(pin,PWM);
+    analogWrite(pin, value);
+}
+
+void digitalWriteCallback(byte port, int value)
+{
+    switch(port) {
+    case 0: // pins 2-7 (don't change Rx/Tx, pins 0 and 1)
+        // 0xFF03 == B1111111100000011    0x03 == B00000011
+        PORTD = (value &~ 0xFF03) | (PORTD & 0x03);
+        break;
+    case 1: // pins 8-13 (14,15 are disabled for the crystal) 
+        PORTB = (byte)value;
+        break;
+    case 2: // analog pins used as digital
+        PORTC = (byte)value;
+        break;
+    }
+}
+
+// -----------------------------------------------------------------------------
+/* sets bits in a bit array (int) to toggle the reporting of the analogIns
+ */
+//void FirmataClass::setAnalogPinReporting(byte pin, byte state) {
+//}
+void reportAnalogCallback(byte pin, int value)
+{
+    if(value == 0) {
+        analogInputsToReport = analogInputsToReport &~ (1 << pin);
+    }
+    else { // everything but 0 enables reporting of that pin
+        analogInputsToReport = analogInputsToReport | (1 << pin);
+    }
+    // TODO: save status to EEPROM here, if changed
+}
+
+void reportDigitalCallback(byte port, int value)
+{
+    reportPINs[port] = (byte)value;
+    if(port == ANALOG_PORT) // turn off analog reporting when used as digital
+        analogInputsToReport = 0;
+}
+
+/*==============================================================================
+ * SETUP()
+ *============================================================================*/
+void setup() 
+{
+    byte i;
+
+    Firmata.setFirmwareVersion(2, 0);
+
+    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
+    Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
+    Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
+    Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
+    Firmata.attach(SET_PIN_MODE, setPinModeCallback);
+
+    portStatus[0] = B00000011;  // ignore Tx/RX pins
+    portStatus[1] = B11000000;  // ignore 14/15 pins 
+    portStatus[2] = B00000000;
+
+//    for(i=0; i<TOTAL_DIGITAL_PINS; ++i) { // TODO make this work with analogs
+    for(i=0; i<14; ++i) {
+        setPinModeCallback(i,OUTPUT);
+    }
+    // set all outputs to 0 to make sure internal pull-up resistors are off
+    PORTB = 0; // pins 8-15
+    PORTC = 0; // analog port
+    PORTD = 0; // pins 0-7
+
+    // TODO rethink the init, perhaps it should report analog on default
+    for(i=0; i<TOTAL_PORTS; ++i) {
+        reportPINs[i] = false;
+    }
+    // TODO: load state from EEPROM here
+
+    /* send digital inputs here, if enabled, to set the initial state on the
+     * host computer, since once in the loop(), this firmware will only send
+     * digital data on change. */
+    if(reportPINs[0]) outputPort(0, PIND &~ B00000011); // ignore Rx/Tx 0/1
+    if(reportPINs[1]) outputPort(1, PINB);
+    if(reportPINs[ANALOG_PORT]) outputPort(ANALOG_PORT, PINC);
+
+    Firmata.begin(115200);
+}
+
+/*==============================================================================
+ * LOOP()
+ *============================================================================*/
+void loop() 
+{
+/* DIGITALREAD - as fast as possible, check for changes and output them to the
+ * FTDI buffer using Serial.print()  */
+    checkDigitalInputs();  
+    currentMillis = millis();
+    if(currentMillis > nextExecuteMillis) {  
+        nextExecuteMillis = currentMillis + 19; // run this every 20ms
+        /* SERIALREAD - Serial.read() uses a 128 byte circular buffer, so handle
+         * all serialReads at once, i.e. empty the buffer */
+        while(Firmata.available())
+            Firmata.processInput();
+        /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
+         * 60 bytes. use a timer to sending an event character every 4 ms to
+         * trigger the buffer to dump. */
+	
+        /* ANALOGREAD - right after the event character, do all of the
+         * analogReads().  These only need to be done every 4ms. */
+        for(analogPin=0;analogPin<TOTAL_ANALOG_PINS;analogPin++) {
+            if( analogInputsToReport & (1 << analogPin) ) {
+                Firmata.sendAnalog(analogPin, analogRead(analogPin));
+            }
+        }
+    }
+}

senha.pde

-/*
- * Copyright (C) 2010 Álvaro Justen <alvaro@justen.eng.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 dated June, 1991.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Library General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
- * 
- * If you find any bugs or have any suggestions, please email me.
- *
- * This software was created on Arduino Hack Days (on 2010-03-28 and 2010-04-01)
- * See more details and authors at: 
- * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday.html
- * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday-foi.html
- * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-day-reloaded.html
- * 
- */
-
-
-//1 -> VSS
-//2 -> VCC
-const int RS = 3;
-const int RW = 4;
-const int E = 5;
-const int D[] = { 6, 7, 8, 9, 10, 11, 12, 13 };
-const int LED = 13;
-//15 - > LED+
-//16 - > LED-
-
-int fila = 0;
-int pinInputIncrement = 1;
-int pinInputRepeat = 2;
-int pinSound = 0;
-
-int delay_enable = 5;
-
-void enable() {
-  delay(delay_enable);
-  digitalWrite(E, 0);
-  delay(delay_enable);
-  digitalWrite(E, 1);
-  delay(delay_enable);
-}
-
-void blink_LED(int t) {
-  digitalWrite(LED, 1);
-  delay(t);
-  digitalWrite(LED, 0);
-}
-
-void print_pins(char c) {
-  digitalWrite(D[7], ((int)c & 0x128) >> 7);
-  digitalWrite(D[6], ((int)c & 0x64) >> 6);
-  digitalWrite(D[5], ((int)c & 0x32) >> 5);
-  digitalWrite(D[4], ((int)c & 0x16) >> 4);
-  digitalWrite(D[3], ((int)c & 0x8) >> 3);
-  digitalWrite(D[2], ((int)c & 0x4) >> 2);
-  digitalWrite(D[1], ((int)c & 0x2) >> 1);
-  digitalWrite(D[0], (int)c & 0x1);
-}
-
-void setup_lcd() {
-  digitalWrite(RW, 0);
-  digitalWrite(RS, 0);
-  digitalWrite(E, 1);
-  
-  /* clean up the display */
-  print_pins(0x0C); /* 0x0F = 15 = 00001111 */
-  enable();
-  
-  /* enable two lines */
-  print_pins(0x3F); /* 0x3F = 63 = 00111111 */
-  enable();
-
-  /* enable two lines */
-  print_pins(128); /* move cursor to the 1st line */
-  enable();
-}
-
-void lcd_print(char *text) {
-  digitalWrite(RS, 1);
-  int size = 0, i;
-  while (text[size] != '\0') {
-    size++;
-  }
-  for (i = 0; i < size; i++) {
-    if (text[i] == '\n') {
-      digitalWrite(RS, 0);
-      print_pins(128 + 49 + 7); /* move cursor to the 2nd line */
-      enable();
-      digitalWrite(RS, 1);
-      delay(delay_enable);
-    }
-    else {
-      print_pins(text[i]);
-      enable();
-    }
-  }
-}
-
-
-void piscar_lcd(char *text){
-  setup_lcd();
-  lcd_print(text);
-  delay(100);
-  setup_lcd();
-  lcd_print("   ");
-  delay(50);
-  setup_lcd();
-  lcd_print(text);
-}
-
-
-void toca_nota(int freq, float time){
-  float delay_time = 1000000.0 / (freq);
-  float delay_time1 = 0.9 * delay_time;
-  float delay_time2 = 0.1 * delay_time;
-  for (int i = 0; i < freq * time; i++){
-      digitalWrite(pinSound, HIGH);
-      delayMicroseconds(delay_time2);
-      digitalWrite(pinSound, LOW);
-      delayMicroseconds(delay_time1);
-  }
-}
-
-
-void vem_ca_filho_da_puta(int senha){
-    char senha_str[10];
-    sprintf(senha_str, "%d", senha);
-    piscar_lcd(senha_str);
-    toca_nota(1720, 1);
-    toca_nota(880, 1);
-}
-
-void setup() {
-  // Define o botão de input para o próximo número 
-  // como input e coloca 5v nele (por causa do pullup
-  // resistor
-  pinMode(pinInputIncrement, INPUT);
-  digitalWrite(pinInputIncrement, HIGH);
-  
-  // Faz o mesmo para o pino de repetição do número
-  pinMode(pinInputRepeat, INPUT);
-  digitalWrite(pinInputRepeat, HIGH);
-  
-  pinMode(pinSound, OUTPUT);
-
-  // Prepara o LCD
-  int i;
-  blink_LED(100);
-  pinMode(RS, OUTPUT);
-  pinMode(RW, OUTPUT);
-  pinMode(E, OUTPUT);
-  for (i = 0; i < 8; i++) {
-    pinMode(D[i], OUTPUT);
-  }
-  pinMode(LED, OUTPUT);
-  
-  // Faz uma pequena apresentação
-  delay(1000);
-  setup_lcd();
-  lcd_print("Arduino Hackday");
-  delay(1000);
-  setup_lcd();
-  lcd_print("               ");
-}
-
-
-
-void loop() {
-  if (digitalRead(pinInputIncrement) == LOW){
-    fila++;
-    vem_ca_filho_da_puta(fila);
-  } else if (digitalRead(pinInputRepeat) == LOW) {
-    vem_ca_filho_da_puta(fila);
-  }
-}
-
+/*
+ * Copyright (C) 2010 Álvaro Justen <alvaro@justen.eng.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 dated June, 1991.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * 
+ * If you find any bugs or have any suggestions, please email me.
+ *
+ * This software was created on Arduino Hack Days (on 2010-03-28 and 2010-04-01)
+ * See more details and authors at: 
+ * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday.html
+ * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday-foi.html
+ * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-day-reloaded.html
+ * 
+ */
+
+
+//1 -> VSS
+//2 -> VCC
+const int RS = 2;
+const int RW = 3;
+const int E = 4;
+const int D[] = { 5, 6, 7, 8, 9, 10, 11, 12 };
+const int LED = 13;
+//15 - > LED+
+//16 - > LED-
+
+int fila = 0;
+int pinInputIncrement = 1;
+int pinInputRepeat = 2;
+int pinSound = 0;
+
+int delay_enable = 5;
+
+void enable() {
+  delay(delay_enable);
+  digitalWrite(E, 0);
+  delay(delay_enable);
+  digitalWrite(E, 1);
+  delay(delay_enable);
+}
+
+void blink_LED(int t) {
+  digitalWrite(LED, 1);
+  delay(t);
+  digitalWrite(LED, 0);
+}
+
+void print_pins(char c) {
+  digitalWrite(D[7], ((int)c & 0x128) >> 7);
+  digitalWrite(D[6], ((int)c & 0x64) >> 6);
+  digitalWrite(D[5], ((int)c & 0x32) >> 5);
+  digitalWrite(D[4], ((int)c & 0x16) >> 4);
+  digitalWrite(D[3], ((int)c & 0x8) >> 3);
+  digitalWrite(D[2], ((int)c & 0x4) >> 2);
+  digitalWrite(D[1], ((int)c & 0x2) >> 1);
+  digitalWrite(D[0], (int)c & 0x1);
+}
+
+void setup_lcd() {
+  digitalWrite(RW, 0);
+  digitalWrite(RS, 0);
+  digitalWrite(E, 1);
+  
+  /* clean up the display */
+  print_pins(0x0C); /* 0x0F = 15 = 00001111 */
+  enable();
+  
+  /* enable two lines */
+  print_pins(0x3F); /* 0x3F = 63 = 00111111 */
+  enable();
+
+  /* enable two lines */
+  print_pins(128); /* move cursor to the 1st line */
+  enable();
+}
+
+void lcd_print(char *text) {
+  digitalWrite(RS, 1);
+  int size = 0, i;
+  while (text[size] != '\0') {
+    size++;
+  }
+  for (i = 0; i < size; i++) {
+    if (text[i] == '\n') {
+      digitalWrite(RS, 0);
+      print_pins(128 + 49 + 7); /* move cursor to the 2nd line */
+      enable();
+      digitalWrite(RS, 1);
+      delay(delay_enable);
+    }
+    else {
+      print_pins(text[i]);
+      enable();
+    }
+  }
+}
+
+
+void piscar_lcd(char *text){
+  setup_lcd();
+  lcd_print(text);
+  delay(100);
+  setup_lcd();
+  lcd_print("   ");
+  delay(50);
+  setup_lcd();
+  lcd_print(text);
+}
+
+
+void toca_nota(int freq, float time){
+  float delay_time = 1000000.0 / (freq);
+  float delay_time1 = 0.9 * delay_time;
+  float delay_time2 = 0.1 * delay_time;
+  for (int i = 0; i < freq * time; i++){
+      digitalWrite(pinSound, HIGH);
+      delayMicroseconds(delay_time2);
+      digitalWrite(pinSound, LOW);
+      delayMicroseconds(delay_time1);
+  }
+}
+
+
+void vem_ca_filho_da_puta(int senha){
+    char senha_str[10];
+    sprintf(senha_str, "%d", senha);
+    piscar_lcd(senha_str);
+    toca_nota(1720, 1);
+    toca_nota(880, 1);
+}
+
+void setup() {
+  // Define o botão de input para o próximo número 
+  // como input e coloca 5v nele (por causa do pullup
+  // resistor
+  pinMode(pinInputIncrement, INPUT);
+  digitalWrite(pinInputIncrement, HIGH);
+  
+  // Faz o mesmo para o pino de repetição do número
+  pinMode(pinInputRepeat, INPUT);
+  digitalWrite(pinInputRepeat, HIGH);
+  
+  pinMode(pinSound, OUTPUT);
+
+  // Prepara o LCD
+  int i;
+  blink_LED(100);
+  pinMode(RS, OUTPUT);
+  pinMode(RW, OUTPUT);
+  pinMode(E, OUTPUT);
+  for (i = 0; i < 8; i++) {
+    pinMode(D[i], OUTPUT);
+  }
+  pinMode(LED, OUTPUT);
+  
+  // Faz uma pequena apresentação
+  delay(1000);
+  setup_lcd();
+  lcd_print("Arduino Hackday");
+  delay(1000);
+  setup_lcd();
+  lcd_print("               ");
+}
+
+
+
+void loop() {
+  if (digitalRead(pinInputIncrement) == LOW){
+    fila++;
+    vem_ca_filho_da_puta(fila);
+  } else if (digitalRead(pinInputRepeat) == LOW) {
+    vem_ca_filho_da_puta(fila);
+  }
+}
+
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.