-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSD1331.c
493 lines (434 loc) · 15.1 KB
/
SSD1331.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
******************************************************************************
* @file xxx.c
* @author Waveshare Team
* @version
* @date xx-xx-2014
* @brief xxxxx.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, WAVESHARE SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
******************************************************************************
*
* Modified by Maksymilian Mruszczak, 2021
*/
/* Includes ------------------------------------------------------------------*/
#include "LIB_Config.h"
#include "SSD1331.h"
#include "Fonts.h"
unsigned int rotate=0;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define SSD1331_CMD 0
#define SSD1331_DATA 1
#define OLED_WIDTH 96
#define OLED_HEIGHT 64
/* Private macro -------------------------------------------------------------*/
#define DRAW_LINE 0x21
#define DRAW_RECTANGLE 0x22
#define COPY_WINDOW 0x23
#define DIM_WINDOW 0x24
#define CLEAR_WINDOW 0x25
#define FILL_WINDOW 0x26
#define DISABLE_FILL 0x00
#define ENABLE_FILL 0x01
#define CONTINUOUS_SCROLLING_SETUP 0x27
#define DEACTIVE_SCROLLING 0x2E
#define ACTIVE_SCROLLING 0x2F
#define SET_COLUMN_ADDRESS 0x15
#define SET_ROW_ADDRESS 0x75
#define SET_CONTRAST_A 0x81
#define SET_CONTRAST_B 0x82
#define SET_CONTRAST_C 0x83
#define MASTER_CURRENT_CONTROL 0x87
#define SET_PRECHARGE_SPEED_A 0x8A
#define SET_PRECHARGE_SPEED_B 0x8B
#define SET_PRECHARGE_SPEED_C 0x8C
#define SET_REMAP 0xA0
#define SET_DISPLAY_START_LINE 0xA1
#define SET_DISPLAY_OFFSET 0xA2
#define NORMAL_DISPLAY 0xA4
#define ENTIRE_DISPLAY_ON 0xA5
#define ENTIRE_DISPLAY_OFF 0xA6
#define INVERSE_DISPLAY 0xA7
#define SET_MULTIPLEX_RATIO 0xA8
#define DIM_MODE_SETTING 0xAB
#define SET_MASTER_CONFIGURE 0xAD
#define DIM_MODE_DISPLAY_ON 0xAC
#define DISPLAY_OFF 0xAE
#define NORMAL_BRIGHTNESS_DISPLAY_ON 0xAF
#define POWER_SAVE_MODE 0xB0
#define PHASE_PERIOD_ADJUSTMENT 0xB1
#define DISPLAY_CLOCK_DIV 0xB3
#define SET_GRAy_SCALE_TABLE 0xB8
#define ENABLE_LINEAR_GRAY_SCALE_TABLE 0xB9
#define SET_PRECHARGE_VOLTAGE 0xBB
#define SET_V_VOLTAGE 0xBE
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief
* @param
* @retval
*/
/**
* @brief Writes an byte to the display data ram or the command register
*
* @param chData: Data to be writen to the display data ram or the command register
* @param chCmd:
* 0: Writes to the command register
* 1: Writes to the display data ram
* @retval None
**/
static void ssd1331_write_byte(uint8_t chData, uint8_t chCmd)
{
if (chCmd) {
__SSD1331_DC_SET();
} else {
__SSD1331_DC_CLR();
}
__SSD1331_CS_CLR();
__SSD1331_WRITE_BYTE(&chData);
__SSD1331_CS_SET();
__SSD1331_DC_SET();
}
void ssd1331_draw_point(uint8_t chXpos, uint8_t chYpos, uint16_t hwColor)
{
if(rotate==90||rotate==270)
{
int temp=chXpos;
chXpos=OLED_WIDTH-chYpos;
chYpos=temp;
}
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
//set column point
ssd1331_write_byte(SET_COLUMN_ADDRESS, SSD1331_CMD);
ssd1331_write_byte(chXpos, SSD1331_CMD);
ssd1331_write_byte(OLED_WIDTH - 1, SSD1331_CMD);
//set row point
ssd1331_write_byte(SET_ROW_ADDRESS, SSD1331_CMD);
ssd1331_write_byte(chYpos, SSD1331_CMD);
ssd1331_write_byte(OLED_HEIGHT - 1, SSD1331_CMD);
//fill 16bit colour
ssd1331_write_byte(hwColor >> 8, SSD1331_DATA);
ssd1331_write_byte(hwColor, SSD1331_DATA);
}
void ssd1331_draw_line(uint8_t chXpos0, uint8_t chYpos0, uint8_t chXpos1, uint8_t chYpos1, uint16_t hwColor)
{
int x = chXpos1 - chXpos0;
int y = chYpos1 - chYpos0;
int dx = abs(x), sx = chXpos0 < chXpos1 ? 1 : -1;
int dy = -abs(y), sy = chYpos0 < chYpos1 ? 1 : -1;
int err = dx + dy, e2;
if (chXpos0 >= OLED_WIDTH || chYpos0 >= OLED_HEIGHT || chXpos1 >= OLED_WIDTH || chYpos1 >= OLED_HEIGHT) {
return;
}
for (;;){
ssd1331_draw_point(chXpos0, chYpos0 , hwColor);
e2 = 2 * err;
if (e2 >= dy) {
if (chXpos0 == chXpos1) break;
err += dy; chXpos0 += sx;
}
if (e2 <= dx) {
if (chYpos0 == chYpos1) break;
err += dx; chYpos0 += sy;
}
}
}
void ssd1331_draw_v_line(uint8_t chXpos, uint8_t chYpos, uint8_t chHeight, uint16_t hwColor)
{
uint16_t i, y1 = MIN(chYpos + chHeight, OLED_HEIGHT - 1);
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
for (i = chYpos; i < y1; i ++) {
ssd1331_draw_point(chXpos, i, hwColor);
}
}
void ssd1331_draw_h_line(uint8_t chXpos, uint8_t chYpos, uint8_t chWidth, uint16_t hwColor)
{
uint16_t i, x1 = MIN(chXpos + chWidth, OLED_WIDTH- 1);
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
for (i = chXpos; i < x1; i ++) {
ssd1331_draw_point(i, chYpos, hwColor);
}
}
void ssd1331_draw_rect(uint8_t chXpos, uint8_t chYpos, uint8_t chWidth, uint8_t chHeight, uint16_t hwColor)
{
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
ssd1331_draw_h_line(chXpos, chYpos, chWidth, hwColor);
ssd1331_draw_h_line(chXpos, chYpos + chHeight, chWidth, hwColor);
ssd1331_draw_v_line(chXpos, chYpos, chHeight, hwColor);
ssd1331_draw_v_line(chXpos + chWidth, chYpos, chHeight + 1, hwColor);
}
void ssd1331_fill_rect(uint8_t chXpos, uint8_t chYpos, uint8_t chWidth, uint8_t chHeight, uint16_t hwColor)
{
uint16_t i, j;
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
for(i = 0; i < chHeight; i ++){
for(j = 0; j < chWidth; j ++){
ssd1331_draw_point(chXpos + j, chYpos + i, hwColor);
}
}
}
void ssd1331_draw_circle(uint8_t chXpos, uint8_t chYpos, uint8_t chRadius, uint16_t hwColor)
{
int x = -chRadius, y = 0, err = 2 - 2 * chRadius, e2;
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
do {
ssd1331_draw_point(chXpos - x, chYpos + y, hwColor);
ssd1331_draw_point(chXpos + x, chYpos + y, hwColor);
ssd1331_draw_point(chXpos + x, chYpos - y, hwColor);
ssd1331_draw_point(chXpos - x, chYpos - y, hwColor);
e2 = err;
if (e2 <= y) {
err += ++ y * 2 + 1;
if(-x == y && e2 <= x) e2 = 0;
}
if(e2 > x) err += ++ x * 2 + 1;
} while(x <= 0);
}
/**
* @brief Displays one character at the specified position
*
* @param chXpos: Specifies the X position
* @param chYpos: Specifies the Y position
* @param chSize:
* @param chMode
* @retval
**/
void ssd1331_display_char(uint8_t chXpos, uint8_t chYpos, uint8_t chChr, uint8_t chSize, uint16_t hwColor)
{
uint8_t i, j, chTemp;
uint8_t chYpos0 = chYpos;
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
for (i = 0; i < chSize; i ++) {
if (FONT_1206 == chSize) {
chTemp = c_chFont1206[chChr - 0x20][i];
} else if (FONT_1608 == chSize) {
chTemp = c_chFont1608[chChr - 0x20][i];
}
for (j = 0; j < 8; j ++) {
if (chTemp & 0x80) {
ssd1331_draw_point(chXpos, chYpos, hwColor);
}
chTemp <<= 1;
chYpos ++;
if ((chYpos - chYpos0) == chSize) {
chYpos = chYpos0;
chXpos ++;
break;
}
}
}
}
static uint32_t _pow(uint8_t m, uint8_t n)
{
uint32_t result = 1;
while(n --) result *= m;
return result;
}
void ssd1331_display_num(uint8_t chXpos, uint8_t chYpos, uint32_t chNum, uint8_t chLen, uint8_t chSize, uint16_t hwColor)
{
uint8_t i;
uint8_t chTemp, chShow = 0;
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
for(i = 0; i < chLen; i ++) {
chTemp = (chNum / _pow(10, chLen - i - 1)) % 10;
if(chShow == 0 && i < (chLen - 1)) {
if(chTemp == 0) {
ssd1331_display_char(chXpos + (chSize / 2) * i, chYpos, ' ', chSize, hwColor);
continue;
} else {
chShow = 1;
}
}
ssd1331_display_char(chXpos + (chSize / 2) * i, chYpos, chTemp + '0', chSize, hwColor);
}
}
void ssd1331_num(unsigned char x, unsigned char y, double num,unsigned char decimal_point, unsigned char Size, unsigned short hwColor)
{
char str[100];
sprintf(str,"%.*lf",decimal_point,num);
//printf("%s",str);
ssd1331_display_string(x,y,(uint8_t *)str,Size,hwColor);
}
/**
* @brief Displays a string on the screen
*
* @param chXpos: Specifies the X position
* @param chYpos: Specifies the Y position
* @param pchString: Pointer to a string to display on the screen
*
* @retval None
**/
void ssd1331_display_string(uint8_t chXpos, uint8_t chYpos, const uint8_t *pchString, uint8_t chSize, uint16_t hwColor)
{
if (chXpos >= OLED_WIDTH || chYpos >= OLED_HEIGHT) {
return;
}
while (*pchString != '\0') {
if (chXpos > (OLED_WIDTH - chSize / 2)) {
chXpos = 0;
chYpos += chSize;
if (chYpos > (OLED_HEIGHT - chSize)) {
chYpos = chXpos = 0;
ssd1331_clear_screen(0x00);
}
}
ssd1331_display_char(chXpos, chYpos, *pchString, chSize, hwColor);
chXpos += chSize / 2;
pchString ++;
}
}
void ssd1331_draw_1616char(uint8_t chXpos, uint8_t chYpos, uint8_t chChar, uint16_t hwColor)
{
uint8_t i, j;
uint8_t chTemp = 0, chYpos0 = chYpos;
for (i = 0; i < 32; i ++) {
chTemp = c_chFont1612[chChar - 0x30][i];
for (j = 0; j < 8; j ++) {
if (chTemp & 0x80) {
ssd1331_draw_point(chXpos, chYpos, hwColor);
}
chTemp <<= 1;
chYpos ++;
if ((chYpos - chYpos0) == 16) {
chYpos = chYpos0;
chXpos ++;
break;
}
}
}
}
void ssd1331_draw_3216char(uint8_t chXpos, uint8_t chYpos, uint8_t chChar, uint16_t hwColor)
{
uint8_t i, j;
uint8_t chTemp = 0, chYpos0 = chYpos;
for (i = 0; i < 64; i ++) {
chTemp = c_chFont3216[chChar - 0x30][i];
for (j = 0; j < 8; j ++) {
if (chTemp & 0x80) {
ssd1331_draw_point(chXpos, chYpos, hwColor);
}
chTemp <<= 1;
chYpos ++;
if ((chYpos - chYpos0) == 32) {
chYpos = chYpos0;
chXpos ++;
break;
}
}
}
}
void ssd1331_draw_bitmap(uint8_t chXpos, uint8_t chYpos, const uint8_t *pchBmp, uint8_t chWidth, uint8_t chHeight, uint16_t hwColor)
{
uint16_t i, j, byteWidth = (chWidth + 7) / 8;
for(j = 0; j < chHeight; j ++){
for(i = 0; i < chWidth; i ++ ) {
if(*(pchBmp + j * byteWidth + i / 8) & (128 >> (i & 7))) {
ssd1331_draw_point(chXpos + i, chYpos + j, hwColor);
}
}
}
}
void ssd1331_clear_screen(uint16_t hwColor)
{
uint16_t i, j;
uint8_t h, l;
h = hwColor >> 8;
l = hwColor;
ssd1331_draw_point(j, i, hwColor);
__SSD1331_DC_SET();
__SSD1331_CS_CLR();
for(i = 0; i < OLED_HEIGHT; i ++){
for(j = 0; j < OLED_WIDTH; j ++){
__SSD1331_WRITE_BYTE(&h);
__SSD1331_WRITE_BYTE(&l);
}
}
__SSD1331_CS_SET();
}
void ssd1331_init(void)
{
__SSD1331_RES_SET(); //RES set
__SSD1331_CS_SET();
ssd1331_write_byte(DISPLAY_OFF, SSD1331_CMD); //Display Off
ssd1331_write_byte(SET_CONTRAST_A, SSD1331_CMD); //Set contrast for color A
ssd1331_write_byte(0xFF, SSD1331_CMD); //145 0x91
ssd1331_write_byte(SET_CONTRAST_B, SSD1331_CMD); //Set contrast for color B
ssd1331_write_byte(0xFF, SSD1331_CMD); //80 0x50
ssd1331_write_byte(SET_CONTRAST_C, SSD1331_CMD); //Set contrast for color C
ssd1331_write_byte(0xFF, SSD1331_CMD); //125 0x7D
ssd1331_write_byte(MASTER_CURRENT_CONTROL, SSD1331_CMD);//master current control
ssd1331_write_byte(0x06, SSD1331_CMD); //6
ssd1331_write_byte(SET_PRECHARGE_SPEED_A, SSD1331_CMD);//Set Second Pre-change Speed For ColorA
ssd1331_write_byte(0x64, SSD1331_CMD); //100
ssd1331_write_byte(SET_PRECHARGE_SPEED_B, SSD1331_CMD);//Set Second Pre-change Speed For ColorB
ssd1331_write_byte(0x78, SSD1331_CMD); //120
ssd1331_write_byte(SET_PRECHARGE_SPEED_C, SSD1331_CMD);//Set Second Pre-change Speed For ColorC
ssd1331_write_byte(0x64, SSD1331_CMD); //100
ssd1331_write_byte(SET_REMAP, SSD1331_CMD); //set remap & data format
ssd1331_write_byte(0x72, SSD1331_CMD); //0x72
ssd1331_write_byte(SET_DISPLAY_START_LINE, SSD1331_CMD);//Set display Start Line
ssd1331_write_byte(0x0, SSD1331_CMD);
ssd1331_write_byte(SET_DISPLAY_OFFSET, SSD1331_CMD); //Set display offset
ssd1331_write_byte(0x0, SSD1331_CMD);
ssd1331_write_byte(NORMAL_DISPLAY, SSD1331_CMD); //Set display mode
ssd1331_write_byte(SET_MULTIPLEX_RATIO, SSD1331_CMD); //Set multiplex ratio
ssd1331_write_byte(0x3F, SSD1331_CMD);
ssd1331_write_byte(SET_MASTER_CONFIGURE, SSD1331_CMD); //Set master configuration
ssd1331_write_byte(0x8E, SSD1331_CMD);
ssd1331_write_byte(POWER_SAVE_MODE, SSD1331_CMD); //Set Power Save Mode
ssd1331_write_byte(0x00, SSD1331_CMD); //0x00
ssd1331_write_byte(PHASE_PERIOD_ADJUSTMENT, SSD1331_CMD);//phase 1 and 2 period adjustment
ssd1331_write_byte(0x31, SSD1331_CMD); //0x31
ssd1331_write_byte(DISPLAY_CLOCK_DIV, SSD1331_CMD); //display clock divider/oscillator frequency
ssd1331_write_byte(0xF0, SSD1331_CMD);
ssd1331_write_byte(SET_PRECHARGE_VOLTAGE, SSD1331_CMD);//Set Pre-Change Level
ssd1331_write_byte(0x3A, SSD1331_CMD);
ssd1331_write_byte(SET_V_VOLTAGE, SSD1331_CMD); //Set vcomH
ssd1331_write_byte(0x3E, SSD1331_CMD);
ssd1331_write_byte(DEACTIVE_SCROLLING, SSD1331_CMD); //disable scrolling
ssd1331_write_byte(NORMAL_BRIGHTNESS_DISPLAY_ON, SSD1331_CMD);//set display on
//ssd1331_fill_rect(0, 0, 96, 64, 0x0000);
ssd1331_clear_screen(0x0000);
}
void ssd1331_Rotate(int Rotate)
{
rotate=Rotate;
if(rotate==180||rotate==270)
{
ssd1331_write_byte(SET_REMAP,SSD1331_CMD); //set remap & data format
ssd1331_write_byte(0x60,SSD1331_CMD); //normal 0x72 180 0x60
}
else
{
ssd1331_write_byte(SET_REMAP,SSD1331_CMD); //set remap & data format
ssd1331_write_byte(0x72,SSD1331_CMD); //normal 0x72 180 0x60
}
}
/*-------------------------------END OF FILE-------------------------------*/