a partial porting OLED_I2C library

Dependents:   ou_mbed_oled ou_mbed_tmp102

Committer:
poushen
Date:
Fri Jun 29 09:11:06 2018 +0000
Revision:
3:909dd63644b4
Parent:
2:e64ae00d801c
remove some member to private

Who changed what in which revision?

UserRevisionLine numberNew contents of line
poushen 1:c928953247eb 1 /** A oled i2c class partial 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 2:e64ae00d801c 12 /*
poushen 2:e64ae00d801c 13 OLED_I2C.h - Arduino/chipKit library support for 128x64 pixel SSD1306 OLEDs
poushen 2:e64ae00d801c 14 Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
poushen 2:e64ae00d801c 15
poushen 2:e64ae00d801c 16 This library has been made to make it easy to use 128x64 pixel OLED displays
poushen 2:e64ae00d801c 17 based on the SSD1306 controller chip with an Arduino or a chipKit.
poushen 2:e64ae00d801c 18
poushen 2:e64ae00d801c 19 You can always find the latest version of the library at
poushen 2:e64ae00d801c 20 http://www.RinkyDinkElectronics.com/
poushen 2:e64ae00d801c 21
poushen 2:e64ae00d801c 22 This library is free software; you can redistribute it and/or
poushen 2:e64ae00d801c 23 modify it under the terms of the CC BY-NC-SA 3.0 license.
poushen 2:e64ae00d801c 24 Please see the included documents for further information.
poushen 2:e64ae00d801c 25
poushen 2:e64ae00d801c 26 Commercial use of this library requires you to buy a license that
poushen 2:e64ae00d801c 27 will allow commercial use. This includes using the library,
poushen 2:e64ae00d801c 28 modified or not, as a tool to sell products.
poushen 2:e64ae00d801c 29
poushen 2:e64ae00d801c 30 The license applies to all part of the library including the
poushen 2:e64ae00d801c 31 examples and tools supplied with the library.
poushen 2:e64ae00d801c 32 */
poushen 2:e64ae00d801c 33
poushen 0:b5cb0f340c1a 34 #ifndef _OLED_I2C_H_
poushen 0:b5cb0f340c1a 35 #define _OLED_I2C_H_
poushen 0:b5cb0f340c1a 36
poushen 0:b5cb0f340c1a 37 #include "mbed.h"
poushen 0:b5cb0f340c1a 38 #include "eeprom.h"
poushen 0:b5cb0f340c1a 39
poushen 0:b5cb0f340c1a 40 #define LEFT 0
poushen 0:b5cb0f340c1a 41 #define RIGHT 9999
poushen 0:b5cb0f340c1a 42 #define CENTER 9998
poushen 0:b5cb0f340c1a 43
poushen 0:b5cb0f340c1a 44 #define SSD1306_128_64
poushen 0:b5cb0f340c1a 45
poushen 0:b5cb0f340c1a 46 #define SSD1306_ADDR 0x78
poushen 0:b5cb0f340c1a 47 #define SSD1306_MEM_SIZE 360
poushen 0:b5cb0f340c1a 48 #define SSD1306_WIDTH 72
poushen 0:b5cb0f340c1a 49 #define SSD1306_LENGTH 40
poushen 0:b5cb0f340c1a 50
poushen 0:b5cb0f340c1a 51 #define SSD1306_COMMAND 0x00
poushen 0:b5cb0f340c1a 52 #define SSD1306_DATA 0xC0
poushen 0:b5cb0f340c1a 53 #define SSD1306_DATA_CONTINUE 0x40
poushen 0:b5cb0f340c1a 54
poushen 0:b5cb0f340c1a 55 //#define SSD1306_DATA_OUT_OPTIMIZED
poushen 0:b5cb0f340c1a 56
poushen 0:b5cb0f340c1a 57 // SSD1306 Commandset
poushen 0:b5cb0f340c1a 58 // ------------------
poushen 0:b5cb0f340c1a 59 // Fundamental Commands
poushen 0:b5cb0f340c1a 60 #define SSD1306_SET_CONTRAST_CONTROL 0x81
poushen 0:b5cb0f340c1a 61 #define SSD1306_DISPLAY_ALL_ON_RESUME 0xA4
poushen 0:b5cb0f340c1a 62 #define SSD1306_DISPLAY_ALL_ON 0xA5
poushen 0:b5cb0f340c1a 63 #define SSD1306_NORMAL_DISPLAY 0xA6
poushen 0:b5cb0f340c1a 64 #define SSD1306_INVERT_DISPLAY 0xA7
poushen 0:b5cb0f340c1a 65 #define SSD1306_DISPLAY_OFF 0xAE
poushen 0:b5cb0f340c1a 66 #define SSD1306_DISPLAY_ON 0xAF
poushen 0:b5cb0f340c1a 67 #define SSD1306_NOP 0xE3
poushen 0:b5cb0f340c1a 68 // Scrolling Commands
poushen 0:b5cb0f340c1a 69 #define SSD1306_HORIZONTAL_SCROLL_RIGHT 0x26
poushen 0:b5cb0f340c1a 70 #define SSD1306_HORIZONTAL_SCROLL_LEFT 0x27
poushen 0:b5cb0f340c1a 71 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_RIGHT 0x29
poushen 0:b5cb0f340c1a 72 #define SSD1306_HORIZONTAL_SCROLL_VERTICAL_AND_LEFT 0x2A
poushen 0:b5cb0f340c1a 73 #define SSD1306_DEACTIVATE_SCROLL 0x2E
poushen 0:b5cb0f340c1a 74 #define SSD1306_ACTIVATE_SCROLL 0x2F
poushen 0:b5cb0f340c1a 75 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
poushen 0:b5cb0f340c1a 76 // Addressing Setting Commands
poushen 0:b5cb0f340c1a 77 #define SSD1306_SET_LOWER_COLUMN 0x00
poushen 0:b5cb0f340c1a 78 #define SSD1306_SET_HIGHER_COLUMN 0x10
poushen 0:b5cb0f340c1a 79 #define SSD1306_MEMORY_ADDR_MODE 0x20
poushen 0:b5cb0f340c1a 80 #define SSD1306_SET_COLUMN_ADDR 0x21
poushen 0:b5cb0f340c1a 81 #define SSD1306_SET_PAGE_ADDR 0x22
poushen 0:b5cb0f340c1a 82 // Hardware Configuration Commands
poushen 0:b5cb0f340c1a 83 #define SSD1306_SET_START_LINE 0x40
poushen 0:b5cb0f340c1a 84 #define SSD1306_SET_SEGMENT_REMAP 0xA0
poushen 0:b5cb0f340c1a 85 #define SSD1306_SET_MULTIPLEX_RATIO 0xA8
poushen 0:b5cb0f340c1a 86 #define SSD1306_COM_SCAN_DIR_INC 0xC0
poushen 0:b5cb0f340c1a 87 #define SSD1306_COM_SCAN_DIR_DEC 0xC8
poushen 0:b5cb0f340c1a 88 #define SSD1306_SET_DISPLAY_OFFSET 0xD3
poushen 0:b5cb0f340c1a 89 #define SSD1306_SET_COM_PINS 0xDA
poushen 0:b5cb0f340c1a 90 #define SSD1306_CHARGE_PUMP 0x8D
poushen 0:b5cb0f340c1a 91 // Timing & Driving Scheme Setting Commands
poushen 0:b5cb0f340c1a 92 #define SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO 0xD5
poushen 0:b5cb0f340c1a 93 #define SSD1306_SET_PRECHARGE_PERIOD 0xD9
poushen 0:b5cb0f340c1a 94 #define SSD1306_SET_VCOM_DESELECT 0xDB
poushen 0:b5cb0f340c1a 95
poushen 0:b5cb0f340c1a 96 /** oled_i2c Class Library
poushen 0:b5cb0f340c1a 97 * to provide access interface to OLED screen for mbed
poushen 0:b5cb0f340c1a 98 *
poushen 0:b5cb0f340c1a 99 * Example:
poushen 0:b5cb0f340c1a 100 * @code
poushen 0:b5cb0f340c1a 101 * #include "mbed.h"
poushen 0:b5cb0f340c1a 102 * #include "oled_i2c.h"
poushen 0:b5cb0f340c1a 103 * #include "oled_font.h"
poushen 0:b5cb0f340c1a 104 *
poushen 0:b5cb0f340c1a 105 * // make oled_i2c instance using I2C object.
poushen 0:b5cb0f340c1a 106 * // with default slave address 0x78 (0x3C in 7bit format)
poushen 0:b5cb0f340c1a 107 * // test ok with SSD1306 I2C 128*64 oled screen
poushen 0:b5cb0f340c1a 108 * I2C i2c(dp5,dp27); // for LPC1114 or LPC1115
poushen 0:b5cb0f340c1a 109 * oled_i2c oled(i2c);
poushen 0:b5cb0f340c1a 110 *
poushen 0:b5cb0f340c1a 111 * int main()
poushen 0:b5cb0f340c1a 112 * {
poushen 0:b5cb0f340c1a 113 * i2c.frequency(1000 * 1000);
poushen 0:b5cb0f340c1a 114 * oled.init_oled();
poushen 0:b5cb0f340c1a 115 *
poushen 0:b5cb0f340c1a 116 * oled.setFont(BigNumbers);
poushen 0:b5cb0f340c1a 117 * //oled.print("27", 20, 10);
poushen 0:b5cb0f340c1a 118 * //oled.printNumI(27, 0, 0);
poushen 0:b5cb0f340c1a 119 * oled.printNumF(27.45, 2, 0, 0);
poushen 0:b5cb0f340c1a 120 * oled.setFont(SmallFont);
poushen 0:b5cb0f340c1a 121 * oled.print("~c", 60, 30);
poushen 0:b5cb0f340c1a 122 * oled.update();
poushen 0:b5cb0f340c1a 123 *
poushen 0:b5cb0f340c1a 124 * while(1);
poushen 0:b5cb0f340c1a 125 * }
poushen 0:b5cb0f340c1a 126 * @endcode
poushen 0:b5cb0f340c1a 127 */
poushen 0:b5cb0f340c1a 128 class oled_i2c
poushen 0:b5cb0f340c1a 129 {
poushen 0:b5cb0f340c1a 130 public:
poushen 0:b5cb0f340c1a 131 /** Create a oled_i2c instance connected to specified I2C pins with specified address
poushen 0:b5cb0f340c1a 132 *
poushen 0:b5cb0f340c1a 133 * @param i2c_obj I2C object (instance)
poushen 0:b5cb0f340c1a 134 * @param address (option) I2C-bus slave address
poushen 0:b5cb0f340c1a 135 */
poushen 0:b5cb0f340c1a 136 oled_i2c(I2C &i2c_obj, char address = SSD1306_ADDR);
poushen 0:b5cb0f340c1a 137
poushen 0:b5cb0f340c1a 138 /** Destractor */
poushen 0:b5cb0f340c1a 139 ~oled_i2c();
poushen 0:b5cb0f340c1a 140
poushen 0:b5cb0f340c1a 141 /** Initialization */
poushen 0:b5cb0f340c1a 142 void init(void);
poushen 0:b5cb0f340c1a 143
poushen 0:b5cb0f340c1a 144 /** send init command to SSD1306 */
poushen 0:b5cb0f340c1a 145 void init_oled(void);
poushen 0:b5cb0f340c1a 146
poushen 0:b5cb0f340c1a 147 /** update screen buffer */
poushen 0:b5cb0f340c1a 148 void update(void);
poushen 0:b5cb0f340c1a 149
poushen 0:b5cb0f340c1a 150 /** clear screen */
poushen 0:b5cb0f340c1a 151 void clrScr(void);
poushen 0:b5cb0f340c1a 152
poushen 0:b5cb0f340c1a 153 /** fill screen */
poushen 0:b5cb0f340c1a 154 void fillScr(void);
poushen 0:b5cb0f340c1a 155
poushen 0:b5cb0f340c1a 156 /** set pixel on coordinate x, y
poushen 0:b5cb0f340c1a 157 *
poushen 0:b5cb0f340c1a 158 * @param x coordinate x (column)
poushen 0:b5cb0f340c1a 159 * @param y coordinate y (row)
poushen 0:b5cb0f340c1a 160 */
poushen 0:b5cb0f340c1a 161 void setPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 162
poushen 0:b5cb0f340c1a 163 /** clear 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 clrPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 169
poushen 0:b5cb0f340c1a 170 /** set font type
poushen 0:b5cb0f340c1a 171 *
poushen 0:b5cb0f340c1a 172 * @param font the font to print
poushen 0:b5cb0f340c1a 173 */
poushen 0:b5cb0f340c1a 174 void setFont(const uint8_t* font);
poushen 0:b5cb0f340c1a 175
poushen 0:b5cb0f340c1a 176 /** print character to coordinate x, y
poushen 0:b5cb0f340c1a 177 *
poushen 0:b5cb0f340c1a 178 * @param c the character to print
poushen 0:b5cb0f340c1a 179 * @param x coordinate x
poushen 0:b5cb0f340c1a 180 * @param y coordinate y
poushen 0:b5cb0f340c1a 181 */
poushen 0:b5cb0f340c1a 182 void print_char(unsigned char c, int x, int y);
poushen 0:b5cb0f340c1a 183
poushen 0:b5cb0f340c1a 184 /** print string to coordinate x, y
poushen 0:b5cb0f340c1a 185 *
poushen 0:b5cb0f340c1a 186 * @param st the string to print
poushen 0:b5cb0f340c1a 187 * @param x coordinate x
poushen 0:b5cb0f340c1a 188 * @param y coordinate y
poushen 0:b5cb0f340c1a 189 */
poushen 0:b5cb0f340c1a 190 void print(char* st, int x, int y);
poushen 0:b5cb0f340c1a 191
poushen 0:b5cb0f340c1a 192 /** print integer number to coordinate x, y
poushen 0:b5cb0f340c1a 193 *
poushen 0:b5cb0f340c1a 194 * @param num the integer to be print
poushen 0:b5cb0f340c1a 195 * @param x coordinate x
poushen 0:b5cb0f340c1a 196 * @param y coordinate y
poushen 0:b5cb0f340c1a 197 * @param length the length of integer to be print
poushen 0:b5cb0f340c1a 198 * @param filler the prefix character
poushen 0:b5cb0f340c1a 199 */
poushen 0:b5cb0f340c1a 200 void printNumI(long num, int x, int y, int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 201
poushen 0:b5cb0f340c1a 202 /** print float number to coordinate x, y
poushen 0:b5cb0f340c1a 203 *
poushen 0:b5cb0f340c1a 204 * @param num the float number to be print
poushen 0:b5cb0f340c1a 205 * @param dec the numbers of digits after point
poushen 0:b5cb0f340c1a 206 * @param x coordinate x
poushen 0:b5cb0f340c1a 207 * @param y coordinate y
poushen 0:b5cb0f340c1a 208 * @param divider should be the point (.)
poushen 0:b5cb0f340c1a 209 * @param length the length of float number to be print
poushen 0:b5cb0f340c1a 210 * @param filler the prefix character
poushen 0:b5cb0f340c1a 211 */
poushen 0:b5cb0f340c1a 212 void printNumF(double num, uint8_t dec, int x, int y, char divider='.', int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 213
poushen 0:b5cb0f340c1a 214
poushen 0:b5cb0f340c1a 215 private:
poushen 0:b5cb0f340c1a 216 I2C &i2c;
poushen 0:b5cb0f340c1a 217 char adr;
poushen 0:b5cb0f340c1a 218 eeprom *epm_p;
poushen 0:b5cb0f340c1a 219 eeprom &epm;
poushen 0:b5cb0f340c1a 220 struct _current_font
poushen 0:b5cb0f340c1a 221 {
poushen 0:b5cb0f340c1a 222 const uint8_t* font;
poushen 0:b5cb0f340c1a 223 uint8_t x_size;
poushen 0:b5cb0f340c1a 224 uint8_t y_size;
poushen 0:b5cb0f340c1a 225 uint8_t offset;
poushen 0:b5cb0f340c1a 226 uint8_t numchars;
poushen 0:b5cb0f340c1a 227 } cfont;
poushen 0:b5cb0f340c1a 228 uint8_t scrbuf[SSD1306_MEM_SIZE];
poushen 3:909dd63644b4 229
poushen 3:909dd63644b4 230 /** send SSD1306 command
poushen 3:909dd63644b4 231 *
poushen 3:909dd63644b4 232 * @param a the SSD1306 command byte to send out
poushen 3:909dd63644b4 233 */
poushen 3:909dd63644b4 234 void comm_out(uint8_t a);
poushen 3:909dd63644b4 235
poushen 3:909dd63644b4 236 /** send many SSD1306 commands in one I2C transaction
poushen 3:909dd63644b4 237 *
poushen 3:909dd63644b4 238 * @param pPage pointer to command buffer
poushen 3:909dd63644b4 239 * @param size the command buffer size
poushen 3:909dd63644b4 240 */
poushen 3:909dd63644b4 241 void comm_page_out(uint8_t* pPage, uint8_t size);
poushen 3:909dd63644b4 242
poushen 3:909dd63644b4 243 /** send SSD1306 data
poushen 3:909dd63644b4 244 *
poushen 3:909dd63644b4 245 * @param a the SSD1306 data byte to send out
poushen 3:909dd63644b4 246 */
poushen 3:909dd63644b4 247 void data_out(uint8_t a);
poushen 3:909dd63644b4 248
poushen 3:909dd63644b4 249 /** send many SSD1306 data bytes in one I2c transaction
poushen 3:909dd63644b4 250 *
poushen 3:909dd63644b4 251 * @param pPage pointer to data buffer
poushen 3:909dd63644b4 252 * @param size the data buffer size
poushen 3:909dd63644b4 253 */
poushen 3:909dd63644b4 254 void data_page_out(uint8_t* pPage, uint8_t size);
poushen 3:909dd63644b4 255
poushen 3:909dd63644b4 256 /** make all pixel on or off acording to screen buffer */
poushen 3:909dd63644b4 257 void pixel_on(void);
poushen 3:909dd63644b4 258
poushen 0:b5cb0f340c1a 259 };
poushen 0:b5cb0f340c1a 260
poushen 0:b5cb0f340c1a 261 #endif