aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-module/displayio/display_core.c
blob: 8b2f0bfdf8f7ab6bf261300900e90ce291e99622 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
 *
 * 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 "shared-bindings/displayio/Display.h"

#include "py/gc.h"
#include "py/runtime.h"
#include "shared-bindings/displayio/FourWire.h"
#include "shared-bindings/displayio/I2CDisplay.h"
#if CIRCUITPY_PARALLELDISPLAY
#include "shared-bindings/paralleldisplay/ParallelBus.h"
#endif
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/time/__init__.h"
#include "shared-module/displayio/__init__.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_core_construct(displayio_display_core_t *self,
    mp_obj_t bus, uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation,
    uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word) {
    self->colorspace.depth = color_depth;
    self->colorspace.grayscale = grayscale;
    self->colorspace.grayscale_bit = 8 - color_depth;
    self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row;
    self->colorspace.bytes_per_cell = bytes_per_cell;
    self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte;
    self->colorspace.reverse_bytes_in_word = reverse_bytes_in_word;
    self->colorspace.dither = false;
    self->current_group = NULL;
    self->colstart = colstart;
    self->rowstart = rowstart;
    self->last_refresh = 0;

    // (framebufferdisplay already validated its 'bus' is a buffer-protocol object)
    if (bus) {
        #if CIRCUITPY_PARALLELDISPLAY
        if (mp_obj_is_type(bus, &paralleldisplay_parallelbus_type)) {
            self->bus_reset = common_hal_paralleldisplay_parallelbus_reset;
            self->bus_free = common_hal_paralleldisplay_parallelbus_bus_free;
            self->begin_transaction = common_hal_paralleldisplay_parallelbus_begin_transaction;
            self->send = common_hal_paralleldisplay_parallelbus_send;
            self->end_transaction = common_hal_paralleldisplay_parallelbus_end_transaction;
        } else
        #endif
        if (mp_obj_is_type(bus, &displayio_fourwire_type)) {
            self->bus_reset = common_hal_displayio_fourwire_reset;
            self->bus_free = common_hal_displayio_fourwire_bus_free;
            self->begin_transaction = common_hal_displayio_fourwire_begin_transaction;
            self->send = common_hal_displayio_fourwire_send;
            self->end_transaction = common_hal_displayio_fourwire_end_transaction;
        } else if (mp_obj_is_type(bus, &displayio_i2cdisplay_type)) {
            self->bus_reset = common_hal_displayio_i2cdisplay_reset;
            self->bus_free = common_hal_displayio_i2cdisplay_bus_free;
            self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction;
            self->send = common_hal_displayio_i2cdisplay_send;
            self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction;
        } else {
            mp_raise_ValueError(translate("Unsupported display bus type"));
        }
    }
    self->bus = bus;


    // (offsetof core is equal in all display types)
    if (self == &displays[0].display.core) {
        supervisor_start_terminal(width, height);
    }

    self->width = width;
    self->height = height;
    self->ram_width = ram_width;
    self->ram_height = ram_height;

    displayio_display_core_set_rotation(self, rotation);
}

void displayio_display_core_set_rotation(displayio_display_core_t *self,
    int rotation) {
    int height = self->height;
    int width = self->width;

    rotation = rotation % 360;
    self->rotation = rotation;
    self->transform.x = 0;
    self->transform.y = 0;
    self->transform.scale = 1;
    self->transform.mirror_x = false;
    self->transform.mirror_y = false;
    self->transform.transpose_xy = false;
    if (rotation == 0 || rotation == 180) {
        if (rotation == 180) {
            self->transform.mirror_x = true;
            self->transform.mirror_y = true;
        }
    } else {
        self->transform.transpose_xy = true;
        if (rotation == 270) {
            self->transform.mirror_y = true;
        } else {
            self->transform.mirror_x = true;
        }
    }

    self->area.x1 = 0;
    self->area.y1 = 0;
    self->area.next = NULL;

    self->transform.dx = 1;
    self->transform.dy = 1;
    if (self->transform.transpose_xy) {
        self->area.x2 = height;
        self->area.y2 = width;
        if (self->transform.mirror_x) {
            self->transform.x = height;
            self->transform.dx = -1;
        }
        if (self->transform.mirror_y) {
            self->transform.y = width;
            self->transform.dy = -1;
        }
    } else {
        self->area.x2 = width;
        self->area.y2 = height;
        if (self->transform.mirror_x) {
            self->transform.x = width;
            self->transform.dx = -1;
        }
        if (self->transform.mirror_y) {
            self->transform.y = height;
            self->transform.dy = -1;
        }
    }
}

bool displayio_display_core_show(displayio_display_core_t *self, displayio_group_t *root_group) {

    if (root_group == NULL) { // set the display to the REPL, reset REPL position and size
        circuitpython_splash.in_group = false;
        // force the circuit_python_splash out of any group (Note: could cause problems with the parent group)
        circuitpython_splash.x = 0; // reset position in case someone moved it.
        circuitpython_splash.y = 0;

        supervisor_start_terminal(self->width, self->height);

        root_group = &circuitpython_splash;
    }
    if (root_group == self->current_group) {
        return true;
    }
    if (root_group != NULL && root_group->in_group) {
        return false;
    }
    if (self->current_group != NULL) {
        self->current_group->in_group = false;
    }

    if (root_group != NULL) {
        displayio_group_update_transform(root_group, &self->transform);
        root_group->in_group = true;
    }
    self->current_group = root_group;
    self->full_refresh = true;
    return true;
}

