SSD1306 LCD 96x16 model

Dependents:   testSoftware8_alles_display

Fork of ssd1306_library by Miguel Angel Rodriguez Jodar

Committer:
rodriguj
Date:
Wed Nov 01 16:19:50 2017 +0000
Revision:
4:35757c8b7625
Parent:
3:bb6fba3e84ff
Child:
5:4f7cdc2ee49a
Attempting to sucess on autogenerating documentation from the source code to have some API docs for users to review

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rodriguj 0:3d84b3bfb794 1 /*
rodriguj 4:35757c8b7625 2 * ssd1306.cpp
rodriguj 0:3d84b3bfb794 3 *
rodriguj 0:3d84b3bfb794 4 * Created on: 20 oct. 2017
rodriguj 3:bb6fba3e84ff 5 * Author: Miguel Angel Rodriguez Jodar
rodriguj 3:bb6fba3e84ff 6 *
rodriguj 3:bb6fba3e84ff 7 * Based upon Adafruit library.
rodriguj 3:bb6fba3e84ff 8 * GPL licensed.
rodriguj 3:bb6fba3e84ff 9 *
rodriguj 0:3d84b3bfb794 10 */
rodriguj 3:bb6fba3e84ff 11
rodriguj 0:3d84b3bfb794 12 #ifndef MBED_SSD1306_LIBRARY
rodriguj 0:3d84b3bfb794 13 #define MBED_SSD1306_LIBRARY
rodriguj 0:3d84b3bfb794 14
rodriguj 0:3d84b3bfb794 15 #include "mbed.h"
rodriguj 0:3d84b3bfb794 16
rodriguj 4:35757c8b7625 17 #define SSD1306_IS_COMMAND 0x00
rodriguj 4:35757c8b7625 18 #define SSD1306_IS_DATA 0x40
rodriguj 4:35757c8b7625 19 #define SSD1306_IS_NOT_LAST 0x80
rodriguj 4:35757c8b7625 20 #define SSD1306_IS_LAST 0x00
rodriguj 0:3d84b3bfb794 21
rodriguj 0:3d84b3bfb794 22 #define SSD1306_SETCONTRAST 0x81
rodriguj 0:3d84b3bfb794 23 #define SSD1306_DISPLAYALLON_RESUME 0xA4
rodriguj 0:3d84b3bfb794 24 #define SSD1306_DISPLAYALLON 0xA5
rodriguj 0:3d84b3bfb794 25 #define SSD1306_NORMALDISPLAY 0xA6
rodriguj 0:3d84b3bfb794 26 #define SSD1306_INVERTDISPLAY 0xA7
rodriguj 0:3d84b3bfb794 27 #define SSD1306_DISPLAYOFF 0xAE
rodriguj 0:3d84b3bfb794 28 #define SSD1306_DISPLAYON 0xAF
rodriguj 0:3d84b3bfb794 29
rodriguj 0:3d84b3bfb794 30 #define SSD1306_SETDISPLAYOFFSET 0xD3
rodriguj 0:3d84b3bfb794 31 #define SSD1306_SETCOMPINS 0xDA
rodriguj 0:3d84b3bfb794 32
rodriguj 0:3d84b3bfb794 33 #define SSD1306_SETVCOMDETECT 0xDB
rodriguj 0:3d84b3bfb794 34
rodriguj 0:3d84b3bfb794 35 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
rodriguj 0:3d84b3bfb794 36 #define SSD1306_SETPRECHARGE 0xD9
rodriguj 0:3d84b3bfb794 37
rodriguj 0:3d84b3bfb794 38 #define SSD1306_SETMULTIPLEX 0xA8
rodriguj 0:3d84b3bfb794 39
rodriguj 0:3d84b3bfb794 40 #define SSD1306_SETLOWCOLUMN 0x00
rodriguj 0:3d84b3bfb794 41 #define SSD1306_SETHIGHCOLUMN 0x10
rodriguj 0:3d84b3bfb794 42
rodriguj 0:3d84b3bfb794 43 #define SSD1306_SETSTARTLINE 0x40
rodriguj 0:3d84b3bfb794 44
rodriguj 0:3d84b3bfb794 45 #define SSD1306_MEMORYMODE 0x20
rodriguj 0:3d84b3bfb794 46
rodriguj 0:3d84b3bfb794 47 #define SSD1306_COMSCANINC 0xC0
rodriguj 0:3d84b3bfb794 48 #define SSD1306_COMSCANDEC 0xC8
rodriguj 0:3d84b3bfb794 49
rodriguj 0:3d84b3bfb794 50 #define SSD1306_SEGREMAP 0xA0
rodriguj 0:3d84b3bfb794 51
rodriguj 0:3d84b3bfb794 52 #define SSD1306_CHARGEPUMP 0x8D
rodriguj 0:3d84b3bfb794 53
rodriguj 0:3d84b3bfb794 54 #define SSD1306_EXTERNALVCC 0x1
rodriguj 0:3d84b3bfb794 55 #define SSD1306_SWITCHCAPVCC 0x2
rodriguj 0:3d84b3bfb794 56
rodriguj 0:3d84b3bfb794 57 // Scrolling #defines
rodriguj 0:3d84b3bfb794 58 #define SSD1306_ACTIVATE_SCROLL 0x2F
rodriguj 0:3d84b3bfb794 59 #define SSD1306_DEACTIVATE_SCROLL 0x2E
rodriguj 0:3d84b3bfb794 60 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
rodriguj 0:3d84b3bfb794 61 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
rodriguj 0:3d84b3bfb794 62 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
rodriguj 0:3d84b3bfb794 63 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
rodriguj 0:3d84b3bfb794 64 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
rodriguj 0:3d84b3bfb794 65
rodriguj 4:35757c8b7625 66 class SSD1306 {
rodriguj 0:3d84b3bfb794 67 public:
rodriguj 0:3d84b3bfb794 68 enum PlotStyle
rodriguj 0:3d84b3bfb794 69 {
rodriguj 0:3d84b3bfb794 70 Normal,
rodriguj 0:3d84b3bfb794 71 Inverse,
rodriguj 0:3d84b3bfb794 72 Xor
rodriguj 0:3d84b3bfb794 73 };
rodriguj 0:3d84b3bfb794 74
rodriguj 0:3d84b3bfb794 75 enum I2CSpeed
rodriguj 0:3d84b3bfb794 76 {
rodriguj 4:35757c8b7625 77 Slow,
rodriguj 0:3d84b3bfb794 78 Medium,
rodriguj 0:3d84b3bfb794 79 Fast
rodriguj 0:3d84b3bfb794 80 };
rodriguj 0:3d84b3bfb794 81
rodriguj 3:bb6fba3e84ff 82 /** Creates an instance of a SSD1306 driver, specifying I2C pins to use
rodriguj 0:3d84b3bfb794 83 *
rodriguj 0:3d84b3bfb794 84 * @param sda I2C data line pin
rodriguj 4:35757c8b7625 85 *
rodriguj 0:3d84b3bfb794 86 * @param scl I2C clock line pin
rodriguj 0:3d84b3bfb794 87 */
rodriguj 3:bb6fba3e84ff 88 SSD1306 (PinName sda, PinName scl, char ssd1306_addr = 0x78);
rodriguj 3:bb6fba3e84ff 89
rodriguj 3:bb6fba3e84ff 90 /** Creates an instance of a SSD1306 driver using an existing I2C object
rodriguj 3:bb6fba3e84ff 91 *
rodriguj 3:bb6fba3e84ff 92 * @param busi2c I2C object
rodriguj 4:35757c8b7625 93 *
rodriguj 3:bb6fba3e84ff 94 * @param ssd1306_addr I2C addr of SSD1306 controller
rodriguj 3:bb6fba3e84ff 95 */
rodriguj 3:bb6fba3e84ff 96 SSD1306 (I2C &busi2c, char ssd1306_addr = 0x78);
rodriguj 0:3d84b3bfb794 97
rodriguj 0:3d84b3bfb794 98 /** Set the frequency of the I2C interface
rodriguj 0:3d84b3bfb794 99 *
rodriguj 0:3d84b3bfb794 100 * @param hz The bus frequency in hertz
rodriguj 0:3d84b3bfb794 101 */
rodriguj 0:3d84b3bfb794 102 void speed (I2CSpeed spd);
rodriguj 0:3d84b3bfb794 103
rodriguj 4:35757c8b7625 104 /** Initialize the SSD1306 controller
rodriguj 4:35757c8b7625 105 */
rodriguj 0:3d84b3bfb794 106 int init (void);
rodriguj 0:3d84b3bfb794 107
rodriguj 4:35757c8b7625 108 /** Scrolls up one text line
rodriguj 4:35757c8b7625 109 *
rodriguj 4:35757c8b7625 110 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 111 */
rodriguj 1:c5cf4ca5939f 112 void scroll (bool refresh=false);
rodriguj 0:3d84b3bfb794 113
rodriguj 4:35757c8b7625 114 /** Print a character at the current text position. Updates the text position and forces a scroll if at the end of the screen
rodriguj 4:35757c8b7625 115 *
rodriguj 4:35757c8b7625 116 * @param c ASCII code of the character to print (8-bit ASCII code, use original IBM code page 437. No control codes.
rodriguj 4:35757c8b7625 117 *
rodriguj 4:35757c8b7625 118 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 119 */
rodriguj 1:c5cf4ca5939f 120 void putchar (char c, bool refresh=false);
rodriguj 0:3d84b3bfb794 121
rodriguj 4:35757c8b7625 122 /** Prints a NUL terminated string
rodriguj 4:35757c8b7625 123 *
rodriguj 4:35757c8b7625 124 * @param s C-style string (NUL terminated) to print.
rodriguj 4:35757c8b7625 125 *
rodriguj 4:35757c8b7625 126 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 127 */
rodriguj 2:7f1160c1a741 128 void puts (char *s, bool refresh=false);
rodriguj 3:bb6fba3e84ff 129
rodriguj 4:35757c8b7625 130 /** printf interface to SSD1306 controller
rodriguj 4:35757c8b7625 131 *
rodriguj 4:35757c8b7625 132 * @param fmt Format string.
rodriguj 4:35757c8b7625 133 */
rodriguj 4:35757c8b7625 134 void printf (const char *fmt,...);
rodriguj 2:7f1160c1a741 135
rodriguj 4:35757c8b7625 136 /** Change the text position
rodriguj 4:35757c8b7625 137 *
rodriguj 4:35757c8b7625 138 * @param row Text row (0-7) to print the next character
rodriguj 4:35757c8b7625 139 *
rodriguj 4:35757c8b7625 140 * @param column Text column (0-15) to print the next character
rodriguj 4:35757c8b7625 141 */
rodriguj 2:7f1160c1a741 142 void locate (char row, char column);
rodriguj 2:7f1160c1a741 143
rodriguj 4:35757c8b7625 144 /** Redraw the physical display, sending the content of the display memory to the SSD1306 controller using the I2C bus
rodriguj 4:35757c8b7625 145 */
rodriguj 0:3d84b3bfb794 146 void display (void);
rodriguj 0:3d84b3bfb794 147
rodriguj 4:35757c8b7625 148 /** Changes the contrast (actually, pixel brightness) of the screen
rodriguj 4:35757c8b7625 149 *
rodriguj 4:35757c8b7625 150 * @param v Contrast level (0-255)
rodriguj 4:35757c8b7625 151 */
rodriguj 0:3d84b3bfb794 152 void set_contrast (char v);
rodriguj 0:3d84b3bfb794 153
rodriguj 4:35757c8b7625 154 /** Clear screen and optionally, fills it with a predefined picture (in 128x64 OLED format, see display datasheet). Also resets the text position.
rodriguj 4:35757c8b7625 155 *
rodriguj 4:35757c8b7625 156 * @param bkground If not NULL, should point to a 1024 byte buffer with the image to load to the display
rodriguj 4:35757c8b7625 157 *
rodriguj 4:35757c8b7625 158 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 159 */
rodriguj 1:c5cf4ca5939f 160 void cls (char *bkground=NULL, bool refresh=false);
rodriguj 0:3d84b3bfb794 161
rodriguj 4:35757c8b7625 162 /** Plots a pixel.
rodriguj 4:35757c8b7625 163 *
rodriguj 4:35757c8b7625 164 * @param x x-coordinate (0-127) of the pixel. X coordinates go left to right.
rodriguj 4:35757c8b7625 165 *
rodriguj 4:35757c8b7625 166 * @param y y-coordinate (0-63) of the pixel. Y coordinates go up to down.
rodriguj 4:35757c8b7625 167 *
rodriguj 4:35757c8b7625 168 * @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
rodriguj 4:35757c8b7625 169 *
rodriguj 4:35757c8b7625 170 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 171 */
rodriguj 4:35757c8b7625 172 void plot (char x, char y, PlotStyle mode, bool refresh=false);
rodriguj 1:c5cf4ca5939f 173
rodriguj 4:35757c8b7625 174 /** Returns the state of a pixel coordinate from screen
rodriguj 4:35757c8b7625 175 *
rodriguj 4:35757c8b7625 176 * @param x x-coordinate (0-127) of the pixel. X coordinates go left to right.
rodriguj 4:35757c8b7625 177 *
rodriguj 4:35757c8b7625 178 * @param y y-coordinate (0-63) of the pixel. Y coordinates go up to down.
rodriguj 4:35757c8b7625 179 *
rodriguj 4:35757c8b7625 180 * @return true if the pixel was set, or false otherwise
rodriguj 4:35757c8b7625 181 */
rodriguj 3:bb6fba3e84ff 182 bool point (char x, char y);
rodriguj 3:bb6fba3e84ff 183
rodriguj 4:35757c8b7625 184 /** Draws a line using the Bresenham algorithm
rodriguj 4:35757c8b7625 185 *
rodriguj 4:35757c8b7625 186 * @param x0 x-coordinate (0-127) of the starting point
rodriguj 4:35757c8b7625 187 *
rodriguj 4:35757c8b7625 188 * @param y0 y-coordinate (0-63) of the starting point
rodriguj 4:35757c8b7625 189 *
rodriguj 4:35757c8b7625 190 * @param x1 x-coordinate (0-127) of the ending point
rodriguj 4:35757c8b7625 191 *
rodriguj 4:35757c8b7625 192 * @param y1 y-coordinate (0-63) of the ending point
rodriguj 4:35757c8b7625 193 *
rodriguj 4:35757c8b7625 194 * @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
rodriguj 4:35757c8b7625 195 *
rodriguj 4:35757c8b7625 196 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 197 */
rodriguj 4:35757c8b7625 198 void line (char x0, char y0, char x1, char y1, PlotStyle mode, bool refresh=false);
rodriguj 0:3d84b3bfb794 199
rodriguj 4:35757c8b7625 200 /** Draws a circle
rodriguj 4:35757c8b7625 201 *
rodriguj 4:35757c8b7625 202 * @param x0 x-coordinate (0-127) of the center point
rodriguj 4:35757c8b7625 203 *
rodriguj 4:35757c8b7625 204 * @param y0 y-coordinate (0-63) of the center point
rodriguj 4:35757c8b7625 205 *
rodriguj 4:35757c8b7625 206 * @param r radius of the circle
rodriguj 4:35757c8b7625 207 *
rodriguj 4:35757c8b7625 208 * @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
rodriguj 4:35757c8b7625 209 *
rodriguj 4:35757c8b7625 210 * @param refresh (optional) Force an actual screen redraw after scrolling
rodriguj 4:35757c8b7625 211 */
rodriguj 4:35757c8b7625 212 void circle (char x0, char y0, char r, PlotStyle mode, bool refresh=false);
rodriguj 3:bb6fba3e84ff 213
rodriguj 0:3d84b3bfb794 214 virtual ~SSD1306 () {
rodriguj 3:bb6fba3e84ff 215 if (!do_not_delete_bus)
rodriguj 3:bb6fba3e84ff 216 delete bus;
rodriguj 0:3d84b3bfb794 217 delete fb;
rodriguj 0:3d84b3bfb794 218 }
rodriguj 0:3d84b3bfb794 219
rodriguj 0:3d84b3bfb794 220 protected:
rodriguj 4:35757c8b7625 221 I2C *bus; // I2C object
rodriguj 4:35757c8b7625 222 char ssd1306_i2c_addr; // I2C address of SSD1306 controller
rodriguj 4:35757c8b7625 223 char *fb; // pointer to display buffer (1024 bytes)
rodriguj 4:35757c8b7625 224 char do_not_delete_bus; // flag to prevent deletion of bus when destroying SSD1306 object
rodriguj 4:35757c8b7625 225 int idxfb; // Current text position (referred to screen address memory)
rodriguj 4:35757c8b7625 226 int command (char c); // Sends a I2C command to SSD1306
rodriguj 4:35757c8b7625 227 int data (char d); // Sends I2C data to SSD1306
rodriguj 4:35757c8b7625 228 int command_data (char c, char c_or_d, char lastitem); // Sends a command or data and signal if it is not the last command/data in a list
rodriguj 4:35757c8b7625 229 char scan (); // Scans the I2C bus searcing for I2C id's 0x78 or 0x7A. Currently not used
rodriguj 0:3d84b3bfb794 230 };
rodriguj 0:3d84b3bfb794 231
rodriguj 0:3d84b3bfb794 232 #endif