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
porting printNumI, printNumF, modify sample code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
poushen 0:b5cb0f340c1a 1 #include "oled_i2c.h"
poushen 0:b5cb0f340c1a 2
poushen 0:b5cb0f340c1a 3 oled_i2c::oled_i2c(I2C &i2c_obj, char address)
poushen 0:b5cb0f340c1a 4 : i2c(i2c_obj), adr(address), epm_p(new eeprom(i2c, address)), epm(*epm_p)
poushen 0:b5cb0f340c1a 5 {
poushen 0:b5cb0f340c1a 6 init();
poushen 0:b5cb0f340c1a 7 }
poushen 0:b5cb0f340c1a 8
poushen 0:b5cb0f340c1a 9 oled_i2c::~oled_i2c()
poushen 0:b5cb0f340c1a 10 {
poushen 0:b5cb0f340c1a 11 if(NULL!=epm_p)
poushen 0:b5cb0f340c1a 12 delete epm_p;
poushen 0:b5cb0f340c1a 13 }
poushen 0:b5cb0f340c1a 14
poushen 0:b5cb0f340c1a 15 void oled_i2c::init(void)
poushen 0:b5cb0f340c1a 16 {
poushen 0:b5cb0f340c1a 17 }
poushen 0:b5cb0f340c1a 18
poushen 0:b5cb0f340c1a 19 void oled_i2c::comm_out(uint8_t a)
poushen 0:b5cb0f340c1a 20 {
poushen 0:b5cb0f340c1a 21 epm.byte_write(SSD1306_COMMAND, ONE_BYTE_ADDRESS, a);
poushen 0:b5cb0f340c1a 22 }
poushen 0:b5cb0f340c1a 23
poushen 0:b5cb0f340c1a 24 void oled_i2c::comm_page_out(uint8_t* pPage, uint8_t size)
poushen 0:b5cb0f340c1a 25 {
poushen 0:b5cb0f340c1a 26 epm.page_write(SSD1306_COMMAND, ONE_BYTE_ADDRESS, pPage, size);
poushen 0:b5cb0f340c1a 27 }
poushen 0:b5cb0f340c1a 28
poushen 0:b5cb0f340c1a 29 void oled_i2c::data_out(uint8_t a)
poushen 0:b5cb0f340c1a 30 {
poushen 0:b5cb0f340c1a 31 epm.byte_write(SSD1306_DATA_CONTINUE, ONE_BYTE_ADDRESS, a);
poushen 0:b5cb0f340c1a 32 }
poushen 0:b5cb0f340c1a 33
poushen 0:b5cb0f340c1a 34 void oled_i2c::data_page_out(uint8_t* pPage, uint8_t size)
poushen 0:b5cb0f340c1a 35 {
poushen 0:b5cb0f340c1a 36 epm.page_write(SSD1306_DATA_CONTINUE, ONE_BYTE_ADDRESS, pPage, size);
poushen 0:b5cb0f340c1a 37 }
poushen 0:b5cb0f340c1a 38
poushen 0:b5cb0f340c1a 39 void oled_i2c::init_oled(void)
poushen 0:b5cb0f340c1a 40 {
poushen 0:b5cb0f340c1a 41 uint8_t comm[33] = {0, 0, 0, SSD1306_DISPLAY_OFF,
poushen 0:b5cb0f340c1a 42 SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO,
poushen 0:b5cb0f340c1a 43 0x80,
poushen 0:b5cb0f340c1a 44 SSD1306_SET_MULTIPLEX_RATIO,
poushen 0:b5cb0f340c1a 45 0x3F,
poushen 0:b5cb0f340c1a 46 SSD1306_SET_DISPLAY_OFFSET,
poushen 0:b5cb0f340c1a 47 0x00,
poushen 0:b5cb0f340c1a 48 SSD1306_SET_START_LINE,
poushen 0:b5cb0f340c1a 49 SSD1306_CHARGE_PUMP,
poushen 0:b5cb0f340c1a 50 0x14,
poushen 0:b5cb0f340c1a 51 SSD1306_MEMORY_ADDR_MODE,
poushen 0:b5cb0f340c1a 52 0x00, // Horizontal Addressing Mode
poushen 0:b5cb0f340c1a 53 // --------
poushen 0:b5cb0f340c1a 54 0x21, // Set Column Address
poushen 0:b5cb0f340c1a 55 0x00, //28
poushen 0:b5cb0f340c1a 56 127,
poushen 0:b5cb0f340c1a 57 0x22, // Set Page Address
poushen 0:b5cb0f340c1a 58 0,
poushen 0:b5cb0f340c1a 59 7,
poushen 0:b5cb0f340c1a 60 // --------
poushen 0:b5cb0f340c1a 61 SSD1306_SET_SEGMENT_REMAP | 0x1,
poushen 0:b5cb0f340c1a 62 SSD1306_COM_SCAN_DIR_DEC,
poushen 0:b5cb0f340c1a 63 SSD1306_SET_COM_PINS,
poushen 0:b5cb0f340c1a 64 0x12,
poushen 0:b5cb0f340c1a 65 // 0xAD,
poushen 0:b5cb0f340c1a 66 // 0x30,
poushen 0:b5cb0f340c1a 67 SSD1306_SET_CONTRAST_CONTROL,
poushen 0:b5cb0f340c1a 68 0xCF,
poushen 0:b5cb0f340c1a 69 SSD1306_SET_PRECHARGE_PERIOD,
poushen 0:b5cb0f340c1a 70 0xF1,
poushen 0:b5cb0f340c1a 71 SSD1306_SET_VCOM_DESELECT,
poushen 0:b5cb0f340c1a 72 0x40,
poushen 0:b5cb0f340c1a 73 SSD1306_DISPLAY_ALL_ON_RESUME,
poushen 0:b5cb0f340c1a 74 SSD1306_NORMAL_DISPLAY,};
poushen 0:b5cb0f340c1a 75 comm_page_out(comm, 30);
poushen 0:b5cb0f340c1a 76
poushen 0:b5cb0f340c1a 77 for(int i=0; i<8; i++) {
poushen 0:b5cb0f340c1a 78 uint8_t data[6] = {0, 0, 0, 0xb0+i, 0x00, 0x00};
poushen 0:b5cb0f340c1a 79 comm_page_out(data, 3);
poushen 0:b5cb0f340c1a 80
poushen 0:b5cb0f340c1a 81 for(int j=0; j<128; j++)
poushen 0:b5cb0f340c1a 82 data_out(0x00);
poushen 0:b5cb0f340c1a 83 }
poushen 0:b5cb0f340c1a 84
poushen 0:b5cb0f340c1a 85 comm_out(SSD1306_DISPLAY_ON); //Set Display On
poushen 0:b5cb0f340c1a 86 }
poushen 0:b5cb0f340c1a 87
poushen 0:b5cb0f340c1a 88 void oled_i2c::pixel_on(void)
poushen 0:b5cb0f340c1a 89 {
poushen 0:b5cb0f340c1a 90 for(int i=0; i<5; i++)
poushen 0:b5cb0f340c1a 91 {
poushen 0:b5cb0f340c1a 92 uint8_t data[6] = {0, 0, 0, 0xb2+i, 0x0c, 0x11};
poushen 0:b5cb0f340c1a 93 comm_page_out(data, 3);
poushen 0:b5cb0f340c1a 94
poushen 0:b5cb0f340c1a 95 //data_page_out(&scrbuf[i*SSD1306_WIDTH], 30);
poushen 0:b5cb0f340c1a 96 //data_page_out(&scrbuf[i*SSD1306_WIDTH + 30], 30);
poushen 0:b5cb0f340c1a 97 //data_page_out(&scrbuf[i*SSD1306_WIDTH + 60], 12);
poushen 0:b5cb0f340c1a 98 for (int j=0; j<72; j++) {
poushen 0:b5cb0f340c1a 99 data_out(scrbuf[i*SSD1306_WIDTH + j]);
poushen 0:b5cb0f340c1a 100 }
poushen 0:b5cb0f340c1a 101 }
poushen 0:b5cb0f340c1a 102 }
poushen 0:b5cb0f340c1a 103
poushen 0:b5cb0f340c1a 104 void oled_i2c::update(void)
poushen 0:b5cb0f340c1a 105 {
poushen 0:b5cb0f340c1a 106 pixel_on();
poushen 0:b5cb0f340c1a 107 }
poushen 0:b5cb0f340c1a 108
poushen 0:b5cb0f340c1a 109 void oled_i2c::clrScr()
poushen 0:b5cb0f340c1a 110 {
poushen 0:b5cb0f340c1a 111 memset(scrbuf, 0, SSD1306_MEM_SIZE);
poushen 0:b5cb0f340c1a 112 }
poushen 0:b5cb0f340c1a 113
poushen 0:b5cb0f340c1a 114 void oled_i2c::fillScr()
poushen 0:b5cb0f340c1a 115 {
poushen 0:b5cb0f340c1a 116 memset(scrbuf, 0xFF, SSD1306_MEM_SIZE);
poushen 0:b5cb0f340c1a 117 }
poushen 0:b5cb0f340c1a 118
poushen 0:b5cb0f340c1a 119 void oled_i2c::setPixel(uint16_t x, uint16_t y)
poushen 0:b5cb0f340c1a 120 {
poushen 0:b5cb0f340c1a 121 int by, bi;
poushen 0:b5cb0f340c1a 122
poushen 0:b5cb0f340c1a 123 if ((x<SSD1306_WIDTH) && (y<SSD1306_LENGTH))
poushen 0:b5cb0f340c1a 124 {
poushen 0:b5cb0f340c1a 125 by = ((y/8)*SSD1306_WIDTH) + x;
poushen 0:b5cb0f340c1a 126 bi = y % 8;
poushen 0:b5cb0f340c1a 127
poushen 0:b5cb0f340c1a 128 scrbuf[by] = scrbuf[by] | (1<<bi);
poushen 0:b5cb0f340c1a 129 }
poushen 0:b5cb0f340c1a 130 }
poushen 0:b5cb0f340c1a 131
poushen 0:b5cb0f340c1a 132 void oled_i2c::clrPixel(uint16_t x, uint16_t y)
poushen 0:b5cb0f340c1a 133 {
poushen 0:b5cb0f340c1a 134 int by, bi;
poushen 0:b5cb0f340c1a 135
poushen 0:b5cb0f340c1a 136 if ((x<SSD1306_WIDTH) && (y<SSD1306_LENGTH))
poushen 0:b5cb0f340c1a 137 {
poushen 0:b5cb0f340c1a 138 by = ((y/8)*SSD1306_WIDTH) + x;
poushen 0:b5cb0f340c1a 139 bi = y % 8;
poushen 0:b5cb0f340c1a 140
poushen 0:b5cb0f340c1a 141 scrbuf[by] = scrbuf[by] & ~(1<<bi);
poushen 0:b5cb0f340c1a 142 }
poushen 0:b5cb0f340c1a 143 }
poushen 0:b5cb0f340c1a 144
poushen 0:b5cb0f340c1a 145 void oled_i2c::setFont(const uint8_t* font)
poushen 0:b5cb0f340c1a 146 {
poushen 0:b5cb0f340c1a 147 cfont.font = font;
poushen 0:b5cb0f340c1a 148 cfont.x_size = font[0];
poushen 0:b5cb0f340c1a 149 cfont.y_size = font[1];
poushen 0:b5cb0f340c1a 150 cfont.offset = font[2];
poushen 0:b5cb0f340c1a 151 cfont.numchars = font[3];
poushen 0:b5cb0f340c1a 152 }
poushen 0:b5cb0f340c1a 153
poushen 0:b5cb0f340c1a 154 void oled_i2c::print_char(unsigned char c, int x, int y)
poushen 0:b5cb0f340c1a 155 {
poushen 0:b5cb0f340c1a 156 if ((cfont.y_size % 8) == 0)
poushen 0:b5cb0f340c1a 157 {
poushen 0:b5cb0f340c1a 158 int font_idx = ((c - cfont.offset)*(cfont.x_size * (cfont.y_size/8)))+4;
poushen 0:b5cb0f340c1a 159 for (int rowcnt=0; rowcnt<(cfont.y_size/8); rowcnt++)
poushen 0:b5cb0f340c1a 160 {
poushen 0:b5cb0f340c1a 161 for(int cnt=0; cnt<cfont.x_size; cnt++)
poushen 0:b5cb0f340c1a 162 {
poushen 0:b5cb0f340c1a 163 for (int b=0; b<8; b++)
poushen 0:b5cb0f340c1a 164 if ((cfont.font[font_idx+cnt+(rowcnt*cfont.x_size)] & (1<<b))!=0)
poushen 0:b5cb0f340c1a 165 //if (cfont.inverted==0)
poushen 0:b5cb0f340c1a 166 setPixel(x+cnt, y+(rowcnt*8)+b);
poushen 0:b5cb0f340c1a 167 //else
poushen 0:b5cb0f340c1a 168 // oled_clrPixel(x+cnt, y+(rowcnt*8)+b);
poushen 0:b5cb0f340c1a 169 else
poushen 0:b5cb0f340c1a 170 //if (cfont.inverted==0)
poushen 0:b5cb0f340c1a 171 clrPixel(x+cnt, y+(rowcnt*8)+b);
poushen 0:b5cb0f340c1a 172 //else
poushen 0:b5cb0f340c1a 173 // oled_setPixel(x+cnt, y+(rowcnt*8)+b);
poushen 0:b5cb0f340c1a 174 }
poushen 0:b5cb0f340c1a 175 }
poushen 0:b5cb0f340c1a 176 }
poushen 0:b5cb0f340c1a 177 else
poushen 0:b5cb0f340c1a 178 {
poushen 0:b5cb0f340c1a 179 int font_idx = ((c - cfont.offset)*((cfont.x_size*cfont.y_size/8)))+4;
poushen 0:b5cb0f340c1a 180 int cbyte=cfont.font[font_idx];
poushen 0:b5cb0f340c1a 181 int cbit=7;
poushen 0:b5cb0f340c1a 182 for (int cx=0; cx<cfont.x_size; cx++)
poushen 0:b5cb0f340c1a 183 {
poushen 0:b5cb0f340c1a 184 for (int cy=0; cy<cfont.y_size; cy++)
poushen 0:b5cb0f340c1a 185 {
poushen 0:b5cb0f340c1a 186 if ((cbyte & (1<<cbit)) != 0)
poushen 0:b5cb0f340c1a 187 //if (cfont.inverted==0)
poushen 0:b5cb0f340c1a 188 setPixel(x+cx, y+cy);
poushen 0:b5cb0f340c1a 189 //else
poushen 0:b5cb0f340c1a 190 // oled_clrPixel(x+cx, y+cy);
poushen 0:b5cb0f340c1a 191 else
poushen 0:b5cb0f340c1a 192 //if (cfont.inverted==0)
poushen 0:b5cb0f340c1a 193 clrPixel(x+cx, y+cy);
poushen 0:b5cb0f340c1a 194 //else
poushen 0:b5cb0f340c1a 195 // oled_setPixel(x+cx, y+cy);
poushen 0:b5cb0f340c1a 196 cbit--;
poushen 0:b5cb0f340c1a 197 if (cbit<0)
poushen 0:b5cb0f340c1a 198 {
poushen 0:b5cb0f340c1a 199 cbit=7;
poushen 0:b5cb0f340c1a 200 font_idx++;
poushen 0:b5cb0f340c1a 201 cbyte=cfont.font[font_idx];
poushen 0:b5cb0f340c1a 202 }
poushen 0:b5cb0f340c1a 203 }
poushen 0:b5cb0f340c1a 204 }
poushen 0:b5cb0f340c1a 205 }
poushen 0:b5cb0f340c1a 206 }
poushen 0:b5cb0f340c1a 207
poushen 0:b5cb0f340c1a 208 void oled_i2c::print(char *st, int x, int y)
poushen 0:b5cb0f340c1a 209 {
poushen 0:b5cb0f340c1a 210 //unsigned char ch;
poushen 0:b5cb0f340c1a 211 int stl;
poushen 0:b5cb0f340c1a 212
poushen 0:b5cb0f340c1a 213 stl = strlen(st);
poushen 0:b5cb0f340c1a 214 if (x == RIGHT)
poushen 0:b5cb0f340c1a 215 x = SSD1306_WIDTH - (stl * cfont.x_size);
poushen 0:b5cb0f340c1a 216 if (x == CENTER)
poushen 0:b5cb0f340c1a 217 x = (SSD1306_WIDTH - (stl * cfont.x_size))/2;
poushen 0:b5cb0f340c1a 218
poushen 0:b5cb0f340c1a 219 for (int cnt=0; cnt<stl; cnt++)
poushen 0:b5cb0f340c1a 220 print_char(*st++, x + (cnt*(cfont.x_size)), y);
poushen 0:b5cb0f340c1a 221 }
poushen 0:b5cb0f340c1a 222
poushen 0:b5cb0f340c1a 223 void oled_i2c::printNumI(long num, int x, int y, int length, char filler)
poushen 0:b5cb0f340c1a 224 {
poushen 0:b5cb0f340c1a 225 char buf[25];
poushen 0:b5cb0f340c1a 226 char st[27];
poushen 0:b5cb0f340c1a 227 bool neg=false;
poushen 0:b5cb0f340c1a 228 int c=0, f=0;
poushen 0:b5cb0f340c1a 229
poushen 0:b5cb0f340c1a 230 if (num==0)
poushen 0:b5cb0f340c1a 231 {
poushen 0:b5cb0f340c1a 232 if (length!=0)
poushen 0:b5cb0f340c1a 233 {
poushen 0:b5cb0f340c1a 234 for (c=0; c<(length-1); c++)
poushen 0:b5cb0f340c1a 235 st[c]=filler;
poushen 0:b5cb0f340c1a 236 st[c]=48;
poushen 0:b5cb0f340c1a 237 st[c+1]=0;
poushen 0:b5cb0f340c1a 238 }
poushen 0:b5cb0f340c1a 239 else
poushen 0:b5cb0f340c1a 240 {
poushen 0:b5cb0f340c1a 241 st[0]=48;
poushen 0:b5cb0f340c1a 242 st[1]=0;
poushen 0:b5cb0f340c1a 243 }
poushen 0:b5cb0f340c1a 244 }
poushen 0:b5cb0f340c1a 245 else
poushen 0:b5cb0f340c1a 246 {
poushen 0:b5cb0f340c1a 247 if (num<0)
poushen 0:b5cb0f340c1a 248 {
poushen 0:b5cb0f340c1a 249 neg=true;
poushen 0:b5cb0f340c1a 250 num=-num;
poushen 0:b5cb0f340c1a 251 }
poushen 0:b5cb0f340c1a 252
poushen 0:b5cb0f340c1a 253 while (num>0)
poushen 0:b5cb0f340c1a 254 {
poushen 0:b5cb0f340c1a 255 buf[c]=48+(num % 10);
poushen 0:b5cb0f340c1a 256 c++;
poushen 0:b5cb0f340c1a 257 num=(num-(num % 10))/10;
poushen 0:b5cb0f340c1a 258 }
poushen 0:b5cb0f340c1a 259 buf[c]=0;
poushen 0:b5cb0f340c1a 260
poushen 0:b5cb0f340c1a 261 if (neg)
poushen 0:b5cb0f340c1a 262 {
poushen 0:b5cb0f340c1a 263 st[0]=45;
poushen 0:b5cb0f340c1a 264 }
poushen 0:b5cb0f340c1a 265
poushen 0:b5cb0f340c1a 266 if (length>(c+neg))
poushen 0:b5cb0f340c1a 267 {
poushen 0:b5cb0f340c1a 268 for (int i=0; i<(length-c-neg); i++)
poushen 0:b5cb0f340c1a 269 {
poushen 0:b5cb0f340c1a 270 st[i+neg]=filler;
poushen 0:b5cb0f340c1a 271 f++;
poushen 0:b5cb0f340c1a 272 }
poushen 0:b5cb0f340c1a 273 }
poushen 0:b5cb0f340c1a 274
poushen 0:b5cb0f340c1a 275 for (int i=0; i<c; i++)
poushen 0:b5cb0f340c1a 276 {
poushen 0:b5cb0f340c1a 277 st[i+neg+f]=buf[c-i-1];
poushen 0:b5cb0f340c1a 278 }
poushen 0:b5cb0f340c1a 279 st[c+neg+f]=0;
poushen 0:b5cb0f340c1a 280 }
poushen 0:b5cb0f340c1a 281
poushen 0:b5cb0f340c1a 282 print(st,x,y);
poushen 0:b5cb0f340c1a 283 }
poushen 0:b5cb0f340c1a 284
poushen 0:b5cb0f340c1a 285 void oled_i2c::printNumF(double num, uint8_t dec, int x, int y, char divider, int length, char filler)
poushen 0:b5cb0f340c1a 286 {
poushen 0:b5cb0f340c1a 287 char st[27];
poushen 0:b5cb0f340c1a 288 bool neg=false;
poushen 0:b5cb0f340c1a 289
poushen 0:b5cb0f340c1a 290 if (num<0)
poushen 0:b5cb0f340c1a 291 neg = true;
poushen 0:b5cb0f340c1a 292
poushen 0:b5cb0f340c1a 293 //_convert_float(st, num, length, dec);
poushen 0:b5cb0f340c1a 294 //ftoa(num, st, dec);
poushen 0:b5cb0f340c1a 295 sprintf(st, "%.*f", dec, num);
poushen 0:b5cb0f340c1a 296 if (divider != '.')
poushen 0:b5cb0f340c1a 297 {
poushen 0:b5cb0f340c1a 298 for (int i=0; i<sizeof(st); i++)
poushen 0:b5cb0f340c1a 299 if (st[i]=='.')
poushen 0:b5cb0f340c1a 300 st[i]=divider;
poushen 0:b5cb0f340c1a 301 }
poushen 0:b5cb0f340c1a 302
poushen 0:b5cb0f340c1a 303 if (filler != ' ')
poushen 0:b5cb0f340c1a 304 {
poushen 0:b5cb0f340c1a 305 if (neg)
poushen 0:b5cb0f340c1a 306 {
poushen 0:b5cb0f340c1a 307 st[0]='-';
poushen 0:b5cb0f340c1a 308 for (int i=1; i<sizeof(st); i++)
poushen 0:b5cb0f340c1a 309 if ((st[i]==' ') || (st[i]=='-'))
poushen 0:b5cb0f340c1a 310 st[i]=filler;
poushen 0:b5cb0f340c1a 311 }
poushen 0:b5cb0f340c1a 312 else
poushen 0:b5cb0f340c1a 313 {
poushen 0:b5cb0f340c1a 314 for (int i=0; i<sizeof(st); i++)
poushen 0:b5cb0f340c1a 315 if (st[i]==' ')
poushen 0:b5cb0f340c1a 316 st[i]=filler;
poushen 0:b5cb0f340c1a 317 }
poushen 0:b5cb0f340c1a 318 }
poushen 0:b5cb0f340c1a 319
poushen 0:b5cb0f340c1a 320 print(st,x,y);
poushen 0:b5cb0f340c1a 321 }