Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
155 Kevin 1
#include <xc.h>
2
#include <delays.h>
3
#include <string.h>
4
#include <stdio.h>
5
#include "defines.h"
158 Kevin 6
#include "base_SPI.h"
7
#include "display_oled_ssd1306.h"
155 Kevin 8
#include "glcdfont.c"
158 Kevin 9
#include "base_UART.h"
155 Kevin 10
 
11
static SSD1306_DATA *ssd1306_data_p;
12
 
13
// 512 (128x32) or 1024 (128x64) bytes allocated for LCD buffer
14
// See linker file for details
15
static char LCD_BUFFER[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = {
16
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
20
    0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21
    0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25
    0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
26
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
27
    0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
28
    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
29
    0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
30
    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
31
    0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32
    0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
33
    0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
34
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
35
    0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
36
    0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
37
    0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
38
    0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
39
    0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40
    0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
41
    0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
42
    0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
43
    0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
44
    0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
45
    0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
46
    0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
47
    0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48
#if (SSD1306_LCDHEIGHT == 64)
49
    0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
50
    0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
51
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
52
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53
    0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
55
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
56
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57
    0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
58
    0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
59
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
60
    0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
61
    0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
62
    0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
63
    0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
64
    0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
65
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66
    0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
67
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
68
    0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
69
    0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
70
    0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
71
    0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
72
    0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
73
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81
#endif
82
};
83
 
84
int SSD1306_Abs(int i) {
85
    if (i < 0)
86
        return -i;
87
    else
88
        return i;
89
}
90
 
91
void SSD1306_Swap(int *a, int *b) {
92
    int tmp = *a;
93
    *a = *b;
94
    *b = tmp;
95
}
96
 
97
void SSD1306_Init(SSD1306_DATA *data) {
98
    ssd1306_data_p = data;
99
    ssd1306_data_p->_width = ssd1306_data_p->WIDTH = SSD1306_LCDWIDTH;
100
    ssd1306_data_p->_height = ssd1306_data_p->HEIGHT = SSD1306_LCDHEIGHT;
101
    ssd1306_data_p->rotation = 0;
102
    ssd1306_data_p->cursor_x = ssd1306_data_p->cursor_y = 0;
103
    ssd1306_data_p->textsize = 1;
104
    ssd1306_data_p->textcolor = SSD1306_WHITE;
105
    ssd1306_data_p->textbgcolor = SSD1306_BLACK;
106
    ssd1306_data_p->wrap = 1;
107
}
108
 
109
void SSD1306_Begin(char vccstate) {
110
    // Toggle reset pin
111
    SPI_RESET_LAT = 0;
112
    Delay10KTCYx(1);
113
    SPI_RESET_LAT = 1;
114
 
115
#if defined SSD1306_128_32
116
    // Init sequence for 128x32 OLED module
117
    SSD1306_Command(SSD1306_DISPLAYOFF); // 0xAE
118
    SSD1306_Command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
119
    SSD1306_Command(0x80); // The suggested ratio 0x80
120
    SSD1306_Command(SSD1306_SETMULTIPLEX); // 0xA8
121
    SSD1306_Command(0x1F);
122
    SSD1306_Command(SSD1306_SETDISPLAYOFFSET); // 0xD3
123
    SSD1306_Command(0x0); // No offset
124
    SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // Line #0
125
    SSD1306_Command(SSD1306_CHARGEPUMP); // 0x8D
126
    if (vccstate == SSD1306_EXTERNALVCC) {
127
        SSD1306_Command(0x10);
128
    } else {
129
        SSD1306_Command(0x14);
130
    }
131
    SSD1306_Command(SSD1306_MEMORYMODE); // 0x20
132
    SSD1306_Command(0x00); // 0x0 act like ks0108
133
    SSD1306_Command(SSD1306_SEGREMAP | 0x1);
134
    SSD1306_Command(SSD1306_COMSCANDEC);
135
    SSD1306_Command(SSD1306_SETCOMPINS); // 0xDA
136
    SSD1306_Command(0x02);
137
    SSD1306_Command(SSD1306_SETCONTRAST); // 0x81
138
    SSD1306_Command(0x8F);
139
    SSD1306_Command(SSD1306_SETPRECHARGE); // 0xd9
140
    if (vccstate == SSD1306_EXTERNALVCC) {
141
        SSD1306_Command(0x22);
142
    } else {
143
        SSD1306_Command(0xF1);
144
    }
145
    SSD1306_Command(SSD1306_SETVCOMDETECT); // 0xDB
146
    SSD1306_Command(0x40);
147
    SSD1306_Command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
148
    SSD1306_Command(SSD1306_NORMALDISPLAY); // 0xA6
149
#endif
150
 
151
#if defined SSD1306_128_64
152
    // Init sequence for 128x64 OLED module
153
    SSD1306_Command(SSD1306_DISPLAYOFF); // 0xAE
154
    SSD1306_Command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
155
    SSD1306_Command(0x80); // The suggested ratio 0x80
156
    SSD1306_Command(SSD1306_SETMULTIPLEX); // 0xA8
157
    SSD1306_Command(0x3F);
158
    SSD1306_Command(SSD1306_SETDISPLAYOFFSET); // 0xD3
159
    SSD1306_Command(0x0); // No offset
160
    SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // Line #0
161
    SSD1306_Command(SSD1306_CHARGEPUMP); // 0x8D
162
    if (vccstate == SSD1306_EXTERNALVCC) {
163
        SSD1306_Command(0x10);
164
    } else {
165
        SSD1306_Command(0x14);
166
    }
167
    SSD1306_Command(SSD1306_MEMORYMODE); // 0x20
168
    SSD1306_Command(0x00); // 0x0 act like ks0108
169
    SSD1306_Command(SSD1306_SEGREMAP | 0x1);
170
    SSD1306_Command(SSD1306_COMSCANDEC);
171
    SSD1306_Command(SSD1306_SETCOMPINS); // 0xDA
172
    SSD1306_Command(0x12);
173
    SSD1306_Command(SSD1306_SETCONTRAST); // 0x81
174
    if (vccstate == SSD1306_EXTERNALVCC) {
175
        SSD1306_Command(0x9F);
176
    } else {
177
        SSD1306_Command(0xCF);
178
    }
179
    SSD1306_Command(SSD1306_SETPRECHARGE); // 0xd9
180
    if (vccstate == SSD1306_EXTERNALVCC) {
181
        SSD1306_Command(0x22);
182
    } else {
183
        SSD1306_Command(0xF1);
184
    }
185
    SSD1306_Command(SSD1306_SETVCOMDETECT); // 0xDB
186
    SSD1306_Command(0x40);
187
    SSD1306_Command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
188
    SSD1306_Command(SSD1306_NORMALDISPLAY); // 0xA6
189
#endif
190
 
191
    SSD1306_Command(SSD1306_DISPLAYON); // Turn on OLED panel
192
}
193
 
194
void SSD1306_Command(char cmd) {
195
    char c = cmd;
196
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
197
    SPI2_Write(&c, 1);
198
}
199
 
200
void SSD1306_Data(char data) {
201
    char c = data;
202
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
203
    SPI2_Write(&c, 1);
204
}
205
 
206
void SSD1306_Clear_Display() {
207
    memset(LCD_BUFFER, 0, (SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8));
208
}
209
 
210
void SSD1306_Invert_Display(char c) {
211
    if (c) {
212
        SSD1306_Command(SSD1306_INVERTDISPLAY);
213
    } else {
214
        SSD1306_Command((SSD1306_NORMALDISPLAY));
215
    }
216
}
217
 
218
void SSD1306_Display() {
219
    SSD1306_Command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
220
    SSD1306_Command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
221
    SSD1306_Command(SSD1306_SETSTARTLINE | 0x0); // line #0
222
 
223
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
224
    SPI2_Write(LCD_BUFFER, SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8);
225
 
226
//    if (SSD1306_LCDHEIGHT == 32) {
227
//        SPI2_Write_Repeat(0, SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8);
228
//    }
229
}
230
 
231
void SSD1306_Draw_Pixel(int x, int y, unsigned int color) {
232
    if ((x < 0) || (x >= ssd1306_data_p->_width) || (y < 0) || (y >= ssd1306_data_p->_height))
233
        return;
234
 
235
    // check rotation, move pixel around if necessary
236
    switch (ssd1306_data_p->rotation) {
237
        case 1:
238
            SSD1306_Swap(&x, &y);
239
            x = SSD1306_LCDWIDTH - x - 1;
240
            break;
241
        case 2:
242
            x = SSD1306_LCDWIDTH - x - 1;
243
            y = SSD1306_LCDHEIGHT - y - 1;
244
            break;
245
        case 3:
246
            SSD1306_Swap(&x, &y);
247
            y = SSD1306_LCDHEIGHT - y - 1;
248
            break;
249
        default:
250
            break;
251
    }
252
 
253
    // Need to do this for some reason since x + (y / 8) * SSD1306_LCDWIDTH returns -128?!
157 Kevin 254
    // TODO: Change this back when they fix the compiler
155 Kevin 255
    int loc = (y / 8) * SSD1306_LCDWIDTH;
256
    loc += x;
257
    // x is which column
258
    if (color == SSD1306_WHITE) {
259
        LCD_BUFFER[loc] |= 1<<(y % 8);
260
    } else {
261
        LCD_BUFFER[loc] &= ~(1<<(y % 8));
262
    }
263
}
264
 
265
void SSD1306_Draw_Line(int x0, int y0, int x1, int y1, unsigned int color) {
266
    int dx, dy, err, ystep;
267
    int steep = SSD1306_Abs(y1 - y0) > SSD1306_Abs(x1 - x0);
268
    if (steep) {
269
        SSD1306_Swap(&x0, &y0);
270
        SSD1306_Swap(&x1, &y1);
271
    }
272
 
273
    if (x0 > x1) {
274
        SSD1306_Swap(&x0, &x1);
275
        SSD1306_Swap(&y0, &y1);
276
    }
277
 
278
    dx = x1 - x0;
279
    dy = SSD1306_Abs(y1 - y0);
280
 
281
    err = dx / 2;
282
 
283
    if (y0 < y1) {
284
        ystep = 1;
285
    } else {
286
        ystep = -1;
287
    }
288
 
289
    for (; x0 <= x1; x0++) {
290
 
291
        if (steep) {
292
            SSD1306_Draw_Pixel(y0, x0, color);
293
        } else {
294
            SSD1306_Draw_Pixel(x0, y0, color);
295
        }
296
        err -= dy;
297
        if (err < 0) {
298
            y0 += ystep;
299
            err += dx;
300
        }
301
    }
302
}
303
 
304
void SSD1306_Draw_Fast_VLine(int x, int y, int h, unsigned int color) {
305
    SSD1306_Draw_Line(x, y, x, y + h - 1, color);
306
}
307
 
308
void SSD1306_Draw_Fast_HLine(int x, int y, int w, unsigned int color) {
309
    SSD1306_Draw_Line(x, y, x + w - 1, y, color);
310
}
311
 
312
void SSD1306_Draw_Rect(int x, int y, int w, int h, unsigned int color) {
313
    SSD1306_Draw_Fast_HLine(x, y, w, color);
314
    SSD1306_Draw_Fast_HLine(x, y + h, w, color);
315
    SSD1306_Draw_Fast_VLine(x, y, h, color);
316
    SSD1306_Draw_Fast_VLine(x + w, y, h, color);
317
}
318
 
319
void SSD1306_Fill_Rect(int x, int y, int w, int h, unsigned int color) {
320
    int i;
321
    for (i = x; i < x + w; i++) {
322
        SSD1306_Draw_Fast_VLine(i, y, h, color);
323
    }
324
}
325
 
326
void SSD1306_Draw_Circle(int x0, int y0, int r, unsigned int color) {
327
    int f = 1 - r;
328
    int ddF_x = 1;
329
    int ddF_y = -2 * r;
330
    int x = 0;
331
    int y = r;
332
 
333
    SSD1306_Draw_Pixel(x0, y0 + r, color);
334
    SSD1306_Draw_Pixel(x0, y0 - r, color);
335
    SSD1306_Draw_Pixel(x0 + r, y0, color);
336
    SSD1306_Draw_Pixel(x0 - r, y0, color);
337
 
338
    while (x < y) {
339
        if (f >= 0) {
340
            y--;
341
            ddF_y += 2;
342
            f += ddF_y;
343
        }
344
        x++;
345
        ddF_x += 2;
346
        f += ddF_x;
347
 
348
        SSD1306_Draw_Pixel(x0 + x, y0 + y, color);
349
        SSD1306_Draw_Pixel(x0 - x, y0 + y, color);
350
        SSD1306_Draw_Pixel(x0 + x, y0 - y, color);
351
        SSD1306_Draw_Pixel(x0 - x, y0 - y, color);
352
        SSD1306_Draw_Pixel(x0 + y, y0 + x, color);
353
        SSD1306_Draw_Pixel(x0 - y, y0 + x, color);
354
        SSD1306_Draw_Pixel(x0 + y, y0 - x, color);
355
        SSD1306_Draw_Pixel(x0 - y, y0 - x, color);
356
    }
357
}
358
 
359
void SSD1306_Draw_Circle_Helper(int x0, int y0, int r, char cornername, unsigned int color) {
360
    int f = 1 - r;
361
    int ddF_x = 1;
362
    int ddF_y = -2 * r;
363
    int x = 0;
364
    int y = r;
365
 
366
    while (x < y) {
367
        if (f >= 0) {
368
            y--;
369
            ddF_y += 2;
370
            f += ddF_y;
371
        }
372
        x++;
373
        ddF_x += 2;
374
        f += ddF_x;
375
        if (cornername & 0x4) {
376
            SSD1306_Draw_Pixel(x0 + x, y0 + y, color);
377
            SSD1306_Draw_Pixel(x0 + y, y0 + x, color);
378
        }
379
        if (cornername & 0x2) {
380
            SSD1306_Draw_Pixel(x0 + x, y0 - y, color);
381
            SSD1306_Draw_Pixel(x0 + y, y0 - x, color);
382
        }
383
        if (cornername & 0x8) {
384
            SSD1306_Draw_Pixel(x0 - y, y0 + x, color);
385
            SSD1306_Draw_Pixel(x0 - x, y0 + y, color);
386
        }
387
        if (cornername & 0x1) {
388
            SSD1306_Draw_Pixel(x0 - y, y0 - x, color);
389
            SSD1306_Draw_Pixel(x0 - x, y0 - y, color);
390
        }
391
    }
392
}
393
 
394
void SSD1306_Fill_Circle(int x0, int y0, int r, unsigned int color) {
395
    SSD1306_Draw_Fast_VLine(x0, y0 - r, 2 * r + 1, color);
396
    SSD1306_Fill_Circle_Helper(x0, y0, r, 3, 0, color);
397
}
398
 
399
void SSD1306_Fill_Circle_Helper(int x0, int y0, int r, char cornername, int delta, unsigned int color) {
400
    int f = 1 - r;
401
    int ddF_x = 1;
402
    int ddF_y = -2 * r;
403
    int x = 0;
404
    int y = r;
405
 
406
    while (x < y) {
407
        if (f >= 0) {
408
            y--;
409
            ddF_y += 2;
410
            f += ddF_y;
411
        }
412
        x++;
413
        ddF_x += 2;
414
        f += ddF_x;
415
 
416
        if (cornername & 0x1) {
417
            SSD1306_Draw_Fast_VLine(x0 + x, y0 - y, 2 * y + 1 + delta, color);
418
            SSD1306_Draw_Fast_VLine(x0 + y, y0 - x, 2 * x + 1 + delta, color);
419
        }
420
        if (cornername & 0x2) {
421
            SSD1306_Draw_Fast_VLine(x0 - x, y0 - y, 2 * y + 1 + delta, color);
422
            SSD1306_Draw_Fast_VLine(x0 - y, y0 - x, 2 * x + 1 + delta, color);
423
        }
424
    }
425
}
426
 
427
void SSD1306_Draw_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
428
    SSD1306_Draw_Line(x0, y0, x1, y1, color);
429
    SSD1306_Draw_Line(x1, y1, x2, y2, color);
430
    SSD1306_Draw_Line(x2, y2, x0, y0, color);
431
}
432
 
