summaryrefslogtreecommitdiff
path: root/shared-module/displayio/__init__.c
blob: f09e4e791a6e929a4066f420bb9ee42a30381922 (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
#include <string.h>
#include "shared-module/displayio/__init__.h"

#include "shared-bindings/displayio/Bitmap.h"
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/Palette.h"
#include "supervisor/shared/display.h"
#include "supervisor/memory.h"
#include "supervisor/usb.h"

primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];

void displayio_refresh_displays(void) {
    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        if (displays[i].display.base.type == NULL || displays[i].display.base.type == &mp_type_NoneType) {
            continue;
        }
        displayio_display_obj_t* display = &displays[i].display;
        displayio_display_update_backlight(display);

        if (!displayio_display_frame_queued(display)) {
            return;
        }
        if (displayio_display_refresh_queued(display)) {
            // We compute the pixels
            uint16_t x0 = 0;
            uint16_t y0 = 0;
            uint16_t x1 = display->width;
            uint16_t y1 = display->height;
            size_t index = 0;
            //size_t row_size = (x1 - x0);
            uint16_t buffer_size = 256;
            uint32_t buffer[buffer_size / 2];
            displayio_display_start_region_update(display, x0, y0, x1, y1);
            for (uint16_t y = y0; y < y1; ++y) {
                for (uint16_t x = x0; x < x1; ++x) {
                    uint16_t* pixel = &(((uint16_t*)buffer)[index]);
                    *pixel = 0;

                    if (display->current_group != NULL) {
                        displayio_group_get_pixel(display->current_group, x, y, pixel);
                    }

                    index += 1;
                    // The buffer is full, send it.
                    if (index >= buffer_size) {
                        if (!displayio_display_send_pixels(display, buffer, buffer_size / 2)) {
                            displayio_display_finish_region_update(display);
                            return;
                        }
                        // TODO(tannewt): Make refresh displays faster so we don't starve other
                        // background tasks.
                        usb_background();
                        index = 0;
                    }
                }
            }
            // Send the remaining data.
            if (index && !displayio_display_send_pixels(display, buffer, index * 2)) {
                displayio_display_finish_region_update(display);
                return;
            }
            displayio_display_finish_region_update(display);
        }
        displayio_display_finish_refresh(display);
    }
}

void common_hal_displayio_release_displays(void) {
    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        mp_const_obj_t bus_type = displays[i].fourwire_bus.base.type;
        if (bus_type == NULL) {
            continue;
        } else if (bus_type == &displayio_fourwire_type) {
            common_hal_displayio_fourwire_deinit(&displays[i].fourwire_bus);
        } else if (bus_type == &displayio_parallelbus_type) {
            common_hal_displayio_parallelbus_deinit(&displays[i].parallel_bus);
        }
        displays[i].fourwire_bus.base.type = &mp_type_NoneType;
    }
    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        release_display(&displays[i].display);
        displays[i].display.base.type = &mp_type_NoneType;
    }

    supervisor_stop_terminal();
}

void reset_displays(void) {
    #if CIRCUITPY_DISPLAYIO
    // The SPI buses used by FourWires may be allocated on the heap so we need to move them inline.
    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        if (displays[i].fourwire_bus.base.type != &displayio_fourwire_type) {
            continue;
        }
        displayio_fourwire_obj_t* fourwire = &displays[i].fourwire_bus;
        if (((uint32_t) fourwire->bus) < ((uint32_t) &displays) ||
            ((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) {
            busio_spi_obj_t* original_spi = fourwire->bus;
            if (original_spi == board_spi()) {
                continue;
            }
            memcpy(&fourwire->inline_bus, original_spi, sizeof(busio_spi_obj_t));
            fourwire->bus = &fourwire->inline_bus;
            // Check for other displays that use the same spi bus and swap them too.
            for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) {
                if (displays[i].fourwire_bus.bus == original_spi) {
                    displays[i].fourwire_bus.bus = &fourwire->inline_bus;
                }
            }
        }
    }

    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        if (displays[i].display.base.type == NULL) {
            continue;
        }
        displayio_display_obj_t* display = &displays[i].display;
        display->auto_brightness = true;
        common_hal_displayio_display_show(display, &circuitpython_splash);
    }
    #endif
}