Güneş Direk Back
Library for OLED display 96x64 px
uOLED-96-G1 library for Arduino.
This is an improved version of Oscar Gonzalez's library, ( available here ) which wasn't working for me, i also added some extra commands.
#define OLED_CLEAR 0x45 #define OLED_ACK 0x06 #define OLED_RESET 9 #define OLED_BGCOLOR 0x42 #define OLED_INITDELAYMS 1000 #define OLED_DETECTBAUD 0x55 #define OLED_CONTROL 0x59 #define OLED_PUTPIXEL 0x50 #define OLED_LINE 0x4C #define OLED_CIRCLE 0x43 #define OLED_RECTANGLE 0x72 #define OLED_TRIANGLE 0x47 #define OLED_PENSIZE 0x70 #define OLED_TEXTFORMATTED 0x54 #define OLED_STRING 0x73 #define OLED_ADDCHAR 0x41 char OLED_GetResponse() { byte incomingByte = OLED_ACK; // Wait for data avaliable while (!Serial.available()) { delay(1); } // Read incoming byte incomingByte = Serial.read(); //LED_Blink(50); return incomingByte; } void OLED_Clear() { Serial.print(OLED_CLEAR, BYTE); // Pixel write delay(20); OLED_GetResponse(); } void OLED_ResetDisplay() { digitalWrite(OLED_RESET, LOW); delay(20); digitalWrite(OLED_RESET, HIGH); delay(20); } void OLED_Init() { // First reset display OLED_ResetDisplay(); delay(OLED_INITDELAYMS); // Wait for init // Autodetect baudrate Serial.print(OLED_DETECTBAUD, BYTE); OLED_GetResponse(); } //--------COMMAND SETS----------------------------------------- void OLED_PutPixel(byte x, byte y, unsigned int color){ Serial.print(OLED_PUTPIXEL,BYTE); Serial.print(x); Serial.print(y); //Color Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of color OLED_GetResponse(); } void OLED_ChangeBg(int red, int green, int blue){ //changes background color, doesnt clear the screen Serial.print(OLED_BGCOLOR,BYTE); unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of color OLED_GetResponse(); } void OLED_DrawCircle(byte x, byte y, byte radius, int red, int green , int blue){ Serial.print(OLED_CIRCLE,BYTE); Serial.print(x); // x Serial.print(y); // y Serial.print(radius); // radius //Color unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of OLED_GetResponse(); } void OLED_DrawRectangle(byte x, byte y, byte w, byte h, int red, int green , int blue) { Serial.print(OLED_RECTANGLE,BYTE); Serial.print(x); // x1 Serial.print(y); // y1 Serial.print(x+w); // x2 Serial.print(y+h); // y1 // Color unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of OLED_GetResponse(); } void OLED_DrawText(byte column, byte row, byte font_size, char text[], int red, int green , int blue) { Serial.print(OLED_STRING,BYTE); // s (Formated text) Serial.print(column); // column Serial.print(row); // row Serial.print(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font) // Color unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of for (int i=0 ; i<strlen(text) ; i++) //send chars one by one { Serial.print(text[i],BYTE); // character to write } Serial.print(0x00,BYTE); // string terminator (always 0x00) OLED_GetResponse(); } void OLED_DrawChar(byte column, byte row, byte font_size, char c, int red, int green , int blue) { Serial.print(OLED_TEXTFORMATTED,BYTE); // T (Formated) Serial.print(c,BYTE); Serial.print(column); Serial.print(row); // Color unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of OLED_GetResponse(); } void OLED_DrawLine(byte x1, byte y1, byte x2, byte y2, int red, int green , int blue){ Serial.print(OLED_LINE,BYTE); Serial.print(x1); Serial.print(y1); Serial.print(x2); Serial.print(y2); // Color unsigned int color = (red<<11)|(green<<5)|blue; // color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of OLED_GetResponse(); } void OLED_AddChar(byte charno,byte row1, byte row2, byte row3, byte row4, byte row5, byte row6, byte row7, byte row8){ Serial.print(OLED_ADDCHAR,BYTE); Serial.print(row1); Serial.print(row2); Serial.print(row3); Serial.print(row4); Serial.print(row5); Serial.print(row6); Serial.print(row7); Serial.print(row8); OLED_GetResponse(); } void OLED_SetSolid(boolean solid){ // sets the drawing type, solid or wireframe Serial.print(OLED_PENSIZE,BYTE); if(solid==false) Serial.print(0x01); else Serial.print(0x00); OLED_GetResponse(); } void OLED_DrawTriangle(byte x1, byte y1, byte x2, byte y2, byte x3 ,byte y3, int red, int green , int blue){ Serial.print(OLED_TRIANGLE,BYTE); //Coordinates in anti-clockwise direction Serial.print(x1); Serial.print(y1); Serial.print(x2); Serial.print(y2); Serial.print(x3); Serial.print(y3); // Color unsigned int color = (red<<11)|(green<<5)|blue; //color values are R/G/B : 32/64/32 Serial.print(highByte(color),BYTE); // send first byte of color Serial.print(lowByte(color),BYTE); // send second byte of OLED_GetResponse(); } //------------ OLED DISPLAY CONTROL FUNCTIONS ------------------- void OLED_DisplayOn(boolean value){ //Turns the backlight on/off Serial.print(OLED_CONTROL,BYTE); Serial.print(0x00,BYTE); if(value == false) Serial.print(0x00); else Serial.print(0x01); OLED_GetResponse(); } void OLED_PowerOn(){ //Turns the power on Serial.print(OLED_CONTROL,BYTE); Serial.print(0x03,BYTE); Serial.print(0x01); OLED_GetResponse(); } void OLED_PowerOff(){ //Turns the power off Serial.print(OLED_CONTROL,BYTE); Serial.print(0x03,BYTE); Serial.print(0x00); OLED_GetResponse(); } void OLED_SetContrast(byte value){ Serial.print(OLED_CONTROL,BYTE); Serial.print(0x02,BYTE); Serial.print(value); //1dec to 15dec (default 15) OLED_GetResponse(); } //------------- DISPLAY SPECIFIC COMMAND SET ----------------- void OLED_DimArea(byte x, byte y, byte w, byte h){ //dims given area on the display Serial.print(0x24,BYTE); //spCmd Serial.print(0x44,BYTE); //cmd Serial.print(x); //horizontal start position of screen area to dim (top left corner) Serial.print(y); //vertical start position of screen area to dim (top left corner) Serial.print(w); //horizontal size of the area to dim Serial.print(h); //vertical size of the area to dim OLED_GetResponse(); }