433
void SSD1306_Fill_Triangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned int color) {
434
    int a, b, y, last;
435
    int dx01 = x1 - x0;
436
    int dy01 = y1 - y0;
437
    int dx02 = x2 - x0;
438
    int dy02 = y2 - y0;
439
    int dx12 = x2 - x1;
440
    int dy12 = y2 - y1;
441
    int sa = 0;
442
    int sb = 0;
443
 
444
    // Sort coordinates by Y order (y2 >= y1 >= y0)
445
    if (y0 > y1) {
446
        SSD1306_Swap(&y0, &y1);
447
        SSD1306_Swap(&x0, &x1);
448
    }
449
    if (y1 > y2) {
450
        SSD1306_Swap(&y2, &y1);
451
        SSD1306_Swap(&x2, &x1);
452
    }
453
    if (y0 > y1) {
454
        SSD1306_Swap(&y0, &y1);
455
        SSD1306_Swap(&x0, &x1);
456
    }
457
 
458
    if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
459
        a = b = x0;
460
        if (x1 < a) a = x1;
461
        else if (x1 > b) b = x1;
462
        if (x2 < a) a = x2;
463
        else if (x2 > b) b = x2;
464
        SSD1306_Draw_Fast_HLine(a, y0, b - a + 1, color);
465
        return;
466
    }
