Driver and Graphics library for MicroOLED 0.66'' (64 x 48 pixel) display with SSD1306 controller. Display can be obtained from Sparkfun or from diverse Chinese sellers (similar displays) via Aliexpress. Display is driven via SPI. The library can be very easily adapted to other OLED displays (up to 128 x 64 pixel) with SSD1306 controller by setting defines and changing controller init commands accordingly.

Dependents:   MicroOLED_Test

Small test-program that briefly shows the basic usage of the library:

#include "mbed.h"
#include "SFE_MicroOLED.h"
 
DigitalOut myled(LED1);
SPI my_spi(p5, p6, p7);
MicroOLED my_oled(my_spi, p11, p10, p9);
 
int main() {
    my_oled.init(0, 8000000);
    my_oled.clear(ALL);
    my_oled.puts("Hello all!");
    my_oled.circle(16, 31, 16);
    my_oled.line(0, 9, 63, 47);
    my_oled.rectFill(33, 32, 8, 8);
    my_oled.display();
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}
Committer:
synvox
Date:
Thu Mar 19 03:37:35 2015 +0000
Revision:
0:b7fc78d2b795
Library for MicroOLED 0.66'' (64 x 48 pixel) display with SSD1306 controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
synvox 0:b7fc78d2b795 1 /******************************************************************************
synvox 0:b7fc78d2b795 2 SFE_MicroOLED.cpp
synvox 0:b7fc78d2b795 3 Main source code for the MicroOLED mbed Library
synvox 0:b7fc78d2b795 4
synvox 0:b7fc78d2b795 5 Jim Lindblom @ SparkFun Electronics
synvox 0:b7fc78d2b795 6 October 26, 2014
synvox 0:b7fc78d2b795 7 https://github.com/sparkfun/Micro_OLED_Breakout/tree/master/Firmware/Arduino/libraries/SFE_MicroOLED
synvox 0:b7fc78d2b795 8
synvox 0:b7fc78d2b795 9 Adapted for mbed by Nenad Milosevic
synvox 0:b7fc78d2b795 10 March, 2015
synvox 0:b7fc78d2b795 11
synvox 0:b7fc78d2b795 12 This file defines the hardware SPI interface for the Micro OLED Breakout.
synvox 0:b7fc78d2b795 13
synvox 0:b7fc78d2b795 14 Development environment specifics:
synvox 0:b7fc78d2b795 15 Various suitable mbed platforms
synvox 0:b7fc78d2b795 16 Micro OLED Breakout v1.0
synvox 0:b7fc78d2b795 17
synvox 0:b7fc78d2b795 18 This code was heavily based around the MicroView library, written by GeekAmmo
synvox 0:b7fc78d2b795 19 (https://github.com/geekammo/MicroView-Arduino-Library), and released under
synvox 0:b7fc78d2b795 20 the terms of the GNU General Public License as published by the Free Software
synvox 0:b7fc78d2b795 21 Foundation, either version 3 of the License, or (at your option) any later
synvox 0:b7fc78d2b795 22 version.
synvox 0:b7fc78d2b795 23
synvox 0:b7fc78d2b795 24 This program is distributed in the hope that it will be useful,
synvox 0:b7fc78d2b795 25 but WITHOUT ANY WARRANTY; without even the implied warranty of
synvox 0:b7fc78d2b795 26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
synvox 0:b7fc78d2b795 27 GNU General Public License for more details.
synvox 0:b7fc78d2b795 28
synvox 0:b7fc78d2b795 29 You should have received a copy of the GNU General Public License
synvox 0:b7fc78d2b795 30 along with this program. If not, see <http://www.gnu.org/licenses/>.
synvox 0:b7fc78d2b795 31 ******************************************************************************/
synvox 0:b7fc78d2b795 32
synvox 0:b7fc78d2b795 33 #include "mbed.h"
synvox 0:b7fc78d2b795 34 #include <stdarg.h>
synvox 0:b7fc78d2b795 35 #include "SFE_MicroOLED.h"
synvox 0:b7fc78d2b795 36
synvox 0:b7fc78d2b795 37 // Add header of the fonts here.
synvox 0:b7fc78d2b795 38 #include "font5x7.h"
synvox 0:b7fc78d2b795 39 #include "font8x16.h"
synvox 0:b7fc78d2b795 40 #include "fontlargenumber.h"
synvox 0:b7fc78d2b795 41 #include "7segment.h"
synvox 0:b7fc78d2b795 42
synvox 0:b7fc78d2b795 43 // Change the total fonts included
synvox 0:b7fc78d2b795 44 #define TOTALFONTS 4
synvox 0:b7fc78d2b795 45
synvox 0:b7fc78d2b795 46 // Add the font name as declared in the header file.
synvox 0:b7fc78d2b795 47 unsigned const char *MicroOLED::fontsPointer[]={
synvox 0:b7fc78d2b795 48 font5x7
synvox 0:b7fc78d2b795 49 ,font8x16
synvox 0:b7fc78d2b795 50 ,sevensegment
synvox 0:b7fc78d2b795 51 ,fontlargenumber
synvox 0:b7fc78d2b795 52 };
synvox 0:b7fc78d2b795 53
synvox 0:b7fc78d2b795 54 /** \brief MicroOLED screen buffer.
synvox 0:b7fc78d2b795 55
synvox 0:b7fc78d2b795 56 Page buffer LCDWIDTH x LCDHEIGHT divided by 8
synvox 0:b7fc78d2b795 57 Page buffer is required because in SPI mode, the host cannot read the SSD1306's GDRAM of the controller. This page buffer serves as a scratch RAM for graphical functions. All drawing function will first be drawn on this page buffer, only upon calling display() function will transfer the page buffer to the actual LCD controller's memory.
synvox 0:b7fc78d2b795 58 */
synvox 0:b7fc78d2b795 59 static uint8_t screenmemory [LCDWIDTH * LCDHEIGHT / 8];
synvox 0:b7fc78d2b795 60 /* SSD1306 Memory organised in 128 horizontal pixel and 8 rows of byte
synvox 0:b7fc78d2b795 61 B B .............B -----
synvox 0:b7fc78d2b795 62 y y .............y \
synvox 0:b7fc78d2b795 63 t t .............t \
synvox 0:b7fc78d2b795 64 e e .............e \
synvox 0:b7fc78d2b795 65 0 1 .............127 \
synvox 0:b7fc78d2b795 66 \
synvox 0:b7fc78d2b795 67 D0 D0.............D0 \
synvox 0:b7fc78d2b795 68 D1 D1.............D1 / ROW 0
synvox 0:b7fc78d2b795 69 D2 D2.............D2 /
synvox 0:b7fc78d2b795 70 D3 D3.............D3 /
synvox 0:b7fc78d2b795 71 D4 D4.............D4 /
synvox 0:b7fc78d2b795 72 D5 D5.............D5 /
synvox 0:b7fc78d2b795 73 D6 D6.............D6 /
synvox 0:b7fc78d2b795 74 D7 D7.............D7 ----
synvox 0:b7fc78d2b795 75 */
synvox 0:b7fc78d2b795 76
synvox 0:b7fc78d2b795 77 /** \brief Initialisation of MicroOLED Library.
synvox 0:b7fc78d2b795 78
synvox 0:b7fc78d2b795 79 Setup IO pins and parameters for SPI then send initialisation commands to the SSD1306 controller inside the OLED.
synvox 0:b7fc78d2b795 80 */
synvox 0:b7fc78d2b795 81 void MicroOLED::init(int spi_mode, int spi_freq)
synvox 0:b7fc78d2b795 82 {
synvox 0:b7fc78d2b795 83 // default 5x7 font
synvox 0:b7fc78d2b795 84 setFontType(0);
synvox 0:b7fc78d2b795 85 setColor(WHITE);
synvox 0:b7fc78d2b795 86 setDrawMode(NORM);
synvox 0:b7fc78d2b795 87 setCursor(0,0);
synvox 0:b7fc78d2b795 88
synvox 0:b7fc78d2b795 89 memset(screenmemory,0,(LCDWIDTH * LCDHEIGHT / 8)); // initially clear Page buffer
synvox 0:b7fc78d2b795 90
synvox 0:b7fc78d2b795 91 // Initialize the SPI library:
synvox 0:b7fc78d2b795 92 dcPin = 0;
synvox 0:b7fc78d2b795 93 csPin = 1;
synvox 0:b7fc78d2b795 94 miol_spi.format(8, spi_mode); // 8 Bit wide SPI and Mode (0 - 3)
synvox 0:b7fc78d2b795 95 miol_spi.frequency(spi_freq); // SPI speed in Hz
synvox 0:b7fc78d2b795 96
synvox 0:b7fc78d2b795 97 // Display reset routine
synvox 0:b7fc78d2b795 98 rstPin = 1; // Initially set RST HIGH
synvox 0:b7fc78d2b795 99 wait_ms(5); // VDD (3.3V) goes high at start, lets just chill for 5 ms
synvox 0:b7fc78d2b795 100 rstPin = 0; // Bring RST low, reset the display
synvox 0:b7fc78d2b795 101 wait_ms(10); // wait 10ms
synvox 0:b7fc78d2b795 102 rstPin = 1; // Set RST HIGH, bring out of reset
synvox 0:b7fc78d2b795 103 wait_ms(5); // wait 5ms
synvox 0:b7fc78d2b795 104
synvox 0:b7fc78d2b795 105 // Display Init sequence for 64x48 OLED module
synvox 0:b7fc78d2b795 106 command(DISPLAYOFF); // 0xAE
synvox 0:b7fc78d2b795 107
synvox 0:b7fc78d2b795 108 command(SETDISPLAYCLOCKDIV, 0x80); // 0xD5, the suggested ratio 0x80
synvox 0:b7fc78d2b795 109
synvox 0:b7fc78d2b795 110 command(SETMULTIPLEX, 0x2F); // 0xA8, 47(0x2F)
synvox 0:b7fc78d2b795 111
synvox 0:b7fc78d2b795 112 command(SETDISPLAYOFFSET, 0x0); // 0xD3, no offset
synvox 0:b7fc78d2b795 113
synvox 0:b7fc78d2b795 114 command(SETSTARTLINE | 0x0); // line #0
synvox 0:b7fc78d2b795 115
synvox 0:b7fc78d2b795 116 command(CHARGEPUMP, 0x14); // enable charge pump
synvox 0:b7fc78d2b795 117
synvox 0:b7fc78d2b795 118 command(NORMALDISPLAY); // 0xA6
synvox 0:b7fc78d2b795 119 command(DISPLAYALLONRESUME); // 0xA4
synvox 0:b7fc78d2b795 120
synvox 0:b7fc78d2b795 121 command(SEGREMAP | 0x1);
synvox 0:b7fc78d2b795 122 command(COMSCANDEC);
synvox 0:b7fc78d2b795 123
synvox 0:b7fc78d2b795 124 command(SETCOMPINS, 0x12); // 0xDA, 0x12 if height > 32 else 0x02
synvox 0:b7fc78d2b795 125
synvox 0:b7fc78d2b795 126 command(SETCONTRAST, 0x8F); // 0x81, 0x8F
synvox 0:b7fc78d2b795 127
synvox 0:b7fc78d2b795 128 command(SETPRECHARGE, 0xF1); // 0xd9, 0xF1
synvox 0:b7fc78d2b795 129
synvox 0:b7fc78d2b795 130 command(SETVCOMDESELECT, 0x40); // 0xDB
synvox 0:b7fc78d2b795 131
synvox 0:b7fc78d2b795 132 command(DISPLAYON); //--turn on oled panel
synvox 0:b7fc78d2b795 133 clear(ALL); // Erase hardware memory inside the OLED controller to avoid random data in memory.
synvox 0:b7fc78d2b795 134 }
synvox 0:b7fc78d2b795 135
synvox 0:b7fc78d2b795 136 /** \brief Send the display command byte(s)
synvox 0:b7fc78d2b795 137
synvox 0:b7fc78d2b795 138 Send command(s) via SPI to SSD1306 controller.
synvox 0:b7fc78d2b795 139 */
synvox 0:b7fc78d2b795 140 void MicroOLED::command(uint8_t c) {
synvox 0:b7fc78d2b795 141
synvox 0:b7fc78d2b795 142 dcPin = 0; // DC pin LOW for a command
synvox 0:b7fc78d2b795 143 csPin = 0; // SS LOW to initialize transfer
synvox 0:b7fc78d2b795 144 miol_spi.write(c); // Transfer the command byte
synvox 0:b7fc78d2b795 145 csPin = 1; // SS HIGH to end transfer
synvox 0:b7fc78d2b795 146
synvox 0:b7fc78d2b795 147 }
synvox 0:b7fc78d2b795 148
synvox 0:b7fc78d2b795 149 void MicroOLED::command(uint8_t c1, uint8_t c2) {
synvox 0:b7fc78d2b795 150
synvox 0:b7fc78d2b795 151 dcPin = 0; // DC pin LOW for a command
synvox 0:b7fc78d2b795 152 csPin = 0; // SS LOW to initialize transfer
synvox 0:b7fc78d2b795 153 miol_spi.write(c1); // Transfer the command byte
synvox 0:b7fc78d2b795 154 miol_spi.write(c2); // Transfer the first parameter
synvox 0:b7fc78d2b795 155 csPin = 1; // SS HIGH to end transfer
synvox 0:b7fc78d2b795 156
synvox 0:b7fc78d2b795 157 }
synvox 0:b7fc78d2b795 158
synvox 0:b7fc78d2b795 159 void MicroOLED::command(uint8_t c1, uint8_t c2, uint8_t c3) {
synvox 0:b7fc78d2b795 160
synvox 0:b7fc78d2b795 161 dcPin = 0; // DC pin LOW for a command
synvox 0:b7fc78d2b795 162 csPin = 0; // SS LOW to initialize transfer
synvox 0:b7fc78d2b795 163 miol_spi.write(c1); // Transfer the command byte
synvox 0:b7fc78d2b795 164 miol_spi.write(c2); // Transfer the first parameter
synvox 0:b7fc78d2b795 165 miol_spi.write(c3); // Transfer the second parameter
synvox 0:b7fc78d2b795 166 csPin = 1; // SS HIGH to end transfer
synvox 0:b7fc78d2b795 167
synvox 0:b7fc78d2b795 168 }
synvox 0:b7fc78d2b795 169
synvox 0:b7fc78d2b795 170 void MicroOLED::command(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4, uint8_t c5, uint8_t c6, uint8_t c7, uint8_t c8) {
synvox 0:b7fc78d2b795 171
synvox 0:b7fc78d2b795 172 dcPin = 0; // DC pin LOW for a command
synvox 0:b7fc78d2b795 173 csPin = 0; // SS LOW to initialize transfer
synvox 0:b7fc78d2b795 174 miol_spi.write(c1); // Transfer the command byte
synvox 0:b7fc78d2b795 175 miol_spi.write(c2); // Transfer the first parameter
synvox 0:b7fc78d2b795 176 miol_spi.write(c3); // Transfer the second parameter
synvox 0:b7fc78d2b795 177 miol_spi.write(c4); // Transfer the third parameter
synvox 0:b7fc78d2b795 178 miol_spi.write(c5); // Transfer the fourth parameter
synvox 0:b7fc78d2b795 179 miol_spi.write(c6); // Transfer the fifth parameter
synvox 0:b7fc78d2b795 180 miol_spi.write(c7); // Transfer the sixth parameter
synvox 0:b7fc78d2b795 181 miol_spi.write(c8); // Transfer the seventh parameter
synvox 0:b7fc78d2b795 182 csPin = 1; // SS HIGH to end transfer
synvox 0:b7fc78d2b795 183
synvox 0:b7fc78d2b795 184 }
synvox 0:b7fc78d2b795 185
synvox 0:b7fc78d2b795 186 /** \brief Clear screen buffer or SSD1306's memory.
synvox 0:b7fc78d2b795 187
synvox 0:b7fc78d2b795 188 To clear all GDRAM inside the LCD controller, pass in the variable mode = ALL and to clear screen page buffer pass in the variable mode = PAGE.
synvox 0:b7fc78d2b795 189 */
synvox 0:b7fc78d2b795 190 void MicroOLED::clear(uint8_t mode) {
synvox 0:b7fc78d2b795 191 if (mode==ALL) {
synvox 0:b7fc78d2b795 192 command(MEMORYMODE, 0, SETCOLUMNBOUNDS, 0, LCDTOTALWIDTH - 1, SETPAGEBOUNDS, 0, (LCDTOTALHEIGHT / 8) - 1); // Set horizontal addressing mode, width and height
synvox 0:b7fc78d2b795 193 dcPin = 1;
synvox 0:b7fc78d2b795 194 csPin = 0;
synvox 0:b7fc78d2b795 195 for (int i = 0; i < (LCDTOTALWIDTH * LCDTOTALHEIGHT / 8); i++) {
synvox 0:b7fc78d2b795 196 miol_spi.write(0);
synvox 0:b7fc78d2b795 197 }
synvox 0:b7fc78d2b795 198 csPin = 1;
synvox 0:b7fc78d2b795 199 command(MEMORYMODE, 2); // Restore to page addressing mode
synvox 0:b7fc78d2b795 200 }
synvox 0:b7fc78d2b795 201 else
synvox 0:b7fc78d2b795 202 {
synvox 0:b7fc78d2b795 203 memset(screenmemory,0,(LCDWIDTH * LCDHEIGHT / 8));
synvox 0:b7fc78d2b795 204 //display();
synvox 0:b7fc78d2b795 205 }
synvox 0:b7fc78d2b795 206 }
synvox 0:b7fc78d2b795 207
synvox 0:b7fc78d2b795 208 /** \brief Clear or replace screen buffer or SSD1306's memory with a character.
synvox 0:b7fc78d2b795 209
synvox 0:b7fc78d2b795 210 To clear GDRAM inside the LCD controller, pass in the variable mode = ALL with c character and to clear screen page buffer, pass in the variable mode = PAGE with c character.
synvox 0:b7fc78d2b795 211 */
synvox 0:b7fc78d2b795 212 void MicroOLED::clear(uint8_t mode, uint8_t c) {
synvox 0:b7fc78d2b795 213 if (mode==ALL) {
synvox 0:b7fc78d2b795 214 command(MEMORYMODE, 0, SETCOLUMNBOUNDS, 0, LCDTOTALWIDTH - 1, SETPAGEBOUNDS, 0, (LCDTOTALHEIGHT / 8) - 1); // Set horizontal addressing mode, width and height
synvox 0:b7fc78d2b795 215 dcPin = 1;
synvox 0:b7fc78d2b795 216 csPin = 0;
synvox 0:b7fc78d2b795 217 for (int i = 0; i < (LCDTOTALWIDTH * LCDTOTALHEIGHT / 8); i++) {
synvox 0:b7fc78d2b795 218 miol_spi.write(c);
synvox 0:b7fc78d2b795 219 }
synvox 0:b7fc78d2b795 220 csPin = 1;
synvox 0:b7fc78d2b795 221 command(MEMORYMODE, 2); // Restore to page addressing mode
synvox 0:b7fc78d2b795 222 }
synvox 0:b7fc78d2b795 223 else
synvox 0:b7fc78d2b795 224 {
synvox 0:b7fc78d2b795 225 memset(screenmemory,c,(LCDWIDTH * LCDHEIGHT / 8));
synvox 0:b7fc78d2b795 226 display();
synvox 0:b7fc78d2b795 227 }
synvox 0:b7fc78d2b795 228 }
synvox 0:b7fc78d2b795 229
synvox 0:b7fc78d2b795 230 /** \brief Invert display.
synvox 0:b7fc78d2b795 231
synvox 0:b7fc78d2b795 232 The WHITE color of the display will turn to BLACK and the BLACK will turn to WHITE.
synvox 0:b7fc78d2b795 233 */
synvox 0:b7fc78d2b795 234 void MicroOLED::invert(boolean inv) {
synvox 0:b7fc78d2b795 235 if (inv)
synvox 0:b7fc78d2b795 236 command(INVERTDISPLAY);
synvox 0:b7fc78d2b795 237 else
synvox 0:b7fc78d2b795 238 command(NORMALDISPLAY);
synvox 0:b7fc78d2b795 239 }
synvox 0:b7fc78d2b795 240
synvox 0:b7fc78d2b795 241 /** \brief Set contrast.
synvox 0:b7fc78d2b795 242
synvox 0:b7fc78d2b795 243 OLED contract value from 0 to 255. Note: Contrast level is not very obvious.
synvox 0:b7fc78d2b795 244 */
synvox 0:b7fc78d2b795 245 void MicroOLED::contrast(uint8_t contrast) {
synvox 0:b7fc78d2b795 246 command(SETCONTRAST, contrast); // 0x81
synvox 0:b7fc78d2b795 247 }
synvox 0:b7fc78d2b795 248
synvox 0:b7fc78d2b795 249 /** \brief Transfer display memory.
synvox 0:b7fc78d2b795 250
synvox 0:b7fc78d2b795 251 Bulk move the screen buffer to the SSD1306 controller's memory so that images/graphics drawn on the screen buffer will be displayed on the OLED.
synvox 0:b7fc78d2b795 252 */
synvox 0:b7fc78d2b795 253 void MicroOLED::display(void) {
synvox 0:b7fc78d2b795 254 command(MEMORYMODE, 0, SETCOLUMNBOUNDS, LCDCOLUMNOFFSET, LCDCOLUMNOFFSET + LCDWIDTH - 1, SETPAGEBOUNDS, 0, (LCDHEIGHT / 8) - 1); // Set horizontal addressing mode, width and height
synvox 0:b7fc78d2b795 255 dcPin = 1;
synvox 0:b7fc78d2b795 256 csPin = 0;
synvox 0:b7fc78d2b795 257 for (int i = 0; i < (LCDWIDTH * LCDHEIGHT / 8); i++) {
synvox 0:b7fc78d2b795 258 miol_spi.write(screenmemory[i]);
synvox 0:b7fc78d2b795 259 }
synvox 0:b7fc78d2b795 260 csPin = 1;
synvox 0:b7fc78d2b795 261 command(MEMORYMODE, 2); // Restore to page addressing mode
synvox 0:b7fc78d2b795 262 }
synvox 0:b7fc78d2b795 263
synvox 0:b7fc78d2b795 264 /*
synvox 0:b7fc78d2b795 265 Classic text print functions.
synvox 0:b7fc78d2b795 266 */
synvox 0:b7fc78d2b795 267
synvox 0:b7fc78d2b795 268 void MicroOLED::putc(char c) {
synvox 0:b7fc78d2b795 269 if (c == '\n') {
synvox 0:b7fc78d2b795 270 cursorY += fontHeight;
synvox 0:b7fc78d2b795 271 cursorX = 0;
synvox 0:b7fc78d2b795 272 } else if (c == '\r') {
synvox 0:b7fc78d2b795 273 // skip
synvox 0:b7fc78d2b795 274 } else {
synvox 0:b7fc78d2b795 275 drawChar(cursorX, cursorY, (uint8_t)c, foreColor, drawMode);
synvox 0:b7fc78d2b795 276 cursorX += fontWidth+1;
synvox 0:b7fc78d2b795 277 if ((cursorX > (LCDWIDTH - fontWidth))) {
synvox 0:b7fc78d2b795 278 cursorY += fontHeight;
synvox 0:b7fc78d2b795 279 cursorX = 0;
synvox 0:b7fc78d2b795 280 }
synvox 0:b7fc78d2b795 281 }
synvox 0:b7fc78d2b795 282 }
synvox 0:b7fc78d2b795 283
synvox 0:b7fc78d2b795 284 void MicroOLED::puts(const char *cstring) {
synvox 0:b7fc78d2b795 285 while (*cstring != 0) {
synvox 0:b7fc78d2b795 286 putc(*cstring++);
synvox 0:b7fc78d2b795 287 }
synvox 0:b7fc78d2b795 288 }
synvox 0:b7fc78d2b795 289
synvox 0:b7fc78d2b795 290 void MicroOLED::printf(const char *format, ...)
synvox 0:b7fc78d2b795 291 {
synvox 0:b7fc78d2b795 292 static char buffer[128];
synvox 0:b7fc78d2b795 293
synvox 0:b7fc78d2b795 294 va_list args;
synvox 0:b7fc78d2b795 295 va_start(args, format);
synvox 0:b7fc78d2b795 296 vsprintf(buffer, format, args);
synvox 0:b7fc78d2b795 297 va_end(args);
synvox 0:b7fc78d2b795 298
synvox 0:b7fc78d2b795 299 char *c = (char *)&buffer;
synvox 0:b7fc78d2b795 300 while (*c != 0)
synvox 0:b7fc78d2b795 301 {
synvox 0:b7fc78d2b795 302 putc(*c++);
synvox 0:b7fc78d2b795 303 }
synvox 0:b7fc78d2b795 304 }
synvox 0:b7fc78d2b795 305
synvox 0:b7fc78d2b795 306 /** \brief Set cursor position.
synvox 0:b7fc78d2b795 307
synvox 0:b7fc78d2b795 308 MicroOLED's cursor position to x,y.
synvox 0:b7fc78d2b795 309 */
synvox 0:b7fc78d2b795 310 void MicroOLED::setCursor(uint8_t x, uint8_t y) {
synvox 0:b7fc78d2b795 311 cursorX=x;
synvox 0:b7fc78d2b795 312 cursorY=y;
synvox 0:b7fc78d2b795 313 }
synvox 0:b7fc78d2b795 314
synvox 0:b7fc78d2b795 315 /** \brief Draw pixel.
synvox 0:b7fc78d2b795 316
synvox 0:b7fc78d2b795 317 Draw pixel using the current fore color and current draw mode in the screen buffer's x,y position.
synvox 0:b7fc78d2b795 318 */
synvox 0:b7fc78d2b795 319 void MicroOLED::pixel(uint8_t x, uint8_t y) {
synvox 0:b7fc78d2b795 320 pixel(x,y,foreColor,drawMode);
synvox 0:b7fc78d2b795 321 }
synvox 0:b7fc78d2b795 322
synvox 0:b7fc78d2b795 323 /** \brief Draw pixel with color and mode.
synvox 0:b7fc78d2b795 324
synvox 0:b7fc78d2b795 325 Draw color pixel in the screen buffer's x,y position with NORM or XOR draw mode.
synvox 0:b7fc78d2b795 326 */
synvox 0:b7fc78d2b795 327 void MicroOLED::pixel(uint8_t x, uint8_t y, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 328 if ((x>=LCDWIDTH) || (y>=LCDHEIGHT))
synvox 0:b7fc78d2b795 329 return;
synvox 0:b7fc78d2b795 330
synvox 0:b7fc78d2b795 331 if (mode==XOR) {
synvox 0:b7fc78d2b795 332 if (color==WHITE)
synvox 0:b7fc78d2b795 333 screenmemory[x+ (y/8)*LCDWIDTH] ^= _BV((y%8));
synvox 0:b7fc78d2b795 334 }
synvox 0:b7fc78d2b795 335 else {
synvox 0:b7fc78d2b795 336 if (color==WHITE)
synvox 0:b7fc78d2b795 337 screenmemory[x+ (y/8)*LCDWIDTH] |= _BV((y%8));
synvox 0:b7fc78d2b795 338 else
synvox 0:b7fc78d2b795 339 screenmemory[x+ (y/8)*LCDWIDTH] &= ~_BV((y%8));
synvox 0:b7fc78d2b795 340 }
synvox 0:b7fc78d2b795 341 }
synvox 0:b7fc78d2b795 342
synvox 0:b7fc78d2b795 343 /** \brief Draw line.
synvox 0:b7fc78d2b795 344
synvox 0:b7fc78d2b795 345 Draw line using current fore color and current draw mode from x0,y0 to x1,y1 of the screen buffer.
synvox 0:b7fc78d2b795 346 */
synvox 0:b7fc78d2b795 347 void MicroOLED::line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
synvox 0:b7fc78d2b795 348 line(x0,y0,x1,y1,foreColor,drawMode);
synvox 0:b7fc78d2b795 349 }
synvox 0:b7fc78d2b795 350
synvox 0:b7fc78d2b795 351 /** \brief Draw line with color and mode.
synvox 0:b7fc78d2b795 352
synvox 0:b7fc78d2b795 353 Draw line using color and mode from x0,y0 to x1,y1 of the screen buffer.
synvox 0:b7fc78d2b795 354 */
synvox 0:b7fc78d2b795 355 void MicroOLED::line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 356 uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
synvox 0:b7fc78d2b795 357 if (steep) {
synvox 0:b7fc78d2b795 358 swap(x0, y0);
synvox 0:b7fc78d2b795 359 swap(x1, y1);
synvox 0:b7fc78d2b795 360 }
synvox 0:b7fc78d2b795 361
synvox 0:b7fc78d2b795 362 if (x0 > x1) {
synvox 0:b7fc78d2b795 363 swap(x0, x1);
synvox 0:b7fc78d2b795 364 swap(y0, y1);
synvox 0:b7fc78d2b795 365 }
synvox 0:b7fc78d2b795 366
synvox 0:b7fc78d2b795 367 uint8_t dx, dy;
synvox 0:b7fc78d2b795 368 dx = x1 - x0;
synvox 0:b7fc78d2b795 369 dy = abs(y1 - y0);
synvox 0:b7fc78d2b795 370
synvox 0:b7fc78d2b795 371 int8_t err = dx / 2;
synvox 0:b7fc78d2b795 372 int8_t ystep;
synvox 0:b7fc78d2b795 373
synvox 0:b7fc78d2b795 374 if (y0 < y1) {
synvox 0:b7fc78d2b795 375 ystep = 1;
synvox 0:b7fc78d2b795 376 } else {
synvox 0:b7fc78d2b795 377 ystep = -1;}
synvox 0:b7fc78d2b795 378
synvox 0:b7fc78d2b795 379 for (; x0<x1; x0++) {
synvox 0:b7fc78d2b795 380 if (steep) {
synvox 0:b7fc78d2b795 381 pixel(y0, x0, color, mode);
synvox 0:b7fc78d2b795 382 } else {
synvox 0:b7fc78d2b795 383 pixel(x0, y0, color, mode);
synvox 0:b7fc78d2b795 384 }
synvox 0:b7fc78d2b795 385 err -= dy;
synvox 0:b7fc78d2b795 386 if (err < 0) {
synvox 0:b7fc78d2b795 387 y0 += ystep;
synvox 0:b7fc78d2b795 388 err += dx;
synvox 0:b7fc78d2b795 389 }
synvox 0:b7fc78d2b795 390 }
synvox 0:b7fc78d2b795 391 }
synvox 0:b7fc78d2b795 392
synvox 0:b7fc78d2b795 393 /** \brief Draw horizontal line.
synvox 0:b7fc78d2b795 394
synvox 0:b7fc78d2b795 395 Draw horizontal line using current fore color and current draw mode from x,y to x+width,y of the screen buffer.
synvox 0:b7fc78d2b795 396 */
synvox 0:b7fc78d2b795 397 void MicroOLED::lineH(uint8_t x, uint8_t y, uint8_t width) {
synvox 0:b7fc78d2b795 398 line(x,y,x+width,y,foreColor,drawMode);
synvox 0:b7fc78d2b795 399 }
synvox 0:b7fc78d2b795 400
synvox 0:b7fc78d2b795 401 /** \brief Draw horizontal line with color and mode.
synvox 0:b7fc78d2b795 402
synvox 0:b7fc78d2b795 403 Draw horizontal line using color and mode from x,y to x+width,y of the screen buffer.
synvox 0:b7fc78d2b795 404 */
synvox 0:b7fc78d2b795 405 void MicroOLED::lineH(uint8_t x, uint8_t y, uint8_t width, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 406 line(x,y,x+width,y,color,mode);
synvox 0:b7fc78d2b795 407 }
synvox 0:b7fc78d2b795 408
synvox 0:b7fc78d2b795 409 /** \brief Draw vertical line.
synvox 0:b7fc78d2b795 410
synvox 0:b7fc78d2b795 411 Draw vertical line using current fore color and current draw mode from x,y to x,y+height of the screen buffer.
synvox 0:b7fc78d2b795 412 */
synvox 0:b7fc78d2b795 413 void MicroOLED::lineV(uint8_t x, uint8_t y, uint8_t height) {
synvox 0:b7fc78d2b795 414 line(x,y,x,y+height,foreColor,drawMode);
synvox 0:b7fc78d2b795 415 }
synvox 0:b7fc78d2b795 416
synvox 0:b7fc78d2b795 417 /** \brief Draw vertical line with color and mode.
synvox 0:b7fc78d2b795 418
synvox 0:b7fc78d2b795 419 Draw vertical line using color and mode from x,y to x,y+height of the screen buffer.
synvox 0:b7fc78d2b795 420 */
synvox 0:b7fc78d2b795 421 void MicroOLED::lineV(uint8_t x, uint8_t y, uint8_t height, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 422 line(x,y,x,y+height,color,mode);
synvox 0:b7fc78d2b795 423 }
synvox 0:b7fc78d2b795 424
synvox 0:b7fc78d2b795 425 /** \brief Draw rectangle.
synvox 0:b7fc78d2b795 426
synvox 0:b7fc78d2b795 427 Draw rectangle using current fore color and current draw mode from x,y to x+width,y+height of the screen buffer.
synvox 0:b7fc78d2b795 428 */
synvox 0:b7fc78d2b795 429 void MicroOLED::rect(uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
synvox 0:b7fc78d2b795 430 rect(x,y,width,height,foreColor,drawMode);
synvox 0:b7fc78d2b795 431 }
synvox 0:b7fc78d2b795 432
synvox 0:b7fc78d2b795 433 /** \brief Draw rectangle with color and mode.
synvox 0:b7fc78d2b795 434
synvox 0:b7fc78d2b795 435 Draw rectangle using color and mode from x,y to x+width,y+height of the screen buffer.
synvox 0:b7fc78d2b795 436 */
synvox 0:b7fc78d2b795 437 void MicroOLED::rect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color , uint8_t mode) {
synvox 0:b7fc78d2b795 438 uint8_t tempHeight;
synvox 0:b7fc78d2b795 439
synvox 0:b7fc78d2b795 440 lineH(x,y, width, color, mode);
synvox 0:b7fc78d2b795 441 lineH(x,y+height-1, width, color, mode);
synvox 0:b7fc78d2b795 442
synvox 0:b7fc78d2b795 443 tempHeight=height-2;
synvox 0:b7fc78d2b795 444
synvox 0:b7fc78d2b795 445 // skip drawing vertical lines to avoid overlapping of pixel that will
synvox 0:b7fc78d2b795 446 // affect XOR plot if no pixel in between horizontal lines
synvox 0:b7fc78d2b795 447 if (tempHeight<1) return;
synvox 0:b7fc78d2b795 448
synvox 0:b7fc78d2b795 449 lineV(x,y+1, tempHeight, color, mode);
synvox 0:b7fc78d2b795 450 lineV(x+width-1, y+1, tempHeight, color, mode);
synvox 0:b7fc78d2b795 451 }
synvox 0:b7fc78d2b795 452
synvox 0:b7fc78d2b795 453 /** \brief Draw filled rectangle.
synvox 0:b7fc78d2b795 454
synvox 0:b7fc78d2b795 455 Draw filled rectangle using current fore color and current draw mode from x,y to x+width,y+height of the screen buffer.
synvox 0:b7fc78d2b795 456 */
synvox 0:b7fc78d2b795 457 void MicroOLED::rectFill(uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
synvox 0:b7fc78d2b795 458 rectFill(x,y,width,height,foreColor,drawMode);
synvox 0:b7fc78d2b795 459 }
synvox 0:b7fc78d2b795 460
synvox 0:b7fc78d2b795 461 /** \brief Draw filled rectangle with color and mode.
synvox 0:b7fc78d2b795 462
synvox 0:b7fc78d2b795 463 Draw filled rectangle using color and mode from x,y to x+width,y+height of the screen buffer.
synvox 0:b7fc78d2b795 464 */
synvox 0:b7fc78d2b795 465 void MicroOLED::rectFill(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color , uint8_t mode) {
synvox 0:b7fc78d2b795 466 // TODO - need to optimise the memory map draw so that this function will not call pixel one by one
synvox 0:b7fc78d2b795 467 for (int i=x; i<x+width;i++) {
synvox 0:b7fc78d2b795 468 lineV(i,y, height, color, mode);
synvox 0:b7fc78d2b795 469 }
synvox 0:b7fc78d2b795 470 }
synvox 0:b7fc78d2b795 471
synvox 0:b7fc78d2b795 472 /** \brief Draw circle.
synvox 0:b7fc78d2b795 473
synvox 0:b7fc78d2b795 474 Draw circle with radius using current fore color and current draw mode at x,y of the screen buffer.
synvox 0:b7fc78d2b795 475 */
synvox 0:b7fc78d2b795 476 void MicroOLED::circle(uint8_t x0, uint8_t y0, uint8_t radius) {
synvox 0:b7fc78d2b795 477 circle(x0,y0,radius,foreColor,drawMode);
synvox 0:b7fc78d2b795 478 }
synvox 0:b7fc78d2b795 479
synvox 0:b7fc78d2b795 480 /** \brief Draw circle with color and mode.
synvox 0:b7fc78d2b795 481
synvox 0:b7fc78d2b795 482 Draw circle with radius using color and mode at x,y of the screen buffer.
synvox 0:b7fc78d2b795 483 */
synvox 0:b7fc78d2b795 484 void MicroOLED::circle(uint8_t x0, uint8_t y0, uint8_t radius, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 485 //TODO - find a way to check for no overlapping of pixels so that XOR draw mode will work perfectly
synvox 0:b7fc78d2b795 486 int8_t f = 1 - radius;
synvox 0:b7fc78d2b795 487 int8_t ddF_x = 1;
synvox 0:b7fc78d2b795 488 int8_t ddF_y = -2 * radius;
synvox 0:b7fc78d2b795 489 int8_t x = 0;
synvox 0:b7fc78d2b795 490 int8_t y = radius;
synvox 0:b7fc78d2b795 491
synvox 0:b7fc78d2b795 492 pixel(x0, y0+radius, color, mode);
synvox 0:b7fc78d2b795 493 pixel(x0, y0-radius, color, mode);
synvox 0:b7fc78d2b795 494 pixel(x0+radius, y0, color, mode);
synvox 0:b7fc78d2b795 495 pixel(x0-radius, y0, color, mode);
synvox 0:b7fc78d2b795 496
synvox 0:b7fc78d2b795 497 while (x<y) {
synvox 0:b7fc78d2b795 498 if (f >= 0) {
synvox 0:b7fc78d2b795 499 y--;
synvox 0:b7fc78d2b795 500 ddF_y += 2;
synvox 0:b7fc78d2b795 501 f += ddF_y;
synvox 0:b7fc78d2b795 502 }
synvox 0:b7fc78d2b795 503 x++;
synvox 0:b7fc78d2b795 504 ddF_x += 2;
synvox 0:b7fc78d2b795 505 f += ddF_x;
synvox 0:b7fc78d2b795 506
synvox 0:b7fc78d2b795 507 pixel(x0 + x, y0 + y, color, mode);
synvox 0:b7fc78d2b795 508 pixel(x0 - x, y0 + y, color, mode);
synvox 0:b7fc78d2b795 509 pixel(x0 + x, y0 - y, color, mode);
synvox 0:b7fc78d2b795 510 pixel(x0 - x, y0 - y, color, mode);
synvox 0:b7fc78d2b795 511
synvox 0:b7fc78d2b795 512 pixel(x0 + y, y0 + x, color, mode);
synvox 0:b7fc78d2b795 513 pixel(x0 - y, y0 + x, color, mode);
synvox 0:b7fc78d2b795 514 pixel(x0 + y, y0 - x, color, mode);
synvox 0:b7fc78d2b795 515 pixel(x0 - y, y0 - x, color, mode);
synvox 0:b7fc78d2b795 516
synvox 0:b7fc78d2b795 517 }
synvox 0:b7fc78d2b795 518 }
synvox 0:b7fc78d2b795 519
synvox 0:b7fc78d2b795 520 /** \brief Draw filled circle.
synvox 0:b7fc78d2b795 521
synvox 0:b7fc78d2b795 522 Draw filled circle with radius using current fore color and current draw mode at x,y of the screen buffer.
synvox 0:b7fc78d2b795 523 */
synvox 0:b7fc78d2b795 524 void MicroOLED::circleFill(uint8_t x0, uint8_t y0, uint8_t radius) {
synvox 0:b7fc78d2b795 525 circleFill(x0,y0,radius,foreColor,drawMode);
synvox 0:b7fc78d2b795 526 }
synvox 0:b7fc78d2b795 527
synvox 0:b7fc78d2b795 528 /** \brief Draw filled circle with color and mode.
synvox 0:b7fc78d2b795 529
synvox 0:b7fc78d2b795 530 Draw filled circle with radius using color and mode at x,y of the screen buffer.
synvox 0:b7fc78d2b795 531 */
synvox 0:b7fc78d2b795 532 void MicroOLED::circleFill(uint8_t x0, uint8_t y0, uint8_t radius, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 533 // TODO - - find a way to check for no overlapping of pixels so that XOR draw mode will work perfectly
synvox 0:b7fc78d2b795 534 int8_t f = 1 - radius;
synvox 0:b7fc78d2b795 535 int8_t ddF_x = 1;
synvox 0:b7fc78d2b795 536 int8_t ddF_y = -2 * radius;
synvox 0:b7fc78d2b795 537 int8_t x = 0;
synvox 0:b7fc78d2b795 538 int8_t y = radius;
synvox 0:b7fc78d2b795 539
synvox 0:b7fc78d2b795 540 // Temporary disable fill circle for XOR mode.
synvox 0:b7fc78d2b795 541 if (mode==XOR) return;
synvox 0:b7fc78d2b795 542
synvox 0:b7fc78d2b795 543 for (uint8_t i=y0-radius; i<=y0+radius; i++) {
synvox 0:b7fc78d2b795 544 pixel(x0, i, color, mode);
synvox 0:b7fc78d2b795 545 }
synvox 0:b7fc78d2b795 546
synvox 0:b7fc78d2b795 547 while (x<y) {
synvox 0:b7fc78d2b795 548 if (f >= 0) {
synvox 0:b7fc78d2b795 549 y--;
synvox 0:b7fc78d2b795 550 ddF_y += 2;
synvox 0:b7fc78d2b795 551 f += ddF_y;
synvox 0:b7fc78d2b795 552 }
synvox 0:b7fc78d2b795 553 x++;
synvox 0:b7fc78d2b795 554 ddF_x += 2;
synvox 0:b7fc78d2b795 555 f += ddF_x;
synvox 0:b7fc78d2b795 556
synvox 0:b7fc78d2b795 557 for (uint8_t i=y0-y; i<=y0+y; i++) {
synvox 0:b7fc78d2b795 558 pixel(x0+x, i, color, mode);
synvox 0:b7fc78d2b795 559 pixel(x0-x, i, color, mode);
synvox 0:b7fc78d2b795 560 }
synvox 0:b7fc78d2b795 561 for (uint8_t i=y0-x; i<=y0+x; i++) {
synvox 0:b7fc78d2b795 562 pixel(x0+y, i, color, mode);
synvox 0:b7fc78d2b795 563 pixel(x0-y, i, color, mode);
synvox 0:b7fc78d2b795 564 }
synvox 0:b7fc78d2b795 565 }
synvox 0:b7fc78d2b795 566 }
synvox 0:b7fc78d2b795 567
synvox 0:b7fc78d2b795 568 /** \brief Get LCD height.
synvox 0:b7fc78d2b795 569
synvox 0:b7fc78d2b795 570 The height of the LCD return as byte.
synvox 0:b7fc78d2b795 571 */
synvox 0:b7fc78d2b795 572 uint8_t MicroOLED::getLCDHeight(void) {
synvox 0:b7fc78d2b795 573 return LCDHEIGHT;
synvox 0:b7fc78d2b795 574 }
synvox 0:b7fc78d2b795 575
synvox 0:b7fc78d2b795 576 /** \brief Get LCD width.
synvox 0:b7fc78d2b795 577
synvox 0:b7fc78d2b795 578 The width of the LCD return as byte.
synvox 0:b7fc78d2b795 579 */
synvox 0:b7fc78d2b795 580 uint8_t MicroOLED::getLCDWidth(void) {
synvox 0:b7fc78d2b795 581 return LCDWIDTH;
synvox 0:b7fc78d2b795 582 }
synvox 0:b7fc78d2b795 583
synvox 0:b7fc78d2b795 584 /** \brief Get font width.
synvox 0:b7fc78d2b795 585
synvox 0:b7fc78d2b795 586 The cucrrent font's width return as byte.
synvox 0:b7fc78d2b795 587 */
synvox 0:b7fc78d2b795 588 uint8_t MicroOLED::getFontWidth(void) {
synvox 0:b7fc78d2b795 589 return fontWidth;
synvox 0:b7fc78d2b795 590 }
synvox 0:b7fc78d2b795 591
synvox 0:b7fc78d2b795 592 /** \brief Get font height.
synvox 0:b7fc78d2b795 593
synvox 0:b7fc78d2b795 594 The current font's height return as byte.
synvox 0:b7fc78d2b795 595 */
synvox 0:b7fc78d2b795 596 uint8_t MicroOLED::getFontHeight(void) {
synvox 0:b7fc78d2b795 597 return fontHeight;
synvox 0:b7fc78d2b795 598 }
synvox 0:b7fc78d2b795 599
synvox 0:b7fc78d2b795 600 /** \brief Get font starting character.
synvox 0:b7fc78d2b795 601
synvox 0:b7fc78d2b795 602 Return the starting ASCII character of the currnet font, not all fonts start with ASCII character 0. Custom fonts can start from any ASCII character.
synvox 0:b7fc78d2b795 603 */
synvox 0:b7fc78d2b795 604 uint8_t MicroOLED::getFontStartChar(void) {
synvox 0:b7fc78d2b795 605 return fontStartChar;
synvox 0:b7fc78d2b795 606 }
synvox 0:b7fc78d2b795 607
synvox 0:b7fc78d2b795 608 /** \brief Get font total characters.
synvox 0:b7fc78d2b795 609
synvox 0:b7fc78d2b795 610 Return the total characters of the current font.
synvox 0:b7fc78d2b795 611 */
synvox 0:b7fc78d2b795 612 uint8_t MicroOLED::getFontTotalChar(void) {
synvox 0:b7fc78d2b795 613 return fontTotalChar;
synvox 0:b7fc78d2b795 614 }
synvox 0:b7fc78d2b795 615
synvox 0:b7fc78d2b795 616 /** \brief Get total fonts.
synvox 0:b7fc78d2b795 617
synvox 0:b7fc78d2b795 618 Return the total number of fonts loaded into the MicroOLED's flash memory.
synvox 0:b7fc78d2b795 619 */
synvox 0:b7fc78d2b795 620 uint8_t MicroOLED::getTotalFonts(void) {
synvox 0:b7fc78d2b795 621 return TOTALFONTS;
synvox 0:b7fc78d2b795 622 }
synvox 0:b7fc78d2b795 623
synvox 0:b7fc78d2b795 624 /** \brief Get font type.
synvox 0:b7fc78d2b795 625
synvox 0:b7fc78d2b795 626 Return the font type number of the current font.
synvox 0:b7fc78d2b795 627 */
synvox 0:b7fc78d2b795 628 uint8_t MicroOLED::getFontType(void) {
synvox 0:b7fc78d2b795 629 return fontType;
synvox 0:b7fc78d2b795 630 }
synvox 0:b7fc78d2b795 631
synvox 0:b7fc78d2b795 632 /** \brief Set font type.
synvox 0:b7fc78d2b795 633
synvox 0:b7fc78d2b795 634 Set the current font type number, ie changing to different fonts base on the type provided.
synvox 0:b7fc78d2b795 635 */
synvox 0:b7fc78d2b795 636 uint8_t MicroOLED::setFontType(uint8_t type) {
synvox 0:b7fc78d2b795 637 if (type>=TOTALFONTS)
synvox 0:b7fc78d2b795 638 return false;
synvox 0:b7fc78d2b795 639
synvox 0:b7fc78d2b795 640 fontType = type;
synvox 0:b7fc78d2b795 641 fontWidth = *(fontsPointer[fontType]+0);
synvox 0:b7fc78d2b795 642 fontHeight = *(fontsPointer[fontType]+1);
synvox 0:b7fc78d2b795 643 fontStartChar = *(fontsPointer[fontType]+2);
synvox 0:b7fc78d2b795 644 fontTotalChar = *(fontsPointer[fontType]+3);
synvox 0:b7fc78d2b795 645 fontMapWidth = (*(fontsPointer[fontType]+4) * 100) + *(fontsPointer[fontType]+5); // two bytes values into integer 16
synvox 0:b7fc78d2b795 646 return true;
synvox 0:b7fc78d2b795 647 }
synvox 0:b7fc78d2b795 648
synvox 0:b7fc78d2b795 649 /** \brief Set color.
synvox 0:b7fc78d2b795 650
synvox 0:b7fc78d2b795 651 Set the current draw's color. Only WHITE and BLACK available.
synvox 0:b7fc78d2b795 652 */
synvox 0:b7fc78d2b795 653 void MicroOLED::setColor(uint8_t color) {
synvox 0:b7fc78d2b795 654 foreColor=color;
synvox 0:b7fc78d2b795 655 }
synvox 0:b7fc78d2b795 656
synvox 0:b7fc78d2b795 657 /** \brief Set draw mode.
synvox 0:b7fc78d2b795 658
synvox 0:b7fc78d2b795 659 Set current draw mode with NORM or XOR.
synvox 0:b7fc78d2b795 660 */
synvox 0:b7fc78d2b795 661 void MicroOLED::setDrawMode(uint8_t mode) {
synvox 0:b7fc78d2b795 662 drawMode=mode;
synvox 0:b7fc78d2b795 663 }
synvox 0:b7fc78d2b795 664
synvox 0:b7fc78d2b795 665 /** \brief Draw character.
synvox 0:b7fc78d2b795 666
synvox 0:b7fc78d2b795 667 Draw character c using current color and current draw mode at x,y.
synvox 0:b7fc78d2b795 668 */
synvox 0:b7fc78d2b795 669 void MicroOLED::drawChar(uint8_t x, uint8_t y, uint8_t c) {
synvox 0:b7fc78d2b795 670 drawChar(x,y,c,foreColor,drawMode);
synvox 0:b7fc78d2b795 671 }
synvox 0:b7fc78d2b795 672
synvox 0:b7fc78d2b795 673 /** \brief Draw character with color and mode.
synvox 0:b7fc78d2b795 674
synvox 0:b7fc78d2b795 675 Draw character c using color and draw mode at x,y.
synvox 0:b7fc78d2b795 676 */
synvox 0:b7fc78d2b795 677 void MicroOLED::drawChar(uint8_t x, uint8_t y, uint8_t c, uint8_t color, uint8_t mode) {
synvox 0:b7fc78d2b795 678 // TODO - New routine to take font of any height, at the moment limited to font height in multiple of 8 pixels
synvox 0:b7fc78d2b795 679
synvox 0:b7fc78d2b795 680 uint8_t rowsToDraw,row, tempC;
synvox 0:b7fc78d2b795 681 uint8_t i,j,temp;
synvox 0:b7fc78d2b795 682 uint16_t charPerBitmapRow,charColPositionOnBitmap,charRowPositionOnBitmap,charBitmapStartPosition;
synvox 0:b7fc78d2b795 683
synvox 0:b7fc78d2b795 684 if ((c<fontStartChar) || (c>(fontStartChar+fontTotalChar-1))) // no bitmap for the required c
synvox 0:b7fc78d2b795 685 return;
synvox 0:b7fc78d2b795 686
synvox 0:b7fc78d2b795 687 tempC=c-fontStartChar;
synvox 0:b7fc78d2b795 688
synvox 0:b7fc78d2b795 689 // each row (in datasheet is call page) is 8 bits high, 16 bit high character will have 2 rows to be drawn
synvox 0:b7fc78d2b795 690 rowsToDraw=fontHeight/8; // 8 is LCD's page size, see SSD1306 datasheet
synvox 0:b7fc78d2b795 691 if (rowsToDraw<=1) rowsToDraw=1;
synvox 0:b7fc78d2b795 692
synvox 0:b7fc78d2b795 693 // the following draw function can draw anywhere on the screen, but SLOW pixel by pixel draw
synvox 0:b7fc78d2b795 694 if (rowsToDraw==1) {
synvox 0:b7fc78d2b795 695 for (i=0;i<fontWidth+1;i++) {
synvox 0:b7fc78d2b795 696 if (i==fontWidth) // this is done in a weird way because for 5x7 font, there is no margin, this code add a margin after col 5
synvox 0:b7fc78d2b795 697 temp=0;
synvox 0:b7fc78d2b795 698 else
synvox 0:b7fc78d2b795 699 temp = *(fontsPointer[fontType]+FONTHEADERSIZE+(tempC*fontWidth)+i);
synvox 0:b7fc78d2b795 700
synvox 0:b7fc78d2b795 701 for (j=0;j<8;j++) { // 8 is the LCD's page height (see datasheet for explanation)
synvox 0:b7fc78d2b795 702 if (temp & 0x1) {
synvox 0:b7fc78d2b795 703 pixel(x+i, y+j, color,mode);
synvox 0:b7fc78d2b795 704 }
synvox 0:b7fc78d2b795 705 else {
synvox 0:b7fc78d2b795 706 pixel(x+i, y+j, !color,mode);
synvox 0:b7fc78d2b795 707 }
synvox 0:b7fc78d2b795 708
synvox 0:b7fc78d2b795 709 temp >>=1;
synvox 0:b7fc78d2b795 710 }
synvox 0:b7fc78d2b795 711 }
synvox 0:b7fc78d2b795 712 return;
synvox 0:b7fc78d2b795 713 }
synvox 0:b7fc78d2b795 714
synvox 0:b7fc78d2b795 715 // font height over 8 bit
synvox 0:b7fc78d2b795 716 // take character "0" ASCII 48 as example
synvox 0:b7fc78d2b795 717 charPerBitmapRow=fontMapWidth/fontWidth; // 256/8 =32 char per row
synvox 0:b7fc78d2b795 718 charColPositionOnBitmap=tempC % charPerBitmapRow; // =16
synvox 0:b7fc78d2b795 719 charRowPositionOnBitmap=int(tempC/charPerBitmapRow); // =1
synvox 0:b7fc78d2b795 720 charBitmapStartPosition=(charRowPositionOnBitmap * fontMapWidth * (fontHeight/8)) + (charColPositionOnBitmap * fontWidth) ;
synvox 0:b7fc78d2b795 721
synvox 0:b7fc78d2b795 722 // each row on LCD is 8 bit height (see datasheet for explanation)
synvox 0:b7fc78d2b795 723 for(row=0;row<rowsToDraw;row++) {
synvox 0:b7fc78d2b795 724 for (i=0; i<fontWidth;i++) {
synvox 0:b7fc78d2b795 725 temp = *(fontsPointer[fontType]+FONTHEADERSIZE+(charBitmapStartPosition+i+(row*fontMapWidth)));
synvox 0:b7fc78d2b795 726 for (j=0;j<8;j++) { // 8 is the LCD's page height (see datasheet for explanation)
synvox 0:b7fc78d2b795 727 if (temp & 0x1) {
synvox 0:b7fc78d2b795 728 pixel(x+i,y+j+(row*8), color, mode);
synvox 0:b7fc78d2b795 729 }
synvox 0:b7fc78d2b795 730 else {
synvox 0:b7fc78d2b795 731 pixel(x+i,y+j+(row*8), !color, mode);
synvox 0:b7fc78d2b795 732 }
synvox 0:b7fc78d2b795 733 temp >>=1;
synvox 0:b7fc78d2b795 734 }
synvox 0:b7fc78d2b795 735 }
synvox 0:b7fc78d2b795 736 }
synvox 0:b7fc78d2b795 737
synvox 0:b7fc78d2b795 738 }
synvox 0:b7fc78d2b795 739
synvox 0:b7fc78d2b795 740 /** \brief Stop scrolling.
synvox 0:b7fc78d2b795 741
synvox 0:b7fc78d2b795 742 Stop the scrolling of graphics on the OLED.
synvox 0:b7fc78d2b795 743 */
synvox 0:b7fc78d2b795 744 void MicroOLED::scrollStop(void){
synvox 0:b7fc78d2b795 745 command(DEACTIVATESCROLL);
synvox 0:b7fc78d2b795 746 }
synvox 0:b7fc78d2b795 747
synvox 0:b7fc78d2b795 748 /** \brief Right scrolling.
synvox 0:b7fc78d2b795 749
synvox 0:b7fc78d2b795 750 Set row start to row stop on the OLED to scroll right. Refer to http://learn.microview.io/intro/general-overview-of-microview.html for explanation of the rows.
synvox 0:b7fc78d2b795 751 */
synvox 0:b7fc78d2b795 752 void MicroOLED::scrollRight(uint8_t start, uint8_t stop){
synvox 0:b7fc78d2b795 753 if (stop<start) // stop must be larger or equal to start
synvox 0:b7fc78d2b795 754 return;
synvox 0:b7fc78d2b795 755 scrollStop(); // need to disable scrolling before starting to avoid memory corrupt
synvox 0:b7fc78d2b795 756 command(RIGHTHORIZONTALSCROLL, 0x00, start, 0x07, stop, 0x00, 0xFF, ACTIVATESCROLL); // scroll speed frames , TODO
synvox 0:b7fc78d2b795 757 }
synvox 0:b7fc78d2b795 758
synvox 0:b7fc78d2b795 759 /** \brief Left scrolling.
synvox 0:b7fc78d2b795 760 Set row start to row stop on the OLED to scroll left. Refer to http://learn.microview.io/intro/general-overview-of-microview.html for explanation of the rows.
synvox 0:b7fc78d2b795 761 */
synvox 0:b7fc78d2b795 762 void MicroOLED::scrollLeft(uint8_t start, uint8_t stop){
synvox 0:b7fc78d2b795 763 if (stop<start) // stop must be larger or equal to start
synvox 0:b7fc78d2b795 764 return;
synvox 0:b7fc78d2b795 765 scrollStop(); // need to disable scrolling before starting to avoid memory corrupt
synvox 0:b7fc78d2b795 766 command(LEFTHORIZONTALSCROLL, 0x00, start, 0x07, stop, 0x00, 0xFF, ACTIVATESCROLL); // scroll speed frames , TODO
synvox 0:b7fc78d2b795 767 }
synvox 0:b7fc78d2b795 768
synvox 0:b7fc78d2b795 769 /** \brief Vertical flip.
synvox 0:b7fc78d2b795 770
synvox 0:b7fc78d2b795 771 Flip the graphics on the OLED vertically.
synvox 0:b7fc78d2b795 772 */
synvox 0:b7fc78d2b795 773 void MicroOLED::flipVertical(boolean flip) {
synvox 0:b7fc78d2b795 774 if (flip) {
synvox 0:b7fc78d2b795 775 command(COMSCANINC);
synvox 0:b7fc78d2b795 776 }
synvox 0:b7fc78d2b795 777 else {
synvox 0:b7fc78d2b795 778 command(COMSCANDEC);
synvox 0:b7fc78d2b795 779 }
synvox 0:b7fc78d2b795 780 }
synvox 0:b7fc78d2b795 781
synvox 0:b7fc78d2b795 782 /** \brief Horizontal flip.
synvox 0:b7fc78d2b795 783
synvox 0:b7fc78d2b795 784 Flip the graphics on the OLED horizontally.
synvox 0:b7fc78d2b795 785 */
synvox 0:b7fc78d2b795 786 void MicroOLED::flipHorizontal(boolean flip) {
synvox 0:b7fc78d2b795 787 if (flip) {
synvox 0:b7fc78d2b795 788 command(SEGREMAP | 0x0);
synvox 0:b7fc78d2b795 789 }
synvox 0:b7fc78d2b795 790 else {
synvox 0:b7fc78d2b795 791 command(SEGREMAP | 0x1);
synvox 0:b7fc78d2b795 792 }
synvox 0:b7fc78d2b795 793 }
synvox 0:b7fc78d2b795 794
synvox 0:b7fc78d2b795 795 /*
synvox 0:b7fc78d2b795 796 Return a pointer to the start of the RAM screen buffer for direct access.
synvox 0:b7fc78d2b795 797 */
synvox 0:b7fc78d2b795 798 uint8_t *MicroOLED::getScreenBuffer(void) {
synvox 0:b7fc78d2b795 799 return screenmemory;
synvox 0:b7fc78d2b795 800 }
synvox 0:b7fc78d2b795 801
synvox 0:b7fc78d2b795 802 /*
synvox 0:b7fc78d2b795 803 Draw Bitmap image on screen. The array for the bitmap can be stored in main program file, so user don't have to mess with the library files.
synvox 0:b7fc78d2b795 804 To use, create const uint8_t array that is LCDWIDTH x LCDHEIGHT pixels (LCDWIDTH * LCDHEIGHT / 8 bytes). Then call .drawBitmap and pass it the array.
synvox 0:b7fc78d2b795 805 */
synvox 0:b7fc78d2b795 806 void MicroOLED::drawBitmap(const uint8_t * bitArray)
synvox 0:b7fc78d2b795 807 {
synvox 0:b7fc78d2b795 808 for (int i=0; i<(LCDWIDTH * LCDHEIGHT / 8); i++)
synvox 0:b7fc78d2b795 809 screenmemory[i] = bitArray[i];
synvox 0:b7fc78d2b795 810 }