From 3592bd18dd45171387460a8c7ac94508559b9582 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 13 Apr 2013 16:30:44 +0200 Subject: Make display size configurable and properly buffer input Signed-off-by: Florian Pritz --- src/main.cpp | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f246e3a..8c67729 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,8 +5,18 @@ void setup(); void loop(); -#define BUFFER_SIZE 64 +// buffer size must be > LCD_CHARS +#define BUFFER_SIZE 80 +char display_data[BUFFER_SIZE]; char buf[BUFFER_SIZE]; +int buffered = 0; + +#define LCD_ROWS 2 +#define LCD_COLS 16 +#define LCD_CHARS (LCD_COLS * LCD_ROWS) +#if LCD_CHARS > BUFFER_SIZE +#error "BUFFER too small" +#endif // initialize the library with the numbers of the interface pins #ifdef ENERGIA @@ -28,7 +38,7 @@ Bounce bounce_buttons[NUMBUTTONS]; void setup() { byte i; - lcd.begin(16, 2); + lcd.begin(LCD_COLS, LCD_ROWS); // The usb chip hangs if it tries to send data and no-one listens // so we give the client time to start up first @@ -54,7 +64,8 @@ void setup() { } Serial.println("ready"); - lcd.print("Serial ready"); + memset(display_data, ' ', BUFFER_SIZE); + strcpy(display_data, "Waiting for data"); } bool button_just_pressed(int i) { @@ -75,16 +86,28 @@ void loop() { Serial.println("next"); } - // output what we got over serial - if (Serial.available() >= 32) { - lcd.setCursor(0, 0); - Serial.readBytes(buf, 32); - for (int i = 0; i < 32; i++) { - if (i == 16) { - lcd.setCursor(0, 1); - } - lcd.write(buf[i]); + // buffer serial input ourselves + if (Serial.available() >= 1) { + buffered += Serial.readBytes(buf + buffered, LCD_CHARS - buffered); + } + + if (buffered >= LCD_CHARS) { + strcpy(display_data, buf); + buffered = 0; + } + + // update display + lcd.setCursor(0, 0); + int line = -1; + for (int i = 0; i < LCD_CHARS; i++) { + if (display_data[i] == '\0') { + break; + } + if (i % LCD_COLS == 0) { + line++; + lcd.setCursor(0, line); } + lcd.write(display_data[i]); } } -- cgit v1.2.3-24-g4f1b