467
 
468
    // For upper part of triangle, find scanline crossings for segments
469
    // 0-1 and 0-2.  If y1=y2 (flat-bottomed triangle), the scanline y1
470
    // is included here (and second loop will be skipped, avoiding a /0
471
    // error there), otherwise scanline y1 is skipped here and handled
472
    // in the second loop...which also avoids a /0 error here if y0=y1
473
    // (flat-topped triangle).
474
    if (y1 == y2) last = y1; // Include y1 scanline
475
    else last = y1 - 1; // Skip it
476
 
477
    for (y = y0; y <= last; y++) {
478
        a = x0 + sa / dy01;
479
        b = x0 + sb / dy02;
480
        sa += dx01;
481
        sb += dx02;
482
        /* longhand:
483
        a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
484
        b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
485
         */
486
        if (a > b) SSD1306_Swap(&a, &b);
487
        SSD1306_Draw_Fast_HLine(a, y, b - a + 1, color);
488
    }
489
 
490
    // For lower part of triangle, find scanline crossings for segments
491
    // 0-2 and 1-2.  This loop is skipped if y1=y2.
492
    sa = dx12 * (y - y1);
493
    sb = dx02 * (y - y0);
494
    for (; y <= y2; y++) {
495
        a = x1 + sa / dy12;
496
        b = x0 + sb / dy02;
497
        sa += dx12;
498
        sb += dx02;
499
        /* longhand:
500
        a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
501
        b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
502
         */
503
        if (a > b) SSD1306_Swap(&a, &b);
504
        SSD1306_Draw_Fast_HLine(a, y, b - a + 1, color);
505
    }
506
}
507
 
508
void SSD1306_Draw_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
509
    // smarter version
510
    SSD1306_Draw_Fast_HLine(x + r, y, w - 2 * r, color); // Top
511
    SSD1306_Draw_Fast_HLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
512
    SSD1306_Draw_Fast_VLine(x, y + r, h - 2 * r, color); // Left
513
    SSD1306_Draw_Fast_VLine(x + w - 1, y + r, h - 2 * r, color); // Right
514
 
515
    // draw four corners
516
    SSD1306_Draw_Circle_Helper(x + r, y + r, r, 1, color);
517
    SSD1306_Draw_Circle_Helper(x + w - r - 1, y + r, r, 2, color);
518
    SSD1306_Draw_Circle_Helper(x + w - r - 1, y + h - r - 1, r, 4, color);
519
    SSD1306_Draw_Circle_Helper(x + r, y + h - r - 1, r, 8, color);
520
}
521
 
522
void SSD1306_Fill_Round_Rect(int x, int y, int w, int h, int r, unsigned int color) {
523
    // smarter version
524
    SSD1306_Fill_Rect(x + r, y, w - 2 * r, h, color);
525
 
526
    // draw four corners
527
    SSD1306_Fill_Circle_Helper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1, color);
528
    SSD1306_Fill_Circle_Helper(x + r, y + r, r, 2, h - 2 * r - 1, color);
529
}
530
 
531
void SSD1306_Draw_Bitmap(int x, int y, const char* bitmap, int w, int h, unsigned int color) {
532
    int i, j;
533
    for (j = 0; j < h; j++) {
534
        for (i = 0; i < w; i++) {
535
            if (bitmap[i + (j / 8) * w] & (j % 8)) {
536
                SSD1306_Draw_Pixel(x + i, y + j, color);
537
            }
538
        }
539
    }
540
}
541
 
542
void SSD1306_Draw_Char(int x, int y, char c, unsigned int color, unsigned int bg, char size) {
543
    int i, j;
544
    unsigned int line;
545
 
546
    if ((x >= ssd1306_data_p->_width) || // Clip right
547
            (y >= ssd1306_data_p->_height) || // Clip bottom
548
            ((x + 5 * size - 1) < 0) || // Clip left
549
            ((y + 8 * size - 1) < 0)) // Clip top
550
        return;
551
 
552
    for (i = 0; i < 6; i++) {
553
        if (i == 5)
554
            line = 0x0;
555
        else
556
            line = font[(c * 5) + i];
557
        for (j = 0; j < 8; j++) {
558
            if (line & 0x1) {
559
                if (size == 1) {// default size
560
                    SSD1306_Draw_Pixel(x + i, y + j, color);
561
                } else { // big size
562
                    SSD1306_Fill_Rect(x + (i * size), y + (j * size), size, size, color);
563
                }
564
            } else if (bg != color) {
565
                if (size == 1) { // default size
566
                    SSD1306_Draw_Pixel(x + i, y + j, bg);
567
                } else { // big size
568
                    SSD1306_Fill_Rect(x + i*size, y + j*size, size, size, bg);
569
                }
570
            }
571
            line >>= 1;
572
        }
573
    }
574
}
575
 
576
void SSD1306_Write(char c) {
577
    if (c == '\n' || c == '\r') {
578
        ssd1306_data_p->cursor_y += ssd1306_data_p->textsize * 8;
579
        ssd1306_data_p->cursor_x = 0;
580
        //    } else if (c == '\r') {
581
        //        // skip em
582
    } else {
583
        SSD1306_Draw_Char(ssd1306_data_p->cursor_x, ssd1306_data_p->cursor_y, c, ssd1306_data_p->textcolor, ssd1306_data_p->textbgcolor, ssd1306_data_p->textsize);
584
        ssd1306_data_p->cursor_x += ssd1306_data_p->textsize * 6;
585
        if (ssd1306_data_p->wrap && (ssd1306_data_p->cursor_x > (ssd1306_data_p->_width - ssd1306_data_p->textsize * 6))) {
586
            ssd1306_data_p->cursor_y += ssd1306_data_p->textsize * 8;
587
            ssd1306_data_p->cursor_x = 0;
588
        }
589
    }
590
}
591
 
592
void SSD1306_Write_String(char* msg, char length) {
593
    for (char i = 0; i < length; i++) {
594
        SSD1306_Write(msg[i]);
595
    }
596
}
597
 
598
//void SSD1306_Write_String(const rom char *fmt, ...) {
599
//    char i, len;
600
//    char buffer[SSD1306_STRING_BUFFER_SIZE];
601
//
602
//    // Parse and create string
603
//    va_list args;
604
//    va_start(args, fmt);
605
//    vsprintf((char *) buffer, fmt, args);
606
//    va_end(args);
607
//    len = strlen((char *) buffer);
608
//
609
//    // Make sure string to insert fits in buffer, truncate if necessary
610
//    if (len > SSD1306_STRING_BUFFER_SIZE)
611
//        len = SSD1306_STRING_BUFFER_SIZE;
612
//
613
//    // Print buffer to string
614
//    for (i = 0; i < len; i++) {
615
//        SSD1306_Write(buffer[i]);
616
//    }
617
//}
618
 
