Skip to content

Instantly share code, notes, and snippets.

@ltlapy
Last active September 15, 2017 22:30
Show Gist options
  • Save ltlapy/4688b7b815eb18e1ed0b31f5ab83b33e to your computer and use it in GitHub Desktop.
Save ltlapy/4688b7b815eb18e1ed0b31f5ab83b33e to your computer and use it in GitHub Desktop.
OLED-Ported version of Arduino LCD Smartie (Powered by u8x8)
// include the library code:
#include <U8x8lib.h>
// initialize the library
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
// these constants will automatically change by constructor
const int numRows = (int)u8x8.getRows();
const int numCols = (int)u8x8.getCols();
// this array will save current cursor position
int curcursorpos[2] = {0, 0};
// this array will save custom charactor
uint8_t customchar[8][8];
void movecursor_x_rel(int x) { // Move cursor relatively on X-axis (negative=left/positive=right)
curcursorpos[0] = curcursorpos[0] + x;
u8x8.setCursor(curcursorpos[0], curcursorpos[1]);
return;
}
void movecursor(int x, int y) {
curcursorpos[0] = x;
curcursorpos[1] = y;
u8x8.setCursor(curcursorpos[0], curcursorpos[1]);
return;
}
void setup() {
Serial.begin(9600);
// set up the OLCD
u8x8.begin();
// initalize custom char array
for (int ix = 0; ix <= 7; ix++) {
for (int iy = 0; iy <= 7; iy++) {
customchar[ix][iy] = 0;
}
}
//u8x8.setFlipMode(1);
u8x8.setFont(u8x8_font_amstrad_cpc_extended_f);
u8x8.clear();
u8x8.inverse();
u8x8.print("**OLED SMARTIE**");
u8x8.noInverse();
u8x8.setCursor(5, 1);
u8x8.print("on Arduino");
u8x8.setCursor(0, 2);
u8x8.setCursor(0, 4 /*numRows - 1*/ );
u8x8.print("Size: ");
u8x8.print(String(numCols) + "x" + String(numRows));
}
byte serial_getch() {
int incoming;
while (Serial.available() == 0) {}
// read the incoming byte:
incoming = Serial.read();
return (byte) (incoming & 0xff);
}
void loop() {
byte rxbyte;
byte temp;
rxbyte = serial_getch();
if (rxbyte <= 15) {
if (rxbyte > 7) {
rxbyte -= 8;
}
u8x8.drawTile(curcursorpos[0], curcursorpos[1], 1, customchar[rxbyte]);
} else {
if (rxbyte == 254) //Matrix Orbital uses 254 prefix for commands
{
switch (serial_getch())
{
case 66: //backlight on (at previously set brightness)
// This feature won't work if device has no support
u8x8.setContrast(255);
break;
case 70: //backlight off
u8x8.setContrast(0);
break;
case 71: //set cursor position
temp = (serial_getch() - 17); //get column byte
movecursor(temp, serial_getch()-1);
break;
case 72: //cursor home (reset display position)
movecursor(0, 0);
break;
case 74: //show underline cursor
// not available on OLED
break;
case 75: //underline cursor off
case 84: //block cursor off
// not available on OLED
break;
case 76: //move cursor left
movecursor_x_rel(-1);
break;
case 77: //move cursor right
movecursor_x_rel(1);
break;
case 78:
{ //define custom char
int ix = serial_getch(); // get char address
for (int iy = 0; iy <= 7; iy++) {
customchar[ix][iy] = serial_getch();
}
}
break;
case 83: //show blinking block cursor
// not available on OLED
break;
case 86: //GPO OFF
//implement later
break;
case 87: //GPO ON
/*temp = serial_getch();
if (temp == 1)
{
GPO1 = GPO_ON;
}*/
break;
case 88: //clear display, cursor home
u8x8.clear();
movecursor(0, 0);
break;
case 152: //set and remember (doesn't save value, though)
case 153: //set backlight brightness
//not implemented
break;
//these commands ignored (no parameters)
case 35: //read serial number
case 36: //read version number
case 55: //read module type
case 59: //exit flow-control mode
case 65: //auto transmit keypresses
case 96: //auto-repeat mode off (keypad)
case 67: //auto line-wrap on
case 68: //auto line-wrap off
case 81: //auto scroll on
case 82: //auto scroll off
case 104: //init horiz bar graph
case 109: //init med size digits
case 115: //init narrow vert bar graph
case 118: //init wide vert bar graph
break;
default:
//all other commands ignored and parameter byte discarded
temp = serial_getch(); //dump the command code
break;
}
return;
} //END OF COMMAND HANDLER
u8x8.print((char)rxbyte); //otherwise a plain char so we print it to lcd
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment