Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 Kevin 1
#include "defines.h"
121 Kevin 2
#include "oled_ssd1331.h"
3
#include "spi.h"
4
#include "string.h"
129 Kevin 5
#include "glcdfont.c"
121 Kevin 6
#include <delays.h>
129 Kevin 7
#include <string.h>
8
#include <stdio.h>
121 Kevin 9
 
129 Kevin 10
static SSD1331_DATA ssd1331_data;
11
static SSD1331_DATA *ssd1331_data_p = &ssd1331_data;
12
 
13
int SSD1331_Abs(int i) {
14
    if (i < 0)
15
        return -i;
16
    else
17
        return i;
18
}
19
 
20
void SSD1331_Swap(int *a, int *b) {
21
    int tmp = *a;
22
    *a = *b;
23
    *b = tmp;
24
}
25
 
121 Kevin 26
void SSD1331_Init() {
129 Kevin 27
    ssd1331_data_p->_width = ssd1331_data_p->WIDTH = SSD1331_LCDWIDTH;
28
    ssd1331_data_p->_height = ssd1331_data_p->HEIGHT = SSD1331_LCDHEIGHT;
29
    ssd1331_data_p->rotation = 0;
30
    ssd1331_data_p->cursor_x = ssd1331_data_p->cursor_y = 0;
31
    ssd1331_data_p->textsize = 1;
32
    ssd1331_data_p->textcolor = ssd1331_data_p->textbgcolor = 0xFFFF;
33
    ssd1331_data_p->wrap = 1;
121 Kevin 34
}
35
 
36
void SSD1331_Begin() {
37
    unsigned char buffer[37];
38
 
39
    // Toggle reset pin
129 Kevin 40
    SPI_RESET_LAT = 0;
121 Kevin 41
    Delay10KTCYx(1);
129 Kevin 42
    SPI_RESET_LAT = 1;
121 Kevin 43
 
44
    // Initialization Sequence
45
    buffer[0] = SSD1331_CMD_DISPLAYOFF; // 0xAE
46
    buffer[1] = SSD1331_CMD_SETREMAP; // 0xA0
47
#if defined SSD1331_COLORORDER_RGB
48
    buffer[2] = 0x72; // RGB Color
49
#else
50
    buffer[2] = 0x76; // BGR Color
51
#endif
52
    buffer[3] = SSD1331_CMD_STARTLINE; // 0xA1
53
    buffer[4] = 0x0;
54
    buffer[5] = SSD1331_CMD_DISPLAYOFFSET; // 0xA2
55
    buffer[6] = 0x0;
56
    buffer[7] = SSD1331_CMD_NORMALDISPLAY; // 0xA4
57
    buffer[8] = SSD1331_CMD_SETMULTIPLEX; // 0xA8
58
    buffer[9] = 0x3F; // 0x3F 1/64 duty
59
    buffer[10] = SSD1331_CMD_SETMASTER; // 0xAD
60
    buffer[11] = 0x8E;
61
    buffer[12] = SSD1331_CMD_POWERMODE; // 0xB0
62
    buffer[13] = 0x0B;
63
    buffer[14] = SSD1331_CMD_PRECHARGE; // 0xB1
64
    buffer[15] = 0x31;
65
    buffer[16] = SSD1331_CMD_CLOCKDIV; // 0xB3
66
    buffer[17] = 0xF0; // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
67
    buffer[18] = SSD1331_CMD_PRECHARGEA; // 0x8A
68
    buffer[19] = 0x64;
69
    buffer[20] = SSD1331_CMD_PRECHARGEB; // 0x8B
70
    buffer[21] = 0x78;
71
    buffer[22] = SSD1331_CMD_PRECHARGEA; // 0x8C
72
    buffer[23] = 0x64;
73
    buffer[24] = SSD1331_CMD_PRECHARGELEVEL; // 0xBB
74
    buffer[25] = 0x3A;
75
    buffer[26] = SSD1331_CMD_VCOMH; // 0xBE
76
    buffer[27] = 0x3E;
77
    buffer[28] = SSD1331_CMD_MASTERCURRENT; // 0x87
78
    buffer[29] = 0x06;
79
    buffer[30] = SSD1331_CMD_CONTRASTA; // 0x81
80
    buffer[31] = 0x91;
81
    buffer[32] = SSD1331_CMD_CONTRASTB; // 0x82
82
    buffer[33] = 0x50;
83
    buffer[34] = SSD1331_CMD_CONTRASTC; // 0x83
84
    buffer[35] = 0x7D;
85
    buffer[36] = SSD1331_CMD_DISPLAYON; //--turn on oled panel
86
 
129 Kevin 87
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 88
    SPI2_Write(buffer, 37);
89
}
90
 
