diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-08-15 11:01:01 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2018-08-31 12:31:52 -0700 |
| commit | 6697544cdf87d0df5b761d401ad6ffe3a8d752fc (patch) | |
| tree | 1526897a43894d045d9ba88c8cd6d6c314ce191f /shared-module/displayio | |
| parent | 9b98ad779468676c3d5f1efdc06b454aaed7c407 (diff) | |
Introduce displayio to render graphics to displays.
It's designed to minimize RAM footprint by using Sprites to
represent objects on the screen. The object model also facilitates
partial screen updating which reduces the bandwidth needed to display.
This is all handled in C. Python simply manipulates the objects with
the ability to synchronize to frame timing.
Diffstat (limited to 'shared-module/displayio')
| -rw-r--r-- | shared-module/displayio/Bitmap.c | 89 | ||||
| -rw-r--r-- | shared-module/displayio/Bitmap.h | 47 | ||||
| -rw-r--r-- | shared-module/displayio/Group.c | 88 | ||||
| -rw-r--r-- | shared-module/displayio/Group.h | 53 | ||||
| -rw-r--r-- | shared-module/displayio/Palette.c | 78 | ||||
| -rw-r--r-- | shared-module/displayio/Palette.h | 47 | ||||
| -rw-r--r-- | shared-module/displayio/Sprite.c | 90 | ||||
| -rw-r--r-- | shared-module/displayio/Sprite.h | 51 | ||||
| -rw-r--r-- | shared-module/displayio/__init__.c | 80 | ||||
| -rw-r--r-- | shared-module/displayio/__init__.h | 32 |
10 files changed, 655 insertions, 0 deletions
diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c new file mode 100644 index 000000000..7c6a2ee80 --- /dev/null +++ b/shared-module/displayio/Bitmap.c @@ -0,0 +1,89 @@ +/* + * This file is part of the Micro Python 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/Bitmap.h" + +#include <string.h> + +#include "py/runtime.h" + +void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width, + uint32_t height, uint32_t value_size) { + uint32_t row_width = width * value_size; + // word align + if (row_width % 32 != 0) { + self->stride = (row_width / 32 + 1); + } else { + self->stride = row_width / 32; + } + self->width = width; + self->height = height; + self->data = m_malloc(self->stride * height * sizeof(uint32_t), false); + + self->bits_per_value = value_size; + + self->x_shift = 0; + uint32_t power_of_two = 1; + while (power_of_two < 32 / value_size ) { + self->x_shift++; + power_of_two <<= 1; + } + self->x_mask = (1 << self->x_shift) - 1; // Used as a modulus on the x value + self->bitmask = (1 << value_size) - 1; +} + +void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len) { + if (len != self->stride * 4) { + mp_raise_ValueError(translate("row must be packed and word aligned")); + } + uint32_t* row_value = self->data + (y * self->stride); + // Do the memcpy ourselves since we may want to flip endianness. + for (uint32_t i = 0; i < self->stride; i++) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" + uint32_t value = ((uint32_t *)data)[i]; + #pragma GCC diagnostic pop + if (self->bits_per_value < 16) { + value = ((value >> 24) & 0xff) | + ((value << 8) & 0xff0000) | + ((value >> 8) & 0xff00) | + ((value << 24) & 0xff000000); + } + *row_value = value; + row_value++; + } +} +uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t x, int16_t y) { + int32_t row_start = y * self->stride; + if (self->bits_per_value < 8) { + uint32_t word = self->data[row_start + (x >> self->x_shift)]; + + return (word >> (32 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask; + } else { + uint32_t bytes_per_value = self->bits_per_value / 8; + return self->data[row_start + x * bytes_per_value]; + } +} diff --git a/shared-module/displayio/Bitmap.h b/shared-module/displayio/Bitmap.h new file mode 100644 index 000000000..781a4d12a --- /dev/null +++ b/shared-module/displayio/Bitmap.h @@ -0,0 +1,47 @@ +/* + * This file is part of the Micro Python 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_SHARED_MODULE_DISPLAYIO_BITMAP_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t* data; + uint16_t stride; // words + uint8_t bits_per_value; + uint8_t x_shift; + uint8_t x_mask; + uint16_t bitmask; +} displayio_bitmap_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c new file mode 100644 index 000000000..885131d47 --- /dev/null +++ b/shared-module/displayio/Group.c @@ -0,0 +1,88 @@ +/* + * This file is part of the Micro Python 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/Group.h" + +#include "py/runtime.h" +#include "shared-bindings/displayio/Sprite.h" + +void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size) { + mp_obj_t* children = m_new(mp_obj_t, max_size); + displayio_group_construct(self, children, max_size); +} + +void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer) { + if (self->size == self->max_size) { + mp_raise_RuntimeError(translate("Group full")); + } + self->children[self->size] = layer; + self->size++; +} + +void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, uint32_t max_size) { + self->x = 0; + self->y = 1; + self->children = child_array; + self->max_size = max_size; +} + +bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, uint16_t* pixel) { + x -= self->x; + y -= self->y; + 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)) { + if (displayio_sprite_get_pixel(layer, x, y, pixel)) { + return true; + } + } + // TODO: Tiled layer + } + return false; +} + +bool displayio_group_needs_refresh(displayio_group_t *self) { + 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)) { + if (displayio_sprite_needs_refresh(layer)) { + return true; + } + } + // TODO: Tiled layer + } + return false; +} + +void displayio_group_finish_refresh(displayio_group_t *self) { + 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)) { + displayio_sprite_finish_refresh(layer); + } + // TODO: Tiled layer + } +} diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h new file mode 100644 index 000000000..7f3e5646a --- /dev/null +++ b/shared-module/displayio/Group.h @@ -0,0 +1,53 @@ +/* + * This file is part of the Micro Python 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_SHARED_MODULE_DISPLAYIO_GROUP_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_GROUP_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + int16_t x; + int16_t y; + uint16_t size; + uint16_t max_size; + mp_obj_t* children; +} displayio_group_t; + + +void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size); +void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer); + +void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, uint32_t max_size); +bool displayio_group_get_pixel(displayio_group_t *group, int16_t x, int16_t y, uint16_t *pixel); +bool displayio_group_needs_refresh(displayio_group_t *self); +void displayio_group_finish_refresh(displayio_group_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_GROUP_H diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c new file mode 100644 index 000000000..e2ec6e057 --- /dev/null +++ b/shared-module/displayio/Palette.c @@ -0,0 +1,78 @@ +/* + * This file is part of the Micro Python 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/Palette.h" + +void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t color_count) { + self->max_value = color_count; + self->colors = (uint32_t *) m_malloc(color_count * sizeof(uint16_t), false); + uint32_t opaque_byte_count = color_count / 8; + if (color_count % 8 > 0) { + opaque_byte_count += 1; + } + self->opaque = (uint32_t *) m_malloc(opaque_byte_count, false); +} + +void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t value) { + self->opaque[value / 32] &= ~(0x1 << (value % 32)); +} + +void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t value) { + self->opaque[value / 32] |= (0x1 << (value % 32)); +} + +void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t value, uint32_t color) { + uint32_t shift = (value % 2) * 16; + uint32_t masked = self->colors[value / 2] & ~(0xffff << shift); + uint32_t b5 = (color >> 19); + uint32_t g6 = (color >> 10) & 0x3f; + uint32_t r5 = (color >> 3) & 0x1f; + uint32_t packed = r5 << 11 | g6 << 5 | b5; + // swap bytes + packed = ((packed >> 8) & 0xff) | ((packed & 0xff) << 8); + self->colors[value / 2] = masked | packed << shift; + self->needs_refresh = true; +} + +bool displayio_palette_get_color(displayio_palette_t *self, uint32_t value, uint16_t* color) { + if (value > self->max_value) { + return false; + } + if ((self->opaque[value / 32] & (0x1 << (value % 32))) != 0) { + return false; + } + *color = (self->colors[value / 2] >> (16 * (value % 2))) & 0xffff; + + return true; +} + +bool displayio_palette_needs_refresh(displayio_palette_t *self) { + return self->needs_refresh; +} + +void displayio_palette_finish_refresh(displayio_palette_t *self) { + self->needs_refresh = false; +} diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h new file mode 100644 index 000000000..22c737c9d --- /dev/null +++ b/shared-module/displayio/Palette.h @@ -0,0 +1,47 @@ +/* + * This file is part of the Micro Python 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_SHARED_MODULE_DISPLAYIO_PALETTE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_PALETTE_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint32_t* opaque; + uint32_t* colors; + uint8_t max_value; + bool needs_refresh; +} displayio_palette_t; + +bool displayio_palette_get_color(displayio_palette_t *palette, uint32_t value, uint16_t* color); +bool displayio_palette_needs_refresh(displayio_palette_t *self); +void displayio_palette_finish_refresh(displayio_palette_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_PALLETE_H diff --git a/shared-module/displayio/Sprite.c b/shared-module/displayio/Sprite.c new file mode 100644 index 000000000..b584c0569 --- /dev/null +++ b/shared-module/displayio/Sprite.c @@ -0,0 +1,90 @@ +/* + * This file is part of the Micro Python 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/Sprite.h" + + +#include "shared-bindings/displayio/Bitmap.h" + +void common_hal_displayio_sprite_construct(displayio_sprite_t *self, mp_obj_t bitmap, + mp_obj_t palette, uint16_t width, uint16_t height, uint16_t x, uint16_t y) { + self->width = width; + self->height = height; + self->bitmap = bitmap; + self->palette = palette; + self->x = x; + self->y = y; +} + +void common_hal_displayio_sprite_get_position(displayio_sprite_t *self, int16_t* x, int16_t* y) { + *x = self->x; + *y = self->y; +} + +void common_hal_displayio_sprite_set_position(displayio_sprite_t *self, int16_t x, int16_t y) { + self->x = x; + self->y = y; + self->needs_refresh = true; +} + + +displayio_palette_t* common_hal_displayio_sprite_get_palette(displayio_sprite_t *self) { + return self->palette; +} + +void common_hal_displayio_sprite_set_palette(displayio_sprite_t *self, displayio_palette_t* palette) { + self->palette = palette; + self->needs_refresh = true; +} + +bool displayio_sprite_get_pixel(displayio_sprite_t *self, int16_t x, int16_t y, uint16_t* pixel) { + x -= self->x; + y -= self->y; + if (y < 0 || y >= self->height || x >= self->width || x < 0) { + return false; + } + + uint32_t value = common_hal_displayio_bitmap_get_pixel(self->bitmap, x, y); + if (self->palette == mp_const_none) { + *pixel = value; + return true; + } else if (MP_OBJ_IS_TYPE(self->palette, &displayio_palette_type) && displayio_palette_get_color(self->palette, value, pixel)) { + return true; + } + + return false; +} + +bool displayio_sprite_needs_refresh(displayio_sprite_t *self) { + return self->needs_refresh || displayio_palette_needs_refresh(self->palette); +} + +void displayio_sprite_finish_refresh(displayio_sprite_t *self) { + self->needs_refresh = false; + displayio_palette_finish_refresh(self->palette); + // TODO(tannewt): We could double buffer changes to position and move them over here. + // That way they won't change during a refresh and tear. +} diff --git a/shared-module/displayio/Sprite.h b/shared-module/displayio/Sprite.h new file mode 100644 index 000000000..213130af8 --- /dev/null +++ b/shared-module/displayio/Sprite.h @@ -0,0 +1,51 @@ +/* + * This file is part of the Micro Python 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_SHARED_MODULE_DISPLAYIO_SPRITE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SPRITE_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" +#include "shared-bindings/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t bitmap; + displayio_palette_t* palette; + uint16_t x; + uint16_t y; + uint16_t width; + uint16_t height; + bool needs_refresh; +} displayio_sprite_t; + +bool displayio_sprite_get_pixel(displayio_sprite_t *sprite, int16_t x, int16_t y, uint16_t *pixel); +bool displayio_sprite_needs_refresh(displayio_sprite_t *self); +void displayio_sprite_finish_refresh(displayio_sprite_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SPRITE_H diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c new file mode 100644 index 000000000..df9c2963a --- /dev/null +++ b/shared-module/displayio/__init__.c @@ -0,0 +1,80 @@ +#include "shared-bindings/displayio/FourWire.h" + +extern displayio_fourwire_obj_t board_display_obj; + +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); +} + +void finish_region_update(displayio_fourwire_obj_t* display) { + // TODO delegate between different display types + displayio_fourwire_finish_region_update(display); +} + +void finish_refresh(displayio_fourwire_obj_t* display) { + // TODO delegate between different display types + displayio_fourwire_finish_refresh(display); +} + +bool frame_queued(displayio_fourwire_obj_t* display) { + // TODO delegate between different display types + return displayio_fourwire_frame_queued(display); +} + +bool refresh_queued(displayio_fourwire_obj_t* display) { + // TODO delegate between different display types + return displayio_fourwire_refresh_queued(display); +} + +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); +} + +void displayio_refresh_display(void) { + displayio_fourwire_obj_t* display = &board_display_obj; + + if (!frame_queued(display)) { + return; + } + if (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]; + 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 (!send_pixels(display, buffer, buffer_size / 2)) { + finish_region_update(display); + return; + } + index = 0; + } + } + } + // Send the remaining data. + if (index && !send_pixels(display, buffer, index * 2)) { + finish_region_update(display); + return; + } + finish_region_update(display); + } + finish_refresh(display); +} diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h new file mode 100644 index 000000000..556af430d --- /dev/null +++ b/shared-module/displayio/__init__.h @@ -0,0 +1,32 @@ +/* + * This file is part of the Micro Python 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_SHARED_BINDINGS_DISPLAYIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H + +void displayio_refresh_display(void); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H |
