Subversion Repositories Code-Repo

Rev

Rev 315 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
315 Kevin 1
#include "defines.h"
2
#include <string.h>
3
//#include <stdio.h>
4
#include "OLED_SSD1306.h"
5
#include "SPI.h"
6
#include "glcdfont.c"
7
 
8
static SSD1306_DATA *ssd1306_data_p;
9
 
10
// 512 (128x32) or 1024 (128x64) bytes allocated for LCD buffer
11
// See linker file for details
12
static uint8_t LCD_BUFFER[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8] = {
13
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
17
    0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18
    0x00, 0x80, 0x80, 0xC0, 0xC0, 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, 0x00,
20
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22
    0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
23
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
24
    0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
25
    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
26
    0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
27
    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
28
    0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29
    0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
30
    0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
31
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
32
    0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
33
    0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
34
    0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
35
    0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
36
    0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37
    0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
38
    0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
39
    0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
40
    0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
41
    0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
42
    0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
43
    0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
44
    0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45
#if (SSD1306_LCDHEIGHT == 64)
46
    0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
47
    0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
48
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
49
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50
    0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
52
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
53
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54
    0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
55
    0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
56
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
57
    0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
58
    0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
59
    0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
60
    0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
61
    0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
62
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63
    0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
64
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
65
    0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
66
    0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
67
    0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
68
    0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
69
    0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
70
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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
#endif
79
};
80
 
81
int16_t SSD1306_Abs(int16_t i) {
82
    if (i < 0)
83
        return -i;
84
    else
85
        return i;
86
}
87
 
88
void SSD1306_Swap(int16_t *a, int16_t *b) {
89
    int16_t tmp = *a;
90
    *a = *b;
91
    *b = tmp;
92
}
93
 
94
void SSD1306_Init(SSD1306_DATA *data) {
95
    ssd1306_data_p = data;
96
 
97
    ssd1306_data_p->_height = SSD1306_LCDHEIGHT;
98
    ssd1306_data_p->HEIGHT = SSD1306_LCDHEIGHT;
99
    ssd1306_data_p->_width = SSD1306_LCDWIDTH;
100
    ssd1306_data_p->WIDTH = SSD1306_LCDWIDTH;
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
    __delay_us(10);
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(uint8_t cmd) {
195
    uint8_t c = cmd;
196
    SPI_DC_SELECT_LAT = 0; // D/C low (cmd)
197
    SPI_Write(&c, 1);
198
}
199
 
200
void SSD1306_Data(uint8_t data) {
201
    uint8_t c = data;
202
    SPI_DC_SELECT_LAT = 1; // D/C high (data)
203
    SPI_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(uint8_t 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
    SPI_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(int16_t x, int16_t y, uint16_t 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?!
254
    // TODO: Change this back when they fix the compiler
255
    int16_t 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(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
266
    int16_t dx, dy, err, ystep;
267
    int16_t 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(int16_t x, int16_t y, int16_t h, uint16_t color) {
305
    SSD1306_Draw_Line(x, y, x, y + h - 1, color);
306
}
307
 
308
void SSD1306_Draw_Fast_HLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
309
    SSD1306_Draw_Line(x, y, x + w - 1, y, color);
310
}
311
 
312
void SSD1306_Draw_Rect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t 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(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
320
    int16_t 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(int16_t x0, int16_t y0, int16_t r, uint16_t color) {
327
    int16_t f = 1 - r;
328
    int16_t ddF_x = 1;
329
    int16_t ddF_y = -2 * r;
330
    int16_t x = 0;
331
    int16_t 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(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color) {
360
    int16_t f = 1 - r;
361
    int16_t ddF_x = 1;
362
    int16_t ddF_y = -2 * r;
363
    int16_t x = 0;
364
    int16_t 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(int16_t x0, int16_t y0, int16_t r, uint16_t 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(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color) {
400
    int16_t f = 1 - r;
401
    int16_t ddF_x = 1;
402
    int16_t ddF_y = -2 * r;
403
    int16_t x = 0;
404
    int16_t 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(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t 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(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
434
    int16_t a, b, y, last;
435
    int16_t dx01 = x1 - x0;
436
    int16_t dy01 = y1 - y0;
437
    int16_t dx02 = x2 - x0;
438
    int16_t dy02 = y2 - y0;
439
    int16_t dx12 = x2 - x1;
440
    int16_t dy12 = y2 - y1;
441
    int16_t sa = 0;
442
    int16_t 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(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t 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(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t 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(int16_t x, int16_t y, const uint8_t* bitmap, int16_t w, int16_t h, uint16_t color) {
532
    int16_t 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(int16_t x, int16_t y, uint8_t c, uint16_t color, uint16_t bg, uint8_t size) {
543
    int16_t i, j;
544
    uint16_t 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(uint8_t 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(uint8_t* msg, uint8_t length) {
593
    for (uint8_t i = 0; i < length; i++) {
594
        SSD1306_Write(msg[i]);
595
    }
596
}
597
 
598
void SSD1306_Set_Cursor(int16_t x, int16_t y) {
599
    ssd1306_data_p->cursor_x = x;
600
    ssd1306_data_p->cursor_y = y;
601
}
602
 
603
void SSD1306_Set_Text_Color(uint16_t c) {
604
    // for 'transparent' background, we'll set the bg
605
    // to the same as fg instead of using a flag
606
    ssd1306_data_p->textcolor = c;
607
    ssd1306_data_p->textbgcolor = c;
608
}
609
 
610
void SSD1306_Set_Text_Color_BG(uint16_t c, uint16_t bg) {
611
    ssd1306_data_p->textcolor = c;
612
    ssd1306_data_p->textbgcolor = bg;
613
}
614
 
615
void SSD1306_Set_Text_Size(uint8_t s) {
616
    ssd1306_data_p->textsize = (s > 0) ? s : 1;
617
}
618
 
619
void SSD1306_Set_Text_Wrap(uint8_t w) {
620
    ssd1306_data_p->wrap = w;
621
}
622
 
623
void SSD1306_Set_Rotation(uint8_t x) {
624
    x %= 4; // cant be higher than 3
625
    ssd1306_data_p->rotation = x;
626
    switch (x) {
627
        case 0:
628
        case 2:
629
            ssd1306_data_p->_width = ssd1306_data_p->WIDTH;
630
            ssd1306_data_p->_height = ssd1306_data_p->HEIGHT;
631
            break;
632
        case 1:
633
        case 3:
634
            ssd1306_data_p->_width = ssd1306_data_p->HEIGHT;
635
            ssd1306_data_p->_height = ssd1306_data_p->WIDTH;
636
            break;
637
    }
638
}
639
 
640
 
641
void SSD1306_Test_DrawChar() {
642
    uint8_t i;
643
    SSD1306_Set_Text_Size(1);
644
    SSD1306_Set_Text_Color(SSD1306_WHITE);
645
    SSD1306_Set_Cursor(0, 0);
646
 
647
    for (i = 0; i < 168; i++) {
648
        if (i == '\n') continue;
649
        SSD1306_Write(i);
650
//        if ((i > 0) && (i % 21 == 0))
651
//            SSD1306_write('\n');
652
    }
653
    SSD1306_Display();
654
}
655
 
656
void SSD1306_Test_DrawCircle() {
657
    int16_t i;
658
    for (i = 0; i < ssd1306_data_p->_height; i += 2) {
659
        SSD1306_Draw_Circle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2, i, SSD1306_WHITE);
660
        SSD1306_Display();
661
    }
662
}
663
 
664
void SSD1306_Test_DrawRect(void) {
665
    int16_t i;
666
    for (i = 0; i < ssd1306_data_p->_height / 2; i += 2) {
667
        SSD1306_Draw_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, SSD1306_WHITE);
668
        SSD1306_Display();
669
    }
670
}
671
 
672
void SSD1306_Test_FillRect(void) {
673
    uint8_t color = 1;
674
    int16_t i;
675
    for (i = 0; i < ssd1306_data_p->_height / 2; i += 3) {
676
        // alternate colors
677
        SSD1306_Fill_Rect(i, i, ssd1306_data_p->_width - i * 2, ssd1306_data_p->_height - i * 2, color % 2);
678
        SSD1306_Display();
679
        color++;
680
    }
681
}
682
 
683
void SSD1306_Test_DrawTriangle(void) {
684
    int16_t i;
685
    int16_t min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
686
    for (i = 0; i < min / 2; i += 5) {
687
        SSD1306_Draw_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
688
                ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
689
                ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
690
        SSD1306_Display();
691
    }
692
}
693
 
694
void SSD1306_Test_FillTriangle(void) {
695
    uint8_t color = SSD1306_WHITE;
696
    int16_t i;
697
    int16_t min = ssd1306_data_p->_width < ssd1306_data_p->_height ? ssd1306_data_p->_width : ssd1306_data_p->_height;
698
    for (i = min / 2; i > 0; i -= 5) {
699
        SSD1306_Fill_Triangle(ssd1306_data_p->_width / 2, ssd1306_data_p->_height / 2 - i,
700
                ssd1306_data_p->_width / 2 - i, ssd1306_data_p->_height / 2 + i,
701
                ssd1306_data_p->_width / 2 + i, ssd1306_data_p->_height / 2 + i, SSD1306_WHITE);
702
        if (color == SSD1306_WHITE) color = SSD1306_BLACK;
703
        else color = SSD1306_WHITE;
704
        SSD1306_Display();
705
    }
706
}
707
 
708
void SSD1306_Test_DrawRoundRect(void) {
709
    int16_t i;
710
    for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
711
        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);
712
        SSD1306_Display();
713
    }
714
}
715
 
716
void SSD1306_Test_FillRoundRect(void) {
717
    uint8_t color = SSD1306_WHITE;
718
    int16_t i;
719
    for (i = 0; i < ssd1306_data_p->_height / 2 - 2; i += 2) {
720
        SSD1306_Fill_Round_Rect(i, i, ssd1306_data_p->_width - 2 * i, ssd1306_data_p->_height - 2 * i, ssd1306_data_p->_height / 4, color);
721
        if (color == SSD1306_WHITE) color = SSD1306_BLACK;
722
        else color = SSD1306_WHITE;
723
        SSD1306_Display();
724
    }
725
}
726
 
727
void SSD1306_Test_DrawLine(void) {
728
    int16_t i;
729
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
730
        SSD1306_Draw_Line(0, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
731
        SSD1306_Display();
732
    }
733
    for (i = 0; i < ssd1306_data_p->_height; i += 4) {
734
        SSD1306_Draw_Line(0, 0, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
735
        SSD1306_Display();
736
    }
737
    __delay_ms(10);
738
 
739
    SSD1306_Clear_Display();
740
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
741
        SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
742
        SSD1306_Display();
743
    }
744
    for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
745
        SSD1306_Draw_Line(0, ssd1306_data_p->_height - 1, ssd1306_data_p->_width - 1, i, SSD1306_WHITE);
746
        SSD1306_Display();
747
    }
748
    __delay_ms(10);
749
 
750
    SSD1306_Clear_Display();
751
    for (i = ssd1306_data_p->_width - 1; i >= 0; i -= 4) {
752
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, i, 0, SSD1306_WHITE);
753
        SSD1306_Display();
754
    }
755
    for (i = ssd1306_data_p->_height - 1; i >= 0; i -= 4) {
756
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, ssd1306_data_p->_height - 1, 0, i, SSD1306_WHITE);
757
        SSD1306_Display();
758
    }
759
    __delay_ms(10);
760
 
761
    SSD1306_Clear_Display();
762
    for (i = 0; i < ssd1306_data_p->_height; i += 4) {
763
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, 0, i, SSD1306_WHITE);
764
        SSD1306_Display();
765
    }
766
    for (i = 0; i < ssd1306_data_p->_width; i += 4) {
767
        SSD1306_Draw_Line(ssd1306_data_p->_width - 1, 0, i, ssd1306_data_p->_height - 1, SSD1306_WHITE);
768
        SSD1306_Display();
769
    }
770
    __delay_ms(10);
771
}