a partial porting OLED_I2C library

Dependents:   ou_mbed_oled ou_mbed_tmp102

Committer:
poushen
Date:
Mon Jun 18 11:30:39 2018 +0000
Revision:
0:b5cb0f340c1a
Child:
1:c928953247eb
porting printNumI, printNumF, modify sample code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
poushen 0:b5cb0f340c1a 1 /** A oled i2c class porting from Arduino's OLED_I2C library
poushen 0:b5cb0f340c1a 2 *
poushen 0:b5cb0f340c1a 3 * @author Poushen Ou
poushen 0:b5cb0f340c1a 4 * @version 1.0
poushen 0:b5cb0f340c1a 5 * @date 17-Jun-2018
poushen 0:b5cb0f340c1a 6 *
poushen 0:b5cb0f340c1a 7 * This code provide operation for I2C OLED SSD1306
poushen 0:b5cb0f340c1a 8 *
poushen 0:b5cb0f340c1a 9 * About OLED SSD1306
poushen 0:b5cb0f340c1a 10 * https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
poushen 0:b5cb0f340c1a 11 */
poushen 0:b5cb0f340c1a 12 #ifndef _OLED_I2C_H_
poushen 0:b5cb0f340c1a 13 #define _OLED_I2C_H_
poushen 0:b5cb0f340c1a 14
poushen 0:b5cb0f340c1a 15 #include "mbed.h"
poushen 0:b5cb0f340c1a 16 #include "eeprom.h"
poushen 0:b5cb0f340c1a 17
poushen 0:b5cb0f340c1a 18 #define LEFT 0
poushen 0:b5cb0f340c1a 19 #define RIGHT 9999
poushen 0:b5cb0f340c1a 20 #define CENTER 9998
poushen 0:b5cb0f340c1a 21
poushen 0:b5cb0f340c1a 22 #define SSD1306_128_64
poushen 0:b5cb0f340c1a 23
poushen 0:b5cb0f340c1a 24 #define SSD1306_ADDR 0x78
poushen 0:b5cb0f340c1a 25 #define SSD1306_MEM_SIZE 360
poushen 0:b5cb0f340c1a 26 #define SSD1306_WIDTH 72
poushen 0:b5cb0f340c1a 27 #define SSD1306_LENGTH 40
poushen 0:b5cb0f340c1a 28
poushen 0:b5cb0f340c1a 29 #define SSD1306_COMMAND 0x00
poushen 0:b5cb0f340c1a 30 #define SSD1306_DATA 0xC0
poushen 0:b5cb0f340c1a 31 #define SSD1306_DATA_CONTINUE 0x40
poushen 0:b5cb0f340c1a 32
poushen 0:b5cb0f340c1a 33 //#define SSD1306_DATA_OUT_OPTIMIZED
poushen 0:b5cb0f340c1a 34
poushen 0:b5cb0f340c1a 35 // SSD1306 Commandset
poushen 0:b5cb0f340c1a 36 // ------------------
poushen 0:b5cb0f340c1a 37 // Fundamental Commands
poushen 0:b5cb0f340c1a 38 #define SSD1306_SET_CONTRAST_CONTROL 0x81
poushen 0:b5cb0f340c1a 39 #define SSD1306_DISPLAY_ALL_ON_RESUME 0xA4
poushen 0:b5cb0f340c1a 40 #define SSD1306_DISPLAY_ALL_ON 0xA5
poushen 0:b5cb0f340c1a 41 #define SSD1306_NORMAL_DISPLAY 0xA6
poushen 0:b5cb0f340c1a 42 #define SSD1306_INVERT_DISPLAY 0xA7
poushen 0:b5cb0f340c1a 43 #define SSD1306_DISPLAY_OFF 0xAE
poushen 0:b5cb0f340c1a 44 #define SSD1306_DISPLAY_ON 0xAF
poushen 0:b5cb0f340c1a 45 #define SSD1306_NOP 0xE3
poushen 0:b5cb0f340c1a 46 // Scrolling Commands
poushen 0:b5cb0f340c1a 47 #define SSD1306_HORIZONTAL_SCROLL_RIGHT 0x26
poushen 0:b5cb0f340c1a 48 #define SSD1306_HORIZONTAL_SCROLL_LEFT 0x27
poushen 0:b5cb0f340c1a 49 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_RIGHT 0x29
poushen 0:b5cb0f340c1a 50 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_LEFT 0x2A
poushen 0:b5cb0f340c1a 51 #define SSD1306_DEACTIVATE_SCROLL 0x2E
poushen 0:b5cb0f340c1a 52 #define SSD1306_ACTIVATE_SCROLL 0x2F
poushen 0:b5cb0f340c1a 53 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
poushen 0:b5cb0f340c1a 54 // Addressing Setting Commands
poushen 0:b5cb0f340c1a 55 #define SSD1306_SET_LOWER_COLUMN 0x00
poushen 0:b5cb0f340c1a 56 #define SSD1306_SET_HIGHER_COLUMN 0x10
poushen 0:b5cb0f340c1a 57 #define SSD1306_MEMORY_ADDR_MODE 0x20
poushen 0:b5cb0f340c1a 58 #define SSD1306_SET_COLUMN_ADDR 0x21
poushen 0:b5cb0f340c1a 59 #define SSD1306_SET_PAGE_ADDR 0x22
poushen 0:b5cb0f340c1a 60 // Hardware Configuration Commands
poushen 0:b5cb0f340c1a 61 #define SSD1306_SET_START_LINE 0x40
poushen 0:b5cb0f340c1a 62 #define SSD1306_SET_SEGMENT_REMAP 0xA0
poushen 0:b5cb0f340c1a 63 #define SSD1306_SET_MULTIPLEX_RATIO 0xA8
poushen 0:b5cb0f340c1a 64 #define SSD1306_COM_SCAN_DIR_INC 0xC0
poushen 0:b5cb0f340c1a 65 #define SSD1306_COM_SCAN_DIR_DEC 0xC8
poushen 0:b5cb0f340c1a 66 #define SSD1306_SET_DISPLAY_OFFSET 0xD3
poushen 0:b5cb0f340c1a 67 #define SSD1306_SET_COM_PINS 0xDA
poushen 0:b5cb0f340c1a 68 #define SSD1306_CHARGE_PUMP 0x8D
poushen 0:b5cb0f340c1a 69 // Timing & Driving Scheme Setting Commands
poushen 0:b5cb0f340c1a 70 #define SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO 0xD5
poushen 0:b5cb0f340c1a 71 #define SSD1306_SET_PRECHARGE_PERIOD 0xD9
poushen 0:b5cb0f340c1a 72 #define SSD1306_SET_VCOM_DESELECT 0xDB
poushen 0:b5cb0f340c1a 73
poushen 0:b5cb0f340c1a 74 /** oled_i2c Class Library
poushen 0:b5cb0f340c1a 75 * to provide access interface to OLED screen for mbed
poushen 0:b5cb0f340c1a 76 *
poushen 0:b5cb0f340c1a 77 * Example:
poushen 0:b5cb0f340c1a 78 * @code
poushen 0:b5cb0f340c1a 79 * #include "mbed.h"
poushen 0:b5cb0f340c1a 80 * #include "oled_i2c.h"
poushen 0:b5cb0f340c1a 81 * #include "oled_font.h"
poushen 0:b5cb0f340c1a 82 *
poushen 0:b5cb0f340c1a 83 * // make oled_i2c instance using I2C object.
poushen 0:b5cb0f340c1a 84 * // with default slave address 0x78 (0x3C in 7bit format)
poushen 0:b5cb0f340c1a 85 * // test ok with SSD1306 I2C 128*64 oled screen
poushen 0:b5cb0f340c1a 86 * I2C i2c(dp5,dp27); // for LPC1114 or LPC1115
poushen 0:b5cb0f340c1a 87 * oled_i2c oled(i2c);
poushen 0:b5cb0f340c1a 88 *
poushen 0:b5cb0f340c1a 89 * int main()
poushen 0:b5cb0f340c1a 90 * {
poushen 0:b5cb0f340c1a 91 * i2c.frequency(1000 * 1000);
poushen 0:b5cb0f340c1a 92 * oled.init_oled();
poushen 0:b5cb0f340c1a 93 *
poushen 0:b5cb0f340c1a 94 * oled.setFont(BigNumbers);
poushen 0:b5cb0f340c1a 95 * //oled.print("27", 20, 10);
poushen 0:b5cb0f340c1a 96 * //oled.printNumI(27, 0, 0);
poushen 0:b5cb0f340c1a 97 * oled.printNumF(27.45, 2, 0, 0);
poushen 0:b5cb0f340c1a 98 * oled.setFont(SmallFont);
poushen 0:b5cb0f340c1a 99 * oled.print("~c", 60, 30);
poushen 0:b5cb0f340c1a 100 * oled.update();
poushen 0:b5cb0f340c1a 101 *
poushen 0:b5cb0f340c1a 102 * while(1);
poushen 0:b5cb0f340c1a 103 * }
poushen 0:b5cb0f340c1a 104 * @endcode
poushen 0:b5cb0f340c1a 105 */
poushen 0:b5cb0f340c1a 106 class oled_i2c
poushen 0:b5cb0f340c1a 107 {
poushen 0:b5cb0f340c1a 108 public:
poushen 0:b5cb0f340c1a 109 /** Create a oled_i2c instance connected to specified I2C pins with specified address
poushen 0:b5cb0f340c1a 110 *
poushen 0:b5cb0f340c1a 111 * @param i2c_obj I2C object (instance)
poushen 0:b5cb0f340c1a 112 * @param address (option) I2C-bus slave address
poushen 0:b5cb0f340c1a 113 */
poushen 0:b5cb0f340c1a 114 oled_i2c(I2C &i2c_obj, char address = SSD1306_ADDR);
poushen 0:b5cb0f340c1a 115
poushen 0:b5cb0f340c1a 116 /** Destractor */
poushen 0:b5cb0f340c1a 117 ~oled_i2c();
poushen 0:b5cb0f340c1a 118
poushen 0:b5cb0f340c1a 119 /** Initialization */
poushen 0:b5cb0f340c1a 120 void init(void);
poushen 0:b5cb0f340c1a 121
poushen 0:b5cb0f340c1a 122 /** send SSD1306 command
poushen 0:b5cb0f340c1a 123 *
poushen 0:b5cb0f340c1a 124 * @param a the SSD1306 command byte to send out
poushen 0:b5cb0f340c1a 125 */
poushen 0:b5cb0f340c1a 126 void comm_out(uint8_t a);
poushen 0:b5cb0f340c1a 127
poushen 0:b5cb0f340c1a 128 /** send many SSD1306 commands in one I2C transaction
poushen 0:b5cb0f340c1a 129 *
poushen 0:b5cb0f340c1a 130 * @param pPage pointer to command buffer
poushen 0:b5cb0f340c1a 131 * @param size the command buffer size
poushen 0:b5cb0f340c1a 132 */
poushen 0:b5cb0f340c1a 133 void comm_page_out(uint8_t* pPage, uint8_t size);
poushen 0:b5cb0f340c1a 134
poushen 0:b5cb0f340c1a 135 /** send SSD1306 data
poushen 0:b5cb0f340c1a 136 *
poushen 0:b5cb0f340c1a 137 * @param a the SSD1306 data byte to send out
poushen 0:b5cb0f340c1a 138 */
poushen 0:b5cb0f340c1a 139 void data_out(uint8_t a);
poushen 0:b5cb0f340c1a 140
poushen 0:b5cb0f340c1a 141 /** send many SSD1306 data bytes in one I2c transaction
poushen 0:b5cb0f340c1a 142 *
poushen 0:b5cb0f340c1a 143 * @param pPage pointer to data buffer
poushen 0:b5cb0f340c1a 144 * @param size the data buffer size
poushen 0:b5cb0f340c1a 145 */
poushen 0:b5cb0f340c1a 146 void data_page_out(uint8_t* pPage, uint8_t size);
poushen 0:b5cb0f340c1a 147
poushen 0:b5cb0f340c1a 148 /** send init command to SSD1306 */
poushen 0:b5cb0f340c1a 149 void init_oled(void);
poushen 0:b5cb0f340c1a 150
poushen 0:b5cb0f340c1a 151 /** make all pixel on or off acording to screen buffer */
poushen 0:b5cb0f340c1a 152 void pixel_on(void);
poushen 0:b5cb0f340c1a 153
poushen 0:b5cb0f340c1a 154 /** update screen buffer */
poushen 0:b5cb0f340c1a 155 void update(void);
poushen 0:b5cb0f340c1a 156
poushen 0:b5cb0f340c1a 157 /** clear screen */
poushen 0:b5cb0f340c1a 158 void clrScr(void);
poushen 0:b5cb0f340c1a 159
poushen 0:b5cb0f340c1a 160 /** fill screen */
poushen 0:b5cb0f340c1a 161 void fillScr(void);
poushen 0:b5cb0f340c1a 162
poushen 0:b5cb0f340c1a 163 /** set pixel on coordinate x, y
poushen 0:b5cb0f340c1a 164 *
poushen 0:b5cb0f340c1a 165 * @param x coordinate x (column)
poushen 0:b5cb0f340c1a 166 * @param y coordinate y (row)
poushen 0:b5cb0f340c1a 167 */
poushen 0:b5cb0f340c1a 168 void setPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 169
poushen 0:b5cb0f340c1a 170 /** clear pixel on coordinate x, y
poushen 0:b5cb0f340c1a 171 *
poushen 0:b5cb0f340c1a 172 * @param x coordinate x (column)
poushen 0:b5cb0f340c1a 173 * @param y coordinate y (row)
poushen 0:b5cb0f340c1a 174 */
poushen 0:b5cb0f340c1a 175 void clrPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 176
poushen 0:b5cb0f340c1a 177 /** set font type
poushen 0:b5cb0f340c1a 178 *
poushen 0:b5cb0f340c1a 179 * @param font the font to print
poushen 0:b5cb0f340c1a 180 */
poushen 0:b5cb0f340c1a 181 void setFont(const uint8_t* font);
poushen 0:b5cb0f340c1a 182
poushen 0:b5cb0f340c1a 183 /** print character to coordinate x, y
poushen 0:b5cb0f340c1a 184 *
poushen 0:b5cb0f340c1a 185 * @param c the character to print
poushen 0:b5cb0f340c1a 186 * @param x coordinate x
poushen 0:b5cb0f340c1a 187 * @param y coordinate y
poushen 0:b5cb0f340c1a 188 */
poushen 0:b5cb0f340c1a 189 void print_char(unsigned char c, int x, int y);
poushen 0:b5cb0f340c1a 190
poushen 0:b5cb0f340c1a 191 /** print string to coordinate x, y
poushen 0:b5cb0f340c1a 192 *
poushen 0:b5cb0f340c1a 193 * @param st the string to print
poushen 0:b5cb0f340c1a 194 * @param x coordinate x
poushen 0:b5cb0f340c1a 195 * @param y coordinate y
poushen 0:b5cb0f340c1a 196 */
poushen 0:b5cb0f340c1a 197 void print(char* st, int x, int y);
poushen 0:b5cb0f340c1a 198
poushen 0:b5cb0f340c1a 199 /** print integer number to coordinate x, y
poushen 0:b5cb0f340c1a 200 *
poushen 0:b5cb0f340c1a 201 * @param num the integer to be print
poushen 0:b5cb0f340c1a 202 * @param x coordinate x
poushen 0:b5cb0f340c1a 203 * @param y coordinate y
poushen 0:b5cb0f340c1a 204 * @param length the length of integer to be print
poushen 0:b5cb0f340c1a 205 * @param filler the prefix character
poushen 0:b5cb0f340c1a 206 */
poushen 0:b5cb0f340c1a 207 void printNumI(long num, int x, int y, int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 208
poushen 0:b5cb0f340c1a 209 /** print float number to coordinate x, y
poushen 0:b5cb0f340c1a 210 *
poushen 0:b5cb0f340c1a 211 * @param num the float number to be print
poushen 0:b5cb0f340c1a 212 * @param dec the numbers of digits after point
poushen 0:b5cb0f340c1a 213 * @param x coordinate x
poushen 0:b5cb0f340c1a 214 * @param y coordinate y
poushen 0:b5cb0f340c1a 215 * @param divider should be the point (.)
poushen 0:b5cb0f340c1a 216 * @param length the length of float number to be print
poushen 0:b5cb0f340c1a 217 * @param filler the prefix character
poushen 0:b5cb0f340c1a 218 */
poushen 0:b5cb0f340c1a 219 void printNumF(double num, uint8_t dec, int x, int y, char divider='.', int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 220
poushen 0:b5cb0f340c1a 221
poushen 0:b5cb0f340c1a 222 private:
poushen 0:b5cb0f340c1a 223 I2C &i2c;
poushen 0:b5cb0f340c1a 224 char adr;
poushen 0:b5cb0f340c1a 225 eeprom *epm_p;
poushen 0:b5cb0f340c1a 226 eeprom &epm;
poushen 0:b5cb0f340c1a 227 struct _current_font
poushen 0:b5cb0f340c1a 228 {
poushen 0:b5cb0f340c1a 229 const uint8_t* font;
poushen 0:b5cb0f340c1a 230 uint8_t x_size;
poushen 0:b5cb0f340c1a 231 uint8_t y_size;
poushen 0:b5cb0f340c1a 232 uint8_t offset;
poushen 0:b5cb0f340c1a 233 uint8_t numchars;
poushen 0:b5cb0f340c1a 234 } cfont;
poushen 0:b5cb0f340c1a 235 uint8_t scrbuf[SSD1306_MEM_SIZE];
poushen 0:b5cb0f340c1a 236 };
poushen 0:b5cb0f340c1a 237
poushen 0:b5cb0f340c1a 238 #endif