Skip to content

Instantly share code, notes, and snippets.

@AndrewFromMelbourne
Created January 4, 2014 10:45
Show Gist options
  • Save AndrewFromMelbourne/8254091 to your computer and use it in GitHub Desktop.
Save AndrewFromMelbourne/8254091 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Wire.h>
//-------------------------------------------------------------------------
enum
{
LCD_CLEAR = 0x01,
LCD_HOME = 0x02,
LCD_NO_DISPLAY = 0x03,
LCD_DISPLAY = 0x04,
LCD_NO_BLINK = 0x05,
LCD_BLINK = 0x06,
LCD_NO_CURSOR = 0x07,
LCD_CURSOR = 0x08,
LCD_SCROLL_DISPLAY_LEFT = 0x09,
LCD_SCROLL_DISPLAY_RIGHT = 0x0A,
LCD_LEFT_TO_RIGHT = 0x0B,
LCD_RIGHT_TO_LEFT = 0x0C,
LCD_AUTOSCROLL = 0x0D,
LCD_NO_AUTOSCROLL = 0x0E,
LCD_CREATE_CHAR = 0x0F,
LCD_SET_CURSOR = 0x10,
LCD_WRITE_BYTE = 0x11,
LCD_BACKLIGHT = 0x7F,
LCD_END = 0x80
};
//-------------------------------------------------------------------------
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
uint8_t backlightPin = 10;
//-------------------------------------------------------------------------
uint8_t blockingRead()
{
while (Serial.available() == 0)
{
// wait
}
return Serial.read();
}
//-------------------------------------------------------------------------
void announce()
{
Serial.print("LCD");
}
//-------------------------------------------------------------------------
void banner()
{
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("LCD Serial");
lcd.setCursor(5, 1);
lcd.print("Server");
}
//-------------------------------------------------------------------------
void setup()
{
pinMode(backlightPin, OUTPUT);
analogWrite(backlightPin, 0);
Serial.begin(115200);
lcd.begin(16, 2);
banner();
announce();
}
//-------------------------------------------------------------------------
void loop()
{
if (Serial.available() > 0)
{
uint8_t command = Serial.read();
switch (command)
{
case LCD_CLEAR:
lcd.clear();
break;
case LCD_HOME:
lcd.home();
break;
case LCD_NO_DISPLAY:
lcd.noDisplay();
break;
case LCD_DISPLAY:
lcd.display();
break;
case LCD_NO_BLINK:
lcd.noBlink();
break;
case LCD_BLINK:
lcd.blink();
break;
case LCD_NO_CURSOR:
lcd.noCursor();
break;
case LCD_CURSOR:
lcd.cursor();
break;
case LCD_SCROLL_DISPLAY_LEFT:
lcd.scrollDisplayLeft();
break;
case LCD_SCROLL_DISPLAY_RIGHT:
lcd.scrollDisplayRight();
break;
case LCD_LEFT_TO_RIGHT:
lcd.leftToRight();
break;
case LCD_RIGHT_TO_LEFT:
lcd.rightToLeft();
break;
case LCD_AUTOSCROLL:
lcd.autoscroll();
break;
case LCD_NO_AUTOSCROLL:
lcd.noAutoscroll();
break;
case LCD_CREATE_CHAR:
{
uint8_t number = blockingRead();
uint8_t data[8];
for (uint8_t i = 0 ; i < 8 ; ++i)
{
data[i] = blockingRead();
}
lcd.createChar(number, data);
break;
}
case LCD_SET_CURSOR:
{
uint8_t column = blockingRead();
uint8_t row = blockingRead();
lcd.setCursor(column, row);
break;
}
case LCD_WRITE_BYTE:
{
uint8_t byte = blockingRead();
lcd.write(byte);
break;
}
case LCD_BACKLIGHT:
{
uint8_t backlightValue = blockingRead();
analogWrite(backlightPin, backlightValue);
break;
}
case LCD_END:
banner();
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment