-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd1306.c
250 lines (204 loc) · 7.01 KB
/
ssd1306.c
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
/*
MIT License
Copyright (c) 2021 David Schramm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pico/stdlib.h>
#include <hardware/i2c.h>
#include <pico/binary_info.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ssd1306.h"
#include "font.h"
// Match SDA to SDA and SCL to SCL
#define DISPLAY_SDA_PIN 0
#define DISPLAY_SCL_PIN 1
void setup_ssd_gpios(void) {
i2c_init(i2c0, 400000);
gpio_set_function(DISPLAY_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(DISPLAY_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(DISPLAY_SDA_PIN);
gpio_pull_up(DISPLAY_SCL_PIN);
}
inline static void swap(uint32_t *a, uint32_t *b) {
static uint32_t *t;
t=a;
*a=*b;
*b=*t;
}
inline static void fancy_write(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, char *name) {
switch(i2c_write_blocking(i2c, addr, src, len, false)) {
case PICO_ERROR_GENERIC:
printf("[%s] addr not acknowledged!\n", name);
break;
case PICO_ERROR_TIMEOUT:
printf("[%s] timeout!\n", name);
break;
default:
//printf("[%s] wrote successfully %lu bytes!\n", name, len);
break;
}
}
inline static void ssd1306_write(ssd1306_t *p, uint8_t val) {
static uint8_t d[2];
//d[2] = {0x00, val};
d[0] = 0x00;
d[1] = val;
fancy_write(p->i2c_i, p->address, d, 2, "ssd1306_write");
}
bool ssd1306_init(ssd1306_t *p, uint16_t width, uint16_t height, uint8_t address, i2c_inst_t *i2c_instance) {
p->width=width;
p->height=height;
p->pages=height/8;
p->address=address;
p->i2c_i=i2c_instance;
p->bufsize=(p->pages)*(p->width);
if((p->buffer=malloc(p->bufsize+1))==NULL) {
p->bufsize=0;
return false;
}
++(p->buffer);
// from https://github.com/makerportal/rpi-pico-ssd1306
int8_t cmds[]= {
SET_DISP | 0x00, // off
// address setting
SET_MEM_ADDR,
0x00, // horizontal
// resolution and layout
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01, // column addr 127 mapped to SEG0
SET_MUX_RATIO,
height - 1,
SET_COM_OUT_DIR | 0x08, // scan from COM[N] to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
width>2*height?0x02:0x12,
// timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
p->external_vcc?0x22:0xF1,
SET_VCOM_DESEL,
0x30, // 0.83*Vcc
// display
SET_CONTRAST,
0xFF, // maximum
SET_ENTIRE_ON, // output follows RAM contents
SET_NORM_INV, // not inverted
// charge pump
SET_CHARGE_PUMP,
p->external_vcc?0x10:0x14,
SET_DISP | 0x01
};
for(size_t i=0; i<sizeof(cmds); ++i)
ssd1306_write(p, cmds[i]);
return true;
}
inline void ssd1306_deinit(ssd1306_t *p) {
free(p->buffer-1);
}
inline void ssd1306_poweroff(ssd1306_t *p) {
ssd1306_write(p, SET_DISP|0x00);
}
inline void ssd1306_poweron(ssd1306_t *p) {
ssd1306_write(p, SET_DISP|0x01);
}
inline void ssd1306_contrast(ssd1306_t *p, uint8_t val) {
ssd1306_write(p, SET_CONTRAST);
ssd1306_write(p, val);
}
inline void ssd1306_invert(ssd1306_t *p, uint8_t inv) {
ssd1306_write(p, SET_NORM_INV | (inv & 1));
}
inline void ssd1306_clear(ssd1306_t *p) {
memset(p->buffer, 0, p->bufsize);
}
void ssd1306_draw_pixel(ssd1306_t *p, uint32_t x, uint32_t y) {
if(x>=p->width || y>=p->height) return;
p->buffer[x+p->width*(y>>3)]|=0x1<<(y&0x07); // y>>3==y/8 && y&0x7==y%8
}
void ssd1306_draw_line(ssd1306_t *p, int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
if(x1>x2) {
swap(&x1, &x2);
swap(&y1, &y2);
}
if(x1==x2) {
if(y1>y2)
swap(&y1, &y2);
for(int32_t i=y1; i<=y2; ++i)
ssd1306_draw_pixel(p, x1, i);
return;
}
float m=(float) (y2-y1) / (float) (x2-x1);
for(int32_t i=x1; i<=x2; ++i) {
float y=m*(float) (i-x1)+(float) y1;
ssd1306_draw_pixel(p, i, (uint32_t) y);
}
}
void ssd1306_draw_square(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
for(uint32_t i=0; i<width; ++i)
for(uint32_t j=0; j<height; ++j)
ssd1306_draw_pixel(p, x+i, y+j);
}
void ssd13606_draw_empty_square(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
ssd1306_draw_line(p, x, y, x+width, y);
ssd1306_draw_line(p, x, y+height, x+width, y+height);
ssd1306_draw_line(p, x, y, x, y+height);
ssd1306_draw_line(p, x+width, y, x+width, y+height);
}
void ssd1306_draw_char_with_font(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, const uint8_t *font, char c) {
if(c > '~')
return;
for(uint8_t i=0; i<font[1]; ++i) {
uint8_t line=(uint8_t)(font[(c-0x20)*font[1]+i+2]);
for(int8_t j=0; j<font[0]; ++j, line>>=1) {
if(line & 1 ==1)
ssd1306_draw_square(p, x+i*scale, y+j*scale, scale, scale);
}
}
}
void ssd1306_draw_string_with_font(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, const uint8_t *font, char *s) {
for(int32_t x_n=x; *s; x_n+=font[0]*scale) {
ssd1306_draw_char_with_font(p, x_n, y, scale, font, *(s++));
}
}
void ssd1306_draw_char(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, char c) {
ssd1306_draw_char_with_font(p, x, y, scale, font_8x5, c);
}
void ssd1306_draw_string(ssd1306_t *p, uint32_t x, uint32_t y, uint32_t scale, char *s) {
ssd1306_draw_string_with_font(p, x, y, scale, font_8x5, s);
}
void ssd1306_show(ssd1306_t *p) {
static uint8_t payload[6];
//{SET_COL_ADDR, 0, p->width-1, SET_PAGE_ADDR, 0, p->pages-1};
payload[0] = SET_COL_ADDR;
payload[1] = 0;
payload[2] = p->width-1;
payload[3] = SET_PAGE_ADDR;
payload[4] = 0;
payload[5] = p->pages-1;
if(p->width==64) {
payload[1]+=32;
payload[2]+=32;
}
for(size_t i=0; i<sizeof(payload); ++i)
ssd1306_write(p, payload[i]);
*(p->buffer-1)=0x40;
fancy_write(p->i2c_i, p->address, p->buffer-1, p->bufsize+1, "ssd1306_show");
}