91
void SSD1331_GoTo(int x, int y) {
92
    unsigned char buffer[6];
93
    if ((x >= SSD1331_LCDWIDTH) || (y >= SSD1331_LCDHEIGHT)) return;
94
 
95
    // set x and y coordinate
96
    buffer[0] = (SSD1331_CMD_SETCOLUMN);
127 Kevin 97
    buffer[1] = (x); // Start x address
98
    buffer[2] = (SSD1331_LCDWIDTH - 1); // End x address
121 Kevin 99
 
100
    buffer[3] = (SSD1331_CMD_SETROW);
127 Kevin 101
    buffer[4] = (y); // Start y address
102
    buffer[5] = (SSD1331_LCDHEIGHT - 1); // End y address
121 Kevin 103
 
129 Kevin 104
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 105
    SPI2_Write(buffer, 6);
106
}
107
 
108
void SSD1331_Command(unsigned char cmd) {
129 Kevin 109
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 110
    SPI2_Write(&cmd, 1);
111
}
112
 
113
void SSD1331_Data(unsigned char data) {
129 Kevin 114
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
121 Kevin 115
    SPI2_Write(&data, 1);
116
}
117
 
129 Kevin 118
void SSD1331_Clear_Display() {
119
    unsigned char buffer[5];
120
 
121
    buffer[0] = SSD1331_CMD_CLEARWINDOW;
122
    buffer[1] = 0;
123
    buffer[2] = 0;
124
    buffer[3] = SSD1331_LCDWIDTH-1;
125
    buffer[4] = SSD1331_LCDHEIGHT-1;
126
 
127
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
128
    SPI2_Write(buffer, 5);
129
 
130
    Delay1KTCYx(4);
131
}
132
 
121 Kevin 133
void SSD1331_Draw_Pixel(int x, int y, unsigned int color) {
134
    unsigned char buffer[2];
135
    buffer[0] = color >> 8;
136
    buffer[1] = color;
129 Kevin 137
    if ((x < 0) || (x >= ssd1331_data_p->_width) || (y < 0) || (y >= ssd1331_data_p->_height)) return;
121 Kevin 138
 
139
    // check rotation, move pixel around if necessary
129 Kevin 140
    switch (ssd1331_data_p->rotation) {
121 Kevin 141
        case 1:
129 Kevin 142
            SSD1331_Swap(&x, &y);
121 Kevin 143
            x = SSD1331_LCDWIDTH - x - 1;
144
            break;
145
        case 2:
146
            x = SSD1331_LCDWIDTH - x - 1;
147
            y = SSD1331_LCDHEIGHT - y - 1;
148
            break;
149
        case 3:
129 Kevin 150
            SSD1331_Swap(&x, &y);
121 Kevin 151
            y = SSD1331_LCDHEIGHT - y - 1;
152
            break;
153
    }
154
 
155
    SSD1331_GoTo(x, y);
156
 
157
    // setup for data
129 Kevin 158
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
121 Kevin 159
 
160
    SPI2_Write(buffer, 2);
161
}
162
 
163
void SSD1331_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
164
    unsigned char buffer[8];
165
 
166
    // check rotation, move pixel around if necessary
129 Kevin 167
    switch (ssd1331_data_p->rotation) {
121 Kevin 168
        case 1:
129 Kevin 169
            SSD1331_Swap(&x0, &y0);
170
            SSD1331_Swap(&x1, &y1);
121 Kevin 171
            x0 = SSD1331_LCDWIDTH - x0 - 1;
172
            x1 = SSD1331_LCDWIDTH - x1 - 1;
173
            break;
174
        case 2:
175
            x0 = SSD1331_LCDWIDTH - x0 - 1;
176
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
177
            x1 = SSD1331_LCDWIDTH - x1 - 1;
178
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
179
            break;
180
        case 3:
129 Kevin 181
            SSD1331_Swap(&x0, &y0);
182
            SSD1331_Swap(&x1, &y1);
121 Kevin 183
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
184
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
185
            break;
186
    }
187
 
188
    // Boundary check
189
    if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
190
        return;
191
    if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
192
        return;
193
    if (x0 >= SSD1331_LCDWIDTH)
194
        x0 = SSD1331_LCDWIDTH - 1;
195
    if (y0 >= SSD1331_LCDHEIGHT)
196
        y0 = SSD1331_LCDHEIGHT - 1;
197
    if (x1 >= SSD1331_LCDWIDTH)
198
        x1 = SSD1331_LCDWIDTH - 1;
199
    if (y1 >= SSD1331_LCDHEIGHT)
200
        y1 = SSD1331_LCDHEIGHT - 1;
201
    if (x0 < 0)
202
        x0 = 0;
203
    if (y0 < 0)
204
        y0 = 0;
205
    if (x1 < 0)
206
        x1 = 0;
207
    if (y1 < 0)
208
        y1 = 0;
209
 
210
    buffer[0] = SSD1331_CMD_DRAWLINE;
211
    buffer[1] = x0;
