lib/unigraphic/SSD1322.cpp

changeset 5
f1c85c2500f2
child 6
8cb67d7afd79
equal deleted inserted replaced
4:219766126afb 5:f1c85c2500f2
1 /* mbed UniGraphic library - Device specific class
2 * Copyright (c) 2015 Peter Drescher
3 * Released under the MIT License: http://mbed.org/license/mit
4 */
5
6 #include "platform.h"
7 #include "SSD1322.h"
8
9 //////////////////////////////////////////////////////////////////////////////////
10 // display settings ///////////////////////////////////////////////////////
11 /////////////////////////////////////////////////////////////////////////
12
13 #define IC_X_SEGS 480 // SSD1322 SEG has range 0-479 (479-0 if MX=1), check your datasheet, important for the orientation
14 #define IC_Y_COMS 128 // SSD1322 COM has range 0-127 (127-0 if MY=1), check your datasheet, important for the orientation
15 #define BPP 4 // SSD1322 has 16 gray levels
16
17 #define SSD1322_CMD_ENABLE_GRAY_SCALE_TABLE 0x00
18 #define SSD1322_CMD_SET_COLUMN_ADDR 0x15
19 #define SSD1322_CMD_WRITE_RAM 0x5C
20 #define SSD1322_CMD_READ_RAM 0x5D
21 #define SSD1322_CMD_SET_ROW_ADDR 0x75
22 #define SSD1322_CMD_SET_REMAP 0xA0
23 #define SSD1322_CMD_SET_DISPLAY_START_LINE 0xA1
24 #define SSD1322_CMD_SET_DISPLAY_OFFSET 0xA2
25 #define SSD1322_CMD_SET_DISPLAY_MODE_OFF 0xA4
26 #define SSD1322_CMD_SET_DISPLAY_MODE_ON 0xA5
27 #define SSD1322_CMD_SET_DISPLAY_MODE_NORMAL 0xA6
28 #define SSD1322_CMD_SET_DISPLAY_MODE_INVERSE 0xA7
29 #define SSD1322_CMD_ENABLE_PARTIAL_DISPLAY 0xA8
30 #define SSD1322_CMD_EXIT_PARTIAL_DISPLAY 0xA9
31 #define SSD1322_CMD_SET_FUNCTION_SELECTION 0xAB
32 #define SSD1322_CMD_SET_DISPLAY_OFF 0xAE
33 #define SSD1322_CMD_SET_DISPLAY_ON 0xAF
34 #define SSD1322_CMD_SET_PHASE_LENGTH 0xB1
35 #define SSD1322_CMD_SET_CLOCK_DIVIDER 0xB3
36 #define SSD1322_CMD_DISPLAY_ENHANCEMENT 0xB4
37 #define SSD1322_CMD_SET_GPIO 0xB5
38 #define SSD1322_CMD_SET_SECOND_PRECHARGE_PERIOD 0xB6
39 #define SSD1322_CMD_SET_GRAY_SCALE_TABLE 0xB8
40 #define SSD1322_CMD_SET_DEFAULT_LINEAR_GRAY_SCALE_TABLE 0xB9
41 #define SSD1322_CMD_SET_PRECHARGE_VOLTAGE 0xBB
42 #define SSD1322_CMD_SET_VCOMH_VOLTAGE 0xBE
43 #define SSD1322_CMD_SET_CONTRAST_CURRENT 0xC1
44 #define SSD1322_CMD_MASTER_CURRENT_CONTROL 0xC7
45 #define SSD1322_CMD_SET_MULTIPLEX_RATIO 0xCA
46 #define SSD1322_CMD_DISPLAY_ENHANCEMENT_B 0xD1
47 #define SSD1322_CMD_SET_COMMAND_LOCK 0xFD
48
49 // set for some OLED displays
50 //#define ALTERNATE_OLED_VERSION
51
52
53 static const uint8_t oledInit[] = {
54 SSD1322_CMD_SET_COMMAND_LOCK, 1, 0x12, /* Unlock OLED driver IC*/
55 SSD1322_CMD_SET_DISPLAY_OFF, 0,
56 SSD1322_CMD_SET_CLOCK_DIVIDER, 1, 0x91,
57 SSD1322_CMD_SET_MULTIPLEX_RATIO, 1, 0x3F, /*duty = 1/64*,64 COMS are enabled*/
58 SSD1322_CMD_SET_DISPLAY_OFFSET, 1, 0x00,
59 SSD1322_CMD_SET_DISPLAY_START_LINE, 1, 0x00, /*set start line position*/
60 SSD1322_CMD_SET_REMAP, 2, 0x14, // Horizontal address increment,
61 // Disable Column Address Re-map,
62 // Enable Nibble Re-map,Scan from COM[N-1] to COM0,
63 // Disable COM Split Odd Even
64 0x11, // Enable Dual COM mode
65 SSD1322_CMD_SET_GPIO, 1, 0x00,
66 SSD1322_CMD_SET_FUNCTION_SELECTION, 1, 0x01, /* selection external VDD */
67 SSD1322_CMD_DISPLAY_ENHANCEMENT, 2, 0xA0, /* enables the external VSL*/
68 0xfd, /* 0xfd,Enhanced low GS display quality;default is 0xb5(normal),*/
69 SSD1322_CMD_SET_CONTRAST_CURRENT, 1, 0xff, /* default is 0x7f*/
70 SSD1322_CMD_MASTER_CURRENT_CONTROL, 1, 0x0f,
71 /* writeCommand(0xB9); GRAY TABLE,linear Gray Scale*/
72 SSD1322_CMD_SET_PHASE_LENGTH, 1, 0xE2, /*default is 0x74*/
73 SSD1322_CMD_DISPLAY_ENHANCEMENT_B, 2, 0x82, 0x20,
74 #ifdef ALTERNATE_OLED_VERSION
75 SSD1322_CMD_SET_PRECHARGE_VOLTAGE, 1, 0x08, /* 0.3xVcc */
76 SSD1322_CMD_SET_SECOND_PRECHARGE_PERIOD, 1, 0x0F, /* 15 clocks */
77 #else
78 SSD1322_CMD_SET_PRECHARGE_VOLTAGE, 1, 0x1F, /* 0.6xVcc */
79 SSD1322_CMD_SET_SECOND_PRECHARGE_PERIOD, 1, 0x08, /* default */
80 #endif
81 SSD1322_CMD_SET_VCOMH_VOLTAGE, 1, 0x07, /*0.86xVcc;default is 0x04*/
82 SSD1322_CMD_SET_DISPLAY_MODE_NORMAL, 0,
83 SSD1322_CMD_SET_DISPLAY_ON, 0
84 };
85
86
87 ////////////////////////////////////////////////////////////////////////////////////////
88 ////////////////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////////////////
90
91
92 //#include "mbed_debug.h"
93
94 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
95
96
97 SSD1322::SSD1322(proto_t displayproto,
98 int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC,
99 const char *name,
100 const unsigned int lcdsize_x, const unsigned int lcdsize_y)
101 : GraphicsDisplay(name),
102 screensize_X(lcdsize_x), screensize_Y(lcdsize_y),
103 _BPP(BPP), _PAGES(lcdsize_y/8*BPP),
104 _IC_X_SEGS(IC_X_SEGS), _IC_Y_COMS(IC_Y_COMS), _IC_PAGES(IC_Y_COMS/8*BPP)
105 {
106 proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC);
107 buffer = (unsigned char*) malloc (screensize_X*_PAGES);
108 set_auto_up(false);
109 foreground(0xFFFF);
110 background(0x0000);
111 hw_reset();
112 bus_enable(true);
113 init();
114 set_orientation(1);
115 cls();
116 locate(0,0);
117 }
118
119 SSD1322::~SSD1322()
120 {
121 free(buffer);
122 }
123
124 void SSD1322::wr_cmd8(unsigned char cmd)
125 {
126 proto->wr_cmd8(cmd);
127 }
128
129 void SSD1322::wr_data8(unsigned char data)
130 {
131 proto->wr_data8(data);
132 }
133
134 void SSD1322::wr_cmd16(unsigned short cmd)
135 {
136 proto->wr_cmd16(cmd);
137 }
138
139 void SSD1322::wr_gram(unsigned short data, unsigned int count)
140 {
141 proto->wr_gram(data, count);
142 }
143
144 void SSD1322::wr_grambuf(unsigned short* data, unsigned int lenght)
145 {
146 proto->wr_grambuf(data, lenght);
147 }
148
149 void SSD1322::hw_reset()
150 {
151 proto->hw_reset();
152 }
153
154 void SSD1322::bus_enable(bool enable)
155 {
156 proto->BusEnable(enable);
157 }
158
159
160
161 // monochrome SSD1322 driver ICs does not have ram rotate in hw (swap raw<->columns) like TFT displays
162 // for portrait views, XY swap will be done in sw in pixel() function
163 void SSD1322::set_orientation(int o)
164 {
165 orientation = o;
166 if (o & 0x01) {
167 set_width(screensize_X);
168 set_height(screensize_Y);
169 } else {
170 set_width(screensize_Y);
171 set_height(screensize_X);
172 }
173 switch (o) {
174 case (0):// portrait view -90°
175 mirrorXY(Y);
176 break;
177 case (1): // default, landscape view 0°
178 mirrorXY(NONE);
179 break;
180 case (2):// portrait view +90°
181 mirrorXY(X);
182 break;
183 case (3):// landscape view +180°
184 mirrorXY(XY);
185 break;
186 }
187 }
188
189 void SSD1322::mirrorXY(mirror_t mode)
190 {
191 // Horizontal address increment,
192 // Disable Column Address Re-map,
193 // Enable Nibble Re-map,Scan from COM[N-1] to COM0,
194 // Disable COM Split Odd Even
195 // Enable Dual COM mode
196 unsigned char d;
197 switch (mode)
198 {
199 case(NONE):
200 d = 0x14;
201 break;
202 case(X):
203 d = 0x10;
204 break;
205 case(Y):
206 d = 0x16;
207 break;
208 case(XY):
209 d = 0x12;
210 break;
211 }
212 wr_cmd8(SSD1322_CMD_SET_REMAP);
213 wr_data8(d);
214 wr_data8(0x11);
215 }
216
217 void SSD1322::invert(unsigned char o)
218 {
219 if(o == 0) wr_cmd8(0xA6);
220 else wr_cmd8(0xA7);
221 }
222
223 void SSD1322::set_contrast(int o)
224 {
225 contrast = o;
226
227 wr_cmd8(SSD1322_CMD_SET_CONTRAST_CURRENT);
228 wr_data8(o&0xFF);
229 }
230
231 int SSD1322::get_contrast(void)
232 {
233 return(contrast);
234 }
235 void SSD1322::window(int x, int y, int w, int h) {
236 // current pixel location
237 cur_x = x;
238 cur_y = y;
239 // window settings
240 win_x1 = x;
241 win_x2 = x + w - 1;
242 win_y1 = y;
243 win_y2 = y + h - 1;
244 }
245 void SSD1322::window_pushpixel(unsigned short color) {
246 pixel(cur_x, cur_y, color);
247 cur_x++;
248 if(cur_x > win_x2) {
249 cur_x = win_x1;
250 cur_y++;
251 if(cur_y > win_y2) {
252 cur_y = win_y1;
253 }
254 }
255 }
256 void SSD1322::window_pushpixel(unsigned short color, unsigned int count) {
257 while(count)
258 {
259 pixel(cur_x, cur_y, color);
260 cur_x++;
261 if(cur_x > win_x2)
262 {
263 cur_x = win_x1;
264 cur_y++;
265 if(cur_y > win_y2)
266 {
267 cur_y = win_y1;
268 }
269 }
270 count--;
271 }
272 }
273 void SSD1322::window_pushpixelbuf(unsigned short* color, unsigned int lenght) {
274 while(lenght)
275 {
276 pixel(cur_x, cur_y, *color++);
277 cur_x++;
278 if(cur_x > win_x2)
279 {
280 cur_x = win_x1;
281 cur_y++;
282 if(cur_y > win_y2)
283 {
284 cur_y = win_y1;
285 }
286 }
287 lenght--;
288 }
289 }
290
291 unsigned short SSD1322::pixelpos(int x, int y)
292 {
293 if(!(orientation&1)) SWAP(x,y);
294 // first check parameter
295 if((x >= screensize_X) || (y >= screensize_Y)) return 0;
296
297 unsigned short page = y * _BPP / 8;
298 return (x + page*screensize_X);
299 }
300
301 void SSD1322::pixel(int x, int y, unsigned short color)
302 {
303 if(!(orientation&1)) SWAP(x,y);
304 // first check parameter
305 if((x >= screensize_X) || (y >= screensize_Y)) return;
306
307 /*
308 unsigned short page = y * _BPP / 8;
309 unsigned char pos = y & ((8 / _BPP) - 1); // position in page
310 unsigned char mask = (( 1 << _BPP) - 1);
311
312 buffer[(x + page*screensize_X)] = (buffer[(x + page*screensize_X)] & ~(mask<<(pos*_BPP))) | ((color&mask)<<(pos*_BPP));
313 */
314
315 unsigned char cval = buffer[(y*128) + x/2];
316 if (x&1) {
317 cval = (cval & 0xF0) | (color & 0x0F);
318 } else {
319 cval = (cval & 0x0F) | (color & 0x0F)<<4;
320 }
321 buffer[(y*128) + x/2] = cval;
322 }
323
324 unsigned short SSD1322::pixelread(int x, int y)
325 {
326 if(!(orientation&1)) SWAP(x,y);
327 // first check parameter
328 if((x >= screensize_X) || (y >= screensize_Y)) return 0;
329
330 unsigned short page = y * _BPP / 8;
331 unsigned char pos = y & ((8 / _BPP) - 1); // position in page
332 unsigned char mask = (( 1 << _BPP) - 1);
333
334 //FAUX
335 return (buffer[(x + page*screensize_X)]); // = (buffer[(x + page*screensize_X)] & ~(mask<<(pos*_BPP))) | ((color&mask)<<(pos*_BPP));
336 }
337
338
339
340 // Set row address 0~32
341 void SSD1322::set_row_address(unsigned char add)
342 {
343 wr_cmd8(SSD1322_CMD_SET_ROW_ADDR);
344 add &= 0x3F;
345 wr_data8(add);
346 wr_data8(0x3F);
347 }
348
349 // Set col address 0~64 for Gray mode)
350 void SSD1322::set_column_address(unsigned char add)
351 {
352 wr_cmd8(SSD1322_CMD_SET_COLUMN_ADDR);
353 add &= 0x3F;
354 wr_data8(0x1c+add); // where does this 0x1C (28) comes from???
355 wr_data8(0x5b); // this 0x5B (91) seems 28+64 (-1)
356 }
357
358 void SSD1322::copy_to_lcd(void)
359 {
360 unsigned int i;
361 unsigned char x, y;
362
363 set_row_address(0);
364 set_column_address(0);
365
366 i = 0;
367 wr_cmd8(SSD1322_CMD_WRITE_RAM);
368 for(y=0; y<64; y++)
369 for(x=0; x<128; x++)
370 wr_data8(buffer[i++]);
371 }
372
373 unsigned long SSD1322::buffaddr(unsigned int i)
374 {
375 return (unsigned long) &(buffer[i]);
376 }
377
378 void SSD1322::clrbuff(const unsigned char value)
379 {
380 memset(buffer, value, screensize_X*_PAGES);
381 }
382
383 void SSD1322::fills(const unsigned char value)
384 {
385 clrbuff(value);
386 copy_to_lcd();
387 }
388
389 void SSD1322::cls(void)
390 {
391 clrbuff();
392 copy_to_lcd();
393 }
394
395 //void ll_fill(const unsigned char value=0xFF, const unsigned char w=0x78, const unsigned char=0x80);
396 void SSD1322::ll_fill(const unsigned char value, const unsigned char w, const unsigned char h)
397 {
398 unsigned short x,y;
399 //w = 0x78; // 120 x 2px by byte => 240px
400 //h = 0x80; // 0x80 for 64 rows because 2 COMs mode
401
402 wr_cmd8(0x15); // COL ADDR
403 wr_data8(0x00);
404 wr_data8(w-1);
405
406 wr_cmd8(0x75); // ROW ADDR
407 wr_data8(0x00);
408 wr_data8(h-1);
409
410 wr_cmd8(0x5C); // WRITE RAM
411 for(y=0; y<h/2; y++)
412 {
413 for(x=0; x<2*w; x++)
414 {
415 if ((y==0) | (y==(h/2-1)))
416 wr_data8(0xFF);
417 else if (x == 56) // ????
418 wr_data8(0xF2);
419 else if (x == 183)
420 //else if (x == (2*w-1))
421 wr_data8(0x2F);
422 else if ((x%10) == 0)
423 wr_data8(0x55);
424 else if ((x%5) == 0)
425 wr_data8(0x33);
426 else
427 wr_data8(0x22);
428 }
429 }
430 }
431
432 int SSD1322::sizeX()
433 {
434 return screensize_X;
435 }
436 int SSD1322::sizeY()
437 {
438 return screensize_Y;
439 }
440
441 // reset and init the lcd controller
442 void SSD1322::init()
443 {
444 /* Start Initial Sequence ----------------------------------------------------*/
445 for (uint16_t ind=0; ind < sizeof(oledInit); ) {
446 wr_cmd8(oledInit[ind++]);
447 uint8_t dataSize = oledInit[ind++];
448 while (dataSize--) {
449 wr_data8(oledInit[ind++]);
450 }
451 }
452 }
453
454 void SSD1322::loop_event(void) {
455 if (loop_pos >= 64)
456 loop_pos = 0;
457 set_row_address(loop_pos);
458 set_column_address(0);
459 wr_cmd8(SSD1322_CMD_WRITE_RAM);
460
461 for(uint8_t i=0; i<128; i++)
462 wr_data8(buffer[loop_pos*64+i]);
463 }
464
465 void SSD1322::start_loop(float tick) {
466 loop_pos = 0;
467 bah.attach(this, &SSD1322::loop_event, tick);
468 }
469
470 void SSD1322::stop_loop() {
471 bah.detach();
472 }

mercurial