-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
273 lines (198 loc) · 5.97 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "lvgl/lvgl.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#define ADDR 0x3C
#define WIDTH 128
#define HEIGHT 64
#define BUFFER_SIZE (WIDTH * HEIGHT)
#define I2C i2c1
enum Command {
COL_ADDR_LOW = 0x00,
COL_ADDR_HIGH = 0x10,
ADDRESS_MODE = 0x20,
CONTRAST = 0x81,
SEGMENT_REMAP = 0xA0,
MULTIPLEX = 0xA8,
ENTIRE_DISPLAY_ON = 0xA4,
REVERSE = 0xA6,
OFFSET = 0xD3,
DC_DC = 0xAD,
ON_OFF = 0xAE,
PAGE_ADDR = 0xB0,
COM_SCAN_DIR = 0xC0,
INTERNAL_CLOCK = 0xD5,
DISCHARGE_PRECHARGE = 0xD9,
VCOM_DESELECT = 0xDB,
START_LINE = 0xDC,
READ_MODIFY_WRITE = 0xE0,
READ_MODIFY_WRITE_END = 0xEE,
NOP = 0xE3,
};
#define PAGES (HEIGHT / 8)
#define BYTES_PER_PAGE WIDTH
const uint8_t startup[] = {
ON_OFF | 0,
START_LINE, 0,
CONTRAST, 0x2f,
ADDRESS_MODE | 0,
SEGMENT_REMAP | 0,
COM_SCAN_DIR | 0xF,
MULTIPLEX, 0x7F,
OFFSET, 0x60,
INTERNAL_CLOCK, 0x51,
DISCHARGE_PRECHARGE, (2) << 4 + (2),
VCOM_DESELECT, 0x35,
PAGE_ADDR | 0,
ENTIRE_DISPLAY_ON | 0,
REVERSE | 0,
ON_OFF | 1
};
void init_i2c() {
i2c_init(I2C, 100 * 1000);
gpio_set_function(2, GPIO_FUNC_I2C);
gpio_set_function(3, GPIO_FUNC_I2C);
gpio_pull_up(2);
gpio_pull_up(3);
}
void send_command(uint8_t command) {
uint8_t data[] = {0x80, command};
i2c_write_blocking(
I2C,
ADDR,
data,
2,
false
);
}
void send_data(const uint8_t* data, size_t length) {
uint8_t* buffer = malloc(length + 1);
memcpy(buffer + 1, data, length);
buffer[0] = 0x40;
i2c_write_blocking(
I2C,
ADDR,
buffer,
length + 1,
false
);
free(buffer);
}
void my_flush_cb(lv_disp_drv_t* disp_drv, const lv_area_t* area, lv_color_t* color_p) {
printf("Flushing area x1=%02x x2=%02x y1=%02x y2=%02x\n", area->x1, area->x2, area->y1, area->y2);
uint8_t start_page = area->x1 >> 3;
uint8_t end_page = area->x2 >> 3;
uint8_t start_col = area->y1;
uint8_t end_col = area->y2 + 1;
uint8_t transfer_size = end_col - start_col;
for (uint8_t page = start_page; page <= end_page; ++page) {
send_command(COL_ADDR_HIGH | 0);
send_command(COL_ADDR_LOW | 0);
send_command(PAGE_ADDR | page);
void* data = color_p + page * WIDTH + start_col;
send_data(data, transfer_size);
}
lv_disp_flush_ready(disp_drv);
}
void my_rounder_cb(lv_disp_drv_t* disp_drv, lv_area_t* area) {
area->x1 &= ~0x7;
area->x2 &= ~0x7;
area->x2 += 7;
// area->y1 = 0;
// area->y2 = HEIGHT - 1;
// area->x1 = 0;
// area->x2 = WIDTH - 1;
}
void my_set_px_cb(lv_disp_drv_t* disp_drv, uint8_t* buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y, lv_color_t color, lv_opa_t opa) {
uint16_t page = x >> 3;
uint16_t column = y;
uint8_t bit = x & 0x7;
uint8_t mask = 1 << bit;
uint16_t buffer_index = page * WIDTH + column;
if (color.full == 0) {
buf[buffer_index] |= mask;
} else {
buf[buffer_index] &= ~mask;
}
}
void do_tick_inc() {
lv_tick_inc(5);
}
int main() {
stdio_init_all();
sleep_ms(5000);
printf("setting up i2c\n");
init_i2c();
for (uint8_t i = 0; i < sizeof(startup)/sizeof(*startup); ++i) {
uint8_t command = startup[i];
// printf("sending command %02x\n", command);
send_command(command);
}
// static lv_color_t buffer[BUFFER_SIZE];
// // for (int x = 0; x < WIDTH; x += 10) {
// // for (int y = 0; y < HEIGHT; y += 10) {
// // my_set_px_cb(NULL, buffer, WIDTH, x, y, lv_color_white(), 0);
// // }
// // }
// for (int row = 0; row < HEIGHT; row += 10) {
// for (int y = row; y < row + 5; ++y) {
// for (int x = 0; x < WIDTH; ++x) {
// my_set_px_cb(NULL, buffer, WIDTH, x, y, lv_color_white(), 0);
// }
// }
// }
// for (int y = 0; y < HEIGHT; ++y) {
// int x = (WIDTH / 2.0) + sin(y / 20.0) * (WIDTH / 2.0);
// my_set_px_cb(NULL, buffer, WIDTH, x, y, lv_color_white(), 0);
// }
// for (int y = 0; y < HEIGHT; ++y) {
// int x = (WIDTH / 2.0) + cos(y / 20.0) * (WIDTH / 2.0);
// my_set_px_cb(NULL, buffer, WIDTH, x, y, lv_color_black(), 0);
// }
// lv_area_t everywhere;
// everywhere.x1 = 0;
// everywhere.x2 = WIDTH - 1;
// everywhere.y1 = 0;
// everywhere.y2 = HEIGHT - 1;
// my_flush_cb(NULL, &everywhere, buffer);
lv_init();
static lv_disp_draw_buf_t disp_buf;
static lv_color_t buf_1[BUFFER_SIZE];
// static lv_color_t buf_2[BUFFER_SIZE];
lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, BUFFER_SIZE);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.draw_buf = &disp_buf;
disp_drv.flush_cb = my_flush_cb;
disp_drv.rounder_cb = my_rounder_cb;
disp_drv.set_px_cb = my_set_px_cb;
disp_drv.hor_res = WIDTH;
disp_drv.ver_res = HEIGHT;
lv_disp_t* disp;
disp = lv_disp_drv_register(&disp_drv);
// lv_theme_t* theme = lv_theme_mono_init(disp, true, NULL);
lv_obj_t* label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Hello!");
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
// lv_obj_t* rectangle = lv_obj_create(lv_scr_act());
// lv_obj_set_size(rectangle, 32, 32);
// lv_obj_set_pos(rectangle, 32, 32);
// lv_style_t style;
// lv_style_init(&style);
// lv_style_set_bg_color(&style, lv_color_white());
// lv_style_set_border_width(&style, 2);
// lv_style_set_border_color(&style, lv_color_black());
// lv_obj_add_style(rectangle, &style, 0);
printf("Label created\n");
repeating_timer_t timer;
add_repeating_timer_ms(5, do_tick_inc, NULL, &timer);
printf("Tick timer initialized\n");
while (true) {
lv_task_handler();
sleep_ms(500);
}
return 0;
}