Wednesday 26 February 2014

Listening to your Teensy

Now I can write to my Teensy it would be nice if I can listen to what it has to say, possibly more useful.

So we start with the Python this time:
 #!/usr/bin/python2.7  
 from time import sleep  
 import serial  
 from serial.tools import list_ports  
 if __name__ == "__main__":  
   ports = [port[0] for port in list_ports.comports()]  
   print "Using serial port %s" % (ports[-1])  
   serial = serial.Serial(ports[-1],baudrate=9600, timeout=0, writeTimeout=1)  
   serial.flushInput()  
   serial.flushOutput()  
   while True:  
      content = serial.readline() # Read the newest output from the Arduino  
      if content:  
           print content       
      sleep(.1) # Delay for one tenth of a second  

Now when I run this program it will wait for a message and if there is anything write it out.

Now we can add to the sketch:
 #include <Wire.h>   
 #include <LiquidCrystal_I2C.h>  
 // Set the LCD address to 0x27 for a 16 chars and 2 line display  
 LiquidCrystal_I2C lcd(0x27, 16, 2);  
 String oldContent; //Keep this between usb reads();  
 void setup(){  
  pinMode(7, INPUT_PULLUP); //Set pin 7 to INPUT_PULLUP, this uses the inbuilt resistor on the pin  
  lcd.begin();  
  Serial.begin(9600); // USB is always 12 Mbit/sec  
  oldContent=""; //Initialise this variable  
 }  
 void loop(){  
  String content = getString(); //Get the USB content  
  checkButton(); //Check for a result  
  //If there was USB content display it  
  if (content != ""){  
   content.concat(" "); //Add another character  
   oldContent = content;//Save the content  
  }  
  //If there is something to display  
  if (oldContent!=""){  
   displayMessage(oldContent); //show me!  
  }else{  
   //Otherwise check again soon!  
   delay(250);  
  }  
 }  
 String getString(){  
  String content = ""; //Set up return variable  
  char character; //char to read from USB  
  while(Serial.available()) { //While there is more to read, do more  
    character = Serial.read(); //Read the char  
    content.concat(character); //Add it to the return variable  
    delay (10); //make sure we don't act too quickly and assume it's over before it really is  
  }  
  Serial.print(content); //Echo back the content heard on USB  
  return content;   
 }  
 void displayMessage(String message){  
  if (message!=""){ //Make sure there is something to show  
    lcd.home(); //Start at row 1  
    printRows(message,0); //print the message, possibly on 2 rows  
    delay(2500); //Show the first screen for a while, give people a change to read  
    for (int posCounter = 0; posCounter < message.length(); posCounter++){ //For all of the chars  
     checkButton();  
     printRows(message,posCounter); //print the message, possibly on 2 rows  
     delay(600);  
    }  
    delay(600);  
  }   
 }  
 void printRows(String message, int pos){  
    lcd.clear();//Clear the screen again  
    lcd.setCursor(0,0);  
    lcd.print(message.substring(pos,pos+16));  
    if (message.length() >16){  
     lcd.setCursor(0, 1); //Set to the second row  
     lcd.print(message.substring(pos+16,pos+32));  
    }  
 }  
 // a little check for a button  
 void checkButton(){  
   if (digitalRead(7) == LOW ) {  
    Serial.print("button pressed");  
   }   
 }  

There are two new bits here. First we echo back what we heard on USB after it was received. Second we have a couple of checks for a button on pin 7. I turned this into a function so I can add the check in more places as there are quite a few loops that could miss the event.

This function would allow me to break out of my loops if the button was pressed if I returned true instead of just printing.

Not a major development but now with the two python programs I can dynamically update my message and listen to my device for anything happening locally.

Next goal is to have a "service" react to the button and trigger something happen like get a REST service response to display a message on the LCD, baby steps :)

Here's a picture of a cat:

No comments:

Post a Comment