opportunistic-xbee/lcd.h

81 lines
1.9 KiB
C++

#ifndef _LCD_H_
#define _LCD_H_
#include <LiquidCrystal_I2C.h>
#define BUFSIZE 64
class LCDWrapper : public LiquidCrystal_I2C{
private:
uint8_t rows, cols;
char* buffer = NULL;
public:
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;
}
void begin(){
LiquidCrystal_I2C::begin();
LiquidCrystal_I2C::backlight();
}
void printfn(const uint8_t line, const char *fmt, ...){
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
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, ...){
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
va_list ap;
va_start(ap, fmt);
int size = vsnprintf(buffer, BUFSIZE, fmt, ap);
va_end(ap);
buffer[BUFSIZE-1] = 0;
if( size < 0 || size >= BUFSIZE ){ free(buffer); return; }
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);
}
protected:
void printf(const char *fmt, ...) {
buffer = (char*) malloc( BUFSIZE * sizeof(char*));
buffer[0] = 0;
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);
}
};
#endif