212
    buffer[2] = y0;
213
    buffer[3] = x1;
214
    buffer[4] = y1;
215
    buffer[5] = (color >> 11) << 1;
216
    buffer[6] = (color >> 5) & 0x3F;
217
    buffer[7] = (color << 1) & 0x3F;
218
 
129 Kevin 219
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 220
    SPI2_Write(buffer, 8);
221
}
222
 
129 Kevin 223
void SSD1331_Draw_Fast_VLine(int x, int y, int h, unsigned int color) {
224
    SSD1331_Draw_Line(x, y, x, y + h - 1, color);
225
}
226
 
227
void SSD1331_Draw_Fast_HLine(int x, int y, int w, unsigned int color) {
228
    SSD1331_Draw_Line(x, y, x + w - 1, y, color);
229
}
230
 
121 Kevin 231
void SSD1331_Draw_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
232
    unsigned char buffer[13];
233
    int x0,y0,x1,y1;
234
 
235
    // check rotation, move pixel around if necessary
129 Kevin 236
    switch (ssd1331_data_p->rotation) {
121 Kevin 237
        case 0:
238
            x0 = tx0;
239
            y0 = ty0;
240
            x1 = tx1;
241
            y1 = ty1;
242
            break;
243
        case 1:
244
            x0 = SSD1331_LCDWIDTH - ty1 - 1;
245
            y0 = tx0;
246
            x1 = SSD1331_LCDWIDTH - ty0 - 1;
247
            y1 = tx1;
248
            break;
249
        case 2:
250
            x0 = SSD1331_LCDWIDTH - tx1 - 1;
251
            y0 = SSD1331_LCDHEIGHT - ty1 - 1;
252
            x1 = SSD1331_LCDWIDTH - tx0 - 1;
253
            y1 = SSD1331_LCDHEIGHT - ty0 - 1;
254
            break;
255
        case 3:
256
            x0 = ty0;
257
            y0 = SSD1331_LCDHEIGHT - tx1 - 1;
258
            x1 = ty1;
259
            y1 = SSD1331_LCDHEIGHT - tx0 - 1;
260
            break;
261
    }
262
 
263
    // Boundary check
264
    if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
265
        return;
266
    if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
267
        return;
268
    if (x0 >= SSD1331_LCDWIDTH)
269
        x0 = SSD1331_LCDWIDTH - 1;
270
    if (y0 >= SSD1331_LCDHEIGHT)
271
        y0 = SSD1331_LCDHEIGHT - 1;
272
    if (x1 >= SSD1331_LCDWIDTH)
273
        x1 = SSD1331_LCDWIDTH - 1;
274
    if (y1 >= SSD1331_LCDHEIGHT)
275
        y1 = SSD1331_LCDHEIGHT - 1;
276
    if (x0 < 0)
277
        x0 = 0;
278
    if (y0 < 0)
279
        y0 = 0;
280
    if (x1 < 0)
281
        x1 = 0;
282
    if (y1 < 0)
283
        y1 = 0;
284
 
285
    buffer[0] = SSD1331_CMD_FILL;
286
    buffer[1] = 0;
287
    buffer[2] = SSD1331_CMD_DRAWRECT;
288
    buffer[3] = x0;
289
    buffer[4] = y0;
290
    buffer[5] = x1;
291
    buffer[6] = y1;
292
    buffer[7] = (color >> 11) << 1;
293
    buffer[8] = (color >> 5) & 0x3F;
294
    buffer[9] = (color << 1) & 0x3F;
295
    buffer[10] = 0;
296
    buffer[11] = 0;
297
    buffer[12] = 0;
298
 
129 Kevin 299
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 300
    SPI2_Write(buffer, 13);
301
}
302
 
303
void SSD1331_Fill_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
304
    unsigned char buffer[13];
305
    int x0,y0,x1,y1;
306
 
307
    // check rotation, move pixel around if necessary
129 Kevin 308
    switch (ssd1331_data_p->rotation) {
121 Kevin 309
        case 0:
310
            x0 = tx0;
311
            y0 = ty0;
312
            x1 = tx1;
313
            y1 = ty1;
314
            break;
315
        case 1:
316
            x0 = SSD1331_LCDWIDTH - ty1 - 1;
317
            y0 = tx0;
318
            x1 = SSD1331_LCDWIDTH - ty0 - 1;
319
            y1 = tx1;
320
            break;
321
        case 2:
322
            x0 = SSD1331_LCDWIDTH - tx1 - 1;
323
            y0 = SSD1331_LCDHEIGHT - ty1 - 1;
324
            x1 = SSD1331_LCDWIDTH - tx0 - 1;
325
            y1 = SSD1331_LCDHEIGHT - ty0 - 1;
326
            break;
327
        case 3:
328
            x0 = ty0;
329
            y0 = SSD1331_LCDHEIGHT - tx1 - 1;
330
            x1 = ty1;
331
            y1 = SSD1331_LCDHEIGHT - tx0 - 1;
332
            break;
333
    }
