|
| 1 | +// |
| 2 | +//==========================================================// |
| 3 | +// Resets display depending on the actual mode. |
| 4 | +static void reset_display(void) |
| 5 | +{ |
| 6 | + displayOff(); |
| 7 | + clear_display(); |
| 8 | + displayOn(); |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +//==========================================================// |
| 14 | +// Turns display on. |
| 15 | +void displayOn(void) |
| 16 | +{ |
| 17 | + sendcommand(0xaf); //display on |
| 18 | +} |
| 19 | + |
| 20 | +//==========================================================// |
| 21 | +// Turns display off. |
| 22 | +void displayOff(void) |
| 23 | +{ |
| 24 | + sendcommand(0xae); //display off |
| 25 | +} |
| 26 | + |
| 27 | +//==========================================================// |
| 28 | +// Clears the display by sendind 0 to all the screen map. |
| 29 | +static void clear_display(void) |
| 30 | +{ |
| 31 | + unsigned char i,k; |
| 32 | + for(k=0;k<8;k++) |
| 33 | + { |
| 34 | + setXY(k,0); |
| 35 | + { |
| 36 | + for(i=0;i<128;i++) //clear all COL |
| 37 | + { |
| 38 | + SendChar(0); //clear all COL |
| 39 | + //delay(10); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +//==========================================================// |
| 48 | +// Actually this sends a byte, not a char to draw in the display. |
| 49 | +// Display's chars uses 8 byte font the small ones and 96 bytes |
| 50 | +// for the big number font. |
| 51 | +static void SendChar(unsigned char data) |
| 52 | +{ |
| 53 | + //if (interrupt && !doing_menu) return; // Stop printing only if interrupt is call but not in button functions |
| 54 | + |
| 55 | + Wire.beginTransmission(OLED_address); // begin transmitting |
| 56 | + Wire.write(0x40);//data mode |
| 57 | + Wire.write(data); |
| 58 | + Wire.endTransmission(); // stop transmitting |
| 59 | +} |
| 60 | + |
| 61 | +//==========================================================// |
| 62 | +// Prints a display char (not just a byte) in coordinates X Y, |
| 63 | +// being multiples of 8. This means we have 16 COLS (0-15) |
| 64 | +// and 8 ROWS (0-7). |
| 65 | +static void sendCharXY(unsigned char data, int X, int Y) |
| 66 | +{ |
| 67 | + setXY(X, Y); |
| 68 | + Wire.beginTransmission(OLED_address); // begin transmitting |
| 69 | + Wire.write(0x40);//data mode |
| 70 | + |
| 71 | + for(int i=0;i<8;i++) |
| 72 | + Wire.write(pgm_read_byte(myFont[data-0x20]+i)); |
| 73 | + |
| 74 | + Wire.endTransmission(); // stop transmitting |
| 75 | +} |
| 76 | + |
| 77 | +//==========================================================// |
| 78 | +// Used to send commands to the display. |
| 79 | +static void sendcommand(unsigned char com) |
| 80 | +{ |
| 81 | + Wire.beginTransmission(OLED_address); //begin transmitting |
| 82 | + Wire.write(0x80); //command mode |
| 83 | + Wire.write(com); |
| 84 | + Wire.endTransmission(); // stop transmitting |
| 85 | +} |
| 86 | + |
| 87 | +//==========================================================// |
| 88 | +// Set the cursor position in a 16 COL * 8 ROW map. |
| 89 | +static void setXY(unsigned char row,unsigned char col) |
| 90 | +{ |
| 91 | + sendcommand(0xb0+row); //set page address |
| 92 | + sendcommand(0x00+(8*col&0x0f)); //set low col address |
| 93 | + sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +//==========================================================// |
| 98 | +// Prints a string regardless the cursor position. |
| 99 | +static void sendStr(unsigned char *string) |
| 100 | +{ |
| 101 | + unsigned char i=0; |
| 102 | + while(*string) |
| 103 | + { |
| 104 | + for(i=0;i<8;i++) |
| 105 | + { |
| 106 | + SendChar(pgm_read_byte(myFont[*string-0x20]+i)); |
| 107 | + } |
| 108 | + *string++; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +//==========================================================// |
| 113 | +// Prints a string in coordinates X Y, being multiples of 8. |
| 114 | +// This means we have 16 COLS (0-15) and 8 ROWS (0-7). |
| 115 | +static void sendStrXY( char *string, int X, int Y) |
| 116 | +{ |
| 117 | + setXY(X,Y); |
| 118 | + unsigned char i=0; |
| 119 | + while(*string) |
| 120 | + { |
| 121 | + for(i=0;i<8;i++) |
| 122 | + { |
| 123 | + SendChar(pgm_read_byte(myFont[*string-0x20]+i)); |
| 124 | + } |
| 125 | + *string++; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +//==========================================================// |
| 131 | +// Inits oled and draws logo at startup |
| 132 | +static void init_OLED(void) |
| 133 | +{ |
| 134 | + sendcommand(0xae); //display off |
| 135 | + sendcommand(0xa6); //Set Normal Display (default) |
| 136 | + // Adafruit Init sequence for 128x64 OLED module |
| 137 | + sendcommand(0xAE); //DISPLAYOFF |
| 138 | + sendcommand(0xD5); //SETDISPLAYCLOCKDIV |
| 139 | + sendcommand(0x80); // the suggested ratio 0x80 |
| 140 | + sendcommand(0xA8); //SSD1306_SETMULTIPLEX |
| 141 | + sendcommand(0x3F); |
| 142 | + sendcommand(0xD3); //SETDISPLAYOFFSET |
| 143 | + sendcommand(0x0); //no offset |
| 144 | + sendcommand(0x40 | 0x0); //SETSTARTLINE |
| 145 | + sendcommand(0x8D); //CHARGEPUMP |
| 146 | + sendcommand(0x14); |
| 147 | + sendcommand(0x20); //MEMORYMODE |
| 148 | + sendcommand(0x00); //0x0 act like ks0108 |
| 149 | + |
| 150 | + //sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg |
| 151 | + sendcommand(0xA0); |
| 152 | + |
| 153 | + //sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg |
| 154 | + sendcommand(0xC0); |
| 155 | + |
| 156 | + sendcommand(0xDA); //0xDA |
| 157 | + sendcommand(0x12); //COMSCANDEC |
| 158 | + sendcommand(0x81); //SETCONTRAS |
| 159 | + sendcommand(0xCF); // |
| 160 | + sendcommand(0xd9); //SETPRECHARGE |
| 161 | + sendcommand(0xF1); |
| 162 | + sendcommand(0xDB); //SETVCOMDETECT |
| 163 | + sendcommand(0x40); |
| 164 | + sendcommand(0xA4); //DISPLAYALLON_RESUME |
| 165 | + sendcommand(0xA6); //NORMALDISPLAY |
| 166 | + |
| 167 | + clear_display(); |
| 168 | + sendcommand(0x2e); // stop scroll |
| 169 | + //----------------------------REVERSE comments----------------------------// |
| 170 | + sendcommand(0xa0); //seg re-map 0->127(default) |
| 171 | + sendcommand(0xa1); //seg re-map 127->0 |
| 172 | + sendcommand(0xc8); |
| 173 | + delay(1000); |
| 174 | + //----------------------------REVERSE comments----------------------------// |
| 175 | + // sendcommand(0xa7); //Set Inverse Display |
| 176 | + // sendcommand(0xae); //display off |
| 177 | + sendcommand(0x20); //Set Memory Addressing Mode |
| 178 | + sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode |
| 179 | + // sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET) |
| 180 | + |
| 181 | + setXY(0,0); |
| 182 | + |
| 183 | + // for(int i=0;i<128*8;i++) // show 128* 64 Logo |
| 184 | + // { |
| 185 | + // SendChar(pgm_read_byte(logo+i)); |
| 186 | + // } |
| 187 | + sendcommand(0xaf); //display on |
| 188 | + |
| 189 | + //sendStrXY("Miker",7,5); |
| 190 | + delay(5000); |
| 191 | +} |
0 commit comments