619
//void SSD1306_Append_String(const rom char *fmt, ...) {
620
//    char i, len;
621
//    char buffer[SSD1306_STRING_BUFFER_SIZE];
622
//
623
//    // Parse and create string
624
//    va_list args;
625
//    va_start(args, fmt);
626
//    vsprintf((char *) buffer, fmt, args);
627
//    va_end(args);
628
//
629
//    // Make sure string to insert fits in buffer, truncate if necessary
630
//    len = strlen((char *) buffer);
631
//
632
//    if (len == 1) {  // This will only occur on "\n"
633
//        // Do nothing?
634
//        return;
635
//    }
636
//
637
//    if (len > SSD1306_STRING_BUFFER_SIZE)
638
//        len = SSD1306_STRING_BUFFER_SIZE;
639
//
640
//    // Omit the newline if string fill entire line
641
//    if (((len - 1)%(ssd1306_data_p->_width / 6)) == 0) { // 16 or 10
642
//        len -= 1;
643
//    }
644
//
645
//    // Shift everything right and insert string at beginning
646
//    for (i = 127; i > len - 1; i--) {
647
//        ssd1306_data_p->lcd_buffer[i] = ssd1306_data_p->lcd_buffer[i - len];
648
//    }
649
//    memcpy((char *)ssd1306_data_p->lcd_buffer, (const char *) buffer, len);
650
//
651
//    // Print full buffer to screen
652
//    SSD1306_Clear_Display();
653
//    SSD1306_Display();
654
//
655
//    SSD1306_Set_Cursor(0,0);
656
//    for (i = 0; i < SSD1306_LCD_BUFFER_SIZE-1; i++) {
657
//        SSD1306_Write(ssd1306_data_p->lcd_buffer[i]);
658
//    }
659
//}
660
 
661
void SSD1306_Set_Cursor(int x, int y) {
662
    ssd1306_data_p->cursor_x = x;
663
    ssd1306_data_p->cursor_y = y;
664
}
665
 
666
void SSD1306_Set_Text_Color(unsigned int c) {
667
    // for 'transparent' background, we'll set the bg
668
    // to the same as fg instead of using a flag
669
    ssd1306_data_p->textcolor = c;
670
    ssd1306_data_p->textbgcolor = c;
671
}
672
 
673
void SSD1306_Set_Text_Color_BG(unsigned int c, unsigned int bg) {
674
    ssd1306_data_p->textcolor = c;
675
    ssd1306_data_p->textbgcolor = bg;
676
}
677
 
678
void SSD1306_Set_Text_Size(char s) {
679
    ssd1306_data_p->textsize = (s > 0) ? s : 1;
680
}
681
 
682
void SSD1306_Set_Text_Wrap(char w) {
683
    ssd1306_data_p->wrap = w;
684
}
685
 
686
void SSD1306_Set_Rotation(char x) {
687
    x %= 4; // cant be higher than 3
688
    ssd1306_data_p->rotation = x;
689
    switch (x) {
690
        case 0:
691
        case 2:
692
            ssd1306_data_p->_width = ssd1306_data_p->WIDTH;
693
            ssd1306_data_p->_height = ssd1306_data_p->HEIGHT;
694
            break;
695
        case 1:
696
        case 3:
697
            ssd1306_data_p->_width = ssd1306_data_p->HEIGHT;
698
            ssd1306_data_p->_height = ssd1306_data_p->WIDTH;
699
            break;
700
    }
701
}
702
 
703
 
704
 
705
void SSD1306_Test_DrawChar() {
706
    char i;
707
    SSD1306_Set_Text_Size(1);
708
    SSD1306_Set_Text_Color(SSD1306_WHITE);
709
    SSD1306_Set_Cursor(0, 0);
710
 
711
    for (i = 0; i < 168; i++) {
712
        if (i == '\n') continue;
713
        SSD1306_Write(i);
714
//        if ((i > 0) && (i % 21 == 0))
715
//            SSD1306_write('\n');
716
    }
717
    SSD1306_Display();
718
}
719
 
720
void SSD1306_Test_DrawCircle() {
721
    int i;
722
    for (i = 0; i < ssd1306_data_p->_height; i += 2) {
723
        SSD1306_Draw_Circle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2, i, SSD1306_WHITE);
724
        SSD1306_Display();
725
    }
726
}
727
 
728
void SSD1306_Test_DrawRect(void) {
729
    int i;
730
    for (i = 0; i < ssd1306_data_p->_height / 2; i += 2) {
731
        SSD1306_Draw_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, SSD1306_WHITE);
732
        SSD1306_Display();
733
    }
734
}
735
 
736
void SSD1306_Test_FillRect(void) {
737
    char color = 1;
738
    int i;
739
    for (i = 0; i < ssd1306_data_p->_height / 2; i += 3) {
740
        // alternate colors
741
        SSD1306_Fill_Rect(i, i, ssd1306_data_p->_width - i * 2, ssd1306_data_p->_height - i * 2, color % 2);
742
        SSD1306_Display();
743
        color++;
744
    }
745
}
746
 
747
void SSD1306_Test_DrawTriangle(void) {
748
    int i;
749
    int min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
750
    for (i = 0; i < min / 2; i += 5) {
751
        SSD1306_Draw_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
752
                ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
753
                ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
754
        SSD1306_Display();
755
    }
756
}
757
 
758
void SSD1306_Test_FillTriangle(void) {
759
    char color = SSD1306_WHITE;
760
    int i;
761
    int min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
762
    for (i = min / 2; i > 0; i -= 5) {
763
        SSD1306_Fill_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
764
                ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
765
                ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
766
        if (color == SSD1306_WHITE) color = SSD1306_BLACK;
767
        else color = SSD1306_WHITE;
768
        SSD1306_Display();
769
    }
770
}
771
 
772
void SSD1306_Test_DrawRoundRect(void) {
773
    int i;
774
    for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
775
        SSD1306_Draw_Round_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, ssd1306_data_p->_height / 4, SSD1306_WHITE);
776
        SSD1306_Display();
777
    }
778
}
779
 
780
void SSD1306_Test_FillRoundRect(void) {
781
    char color = SSD1306_WHITE;
782
    int i;
783
    for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
784
        SSD1306_Fill_Round_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, ssd1306_data_p->_height / 4, color);
785
        if (color == SSD1306_WHITE) color = SSD1306_BLACK;
786
        else color = SSD1306_WHITE;
787
        SSD1306_Display();
788
    }
789
}
790
 
791
void SSD1306_Test_DrawLine(void) {
792
    int i;
793
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
794
        SSD1306_Draw_Line(0, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
795
        SSD1306_Display();
796
    }
797
    for (i = 0; i < ssd1306_data_p->_height; i += 4) {
798
        SSD1306_Draw_Line(0, 0, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
799
        SSD1306_Display();
800
    }
801
    Delay10KTCYx(255);
802
 
803
    SSD1306_Clear_Display();
804
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
805
        SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
806
        SSD1306_Display();
807
    }
808
    for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
809
        SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
810
        SSD1306_Display();
811
    }
812
    Delay10KTCYx(255);
813
 
814
    SSD1306_Clear_Display();
815
    for (i = ssd1306_data_p->_width - 1; i >= 0; i -= 4) {
816
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
817
        SSD1306_Display();
818
    }
819
    for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
820
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, 0, i, SSD1306_WHITE);
821
        SSD1306_Display();
822
    }
823
    Delay10KTCYx(255);
824
 
825
    SSD1306_Clear_Display();
826
    for (i = 0; i < ssd1306_data_p->_height; i += 4) {
827
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, 0, i, SSD1306_WHITE);
828
        SSD1306_Display();
829
    }
830
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
831
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
832
        SSD1306_Display();
833
    }
834
    Delay10KTCYx(255);
835
}