From 6697544cdf87d0df5b761d401ad6ffe3a8d752fc Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 15 Aug 2018 11:01:01 -0700 Subject: 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. --- shared-module/displayio/Bitmap.c | 89 +++++++++++++++++++++++++++++++++++++ shared-module/displayio/Bitmap.h | 47 ++++++++++++++++++++ shared-module/displayio/Group.c | 88 +++++++++++++++++++++++++++++++++++++ shared-module/displayio/Group.h | 53 ++++++++++++++++++++++ shared-module/displayio/Palette.c | 78 +++++++++++++++++++++++++++++++++ shared-module/displayio/Palette.h | 47 ++++++++++++++++++++ shared-module/displayio/Sprite.c | 90 ++++++++++++++++++++++++++++++++++++++ shared-module/displayio/Sprite.h | 51 +++++++++++++++++++++ shared-module/displayio/__init__.c | 80 +++++++++++++++++++++++++++++++++ shared-module/displayio/__init__.h | 32 ++++++++++++++ 10 files changed, 655 insertions(+) create mode 100644 shared-module/displayio/Bitmap.c create mode 100644 shared-module/displayio/Bitmap.h create mode 100644 shared-module/displayio/Group.c create mode 100644 shared-module/displayio/Group.h create mode 100644 shared-module/displayio/Palette.c create mode 100644 shared-module/displayio/Palette.h create mode 100644 shared-module/displayio/Sprite.c create mode 100644 shared-module/displayio/Sprite.h create mode 100644 shared-module/displayio/__init__.c create mode 100644 shared-module/displayio/__init__.h (limited to 'shared-module') 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 + +#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 +#include + +#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 +#include + +#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 +#include + +#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 +#include + +#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 -- cgit v1.2.3 From 121903b6eeb9712cae86a24c5fe82cb7b144e504 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 31 Aug 2018 14:21:48 -0700 Subject: Tweaks based on feedback --- .../atmel-samd/boards/hallowing_m0_express/board.c | 2 +- ports/atmel-samd/common-hal/displayio/FourWire.c | 8 +-- ports/atmel-samd/mpconfigport.h | 2 - shared-bindings/displayio/Bitmap.c | 17 +++--- shared-bindings/displayio/Group.c | 2 +- shared-bindings/displayio/Palette.c | 69 +++++++++++----------- shared-bindings/displayio/__init__.h | 4 -- shared-module/displayio/Bitmap.c | 5 +- 8 files changed, 50 insertions(+), 59 deletions(-) (limited to 'shared-module') diff --git a/ports/atmel-samd/boards/hallowing_m0_express/board.c b/ports/atmel-samd/boards/hallowing_m0_express/board.c index 69d7ecd37..85bcaabc7 100644 --- a/ports/atmel-samd/boards/hallowing_m0_express/board.c +++ b/ports/atmel-samd/boards/hallowing_m0_express/board.c @@ -84,7 +84,7 @@ void board_init(void) { } else if (*cmd == 0x11) { uint64_t start = ticks_ms; while (ticks_ms - start < 500) {} - } { + } else { uint64_t start = ticks_ms; while (ticks_ms - start < 10) {} } diff --git a/ports/atmel-samd/common-hal/displayio/FourWire.c b/ports/atmel-samd/common-hal/displayio/FourWire.c index 42038a58e..033970c77 100644 --- a/ports/atmel-samd/common-hal/displayio/FourWire.c +++ b/ports/atmel-samd/common-hal/displayio/FourWire.c @@ -63,22 +63,17 @@ bool common_hal_displayio_fourwire_begin_transaction(displayio_fourwire_obj_t* s 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_wait_for_send(displayio_fourwire_obj_t* self) { -} - void common_hal_displayio_fourwire_send(displayio_fourwire_obj_t* self, bool command, uint8_t *data, uint32_t data_length) { - common_hal_displayio_fourwire_wait_for_send(self); 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(displayio_fourwire_obj_t* self) { common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); common_hal_busio_spi_unlock(&self->bus); @@ -106,6 +101,7 @@ static uint16_t swap(uint16_t x) { } void displayio_fourwire_start_region_update(displayio_fourwire_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { + // TODO(tannewt): Handle displays with single byte bounds. common_hal_displayio_fourwire_begin_transaction(self); uint16_t data[2]; common_hal_displayio_fourwire_send(self, true, &self->set_column_command, 1); diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 58f74d7ec..14f72ccfb 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -239,8 +239,6 @@ extern const struct _mp_obj_module_t usb_hid_module; #define DISPLAYIO_MODULE #endif - - #ifndef EXTRA_BUILTIN_MODULES #define EXTRA_BUILTIN_MODULES \ AUDIOIO_MODULE \ diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index b01fe0e0c..d415127d2 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -45,30 +45,29 @@ //| //| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental. //| -//| .. class:: Bitmap(width, height, value_size) +//| .. class:: Bitmap(width, height, value_count) //| -//| Create a Bitmap object with the given fixed size. +//| Create a Bitmap object with the given fixed size. Each pixel stores a value that is used to +//| index into a corresponding palette. This enables differently colored sprites to share the +//| underlying Bitmap. value_count is used to minimize the memory used to store the Bitmap. //| //| :param int width: The number of values wide //| :param int height: The number of values high -//| :param int value_size: The value size in bits. Must be power of 2. +//| :param int value_count: The number of possible pixel values. //| STATIC mp_obj_t displayio_bitmap_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 3, 3, false); uint32_t width = mp_obj_get_int(pos_args[0]); uint32_t height = mp_obj_get_int(pos_args[1]); - uint32_t value_size = mp_obj_get_int(pos_args[2]); + uint32_t value_count = mp_obj_get_int(pos_args[2]); uint32_t power_of_two = 1; - while (value_size > power_of_two) { + while (value_count > (1U << power_of_two)) { power_of_two <<= 1; } - if (value_size != power_of_two) { - mp_raise_ValueError(translate("value_size must be power of two")); - } displayio_bitmap_t *self = m_new_obj(displayio_bitmap_t); self->base.type = &displayio_bitmap_type; - common_hal_displayio_bitmap_construct(self, width, height, value_size); + common_hal_displayio_bitmap_construct(self, width, height, power_of_two); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index a2a879987..e6f1fbbf5 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -74,7 +74,7 @@ STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg //| .. method:: append(layer) //| -//| Switches do displaying the given group of elements. +//| Append a layer to the group. It will be drawn above other layers. //| STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) { displayio_group_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index bb2af9410..99963f131 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -41,8 +41,8 @@ //| :class:`Palette` -- Stores a mapping from bitmap pixel values to display colors //| =============================================================================== //| -//| Manage updating a display over SPI four wire protocol in the background while Python code runs. -//| It doesn't handle display initialization. +//| Map a pixel value to a full color. Colors are transformed to the display's format internally to +//| save memory. //| //| .. warning:: This will be changed before 4.0.0. Consider it very experimental. //| @@ -76,42 +76,41 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val if (value == MP_OBJ_NULL) { // delete item return MP_OBJ_NULL; // op not supported - } else { - displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); - if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) { - return MP_OBJ_NULL; // Slicing not supported. Use a duplicate Palette to swap multiple colors atomically. - } else { - if (value == MP_OBJ_SENTINEL) { - return MP_OBJ_NULL; // index read is not supported - } else { - size_t index = mp_get_index(&displayio_palette_type, self->max_value, index_in, false); + } + // Slicing not supported. Use a duplicate Palette to swap multiple colors atomically. + if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) { + return MP_OBJ_NULL; + } + // index read is not supported + if (value == MP_OBJ_SENTINEL) { + return MP_OBJ_NULL; + } + displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); + size_t index = mp_get_index(&displayio_palette_type, self->max_value, index_in, false); - uint32_t color; - mp_int_t int_value; - mp_buffer_info_t bufinfo; - if (mp_get_buffer(value, &bufinfo, MP_BUFFER_READ)) { - if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { - mp_raise_ValueError(translate("color buffer must be a bytearray or array of type 'b' or 'B'")); - } - uint8_t* buf = bufinfo.buf; - if (bufinfo.len == 3 || bufinfo.len == 4) { - color = buf[0] << 16 | buf[1] << 8 | buf[2]; - } else { - mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)")); - } - } else if (mp_obj_get_int_maybe(value, &int_value)) { - if (int_value < 0 || int_value > 0xffffff) { - mp_raise_TypeError(translate("color must be between 0x000000 and 0xffffff")); - } - color = int_value; - } else { - mp_raise_TypeError(translate("color buffer must be a buffer or int")); - } - common_hal_displayio_palette_set_color(self, index, color); - return mp_const_none; - } + uint32_t color; + mp_int_t int_value; + mp_buffer_info_t bufinfo; + if (mp_get_buffer(value, &bufinfo, MP_BUFFER_READ)) { + if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) { + mp_raise_ValueError(translate("color buffer must be a bytearray or array of type 'b' or 'B'")); } + uint8_t* buf = bufinfo.buf; + if (bufinfo.len == 3 || bufinfo.len == 4) { + color = buf[0] << 16 | buf[1] << 8 | buf[2]; + } else { + mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)")); + } + } else if (mp_obj_get_int_maybe(value, &int_value)) { + if (int_value < 0 || int_value > 0xffffff) { + mp_raise_TypeError(translate("color must be between 0x000000 and 0xffffff")); + } + color = int_value; + } else { + mp_raise_TypeError(translate("color buffer must be a buffer or int")); } + common_hal_displayio_palette_set_color(self, index, color); + return mp_const_none; } //| .. method:: make_transparent(value) diff --git a/shared-bindings/displayio/__init__.h b/shared-bindings/displayio/__init__.h index 5c3fc2b55..a6663bf57 100644 --- a/shared-bindings/displayio/__init__.h +++ b/shared-bindings/displayio/__init__.h @@ -31,8 +31,4 @@ // Nothing now. -// typedef enum { -// PIXEL_ -// } displayio_pixel_format; - #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 7c6a2ee80..453dc1c59 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -56,7 +56,7 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi } 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) { + if (len != self->stride * sizeof(uint32_t)) { mp_raise_ValueError(translate("row must be packed and word aligned")); } uint32_t* row_value = self->data + (y * self->stride); @@ -77,6 +77,9 @@ void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, } } uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t x, int16_t y) { + if (x >= self->width || x < 0 || y >= self->height || y < 0) { + return 0; + } int32_t row_start = y * self->stride; if (self->bits_per_value < 8) { uint32_t word = self->data[row_start + (x >> self->x_shift)]; -- cgit v1.2.3 From 1683eb913d5bfb4265df56bf286d9971185a3a82 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 6 Sep 2018 14:49:49 -0700 Subject: Minor tweaks based on feedback --- locale/circuitpython.pot | 40 +++++------ locale/en_US.po | 40 +++++------ locale/es.po | 40 +++++------ locale/fil.po | 82 +++++++++++++++++++++- locale/fr.po | 45 ++++++------ .../atmel-samd/boards/hallowing_m0_express/board.c | 41 +++++++---- .../atmel-samd/boards/hallowing_m0_express/pins.c | 2 +- ports/atmel-samd/common-hal/displayio/FourWire.c | 12 ++-- shared-bindings/displayio/Bitmap.h | 2 +- shared-bindings/displayio/Palette.c | 32 ++++----- shared-bindings/displayio/Palette.h | 6 +- shared-bindings/displayio/Sprite.c | 7 +- shared-module/displayio/Bitmap.c | 21 ++++-- shared-module/displayio/Palette.c | 28 ++++---- shared-module/displayio/Palette.h | 4 +- shared-module/displayio/mipi_constants.h | 37 ++++++++++ 16 files changed, 291 insertions(+), 148 deletions(-) create mode 100644 shared-module/displayio/mipi_constants.h (limited to 'shared-module') diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index c7b7085b7..1d94dccae 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-08-30 23:06-0700\n" +"POT-Creation-Date: 2018-09-06 14:49-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -2022,19 +2022,15 @@ msgstr "" msgid "Unsupported pull value." msgstr "" -#: shared-bindings/displayio/Bitmap.c:66 -msgid "value_size must be power of two" -msgstr "" - -#: shared-bindings/displayio/Bitmap.c:85 +#: shared-bindings/displayio/Bitmap.c:84 msgid "y should be an int" msgstr "" -#: shared-bindings/displayio/Bitmap.c:90 +#: shared-bindings/displayio/Bitmap.c:89 msgid "row buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Bitmap.c:95 +#: shared-bindings/displayio/Bitmap.c:94 msgid "row data must be a buffer" msgstr "" @@ -2047,36 +2043,36 @@ msgstr "" msgid "Group must have size at least 1" msgstr "" -#: shared-bindings/displayio/Palette.c:94 +#: shared-bindings/displayio/Palette.c:96 msgid "color buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Palette.c:100 -msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)" +#: shared-bindings/displayio/Palette.c:102 +msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)" msgstr "" -#: shared-bindings/displayio/Palette.c:104 +#: shared-bindings/displayio/Palette.c:106 msgid "color must be between 0x000000 and 0xffffff" msgstr "" -#: shared-bindings/displayio/Palette.c:108 +#: shared-bindings/displayio/Palette.c:110 msgid "color buffer must be a buffer or int" msgstr "" -#: shared-bindings/displayio/Palette.c:124 -#: shared-bindings/displayio/Palette.c:138 -msgid "value should be an int" +#: shared-bindings/displayio/Palette.c:123 +#: shared-bindings/displayio/Palette.c:137 +msgid "palette_index should be an int" msgstr "" -#: shared-bindings/displayio/Sprite.c:44 +#: shared-bindings/displayio/Sprite.c:45 msgid "position must be 2-tuple" msgstr "" -#: shared-bindings/displayio/Sprite.c:86 +#: shared-bindings/displayio/Sprite.c:87 msgid "unsupported bitmap type" msgstr "" -#: shared-bindings/displayio/Sprite.c:151 +#: shared-bindings/displayio/Sprite.c:152 msgid "palette must be displayio.Palette" msgstr "" @@ -2294,7 +2290,11 @@ msgstr "" msgid "Cannot transfer without MOSI and MISO pins." msgstr "" -#: shared-module/displayio/Bitmap.c:60 +#: shared-module/displayio/Bitmap.c:49 +msgid "Only bit maps of 8 bit color or less are supported" +msgstr "" + +#: shared-module/displayio/Bitmap.c:69 msgid "row must be packed and word aligned" msgstr "" diff --git a/locale/en_US.po b/locale/en_US.po index 78e32cc82..c310ea920 100644 --- a/locale/en_US.po +++ b/locale/en_US.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-08-30 16:12-0700\n" +"POT-Creation-Date: 2018-09-06 14:49-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: \n" @@ -2022,19 +2022,15 @@ msgstr "" msgid "Unsupported pull value." msgstr "" -#: shared-bindings/displayio/Bitmap.c:66 -msgid "value_size must be power of two" -msgstr "" - -#: shared-bindings/displayio/Bitmap.c:85 +#: shared-bindings/displayio/Bitmap.c:84 msgid "y should be an int" msgstr "" -#: shared-bindings/displayio/Bitmap.c:90 +#: shared-bindings/displayio/Bitmap.c:89 msgid "row buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Bitmap.c:95 +#: shared-bindings/displayio/Bitmap.c:94 msgid "row data must be a buffer" msgstr "" @@ -2047,36 +2043,36 @@ msgstr "" msgid "Group must have size at least 1" msgstr "" -#: shared-bindings/displayio/Palette.c:94 +#: shared-bindings/displayio/Palette.c:96 msgid "color buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Palette.c:100 -msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)" +#: shared-bindings/displayio/Palette.c:102 +msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)" msgstr "" -#: shared-bindings/displayio/Palette.c:104 +#: shared-bindings/displayio/Palette.c:106 msgid "color must be between 0x000000 and 0xffffff" msgstr "" -#: shared-bindings/displayio/Palette.c:108 +#: shared-bindings/displayio/Palette.c:110 msgid "color buffer must be a buffer or int" msgstr "" -#: shared-bindings/displayio/Palette.c:124 -#: shared-bindings/displayio/Palette.c:138 -msgid "value should be an int" +#: shared-bindings/displayio/Palette.c:123 +#: shared-bindings/displayio/Palette.c:137 +msgid "palette_index should be an int" msgstr "" -#: shared-bindings/displayio/Sprite.c:44 +#: shared-bindings/displayio/Sprite.c:45 msgid "position must be 2-tuple" msgstr "" -#: shared-bindings/displayio/Sprite.c:86 +#: shared-bindings/displayio/Sprite.c:87 msgid "unsupported bitmap type" msgstr "" -#: shared-bindings/displayio/Sprite.c:151 +#: shared-bindings/displayio/Sprite.c:152 msgid "palette must be displayio.Palette" msgstr "" @@ -2294,7 +2290,11 @@ msgstr "" msgid "Cannot transfer without MOSI and MISO pins." msgstr "" -#: shared-module/displayio/Bitmap.c:60 +#: shared-module/displayio/Bitmap.c:49 +msgid "Only bit maps of 8 bit color or less are supported" +msgstr "" + +#: shared-module/displayio/Bitmap.c:69 msgid "row must be packed and word aligned" msgstr "" diff --git a/locale/es.po b/locale/es.po index c9de75c38..f7e0ed4e4 100644 --- a/locale/es.po +++ b/locale/es.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-08-30 16:12-0700\n" +"POT-Creation-Date: 2018-09-06 14:49-0700\n" "PO-Revision-Date: 2018-08-24 22:56-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -2068,19 +2068,15 @@ msgstr "" msgid "Unsupported pull value." msgstr "" -#: shared-bindings/displayio/Bitmap.c:66 -msgid "value_size must be power of two" -msgstr "" - -#: shared-bindings/displayio/Bitmap.c:85 +#: shared-bindings/displayio/Bitmap.c:84 msgid "y should be an int" msgstr "" -#: shared-bindings/displayio/Bitmap.c:90 +#: shared-bindings/displayio/Bitmap.c:89 msgid "row buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Bitmap.c:95 +#: shared-bindings/displayio/Bitmap.c:94 msgid "row data must be a buffer" msgstr "" @@ -2093,36 +2089,36 @@ msgstr "" msgid "Group must have size at least 1" msgstr "" -#: shared-bindings/displayio/Palette.c:94 +#: shared-bindings/displayio/Palette.c:96 msgid "color buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" -#: shared-bindings/displayio/Palette.c:100 -msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)" +#: shared-bindings/displayio/Palette.c:102 +msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)" msgstr "" -#: shared-bindings/displayio/Palette.c:104 +#: shared-bindings/displayio/Palette.c:106 msgid "color must be between 0x000000 and 0xffffff" msgstr "" -#: shared-bindings/displayio/Palette.c:108 +#: shared-bindings/displayio/Palette.c:110 msgid "color buffer must be a buffer or int" msgstr "" -#: shared-bindings/displayio/Palette.c:124 -#: shared-bindings/displayio/Palette.c:138 -msgid "value should be an int" +#: shared-bindings/displayio/Palette.c:123 +#: shared-bindings/displayio/Palette.c:137 +msgid "palette_index should be an int" msgstr "" -#: shared-bindings/displayio/Sprite.c:44 +#: shared-bindings/displayio/Sprite.c:45 msgid "position must be 2-tuple" msgstr "" -#: shared-bindings/displayio/Sprite.c:86 +#: shared-bindings/displayio/Sprite.c:87 msgid "unsupported bitmap type" msgstr "" -#: shared-bindings/displayio/Sprite.c:151 +#: shared-bindings/displayio/Sprite.c:152 msgid "palette must be displayio.Palette" msgstr "" @@ -2341,7 +2337,11 @@ msgstr "" msgid "Cannot transfer without MOSI and MISO pins." msgstr "" -#: shared-module/displayio/Bitmap.c:60 +#: shared-module/displayio/Bitmap.c:49 +msgid "Only bit maps of 8 bit color or less are supported" +msgstr "" + +#: shared-module/displayio/Bitmap.c:69 msgid "row must be packed and word aligned" msgstr "" diff --git a/locale/fil.po b/locale/fil.po index 7e8b9c7a3..8f681bcb4 100644 --- a/locale/fil.po +++ b/locale/fil.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-08-30 18:29-0700\n" +"POT-Creation-Date: 2018-09-06 14:49-0700\n" "PO-Revision-Date: 2018-08-30 23:04-0700\n" "Last-Translator: Timothy \n" "Language-Team: fil\n" @@ -2060,6 +2060,74 @@ msgstr "Pull hindi ginagamit kapag ang direksyon ay output." msgid "Unsupported pull value." msgstr "Hindi suportado ang pull value." +#: shared-bindings/displayio/Bitmap.c:84 +#, fuzzy +msgid "y should be an int" +msgstr "Haba ay dapat int" + +#: shared-bindings/displayio/Bitmap.c:89 +#, fuzzy +msgid "row buffer must be a bytearray or array of type 'b' or 'B'" +msgstr "" +"ang sample_source buffer ay dapat na isang bytearray o array ng uri na 'h', " +"'H', 'b' o'B'" + +#: shared-bindings/displayio/Bitmap.c:94 +#, fuzzy +msgid "row data must be a buffer" +msgstr "constant ay dapat na integer" + +#: shared-bindings/displayio/FourWire.c:55 +#: shared-bindings/displayio/FourWire.c:64 +msgid "displayio is a work in progress" +msgstr "" + +#: shared-bindings/displayio/Group.c:65 +#, fuzzy +msgid "Group must have size at least 1" +msgstr "Buffer dapat ay hindi baba sa 1 na haba" + +#: shared-bindings/displayio/Palette.c:96 +#, fuzzy +msgid "color buffer must be a bytearray or array of type 'b' or 'B'" +msgstr "" +"ang sample_source buffer ay dapat na isang bytearray o array ng uri na 'h', " +"'H', 'b' o'B'" + +#: shared-bindings/displayio/Palette.c:102 +msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)" +msgstr "" + +#: shared-bindings/displayio/Palette.c:106 +#, fuzzy +msgid "color must be between 0x000000 and 0xffffff" +msgstr "Sa gitna ng 0 o 255 dapat ang bytes." + +#: shared-bindings/displayio/Palette.c:110 +#, fuzzy +msgid "color buffer must be a buffer or int" +msgstr "buffer ay dapat bytes-like object" + +#: shared-bindings/displayio/Palette.c:123 +#: shared-bindings/displayio/Palette.c:137 +msgid "palette_index should be an int" +msgstr "" + +#: shared-bindings/displayio/Sprite.c:45 +#, fuzzy +msgid "position must be 2-tuple" +msgstr "stop dapat 1 o 2" + +#: shared-bindings/displayio/Sprite.c:87 +#, fuzzy +msgid "unsupported bitmap type" +msgstr "Hindi supportadong baudrate" + +#: shared-bindings/displayio/Sprite.c:152 +#, fuzzy +msgid "palette must be displayio.Palette" +msgstr "ang palette ay dapat 32 bytes ang haba" + #: shared-bindings/gamepad/GamePad.c:100 msgid "too many arguments" msgstr "masyadong maraming argumento" @@ -2277,6 +2345,18 @@ msgstr "Hindi maaring mabasa kapag walang MISO pin." msgid "Cannot transfer without MOSI and MISO pins." msgstr "Hindi maaaring ilipat kapag walang MOSI at MISO pin." +#: shared-module/displayio/Bitmap.c:49 +msgid "Only bit maps of 8 bit color or less are supported" +msgstr "" + +#: shared-module/displayio/Bitmap.c:69 +msgid "row must be packed and word aligned" +msgstr "" + +#: shared-module/displayio/Group.c:39 +msgid "Group full" +msgstr "" + #: shared-module/struct/__init__.c:39 msgid "'S' and 'O' are not supported format types" msgstr "Ang 'S' at 'O' ay hindi suportadong uri ng format" diff --git a/locale/fr.po b/locale/fr.po index 3bbd77969..740830e10 100644 --- a/locale/fr.po +++ b/locale/fr.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: 0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-08-30 16:12-0700\n" +"POT-Creation-Date: 2018-09-06 14:49-0700\n" "PO-Revision-Date: 2018-08-14 11:01+0200\n" "Last-Translator: Pierrick Couturier \n" "Language-Team: fr\n" @@ -2051,24 +2051,19 @@ msgstr "Le tirage 'pull' n'est pas utilisé quand la direction est 'output'" msgid "Unsupported pull value." msgstr "Valeur de 'pull' non supportée" -#: shared-bindings/displayio/Bitmap.c:66 -#, fuzzy -msgid "value_size must be power of two" -msgstr "'len' doit être un multiple de 4" - -#: shared-bindings/displayio/Bitmap.c:85 +#: shared-bindings/displayio/Bitmap.c:84 #, fuzzy msgid "y should be an int" msgstr "La longueur doit être entière" -#: shared-bindings/displayio/Bitmap.c:90 +#: shared-bindings/displayio/Bitmap.c:89 #, fuzzy msgid "row buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" "le tampon de sample_source doit être un bytearray ou un tableau de type " "'h','H', 'b' ou 'B'" -#: shared-bindings/displayio/Bitmap.c:95 +#: shared-bindings/displayio/Bitmap.c:94 #, fuzzy msgid "row data must be a buffer" msgstr "les constantes doivent être des entiers" @@ -2083,44 +2078,44 @@ msgstr "" msgid "Group must have size at least 1" msgstr "Le tampon doit être de longueur au moins 1" -#: shared-bindings/displayio/Palette.c:94 +#: shared-bindings/displayio/Palette.c:96 #, fuzzy msgid "color buffer must be a bytearray or array of type 'b' or 'B'" msgstr "" "le tampon de sample_source doit être un bytearray ou un tableau de type " "'h','H', 'b' ou 'B'" -#: shared-bindings/displayio/Palette.c:100 -msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)" +#: shared-bindings/displayio/Palette.c:102 +msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)" msgstr "" -#: shared-bindings/displayio/Palette.c:104 +#: shared-bindings/displayio/Palette.c:106 #, fuzzy msgid "color must be between 0x000000 and 0xffffff" msgstr "Les octets 'bytes' doivent être entre 0 et 255" -#: shared-bindings/displayio/Palette.c:108 +#: shared-bindings/displayio/Palette.c:110 #, fuzzy msgid "color buffer must be a buffer or int" msgstr "le tampon doit être un objet bytes-like" -#: shared-bindings/displayio/Palette.c:124 -#: shared-bindings/displayio/Palette.c:138 +#: shared-bindings/displayio/Palette.c:123 +#: shared-bindings/displayio/Palette.c:137 #, fuzzy -msgid "value should be an int" +msgid "palette_index should be an int" msgstr "Les valeurs du tableau doivent être des octets simples 'bytes'" -#: shared-bindings/displayio/Sprite.c:44 +#: shared-bindings/displayio/Sprite.c:45 #, fuzzy msgid "position must be 2-tuple" msgstr "stop doit être 1 ou 2" -#: shared-bindings/displayio/Sprite.c:86 +#: shared-bindings/displayio/Sprite.c:87 #, fuzzy msgid "unsupported bitmap type" msgstr "Débit non supporté" -#: shared-bindings/displayio/Sprite.c:151 +#: shared-bindings/displayio/Sprite.c:152 #, fuzzy msgid "palette must be displayio.Palette" msgstr "la palette doit être longue de 32 octets" @@ -2346,7 +2341,11 @@ msgstr "Impossible de lire sans broche MISO." msgid "Cannot transfer without MOSI and MISO pins." msgstr "Pas de transfert sans broches MOSI et MISO" -#: shared-module/displayio/Bitmap.c:60 +#: shared-module/displayio/Bitmap.c:49 +msgid "Only bit maps of 8 bit color or less are supported" +msgstr "" + +#: shared-module/displayio/Bitmap.c:69 msgid "row must be packed and word aligned" msgstr "" @@ -2361,3 +2360,7 @@ msgstr "'S' et 'O' ne sont pas des types de format supportés" #: shared-module/struct/__init__.c:83 msgid "too many arguments provided with the given format" msgstr "trop d'arguments fournis avec ce format" + +#, fuzzy +#~ msgid "value_size must be power of two" +#~ msgstr "'len' doit être un multiple de 4" diff --git a/ports/atmel-samd/boards/hallowing_m0_express/board.c b/ports/atmel-samd/boards/hallowing_m0_express/board.c index 85bcaabc7..b47c5fbb2 100644 --- a/ports/atmel-samd/boards/hallowing_m0_express/board.c +++ b/ports/atmel-samd/boards/hallowing_m0_express/board.c @@ -27,14 +27,17 @@ #include "boards/board.h" #include "shared-bindings/displayio/FourWire.h" +#include "shared-module/displayio/mipi_constants.h" #include "tick.h" displayio_fourwire_obj_t board_display_obj; +#define DELAY 0x80 + uint8_t display_init_sequence[] = { - 0x01, 0, // SWRESET - 0x11, 0, // SLPOUT + 0x01, 0 | DELAY, 150, // SWRESET + 0x11, 0 | DELAY, 255, // SLPOUT 0xb1, 3, 0x01, 0x2C, 0x2D, // _FRMCTR1 0xb2, 3, 0x01, 0x2C, 0x2D, // 0xb3, 6, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D, @@ -60,36 +63,50 @@ uint8_t display_init_sequence[] = { 0x00, 0x00, 0x02, 0x10, 0x2a, 3, 0x02, 0x00, 0x81, // _CASET XSTART = 2, XEND = 129 0x2b, 3, 0x02, 0x00, 0x81, // _RASET XSTART = 2, XEND = 129 - 0x13, 0, // _NORON - 0x29, 0, // _DISPON + 0x13, 0 | DELAY, 10, // _NORON + 0x29, 0 | DELAY, 100, // _DISPON }; void board_init(void) { board_display_obj.base.type = &displayio_fourwire_type; common_hal_displayio_fourwire_construct(&board_display_obj, - &pin_PB23, &pin_PB22, &pin_PA28, &pin_PA01, &pin_PA27, - 128, 128, 2, 0, 16, 0x2a, 0x2b, 0x2c); + &pin_PB23, // Clock + &pin_PB22, // Data + &pin_PA28, // Command or data + &pin_PA01, // Chip select + &pin_PA27, // Reset + 128, // Width + 128, // Height + 2, // column start + 0, // row start + 16, // Color depth + MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command + MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command + MIPI_COMMAND_WRITE_MEMORY_START); // Write memory command uint32_t i = 0; common_hal_displayio_fourwire_begin_transaction(&board_display_obj); while (i < sizeof(display_init_sequence)) { uint8_t *cmd = display_init_sequence + i; uint8_t data_size = *(cmd + 1); + bool delay = (data_size & DELAY) != 0; + data_size &= ~DELAY; uint8_t *data = cmd + 2; common_hal_displayio_fourwire_send(&board_display_obj, true, cmd, 1); common_hal_displayio_fourwire_send(&board_display_obj, false, data, data_size); - if (*cmd == 0x01) { - uint64_t start = ticks_ms; - while (ticks_ms - start < 120) {} - } else if (*cmd == 0x11) { + 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 < 500) {} + while (ticks_ms - start < delay_length_ms) {} } else { uint64_t start = ticks_ms; while (ticks_ms - start < 10) {} } i += 2 + data_size; - } common_hal_displayio_fourwire_end_transaction(&board_display_obj); } diff --git a/ports/atmel-samd/boards/hallowing_m0_express/pins.c b/ports/atmel-samd/boards/hallowing_m0_express/pins.c index 28c3d6a5c..7d4afc7a4 100644 --- a/ports/atmel-samd/boards/hallowing_m0_express/pins.c +++ b/ports/atmel-samd/boards/hallowing_m0_express/pins.c @@ -58,6 +58,6 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, - { MP_ROM_QSTR(MP_QSTR_display), MP_ROM_PTR(&board_display_obj)} + { MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&board_display_obj)} }; MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table); diff --git a/ports/atmel-samd/common-hal/displayio/FourWire.c b/ports/atmel-samd/common-hal/displayio/FourWire.c index 033970c77..6c3b75a85 100644 --- a/ports/atmel-samd/common-hal/displayio/FourWire.c +++ b/ports/atmel-samd/common-hal/displayio/FourWire.c @@ -96,21 +96,17 @@ int32_t common_hal_displayio_fourwire_wait_for_frame(displayio_fourwire_obj_t* s return 0; } -static uint16_t swap(uint16_t x) { - return (x & 0x00ff) << 8 | x >> 8; -} - void displayio_fourwire_start_region_update(displayio_fourwire_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { // TODO(tannewt): Handle displays with single byte bounds. common_hal_displayio_fourwire_begin_transaction(self); uint16_t data[2]; common_hal_displayio_fourwire_send(self, true, &self->set_column_command, 1); - data[0] = swap(x0 + self->colstart); - data[1] = swap(x1-1 + self->colstart); + data[0] = __builtin_bswap16(x0 + self->colstart); + data[1] = __builtin_bswap16(x1-1 + self->colstart); common_hal_displayio_fourwire_send(self, false, (uint8_t*) data, 4); common_hal_displayio_fourwire_send(self, true, &self->set_row_command, 1); - data[0] = swap(y0 + 1 + self->rowstart); - data[1] = swap(y1 + self->rowstart); + data[0] = __builtin_bswap16(y0 + 1 + self->rowstart); + data[1] = __builtin_bswap16(y1 + self->rowstart); common_hal_displayio_fourwire_send(self, false, (uint8_t*) data, 4); common_hal_displayio_fourwire_send(self, true, &self->write_ram_command, 1); } diff --git a/shared-bindings/displayio/Bitmap.h b/shared-bindings/displayio/Bitmap.h index 2ae468481..b736f9459 100644 --- a/shared-bindings/displayio/Bitmap.h +++ b/shared-bindings/displayio/Bitmap.h @@ -32,7 +32,7 @@ extern const mp_obj_type_t displayio_bitmap_type; void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width, - uint32_t height, uint32_t value_size); + uint32_t height, uint32_t bits_per_value); void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len); diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index 99963f131..21744f947 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -38,10 +38,10 @@ //| .. currentmodule:: displayio //| -//| :class:`Palette` -- Stores a mapping from bitmap pixel values to display colors +//| :class:`Palette` -- Stores a mapping from bitmap pixel palette_indexes to display colors //| =============================================================================== //| -//| Map a pixel value to a full color. Colors are transformed to the display's format internally to +//| Map a pixel palette_index to a full color. Colors are transformed to the display's format internally to //| save memory. //| //| .. warning:: This will be changed before 4.0.0. Consider it very experimental. @@ -86,7 +86,7 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val return MP_OBJ_NULL; } displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); - size_t index = mp_get_index(&displayio_palette_type, self->max_value, index_in, false); + size_t index = mp_get_index(&displayio_palette_type, self->color_count, index_in, false); uint32_t color; mp_int_t int_value; @@ -99,7 +99,7 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val if (bufinfo.len == 3 || bufinfo.len == 4) { color = buf[0] << 16 | buf[1] << 8 | buf[2]; } else { - mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)")); + mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)")); } } else if (mp_obj_get_int_maybe(value, &int_value)) { if (int_value < 0 || int_value > 0xffffff) { @@ -113,30 +113,30 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val return mp_const_none; } -//| .. method:: make_transparent(value) +//| .. method:: make_transparent(palette_index) //| -STATIC mp_obj_t displayio_palette_obj_make_transparent(mp_obj_t self_in, mp_obj_t value_obj) { +STATIC mp_obj_t displayio_palette_obj_make_transparent(mp_obj_t self_in, mp_obj_t palette_index_obj) { displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t value; - if (!mp_obj_get_int_maybe(value_obj, &value)) { - mp_raise_ValueError(translate("value should be an int")); + mp_int_t palette_index; + if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) { + mp_raise_ValueError(translate("palette_index should be an int")); } - common_hal_displayio_palette_make_transparent(self, value); + common_hal_displayio_palette_make_transparent(self, palette_index); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_make_transparent_obj, displayio_palette_obj_make_transparent); -//| .. method:: make_opaque(value) +//| .. method:: make_opaque(palette_index) //| -STATIC mp_obj_t displayio_palette_obj_make_opaque(mp_obj_t self_in, mp_obj_t value_obj) { +STATIC mp_obj_t displayio_palette_obj_make_opaque(mp_obj_t self_in, mp_obj_t palette_index_obj) { displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t value; - if (!mp_obj_get_int_maybe(value_obj, &value)) { - mp_raise_ValueError(translate("value should be an int")); + mp_int_t palette_index; + if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) { + mp_raise_ValueError(translate("palette_index should be an int")); } - common_hal_displayio_palette_make_opaque(self, value); + common_hal_displayio_palette_make_opaque(self, palette_index); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_make_opaque_obj, displayio_palette_obj_make_opaque); diff --git a/shared-bindings/displayio/Palette.h b/shared-bindings/displayio/Palette.h index fde6ab7d3..767fc7b63 100644 --- a/shared-bindings/displayio/Palette.h +++ b/shared-bindings/displayio/Palette.h @@ -32,9 +32,9 @@ extern const mp_obj_type_t displayio_palette_type; void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t color_count); -void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t value, uint32_t color); +void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t palette_index, uint32_t color); -void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t value); -void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t value); +void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t palette_index); +void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_PALETTE_H diff --git a/shared-bindings/displayio/Sprite.c b/shared-bindings/displayio/Sprite.c index 25ea79f5c..223a8a394 100644 --- a/shared-bindings/displayio/Sprite.c +++ b/shared-bindings/displayio/Sprite.c @@ -35,7 +35,8 @@ #include "shared-bindings/displayio/Bitmap.h" #include "supervisor/shared/translate.h" -void parse_position(mp_obj_t position_obj, int16_t* x, int16_t* y) { +void unpack_position(mp_obj_t position_obj, int16_t* x, int16_t* y) { + // TODO(tannewt): Support any value sequence such as bytearray or bytes. mp_obj_tuple_t *position = MP_OBJ_TO_PTR(position_obj); if (MP_OBJ_IS_TYPE(position_obj, &mp_type_tuple) && position->len == 2) { *x = mp_obj_get_int(position->items[0]); @@ -88,7 +89,7 @@ STATIC mp_obj_t displayio_sprite_make_new(const mp_obj_type_t *type, size_t n_ar int16_t x = 0; int16_t y = 0; mp_obj_t position_obj = args[ARG_position].u_obj; - parse_position(position_obj, &x, &y); + unpack_position(position_obj, &x, &y); displayio_sprite_t *self = m_new_obj(displayio_sprite_t); self->base.type = &displayio_sprite_type; @@ -120,7 +121,7 @@ STATIC mp_obj_t displayio_sprite_obj_set_position(mp_obj_t self_in, mp_obj_t val int16_t x = 0; int16_t y = 0; - parse_position(value, &x, &y); + unpack_position(value, &x, &y); common_hal_displayio_sprite_set_position(self, x, y); diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 453dc1c59..addc341a7 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -31,8 +31,8 @@ #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; + uint32_t height, uint32_t bits_per_value) { + uint32_t row_width = width * bits_per_value; // word align if (row_width % 32 != 0) { self->stride = (row_width / 32 + 1); @@ -43,16 +43,25 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi self->height = height; self->data = m_malloc(self->stride * height * sizeof(uint32_t), false); - self->bits_per_value = value_size; + self->bits_per_value = bits_per_value; - self->x_shift = 0; + if (bits_per_value > 8) { + mp_raise_NotImplementedError(translate("Only bit maps of 8 bit color or less are supported")); + } + + // Division and modulus can be slow because it has to handle any integer. We know bits_per_value + // is a power of two. We divide and mod by bits_per_value to compute the offset into the byte + // array. So, we can the offset computation to simplify to a shift for division and mask for mod. + + self->x_shift = 0; // Used to divide the index by the number of pixels per word. Its used in a + // shift which effectively divides by 2 ** x_shift. uint32_t power_of_two = 1; - while (power_of_two < 32 / value_size ) { + while (power_of_two < 32 / bits_per_value ) { 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; + self->bitmask = (1 << bits_per_value) - 1; } void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len) { diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c index e2ec6e057..5f410d786 100644 --- a/shared-module/displayio/Palette.c +++ b/shared-module/displayio/Palette.c @@ -27,7 +27,7 @@ #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->color_count = 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) { @@ -36,35 +36,35 @@ void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t 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_opaque(displayio_palette_t* self, uint32_t palette_index) { + self->opaque[palette_index / 32] &= ~(0x1 << (palette_index % 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_make_transparent(displayio_palette_t* self, uint32_t palette_index) { + self->opaque[palette_index / 32] |= (0x1 << (palette_index % 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); +void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t palette_index, uint32_t color) { + uint32_t shift = (palette_index % 2) * 16; + uint32_t masked = self->colors[palette_index / 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; + packed = __builtin_bswap16(packed); + self->colors[palette_index / 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) { +bool displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index, uint16_t* color) { + if (palette_index > self->color_count) { return false; } - if ((self->opaque[value / 32] & (0x1 << (value % 32))) != 0) { + if ((self->opaque[palette_index / 32] & (0x1 << (palette_index % 32))) != 0) { return false; } - *color = (self->colors[value / 2] >> (16 * (value % 2))) & 0xffff; + *color = (self->colors[palette_index / 2] >> (16 * (palette_index % 2))) & 0xffff; return true; } diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h index 22c737c9d..5a5f1d287 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -36,11 +36,11 @@ typedef struct { mp_obj_base_t base; uint32_t* opaque; uint32_t* colors; - uint8_t max_value; + uint8_t color_count; bool needs_refresh; } displayio_palette_t; -bool displayio_palette_get_color(displayio_palette_t *palette, uint32_t value, uint16_t* color); +bool displayio_palette_get_color(displayio_palette_t *palette, uint32_t palette_index, uint16_t* color); bool displayio_palette_needs_refresh(displayio_palette_t *self); void displayio_palette_finish_refresh(displayio_palette_t *self); diff --git a/shared-module/displayio/mipi_constants.h b/shared-module/displayio/mipi_constants.h new file mode 100644 index 000000000..3cb7e4292 --- /dev/null +++ b/shared-module/displayio/mipi_constants.h @@ -0,0 +1,37 @@ +/* + * 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_MIPI_CONSTANTS_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_MIPI_CONSTANTS_H + +// More info here: https://www.tonylabs.com/wp-content/uploads/MIPI_DCS_specification_v1.02.00.pdf +enum mipi_command { + MIPI_COMMAND_SET_COLUMN_ADDRESS = 0x2a, + MIPI_COMMAND_SET_PAGE_ADDRESS = 0x2b, + MIPI_COMMAND_WRITE_MEMORY_START = 0x2c, +}; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_MIPI_CONSTANTS_H -- cgit v1.2.3