diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-01-22 14:02:07 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-01-22 14:02:07 -0800 |
| commit | a380865655c1aa17e589e46adb6d5ce926e14a0d (patch) | |
| tree | 9816eb8e2dfd0616aa306050d5cbbb5f99b0c26e /shared-module | |
| parent | 02266eafd87c410593c4f94c820490023e78eb55 (diff) | |
| parent | f46ec0253db76a84c8a8879a60f949cca3c9a443 (diff) | |
Merge remote-tracking branch 'adafruit/master' into HEAD
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/displayio/Display.c | 149 | ||||
| -rw-r--r-- | shared-module/displayio/Display.h | 55 | ||||
| -rw-r--r-- | shared-module/displayio/FourWire.c | 89 | ||||
| -rw-r--r-- | shared-module/displayio/FourWire.h | 43 | ||||
| -rw-r--r-- | shared-module/displayio/Group.c | 3 | ||||
| -rw-r--r-- | shared-module/displayio/Group.h | 1 | ||||
| -rw-r--r-- | shared-module/displayio/OnDiskBitmap.c | 8 | ||||
| -rw-r--r-- | shared-module/displayio/__init__.c | 241 | ||||
| -rw-r--r-- | shared-module/displayio/__init__.h | 17 | ||||
| -rw-r--r-- | shared-module/struct/__init__.c | 113 |
10 files changed, 600 insertions, 119 deletions
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c new file mode 100644 index 000000000..f75f0392f --- /dev/null +++ b/shared-module/displayio/Display.c @@ -0,0 +1,149 @@ +/* + * 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/runtime.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/ParallelBus.h" + +#include <stdint.h> + +#include "tick.h" + +#define DELAY 0x80 + +void common_hal_displayio_display_construct(displayio_display_obj_t* self, + mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, + uint16_t color_depth, uint8_t set_column_command, uint8_t set_row_command, + uint8_t write_ram_command, uint8_t* init_sequence, uint16_t init_sequence_len) { + self->width = width; + self->height = height; + self->color_depth = color_depth; + self->set_column_command = set_column_command; + self->set_row_command = set_row_command; + self->write_ram_command = write_ram_command; + self->current_group = NULL; + self->colstart = colstart; + self->rowstart = rowstart; + + if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) { + self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction; + self->send = common_hal_displayio_parallelbus_send; + self->end_transaction = common_hal_displayio_parallelbus_end_transaction; + } else if (MP_OBJ_IS_TYPE(bus, &displayio_fourwire_type)) { + 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 { + mp_raise_ValueError(translate("Unsupported display bus type")); + } + self->bus = bus; + + uint32_t i = 0; + self->begin_transaction(self->bus); + while (i < init_sequence_len) { + uint8_t *cmd = init_sequence + i; + uint8_t data_size = *(cmd + 1); + bool delay = (data_size & DELAY) != 0; + data_size &= ~DELAY; + uint8_t *data = cmd + 2; + self->send(self->bus, true, cmd, 1); + self->send(self->bus, false, data, data_size); + if (delay) { + data_size++; + uint16_t delay_length_ms = *(cmd + 1 + data_size); + if (delay_length_ms == 255) { + delay_length_ms = 500; + } + uint64_t start = ticks_ms; + while (ticks_ms - start < delay_length_ms) {} + } else { + uint64_t start = ticks_ms; + while (ticks_ms - start < 10) {} + } + i += 2 + data_size; + } + self->end_transaction(self->bus); +} + +void common_hal_displayio_display_show(displayio_display_obj_t* self, displayio_group_t* root_group) { + self->current_group = root_group; + common_hal_displayio_display_refresh_soon(self); +} + +void common_hal_displayio_display_refresh_soon(displayio_display_obj_t* self) { + self->refresh = true; +} + +int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* self) { + uint64_t last_refresh = self->last_refresh; + while (last_refresh == self->last_refresh) { + MICROPY_VM_HOOK_LOOP + } + return 0; +} + +void displayio_display_start_region_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { + // TODO(tannewt): Handle displays with single byte bounds. + self->begin_transaction(self->bus); + uint16_t data[2]; + self->send(self->bus, true, &self->set_column_command, 1); + data[0] = __builtin_bswap16(x0 + self->colstart); + data[1] = __builtin_bswap16(x1 - 1 + self->colstart); + self->send(self->bus, false, (uint8_t*) data, 4); + self->send(self->bus, true, &self->set_row_command, 1); + data[0] = __builtin_bswap16(y0 + 1 + self->rowstart); + data[1] = __builtin_bswap16(y1 + self->rowstart); + self->send(self->bus, false, (uint8_t*) data, 4); + self->send(self->bus, true, &self->write_ram_command, 1); +} + +void displayio_display_finish_region_update(displayio_display_obj_t* self) { + self->end_transaction(self->bus); +} + +bool displayio_display_frame_queued(displayio_display_obj_t* self) { + // Refresh at ~30 fps. + return (ticks_ms - self->last_refresh) > 32; +} + +bool displayio_display_refresh_queued(displayio_display_obj_t* self) { + return self->refresh || (self->current_group != NULL && displayio_group_needs_refresh(self->current_group)); +} + +void displayio_display_finish_refresh(displayio_display_obj_t* self) { + if (self->current_group != NULL) { + displayio_group_finish_refresh(self->current_group); + } + self->refresh = false; + self->last_refresh = ticks_ms; +} + +bool displayio_display_send_pixels(displayio_display_obj_t* self, uint32_t* pixels, uint32_t length) { + self->send(self->bus, false, (uint8_t*) pixels, length * 4); + return true; +} diff --git a/shared-module/displayio/Display.h b/shared-module/displayio/Display.h new file mode 100644 index 000000000..1f1355d31 --- /dev/null +++ b/shared-module/displayio/Display.h @@ -0,0 +1,55 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H + +#include "shared-module/displayio/Group.h" + +typedef bool (*display_bus_begin_transaction)(mp_obj_t bus); +typedef void (*display_bus_send)(mp_obj_t bus, bool command, uint8_t *data, uint32_t data_length); +typedef void (*display_bus_end_transaction)(mp_obj_t bus); + +typedef struct { + mp_obj_base_t base; + mp_obj_t bus; + uint16_t width; + uint16_t height; + uint16_t color_depth; + uint8_t set_column_command; + uint8_t set_row_command; + uint8_t write_ram_command; + displayio_group_t *current_group; + bool refresh; + uint64_t last_refresh; + int16_t colstart; + int16_t rowstart; + display_bus_begin_transaction begin_transaction; + display_bus_send send; + display_bus_end_transaction end_transaction; +} displayio_display_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c new file mode 100644 index 000000000..9da5c0701 --- /dev/null +++ b/shared-module/displayio/FourWire.c @@ -0,0 +1,89 @@ +/* + * 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/FourWire.h" + +#include <stdint.h> + +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +#include "tick.h" + +void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self, + busio_spi_obj_t* spi, const mcu_pin_obj_t* command, + const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset) { + + self->bus = spi; + common_hal_busio_spi_never_reset(self->bus); + + common_hal_digitalio_digitalinout_construct(&self->command, command); + common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL); + common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); + common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); + + if (reset != NULL) { + common_hal_digitalio_digitalinout_construct(&self->reset, reset); + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); + never_reset_pin_number(reset->number); + } + + never_reset_pin_number(command->number); + never_reset_pin_number(chip_select->number); +} + +void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t* self) { + if (self->bus == &self->inline_bus) { + common_hal_busio_spi_deinit(self->bus); + } + + reset_pin_number(self->command.pin->number); + reset_pin_number(self->chip_select.pin->number); + reset_pin_number(self->reset.pin->number); +} + +bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) { + displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj); + if (!common_hal_busio_spi_try_lock(self->bus)) { + return false; + } + // TODO(tannewt): Stop hardcoding SPI frequency, polarity and phase. + common_hal_busio_spi_configure(self->bus, 12000000, 0, 0, 8); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + return true; +} + +void common_hal_displayio_fourwire_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) { + displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj); + common_hal_digitalio_digitalinout_set_value(&self->command, !command); + common_hal_busio_spi_write(self->bus, data, data_length); +} + +void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) { + displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_busio_spi_unlock(self->bus); +} diff --git a/shared-module/displayio/FourWire.h b/shared-module/displayio/FourWire.h new file mode 100644 index 000000000..234bcf794 --- /dev/null +++ b/shared-module/displayio/FourWire.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H + +#include "common-hal/busio/SPI.h" +#include "common-hal/digitalio/DigitalInOut.h" +#include "shared-module/displayio/Group.h" + +typedef struct { + mp_obj_base_t base; + busio_spi_obj_t* bus; + busio_spi_obj_t inline_bus; + digitalio_digitalinout_obj_t command; + digitalio_digitalinout_obj_t chip_select; + digitalio_digitalinout_obj_t reset; +} displayio_fourwire_obj_t; + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c index b9923128a..255349d7c 100644 --- a/shared-module/displayio/Group.c +++ b/shared-module/displayio/Group.c @@ -67,11 +67,14 @@ void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, u self->children = child_array; self->max_size = max_size; self->needs_refresh = false; + self->scale = 1; } bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, uint16_t* pixel) { x -= self->x; y -= self->y; + x /= self->scale; + y /= self->scale; for (int32_t i = self->size - 1; i >= 0 ; i--) { mp_obj_t layer = self->children[i]; if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) { diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h index 272f3114c..60f573ff6 100644 --- a/shared-module/displayio/Group.h +++ b/shared-module/displayio/Group.h @@ -36,6 +36,7 @@ typedef struct { mp_obj_base_t base; int16_t x; int16_t y; + uint16_t scale; uint16_t size; uint16_t max_size; mp_obj_t* children; diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c index 6e4a4c6f0..aa2ca3e10 100644 --- a/shared-module/displayio/OnDiskBitmap.c +++ b/shared-module/displayio/OnDiskBitmap.c @@ -91,3 +91,11 @@ uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *s } return 0; } + +uint16_t common_hal_displayio_ondiskbitmap_get_height(displayio_ondiskbitmap_t *self) { + return self->height; +} + +uint16_t common_hal_displayio_ondiskbitmap_get_width(displayio_ondiskbitmap_t *self) { + return self->width; +} diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index f1a00db9f..59a5cf398 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -1,91 +1,188 @@ -#include "shared-bindings/displayio/FourWire.h" -extern displayio_fourwire_obj_t board_display_obj; +#include <string.h> +#include "shared-module/displayio/__init__.h" -void start_region_update(displayio_fourwire_obj_t* display, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { - // TODO delegate between different display types - displayio_fourwire_start_region_update(display, x0, y0, x1, y1); -} +#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 "shared-bindings/displayio/Sprite.h" +#include "supervisor/usb.h" -void finish_region_update(displayio_fourwire_obj_t* display) { - // TODO delegate between different display types - displayio_fourwire_finish_region_update(display); -} +primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; -void finish_refresh(displayio_fourwire_obj_t* display) { - // TODO delegate between different display types - displayio_fourwire_finish_refresh(display); -} +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; -bool frame_queued(displayio_fourwire_obj_t* display) { - // TODO delegate between different display types - return displayio_fourwire_frame_queued(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; -bool refresh_queued(displayio_fourwire_obj_t* display) { - // TODO delegate between different display types - return displayio_fourwire_refresh_queued(display); -} + if (display->current_group != NULL) { + displayio_group_get_pixel(display->current_group, x, y, pixel); + } -bool send_pixels(displayio_fourwire_obj_t* display, uint32_t* pixels, uint32_t length) { - // TODO delegate between different display types - return displayio_fourwire_send_pixels(display, pixels, length); + 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 displayio_refresh_display(void) { - displayio_fourwire_obj_t* display = &board_display_obj; +uint32_t blinka_bitmap_data[32] = { + 0x00000011, 0x11000000, + 0x00000111, 0x53100000, + 0x00000111, 0x56110000, + 0x00000111, 0x11140000, + 0x00000111, 0x20002000, + 0x00000011, 0x13000000, + 0x00000001, 0x11200000, + 0x00000000, 0x11330000, + 0x00000000, 0x01122000, + 0x00001111, 0x44133000, + 0x00032323, 0x24112200, + 0x00111114, 0x44113300, + 0x00323232, 0x34112200, + 0x11111144, 0x44443300, + 0x11111111, 0x11144401, + 0x23232323, 0x21111110 +}; - if (!frame_queued(display)) { - return; - } - if (refresh_queued(display)) { - PORT->Group[1].DIRSET.reg = 1 << 22; - - // 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]; - 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; - - PORT->Group[1].OUTTGL.reg = 1 << 22; - - //if (index == 0) { - if (display->current_group != NULL) { - displayio_group_get_pixel(display->current_group, x, y, pixel); - } - // } else { - // *pixel = (((uint16_t*)buffer)[0]); - // } +displayio_bitmap_t blinka_bitmap = { + .base = {.type = &displayio_bitmap_type }, + .width = 16, + .height = 16, + .data = blinka_bitmap_data, + .stride = 2, + .bits_per_value = 4, + .x_shift = 3, + .x_mask = 0x7, + .bitmask = 0xf +}; +uint32_t blinka_transparency[1] = {0x80000000}; - PORT->Group[1].OUTTGL.reg = 1 << 22; +// TODO(tannewt): Fix these colors +uint32_t blinka_colors[8] = {0x91780000, 0x879FFC98, 0xffff0000, 0x0000f501, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - index += 1; - // The buffer is full, send it. - if (index >= buffer_size) { - if (!send_pixels(display, buffer, buffer_size / 2)) { - finish_region_update(display); - return; - } - index = 0; +displayio_palette_t blinka_palette = { + .base = {.type = &displayio_palette_type }, + .opaque = blinka_transparency, + .colors = blinka_colors, + .color_count = 16, + .needs_refresh = false +}; + +displayio_sprite_t blinka_sprite = { + .base = {.type = &displayio_sprite_type }, + .bitmap = &blinka_bitmap, + .pixel_shader = &blinka_palette, + .x = 0, + .y = 0, + .width = 16, + .height = 16, + .needs_refresh = false +}; + +mp_obj_t splash_children[1] = { + &blinka_sprite, +}; + +displayio_group_t splash = { + .base = {.type = &displayio_group_type }, + .x = 0, + .y = 0, + .scale = 2, + .size = 1, + .max_size = 1, + .children = splash_children, + .needs_refresh = true +}; + +void reset_displays(void) { + // 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; } } } - // Send the remaining data. - if (index && !send_pixels(display, buffer, index * 2)) { - finish_region_update(display); - return; + } + + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].display.base.type == NULL) { + continue; } - finish_region_update(display); + displayio_display_obj_t* display = &displays[i].display; + common_hal_displayio_display_show(display, &splash); + } +} + +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++) { + displays[i].display.base.type = &mp_type_NoneType; } - finish_refresh(display); } diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index 556af430d..fdabd4ec4 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -27,6 +27,21 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H #define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H -void displayio_refresh_display(void); +#include "shared-bindings/displayio/Display.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/ParallelBus.h" + +typedef struct { + union { + displayio_fourwire_obj_t fourwire_bus; + displayio_parallelbus_obj_t parallel_bus; + }; + displayio_display_obj_t display; +} primary_display_t; + +extern primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; + +void displayio_refresh_displays(void); +void reset_displays(void); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H diff --git a/shared-module/struct/__init__.c b/shared-module/struct/__init__.c index 78c04f07b..245dbbda9 100644 --- a/shared-module/struct/__init__.c +++ b/shared-module/struct/__init__.c @@ -71,45 +71,6 @@ mp_uint_t get_fmt_num(const char **p) { return val; } -void shared_modules_struct_pack_into(mp_obj_t fmt_in, byte *p, byte* end_p, size_t n_args, const mp_obj_t *args) { - const char *fmt = mp_obj_str_get_str(fmt_in); - char fmt_type = get_fmt_type(&fmt); - - size_t i; - for (i = 0; i < n_args;) { - mp_uint_t sz = 1; - if (*fmt == '\0') { - // more arguments given than used by format string; CPython raises struct.error here - mp_raise_RuntimeError(translate("too many arguments provided with the given format")); - } - struct_validate_format(*fmt); - - if (unichar_isdigit(*fmt)) { - sz = get_fmt_num(&fmt); - } - if (p + sz > end_p) { - mp_raise_RuntimeError(translate("buffer too small")); - } - - if (*fmt == 's') { - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[i++], &bufinfo, MP_BUFFER_READ); - mp_uint_t to_copy = sz; - if (bufinfo.len < to_copy) { - to_copy = bufinfo.len; - } - memcpy(p, bufinfo.buf, to_copy); - memset(p + to_copy, 0, sz - to_copy); - p += sz; - } else { - while (sz--) { - mp_binary_set_val(fmt_type, *fmt, args[i++], &p); - } - } - fmt++; - } -} - mp_uint_t calcsize_items(const char *fmt) { mp_uint_t cnt = 0; while (*fmt) { @@ -120,7 +81,10 @@ mp_uint_t calcsize_items(const char *fmt) { num = 1; } } - cnt += num; + // Pad bytes are skipped and don't get included in the item count. + if (*fmt != 'x') { + cnt += num; + } fmt++; } return cnt; @@ -155,14 +119,71 @@ mp_uint_t shared_modules_struct_calcsize(mp_obj_t fmt_in) { return size; } +void shared_modules_struct_pack_into(mp_obj_t fmt_in, byte *p, byte* end_p, size_t n_args, const mp_obj_t *args) { + const char *fmt = mp_obj_str_get_str(fmt_in); + char fmt_type = get_fmt_type(&fmt); + const mp_uint_t total_sz = shared_modules_struct_calcsize(fmt_in); + + if (p + total_sz > end_p) { + mp_raise_RuntimeError(translate("buffer too small")); + } + + size_t i; + for (i = 0; i < n_args;) { + mp_uint_t sz = 1; + if (*fmt == '\0') { + // more arguments given than used by format string; CPython raises struct.error here + mp_raise_RuntimeError(translate("too many arguments provided with the given format")); + } + struct_validate_format(*fmt); + + if (unichar_isdigit(*fmt)) { + sz = get_fmt_num(&fmt); + } + + if (*fmt == 's') { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[i++], &bufinfo, MP_BUFFER_READ); + mp_uint_t to_copy = sz; + if (bufinfo.len < to_copy) { + to_copy = bufinfo.len; + } + memcpy(p, bufinfo.buf, to_copy); + memset(p + to_copy, 0, sz - to_copy); + p += sz; + } else { + while (sz--) { + mp_binary_set_val(fmt_type, *fmt, args[i], &p); + // Pad bytes don't have a corresponding argument. + if (*fmt != 'x') { + i++; + } + } + } + fmt++; + } +} -mp_obj_tuple_t * shared_modules_struct_unpack_from(mp_obj_t fmt_in, byte *p, byte *end_p) { +mp_obj_tuple_t * shared_modules_struct_unpack_from(mp_obj_t fmt_in, byte *p, byte *end_p, bool exact_size) { const char *fmt = mp_obj_str_get_str(fmt_in); char fmt_type = get_fmt_type(&fmt); - mp_uint_t num_items = calcsize_items(fmt); + const mp_uint_t num_items = calcsize_items(fmt); + const mp_uint_t total_sz = shared_modules_struct_calcsize(fmt_in); mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_items, NULL)); + // If exact_size, make sure the buffer is exactly the right size. + // Otherwise just make sure it's big enough. + if (exact_size) { + if (p + total_sz != end_p) { + mp_raise_RuntimeError(translate("buffer size must match format")); + } + } else { + if (p + total_sz > end_p) { + mp_raise_RuntimeError(translate("buffer too small")); + } + } + for (uint i = 0; i < num_items;) { mp_uint_t sz = 1; @@ -171,9 +192,6 @@ mp_obj_tuple_t * shared_modules_struct_unpack_from(mp_obj_t fmt_in, byte *p, byt if (unichar_isdigit(*fmt)) { sz = get_fmt_num(&fmt); } - if (p + sz > end_p) { - mp_raise_RuntimeError(translate("buffer too small")); - } mp_obj_t item; if (*fmt == 's') { item = mp_obj_new_bytes(p, sz); @@ -182,7 +200,10 @@ mp_obj_tuple_t * shared_modules_struct_unpack_from(mp_obj_t fmt_in, byte *p, byt } else { while (sz--) { item = mp_binary_get_val(fmt_type, *fmt, &p); - res->items[i++] = item; + // Pad bytes are not stored. + if (*fmt != 'x') { + res->items[i++] = item; + } } } fmt++; |