334
 
335
    // Boundary check
336
    if ((y0 >= SSD1331_LCDHEIGHT) && (y1 >= SSD1331_LCDHEIGHT))
337
        return;
338
    if ((x0 >= SSD1331_LCDWIDTH) && (x1 >= SSD1331_LCDWIDTH))
339
        return;
340
    if (x0 >= SSD1331_LCDWIDTH)
341
        x0 = SSD1331_LCDWIDTH - 1;
342
    if (y0 >= SSD1331_LCDHEIGHT)
343
        y0 = SSD1331_LCDHEIGHT - 1;
344
    if (x1 >= SSD1331_LCDWIDTH)
345
        x1 = SSD1331_LCDWIDTH - 1;
346
    if (y1 >= SSD1331_LCDHEIGHT)
347
        y1 = SSD1331_LCDHEIGHT - 1;
348
    if (x0 < 0)
349
        x0 = 0;
350
    if (y0 < 0)
351
        y0 = 0;
352
    if (x1 < 0)
353
        x1 = 0;
354
    if (y1 < 0)
355
        y1 = 0;
356
 
357
    buffer[0] = SSD1331_CMD_FILL;
358
    buffer[1] = 1;
359
    buffer[2] = SSD1331_CMD_DRAWRECT;
360
    buffer[3] = x0;
361
    buffer[4] = y0;
362
    buffer[5] = x1;
363
    buffer[6] = y1;
364
    buffer[7] = (color >> 11) << 1;
365
    buffer[8] = (color >> 5) & 0x3F;
366
    buffer[9] = (color << 1) & 0x3F;
367
    buffer[10] = (color >> 11) << 1;
368
    buffer[11] = (color >> 5) & 0x3F;
369
    buffer[12] = (color << 1) & 0x3F;
370
 
129 Kevin 371
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
121 Kevin 372
    SPI2_Write(buffer, 13);
373
 
374
    Delay1KTCYx(4);
375
}
376
 
129 Kevin 377
void SSD1331_Draw_Circle(int x0, int y0, int r, unsigned int color) {
378
    int f = 1 - r;
379
    int ddF_x = 1;
380
    int ddF_y = -2 * r;
381
    int x = 0;
382
    int y = r;
121 Kevin 383
 
129 Kevin 384
    SSD1331_Draw_Pixel(x0, y0 + r, color);
385
    SSD1331_Draw_Pixel(x0, y0 - r, color);
386
    SSD1331_Draw_Pixel(x0 + r, y0, color);
387
    SSD1331_Draw_Pixel(x0 - r, y0, color);
121 Kevin 388
 
129 Kevin 389
    while (x < y) {
390
        if (f >= 0) {
391
            y--;
392
            ddF_y += 2;
393
            f += ddF_y;
394
        }
395
        x++;
396
        ddF_x += 2;
397
        f += ddF_x;
121 Kevin 398
 
129 Kevin 399
        SSD1331_Draw_Pixel(x0 + x, y0 + y, color);
400
        SSD1331_Draw_Pixel(x0 - x, y0 + y, color);
401
        SSD1331_Draw_Pixel(x0 + x, y0 - y, color);
402
        SSD1331_Draw_Pixel(x0 - x, y0 - y, color);
403
        SSD1331_Draw_Pixel(x0 + y, y0 + x, color);
404
        SSD1331_Draw_Pixel(x0 - y, y0 + x, color);
405
        SSD1331_Draw_Pixel(x0 + y, y0 - x, color);
406
        SSD1331_Draw_Pixel(x0 - y, y0 - x, color);
407
    }
121 Kevin 408
}
409
 
129 Kevin 410
void SSD1331_Draw_Circle_Helper(int x0, int y0, int r, unsigned char cornername, unsigned int color) {
411
    int f = 1 - r;
412
    int ddF_x = 1;
413
    int ddF_y = -2 * r;
414
    int x = 0;
415
    int y = r;
416
 
417
    while (x < y) {
418
        if (f >= 0) {
419
            y--;
420
            ddF_y += 2;
421
            f += ddF_y;
422
        }
423
        x++;
424
        ddF_x += 2;
425
        f += ddF_x;
426
 
427
        if (cornername & 0x4) {
428
            SSD1331_Draw_Pixel(x0 + x, y0 + y, color);
429
            SSD1331_Draw_Pixel(x0 + y, y0 + x, color);
430
        }
431
        if (cornername & 0x2) {
432
            SSD1331_Draw_Pixel(x0 + x, y0 - y, color);
433
            SSD1331_Draw_Pixel(x0 + y, y0 - x, color);
434
        }
435
        if (cornername & 0x8) {
436
            SSD1331_Draw_Pixel(x0 - y, y0 + x, color);
437
            SSD1331_Draw_Pixel(x0 - x, y0 + y, color);
438
        }
439
        if (cornername & 0x1) {
440
            SSD1331_Draw_Pixel(x0 - y, y0 - x, color);
441
            SSD1331_Draw_Pixel(x0 - x, y0 - y, color);
442
        }
443
    }
444
}
445
 