uint16_t displayio_display_core_get_width(displayio_display_core_t *self) {
    return self->width;
}

uint16_t displayio_display_core_get_height(displayio_display_core_t *self) {
    return self->height;
}

void displayio_display_core_set_dither(displayio_display_core_t *self, bool dither) {
    self->colorspace.dither = dither;
}

bool displayio_display_core_get_dither(displayio_display_core_t *self) {
    return self->colorspace.dither;
}

bool displayio_display_core_bus_free(displayio_display_core_t *self) {
    return !self->bus || self->bus_free(self->bus);
}

bool displayio_display_core_begin_transaction(displayio_display_core_t *self) {
    return self->begin_transaction(self->bus);
}

void displayio_display_core_end_transaction(displayio_display_core_t *self) {
    self->end_transaction(self->bus);
}

void displayio_display_core_set_region_to_update(displayio_display_core_t *self, uint8_t column_command,
    uint8_t row_command, uint16_t set_current_column_command, uint16_t set_current_row_command,
    bool data_as_commands, bool always_toggle_chip_select,
    displayio_area_t *area, bool SH1107_addressing) {
    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 (self->colorspace.depth < 8) {
        uint8_t pixels_per_byte = 8 / self->colorspace.depth;
        if (self->colorspace.pixels_in_byte_share_row) {
            x1 /= pixels_per_byte * self->colorspace.bytes_per_cell;
            x2 /= pixels_per_byte * self->colorspace.bytes_per_cell;
        } else {
            y1 /= pixels_per_byte * self->colorspace.bytes_per_cell;
            y2 /= pixels_per_byte * self->colorspace.bytes_per_cell;
        }
    }

    x2 -= 1;
    y2 -= 1;

    display_chip_select_behavior_t chip_select = CHIP_SELECT_UNTOUCHED;
    if (always_toggle_chip_select || data_as_commands) {
        chip_select = CHIP_SELECT_TOGGLE_EVERY_BYTE;
    }

    // Set column.
    displayio_display_core_begin_transaction(self);
    uint8_t data[5];
    data[0] = column_command;
    uint8_t data_length = 1;
    display_byte_type_t data_type = DISPLAY_DATA;
    if (!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 {
        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 (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_core_end_transaction(self);

    if (set_current_column_command != NO_COMMAND) {
        uint8_t command = set_current_column_command;
        displayio_display_core_begin_transaction(self);
        self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
        self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);
        displayio_display_core_end_transaction(self);
    }


    // Set row.
    displayio_display_core_begin_transaction(self);
    data[0] = row_command;
    data_length = 1;
    if (!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 {
        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 (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_core_end_transaction(self);

    if (set_current_row_command != NO_COMMAND) {
        uint8_t command = set_current_row_command;
        displayio_display_core_begin_transaction(self);
        self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
        self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);
        displayio_display_core_end_transaction(self);
    }
}

bool displayio_display_core_start_refresh(displayio_display_core_t *self) {
    if (!displayio_display_core_bus_free(self)) {
        // Can't acquire display bus; skip updating this display. Try next display.
        return false;
    }
    if (self->refresh_in_progress) {
        return false;
    }
    self->refresh_in_progress = true;
    self->last_refresh = supervisor_ticks_ms64();
    return true;
}

void displayio_display_core_finish_refresh(displayio_display_core_t *self) {
    if (self->current_group != NULL) {
        DISPLAYIO_CORE_DEBUG("displayiocore group_finish_refresh\n");
        displayio_group_finish_refresh(self->current_group);
    }
    self->full_refresh = false;
    self->refresh_in_progress = false;
    self->last_refresh = supervisor_ticks_ms64();
}

void release_display_core(displayio_display_core_t *self) {
    if (self->current_group != NULL) {
        self->current_group->in_group = false;
    }
}

void displayio_display_core_collect_ptrs(displayio_display_core_t *self) {
    gc_collect_ptr(self->current_group);
}

bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer) {
    return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer);
}

bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped) {
    bool overlaps = displayio_area_compute_overlap(&self->area, area, clipped);
    if (!overlaps) {
        return false;
    }
    // Expand the area if we have multiple pixels per byte and we need to byte
    // align the bounds.
    if (self->colorspace.depth < 8) {
        uint8_t pixels_per_byte = 8 / self->colorspace.depth * self->colorspace.bytes_per_cell;
        if (self->colorspace.pixels_in_byte_share_row) {
            if (clipped->x1 % pixels_per_byte != 0) {
                clipped->x1 -= clipped->x1 % pixels_per_byte;
            }
            if (clipped->x2 % pixels_per_byte != 0) {
                clipped->x2 += pixels_per_byte - clipped->x2 % pixels_per_byte;
            }
        } else {
            if (clipped->y1 % pixels_per_byte != 0) {
                clipped->y1 -= clipped->y1 % pixels_per_byte;
            }
            if (clipped->y2 % pixels_per_byte != 0) {
                clipped->y2 += pixels_per_byte - clipped->y2 % pixels_per_byte;
            }
        }
    }
    return true;
}