-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathssd1306xled.cpp
301 lines (255 loc) · 9.46 KB
/
ssd1306xled.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* SSD1306xLED - Drivers for SSD1306 controlled dot matrix OLED/PLED 128x64 displays
*
* @created: 2014-08-12
* @author: Neven Boyanov
*
* Source code available at: https://bitbucket.org/tinusaur/ssd1306xled
*
* Modified by Tejashwi Kalp Taru, with the help of TinyI2C (https://github.com/technoblogy/tiny-i2c/)
* Modified code available at: https://github.com/tejashwikalptaru/ssd1306xled
*/
// ----------------------------------------------------------------------------
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "ssd1306xled.h"
#include "font6x8.h"
#include "font8x16.h"
// ----------------------------------------------------------------------------
// Some code based on "IIC_wtihout_ACK" by http://www.14blog.com/archives/1358
const uint8_t ssd1306_init_sequence [] PROGMEM = { // Initialization Sequence
0xAE, // Set Display ON/OFF - AE=OFF, AF=ON
0xD5, 0xF0, // Set display clock divide ratio/oscillator frequency, set divide ratio
0xA8, 0x3F, // Set multiplex ratio (1 to 64) ... (height - 1)
0xD3, 0x00, // Set display offset. 00 = no offset
0x40 | 0x00, // Set start line address, at 0.
0x8D, 0x14, // Charge Pump Setting, 14h = Enable Charge Pump
0x20, 0x00, // Set Memory Addressing Mode - 00=Horizontal, 01=Vertical, 10=Page, 11=Invalid
0xA0 | 0x01, // Set Segment Re-map
0xC8, // Set COM Output Scan Direction
0xDA, 0x12, // Set COM Pins Hardware Configuration - 128x32:0x02, 128x64:0x12
0x81, 0x3F, // Set contrast control register
0xD9, 0x22, // Set pre-charge period (0x22 or 0xF1)
0xDB, 0x20, // Set Vcomh Deselect Level - 0x00: 0.65 x VCC, 0x20: 0.77 x VCC (RESET), 0x30: 0.83 x VCC
0xA4, // Entire Display ON (resume) - output RAM to display
0xA6, // Set Normal/Inverse Display mode. A6=Normal; A7=Inverse
0x2E, // Deactivate Scroll command
0xAF, // Set Display ON/OFF - AE=OFF, AF=ON
};
// Program: 5248 bytes
SSD1306Device::SSD1306Device(void){}
void SSD1306Device::I2CInit() {
PORT_USI |= 1<<PIN_USI_SDA; // Enable pullup on SDA.
PORT_USI_CL |= 1<<PIN_USI_SCL; // Enable pullup on SCL.
DDR_USI_CL |= 1<<PIN_USI_SCL; // Enable SCL as output.
DDR_USI |= 1<<PIN_USI_SDA; // Enable SDA as output.
USIDR = 0xFF; // Preload data register with "released level" data.
USICR = 0<<USISIE | 0<<USIOIE | // Disable Interrupts.
1<<USIWM1 | 0<<USIWM0 | // Set USI in Two-wire mode.
1<<USICS1 | 0<<USICS0 | 1<<USICLK | // Software stobe as counter clock source
0<<USITC;
USISR = 1<<USISIF | 1<<USIOIF | 1<<USIPF | 1<<USIDC | // Clear flags,
0x0<<USICNT0; // and reset counter.
}
bool SSD1306Device::I2CStart(uint8_t address, int readcount) {
if (readcount != 0) { I2Ccount = readcount; readcount = 1; }
uint8_t addressRW = address<<1 | readcount;
/* Release SCL to ensure that (repeated) Start can be performed */
PORT_USI_CL |= 1<<PIN_USI_SCL; // Release SCL.
while (!(PIN_USI_CL & 1<<PIN_USI_SCL)); // Verify that SCL becomes high.
#ifdef TWI_FAST_MODE
DELAY_T4TWI;
#else
DELAY_T2TWI;
#endif
/* Generate Start Condition */
PORT_USI &= ~(1<<PIN_USI_SDA); // Force SDA LOW.
DELAY_T4TWI;
PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
PORT_USI |= 1<<PIN_USI_SDA; // Release SDA.
if (!(USISR & 1<<USISIF)) return false;
/*Write address */
PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
USIDR = addressRW; // Setup data.
I2CTransfer(USISR_8bit); // Send 8 bits on bus.
/* Clock and verify (N)ACK from slave */
DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
if (I2CTransfer(USISR_1bit) & 1<<TWI_NACK_BIT) return false; // No ACK
return true; // Start successfully completed
}
uint8_t SSD1306Device::I2CTransfer (uint8_t data) {
USISR = data; // Set USISR according to data.
// Prepare clocking.
data = 0<<USISIE | 0<<USIOIE | // Interrupts disabled
1<<USIWM1 | 0<<USIWM0 | // Set USI in Two-wire mode.
1<<USICS1 | 0<<USICS0 | 1<<USICLK | // Software clock strobe as source.
1<<USITC; // Toggle Clock Port.
do {
DELAY_T2TWI;
USICR = data; // Generate positive SCL edge.
while (!(PIN_USI_CL & 1<<PIN_USI_SCL)); // Wait for SCL to go high.
DELAY_T4TWI;
USICR = data; // Generate negative SCL edge.
} while (!(USISR & 1<<USIOIF)); // Check for transfer complete.
DELAY_T2TWI;
data = USIDR; // Read out data.
USIDR = 0xFF; // Release SDA.
DDR_USI |= (1<<PIN_USI_SDA); // Enable SDA as output.
return data; // Return the data from the USIDR
}
void SSD1306Device::I2CStop (void) {
PORT_USI &= ~(1<<PIN_USI_SDA); // Pull SDA low.
PORT_USI_CL |= 1<<PIN_USI_SCL; // Release SCL.
while (!(PIN_USI_CL & 1<<PIN_USI_SCL)); // Wait for SCL to go high.
DELAY_T4TWI;
PORT_USI |= 1<<PIN_USI_SDA; // Release SDA.
DELAY_T2TWI;
}
void SSD1306Device::begin() {
I2CInit();
#ifndef TINY4KOLED_QUICK_BEGIN
while (!I2CStart(SSD1306_SA, 0)) {
delay(10);
}
I2CStop();
#endif
}
bool SSD1306Device::I2CWrite(uint8_t data) {
/* Write a byte */
PORT_USI_CL &= ~(1<<PIN_USI_SCL); // Pull SCL LOW.
USIDR = data; // Setup data.
I2CTransfer(USISR_8bit); // Send 8 bits on bus.
/* Clock and verify (N)ACK from slave */
DDR_USI &= ~(1<<PIN_USI_SDA); // Enable SDA as input.
if (I2CTransfer(USISR_1bit) & 1<<TWI_NACK_BIT) return false;
return true;
}
void SSD1306Device::ssd1306_init(void)
{
begin();
ssd1306_send_command_start();
for (uint8_t i = 0; i < sizeof (ssd1306_init_sequence); i++) {
ssd1306_send_byte(pgm_read_byte(&ssd1306_init_sequence[i]));
}
ssd1306_send_command_stop();
ssd1306_fillscreen(0);
}
// A shorter init saves 52 flash bytes (if zeroed screen is not required)
// The code of 'ssd1306_init()' is replicated to allow the linker to drop the unused method during linkage.
void SSD1306Device::ssd1306_tiny_init(void)
{
begin();
ssd1306_send_command_start();
for (uint8_t i = 0; i < sizeof (ssd1306_init_sequence); i++) {
ssd1306_send_byte(pgm_read_byte(&ssd1306_init_sequence[i]));
}
ssd1306_send_command_stop();
// save 52 bytes :)
//ssd1306_fillscreen(0);
}
void SSD1306Device::ssd1306_send_command_start(void) {
I2CStop();
I2CStart(SSD1306_SA, 0);
I2CWrite(SSD1306_COMMAND);
}
void SSD1306Device::ssd1306_send_command_stop() {
I2CStop();
}
void SSD1306Device::ssd1306_send_command(uint8_t command) {
ssd1306_send_command_start();
ssd1306_send_byte(command);
ssd1306_send_command_stop();
}
void SSD1306Device::ssd1306_send_byte(uint8_t byte) {
I2CWrite(byte);
}
void SSD1306Device::ssd1306_send_data_start(void) {
I2CStop();
I2CStart(SSD1306_SA, 0);
I2CWrite(SSD1306_DATA);
}
void SSD1306Device::ssd1306_send_data_stop() {
I2CStop();
}
void SSD1306Device::ssd1306_fillscreen(uint8_t fill) {
ssd1306_setpos(0, 0);
ssd1306_send_data_start(); // Initiate transmission of data
for (uint16_t i = 0; i < 128 * 8 / 4; i++) {
ssd1306_send_byte(fill);
ssd1306_send_byte(fill);
ssd1306_send_byte(fill);
ssd1306_send_byte(fill);
}
ssd1306_send_data_stop(); // Finish transmission
}
void SSD1306Device::ssd1306_setpos(uint8_t x, uint8_t y)
{
ssd1306_send_command_start();
ssd1306_send_byte(0xb0 | (y & 0x07));
ssd1306_send_byte(0x10 | ((x & 0xf0) >> 4));
ssd1306_send_byte(x & 0x0f); // | 0x01
ssd1306_send_command_stop();
}
void SSD1306Device::ssd1306_char_font6x8(char ch) {
uint8_t i;
uint8_t c = ch - 32;
ssd1306_send_data_start();
for (i= 0; i < 6; i++)
{
ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font6x8[c * 6 + i]));
}
ssd1306_send_data_stop();
}
void SSD1306Device::ssd1306_string_font6x8(char *s) {
while (*s) {
ssd1306_char_font6x8(*s++);
}
}
void SSD1306Device::ssd1306_draw_bmp(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, const uint8_t bitmap[]){
uint16_t j = 0;
uint8_t y, x;
if (y1 % 8 == 0) y = y1 / 8;
else y = y1 / 8 + 1;
for (y = y0; y < y1; y++)
{
ssd1306_setpos(x0,y);
ssd1306_send_data_start();
for (x = x0; x < x1; x++)
{
ssd1306_send_byte(pgm_read_byte(&bitmap[j++]));
}
ssd1306_send_data_stop();
}
}
void SSD1306Device::ssd1306_char_f8x16(uint8_t x, uint8_t y, const char ch[])
{
uint8_t c, j, i = 0;
while (ch[j] != '\0')
{
c = ch[j] - 32;
if (x > 120)
{
x = 0;
y++;
}
ssd1306_setpos(x, y);
ssd1306_send_data_start();
for (i = 0; i < 8; i++)
{
ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8x16[c * 16 + i]));
}
ssd1306_send_data_stop();
ssd1306_setpos(x, y + 1);
ssd1306_send_data_start();
for (i = 0; i < 8; i++)
{
ssd1306_send_byte(pgm_read_byte(&ssd1306xled_font8x16[c * 16 + i + 8]));
}
ssd1306_send_data_stop();
x += 8;
j++;
}
}
SSD1306Device SSD1306;
// ----------------------------------------------------------------------------