Created
November 17, 2017 15:35
-
-
Save CapnBry/7979cc09daafceb1aef4ed4bb349bfe7 to your computer and use it in GitHub Desktop.
Add character LCD support to emonesp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <ESP8266Wifi.h> | |
#include "lcd.h" | |
static LiquidCrystal lcd(4, 5, 12, 13, 14, 16); | |
#define MAXCT 4 | |
static int _ctdata[MAXCT]; | |
static int _vrms; | |
static const char LCDCHAR_HOUSE[8] PROGMEM = { | |
0b00000, | |
0b00100, | |
0b01110, | |
0b11111, | |
0b01110, | |
0b01010, | |
0b01010, | |
0b00000 | |
}; | |
static const char LCDCHAR_SUN[8] PROGMEM = { | |
0b00100, | |
0b10101, | |
0b01110, | |
0b01010, | |
0b11011, | |
0b01110, | |
0b10101, | |
0b00100 | |
}; | |
static const char LCDCHAR_ANTENNA[8] PROGMEM = { | |
0b00000, | |
0b11111, | |
0b10101, | |
0b01110, | |
0b00100, | |
0b00100, | |
0b00100, | |
0b00000 | |
}; | |
static const char LCDCHAR_WIFI_0[8] PROGMEM = { | |
0b00000, | |
0b00000, | |
0b10001, | |
0b01010, | |
0b00000, | |
0b01010, | |
0b10001, | |
0b00000 | |
}; | |
static const char LCDCHAR_WIFI_1[8] PROGMEM = { | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b10000, | |
0b10000, | |
0b00000 | |
}; | |
static const char LCDCHAR_WIFI_2[8] PROGMEM = { | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00100, | |
0b00100, | |
0b10100, | |
0b10100, | |
0b00000 | |
}; | |
static const char LCDCHAR_WIFI_3[8] PROGMEM = { | |
0b00000, | |
0b00001, | |
0b00001, | |
0b00101, | |
0b00101, | |
0b10101, | |
0b10101, | |
0b00000 | |
}; | |
static void parseCtInput(String &input) | |
{ | |
char *cstr = new char[input.length()+1]; | |
strcpy(cstr, input.c_str()); | |
// Parse ct1:XXX,ct2:XXX,...,t0:XXX,vrms:XXX | |
char *reading = strtok(cstr, ","); | |
while (reading != 0) | |
{ | |
char *separator = strchr(reading, ':'); | |
if (separator != 0) | |
{ | |
int val = atoi(separator+1); | |
*separator = '\0'; | |
if (strncmp(reading, "ct", 2) == 0) | |
{ | |
uint8_t which = reading[2] - '1'; | |
if (which < MAXCT) | |
_ctdata[which] = val; | |
} | |
else if (strcmp(reading, "vrms") == 0) | |
_vrms = val; | |
} | |
reading = strtok(0, ","); | |
} | |
delete[] cstr; | |
} | |
char lcd_getWifiStatus() | |
{ | |
byte wifi_status; | |
if (WiFi.status() != WL_CONNECTED) | |
wifi_status = '\3'; | |
else | |
{ | |
int rssi = WiFi.RSSI(); | |
if (rssi > -65) | |
wifi_status = '\6'; | |
else if (rssi > -80) | |
wifi_status = '\5'; | |
else if (rssi > -95) | |
wifi_status = '\4'; | |
else | |
wifi_status = ' '; | |
} | |
return wifi_status; | |
} | |
void lcd_updateDisplay() | |
{ | |
char buf[17]; | |
int solar = _ctdata[2] + _ctdata[3]; | |
int use = _ctdata[0] + _ctdata[1] + solar; | |
solar = constrain(solar, -9999, 99999); | |
use = constrain(use, -9999, 99999); | |
/* | |
* [H99000W Use 120V] | |
* [S 9000W Solar A1] | |
*/ | |
snprintf(buf, sizeof(buf), "%c%5dW Use %3dV", | |
'\0', use, (_vrms+50) / 100); | |
lcd.setCursor(0, 0); | |
lcd.write(buf, sizeof(buf)-1); | |
snprintf(buf, sizeof(buf), "\1%5dW Solar \2%c", | |
solar, lcd_getWifiStatus()); | |
lcd.setCursor(0, 1); | |
lcd.write(buf, sizeof(buf)-1); | |
} | |
void ICACHE_FLASH_ATTR lcd_createChar(uint8_t idx, PGM_P data) | |
{ | |
lcd.command(LCD_SETCGRAMADDR | (idx << 3)); | |
for (uint8_t i=0; i<8; i++) | |
lcd.write(pgm_read_byte(data++)); | |
} | |
void ICACHE_FLASH_ATTR lcd_setup() | |
{ | |
lcd.begin(16, 2); | |
lcd_createChar(0, LCDCHAR_HOUSE); | |
lcd_createChar(1, LCDCHAR_SUN); | |
lcd_createChar(2, LCDCHAR_ANTENNA); | |
lcd_createChar(3, LCDCHAR_WIFI_0); | |
lcd_createChar(4, LCDCHAR_WIFI_1); | |
lcd_createChar(5, LCDCHAR_WIFI_2); | |
lcd_createChar(6, LCDCHAR_WIFI_3); | |
lcd_updateDisplay(); | |
} | |
void lcd_publish(String &input) | |
{ | |
parseCtInput(input); | |
lcd_updateDisplay(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _EMONESP_LCD_H | |
#define _EMONESP_LCD_H | |
#include <Arduino.h> | |
void lcd_setup(); | |
void lcd_publish(String &input); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment