forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbus_core.c
More file actions
224 lines (197 loc) · 8.49 KB
/
bus_core.c
File metadata and controls
224 lines (197 loc) · 8.49 KB
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
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-module/displayio/bus_core.h"
#include "py/gc.h"
#include "py/runtime.h"
#if CIRCUITPY_FOURWIRE
#include "shared-bindings/fourwire/FourWire.h"
#endif
#if CIRCUITPY_I2CDISPLAYBUS
#include "shared-bindings/i2cdisplaybus/I2CDisplayBus.h"
#endif
#if CIRCUITPY_PARALLELDISPLAYBUS
#include "shared-bindings/paralleldisplaybus/ParallelBus.h"
#endif
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/time/__init__.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/display_core.h"
#include "supervisor/shared/display.h"
#include "supervisor/shared/tick.h"
#include <stdint.h>
#include <string.h>
#define DISPLAYIO_CORE_DEBUG(...) (void)0
// #define DISPLAYIO_CORE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
void displayio_display_bus_construct(displayio_display_bus_t *self,
mp_obj_t bus, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart,
uint16_t column_command, uint16_t row_command, uint16_t set_current_column_command, uint16_t set_current_row_command,
bool data_as_commands, bool always_toggle_chip_select, bool SH1107_addressing, bool address_little_endian) {
self->ram_width = ram_width;
self->ram_height = ram_height;
self->colstart = colstart;
self->rowstart = rowstart;
self->column_command = column_command;
self->row_command = row_command;
self->set_current_column_command = set_current_column_command;
self->set_current_row_command = set_current_row_command;
self->data_as_commands = data_as_commands;
self->always_toggle_chip_select = always_toggle_chip_select;
self->SH1107_addressing = SH1107_addressing;
self->address_little_endian = address_little_endian;
#if CIRCUITPY_PARALLELDISPLAYBUS
if (mp_obj_is_type(bus, ¶lleldisplaybus_parallelbus_type)) {
self->bus_reset = common_hal_paralleldisplaybus_parallelbus_reset;
self->bus_free = common_hal_paralleldisplaybus_parallelbus_bus_free;
self->begin_transaction = common_hal_paralleldisplaybus_parallelbus_begin_transaction;
self->send = common_hal_paralleldisplaybus_parallelbus_send;
self->end_transaction = common_hal_paralleldisplaybus_parallelbus_end_transaction;
self->collect_ptrs = common_hal_paralleldisplaybus_parallelbus_collect_ptrs;
} else
#endif
#if CIRCUITPY_FOURWIRE
if (mp_obj_is_type(bus, &fourwire_fourwire_type)) {
self->bus_reset = common_hal_fourwire_fourwire_reset;
self->bus_free = common_hal_fourwire_fourwire_bus_free;
self->begin_transaction = common_hal_fourwire_fourwire_begin_transaction;
self->send = common_hal_fourwire_fourwire_send;
self->end_transaction = common_hal_fourwire_fourwire_end_transaction;
self->collect_ptrs = common_hal_fourwire_fourwire_collect_ptrs;
} else
#endif
#if CIRCUITPY_I2CDISPLAYBUS
if (mp_obj_is_type(bus, &i2cdisplaybus_i2cdisplaybus_type)) {
self->bus_reset = common_hal_i2cdisplaybus_i2cdisplaybus_reset;
self->bus_free = common_hal_i2cdisplaybus_i2cdisplaybus_bus_free;
self->begin_transaction = common_hal_i2cdisplaybus_i2cdisplaybus_begin_transaction;
self->send = common_hal_i2cdisplaybus_i2cdisplaybus_send;
self->end_transaction = common_hal_i2cdisplaybus_i2cdisplaybus_end_transaction;
self->collect_ptrs = common_hal_i2cdisplaybus_i2cdisplaybus_collect_ptrs;
} else
#endif
{
mp_raise_ValueError(MP_ERROR_TEXT("Unsupported display bus type"));
}
self->bus = bus;
}
bool displayio_display_bus_is_free(displayio_display_bus_t *self) {
return !self->bus || self->bus_free(self->bus);
}
bool displayio_display_bus_begin_transaction(displayio_display_bus_t *self) {
mp_obj_base_t *bus_base = MP_OBJ_TO_PTR(self->bus);
if (bus_base->type == &mp_type_NoneType) {
return false;
}
return self->begin_transaction(self->bus);
}
void displayio_display_bus_end_transaction(displayio_display_bus_t *self) {
self->end_transaction(self->bus);
}
void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, displayio_display_core_t *display, displayio_area_t *area) {
uint16_t x1 = area->x1 + self->colstart;
uint16_t x2 = area->x2 + self->colstart;
uint16_t y1 = area->y1 + self->rowstart;
uint16_t y2 = area->y2 + self->rowstart;
// Collapse down the dimension where multiple pixels are in a byte.
if (display->colorspace.depth < 8) {
uint8_t pixels_per_byte = 8 / display->colorspace.depth;
if (display->colorspace.pixels_in_byte_share_row) {
x1 /= pixels_per_byte * display->colorspace.bytes_per_cell;
x2 /= pixels_per_byte * display->colorspace.bytes_per_cell;
} else {
y1 /= pixels_per_byte * display->colorspace.bytes_per_cell;
y2 /= pixels_per_byte * display->colorspace.bytes_per_cell;
}
}
x2 -= 1;
y2 -= 1;
display_chip_select_behavior_t chip_select = CHIP_SELECT_UNTOUCHED;
if (self->always_toggle_chip_select || self->data_as_commands) {
chip_select = CHIP_SELECT_TOGGLE_EVERY_BYTE;
}
// Set column.
displayio_display_bus_begin_transaction(self);
uint8_t data[5];
data[0] = self->column_command;
uint8_t data_length = 1;
display_byte_type_t data_type = DISPLAY_DATA;
if (!self->data_as_commands) {
self->send(self->bus, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, 1);
data_length = 0;
} else {
data_type = DISPLAY_COMMAND;
}
if (self->ram_width < 0x100) {
data[data_length++] = x1;
data[data_length++] = x2;
} else {
if (self->address_little_endian) {
x1 = __builtin_bswap16(x1);
x2 = __builtin_bswap16(x2);
}
data[data_length++] = x1 >> 8;
data[data_length++] = x1 & 0xff;
data[data_length++] = x2 >> 8;
data[data_length++] = x2 & 0xff;
}
// Quirk for SH1107 "SH1107_addressing"
// Column lower command = 0x00, Column upper command = 0x10
if (self->SH1107_addressing) {
data[0] = ((x1 >> 4) & 0x0F) | 0x10; // 0x10 to 0x17
data[1] = x1 & 0x0F; // 0x00 to 0x0F
data_length = 2;
}
self->send(self->bus, data_type, chip_select, data, data_length);
displayio_display_bus_end_transaction(self);
if (self->set_current_column_command != NO_COMMAND) {
uint8_t command = self->set_current_column_command;
displayio_display_bus_begin_transaction(self);
self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
// Only send the first half of data because it is the first coordinate.
self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);
displayio_display_bus_end_transaction(self);
}
// Set row.
displayio_display_bus_begin_transaction(self);
data[0] = self->row_command;
data_length = 1;
if (!self->data_as_commands) {
self->send(self->bus, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, 1);
data_length = 0;
}
if (self->ram_height < 0x100) {
data[data_length++] = y1;
data[data_length++] = y2;
} else {
if (self->address_little_endian) {
y1 = __builtin_bswap16(y1);
y2 = __builtin_bswap16(y2);
}
data[data_length++] = y1 >> 8;
data[data_length++] = y1 & 0xff;
data[data_length++] = y2 >> 8;
data[data_length++] = y2 & 0xff;
}
// Quirk for SH1107 "SH1107_addressing"
// Page address command = 0xB0
if (self->SH1107_addressing) {
// set the page to our y value
data[0] = 0xB0 | y1;
data_length = 1;
}
self->send(self->bus, data_type, chip_select, data, data_length);
displayio_display_bus_end_transaction(self);
if (self->set_current_row_command != NO_COMMAND) {
uint8_t command = self->set_current_row_command;
displayio_display_bus_begin_transaction(self);
self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
// Only send the first half of data because it is the first coordinate.
self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);
displayio_display_bus_end_transaction(self);
}
}
void displayio_display_bus_collect_ptrs(displayio_display_bus_t *self) {
self->collect_ptrs(self->bus);
}