- Sun Mar 21, 2010 12:30 pm
#96752
Code: Select all
#include <NewSoftSerial.h>
// Graphic Serial LCD 128x64
// Arduino Pin 8 (RX) to LCD Pin TX (NOT USED, NOT NECESSARY)
// Arduino Pin 9 (TX) to LCD Pin RX
// Arduino Pin Vin to LCD Vin (Assuming you're powering Arduino externally with 9 VDC)
// Arduino Pin Gnd to LCD Pin Gnd
NewSoftSerial lcdSerialPort(8, 9);
void setup() {
setBaudRate(9600);
clearScreen();
// Set a dim display
setBackgroundBrightness(1);
// Note how by default it prints at upper left
print("hello");
// Note that 0,0 is at lower left side of display
setX(5);
setY(10);
print("world");
// Draw a big X
drawLine(0, 0, 127, 63, 1);
drawLine(127, 0, 0, 63, 1);
// Draw a centered circle
drawCircle(63, 31, 31, 1);
// Draw a box in the center
drawBox(31, 15, 95, 47, 1);
}
void loop()
{
}
void print(char *data){
lcdSerialPort.print(data);
}
void clearScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x00,BYTE);
}
void demo(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x04,BYTE);
}
void toggleSplashScreen(){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x13,BYTE);
}
void setBackgroundBrightness(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x02,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setBaudRate(long value){
// Get the internal reference for this baud rate
char *ref = " ";
if(value == 4800)
ref = "1";
else if(value == 9600)
ref = "2";
else if(value == 19200)
ref = "3";
else if(value == 38400)
ref = "4";
else if(value == 57600)
ref = "5";
else if(value == 115200)
ref = "6";
else
return;
// Since it often rolls back to 115200, try setting it via 115200 first
lcdSerialPort.begin(115200);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
// Now change the serial port to the desired rate, and set it again.
lcdSerialPort.begin(value);
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x07,BYTE);
lcdSerialPort.print(ref);
}
void setX(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x18,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setY(byte value){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x19,BYTE);
lcdSerialPort.print(value,BYTE);
}
void setPixel(byte state){
lcdSerialPort.print(0x50,BYTE);
lcdSerialPort.print(0x40,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0C,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(endX,BYTE);
lcdSerialPort.print(endY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawCircle(byte startX, byte startY, byte radius, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x03,BYTE);
lcdSerialPort.print(startX,BYTE);
lcdSerialPort.print(startY,BYTE);
lcdSerialPort.print(radius,BYTE);
lcdSerialPort.print(state,BYTE);
}
void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x0F,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}
void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
lcdSerialPort.print(0x7C,BYTE);
lcdSerialPort.print(0x05,BYTE);
lcdSerialPort.print(topLeftX,BYTE);
lcdSerialPort.print(topLeftY,BYTE);
lcdSerialPort.print(bottomRightX,BYTE);
lcdSerialPort.print(bottomRightY,BYTE);
lcdSerialPort.print(state,BYTE);
}