446
void SSD1331_Fill_Circle(int x0, int y0, int r, unsigned int color) {
447
    SSD1331_Draw_Fast_VLine(x0, y0 - r, 2 * r + 1, color);
448
    SSD1331_Fill_Circle_Helper(x0, y0, r, 3, 0, color);
449
}
450
 
451
void SSD1331_Fill_Circle_Helper(int x0, int y0, int r, unsigned char cornername, int delta, unsigned int color) {
452
    int f = 1 - r;
453
    int ddF_x = 1;
454
    int ddF_y = -2 * r;
455
    int x = 0;
456
    int y = r;
457
 
458
    while (x < y) {
459
        if (f >= 0) {
460
            y--;
461
            ddF_y += 2;
462
            f += ddF_y;
463
        }
464
        x++;
465
        ddF_x += 2;
466
        f += ddF_x;
467
 
468
        if (cornername & 0x1) {
469
            SSD1331_Draw_Fast_VLine(x0 + x, y0 - y, 2 * y + 1 + delta, color);
470
            SSD1331_Draw_Fast_VLine(x0 + y, y0 - x, 2 * x + 1 + delta, color);
471
        }
472
        if (cornername & 0x2) {
473
            SSD1331_Draw_Fast_VLine(x0 - x, y0 - y, 2 * y + 1 + delta, color);
474
            SSD1331_Draw_Fast_VLine(x0 - y, y0 - x, 2 * x + 1 + delta, color);
475
        }
476
    }
477
}
478
void SSD1331_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
479
    SSD1331_Draw_Line(x0, y0, x1, y1, color);
480
    SSD1331_Draw_Line(x1, y1, x2, y2, color);
481
    SSD1331_Draw_Line(x2, y2, x0, y0, color);
482
}
483
 
484
void SSD1331_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
485
    int a, b, y, last;
486
    int dx01 = x1 - x0;
487
    int dy01 = y1 - y0;
488
    int dx02 = x2 - x0;
489
    int dy02 = y2 - y0;
490
    int dx12 = x2 - x1;
491
    int dy12 = y2 - y1;
492
    int sa = 0;
493
    int sb = 0;
494
 
495
    // Sort coordinates by Y order (y2 >= y1 >= y0)
496
    if (y0 > y1) {
497
        SSD1331_Swap(&y0, &y1);
498
        SSD1331_Swap(&x0, &x1);
499
    }
500
    if (y1 > y2) {
501
        SSD1331_Swap(&y2, &y1);
502
        SSD1331_Swap(&x2, &x1);
503
    }
504
    if (y0 > y1) {
505
        SSD1331_Swap(&y0, &y1);
506
        SSD1331_Swap(&x0, &x1);
507
    }
508
 
509
    if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
510
        a = b = x0;
511
        if (x1 < a) a = x1;
512
        else if (x1 > b) b = x1;
513
        if (x2 < a) a = x2;
514
        else if (x2 > b) b = x2;
515
        SSD1331_Draw_Fast_HLine(a, y0, b - a + 1, color);
516
        return;
517
    }
518
 
519
    // For upper part of triangle, find scanline crossings for segments
520
    // 0-1 and 0-2.  If y1=y2 (flat-bottomed triangle), the scanline y1
521
    // is included here (and second loop will be skipped, avoiding a /0
522
    // error there), otherwise scanline y1 is skipped here and handled
523
    // in the second loop...which also avoids a /0 error here if y0=y1
524
    // (flat-topped triangle).
525
    if (y1 == y2) last = y1; // Include y1 scanline
526
    else last = y1 - 1; // Skip it
527
 
528
    for (y = y0; y <= last; y++) {
529
        a = x0 + sa / dy01;
530
        b = x0 + sb / dy02;
531
        sa += dx01;
532
        sb += dx02;
533
        /* longhand:
534
        a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
535
        b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
536
         */
537
        if (a > b) SSD1331_Swap(&a, &b);
538
        SSD1331_Draw_Fast_HLine(a, y, b - a + 1, color);
539
    }
540
 
541
    // For lower part of triangle, find scanline crossings for segments
542
    // 0-2 and 1-2.  This loop is skipped if y1=y2.
543
    sa = dx12 * (y - y1);
544
    sb = dx02 * (y - y0);
545
    for (; y <= y2; y++) {
546
        a = x1 + sa / dy12;
547
        b = x0 + sb / dy02;
548
        sa += dx12;
549
        sb += dx02;
550
        /* longhand:
551
        a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
552
        b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
553
         */
554
        if (a > b) SSD1331_Swap(&a, &b);
555
        SSD1331_Draw_Fast_HLine(a, y, b - a + 1, color);
556
    }
557
}
558
 
559
void SSD1331_Draw_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
560
    // smarter version
561
    SSD1331_Draw_Fast_HLine(x + r, y, w - 2 * r, color); // Top
562
    SSD1331_Draw_Fast_HLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
563
    SSD1331_Draw_Fast_VLine(x, y + r, h - 2 * r, color); // Left
564
    SSD1331_Draw_Fast_VLine(x + w - 1, y + r, h - 2 * r, color); // Right
565
 
566
    // draw four corners
567
    SSD1331_Draw_Circle_Helper(x + r, y + r, r, 1, color);
568
    SSD1331_Draw_Circle_Helper(x + w - r - 1, y + r, r, 2, color);
569
    SSD1331_Draw_Circle_Helper(x + w - r - 1, y + h - r - 1, r, 4, color);
570
    SSD1331_Draw_Circle_Helper(x + r, y + h - r - 1, r, 8, color);
571
}
572
 
573
void SSD1331_Fill_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
574
    // smarter version
575
    SSD1331_Fill_Rect(x + r, y, w - 2 * r, h, color);
576
 
577
    // draw four corners
578
    SSD1331_Fill_Circle_Helper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color);
579
    SSD1331_Fill_Circle_Helper(x + r, y + r, r, 2, h - 2 * r - 1, color);
580
}
581
 
582
void SSD1331_Draw_Bitmap(int x, int y, const unsigned char* bitmap, int w, int h, unsigned int color) {
583
    int i, j;
584
    for (j = 0; j < h; j++) {
585
        for (i = 0; i < w; i++) {
586
            if (bitmap[i + (j / 8) * w] & (j % 8)) {
587
                SSD1331_Draw_Pixel(x + i, y + j, color);
588
            }
589
        }
590
    }
591
}
592
 
593
void SSD1331_Draw_Char(int x, int y, unsigned char c, unsigned int color, unsigned int bg, unsigned char size) {
594
    int i, j;
595
    unsigned int line;
596
 
597
    if ((x >= ssd1331_data_p->_width) || // Clip right
598
            (y >= ssd1331_data_p->_height) || // Clip bottom
599
            ((x + 5 * size - 1) < 0) || // Clip left
600
            ((y + 8 * size - 1) < 0)) // Clip top
601
        return;
602
 
603
    for (i = 0; i < 6; i++) {
604
        if (i == 5)
605
            line = 0x0;
606
        else
607
            line = font[(c * 5) + i];
608
        for (j = 0; j < 8; j++) {
609
            if (line & 0x1) {
610
                if (size == 1) {// default size
611
                    SSD1331_Draw_Pixel(x + i, y + j, color);
612
                } else { // big size
613
                    SSD1331_Fill_Rect(x + (i * size), y + (j * size), size, size, color);
614
                }
615
            } else if (bg != color) {
616
                if (size == 1) { // default size
617
                    SSD1331_Draw_Pixel(x + i, y + j, bg);
618
                } else { // big size
619
                    SSD1331_Fill_Rect(x + i*size, y + j*size, size, size, bg);
620
                }
621
            }
622
            line >>= 1;
623
        }
624
    }
625
}
626
 
627
void SSD1331_Write(unsigned char c) {
628
    if (c == '\n' || c == '\r') {
629
        ssd1331_data_p->cursor_y += ssd1331_data_p->textsize * 8;
630
        ssd1331_data_p->cursor_x = 0;
631
        //    } else if (c == '\r') {
632
        //        // skip em
633
    } else {
634
        SSD1331_Draw_Char(ssd1331_data_p->cursor_x, ssd1331_data_p->cursor_y, c, ssd1331_data_p->textcolor, ssd1331_data_p->textbgcolor, ssd1331_data_p->textsize);
635
        ssd1331_data_p->cursor_x += ssd1331_data_p->textsize * 6;
636
        if (ssd1331_data_p->wrap && (ssd1331_data_p->cursor_x > (ssd1331_data_p->_width - ssd1331_data_p->textsize * 6))) {
637
            ssd1331_data_p->cursor_y += ssd1331_data_p->textsize * 8;
638
            ssd1331_data_p->cursor_x = 0;
639
        }
640
    }
641
}
642
 
643
void SSD1331_Write_String(const rom char *fmt, ...) {
644
    unsigned char i, len;
645
    unsigned char buffer[SSD1331_STRING_BUFFER_SIZE];
646
 
647
    // Parse and create string
648
    va_list args;
649
    va_start(args, fmt);
650
    vsprintf((char *) buffer, fmt, args);
651
    va_end(args);
652
    len = strlen((char *) buffer);
653
 
654
    // Make sure string to insert fits in buffer, truncate if necessary
655
    if (len > SSD1331_STRING_BUFFER_SIZE)
656
        len = SSD1331_STRING_BUFFER_SIZE;
657
 
658
    // Print buffer to string
659
    for (i = 0; i < len; i++) {
660
        SSD1331_Write(buffer[i]);
661
    }
662
}
663
 
664
void SSD1331_Set_Cursor(int x, int y) {
665
    ssd1331_data_p->cursor_x = x;
666
    ssd1331_data_p->cursor_y = y;
667
}
668
 
669
void SSD1331_Set_Text_Color(unsigned int c) {
670
    // for 'transparent' background, we'll set the bg
671
    // to the same as fg instead of using a flag
672
    ssd1331_data_p->textcolor = c;
673
    ssd1331_data_p->textbgcolor = c;
674
}
675
 
676
void SSD1331_Set_Text_Color_BG(unsigned int c, unsigned int bg) {
677
    ssd1331_data_p->textcolor = c;
678
    ssd1331_data_p->textbgcolor = bg;
679
}
680
 
681
void SSD1331_Set_Text_Size(unsigned char s) {
682
    ssd1331_data_p->textsize = (s > 0) ? s : 1;
683
}
684
 
685
void SSD1331_Set_Text_Wrap(unsigned char w) {
686
    ssd1331_data_p->wrap = w;
687
}
688
 
689
void SSD1331_Set_Rotation(unsigned char x) {
690
    x %= 4; // cant be higher than 3
691
    ssd1331_data_p->rotation = x;
692
    switch (x) {
693
        case 0:
694
        case 2:
695
            ssd1331_data_p->_width = ssd1331_data_p->WIDTH;
696
            ssd1331_data_p->_height = ssd1331_data_p->HEIGHT;
697
            break;
698
        case 1:
699
        case 3:
700
            ssd1331_data_p->_width = ssd1331_data_p->HEIGHT;
701
            ssd1331_data_p->_height = ssd1331_data_p->WIDTH;
702
            break;
703
    }
704
}
705
 
121 Kevin 706
unsigned int SSD1331_Color565(unsigned char r, unsigned char g, unsigned char b) {
707
    unsigned int c;
708
    c = r >> 3;
709
    c <<= 6;
710
    c |= g >> 2;
711
    c <<= 5;
712
    c |= b >> 3;
713
 
714
    return c;
715
}
716
 
717
void SSD1331_Test_DrawLines(unsigned int color) {
718
    int x, y;
129 Kevin 719
    SSD1331_Clear_Display();
720
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
721
        SSD1331_Draw_Line(0, 0, x, ssd1331_data_p->_height - 1, color);
121 Kevin 722
    }
129 Kevin 723
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
724
        SSD1331_Draw_Line(0, 0, ssd1331_data_p->_width - 1, y, color);
121 Kevin 725
    }
726
 
129 Kevin 727
    SSD1331_Clear_Display();
728
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
729
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, x, ssd1331_data_p->_height - 1, color);
121 Kevin 730
    }
129 Kevin 731
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
732
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, 0, y, color);
121 Kevin 733
    }
734
 
129 Kevin 735
    SSD1331_Clear_Display();
736
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
737
        SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, x, 0, color);
121 Kevin 738
    }
129 Kevin 739
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
740
        SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, ssd1331_data_p->_width - 1, y, color);
121 Kevin 741
    }
742
 
129 Kevin 743
    SSD1331_Clear_Display();
744
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
745
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, x, 0, color);
121 Kevin 746
    }
129 Kevin 747
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
748
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, 0, y, color);
121 Kevin 749
    }
750
}
751
 
752
void SSD1331_Test_DrawRect(unsigned int color) {
753
    int x;
129 Kevin 754
    SSD1331_Clear_Display();
755
    if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
756
        for (x = 0; x < ssd1331_data_p->_height - 1; x += 6) {
757
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
121 Kevin 758
        }
759
    } else {
129 Kevin 760
        for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
761
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
121 Kevin 762
        }
763
    }
764
}
765
 
766
void SSD1331_Test_FillRect(unsigned int color1, unsigned int color2) {
767
    int x;
129 Kevin 768
    SSD1331_Clear_Display();
769
    if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
770
        for (x = ssd1331_data_p->_height - 1; x > 6; x -= 6) {
771
            SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
772
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
121 Kevin 773
        }
774
    } else {
129 Kevin 775
        for (x = ssd1331_data_p->_width - 1; x > 6; x -= 6) {
776
            SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
777
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
121 Kevin 778
        }
779
    }
780
}
781
 
782
void SSD1331_Test_DrawCircle(unsigned int radius, unsigned int color) {
783
    int x, y;
129 Kevin 784
    for (x = 0; x < ssd1331_data_p->_width - 1 + radius; x += radius * 2) {
785
        for (y = 0; y < ssd1331_data_p->_height - 1 + radius; y += radius * 2) {
786
            SSD1331_Draw_Circle(x, y, radius, color);
121 Kevin 787
        }
788
    }
789
}
790
 
