Solved::Teensy 3.0 serial communication

Anyone have experience connecting openFrameworks to a Teensy 3 via serial? Having trouble sending data to one using ofSerial.

Best,
Jason

EDIT – Here’s what I did to resolve:
Everything’s working now and I built out a class… made for the Teensy 3.0 when it’s flashed with the OctoWS2811 lib.

openFrameworks class/example here:
https://github.com/jasonrwalters/ofxTeensyOcto

Info on OctoWS2811 library:
http://www.pjrc.com/teensy/td-libs-OctoWS2811.html

Are you on OSX? This might be relevant to you: http://forum.pjrc.com/threads/24099-Teensy3-Mac-Serial?highlight=teensy3.0+arduino

I need to get my Teensy 3.0 setup working again on my new computer, trying to do that now so I can be more helpful.

Yes on OSX. Thank you, will review and let you know if that helps.

Actually, I’m on 10.7 and with OF 0.8, Teensy 3.0 and Arduino 1.05 it all seems to work fine. Serial.available() reports available way longer than it should (i.e. that buffer isn’t being cleared) but this works fine:

  
  
void setup()  
{  
  pinMode(2, OUTPUT);  
  Serial.begin(57600);  
}  
  
void loop()  
{  
  char bb[3];  
  if(Serial.readBytes(bb, 2) > 0)  
  {  
    digitalWrite(2, HIGH);  
    delay(200);  
    digitalWrite(2, LOW);  
    Serial.print("ok");  
  }  
}  
  
  

on the OF side:

  
  
void testApp::update()  
{  
    if( serial.available() > 2 )  
    {  
        unsigned char *c = new unsigned char[3];  
        serial.readBytes(c, 3);  
        cout << *c << endl;  
    }  
}  
  
void testApp::keyPressed  (int key){  
      
    unsigned char bb[] = { 'o', 'k', '\255' };  
    serial.writeBytes(&bb[0], 3);  
    serial.drain();  
	  
}  
  

It looks like it’s working. Going to do more tests this morning and will report back. Thank you!

For reference, I’m trying to port the processing sketch here:
http://www.pjrc.com/teensy/td-libs-OctoWS2811.html

Ah, that seems substantially more complex than “send three bytes” :slight_smile:

I don’t have any LED strips laying around so I’m not sure how much more help I can be but I’d be happy to try.

haha yeah… maybe i’m just doing something wrong programmically. here’s my source:

  
  
  
#include "testApp.h"  
  
ofSerial * ledSerial = new ofSerial[maxPorts];  
ofRectangle * ledArea = new ofRectangle[maxPorts];  
bool * ledLayout = new bool[maxPorts];  
ofImage * ledImage = new ofImage[maxPorts];  
  
//--------------------------------------------------------------  
void testApp::setup(){  
          
    numPorts = 0;  
    errorCount = 0;  
    framerate = 0.0f;  
      
    ledSerial[numPorts].listDevices();  
    vector <ofSerialDeviceInfo> deviceList = ledSerial[numPorts].getDeviceList();  
    setupSerial();  
}  
  
//--------------------------------------------------------------  
void testApp::setupSerial(){  
      
    if (numPorts >= maxPorts) {  
        cout << "too many serial ports, please increase maxPorts" << endl;  
        errorCount++;  
        return;  
    }  
    else {  
        int baud = 115200;  
        ledSerial[numPorts].setup(0, baud);  
        ledSerial[numPorts].writeByte('?');  
    }  
      
    ledImage[numPorts].allocate(60, 16, OF_IMAGE_COLOR);  
    ledArea[numPorts].set(0, 0, 100, 100);  
    ledLayout[numPorts] = 0 == 0;  
    numPorts++;  
}  
  
// translate the 24 bit color from RGB to the actual  
// order used by the LED wiring.  GRB is the most common.  
int colorWiring(int c) {  
    //return c;  
    return ((c & 0xFF0000) >> 8) | ((c & 0x00FF00) << 8) | (c & 0x0000FF); // GRB - most common wiring  
}  
  
