Skip to content

Instantly share code, notes, and snippets.

@kylegordon
Created October 20, 2012 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylegordon/3923873 to your computer and use it in GitHub Desktop.
Save kylegordon/3923873 to your computer and use it in GitHub Desktop.
lcdlogger
#include <LCDLogger.h>
#include <LiquidCrystal.h>
// Always check the feature is enabled before any code.
#ifdef ENABLE_LCD_OUTPUT
// initialize the library with the numbers of the interface pins
// The arguments are in this order (RS, EN, D4, D5, D6, D7)
LiquidCrystal LCDLogger::lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D1_PIN, LCD_D2_PIN, LCD_D3_PIN, LCD_D4_PIN);
long LCDLogger::last_time; // The last time the screen was updated
String LCDLogger::displayed_metric; // The last displayed metric
String LCDLogger::metricname; // Metric name
String LCDLogger::token;
int LCDLogger::deviceindex = 0;
//char* LCDLogger::newdevices[][100];
const char* devices[][2] = { // Endless list, 2 wide
{"DS18B20.28b8c81d300e5.Temp", "Device 1"},
{"DS18B20.2816401d3005f.Temp", "Device 2"},
{"DS18B20.28ac871d300e4.Temp", "Device 3"}
};
/**
* Writes the message to the LCD.
*/
void LCDLogger::log(){
if ((
// If the time has been reached for an update
((millis() - last_time) > (LCD_CYCLE_TIME * 1000))
// And the current metric is different from the displayed metric
&& (displayed_metric != m.nameSpace)
// OR if the last_time is unset (just started)
) || (last_time == 0)) {
int i;
int entries = sizeof(devices)/sizeof(int); // Get the length of the list
metricname = m.nameSpace;
for (i = 0; i < entries;i++) // Go through the list...
{
if (String(devices[i][0]) == metricname) { // If something matches the lookup table
metricname = devices[i][1]; // use the name from the lookup table.
break;}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(metricname); // Print the new, hopefully matched, device name
lcd.setCursor(0, 1); // Drop down a line
lcd.print(m.value); // Print the value, a space and the unit.
lcd.print(" ");
lcd.print(m.units);
displayed_metric = m.nameSpace; // Tag this for the next cycle
last_time = millis(); // Note the time to calculate the delta next time round
}
//last_time = millis();
}
/**
* Configures the LCD display.
*/
void LCDLogger::begin(){
lcd.begin(LCD_WIDTH, LCD_HEIGHT); // Width and height of LCD. Defined in config
/*
Example working constant list
const char* devices[][2] = { // Endless list, 2 wide
{"DS18B20.28b8c81d300e5.Temp", "Device 1"},
{"DS18B20.2816401d3005f.Temp", "Device 2"},
{"DS18B20.28ac871d300e4.Temp", "Device 3"}
};
Match Aliases to real devices. Split up each line with strtok, and populate the lookup table
It should have a list similar to devices[][] above.
LCD_ALIAS_REAL = "DS18B20.28b8c81d300e5.Temp,DS18B20.2816401d3005f.Temp,DS18B20.28ac871d300e4.Temp"
LCD_ALIAS_ALIAS = "Front_air_intake,Bottom_hose_temp,Top_hose_temp"
*/
// Submitted by mgdm from https://gist.github.com/3914954
char **newdevices;
char *lcd_device_string = strdup(LCD_ALIAS_REAL);
char *lcd_alias_string = strdup(LCD_ALIAS_ALIAS);
char *deviceToken, *aliasToken, *current, *currentDevice, *currentAlias;
int deviceIndex = 0, deviceCount = 1;
#define LCD_ALIAS_REAL "DS18B20.28b8c81d300e5.Temp,DS18B20.2816401d3005f.Temp,DS18B20.28ac871d300e4.Temp"
#define LCD_ALIAS_ALIAS "Front_air_intake,Bottom_hose_temp,Top_hose_temp"
current = LCD_ALIAS_REAL;
while (*(current++) != '\0') {
if (*current == ',') deviceCount++;
}
// Allocate an array of pointers to strings, twice the number of devices
// to account for aliases too
newdevices = (char **) calloc(sizeof(char *), 2 * deviceCount);
deviceToken = strtok_r(lcd_device_string, ",", &currentDevice);
aliasToken = strtok_r(lcd_alias_string, ",", &currentAlias);
while (deviceToken != NULL) {
// Print the current token
printf("%s -> %s\n", deviceToken, aliasToken);
// Put the device name into the array
// newdevices[deviceIndex] = strdup(deviceToken);
// newdevices[deviceIndex + 1] = strdup(aliasToken);
// Should be side by side
newdevices[deviceIndex][0] = strdup(deviceToken);
newdevices[deviceIndex][1] = strdup(aliasToken);
// Move to the next token, after the ,
deviceToken = strtok_r(NULL,",", &currentDevice);
aliasToken = strtok_r(NULL, ",", &currentAlias);
// Increment the index - FIXME this should happen after the second list is parsed
//deviceIndex += 2;
deviceIndex++;
}
int i;
for (i = 0; i < 6; i = i + 1) {
Serial.println(newdevices[i]);
}
free(lcd_device_string);
free(lcd_alias_string);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment