Subversion Repositories Code-Repo

Rev

Rev 127 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 127 Rev 129
Line 1... Line 1...
1
#include "maindefs.h"
1
#include "maindefs.h"
2
#include "oled_ssd1331.h"
2
#include "oled_ssd1331.h"
3
#include "spi.h"
3
#include "spi.h"
4
#include "string.h"
4
#include "string.h"
5
#include "Adafruit_GFX.h"
5
#include "glcdfont.c"
6
#include <delays.h>
6
#include <delays.h>
-
 
7
#include <string.h>
-
 
8
#include <stdio.h>
-
 
9
 
-
 
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
}
7
 
25
 
8
void SSD1331_Init() {
26
void SSD1331_Init() {
9
    GFX_Init(SSD1331_LCDWIDTH, SSD1331_LCDHEIGHT);
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;
10
}
34
}
11
 
35
 
12
void SSD1331_Begin() {
36
void SSD1331_Begin() {
13
    unsigned char buffer[37];
37
    unsigned char buffer[37];
14
 
38
 
15
    // Toggle reset pin
39
    // Toggle reset pin
16
    LATBbits.LATB6 = 0;
40
    SPI_RESET_LAT = 0;
17
    Delay10KTCYx(1);
41
    Delay10KTCYx(1);
18
    LATBbits.LATB6 = 1;
42
    SPI_RESET_LAT = 1;
19
 
43
 
20
    // Initialization Sequence
44
    // Initialization Sequence
21
    buffer[0] = SSD1331_CMD_DISPLAYOFF; // 0xAE
45
    buffer[0] = SSD1331_CMD_DISPLAYOFF; // 0xAE
22
    buffer[1] = SSD1331_CMD_SETREMAP; // 0xA0
46
    buffer[1] = SSD1331_CMD_SETREMAP; // 0xA0
23
#if defined SSD1331_COLORORDER_RGB
47
#if defined SSD1331_COLORORDER_RGB
Line 58... Line 82...
58
    buffer[33] = 0x50;
82
    buffer[33] = 0x50;
59
    buffer[34] = SSD1331_CMD_CONTRASTC; // 0x83
83
    buffer[34] = SSD1331_CMD_CONTRASTC; // 0x83
60
    buffer[35] = 0x7D;
84
    buffer[35] = 0x7D;
61
    buffer[36] = SSD1331_CMD_DISPLAYON; //--turn on oled panel
85
    buffer[36] = SSD1331_CMD_DISPLAYON; //--turn on oled panel
62
 
86
 
63
    LATBbits.LATB5 = 0; // D/C low (cmd)
87
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
64
    SPI2_Write(buffer, 37);
88
    SPI2_Write(buffer, 37);
65
}
89
}
66
 
90
 
67
void SSD1331_GoTo(int x, int y) {
91
void SSD1331_GoTo(int x, int y) {
68
    unsigned char buffer[6];
92
    unsigned char buffer[6];
Line 75... Line 99...
75
 
99
 
76
    buffer[3] = (SSD1331_CMD_SETROW);
100
    buffer[3] = (SSD1331_CMD_SETROW);
77
    buffer[4] = (y); // Start y address
101
    buffer[4] = (y); // Start y address
78
    buffer[5] = (SSD1331_LCDHEIGHT - 1); // End y address
102
    buffer[5] = (SSD1331_LCDHEIGHT - 1); // End y address
79
 
103
 
80
    LATBbits.LATB5 = 0; // D/C low (cmd)
104
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
81
    SPI2_Write(buffer, 6);
105
    SPI2_Write(buffer, 6);
82
}
106
}
83
 
107
 
84
void SSD1331_Reset() {
-
 
85
 
-
 
86
}
-
 
87
 
-
 
88
void SSD1331_Command(unsigned char cmd) {
108
void SSD1331_Command(unsigned char cmd) {
89
    LATBbits.LATB5 = 0; // D/C low (cmd)
109
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
90
    SPI2_Write(&cmd, 1);
110
    SPI2_Write(&cmd, 1);
91
}
111
}
92
 
112
 
93
void SSD1331_Data(unsigned char data) {
113
void SSD1331_Data(unsigned char data) {
94
    LATBbits.LATB5 = 1; // D/C high (data)
114
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
95
    SPI2_Write(&data, 1);
115
    SPI2_Write(&data, 1);
96
}
116
}
97
 
117
 
-
 
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
 
98
void SSD1331_Draw_Pixel(int x, int y, unsigned int color) {
133
void SSD1331_Draw_Pixel(int x, int y, unsigned int color) {
99
    unsigned char buffer[2];
134
    unsigned char buffer[2];
100
    buffer[0] = color >> 8;
135
    buffer[0] = color >> 8;
101
    buffer[1] = color;
136
    buffer[1] = color;
102
    if ((x < 0) || (x >= GFX_width()) || (y < 0) || (y >= GFX_height())) return;
137
    if ((x < 0) || (x >= ssd1331_data_p->_width) || (y < 0) || (y >= ssd1331_data_p->_height)) return;
103
 
138
 
104
    // check rotation, move pixel around if necessary
139
    // check rotation, move pixel around if necessary
105
    switch (GFX_getRotation()) {
140
    switch (ssd1331_data_p->rotation) {
106
        case 1:
141
        case 1:
107
            GFX_Swap(&x, &y);
142
            SSD1331_Swap(&x, &y);
108
            x = SSD1331_LCDWIDTH - x - 1;
143
            x = SSD1331_LCDWIDTH - x - 1;
109
            break;
144
            break;
110
        case 2:
145
        case 2:
111
            x = SSD1331_LCDWIDTH - x - 1;
146
            x = SSD1331_LCDWIDTH - x - 1;
112
            y = SSD1331_LCDHEIGHT - y - 1;
147
            y = SSD1331_LCDHEIGHT - y - 1;
113
            break;
148
            break;
114
        case 3:
149
        case 3:
115
            GFX_Swap(&x, &y);
150
            SSD1331_Swap(&x, &y);
116
            y = SSD1331_LCDHEIGHT - y - 1;
151
            y = SSD1331_LCDHEIGHT - y - 1;
117
            break;
152
            break;
118
    }
153
    }
119
 
154
 
120
    SSD1331_GoTo(x, y);
155
    SSD1331_GoTo(x, y);
121
 
156
 
122
    // setup for data
157
    // setup for data
123
    LATBbits.LATB5 = 1; // D/C high (data)
158
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
124
 
159
 
125
    SPI2_Write(buffer, 2);
160
    SPI2_Write(buffer, 2);
126
}
161
}
127
 
162
 
128
void SSD1331_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
163
void SSD1331_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
129
    unsigned char buffer[8];
164
    unsigned char buffer[8];
130
 
165
 
131
    // check rotation, move pixel around if necessary
166
    // check rotation, move pixel around if necessary
132
    switch (GFX_getRotation()) {
167
    switch (ssd1331_data_p->rotation) {
133
        case 1:
168
        case 1:
134
            GFX_Swap(&x0, &y0);
169
            SSD1331_Swap(&x0, &y0);
135
            GFX_Swap(&x1, &y1);
170
            SSD1331_Swap(&x1, &y1);
136
            x0 = SSD1331_LCDWIDTH - x0 - 1;
171
            x0 = SSD1331_LCDWIDTH - x0 - 1;
137
            x1 = SSD1331_LCDWIDTH - x1 - 1;
172
            x1 = SSD1331_LCDWIDTH - x1 - 1;
138
            break;
173
            break;
139
        case 2:
174
        case 2:
140
            x0 = SSD1331_LCDWIDTH - x0 - 1;
175
            x0 = SSD1331_LCDWIDTH - x0 - 1;
141
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
176
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
142
            x1 = SSD1331_LCDWIDTH - x1 - 1;
177
            x1 = SSD1331_LCDWIDTH - x1 - 1;
143
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
178
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
144
            break;
179
            break;
145
        case 3:
180
        case 3:
146
            GFX_Swap(&x0, &y0);
181
            SSD1331_Swap(&x0, &y0);
147
            GFX_Swap(&x1, &y1);
182
            SSD1331_Swap(&x1, &y1);
148
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
183
            y0 = SSD1331_LCDHEIGHT - y0 - 1;
149
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
184
            y1 = SSD1331_LCDHEIGHT - y1 - 1;
150
            break;
185
            break;
151
    }
186
    }
152
 
187
 
Line 179... Line 214...
179
    buffer[4] = y1;
214
    buffer[4] = y1;
180
    buffer[5] = (color >> 11) << 1;
215
    buffer[5] = (color >> 11) << 1;
181
    buffer[6] = (color >> 5) & 0x3F;
216
    buffer[6] = (color >> 5) & 0x3F;
182
    buffer[7] = (color << 1) & 0x3F;
217
    buffer[7] = (color << 1) & 0x3F;
183
 
218
 
184
    LATBbits.LATB5 = 0; // D/C low (cmd)
219
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
185
    SPI2_Write(buffer, 8);
220
    SPI2_Write(buffer, 8);
186
}
221
}
187
 
222
 
-
 
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
 
188
void SSD1331_Draw_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
231
void SSD1331_Draw_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
189
    unsigned char buffer[13];
232
    unsigned char buffer[13];
190
    int x0,y0,x1,y1;
233
    int x0,y0,x1,y1;
191
    
234
    
192
    // check rotation, move pixel around if necessary
235
    // check rotation, move pixel around if necessary
193
    switch (GFX_getRotation()) {
236
    switch (ssd1331_data_p->rotation) {
194
        case 0:
237
        case 0:
195
            x0 = tx0;
238
            x0 = tx0;
196
            y0 = ty0;
239
            y0 = ty0;
197
            x1 = tx1;
240
            x1 = tx1;
198
            y1 = ty1;
241
            y1 = ty1;
Line 251... Line 294...
251
    buffer[9] = (color << 1) & 0x3F;
294
    buffer[9] = (color << 1) & 0x3F;
252
    buffer[10] = 0;
295
    buffer[10] = 0;
253
    buffer[11] = 0;
296
    buffer[11] = 0;
254
    buffer[12] = 0;
297
    buffer[12] = 0;
255
 
298
 
256
    LATBbits.LATB5 = 0; // D/C low (cmd)
299
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
257
    SPI2_Write(buffer, 13);
300
    SPI2_Write(buffer, 13);
258
}
301
}
259
 
302
 
260
void SSD1331_Fill_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
303
void SSD1331_Fill_Rect(int tx0, int ty0, int tx1, int ty1, unsigned int color) {
261
    unsigned char buffer[13];
304
    unsigned char buffer[13];
262
    int x0,y0,x1,y1;
305
    int x0,y0,x1,y1;
263
    
306
    
264
    // check rotation, move pixel around if necessary
307
    // check rotation, move pixel around if necessary
265
    switch (GFX_getRotation()) {
308
    switch (ssd1331_data_p->rotation) {
266
        case 0:
309
        case 0:
267
            x0 = tx0;
310
            x0 = tx0;
268
            y0 = ty0;
311
            y0 = ty0;
269
            x1 = tx1;
312
            x1 = tx1;
270
            y1 = ty1;
313
            y1 = ty1;
Line 323... Line 366...
323
    buffer[9] = (color << 1) & 0x3F;
366
    buffer[9] = (color << 1) & 0x3F;
324
    buffer[10] = (color >> 11) << 1;
367
    buffer[10] = (color >> 11) << 1;
325
    buffer[11] = (color >> 5) & 0x3F;
368
    buffer[11] = (color >> 5) & 0x3F;
326
    buffer[12] = (color << 1) & 0x3F;
369
    buffer[12] = (color << 1) & 0x3F;
327
 
370
 
328
    LATBbits.LATB5 = 0; // D/C low (cmd)
371
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
329
    SPI2_Write(buffer, 13);
372
    SPI2_Write(buffer, 13);
330
 
373
 
331
    Delay1KTCYx(4);
374
    Delay1KTCYx(4);
332
}
375
}
333
 
376
 
-
 
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;
-
 
383
 
-
 
384
    SSD1331_Draw_Pixel(x0, y0 + r, color);
-
 
385
    SSD1331_Draw_Pixel(x0, y0 - r, color);
334
void SSD1331_Clear_Display() {
386
    SSD1331_Draw_Pixel(x0 + r, y0, color);
-
 
387
    SSD1331_Draw_Pixel(x0 - r, y0, color);
-
 
388
 
-
 
389
    while (x < y) {
-
 
390
        if (f >= 0) {
-
 
391
            y--;
-
 
392
            ddF_y += 2;
335
    unsigned char buffer[5];
393
            f += ddF_y;
-
 
394
        }
-
 
395
        x++;
-
 
396
        ddF_x += 2;
-
 
397
        f += ddF_x;
-
 
398
 
-
 
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
    }
-
 
408
}
336
 
409
 
-
 
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;
337
    buffer[0] = SSD1331_CMD_CLEARWINDOW;
413
    int ddF_y = -2 * r;
338
    buffer[1] = 0;
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++;
339
    buffer[2] = 0;
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
        }
340
    buffer[3] = SSD1331_LCDWIDTH-1;
435
        if (cornername & 0x8) {
-
 
436
            SSD1331_Draw_Pixel(x0 - y, y0 + x, color);
-
 
437
            SSD1331_Draw_Pixel(x0 - x, y0 + y, color);
-
 
438
        }
341
    buffer[4] = SSD1331_LCDHEIGHT-1;
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
}
342
 
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) {
343
    LATBbits.LATB5 = 0; // D/C low (cmd)
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;
344
    SPI2_Write(buffer, 5);
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
    }
345
 
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);
346
    Delay1KTCYx(4);
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
    }
347
}
704
}
348
 
705
 
349
unsigned int SSD1331_Color565(unsigned char r, unsigned char g, unsigned char b) {
706
unsigned int SSD1331_Color565(unsigned char r, unsigned char g, unsigned char b) {
350
    unsigned int c;
707
    unsigned int c;
351
    c = r >> 3;
708
    c = r >> 3;
Line 357... Line 714...
357
    return c;
714
    return c;
358
}
715
}
359
 
716
 
360
void SSD1331_Test_DrawLines(unsigned int color) {
717
void SSD1331_Test_DrawLines(unsigned int color) {
361
    int x, y;
718
    int x, y;
362
    GFX_clearScreen();
719
    SSD1331_Clear_Display();
363
    for (x = 0; x < GFX_width() - 1; x += 6) {
720
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
364
        GFX_drawLine(0, 0, x, GFX_height() - 1, color);
721
        SSD1331_Draw_Line(0, 0, x, ssd1331_data_p->_height - 1, color);
365
    }
722
    }
366
    for (y = 0; y < GFX_height() - 1; y += 6) {
723
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
367
        GFX_drawLine(0, 0, GFX_width() - 1, y, color);
724
        SSD1331_Draw_Line(0, 0, ssd1331_data_p->_width - 1, y, color);
368
    }
725
    }
369
 
726
 
370
    GFX_clearScreen();
727
    SSD1331_Clear_Display();
371
    for (x = 0; x < GFX_width() - 1; x += 6) {
728
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
372
        GFX_drawLine(GFX_width() - 1, 0, x, GFX_height() - 1, color);
729
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, x, ssd1331_data_p->_height - 1, color);
373
    }
730
    }
374
    for (y = 0; y < GFX_height() - 1; y += 6) {
731
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
375
        GFX_drawLine(GFX_width() - 1, 0, 0, y, color);
732
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, 0, 0, y, color);
376
    }
733
    }
377
 
734
 
378
    GFX_clearScreen();
735
    SSD1331_Clear_Display();
379
    for (x = 0; x < GFX_width() - 1; x += 6) {
736
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
380
        GFX_drawLine(0, GFX_height() - 1, x, 0, color);
737
        SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, x, 0, color);
381
    }
738
    }
382
    for (y = 0; y < GFX_height() - 1; y += 6) {
739
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
383
        GFX_drawLine(0, GFX_height() - 1, GFX_width() - 1, y, color);
740
        SSD1331_Draw_Line(0, ssd1331_data_p->_height - 1, ssd1331_data_p->_width - 1, y, color);
384
    }
741
    }
385
 
742
 
386
    GFX_clearScreen();
743
    SSD1331_Clear_Display();
387
    for (x = 0; x < GFX_width() - 1; x += 6) {
744
    for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
388
        GFX_drawLine(GFX_width() - 1, GFX_height() - 1, x, 0, color);
745
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, x, 0, color);
389
    }
746
    }
390
    for (y = 0; y < GFX_height() - 1; y += 6) {
747
    for (y = 0; y < ssd1331_data_p->_height - 1; y += 6) {
391
        GFX_drawLine(GFX_width() - 1, GFX_height() - 1, 0, y, color);
748
        SSD1331_Draw_Line(ssd1331_data_p->_width - 1, ssd1331_data_p->_height - 1, 0, y, color);
392
    }
749
    }
393
}
750
}
394
 
751
 
395
void SSD1331_Test_DrawRect(unsigned int color) {
752
void SSD1331_Test_DrawRect(unsigned int color) {
396
    int x;
753
    int x;
397
    GFX_clearScreen();
754
    SSD1331_Clear_Display();
398
    if (GFX_height() < GFX_width()) {
755
    if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
399
        for (x = 0; x < GFX_height() - 1; x += 6) {
756
        for (x = 0; x < ssd1331_data_p->_height - 1; x += 6) {
400
            GFX_drawRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color);
757
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
401
        }
758
        }
402
    } else {
759
    } else {
403
        for (x = 0; x < GFX_width() - 1; x += 6) {
760
        for (x = 0; x < ssd1331_data_p->_width - 1; x += 6) {
404
            GFX_drawRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color);
761
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color);
405
        }
762
        }
406
    }
763
    }
407
}
764
}
408
 
765
 
409
void SSD1331_Test_FillRect(unsigned int color1, unsigned int color2) {
766
void SSD1331_Test_FillRect(unsigned int color1, unsigned int color2) {
410
    int x;
767
    int x;
411
    GFX_clearScreen();
768
    SSD1331_Clear_Display();
412
    if (GFX_height() < GFX_width()) {
769
    if (ssd1331_data_p->_height < ssd1331_data_p->_width) {
413
        for (x = GFX_height() - 1; x > 6; x -= 6) {
770
        for (x = ssd1331_data_p->_height - 1; x > 6; x -= 6) {
414
            GFX_fillRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color1);
771
            SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
415
            GFX_drawRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color2);
772
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
416
        }
773
        }
417
    } else {
774
    } else {
418
        for (x = GFX_width() - 1; x > 6; x -= 6) {
775
        for (x = ssd1331_data_p->_width - 1; x > 6; x -= 6) {
419
            GFX_fillRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color1);
776
            SSD1331_Fill_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color1);
420
            GFX_drawRect((GFX_width() - 1) / 2 - x / 2, (GFX_height() - 1) / 2 - x / 2, x, x, color2);
777
            SSD1331_Draw_Rect((ssd1331_data_p->_width - 1) / 2 - x / 2, (ssd1331_data_p->_height - 1) / 2 - x / 2, x, x, color2);
421
        }
778
        }
422
    }
779
    }
423
}
780
}
424
 
781
 
425
void SSD1331_Test_DrawCircle(unsigned int radius, unsigned int color) {
782
void SSD1331_Test_DrawCircle(unsigned int radius, unsigned int color) {
426
    int x, y;
783
    int x, y;
427
    for (x = 0; x < GFX_width() - 1 + radius; x += radius * 2) {
784
    for (x = 0; x < ssd1331_data_p->_width - 1 + radius; x += radius * 2) {
428
        for (y = 0; y < GFX_height() - 1 + radius; y += radius * 2) {
785
        for (y = 0; y < ssd1331_data_p->_height - 1 + radius; y += radius * 2) {
429
            GFX_drawCircle(x, y, radius, color);
786
            SSD1331_Draw_Circle(x, y, radius, color);
430
        }
787
        }
431
    }
788
    }
432
}
789
}
433
 
790
 
434
void SSD1331_Test_FillCircle(unsigned int radius, unsigned int color) {
791
void SSD1331_Test_FillCircle(unsigned int radius, unsigned int color) {
435
    unsigned char x, y;
792
    unsigned char x, y;
436
    for (x = radius; x < GFX_width() - 1; x += radius * 2) {
793
    for (x = radius; x < ssd1331_data_p->_width - 1; x += radius * 2) {
437
        for (y = radius; y < GFX_height() - 1; y += radius * 2) {
794
        for (y = radius; y < ssd1331_data_p->_height - 1; y += radius * 2) {
438
            GFX_fillCircle(x, y, radius, color);
795
            SSD1331_Fill_Circle(x, y, radius, color);
439
        }
796
        }
440
    }
797
    }
441
}
798
}
442
 
799
 
443
void SSD1331_Test_DrawTria(void) {
800
void SSD1331_Test_DrawTria(void) {
444
    int color = 0xF800;
801
    int color = 0xF800;
445
    int t;
802
    int t;
446
    int w = GFX_width() / 2;
803
    int w = ssd1331_data_p->_width / 2;
447
    int x = GFX_height();
804
    int x = ssd1331_data_p->_height;
448
    int y = 0;
805
    int y = 0;
449
    int z = GFX_width();
806
    int z = ssd1331_data_p->_width;
450
    GFX_clearScreen();
807
    SSD1331_Clear_Display();
451
    for (t = 0; t <= 15; t += 1) {
808
    for (t = 0; t <= 15; t += 1) {
452
        GFX_drawTriangle(w, y, y, x, z, x, color);
809
        SSD1331_Draw_Triangle(w, y, y, x, z, x, color);
453
        x -= 4;
810
        x -= 4;
454
        y += 4;
811
        y += 4;
455
        z -= 4;
812
        z -= 4;
456
        color += 100;
813
        color += 100;
457
    }
814
    }
458
}
815
}
459
 
816
 
460
void SSD1331_Test_DrawRoundRect(void) {
817
void SSD1331_Test_DrawRoundRect(void) {
461
    int color = 100;
818
    int color = 100;
462
    int i, t, x, y, w, h;
819
    int i, t, x, y, w, h;
463
    GFX_clearScreen();
820
    SSD1331_Clear_Display();
464
    for (t = 0; t <= 4; t += 1) {
821
    for (t = 0; t <= 4; t += 1) {
465
        x = 0;
822
        x = 0;
466
        y = 0;
823
        y = 0;
467
        w = GFX_width();
824
        w = ssd1331_data_p->_width;
468
        h = GFX_height();
825
        h = ssd1331_data_p->_height;
469
        for (i = 0; i <= 24; i += 1) {
826
        for (i = 0; i <= 24; i += 1) {
470
            GFX_drawRoundRect(x, y, w, h, 5, color);
827
            SSD1331_Draw_Round_Rect(x, y, w, h, 5, color);
471
            x += 2;
828
            x += 2;
472
            y += 3;
829
            y += 3;
473
            w -= 4;
830
            w -= 4;
474
            h -= 6;
831
            h -= 6;
475
            color += 1100;
832
            color += 1100;
Line 478... Line 835...
478
    }
835
    }
479
}
836
}
480
 
837
 
481
void SSD1331_Test_MediaButtons(void) {
838
void SSD1331_Test_MediaButtons(void) {
482
    // play
839
    // play
483
    GFX_clearScreen();
840
    SSD1331_Clear_Display();
484
    GFX_fillRoundRect(25, 10, 78, 60, 8, SSD1331_WHITE);
841
    SSD1331_Fill_Round_Rect(25, 10, 78, 60, 8, SSD1331_WHITE);
485
    GFX_fillTriangle(42, 20, 42, 60, 90, 40, SSD1331_RED);
842
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_RED);
486
    Delay10KTCYx(100);
843
    Delay10KTCYx(100);
487
    // pause
844
    // pause
488
    GFX_fillRoundRect(25, 90, 78, 60, 8, SSD1331_WHITE);
845
    SSD1331_Fill_Round_Rect(25, 90, 78, 60, 8, SSD1331_WHITE);
489
    GFX_fillRoundRect(39, 98, 20, 45, 5, SSD1331_GREEN);
846
    SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_GREEN);
490
    GFX_fillRoundRect(69, 98, 20, 45, 5, SSD1331_GREEN);
847
    SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_GREEN);
491
    Delay10KTCYx(100);
848
    Delay10KTCYx(100);
492
    // play color
849
    // play color
493
    GFX_fillTriangle(42, 20, 42, 60, 90, 40, SSD1331_BLUE);
850
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_BLUE);
494
    Delay10KTCYx(100);
851
    Delay10KTCYx(100);
495
    // pause color
852
    // pause color
496
    GFX_fillRoundRect(39, 98, 20, 45, 5, SSD1331_RED);
853
    SSD1331_Fill_Round_Rect(39, 98, 20, 45, 5, SSD1331_RED);
497
    GFX_fillRoundRect(69, 98, 20, 45, 5, SSD1331_RED);
854
    SSD1331_Fill_Round_Rect(69, 98, 20, 45, 5, SSD1331_RED);
498
    // play color
855
    // play color
499
    GFX_fillTriangle(42, 20, 42, 60, 90, 40, SSD1331_GREEN);
856
    SSD1331_Fill_Triangle(42, 20, 42, 60, 90, 40, SSD1331_GREEN);
500
}
857
}
501
 
858
 
502
void SSD1331_Test_Pattern(void) {
859
void SSD1331_Test_Pattern(void) {
503
    unsigned char buffer[2];
860
    unsigned char buffer[2];
504
    unsigned int i, j;
861
    unsigned int i, j;
Line 529... Line 886...
529
                buffer[1] = (SSD1331_YELLOW);
886
                buffer[1] = (SSD1331_YELLOW);
530
            } else {
887
            } else {
531
                buffer[0] = (SSD1331_BLACK >> 8);
888
                buffer[0] = (SSD1331_BLACK >> 8);
532
                buffer[1] = (SSD1331_BLACK);
889
                buffer[1] = (SSD1331_BLACK);
533
            }
890
            }
534
            LATBbits.LATB5 = 1; // D/C high (data)
891
            SPI_DC_SELECT_LAT = 1; // D/C high (data)
535
            SPI2_Write(buffer, 2);
892
            SPI2_Write(buffer, 2);
536
        }
893
        }
537
    }
894
    }
538
}
895
}