// image2data converts an image to OctoWS2811's raw data format.  
// The number of vertical pixels in the image must be a multiple  
// of 8.  The data array must be the proper size for the image.  
void testApp::image2data(ofImage image, unsigned char * data, bool layout) {  
    int offset = 3;  
    int x, y, xbegin, xend, xinc, mask;  
    int linesPerPin = image.getHeight() / 8;  
    int * pixel = new int[8];  
      
    for (y = 0; y < linesPerPin; y++) {  
        if ((y & 1) == (layout ? 0 : 1)) {  
            // even numbered rows are left to right  
            xbegin = 0;  
            xend = image.getWidth();  
            xinc = 1;  
        } else {  
            // odd numbered rows are right to left  
            xbegin = image.getWidth() - 1;  
            xend = -1;  
            xinc = -1;  
        }  
          
        unsigned char * pixels = image.getPixels();  
        for (x = xbegin; x != xend; x += xinc) {  
            for (int i=0; i < 8; i++) {  
                // fetch 8 pixels from the image, 1 for each pin  
                pixel[i] = pixels[x + (y + linesPerPin * i) * (int)image.getWidth()];  
                pixel[i] = colorWiring(pixel[i]);  
            }  
            // convert 8 pixels to 24 bytes  
            for (mask = 0x800000; mask != 0; mask >>= 1) {  
                 char b = 0;  
                for (int i=0; i < 8; i++) {  
                    if ((pixel[i] & mask) != 0) b |= (1 << i);  
                }  
                data[offset++] = b;  
            }  
        }  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
      
    vid.update();  
          
    framerate = 30.0f;  
              
        for (int i=0; i < numPorts; i++) {  
  
            // pixel to image...  
            for (int y = 0; y < ledImage[i].getHeight(); y++) {  
                for (int x = 0; x < ledImage[i].getWidth(); x++) {  
                    int loc = x + y * ledImage[i].getWidth();  
                      
                    ofColor col;  
                    ledImage[i].setColor(x, y, col.red);  
                    ledImage[i].update();  
                }  
            }  
              
            // convert the LED image to raw data  
            unsigned char * ledData = new unsigned char[((int)ledImage[i].getWidth() * (int)ledImage[i].getHeight() * 3) + 3];  
            image2data(ledImage[i], ledData, ledLayout[i]);  
            if (i == 0) {  
                ledData[0] = '*';  // first Teensy is the frame sync master  
                int usec = (int)((1000000.0 / framerate) * 0.75);  
                ledData[1] = (unsigned char)(usec);   // request the frame sync pulse  
                ledData[2] = (unsigned char)(usec >> 8); // at 75% of the frame time  
            } else {  
                ledData[0] = '%';  // others sync to the master board  
                ledData[1] = 0;  
                ledData[2] = 0;  
            }  
              
            // send the raw data to the LEDs  :-)  
            ledSerial[i].writeBytes(ledData, 3);  
            ledSerial[i].drain();  
        }  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
      
    // then try to show what was most recently sent to the LEDs  
    // by displaying all the images for each port.  
    for (int i=0; i < numPorts; i++) {  
        ledImage[i].draw(60, 16, 210, 10);  
    }  
      
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){  
      
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::gotMessage(ofMessage msg){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::dragEvent(ofDragInfo dragInfo){  
	  
}  
  

Ugh, totally misread your code, just woke up here. Sorry :confused: Will edit this back to something helpful in a moment :slight_smile:

I’m going to stick with my original comment actually,

  
ledSerial[i].writeBytes(ledData, 3);   

is “data *” and “length of data array that pointer points to”. Looks like you’re sending a lot more than 3 bytes, no? Because

  
unsigned char * ledData = new unsigned char[((int)ledImage[i].getWidth() * (int)ledImage[i].getHeight() * 3) + 3];  

looks like you’re trying to send the whole image.

maybe this will be helpful… the original processing sketch that works:

  
  
import processing.video.*;  
import processing.serial.*;  
import java.awt.Rectangle;  
  
Movie myMovie;  
  
int numPorts=0;  // the number of serial ports in use  
int maxPorts=4; // maximum number of serial ports  
  
Serial[] ledSerial = new Serial[maxPorts];     // each port's actual Serial port  
Rectangle[] ledArea = new Rectangle[maxPorts]; // the area of the movie each port gets, in % (0-100)  
boolean[] ledLayout = new boolean[maxPorts];   // layout of rows, true = even is left->right  
PImage[] ledImage = new PImage[maxPorts];      // image sent to each port  
int errorCount=0;  
float framerate=0;  
  
void setup() {  
    
  String[] list = Serial.list();  
  delay(20);  
  println("Serial Ports List:");  
  println(list);  
  serialConfigure("/dev/tty.usbmodem14761");  // change these to your port names  
  if (errorCount > 0) exit();  
    
  size(1024, 800);  
    
  myMovie = new Movie(this, "red_blue_strips.mov");  
  myMovie.loop();  
    
}  
  
// movieEvent runs for each new frame of movie data  
void movieEvent(Movie m) {  
  // read the movie's next frame  
  m.read();  
    
  framerate = 30.0; // TODO, how to read the frame rate???  
    
  for (int i=0; i < numPorts; i++) {      
    // copy a portion of the movie's image to the LED image  
    int xoffset = percentage(m.width, ledArea[i].x);  
    int yoffset = percentage(m.height, ledArea[i].y);  
    int xwidth =  percentage(m.width, ledArea[i].width);  
    int yheight = percentage(m.height, ledArea[i].height);  
      
    ledImage[i].copy(m, xoffset, yoffset, xwidth, yheight,0, 0, ledImage[i].width, ledImage[i].height);  
                       
    // convert the LED image to raw data  
    byte[] ledData =  new byte[(ledImage[i].width * ledImage[i].height * 3) + 3];  
    image2data(ledImage[i], ledData, ledLayout[i]);  
    if (i == 0) {  
      ledData[0] = '*';  // first Teensy is the frame sync master  
      int usec = (int)((1000000.0 / framerate) * 0.75);  
      ledData[1] = (byte)(usec);   // request the frame sync pulse  
      ledData[2] = (byte)(usec >> 8); // at 75% of the frame time  
    } else {  
      ledData[0] = '%';  // others sync to the master board  
      ledData[1] = 0;  
      ledData[2] = 0;  
    }  
    // send the raw data to the LEDs  :-)  
    ledSerial[i].write(ledData);  
  }  
}  
  
// image2data converts an image to OctoWS2811's raw data format.  
// The number of vertical pixels in the image must be a multiple  
// of 8.  The data array must be the proper size for the image.  
void image2data(PImage image, byte[] data, boolean layout) {  
  int offset = 3;  
  int x, y, xbegin, xend, xinc, mask;  
  int linesPerPin = image.height / 8;  
  int pixel[] = new int[8];  
    
  for (y = 0; y < linesPerPin; y++) {  
    if ((y & 1) == (layout ? 0 : 1)) {  
      // even numbered rows are left to right  
      xbegin = 0;  
      xend = image.width;  
      xinc = 1;  
    } else {  
      // odd numbered rows are right to left  
      xbegin = image.width - 1;  
      xend = -1;  
      xinc = -1;  
    }  
    for (x = xbegin; x != xend; x += xinc) {  
      for (int i=0; i < 8; i++) {  
        // fetch 8 pixels from the image, 1 for each pin  
        pixel[i] = image.pixels[x + (y + linesPerPin * i) * image.width];  
        pixel[i] = colorWiring(pixel[i]);  
      }  
      // convert 8 pixels to 24 bytes  
      for (mask = 0x800000; mask != 0; mask >>= 1) {  
        byte b = 0;  
        for (int i=0; i < 8; i++) {  
          if ((pixel[i] & mask) != 0) b |= (1 << i);  
        }  
        data[offset++] = b;  
      }  
    }  
  }   
}  
  
// translate the 24 bit color from RGB to the actual  
// order used by the LED wiring.  GRB is the most common.  
int colorWiring(int c) {  
  //return c;  // RGB  
  return ((c & 0xFF0000) >> 8) | ((c & 0x00FF00) << 8) | (c & 0x0000FF); // GRB - most common wiring  
}  
  
// ask a Teensy board for its LED configuration, and set up the info for it.  
void serialConfigure(String portName) {  
  if (numPorts >= maxPorts) {  
    println("too many serial ports, please increase maxPorts");  
    errorCount++;  
    return;  
  }  
  else {  
    ledSerial[numPorts] = new Serial(this, portName);  
    ledSerial[numPorts].write('?');  
  }  
  // only store the info and increase numPorts if Teensy responds properly  
  ledImage[numPorts] = new PImage(60, 16, RGB);  
  ledArea[numPorts] = new Rectangle(0, 0,  
                     100, 100);  
  ledLayout[numPorts] = 0 == 0;  
  numPorts++;  
}  
  
// respond to mouse clicks as pause/play  
boolean isPlaying = true;  
void mousePressed() {  
  if (isPlaying) {  
    myMovie.pause();  
    isPlaying = false;  
  } else {  
    myMovie.play();  
    isPlaying = true;  
  }  
}  
  
// scale a number by a percentage, from 0 to 100  
int percentage(int num, int percent) {  
  double mult = percentageFloat(percent);  
  double output = num * mult;  
  return (int)output;  
}  
  
// scale a number by the inverse of a percentage, from 0 to 100  
int percentageInverse(int num, int percent) {  
  double div = percentageFloat(percent);  
  double output = num / div;  
  return (int)output;  
}  
  
// convert an integer from 0 to 100 to a float percentage  
// from 0.0 to 1.0.  Special cases for 1/3, 1/6, 1/7, etc  
// are handled automatically to fix integer rounding.  
double percentageFloat(int percent) {  
  if (percent == 33) return 1.0 / 3.0;  
  if (percent == 17) return 1.0 / 6.0;  
  if (percent == 14) return 1.0 / 7.0;  
  if (percent == 13) return 1.0 / 8.0;  
  if (percent == 11) return 1.0 / 9.0;  
  if (percent ==  9) return 1.0 / 11.0;  
  if (percent ==  8) return 1.0 / 12.0;  
  return (double)percent / 100.0;  
}  
  
void draw() {  
  // show the original video  
  image(myMovie, 0, 80);  
    
  // then try to show what was most recently sent to the LEDs  
  // by displaying all the images for each port.  
  for (int i=0; i < numPorts; i++) {  
    // compute the intended size of the entire LED array  
    int xsize = percentageInverse(ledImage[i].width, ledArea[i].width);  
    int ysize = percentageInverse(ledImage[i].height, ledArea[i].height);  
    // computer this image's position within it  
    int xloc =  percentage(xsize, ledArea[i].x);  
    int yloc =  percentage(ysize, ledArea[i].y);  
      
    float imgX = 240 - xsize / 2 + xloc;  
    float imgY = 10 + yloc;  
      
    // show  should appear on the LEDs  
    image(ledImage[i], imgX, imgY);  
  }   
}  
  

Ah ha! I’m able to light up some LEDs now per your previous response. :slight_smile:

Will keep you posted. Thanks so much!

Everything’s working now and I built out a class… made for the Teensy 3.0 when it’s flashed with the OctoWS2811 lib.

openFrameworks class/example here:
https://github.com/jasonrwalters/ofxTeensyOcto

Info on OctoWS2811 library:
http://www.pjrc.com/teensy/td-libs-OctoWS2811.html

@Jason Walters , do you notice that the colors are off and the pixels are shifted right for each row you move to with your library? When I run it on an 30x8 LED setup, it seems to be shifted and the colors to be off. Running the debug draw you wrote, when I changed from black -> red, each row changes colors when I shift to them. For example the first row would be red, the next blue, the next green and repeats.

I found out why the pixels were shifting. When I allocated the FBO as follows:

fbo.allocate(stripWidth, stripHeight*stripsPerPort*numPorts, GL_RGB);

it shifted the pixels. So I had to remove GL_RGB:

fbo.allocate(stripWidth, stripHeight*stripsPerPort*numPorts);

And it works fine now.

Hi there, I know this is a pretty old thread. I just would love to have a look into the full source. The repo on github is gone. Is the code here in the forum the final working project?

Don’t worry. I found it. https://github.com/BBDO/ofxTeensyOcto :smile_cat: