Hi All,
I seem to be receiving huge delays with writing to the serial port in a teensy.
I'm currently running the following code:
This code continually grabs 30 bytes to be read from a device on the other end of the serial port, then draws one column on my matrix, then sends information back to the device on the other end of the serial.
When I don't send serial communication from the teensy to the other device (in this case my PC running pySerial) I get awesome framerates, maybe 60fps. However when I start sending data back, my program slows down to a crawl. See attached video:
http://youtu.be/sMRN28Zhrjw
Any suggestions?
The functions referenced in the above code are as follows:
I seem to be receiving huge delays with writing to the serial port in a teensy.
I'm currently running the following code:
Code:
void loop() {
// put your main code here, to run repeatedly:
for(int i = 0; i < 30; i++) {
bool sysReady = initiate();
if(sysReady){
readInput(); //read input from Pi
}
}
draw(); //send entire array from Pi to LED's
if(receivedData && shiftRegCount == 0) {
readSensors(); //Send entire array to Pi
}
}
This code continually grabs 30 bytes to be read from a device on the other end of the serial port, then draws one column on my matrix, then sends information back to the device on the other end of the serial.
When I don't send serial communication from the teensy to the other device (in this case my PC running pySerial) I get awesome framerates, maybe 60fps. However when I start sending data back, my program slows down to a crawl. See attached video:
http://youtu.be/sMRN28Zhrjw
Any suggestions?
The functions referenced in the above code are as follows:
Code:
//========================READ SENSORS==========================
void readSensors(){
for(int i=0; i<yMax; i++){
//
//digitalWrite(S0,bitRead(i,S0));
//digitalWrite(S1,bitRead(i,S1));
//digitalWrite(S2,bitRead(i,S2));
//digitalWrite(S3,bitRead(i,S3));
//digitalWrite(4,0); we can just ground this
for (int j=0; j<xMax; j++){
//Serial.write(char(255));
//Serial.write(char((i)*10 + (j) + 1));
//Serial.write(char(i+j+1));
//Serial.write(char(0));
}
}
Serial.println("This is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, this is a test, ");
}
void draw(){
for(int j = 0; j<48; j++) {
setGSData(j,ledArray[shiftRegCount][j%16][j/16]);
}
//Serial.println(j);
//delay(50);
//Send out values and shift the shift register
sendGSData(shiftSelect[shiftRegCount]);
//Increment count
++shiftRegCount;
if(shiftRegCount > 7) {
shiftRegCount = 0;
}
}
//=========================READ INPUT FROM PI==============================
void readInput() {
inByte = Serial.read(); //reads first character in buffer from incoming serial data
payloadByte = int(inByte); //typecasts inByte from a char to an int
if (inPacket) { //If we're in a packet (have read start bit), then continue
if(payloadByte == 0) { //Checks to see if we're reading the stop bit
inPacket = false; //Sets inPacket to false, meaning we're not in a packet and will look for another start bit
//Once we hit the stop bit, package has been read and we update the mask
//ledArray[currentValue[0]][currentValue[1]][6]=255;
if(++updateCount > 159) {
updateCount = 0;
if(writeToLeftArray == 0) {
writeToLeftArray = 1;
shifter = 3;
}
else {
writeToLeftArray = 0;
shifter = 0;
}
}
}
else {
if(payloadByte == 255) {
inPacket = true;
packetPlacement = 0;
}
if(packetPlacement == 0) { //First piece of data in packet denote position on table
int x = 0;
int y = 0;
if(payloadByte > 160) {
payloadByte = 160;
}
xyconvert(payloadByte-1, &x, &y); //Converts position to x-y coordinates
currentValue[0] = x;
currentValue[1] = y;
} //Next three bytes will denotes r,g,b brightness,
else if(payloadByte == 1) { //
payloadByte = 0;
}
else if(payloadByte == 254) {
payloadByte = 255;
}
if(packetPlacement > 0 && packetPlacement < 4) {
if(writeToLeftArray == 1){ //Determines which array to write to
ledArray[currentValue[0]][currentValue[1]][packetPlacement-1] = map(payloadByte,0,255,0,4095); //Writing to first array
}else{
ledArray[currentValue[0]][currentValue[1]][packetPlacement+2] = map(payloadByte,0,255,0,4095); //Writing to second array
}
}
if(packetPlacement < 0 || packetPlacement > 3) {
Serial.println("Error: Incorrect data format");
}
if(++packetPlacement > 6) {
inPacket = false;
}
}
}
else {
if(payloadByte == 255) { //ASCII start bit
inPacket = true;
receivedData = true;
packetPlacement = 0;
}
}
//Logic to flip from reading to left to reading from right and vice versa for data input from serial
}