From 1fd98882013edcc615616d376ef7834904dcfc90 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Tue, 9 Jul 2013 03:34:25 +0200 Subject: [PATCH] Code cleanup and increased test display update speed to get character drops to test with --- .../tinyLCD_I2C_Test/tinyLCD_I2C_Test.ino | 2 +- firmware/tinyLCD_I2C/tinyLCD_I2C.ino | 68 ++++++------------- src/tinyLCD_I2C/tinyLCD_I2C.cpp | 35 +--------- 3 files changed, 25 insertions(+), 80 deletions(-) diff --git a/examples/tinyLCD_I2C_Test/tinyLCD_I2C_Test.ino b/examples/tinyLCD_I2C_Test/tinyLCD_I2C_Test.ino index b685b0a..a7354c1 100644 --- a/examples/tinyLCD_I2C_Test/tinyLCD_I2C_Test.ino +++ b/examples/tinyLCD_I2C_Test/tinyLCD_I2C_Test.ino @@ -24,5 +24,5 @@ void loop() { lcd.setCursor(0,1); lcd.print(millis()/1); lcd.print(" ms"); - delay(1000); + delay(100); } diff --git a/firmware/tinyLCD_I2C/tinyLCD_I2C.ino b/firmware/tinyLCD_I2C/tinyLCD_I2C.ino index 52ef9f3..0eea42a 100644 --- a/firmware/tinyLCD_I2C/tinyLCD_I2C.ino +++ b/firmware/tinyLCD_I2C/tinyLCD_I2C.ino @@ -25,9 +25,8 @@ #define LCD_BACKLIGHT 0x81 #define LCD_NOBACKLIGHT 0x80 -// I2C address/buffersize +// I2C default address #define I2C_SLAVE_ADDRESS 0x50 -#define TWI_RX_BUFFER_SIZE ( 32 ) #define CONTRAST_PIN 2 @@ -49,11 +48,10 @@ volatile byte reg_position; unsigned int buf_ix = 0; // store time when the last char to print was received unsigned long lastwrite = 0; -// char buffer for incoming strings -char bigbuffer[32]; // more data expected volatile byte data_expected = 0; + // initialize the library with the numbers of the interface pins // LiquidCrystal(rs, enable, d4, d5, d6, d7); // Breadboard: @@ -68,7 +66,7 @@ void setup() { slave_address = I2C_SLAVE_ADDRESS; } TinyWireS.begin(slave_address); - TinyWireS.onReceive(receiveEvent); + TinyWireS.onReceive(receive_event); //TinyWireS.onRequest(requestEvent); analogWrite(CONTRAST_PIN, 10); lcd.begin(16,2); @@ -77,15 +75,12 @@ void setup() { } void loop() { - if ( buf_ix > 0 && micros() - lastwrite > CHARBUF_TIMEOUT ) { - flush_buffer(); - } TinyWireS_stop_check(); } // not used yet: -void requestEvent() +void request_event() { TinyWireS.send(i2c_regs[reg_position]); // Increment the reg position on each read, and loop back to zero @@ -94,40 +89,25 @@ void requestEvent() lcd.print("="); } -void receiveEvent(uint8_t howMany) { - //static int buf_ix = 0; - if (howMany < 1) - { - // Sanity-check - return; - } - - char cmd = TinyWireS.receive(); - // wait for a command - if ( cmd == 0 ) { - while (howMany < 1) { - tws_delay(1); - } - char rxbuffer = TinyWireS.receive(); - commandByte(rxbuffer); - } - /* - // direct print - else if ( cmd == 1 ) { - //lcd.print(rxbuffer); - // are we receiving a larger string ? Keep adding to buffer - if ( buf_ix == 0 || micros() - lastwrite < CHARBUF_TIMEOUT ) { - bigbuffer[buf_ix] = rxbuffer; - buf_ix++; - } - } - */ - else if ( cmd > 1 ) { - lcd.print(cmd); - } +void receive_event(uint8_t howMany) { + //static int buf_ix = 0; + if (howMany < 1) { + // Sanity-check + return; + } + while (TinyWireS.available()) { + char cmd = TinyWireS.receive(); + // wait for a command + if ( cmd == 0 && howMany > 1 ) { + char rxbuffer = TinyWireS.receive(); + command_byte(rxbuffer, howMany - 2); + } else if ( cmd > 1 ) { + lcd.print(cmd); + } + } } -void commandByte(char c) { +void command_byte(char c, byte bytesInBuffer) { uint8_t cols, rows, col, row, addr; switch (c) { case LCD_CLEARDISPLAY: @@ -217,12 +197,6 @@ void lcd_revision() { lcd.print("$Revision: 1.13 $"); } -// flush receive buffer and output to LCD -void flush_buffer() { - lcd.print(bigbuffer); - buf_ix = 0; - memset(&bigbuffer[0], 0, sizeof(bigbuffer)); -} void test_lcd() { lcd.print("==T=E=S=T======="); diff --git a/src/tinyLCD_I2C/tinyLCD_I2C.cpp b/src/tinyLCD_I2C/tinyLCD_I2C.cpp index c94015f..f9b2584 100644 --- a/src/tinyLCD_I2C/tinyLCD_I2C.cpp +++ b/src/tinyLCD_I2C/tinyLCD_I2C.cpp @@ -173,43 +173,14 @@ inline size_t tinyLCD_I2C::write(uint8_t value) { // write either command or data void tinyLCD_I2C::send(uint8_t value, uint8_t mode) { - /* - uint8_t highnib=value>>4; - uint8_t lownib=value & 0x0F; - write4bits((highnib)|mode); - write4bits((lownib)|mode); - */ Wire.beginTransmission(_Addr); - if ( mode == 0 ) { - Wire.write(mode); - } - // bad delay, need to make the opposite site faster by using a buffer - delayMicroseconds(80); + if ( mode == 0 ) { + Wire.write(mode); + } Wire.write((int)(value)); Wire.endTransmission(); } -/* -void tinyLCD_I2C::write4bits(uint8_t value) { - expanderWrite(value); - pulseEnable(value); -} - -void tinyLCD_I2C::expanderWrite(uint8_t _data){ - Wire.beginTransmission(_Addr); - Wire.write((int)(_data) | _backlightval); - Wire.endTransmission(); -} - -void tinyLCD_I2C::pulseEnable(uint8_t _data){ - expanderWrite(_data | En); // En high - delayMicroseconds(1); // enable pulse must be >450ns - - expanderWrite(_data & ~En); // En low - delayMicroseconds(50); // commands need > 37us to settle -} -*/ - // Alias functions