update 'lcd' to *inherit* LiquidCrystal_I2C, not to wrap it | tested and working

This commit is contained in:
Adrien Marquès 2018-12-09 15:21:15 +01:00
parent a2688fa19d
commit 90529c814f
3 changed files with 65 additions and 53 deletions

92
lcd.h
View File

@ -3,69 +3,75 @@
#include <LiquidCrystal_I2C.h>
class LCDWrapper {
#define BUFSIZE 32
class LCDWrapper : public LiquidCrystal_I2C{
private:
LiquidCrystal_I2C *display;
size_t cursor;
uint8_t cols;
uint8_t rows;
uint8_t rows, cols;
char* buffer = NULL;
public:
LCDWrapper(uint8_t addr, uint8_t cols, uint8_t rows){
rows = rows;
cols = cols;
*display = LiquidCrystal_I2C(addr, cols, rows, LCD_5x8DOTS);
cursor = 0;
LCDWrapper(uint8_t addr, uint8_t cols, uint8_t rows, uint8_t charsize=LCD_5x8DOTS) : LiquidCrystal_I2C(addr,cols,rows,charsize) {
this->cols = cols;
this->rows = rows;
}
~LCDWrapper(){ display = NULL; }
void begin(){
display->begin();
display->backlight();
display->home();
clear();
LiquidCrystal_I2C::begin();
LiquidCrystal_I2C::backlight();
}
void move(const uint8_t line, const uint8_t c){ display->setCursor(line, c); }
void clear(){ cursor = 0; display->clear(); }
void printf(const char *fmt, ...){
va_list va;
va_start(va, fmt);
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
display->print(buffer);
va_list ap;
va_start(ap, fmt);
int size = vsnprintf(buffer, BUFSIZE, fmt, ap);
va_end(ap);
if( size < 0 || size > BUFSIZE ){ free(buffer); return; }
LiquidCrystal_I2C::print(buffer);
free(buffer);
}
void printfn(const uint8_t line, const char *fmt, ...){
va_list va;
va_start(va, fmt);
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
display->setCursor(0,line);
display->print(buffer);
va_list ap;
va_start(ap, fmt);
int size = vsnprintf(buffer, BUFSIZE, fmt, ap);
va_end(ap);
if( size < 0 || size > BUFSIZE ){ free(buffer); return; }
LiquidCrystal_I2C::setCursor(0, line);
LiquidCrystal_I2C::print(buffer);
free(buffer);
}
void printfn_overflow(const uint8_t line, const char *fmt, ...){
va_list va;
va_start(va, fmt);
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
display->setCursor(0,line);
display->print(buffer);
va_list ap;
va_start(ap, fmt);
int size = vsnprintf(buffer, BUFSIZE, fmt, ap);
va_end(ap);
if( size < 0 || size > BUFSIZE ){ free(buffer); return; }
for( uint8_t offset = 0 ; size-offset > cols ; offset++ ){
delay(50);
display->setCursor(0,line);
display->print(buffer+offset);
LiquidCrystal_I2C::setCursor(0, line);
LiquidCrystal_I2C::print(buffer);
if( size <= cols ){ free(buffer); return; }
for( uint8_t offset = 1 ; offset <= size-cols ; offset++ ){
delay(200);
LiquidCrystal_I2C::setCursor(0, line);
LiquidCrystal_I2C::print(buffer+offset);
}
free(buffer);
}
};

View File

@ -6,7 +6,7 @@
#define SEND_TIMEOUT 10000
// Peripherals
LCDWrapper screen = LCDWrapper(0x20, 16, 2);
LCDWrapper screen(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA
@ -54,13 +54,17 @@ void loop() {
}
void send_data(){
Serial.println(" -> message");
// 1. prepare message
send.setOpcode(1);
send.setDist(dist);
send.setTTL(MESSAGE_TTL);
uint8_t data[19] = {0};
memcpy(data, "https://git.xdrm.io", 19);
send.setData(data, 19);
uint8_t data[32] = {0};
strcpy(data, "https://git.xdrm.io");
Serial.print("'");
Serial.print( (char*) data);
Serial.println("'");
send.setData(data, strlen(data));
send.setSender(SENDERID);
// 2. debug
@ -75,11 +79,13 @@ void send_data(){
void manage_discover(){
Serial.println(" <- discover");
// security
if( recv.getDist() == 0 && recv.getSender() != 255 )
if( recv.getDist() == 0 && recv.getSender() != SENDERID )
return;
screen.printfn(0, "dsc[%3d] %3d / %3d", recv.getSender(), recv.getDist(), recv.getWave());
screen.clear();
screen.printfn(0, "dsc[%3d] %3d/%3d", recv.getSender(), recv.getDist(), recv.getWave());
// ignore if same wave
if( recv.getWave() == wave_id ){
@ -97,11 +103,12 @@ void manage_discover(){
send.setDist(dist);
send.setSender(SENDERID);
screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());
screen.printfn(1, "dsc[%3d] %3d/%3d", send.getSender(), send.getDist(), send.getWave());
xbee.broadcast(send);
}
void manage_message(){
Serial.println(" <- message");
screen.clear();
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
delay(500);
@ -121,4 +128,4 @@ void manage_message(){
screen.printfn(1, "msg[%3d/%3d] %3d", send.getDist(), send.getTTL(), send.getSize());
xbee.broadcast(send);
}
}

View File

@ -37,7 +37,7 @@ void loop() {
send.setSender(SENDERID);
screen.clear();
screen.printfn(1, "dsc[%3d] %3d / %3d", send.getSender(), send.getDist(), send.getWave());
screen.printfn(0, "dsc[%3d] %3d/%3d", send.getSender(), send.getDist(), send.getWave());
if( xbee.broadcast(send) == XBWSEND_OK ) screen.printfn(1, "sent");
else screen.printfn(1, "failed");
@ -46,7 +46,6 @@ void loop() {
/* [2] Listen for incoming data */
if( xbee.receive(recv) != XBWRECV_OK )
return;
uint8_t opcode = recv.getOpcode();
// ignore DISCOVER requests