a partial porting OLED_I2C library

Dependents:   ou_mbed_oled ou_mbed_tmp102

Committer:
poushen
Date:
Tue Jun 19 05:49:09 2018 +0000
Revision:
2:e64ae00d801c
Parent:
1:c928953247eb
Child:
3:909dd63644b4
add license notes

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 SSD1306 command
poushen 0:b5cb0f340c1a 145 *
poushen 0:b5cb0f340c1a 146 * @param a the SSD1306 command byte to send out
poushen 0:b5cb0f340c1a 147 */
poushen 0:b5cb0f340c1a 148 void comm_out(uint8_t a);
poushen 0:b5cb0f340c1a 149
poushen 0:b5cb0f340c1a 150 /** send many SSD1306 commands in one I2C transaction
poushen 0:b5cb0f340c1a 151 *
poushen 0:b5cb0f340c1a 152 * @param pPage pointer to command buffer
poushen 0:b5cb0f340c1a 153 * @param size the command buffer size
poushen 0:b5cb0f340c1a 154 */
poushen 0:b5cb0f340c1a 155 void comm_page_out(uint8_t* pPage, uint8_t size);
poushen 0:b5cb0f340c1a 156
poushen 0:b5cb0f340c1a 157 /** send SSD1306 data
poushen 0:b5cb0f340c1a 158 *
poushen 0:b5cb0f340c1a 159 * @param a the SSD1306 data byte to send out
poushen 0:b5cb0f340c1a 160 */
poushen 0:b5cb0f340c1a 161 void data_out(uint8_t a);
poushen 0:b5cb0f340c1a 162
poushen 0:b5cb0f340c1a 163 /** send many SSD1306 data bytes in one I2c transaction
poushen 0:b5cb0f340c1a 164 *
poushen 0:b5cb0f340c1a 165 * @param pPage pointer to data buffer
poushen 0:b5cb0f340c1a 166 * @param size the data buffer size
poushen 0:b5cb0f340c1a 167 */
poushen 0:b5cb0f340c1a 168 void data_page_out(uint8_t* pPage, uint8_t size);
poushen 0:b5cb0f340c1a 169
poushen 0:b5cb0f340c1a 170 /** send init command to SSD1306 */
poushen 0:b5cb0f340c1a 171 void init_oled(void);
poushen 0:b5cb0f340c1a 172
poushen 0:b5cb0f340c1a 173 /** make all pixel on or off acording to screen buffer */
poushen 0:b5cb0f340c1a 174 void pixel_on(void);
poushen 0:b5cb0f340c1a 175
poushen 0:b5cb0f340c1a 176 /** update screen buffer */
poushen 0:b5cb0f340c1a 177 void update(void);
poushen 0:b5cb0f340c1a 178
poushen 0:b5cb0f340c1a 179 /** clear screen */
poushen 0:b5cb0f340c1a 180 void clrScr(void);
poushen 0:b5cb0f340c1a 181
poushen 0:b5cb0f340c1a 182 /** fill screen */
poushen 0:b5cb0f340c1a 183 void fillScr(void);
poushen 0:b5cb0f340c1a 184
poushen 0:b5cb0f340c1a 185 /** set pixel on coordinate x, y
poushen 0:b5cb0f340c1a 186 *
poushen 0:b5cb0f340c1a 187 * @param x coordinate x (column)
poushen 0:b5cb0f340c1a 188 * @param y coordinate y (row)
poushen 0:b5cb0f340c1a 189 */
poushen 0:b5cb0f340c1a 190 void setPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 191
poushen 0:b5cb0f340c1a 192 /** clear pixel on coordinate x, y
poushen 0:b5cb0f340c1a 193 *
poushen 0:b5cb0f340c1a 194 * @param x coordinate x (column)
poushen 0:b5cb0f340c1a 195 * @param y coordinate y (row)
poushen 0:b5cb0f340c1a 196 */
poushen 0:b5cb0f340c1a 197 void clrPixel(uint16_t x, uint16_t y);
poushen 0:b5cb0f340c1a 198
poushen 0:b5cb0f340c1a 199 /** set font type
poushen 0:b5cb0f340c1a 200 *
poushen 0:b5cb0f340c1a 201 * @param font the font to print
poushen 0:b5cb0f340c1a 202 */
poushen 0:b5cb0f340c1a 203 void setFont(const uint8_t* font);
poushen 0:b5cb0f340c1a 204
poushen 0:b5cb0f340c1a 205 /** print character to coordinate x, y
poushen 0:b5cb0f340c1a 206 *
poushen 0:b5cb0f340c1a 207 * @param c the character to print
poushen 0:b5cb0f340c1a 208 * @param x coordinate x
poushen 0:b5cb0f340c1a 209 * @param y coordinate y
poushen 0:b5cb0f340c1a 210 */
poushen 0:b5cb0f340c1a 211 void print_char(unsigned char c, int x, int y);
poushen 0:b5cb0f340c1a 212
poushen 0:b5cb0f340c1a 213 /** print string to coordinate x, y
poushen 0:b5cb0f340c1a 214 *
poushen 0:b5cb0f340c1a 215 * @param st the string to print
poushen 0:b5cb0f340c1a 216 * @param x coordinate x
poushen 0:b5cb0f340c1a 217 * @param y coordinate y
poushen 0:b5cb0f340c1a 218 */
poushen 0:b5cb0f340c1a 219 void print(char* st, int x, int y);
poushen 0:b5cb0f340c1a 220
poushen 0:b5cb0f340c1a 221 /** print integer number to coordinate x, y
poushen 0:b5cb0f340c1a 222 *
poushen 0:b5cb0f340c1a 223 * @param num the integer to be print
poushen 0:b5cb0f340c1a 224 * @param x coordinate x
poushen 0:b5cb0f340c1a 225 * @param y coordinate y
poushen 0:b5cb0f340c1a 226 * @param length the length of integer to be print
poushen 0:b5cb0f340c1a 227 * @param filler the prefix character
poushen 0:b5cb0f340c1a 228 */
poushen 0:b5cb0f340c1a 229 void printNumI(long num, int x, int y, int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 230
poushen 0:b5cb0f340c1a 231 /** print float number to coordinate x, y
poushen 0:b5cb0f340c1a 232 *
poushen 0:b5cb0f340c1a 233 * @param num the float number to be print
poushen 0:b5cb0f340c1a 234 * @param dec the numbers of digits after point
poushen 0:b5cb0f340c1a 235 * @param x coordinate x
poushen 0:b5cb0f340c1a 236 * @param y coordinate y
poushen 0:b5cb0f340c1a 237 * @param divider should be the point (.)
poushen 0:b5cb0f340c1a 238 * @param length the length of float number to be print
poushen 0:b5cb0f340c1a 239 * @param filler the prefix character
poushen 0:b5cb0f340c1a 240 */
poushen 0:b5cb0f340c1a 241 void printNumF(double num, uint8_t dec, int x, int y, char divider='.', int length=0, char filler=' ');
poushen 0:b5cb0f340c1a 242
poushen 0:b5cb0f340c1a 243
poushen 0:b5cb0f340c1a 244 private:
poushen 0:b5cb0f340c1a 245 I2C &i2c;
poushen 0:b5cb0f340c1a 246 char adr;
poushen 0:b5cb0f340c1a 247 eeprom *epm_p;
poushen 0:b5cb0f340c1a 248 eeprom &epm;
poushen 0:b5cb0f340c1a 249 struct _current_font
poushen 0:b5cb0f340c1a 250 {
poushen 0:b5cb0f340c1a 251 const uint8_t* font;
poushen 0:b5cb0f340c1a 252 uint8_t x_size;
poushen 0:b5cb0f340c1a 253 uint8_t y_size;
poushen 0:b5cb0f340c1a 254 uint8_t offset;
poushen 0:b5cb0f340c1a 255 uint8_t numchars;
poushen 0:b5cb0f340c1a 256 } cfont;
poushen 0:b5cb0f340c1a 257 uint8_t scrbuf[SSD1306_MEM_SIZE];
poushen 0:b5cb0f340c1a 258 };
poushen 0:b5cb0f340c1a 259
poushen 0:b5cb0f340c1a 260 #endif