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> #include <LiquidCrystal_I2C.h>
class LCDWrapper { #define BUFSIZE 32
class LCDWrapper : public LiquidCrystal_I2C{
private: private:
LiquidCrystal_I2C *display; uint8_t rows, cols;
size_t cursor; char* buffer = NULL;
uint8_t cols;
uint8_t rows;
public: public:
LCDWrapper(uint8_t addr, uint8_t cols, uint8_t rows){ LCDWrapper(uint8_t addr, uint8_t cols, uint8_t rows, uint8_t charsize=LCD_5x8DOTS) : LiquidCrystal_I2C(addr,cols,rows,charsize) {
rows = rows; this->cols = cols;
cols = cols; this->rows = rows;
*display = LiquidCrystal_I2C(addr, cols, rows, LCD_5x8DOTS);
cursor = 0;
} }
~LCDWrapper(){ display = NULL; }
void begin(){ void begin(){
display->begin(); LiquidCrystal_I2C::begin();
display->backlight(); LiquidCrystal_I2C::backlight();
display->home();
clear();
} }
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, ...){ void printf(const char *fmt, ...){
va_list va; buffer = (char*) malloc( BUFSIZE * sizeof(char*));
va_start(va, fmt); buffer[0] = 0;
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
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, ...){ void printfn(const uint8_t line, const char *fmt, ...){
va_list va; buffer = (char*) malloc( BUFSIZE * sizeof(char*));
va_start(va, fmt); buffer[0] = 0;
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
display->setCursor(0,line); va_list ap;
display->print(buffer); 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, ...){ void printfn_overflow(const uint8_t line, const char *fmt, ...){
va_list va; buffer = (char*) malloc( BUFSIZE * sizeof(char*));
va_start(va, fmt); buffer[0] = 0;
char buffer[34] = {0};
int size = sprintf(buffer, fmt, va);
va_end(va);
display->setCursor(0,line); va_list ap;
display->print(buffer); 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++ ){ LiquidCrystal_I2C::setCursor(0, line);
delay(50); LiquidCrystal_I2C::print(buffer);
display->setCursor(0,line); if( size <= cols ){ free(buffer); return; }
display->print(buffer+offset);
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 #define SEND_TIMEOUT 10000
// Peripherals // Peripherals
LCDWrapper screen = LCDWrapper(0x20, 16, 2); LCDWrapper screen(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper(); XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
@ -54,13 +54,17 @@ void loop() {
} }
void send_data(){ void send_data(){
Serial.println(" -> message");
// 1. prepare message // 1. prepare message
send.setOpcode(1); send.setOpcode(1);
send.setDist(dist); send.setDist(dist);
send.setTTL(MESSAGE_TTL); send.setTTL(MESSAGE_TTL);
uint8_t data[19] = {0}; uint8_t data[32] = {0};
memcpy(data, "https://git.xdrm.io", 19); strcpy(data, "https://git.xdrm.io");
send.setData(data, 19); Serial.print("'");
Serial.print( (char*) data);
Serial.println("'");
send.setData(data, strlen(data));
send.setSender(SENDERID); send.setSender(SENDERID);
// 2. debug // 2. debug
@ -75,11 +79,13 @@ void send_data(){
void manage_discover(){ void manage_discover(){
Serial.println(" <- discover");
// security // security
if( recv.getDist() == 0 && recv.getSender() != 255 ) if( recv.getDist() == 0 && recv.getSender() != SENDERID )
return; 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 // ignore if same wave
if( recv.getWave() == wave_id ){ if( recv.getWave() == wave_id ){
@ -97,11 +103,12 @@ void manage_discover(){
send.setDist(dist); send.setDist(dist);
send.setSender(SENDERID); 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); xbee.broadcast(send);
} }
void manage_message(){ void manage_message(){
Serial.println(" <- message");
screen.clear(); screen.clear();
screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); screen.printfn(0, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
delay(500); delay(500);

View File

@ -37,7 +37,7 @@ void loop() {
send.setSender(SENDERID); send.setSender(SENDERID);
screen.clear(); 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"); if( xbee.broadcast(send) == XBWSEND_OK ) screen.printfn(1, "sent");
else screen.printfn(1, "failed"); else screen.printfn(1, "failed");
@ -46,7 +46,6 @@ void loop() {
/* [2] Listen for incoming data */ /* [2] Listen for incoming data */
if( xbee.receive(recv) != XBWRECV_OK ) if( xbee.receive(recv) != XBWRECV_OK )
return; return;
uint8_t opcode = recv.getOpcode(); uint8_t opcode = recv.getOpcode();
// ignore DISCOVER requests // ignore DISCOVER requests