791
void SSD1331_Test_FillCircle(unsigned int radius, unsigned int color) {
792
    unsigned char x, y;
129 Kevin 793
    for (x = radius; x < ssd1331_data_p->_width - 1; x += radius * 2) {
794
        for (y = radius; y < ssd1331_data_p->_height - 1; y += radius * 2) {
795
            SSD1331_Fill_Circle(x, y, radius, color);
121 Kevin 796
        }
797
    }
798
}
799
 
800
void SSD1331_Test_DrawTria(void) {
801
    int color = 0xF800;
802
    int t;
129 Kevin 803
    int w = ssd1331_data_p->_width / 2;
804
    int x = ssd1331_data_p->_height;
121 Kevin 805
    int y = 0;
129 Kevin 806
    int z = ssd1331_data_p->_width;
807
    SSD1331_Clear_Display();
121 Kevin 808
    for (t = 0; t <= 15; t += 1) {
129 Kevin 809
        SSD1331_Draw_Triangle(w, y, y, x, z, x, color);
121 Kevin 810
        x -= 4;
811
        y += 4;
812
        z -= 4;
813
        color += 100;
814
    }
815
}
816
 
817
void SSD1331_Test_DrawRoundRect(void) {
818
    int color = 100;
819
    int i, t, x, y, w, h;
129 Kevin 820
    SSD1331_Clear_Display();
121 Kevin 821
    for (t = 0; t <= 4; t += 1) {
822
        x = 0;
823
        y = 0;
129 Kevin 824
        w = ssd1331_data_p->_width;
825
        h = ssd1331_data_p->_height;
121 Kevin 826
        for (i = 0; i <= 24; i += 1) {
129 Kevin 827
            SSD1331_Draw_Round_Rect(x, y, w, h, 5, color);
121 Kevin 828
            x += 2;
829
            y += 3;
830
            w -= 4;
831
            h -= 6;
832
            color += 1100;
833
        }
834
        color += 100;
835
    }
836
}
837
 
838
void SSD1331_Test_MediaButtons(void) {
839
    // play
129 Kevin 840
    SSD1331_Clear_Display();
841
    SSD1331_Fill_Round_Rect(25, 10, 78, 60, 8, SSD1331_WHITE);
842
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_RED);
121 Kevin 843
    Delay10KTCYx(100);
844
    // pause
129 Kevin 845
    SSD1331_Fill_Round_Rect(25, 90, 78, 60, 8, SSD1331_WHITE);
846
    SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_GREEN);
847
    SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_GREEN);
121 Kevin 848
    Delay10KTCYx(100);
849
    // play color
129 Kevin 850
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_BLUE);
121 Kevin 851
    Delay10KTCYx(100);
852
    // pause color
129 Kevin 853
    SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_RED);
854
    SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_RED);
121 Kevin 855
    // play color
129 Kevin 856
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_GREEN);
121 Kevin 857
}
858
 
859
void SSD1331_Test_Pattern(void) {
860
    unsigned char buffer[2];
861
    unsigned int i, j;
862
    SSD1331_GoTo(0, 0);
863
 
864
    for (i = 0; i < 64; i++) {
865
        for (j = 0; j < 96; j++) {
866
            if (i > 55) {
867
                buffer[0] = (SSD1331_WHITE >> 8);
868
                buffer[1] = (SSD1331_WHITE);
869
            } else if (i > 47) {
870
                buffer[0] = (SSD1331_BLUE >> 8);
871
                buffer[1] = (SSD1331_BLUE);
872
            } else if (i > 39) {
873
                buffer[0] = (SSD1331_GREEN >> 8);
874
                buffer[1] = (SSD1331_GREEN);
875
            } else if (i > 31) {
876
                buffer[0] = (SSD1331_CYAN >> 8);
877
                buffer[1] = (SSD1331_CYAN);
878
            } else if (i > 23) {
879
                buffer[0] = (SSD1331_RED >> 8);
880
                buffer[1] = (SSD1331_RED);
881
            } else if (i > 15) {
882
                buffer[0] = (SSD1331_MAGENTA >> 8);
883
                buffer[1] = (SSD1331_MAGENTA);
884
            } else if (i > 7) {
885
                buffer[0] = (SSD1331_YELLOW >> 8);
886
                buffer[1] = (SSD1331_YELLOW);
887
            } else {
888
                buffer[0] = (SSD1331_BLACK >> 8);
889
                buffer[1] = (SSD1331_BLACK);
890
            }
129 Kevin 891
            SPI_DC_SELECT_LAT = 1; // D/C high (data)
121 Kevin 892
            SPI2_Write(buffer, 2);
893
        }
894
    }
895
}