Hi guys,
First, a thanks to Jars for his help so far my program is based off of one he gave me last week.
So I'm trying to control a DMD with a series of CSV's over the serial port.
Syntax: Input,xPos,yPos
Input: Text to be displayed
xPos: Where to display txt on the x-axis
yPos: Where to display txt on the y-axis
Ex: Hello,0,0
The problem is when I send a short Input such as "h,0,0" it displays "h00" on the DMD instead of just the "h". My gut says its something to do with the way strtok splits the string. From what I've read on cplusplus.com, it sounds like it doesn't actually reassign the parts of the string to new variables, it just separates them with tokens. Is that correct?
#include "SPI.h"
#include "DMD.h"
#include "TimerOne.h"
#include "SystemFont5x7.h"
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
/* change these values if you have more than one DMD connected */
DMD dmd(DISPLAYS_ACROSS,DISPLAYS_DOWN);
#define SEPERATING_CHAR 10 // ASCII-10 is '\n' newline charactor and seporates strings
void ScanDMD() {
dmd.scanDisplayBySPI();
}
void setup() {
Timer1.initialize( 5000 );
/*period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.*/
Timer1.attachInterrupt( ScanDMD );
/*attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()*/
dmd.clearScreen( true );
/* true is normal (all pixels off), false is negative (all pixels on) */
dmd.selectFont(SystemFont5x7);
Serial.begin(9600);
}
void loop() {
char *str, *input, *xPos, *yPos;
if (Serial.available()) {
str = receiveCharArray(Serial.read());
if (str != NULL) {
input = strtok(str, ",");
xPos = strtok(NULL, ",");
yPos = strtok(NULL, ",");
Serial.println(input);
Serial.println(xPos);
Serial.println(yPos);
dmd.clearScreen(true);
dmd.drawString(0, 0, input, 5, GRAPHICS_NORMAL);
}
}
}
char* receiveCharArray(char c) {
static char lineBuffer[81];
static byte counter = 0;
if (counter == 0) memset(lineBuffer, 0, sizeof(lineBuffer));
if (c == SEPERATING_CHAR) {
counter = 0;
return lineBuffer;
}
else if (c >= 32 && counter < sizeof(lineBuffer) - 1) {
lineBuffer[counter] = c;
counter++;
}
return NULL;
}
What do you think?