diff options
Diffstat (limited to 'circuitpython/shared-module/displayio')
29 files changed, 5244 insertions, 0 deletions
diff --git a/circuitpython/shared-module/displayio/Bitmap.c b/circuitpython/shared-module/displayio/Bitmap.c new file mode 100644 index 0000000..933d3a8 --- /dev/null +++ b/circuitpython/shared-module/displayio/Bitmap.c @@ -0,0 +1,256 @@ +/* + * 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 bits_per_value) { + uint32_t row_width = width * bits_per_value; + // align to uint32_t + uint8_t align_bits = 8 * sizeof(uint32_t); + if (row_width % align_bits != 0) { + self->stride = (row_width / align_bits + 1); + } else { + self->stride = row_width / align_bits; + } + self->width = width; + self->height = height; + self->data = m_malloc(self->stride * height * sizeof(uint32_t), false); + self->read_only = false; + self->bits_per_value = bits_per_value; + + if (bits_per_value > 8 && bits_per_value != 16 && bits_per_value != 32) { + mp_raise_NotImplementedError(translate("Invalid bits per value")); + } + + // 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 < align_bits / 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 << bits_per_value) - 1; + + self->dirty_area.x1 = 0; + self->dirty_area.x2 = width; + self->dirty_area.y1 = 0; + self->dirty_area.y2 = height; +} + +uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self) { + return self->height; +} + +uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self) { + return self->width; +} + +uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self) { + return self->bits_per_value; +} + +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; + uint32_t bytes_per_value = self->bits_per_value / 8; + if (bytes_per_value < 1) { + uint32_t word = self->data[row_start + (x >> self->x_shift)]; + + return (word >> (sizeof(uint32_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask; + } else { + uint32_t *row = self->data + row_start; + if (bytes_per_value == 1) { + return ((uint8_t *)row)[x]; + } else if (bytes_per_value == 2) { + return ((uint16_t *)row)[x]; + } else if (bytes_per_value == 4) { + return ((uint32_t *)row)[x]; + } + } + return 0; +} + +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) { + if (self->read_only) { + mp_raise_RuntimeError(translate("Read-only object")); + } + + displayio_area_t area = *dirty_area; + displayio_area_canon(&area); + displayio_area_union(&area, &self->dirty_area, &area); + displayio_area_t bitmap_area = {0, 0, self->width, self->height, NULL}; + displayio_area_compute_overlap(&area, &bitmap_area, &self->dirty_area); +} + +void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { + // Writes the color index value into a pixel position + // Must update the dirty area separately + + // Update one pixel of data + int32_t row_start = y * self->stride; + uint32_t bytes_per_value = self->bits_per_value / 8; + if (bytes_per_value < 1) { + uint32_t bit_position = (sizeof(uint32_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value); + uint32_t index = row_start + (x >> self->x_shift); + uint32_t word = self->data[index]; + word &= ~(self->bitmask << bit_position); + word |= (value & self->bitmask) << bit_position; + self->data[index] = word; + } else { + uint32_t *row = self->data + row_start; + if (bytes_per_value == 1) { + ((uint8_t *)row)[x] = value; + } else if (bytes_per_value == 2) { + ((uint16_t *)row)[x] = value; + } else if (bytes_per_value == 4) { + ((uint32_t *)row)[x] = value; + } + } +} + +void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source, + int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_index, bool skip_index_none) { + // Copy region of "source" bitmap into "self" bitmap at location x,y in the "self" + // If skip_value is encountered in the source bitmap, it will not be copied. + // If skip_value is `None`, then all pixels are copied. + // This function assumes input checks were performed for pixel index entries. + + // Update the dirty area + int16_t dirty_x_max = (x + (x2 - x1)); + if (dirty_x_max > self->width) { + dirty_x_max = self->width; + } + int16_t dirty_y_max = y + (y2 - y1); + if (dirty_y_max > self->height) { + dirty_y_max = self->height; + } + + displayio_area_t a = { x, y, dirty_x_max, dirty_y_max, NULL}; + displayio_bitmap_set_dirty_area(self, &a); + + bool x_reverse = false; + bool y_reverse = false; + + // Add reverse direction option to protect blitting of self bitmap back into self bitmap + if (x > x1) { + x_reverse = true; + } + if (y > y1) { + y_reverse = true; + } + + // simplest version - use internal functions for get/set pixels + for (int16_t i = 0; i < (x2 - x1); i++) { + + const int xs_index = x_reverse ? ((x2) - i - 1) : x1 + i; // x-index into the source bitmap + const int xd_index = x_reverse ? ((x + (x2 - x1)) - i - 1) : x + i; // x-index into the destination bitmap + + if ((xd_index >= 0) && (xd_index < self->width)) { + for (int16_t j = 0; j < (y2 - y1); j++) { + + const int ys_index = y_reverse ? ((y2) - j - 1) : y1 + j; // y-index into the source bitmap + const int yd_index = y_reverse ? ((y + (y2 - y1)) - j - 1) : y + j; // y-index into the destination bitmap + + if ((yd_index >= 0) && (yd_index < self->height)) { + uint32_t value = common_hal_displayio_bitmap_get_pixel(source, xs_index, ys_index); + if ((skip_index_none) || (value != skip_index)) { // write if skip_value_none is True + displayio_bitmap_write_pixel(self, xd_index, yd_index, value); + } + } + } + } + } +} + +void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { + // update the dirty region + displayio_area_t a = {x, y, x + 1, y + 1, NULL}; + displayio_bitmap_set_dirty_area(self, &a); + + // write the pixel + displayio_bitmap_write_pixel(self, x, y, value); + +} + +displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail) { + if (self->dirty_area.x1 == self->dirty_area.x2) { + return tail; + } + self->dirty_area.next = tail; + return &self->dirty_area; +} + +void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) { + self->dirty_area.x1 = 0; + self->dirty_area.x2 = 0; +} + +void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) { + displayio_area_t a = {0, 0, self->width, self->height, NULL}; + displayio_bitmap_set_dirty_area(self, &a); + + // build the packed word + uint32_t word = 0; + for (uint8_t i = 0; i < 32 / self->bits_per_value; i++) { + word |= (value & self->bitmask) << (32 - ((i + 1) * self->bits_per_value)); + } + // copy it in + for (uint32_t i = 0; i < self->stride * self->height; i++) { + self->data[i] = word; + } +} + +int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + if ((flags & MP_BUFFER_WRITE) && self->read_only) { + return 1; + } + bufinfo->len = self->stride * self->height * sizeof(uint32_t); + bufinfo->buf = self->data; + switch (self->bits_per_value) { + case 32: + bufinfo->typecode = 'I'; + break; + case 16: + bufinfo->typecode = 'H'; + break; + default: + bufinfo->typecode = 'B'; + break; + } + return 0; +} diff --git a/circuitpython/shared-module/displayio/Bitmap.h b/circuitpython/shared-module/displayio/Bitmap.h new file mode 100644 index 0000000..0373ae8 --- /dev/null +++ b/circuitpython/shared-module/displayio/Bitmap.h @@ -0,0 +1,55 @@ +/* + * 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" +#include "shared-module/displayio/area.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t *data; + uint16_t stride; // uint32_t's + uint8_t bits_per_value; + uint8_t x_shift; + size_t x_mask; + displayio_area_t dirty_area; + uint16_t bitmask; + bool read_only; +} displayio_bitmap_t; + +void displayio_bitmap_finish_refresh(displayio_bitmap_t *self); +displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail); +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area); +void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H diff --git a/circuitpython/shared-module/displayio/ColorConverter.c b/circuitpython/shared-module/displayio/ColorConverter.c new file mode 100644 index 0000000..707601a --- /dev/null +++ b/circuitpython/shared-module/displayio/ColorConverter.c @@ -0,0 +1,287 @@ +/* + * 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/ColorConverter.h" + +#include "py/misc.h" +#include "py/runtime.h" + +#define NO_TRANSPARENT_COLOR (0x1000000) + +uint32_t displayio_colorconverter_dither_noise_1(uint32_t n) { + n = (n >> 13) ^ n; + int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; + return (uint32_t)(((float)nn / (1073741824.0f * 2)) * 255); +} + +uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) { + return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF); +} + +void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t *self, bool dither, displayio_colorspace_t input_colorspace) { + self->dither = dither; + self->transparent_color = NO_TRANSPARENT_COLOR; + self->input_colorspace = input_colorspace; +} + +uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) { + uint32_t r5 = (color_rgb888 >> 19); + uint32_t g6 = (color_rgb888 >> 10) & 0x3f; + uint32_t b5 = (color_rgb888 >> 3) & 0x1f; + return r5 << 11 | g6 << 5 | b5; +} + +uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + return (r8 * 19 + g8 * 182 + b8 * 54) / 255; +} + +uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + uint8_t max = MAX(r8, MAX(g8, b8)); + uint8_t min = MIN(r8, MIN(g8, b8)); + return max - min; +} + +uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + uint8_t max = MAX(r8, MAX(g8, b8)); + uint8_t min = MIN(r8, MIN(g8, b8)); + uint8_t c = max - min; + if (c == 0) { + return 0; + } + + int32_t hue = 0; + if (max == r8) { + hue = (((int32_t)(g8 - b8) * 40) / c) % 240; + } else if (max == g8) { + hue = (((int32_t)(b8 - r8) + (2 * c)) * 40) / c; + } else if (max == b8) { + hue = (((int32_t)(r8 - g8) + (4 * c)) * 40) / c; + } + if (hue < 0) { + hue += 240; + } + + return hue; +} + +void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t *colorspace, uint8_t pixel_hue, uint32_t *color) { + + int16_t hue_diff = colorspace->tricolor_hue - pixel_hue; + if ((-10 <= hue_diff && hue_diff <= 10) || hue_diff <= -220 || hue_diff >= 220) { + if (colorspace->grayscale) { + *color = 0; + } else { + *color = 1; + } + } else if (!colorspace->grayscale) { + *color = 0; + } +} + +void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t *colorspace, uint32_t input_color, uint32_t *output_color) { + displayio_input_pixel_t input_pixel; + input_pixel.pixel = input_color; + input_pixel.x = input_pixel.y = input_pixel.tile = input_pixel.tile_x = input_pixel.tile_y = 0; + + displayio_output_pixel_t output_pixel; + output_pixel.pixel = 0; + output_pixel.opaque = false; + + displayio_colorconverter_convert(self, colorspace, &input_pixel, &output_pixel); + + (*output_color) = output_pixel.pixel; +} + +void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t *self, bool dither) { + self->dither = dither; +} + +bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t *self) { + return self->dither; +} + +void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t *self, uint32_t transparent_color) { + if (self->transparent_color != NO_TRANSPARENT_COLOR) { + mp_raise_RuntimeError(translate("Only one color can be transparent at a time")); + } + self->transparent_color = transparent_color; +} + +void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t *self, uint32_t transparent_color) { + (void)transparent_color; + // NO_TRANSPARENT_COLOR will never equal a valid color + self->transparent_color = NO_TRANSPARENT_COLOR; +} + + +// Convert a single input pixel to RGB888 +uint32_t displayio_colorconverter_convert_pixel(displayio_colorspace_t colorspace, uint32_t pixel) { + switch (colorspace) { + case DISPLAYIO_COLORSPACE_RGB565_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_RGB565: { + uint32_t r8 = (pixel >> 11) << 3; + uint32_t g8 = ((pixel >> 5) << 2) & 0xff; + uint32_t b8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_RGB555_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_RGB555: { + uint32_t r8 = (pixel >> 10) << 3; + uint32_t g8 = ((pixel >> 5) << 3) & 0xff; + uint32_t b8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_BGR565_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_BGR565: { + uint32_t b8 = (pixel >> 11) << 3; + uint32_t g8 = ((pixel >> 5) << 2) & 0xff; + uint32_t r8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_BGR555_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_BGR555: { + uint32_t b8 = (pixel >> 10) << 3; + uint32_t g8 = ((pixel >> 5) << 3) & 0xff; + uint32_t r8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + default: + case DISPLAYIO_COLORSPACE_RGB888: + break; + + case DISPLAYIO_COLORSPACE_L8: { + uint32_t l8 = pixel & 0xff; + pixel = l8 * 0x010101; + } + break; + } + + return pixel; +} + +void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t *colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { + uint32_t pixel = input_pixel->pixel; + + if (self->transparent_color == pixel) { + output_color->opaque = false; + return; + } + + pixel = displayio_colorconverter_convert_pixel(self->input_colorspace, pixel); + + + if (self->dither) { + uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y)); + uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x + 33,input_pixel->tile_y)); + uint8_t randb = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y + 33)); + + uint32_t r8 = (pixel >> 16); + uint32_t g8 = (pixel >> 8) & 0xff; + uint32_t b8 = pixel & 0xff; + + if (colorspace->depth == 16) { + b8 = MIN(255,b8 + (randb & 0x07)); + r8 = MIN(255,r8 + (randr & 0x07)); + g8 = MIN(255,g8 + (randg & 0x03)); + } else { + int bitmask = 0xFF >> colorspace->depth; + b8 = MIN(255,b8 + (randb & bitmask)); + r8 = MIN(255,r8 + (randr & bitmask)); + g8 = MIN(255,g8 + (randg & bitmask)); + } + pixel = r8 << 16 | g8 << 8 | b8; + } + + if (colorspace->depth == 16) { + uint16_t packed = displayio_colorconverter_compute_rgb565(pixel); + if (colorspace->reverse_bytes_in_word) { + // swap bytes + packed = __builtin_bswap16(packed); + } + output_color->pixel = packed; + output_color->opaque = true; + return; + } else if (colorspace->tricolor) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + output_color->pixel = luma >> (8 - colorspace->depth); + if (displayio_colorconverter_compute_chroma(pixel) <= 16) { + if (!colorspace->grayscale) { + output_color->pixel = 0; + } + output_color->opaque = true; + return; + } + uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel); + displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, &output_color->pixel); + return; + } else if (colorspace->grayscale && colorspace->depth <= 8) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + size_t bitmask = (1 << colorspace->depth) - 1; + output_color->pixel = (luma >> colorspace->grayscale_bit) & bitmask; + output_color->opaque = true; + return; + } else if (colorspace->depth == 32) { + output_color->pixel = pixel; + output_color->opaque = true; + return; + } + output_color->opaque = false; +} + + + +// Currently no refresh logic is needed for a ColorConverter. +bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) { + return false; +} + +void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self) { +} diff --git a/circuitpython/shared-module/displayio/ColorConverter.h b/circuitpython/shared-module/displayio/ColorConverter.h new file mode 100644 index 0000000..7e4e981 --- /dev/null +++ b/circuitpython/shared-module/displayio/ColorConverter.h @@ -0,0 +1,56 @@ +/* + * 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_COLORCONVERTER_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" +#include "shared-module/displayio/Palette.h" + +typedef struct displayio_colorconverter { + mp_obj_base_t base; + bool dither; + uint8_t input_colorspace; + uint32_t transparent_color; +} displayio_colorconverter_t; + +bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self); +void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self); +void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t *colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color); + +uint32_t displayio_colorconverter_dither_noise_1(uint32_t n); +uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y); + +uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888); +uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888); +uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888); +uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888); +void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t *colorspace, uint8_t pixel_hue, uint32_t *color); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H diff --git a/circuitpython/shared-module/displayio/Display.c b/circuitpython/shared-module/displayio/Display.c new file mode 100644 index 0000000..255cd49 --- /dev/null +++ b/circuitpython/shared-module/displayio/Display.c @@ -0,0 +1,461 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/Display.h" + +#include "py/runtime.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/I2CDisplay.h" +#if CIRCUITPY_PARALLELDISPLAY +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#endif +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/display_core.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/tick.h" +#include "supervisor/usb.h" + +#include <stdint.h> +#include <string.h> + +#define DELAY 0x80 + +void common_hal_displayio_display_construct(displayio_display_obj_t *self, + mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, + uint16_t rotation, uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, + uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word, uint8_t set_column_command, + uint8_t set_row_command, uint8_t write_ram_command, + uint8_t *init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t *backlight_pin, + uint16_t brightness_command, mp_float_t brightness, bool auto_brightness, + bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second, + bool backlight_on_high, bool SH1107_addressing) { + + // Turn off auto-refresh as we init. + self->auto_refresh = false; + uint16_t ram_width = 0x100; + uint16_t ram_height = 0x100; + if (single_byte_bounds) { + ram_width = 0xff; + ram_height = 0xff; + } + displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation, + color_depth, grayscale, pixels_in_byte_share_row, bytes_per_cell, reverse_pixels_in_byte, reverse_bytes_in_word); + + self->set_column_command = set_column_command; + self->set_row_command = set_row_command; + self->write_ram_command = write_ram_command; + self->brightness_command = brightness_command; + self->auto_brightness = auto_brightness; + self->first_manual_refresh = !auto_refresh; + self->data_as_commands = data_as_commands; + self->backlight_on_high = backlight_on_high; + self->SH1107_addressing = SH1107_addressing && color_depth == 1; + + self->native_frames_per_second = native_frames_per_second; + self->native_ms_per_frame = 1000 / native_frames_per_second; + + uint32_t i = 0; + while (i < init_sequence_len) { + uint8_t *cmd = init_sequence + i; + uint8_t data_size = *(cmd + 1); + bool delay = (data_size & DELAY) != 0; + data_size &= ~DELAY; + uint8_t *data = cmd + 2; + while (!displayio_display_core_begin_transaction(&self->core)) { + RUN_BACKGROUND_TASKS; + } + if (self->data_as_commands) { + uint8_t full_command[data_size + 1]; + full_command[0] = cmd[0]; + memcpy(full_command + 1, data, data_size); + self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, full_command, data_size + 1); + } else { + self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, cmd, 1); + self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, data, data_size); + } + displayio_display_core_end_transaction(&self->core); + uint16_t delay_length_ms = 10; + if (delay) { + data_size++; + delay_length_ms = *(cmd + 1 + data_size); + if (delay_length_ms == 255) { + delay_length_ms = 500; + } + } + common_hal_time_delay_ms(delay_length_ms); + i += 2 + data_size; + } + + // Always set the backlight type in case we're reusing memory. + self->backlight_inout.base.type = &mp_type_NoneType; + if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) { + // Avoid PWM types and functions when the module isn't enabled + #if (CIRCUITPY_PWMIO) + pwmout_result_t result = common_hal_pwmio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 50000, false); + if (result != PWMOUT_OK) { + self->backlight_inout.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin); + common_hal_never_reset_pin(backlight_pin); + } else { + self->backlight_pwm.base.type = &pwmio_pwmout_type; + common_hal_pwmio_pwmout_never_reset(&self->backlight_pwm); + } + #else + // Otherwise default to digital + self->backlight_inout.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin); + common_hal_never_reset_pin(backlight_pin); + #endif + } + if (!self->auto_brightness && (self->backlight_inout.base.type != &mp_type_NoneType || + brightness_command != NO_BRIGHTNESS_COMMAND)) { + common_hal_displayio_display_set_brightness(self, brightness); + } else { + self->current_brightness = -1.0; + } + + // Set the group after initialization otherwise we may send pixels while we delay in + // initialization. + common_hal_displayio_display_show(self, &circuitpython_splash); + common_hal_displayio_display_set_auto_refresh(self, auto_refresh); +} + +bool common_hal_displayio_display_show(displayio_display_obj_t *self, displayio_group_t *root_group) { + return displayio_display_core_show(&self->core, root_group); +} + +uint16_t common_hal_displayio_display_get_width(displayio_display_obj_t *self) { + return displayio_display_core_get_width(&self->core); +} + +uint16_t common_hal_displayio_display_get_height(displayio_display_obj_t *self) { + return displayio_display_core_get_height(&self->core); +} + +bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t *self) { + return self->auto_brightness; +} + +void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t *self, bool auto_brightness) { + self->auto_brightness = auto_brightness; +} + +mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t *self) { + return self->current_brightness; +} + +bool common_hal_displayio_display_set_brightness(displayio_display_obj_t *self, mp_float_t brightness) { + self->updating_backlight = true; + if (!self->backlight_on_high) { + brightness = 1.0 - brightness; + } + bool ok = false; + + // Avoid PWM types and functions when the module isn't enabled + #if (CIRCUITPY_PWMIO) + bool ispwm = (self->backlight_pwm.base.type == &pwmio_pwmout_type) ? true : false; + #else + bool ispwm = false; + #endif + + if (ispwm) { + #if (CIRCUITPY_PWMIO) + common_hal_pwmio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t)(0xffff * brightness)); + ok = true; + #else + ok = false; + #endif + } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99); + ok = true; + } else if (self->brightness_command != NO_BRIGHTNESS_COMMAND) { + ok = displayio_display_core_begin_transaction(&self->core); + if (ok) { + if (self->data_as_commands) { + uint8_t set_brightness[2] = {self->brightness_command, (uint8_t)(0xff * brightness)}; + self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, set_brightness, 2); + } else { + uint8_t command = self->brightness_command; + uint8_t hex_brightness = 0xff * brightness; + self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &command, 1); + self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, &hex_brightness, 1); + } + displayio_display_core_end_transaction(&self->core); + } + + } + self->updating_backlight = false; + if (ok) { + self->current_brightness = brightness; + } + return ok; +} + +mp_obj_t common_hal_displayio_display_get_bus(displayio_display_obj_t *self) { + return self->core.bus; +} + +mp_obj_t common_hal_displayio_display_get_root_group(displayio_display_obj_t *self) { + return self->core.current_group; +} + +STATIC const displayio_area_t *_get_refresh_areas(displayio_display_obj_t *self) { + if (self->core.full_refresh) { + self->core.area.next = NULL; + return &self->core.area; + } else if (self->core.current_group != NULL) { + return displayio_group_get_refresh_areas(self->core.current_group, NULL); + } + return NULL; +} + +STATIC void _send_pixels(displayio_display_obj_t *self, uint8_t *pixels, uint32_t length) { + if (!self->data_as_commands) { + self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &self->write_ram_command, 1); + } + self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, pixels, length); +} + +STATIC bool _refresh_area(displayio_display_obj_t *self, const displayio_area_t *area) { + uint16_t buffer_size = 128; // In uint32_ts + + displayio_area_t clipped; + // Clip the area to the display by overlapping the areas. If there is no overlap then we're done. + if (!displayio_display_core_clip_area(&self->core, area, &clipped)) { + return true; + } + uint16_t rows_per_buffer = displayio_area_height(&clipped); + uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / self->core.colorspace.depth; + uint16_t pixels_per_buffer = displayio_area_size(&clipped); + + uint16_t subrectangles = 1; + // for SH1107 and other boundary constrained controllers + // write one single row at a time + if (self->SH1107_addressing) { + subrectangles = rows_per_buffer / 8; // page addressing mode writes 8 rows at a time + rows_per_buffer = 8; + } else if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) { + rows_per_buffer = buffer_size * pixels_per_word / displayio_area_width(&clipped); + if (rows_per_buffer == 0) { + rows_per_buffer = 1; + } + // If pixels are packed by column then ensure rows_per_buffer is on a byte boundary. + if (self->core.colorspace.depth < 8 && !self->core.colorspace.pixels_in_byte_share_row) { + uint8_t pixels_per_byte = 8 / self->core.colorspace.depth; + if (rows_per_buffer % pixels_per_byte != 0) { + rows_per_buffer -= rows_per_buffer % pixels_per_byte; + } + } + subrectangles = displayio_area_height(&clipped) / rows_per_buffer; + if (displayio_area_height(&clipped) % rows_per_buffer != 0) { + subrectangles++; + } + pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped); + buffer_size = pixels_per_buffer / pixels_per_word; + if (pixels_per_buffer % pixels_per_word) { + buffer_size += 1; + } + } + + // Allocated and shared as a uint32_t array so the compiler knows the + // alignment everywhere. + uint32_t buffer[buffer_size]; + uint32_t mask_length = (pixels_per_buffer / 32) + 1; + uint32_t mask[mask_length]; + uint16_t remaining_rows = displayio_area_height(&clipped); + + for (uint16_t j = 0; j < subrectangles; j++) { + displayio_area_t subrectangle = { + .x1 = clipped.x1, + .y1 = clipped.y1 + rows_per_buffer * j, + .x2 = clipped.x2, + .y2 = clipped.y1 + rows_per_buffer * (j + 1) + }; + if (remaining_rows < rows_per_buffer) { + subrectangle.y2 = subrectangle.y1 + remaining_rows; + } + remaining_rows -= rows_per_buffer; + + displayio_display_core_set_region_to_update(&self->core, self->set_column_command, + self->set_row_command, NO_COMMAND, NO_COMMAND, self->data_as_commands, false, + &subrectangle, self->SH1107_addressing); + + uint16_t subrectangle_size_bytes; + if (self->core.colorspace.depth >= 8) { + subrectangle_size_bytes = displayio_area_size(&subrectangle) * (self->core.colorspace.depth / 8); + } else { + subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / self->core.colorspace.depth); + } + + memset(mask, 0, mask_length * sizeof(mask[0])); + memset(buffer, 0, buffer_size * sizeof(buffer[0])); + + displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer); + + // Can't acquire display bus; skip the rest of the data. + if (!displayio_display_core_bus_free(&self->core)) { + return false; + } + + displayio_display_core_begin_transaction(&self->core); + _send_pixels(self, (uint8_t *)buffer, subrectangle_size_bytes); + displayio_display_core_end_transaction(&self->core); + + // TODO(tannewt): Make refresh displays faster so we don't starve other + // background tasks. + #if CIRCUITPY_USB + usb_background(); + #endif + } + return true; +} + +STATIC void _refresh_display(displayio_display_obj_t *self) { + if (!displayio_display_core_start_refresh(&self->core)) { + // A refresh on this bus is already in progress. Try next display. + return; + } + const displayio_area_t *current_area = _get_refresh_areas(self); + while (current_area != NULL) { + _refresh_area(self, current_area); + current_area = current_area->next; + } + displayio_display_core_finish_refresh(&self->core); +} + +void common_hal_displayio_display_set_rotation(displayio_display_obj_t *self, int rotation) { + bool transposed = (self->core.rotation == 90 || self->core.rotation == 270); + bool will_transposed = (rotation == 90 || rotation == 270); + if (transposed != will_transposed) { + int tmp = self->core.width; + self->core.width = self->core.height; + self->core.height = tmp; + } + displayio_display_core_set_rotation(&self->core, rotation); + if (self == &displays[0].display) { + supervisor_stop_terminal(); + supervisor_start_terminal(self->core.width, self->core.height); + } + if (self->core.current_group != NULL) { + displayio_group_update_transform(self->core.current_group, &self->core.transform); + } +} + +uint16_t common_hal_displayio_display_get_rotation(displayio_display_obj_t *self) { + return self->core.rotation; +} + + +bool common_hal_displayio_display_refresh(displayio_display_obj_t *self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) { + if (!self->auto_refresh && !self->first_manual_refresh && (target_ms_per_frame != 0xffffffff)) { + uint64_t current_time = supervisor_ticks_ms64(); + uint32_t current_ms_since_real_refresh = current_time - self->core.last_refresh; + // Test to see if the real frame time is below our minimum. + if (current_ms_since_real_refresh > maximum_ms_per_real_frame) { + mp_raise_RuntimeError(translate("Below minimum frame rate")); + } + uint32_t current_ms_since_last_call = current_time - self->last_refresh_call; + self->last_refresh_call = current_time; + // Skip the actual refresh to help catch up. + if (current_ms_since_last_call > target_ms_per_frame) { + return false; + } + uint32_t remaining_time = target_ms_per_frame - (current_ms_since_real_refresh % target_ms_per_frame); + // We're ahead of the game so wait until we align with the frame rate. + while (supervisor_ticks_ms64() - self->last_refresh_call < remaining_time) { + RUN_BACKGROUND_TASKS; + } + } + self->first_manual_refresh = false; + _refresh_display(self); + return true; +} + +bool common_hal_displayio_display_get_auto_refresh(displayio_display_obj_t *self) { + return self->auto_refresh; +} + +void common_hal_displayio_display_set_auto_refresh(displayio_display_obj_t *self, + bool auto_refresh) { + self->first_manual_refresh = !auto_refresh; + if (auto_refresh != self->auto_refresh) { + if (auto_refresh) { + supervisor_enable_tick(); + } else { + supervisor_disable_tick(); + } + } + self->auto_refresh = auto_refresh; +} + +STATIC void _update_backlight(displayio_display_obj_t *self) { + if (!self->auto_brightness || self->updating_backlight) { + return; + } + if (supervisor_ticks_ms64() - self->last_backlight_refresh < 100) { + return; + } + // TODO(tannewt): Fade the backlight based on its existing value and a target value. The target + // should account for ambient light when possible. + common_hal_displayio_display_set_brightness(self, 1.0); + + self->last_backlight_refresh = supervisor_ticks_ms64(); +} + +void displayio_display_background(displayio_display_obj_t *self) { + _update_backlight(self); + + if (self->auto_refresh && (supervisor_ticks_ms64() - self->core.last_refresh) > self->native_ms_per_frame) { + _refresh_display(self); + } +} + +void release_display(displayio_display_obj_t *self) { + common_hal_displayio_display_set_auto_refresh(self, false); + release_display_core(&self->core); + #if (CIRCUITPY_PWMIO) + if (self->backlight_pwm.base.type == &pwmio_pwmout_type) { + common_hal_pwmio_pwmout_reset_ok(&self->backlight_pwm); + common_hal_pwmio_pwmout_deinit(&self->backlight_pwm); + } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_deinit(&self->backlight_inout); + } + #else + common_hal_digitalio_digitalinout_deinit(&self->backlight_inout); + #endif +} + +void reset_display(displayio_display_obj_t *self) { + common_hal_displayio_display_set_auto_refresh(self, true); + self->auto_brightness = true; + common_hal_displayio_display_show(self, NULL); +} + +void displayio_display_collect_ptrs(displayio_display_obj_t *self) { + displayio_display_core_collect_ptrs(&self->core); +} diff --git a/circuitpython/shared-module/displayio/Display.h b/circuitpython/shared-module/displayio/Display.h new file mode 100644 index 0000000..a0049f0 --- /dev/null +++ b/circuitpython/shared-module/displayio/Display.h @@ -0,0 +1,73 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H + +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/displayio/Group.h" +#if CIRCUITPY_PWMIO +#include "shared-bindings/pwmio/PWMOut.h" +#endif + +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + union { + digitalio_digitalinout_obj_t backlight_inout; + #if CIRCUITPY_PWMIO + pwmio_pwmout_obj_t backlight_pwm; + #endif + }; + uint64_t last_backlight_refresh; + uint64_t last_refresh_call; + mp_float_t current_brightness; + uint16_t brightness_command; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + uint8_t set_column_command; + uint8_t set_row_command; + uint8_t write_ram_command; + bool auto_refresh; + bool first_manual_refresh; + bool data_as_commands; + bool auto_brightness; + bool updating_backlight; + bool backlight_on_high; + // new quirk for sh1107 + bool SH1107_addressing; +} displayio_display_obj_t; + +void displayio_display_background(displayio_display_obj_t *self); +void release_display(displayio_display_obj_t *self); +void reset_display(displayio_display_obj_t *self); + +void displayio_display_collect_ptrs(displayio_display_obj_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H diff --git a/circuitpython/shared-module/displayio/EPaperDisplay.c b/circuitpython/shared-module/displayio/EPaperDisplay.c new file mode 100644 index 0000000..b1e9980 --- /dev/null +++ b/circuitpython/shared-module/displayio/EPaperDisplay.c @@ -0,0 +1,453 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/EPaperDisplay.h" + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/I2CDisplay.h" +#if CIRCUITPY_PARALLELDISPLAY +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#endif +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/tick.h" +#include "supervisor/usb.h" + +#include <stdint.h> +#include <string.h> + +#define DELAY 0x80 + +void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t *self, + mp_obj_t bus, const uint8_t *start_sequence, uint16_t start_sequence_len, + const uint8_t *stop_sequence, uint16_t stop_sequence_len, + uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, + int16_t colstart, int16_t rowstart, uint16_t rotation, + uint16_t set_column_window_command, uint16_t set_row_window_command, + uint16_t set_current_column_command, uint16_t set_current_row_command, + uint16_t write_black_ram_command, bool black_bits_inverted, uint16_t write_color_ram_command, bool color_bits_inverted, uint32_t highlight_color, uint16_t refresh_display_command, mp_float_t refresh_time, + const mcu_pin_obj_t *busy_pin, bool busy_state, mp_float_t seconds_per_frame, bool chip_select, bool grayscale, bool two_byte_sequence_length) { + if (highlight_color != 0x000000) { + self->core.colorspace.tricolor = true; + self->core.colorspace.tricolor_hue = displayio_colorconverter_compute_hue(highlight_color); + self->core.colorspace.tricolor_luma = displayio_colorconverter_compute_luma(highlight_color); + } + + displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation, 1, true, true, 1, true, true); + + self->set_column_window_command = set_column_window_command; + self->set_row_window_command = set_row_window_command; + self->set_current_column_command = set_current_column_command; + self->set_current_row_command = set_current_row_command; + self->write_black_ram_command = write_black_ram_command; + self->black_bits_inverted = black_bits_inverted; + self->write_color_ram_command = write_color_ram_command; + self->color_bits_inverted = color_bits_inverted; + self->refresh_display_command = refresh_display_command; + self->refresh_time = refresh_time * 1000; + self->busy_state = busy_state; + self->refreshing = false; + self->milliseconds_per_frame = seconds_per_frame * 1000; + self->chip_select = chip_select ? CHIP_SELECT_TOGGLE_EVERY_BYTE : CHIP_SELECT_UNTOUCHED; + self->grayscale = grayscale; + + self->start_sequence = start_sequence; + self->start_sequence_len = start_sequence_len; + self->stop_sequence = stop_sequence; + self->stop_sequence_len = stop_sequence_len; + + self->busy.base.type = &mp_type_NoneType; + self->two_byte_sequence_length = two_byte_sequence_length; + if (busy_pin != NULL) { + self->busy.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->busy, busy_pin); + common_hal_never_reset_pin(busy_pin); + } + + // Clear the color memory if it isn't in use. + if (highlight_color == 0x00 && write_color_ram_command != NO_COMMAND) { + // TODO: Clear + } + + // Set the group after initialization otherwise we may send pixels while we delay in + // initialization. + common_hal_displayio_epaperdisplay_show(self, &circuitpython_splash); +} + +bool common_hal_displayio_epaperdisplay_show(displayio_epaperdisplay_obj_t *self, displayio_group_t *root_group) { + return displayio_display_core_show(&self->core, root_group); +} + +STATIC const displayio_area_t *displayio_epaperdisplay_get_refresh_areas(displayio_epaperdisplay_obj_t *self) { + if (self->core.full_refresh) { + self->core.area.next = NULL; + return &self->core.area; + } + const displayio_area_t *first_area = NULL; + if (self->core.current_group != NULL) { + first_area = displayio_group_get_refresh_areas(self->core.current_group, NULL); + } + if (first_area != NULL && self->set_row_window_command == NO_COMMAND) { + self->core.area.next = NULL; + return &self->core.area; + } + return first_area; +} + +uint16_t common_hal_displayio_epaperdisplay_get_width(displayio_epaperdisplay_obj_t *self) { + return displayio_display_core_get_width(&self->core); +} + +uint16_t common_hal_displayio_epaperdisplay_get_height(displayio_epaperdisplay_obj_t *self) { + return displayio_display_core_get_height(&self->core); +} + +STATIC void wait_for_busy(displayio_epaperdisplay_obj_t *self) { + if (self->busy.base.type == &mp_type_NoneType) { + return; + } + while (common_hal_digitalio_digitalinout_get_value(&self->busy) == self->busy_state) { + RUN_BACKGROUND_TASKS; + } +} + +STATIC void send_command_sequence(displayio_epaperdisplay_obj_t *self, + bool should_wait_for_busy, const uint8_t *sequence, uint32_t sequence_len) { + uint32_t i = 0; + while (i < sequence_len) { + const uint8_t *cmd = sequence + i; + uint8_t data_size = *(cmd + 1); + bool delay = (data_size & DELAY) != 0; + const uint8_t *data = cmd + 2; + data_size &= ~DELAY; + if (self->two_byte_sequence_length) { + data_size = ((data_size & ~DELAY) << 8) + *(cmd + 2); + data = cmd + 3; + } + displayio_display_core_begin_transaction(&self->core); + self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, cmd, 1); + self->core.send(self->core.bus, DISPLAY_DATA, self->chip_select, data, data_size); + displayio_display_core_end_transaction(&self->core); + uint16_t delay_length_ms = 0; + if (delay) { + data_size++; + delay_length_ms = *(cmd + 1 + data_size); + if (delay_length_ms == 255) { + delay_length_ms = 500; + } + } + common_hal_time_delay_ms(delay_length_ms); + if (should_wait_for_busy) { + wait_for_busy(self); + } + i += 2 + data_size; + if (self->two_byte_sequence_length) { + i++; + } + } +} + +void displayio_epaperdisplay_change_refresh_mode_parameters(displayio_epaperdisplay_obj_t *self, + mp_buffer_info_t *start_sequence, float seconds_per_frame) { + self->start_sequence = (uint8_t *)start_sequence->buf; + self->start_sequence_len = start_sequence->len; + self->milliseconds_per_frame = seconds_per_frame * 1000; +} + +STATIC void displayio_epaperdisplay_start_refresh(displayio_epaperdisplay_obj_t *self) { + // run start sequence + self->core.bus_reset(self->core.bus); + + send_command_sequence(self, true, self->start_sequence, self->start_sequence_len); + displayio_display_core_start_refresh(&self->core); +} + +uint32_t common_hal_displayio_epaperdisplay_get_time_to_refresh(displayio_epaperdisplay_obj_t *self) { + if (self->core.last_refresh == 0) { + return 0; + } + // Refresh at seconds per frame rate. + uint32_t elapsed_time = supervisor_ticks_ms64() - self->core.last_refresh; + if (elapsed_time > self->milliseconds_per_frame) { + return 0; + } + return self->milliseconds_per_frame - elapsed_time; +} + +STATIC void displayio_epaperdisplay_finish_refresh(displayio_epaperdisplay_obj_t *self) { + // Actually refresh the display now that all pixel RAM has been updated. + displayio_display_core_begin_transaction(&self->core); + self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, &self->refresh_display_command, 1); + displayio_display_core_end_transaction(&self->core); + supervisor_enable_tick(); + self->refreshing = true; + + displayio_display_core_finish_refresh(&self->core); +} + +mp_obj_t common_hal_displayio_epaperdisplay_get_bus(displayio_epaperdisplay_obj_t *self) { + return self->core.bus; +} + +void common_hal_displayio_epaperdisplay_set_rotation(displayio_epaperdisplay_obj_t *self, int rotation) { + bool transposed = (self->core.rotation == 90 || self->core.rotation == 270); + bool will_transposed = (rotation == 90 || rotation == 270); + if (transposed != will_transposed) { + int tmp = self->core.width; + self->core.width = self->core.height; + self->core.height = tmp; + } + displayio_display_core_set_rotation(&self->core, rotation); + if (self == &displays[0].epaper_display) { + supervisor_stop_terminal(); + supervisor_start_terminal(self->core.width, self->core.height); + } + if (self->core.current_group != NULL) { + displayio_group_update_transform(self->core.current_group, &self->core.transform); + } +} + +uint16_t common_hal_displayio_epaperdisplay_get_rotation(displayio_epaperdisplay_obj_t *self) { + return self->core.rotation; +} + + +STATIC bool displayio_epaperdisplay_refresh_area(displayio_epaperdisplay_obj_t *self, const displayio_area_t *area) { + uint16_t buffer_size = 128; // In uint32_ts + + displayio_area_t clipped; + // Clip the area to the display by overlapping the areas. If there is no overlap then we're done. + if (!displayio_display_core_clip_area(&self->core, area, &clipped)) { + return true; + } + uint16_t subrectangles = 1; + uint16_t rows_per_buffer = displayio_area_height(&clipped); + uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / self->core.colorspace.depth; + uint16_t pixels_per_buffer = displayio_area_size(&clipped); + if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) { + rows_per_buffer = buffer_size * pixels_per_word / displayio_area_width(&clipped); + if (rows_per_buffer == 0) { + rows_per_buffer = 1; + } + subrectangles = displayio_area_height(&clipped) / rows_per_buffer; + if (displayio_area_height(&clipped) % rows_per_buffer != 0) { + subrectangles++; + } + pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped); + buffer_size = pixels_per_buffer / pixels_per_word; + if (pixels_per_buffer % pixels_per_word) { + buffer_size += 1; + } + } + + // Allocated and shared as a uint32_t array so the compiler knows the + // alignment everywhere. + uint32_t buffer[buffer_size]; + volatile uint32_t mask_length = (pixels_per_buffer / 32) + 1; + uint32_t mask[mask_length]; + + uint8_t passes = 1; + if (self->core.colorspace.tricolor || self->grayscale) { + passes = 2; + } + for (uint8_t pass = 0; pass < passes; pass++) { + uint16_t remaining_rows = displayio_area_height(&clipped); + + if (self->set_row_window_command != NO_COMMAND) { + displayio_display_core_set_region_to_update(&self->core, self->set_column_window_command, + self->set_row_window_command, self->set_current_column_command, self->set_current_row_command, + false, self->chip_select, &clipped, false /* SH1107_addressing */); + } + + uint8_t write_command = self->write_black_ram_command; + if (pass == 1) { + write_command = self->write_color_ram_command; + } + displayio_display_core_begin_transaction(&self->core); + self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, &write_command, 1); + displayio_display_core_end_transaction(&self->core); + + for (uint16_t j = 0; j < subrectangles; j++) { + displayio_area_t subrectangle = { + .x1 = clipped.x1, + .y1 = clipped.y1 + rows_per_buffer * j, + .x2 = clipped.x2, + .y2 = clipped.y1 + rows_per_buffer * (j + 1) + }; + if (remaining_rows < rows_per_buffer) { + subrectangle.y2 = subrectangle.y1 + remaining_rows; + } + remaining_rows -= rows_per_buffer; + + + uint16_t subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / self->core.colorspace.depth); + + memset(mask, 0, mask_length * sizeof(mask[0])); + memset(buffer, 0, buffer_size * sizeof(buffer[0])); + + self->core.colorspace.grayscale = true; + self->core.colorspace.grayscale_bit = 7; + if (pass == 1) { + if (self->grayscale) { // 4-color grayscale + self->core.colorspace.grayscale_bit = 6; + } else { // Tri-color + self->core.colorspace.grayscale = false; + } + } + displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer); + + // Invert it all. + if ((pass == 1 && self->color_bits_inverted) || + (pass == 0 && self->black_bits_inverted)) { + for (uint16_t k = 0; k < buffer_size; k++) { + buffer[k] = ~buffer[k]; + } + } + + if (!displayio_display_core_begin_transaction(&self->core)) { + // Can't acquire display bus; skip the rest of the data. Try next display. + return false; + } + self->core.send(self->core.bus, DISPLAY_DATA, self->chip_select, (uint8_t *)buffer, subrectangle_size_bytes); + displayio_display_core_end_transaction(&self->core); + + // TODO(tannewt): Make refresh displays faster so we don't starve other + // background tasks. + #if CIRCUITPY_USB + usb_background(); + #endif + } + } + + return true; +} + +bool common_hal_displayio_epaperdisplay_refresh(displayio_epaperdisplay_obj_t *self) { + + if (self->refreshing && self->busy.base.type == &digitalio_digitalinout_type) { + if (common_hal_digitalio_digitalinout_get_value(&self->busy) != self->busy_state) { + supervisor_disable_tick(); + self->refreshing = false; + // Run stop sequence but don't wait for busy because busy is set when sleeping. + send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len); + } else { + return false; + } + } + if (self->core.current_group == NULL) { + return true; + } + // Refresh at seconds per frame rate. + if (common_hal_displayio_epaperdisplay_get_time_to_refresh(self) > 0) { + return false; + } + if (!displayio_display_core_bus_free(&self->core)) { + // Can't acquire display bus; skip updating this display. Try next display. + return false; + } + const displayio_area_t *current_area = displayio_epaperdisplay_get_refresh_areas(self); + if (current_area == NULL) { + return true; + } + displayio_epaperdisplay_start_refresh(self); + while (current_area != NULL) { + displayio_epaperdisplay_refresh_area(self, current_area); + current_area = current_area->next; + } + displayio_epaperdisplay_finish_refresh(self); + return true; +} + +void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t *self) { + if (self->refreshing) { + bool refresh_done = false; + if (self->busy.base.type == &digitalio_digitalinout_type) { + bool busy = common_hal_digitalio_digitalinout_get_value(&self->busy); + refresh_done = busy != self->busy_state; + } else { + refresh_done = supervisor_ticks_ms64() - self->core.last_refresh > self->refresh_time; + } + if (refresh_done) { + supervisor_disable_tick(); + self->refreshing = false; + // Run stop sequence but don't wait for busy because busy is set when sleeping. + send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len); + } + } +} + +bool common_hal_displayio_epaperdisplay_get_busy(displayio_epaperdisplay_obj_t *self) { + displayio_epaperdisplay_background(self); + return self->refreshing; +} + +void release_epaperdisplay(displayio_epaperdisplay_obj_t *self) { + if (self->refreshing) { + wait_for_busy(self); + supervisor_disable_tick(); + self->refreshing = false; + // Run stop sequence but don't wait for busy because busy is set when sleeping. + send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len); + } + + release_display_core(&self->core); + if (self->busy.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_deinit(&self->busy); + } +} + +void displayio_epaperdisplay_collect_ptrs(displayio_epaperdisplay_obj_t *self) { + displayio_display_core_collect_ptrs(&self->core); + gc_collect_ptr((void *)self->start_sequence); + gc_collect_ptr((void *)self->stop_sequence); +} + +size_t maybe_refresh_epaperdisplay(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].epaper_display.base.type != &displayio_epaperdisplay_type || + displays[i].epaper_display.core.current_group != &circuitpython_splash) { + // Skip regular displays and those not showing the splash. + continue; + } + displayio_epaperdisplay_obj_t *display = &displays[i].epaper_display; + size_t time_to_refresh = common_hal_displayio_epaperdisplay_get_time_to_refresh(display); + if (time_to_refresh > 0) { + return time_to_refresh; + } + if (common_hal_displayio_epaperdisplay_refresh(display)) { + return 0; + } + // If we could refresh but it failed, then we want to retry. + return 1; + } + // Return 0 if no ePaper displays are available to pretend it was updated. + return 0; +} diff --git a/circuitpython/shared-module/displayio/EPaperDisplay.h b/circuitpython/shared-module/displayio/EPaperDisplay.h new file mode 100644 index 0000000..55feaf9 --- /dev/null +++ b/circuitpython/shared-module/displayio/EPaperDisplay.h @@ -0,0 +1,71 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_EPAPERDISPLAY_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_EPAPERDISPLAY_H + +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/displayio/Group.h" + +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + digitalio_digitalinout_obj_t busy; + uint32_t milliseconds_per_frame; + const uint8_t *start_sequence; + uint32_t start_sequence_len; + const uint8_t *stop_sequence; + uint32_t stop_sequence_len; + uint16_t refresh_time; + uint16_t set_column_window_command; + uint16_t set_row_window_command; + uint16_t set_current_column_command; + uint16_t set_current_row_command; + uint16_t write_black_ram_command; + uint16_t write_color_ram_command; + uint8_t refresh_display_command; + uint8_t hue; + bool busy_state; + bool black_bits_inverted; + bool color_bits_inverted; + bool refreshing; + bool grayscale; + display_chip_select_behavior_t chip_select; + bool two_byte_sequence_length; +} displayio_epaperdisplay_obj_t; + +void displayio_epaperdisplay_change_refresh_mode_parameters(displayio_epaperdisplay_obj_t *self, + mp_buffer_info_t *start_sequence, float seconds_per_frame); +void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t *self); +void release_epaperdisplay(displayio_epaperdisplay_obj_t *self); +size_t maybe_refresh_epaperdisplay(void); + +void displayio_epaperdisplay_collect_ptrs(displayio_epaperdisplay_obj_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_EPAPERDISPLAY_H diff --git a/circuitpython/shared-module/displayio/FourWire.c b/circuitpython/shared-module/displayio/FourWire.c new file mode 100644 index 0000000..41d4340 --- /dev/null +++ b/circuitpython/shared-module/displayio/FourWire.c @@ -0,0 +1,179 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/FourWire.h" + +#include <stdint.h> + +#include "py/gc.h" +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/display_core.h" + +void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t *self, + busio_spi_obj_t *spi, const mcu_pin_obj_t *command, + const mcu_pin_obj_t *chip_select, const mcu_pin_obj_t *reset, uint32_t baudrate, + uint8_t polarity, uint8_t phase) { + + self->bus = spi; + common_hal_busio_spi_never_reset(self->bus); + // Our object is statically allocated off the heap so make sure the bus object lives to the end + // of the heap as well. + gc_never_free(self->bus); + + self->frequency = baudrate; + self->polarity = polarity; + self->phase = phase; + + common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); + common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); + + self->command.base.type = &mp_type_NoneType; + if (command != NULL) { + self->command.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->command, command); + common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL); + common_hal_never_reset_pin(command); + } + self->reset.base.type = &mp_type_NoneType; + if (reset != NULL) { + self->reset.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->reset, reset); + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); + common_hal_never_reset_pin(reset); + common_hal_displayio_fourwire_reset(self); + } + + common_hal_never_reset_pin(chip_select); +} + +void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t *self) { + if (self->bus == &self->inline_bus) { + common_hal_busio_spi_deinit(self->bus); + } + + common_hal_reset_pin(self->command.pin); + common_hal_reset_pin(self->chip_select.pin); + common_hal_reset_pin(self->reset.pin); +} + +bool common_hal_displayio_fourwire_reset(mp_obj_t obj) { + displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); + if (self->reset.base.type == &mp_type_NoneType) { + return false; + } + common_hal_digitalio_digitalinout_set_value(&self->reset, false); + common_hal_mcu_delay_us(1000); + common_hal_digitalio_digitalinout_set_value(&self->reset, true); + common_hal_mcu_delay_us(1000); + return true; +} + +bool common_hal_displayio_fourwire_bus_free(mp_obj_t obj) { + displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); + if (!common_hal_busio_spi_try_lock(self->bus)) { + return false; + } + common_hal_busio_spi_unlock(self->bus); + return true; +} + +bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) { + displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); + if (!common_hal_busio_spi_try_lock(self->bus)) { + return false; + } + common_hal_busio_spi_configure(self->bus, self->frequency, self->polarity, + self->phase, 8); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + return true; +} + +void common_hal_displayio_fourwire_send(mp_obj_t obj, display_byte_type_t data_type, + display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) { + displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); + if (self->command.base.type == &mp_type_NoneType) { + // When the data/command pin is not specified, we simulate a 9-bit SPI mode, by + // adding a data/command bit to every byte, and then splitting the resulting data back + // into 8-bit chunks for transmission. If the length of the data being transmitted + // is not a multiple of 8, there will be additional bits at the end of the + // transmission. We toggle the CS pin to make the receiver discard them. + uint8_t buffer = 0; + uint8_t bits = 0; + uint8_t dc = (data_type == DISPLAY_DATA); + + for (size_t i = 0; i < data_length; i++) { + bits = (bits + 1) % 8; + + if (bits == 0) { + // send the previous byte and the dc bit + // we will send the current byte later + buffer = (buffer << 1) | dc; + common_hal_busio_spi_write(self->bus, &buffer, 1); + // send the current byte, because previous byte already filled all bits + common_hal_busio_spi_write(self->bus, &data[i], 1); + } else { + // send remaining bits from previous byte, dc and beginning of current byte + buffer = (buffer << (9 - bits)) | (dc << (8 - bits)) | (data[i] >> bits); + common_hal_busio_spi_write(self->bus, &buffer, 1); + } + // save the current byte + buffer = data[i]; + } + // send any remaining bits + if (bits > 0) { + buffer = buffer << (8 - bits); + common_hal_busio_spi_write(self->bus, &buffer, 1); + // toggle CS to discard superfluous bits + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } + } else { + common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA); + if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) { + // Toggle chip select after each command byte in case the display driver + // IC latches commands based on it. + for (size_t i = 0; i < data_length; i++) { + common_hal_busio_spi_write(self->bus, &data[i], 1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } + } else { + common_hal_busio_spi_write(self->bus, data, data_length); + } + } +} + +void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) { + displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_busio_spi_unlock(self->bus); +} diff --git a/circuitpython/shared-module/displayio/FourWire.h b/circuitpython/shared-module/displayio/FourWire.h new file mode 100644 index 0000000..b28c1ef --- /dev/null +++ b/circuitpython/shared-module/displayio/FourWire.h @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H + +#include "common-hal/busio/SPI.h" +#include "common-hal/digitalio/DigitalInOut.h" +#include "shared-module/displayio/Group.h" + +typedef struct { + mp_obj_base_t base; + busio_spi_obj_t *bus; + busio_spi_obj_t inline_bus; + digitalio_digitalinout_obj_t command; + digitalio_digitalinout_obj_t chip_select; + digitalio_digitalinout_obj_t reset; + uint32_t frequency; + uint8_t polarity; + uint8_t phase; +} displayio_fourwire_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H diff --git a/circuitpython/shared-module/displayio/Group.c b/circuitpython/shared-module/displayio/Group.c new file mode 100644 index 0000000..b9179f0 --- /dev/null +++ b/circuitpython/shared-module/displayio/Group.c @@ -0,0 +1,451 @@ +/* + * 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 "py/objlist.h" +#include "shared-bindings/displayio/TileGrid.h" + +#if CIRCUITPY_VECTORIO +#include "shared-bindings/vectorio/VectorShape.h" +#endif + + +void common_hal_displayio_group_construct(displayio_group_t *self, uint32_t scale, mp_int_t x, mp_int_t y) { + mp_obj_list_t *members = mp_obj_new_list(0, NULL); + displayio_group_construct(self, members, scale, x, y); +} + +bool common_hal_displayio_group_get_hidden(displayio_group_t *self) { + return self->hidden; +} + +void common_hal_displayio_group_set_hidden(displayio_group_t *self, bool hidden) { + if (self->hidden == hidden) { + return; + } + self->hidden = hidden; + if (self->hidden_by_parent) { + return; + } + for (size_t i = 0; i < self->members->len; i++) { + mp_obj_t layer; + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + displayio_tilegrid_set_hidden_by_parent(layer, hidden); + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + displayio_group_set_hidden_by_parent(layer, hidden); + continue; + } + } +} + +void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden) { + if (self->hidden_by_parent == hidden) { + return; + } + self->hidden_by_parent = hidden; + // If we're already hidden, then we're done. + if (self->hidden) { + return; + } + for (size_t i = 0; i < self->members->len; i++) { + mp_obj_t layer; + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + displayio_tilegrid_set_hidden_by_parent(layer, hidden); + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + displayio_group_set_hidden_by_parent(layer, hidden); + continue; + } + } +} + +uint32_t common_hal_displayio_group_get_scale(displayio_group_t *self) { + return self->scale; +} + +bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t *area) { + bool first = true; + for (size_t i = 0; i < self->members->len; i++) { + mp_obj_t layer; + displayio_area_t layer_area; + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + if (!displayio_tilegrid_get_previous_area(layer, &layer_area)) { + continue; + } + } else { + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + if (!displayio_group_get_previous_area(layer, &layer_area)) { + continue; + } + } + } + if (first) { + displayio_area_copy(&layer_area, area); + first = false; + } else { + displayio_area_union(area, &layer_area, area); + } + } + if (self->item_removed) { + if (first) { + displayio_area_copy(&self->dirty_area, area); + first = false; + } else { + displayio_area_union(area, &self->dirty_area, area); + } + } + return !first; +} + +static void _update_child_transforms(displayio_group_t *self) { + if (!self->in_group) { + return; + } + for (size_t i = 0; i < self->members->len; i++) { + mp_obj_t layer; + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[i]); + draw_protocol->draw_protocol_impl->draw_update_transform(layer, &self->absolute_transform); + continue; + } + #endif + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + displayio_tilegrid_update_transform(layer, &self->absolute_transform); + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + displayio_group_update_transform(layer, &self->absolute_transform); + continue; + } + } +} + +void displayio_group_update_transform(displayio_group_t *self, + const displayio_buffer_transform_t *parent_transform) { + self->in_group = parent_transform != NULL; + if (self->in_group) { + int16_t x = self->x; + int16_t y = self->y; + if (parent_transform->transpose_xy) { + x = y; + y = self->x; + } + self->absolute_transform.x = parent_transform->x + parent_transform->dx * x; + self->absolute_transform.y = parent_transform->y + parent_transform->dy * y; + self->absolute_transform.dx = parent_transform->dx * self->scale; + self->absolute_transform.dy = parent_transform->dy * self->scale; + self->absolute_transform.transpose_xy = parent_transform->transpose_xy; + self->absolute_transform.mirror_x = parent_transform->mirror_x; + self->absolute_transform.mirror_y = parent_transform->mirror_y; + + self->absolute_transform.scale = parent_transform->scale * self->scale; + } + _update_child_transforms(self); +} + +void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scale) { + if (self->scale == scale) { + return; + } + uint8_t parent_scale = self->absolute_transform.scale / self->scale; + self->absolute_transform.dx = self->absolute_transform.dx / self->scale * scale; + self->absolute_transform.dy = self->absolute_transform.dy / self->scale * scale; + self->absolute_transform.scale = parent_scale * scale; + self->scale = scale; + _update_child_transforms(self); +} + +mp_int_t common_hal_displayio_group_get_x(displayio_group_t *self) { + return self->x; +} + +void common_hal_displayio_group_set_x(displayio_group_t *self, mp_int_t x) { + if (self->x == x) { + return; + } + if (self->absolute_transform.transpose_xy) { + int16_t dy = self->absolute_transform.dy / self->scale; + self->absolute_transform.y += dy * (x - self->x); + } else { + int16_t dx = self->absolute_transform.dx / self->scale; + self->absolute_transform.x += dx * (x - self->x); + } + + self->x = x; + _update_child_transforms(self); +} + +mp_int_t common_hal_displayio_group_get_y(displayio_group_t *self) { + return self->y; +} + +void common_hal_displayio_group_set_y(displayio_group_t *self, mp_int_t y) { + if (self->y == y) { + return; + } + if (self->absolute_transform.transpose_xy) { + int8_t dx = self->absolute_transform.dx / self->scale; + self->absolute_transform.x += dx * (y - self->y); + } else { + int8_t dy = self->absolute_transform.dy / self->scale; + self->absolute_transform.y += dy * (y - self->y); + } + self->y = y; + _update_child_transforms(self); +} + +static void _add_layer(displayio_group_t *self, mp_obj_t layer) { + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, layer); + if (draw_protocol != NULL) { + draw_protocol->draw_protocol_impl->draw_update_transform(draw_protocol->draw_get_protocol_self(layer), &self->absolute_transform); + return; + } + #endif + mp_obj_t native_layer = mp_obj_cast_to_native_base(layer, &displayio_tilegrid_type); + if (native_layer != MP_OBJ_NULL) { + displayio_tilegrid_t *tilegrid = native_layer; + if (tilegrid->in_group) { + mp_raise_ValueError(translate("Layer already in a group.")); + } else { + tilegrid->in_group = true; + } + displayio_tilegrid_update_transform(tilegrid, &self->absolute_transform); + displayio_tilegrid_set_hidden_by_parent( + tilegrid, self->hidden || self->hidden_by_parent); + return; + } + native_layer = mp_obj_cast_to_native_base(layer, &displayio_group_type); + if (native_layer != MP_OBJ_NULL) { + displayio_group_t *group = native_layer; + if (group->in_group) { + mp_raise_ValueError(translate("Layer already in a group.")); + } else { + group->in_group = true; + } + displayio_group_update_transform(group, &self->absolute_transform); + displayio_group_set_hidden_by_parent( + group, self->hidden || self->hidden_by_parent); + return; + } + mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass.")); +} + +static void _remove_layer(displayio_group_t *self, size_t index) { + mp_obj_t layer; + displayio_area_t layer_area; + bool rendered_last_frame = false; + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[index]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[index]); + bool has_dirty_area = draw_protocol->draw_protocol_impl->draw_get_dirty_area(layer, &layer_area); + rendered_last_frame = has_dirty_area; + draw_protocol->draw_protocol_impl->draw_update_transform(layer, NULL); + } + #endif + layer = mp_obj_cast_to_native_base( + self->members->items[index], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + displayio_tilegrid_t *tilegrid = layer; + rendered_last_frame = displayio_tilegrid_get_previous_area(tilegrid, &layer_area); + displayio_tilegrid_update_transform(tilegrid, NULL); + } + layer = mp_obj_cast_to_native_base( + self->members->items[index], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + displayio_group_t *group = layer; + rendered_last_frame = displayio_group_get_previous_area(group, &layer_area); + displayio_group_update_transform(group, NULL); + } + if (!rendered_last_frame) { + return; + } + if (!self->item_removed) { + displayio_area_copy(&layer_area, &self->dirty_area); + } else { + displayio_area_union(&self->dirty_area, &layer_area, &self->dirty_area); + } + self->item_removed = true; +} + +void common_hal_displayio_group_insert(displayio_group_t *self, size_t index, mp_obj_t layer) { + _add_layer(self, layer); + mp_obj_list_insert(self->members, index, layer); +} + +mp_obj_t common_hal_displayio_group_pop(displayio_group_t *self, size_t index) { + _remove_layer(self, index); + return mp_obj_list_pop(self->members, index); +} + +mp_int_t common_hal_displayio_group_index(displayio_group_t *self, mp_obj_t layer) { + mp_obj_t args[] = {self->members, layer}; + mp_obj_t *index = mp_seq_index_obj( + self->members->items, self->members->len, 2, args); + return MP_OBJ_SMALL_INT_VALUE(index); +} + +size_t common_hal_displayio_group_get_len(displayio_group_t *self) { + return self->members->len; +} + +mp_obj_t common_hal_displayio_group_get(displayio_group_t *self, size_t index) { + return self->members->items[index]; +} + +void common_hal_displayio_group_set(displayio_group_t *self, size_t index, mp_obj_t layer) { + _add_layer(self, layer); + _remove_layer(self, index); + mp_obj_list_store(self->members, MP_OBJ_NEW_SMALL_INT(index), layer); +} + +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, uint32_t scale, mp_int_t x, mp_int_t y) { + self->x = x; + self->y = y; + self->members = members; + self->item_removed = false; + self->scale = scale; + self->in_group = false; +} + +bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { + // Track if any of the layers finishes filling in the given area. We can ignore any remaining + // layers at that point. + for (int32_t i = self->members->len - 1; i >= 0; i--) { + mp_obj_t layer; + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[i]); + if (draw_protocol->draw_protocol_impl->draw_fill_area(layer, colorspace, area, mask, buffer)) { + return true; + } + continue; + } + #endif + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + if (displayio_tilegrid_fill_area(layer, colorspace, area, mask, buffer)) { + return true; + } + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + if (displayio_group_fill_area(layer, colorspace, area, mask, buffer)) { + return true; + } + continue; + } + } + return false; +} + +void displayio_group_finish_refresh(displayio_group_t *self) { + self->item_removed = false; + for (int32_t i = self->members->len - 1; i >= 0; i--) { + mp_obj_t layer; + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[i]); + draw_protocol->draw_protocol_impl->draw_finish_refresh(layer); + continue; + } + #endif + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + displayio_tilegrid_finish_refresh(layer); + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + displayio_group_finish_refresh(layer); + continue; + } + } +} + +displayio_area_t *displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t *tail) { + if (self->item_removed) { + self->dirty_area.next = tail; + tail = &self->dirty_area; + } + + for (int32_t i = self->members->len - 1; i >= 0; i--) { + mp_obj_t layer; + #if CIRCUITPY_VECTORIO + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[i]); + tail = draw_protocol->draw_protocol_impl->draw_get_refresh_areas(layer, tail); + continue; + } + #endif + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_tilegrid_type); + if (layer != MP_OBJ_NULL) { + tail = displayio_tilegrid_get_refresh_areas(layer, tail); + continue; + } + layer = mp_obj_cast_to_native_base( + self->members->items[i], &displayio_group_type); + if (layer != MP_OBJ_NULL) { + tail = displayio_group_get_refresh_areas(layer, tail); + continue; + } + } + + return tail; +} diff --git a/circuitpython/shared-module/displayio/Group.h b/circuitpython/shared-module/displayio/Group.h new file mode 100644 index 0000000..f684223 --- /dev/null +++ b/circuitpython/shared-module/displayio/Group.h @@ -0,0 +1,61 @@ +/* + * 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" +#include "py/objlist.h" +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_list_t *members; + displayio_buffer_transform_t absolute_transform; + displayio_area_t dirty_area; // Catch all for changed area + int16_t x; + int16_t y; + uint16_t scale; + bool item_removed : 1; + bool in_group : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + uint8_t padding : 4; +} displayio_group_t; + +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, uint32_t scale, mp_int_t x, mp_int_t y); +void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden); +bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t *area); +bool displayio_group_fill_area(displayio_group_t *group, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); +void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t *parent_transform); +void displayio_group_finish_refresh(displayio_group_t *self); +displayio_area_t *displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t *tail); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_GROUP_H diff --git a/circuitpython/shared-module/displayio/I2CDisplay.c b/circuitpython/shared-module/displayio/I2CDisplay.c new file mode 100644 index 0000000..8fae5d3 --- /dev/null +++ b/circuitpython/shared-module/displayio/I2CDisplay.c @@ -0,0 +1,127 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/I2CDisplay.h" + +#include <stdint.h> +#include <string.h> + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/busio/I2C.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/display_core.h" + +void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t *self, + busio_i2c_obj_t *i2c, uint16_t device_address, const mcu_pin_obj_t *reset) { + + // Reset the display before probing + self->reset.base.type = &mp_type_NoneType; + if (reset != NULL) { + self->reset.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->reset, reset); + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); + common_hal_never_reset_pin(reset); + common_hal_displayio_i2cdisplay_reset(self); + } + + // Probe the bus to see if a device acknowledges the given address. + if (!common_hal_busio_i2c_probe(i2c, device_address)) { + self->base.type = &mp_type_NoneType; + mp_raise_ValueError_varg(translate("Unable to find I2C Display at %x"), device_address); + } + + // Write to the device and return 0 on success or an appropriate error code from mperrno.h + self->bus = i2c; + common_hal_busio_i2c_never_reset(self->bus); + // Our object is statically allocated off the heap so make sure the bus object lives to the end + // of the heap as well. + gc_never_free(self->bus); + + self->address = device_address; +} + +void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t *self) { + if (self->bus == &self->inline_bus) { + common_hal_busio_i2c_deinit(self->bus); + } + + if (self->reset.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_deinit(&self->reset); + } +} + +bool common_hal_displayio_i2cdisplay_reset(mp_obj_t obj) { + displayio_i2cdisplay_obj_t *self = MP_OBJ_TO_PTR(obj); + if (self->reset.base.type == &mp_type_NoneType) { + return false; + } + + common_hal_digitalio_digitalinout_set_value(&self->reset, false); + common_hal_mcu_delay_us(4); + common_hal_digitalio_digitalinout_set_value(&self->reset, true); + return true; +} + +bool common_hal_displayio_i2cdisplay_bus_free(mp_obj_t obj) { + displayio_i2cdisplay_obj_t *self = MP_OBJ_TO_PTR(obj); + if (!common_hal_busio_i2c_try_lock(self->bus)) { + return false; + } + common_hal_busio_i2c_unlock(self->bus); + return true; +} + +bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t obj) { + displayio_i2cdisplay_obj_t *self = MP_OBJ_TO_PTR(obj); + return common_hal_busio_i2c_try_lock(self->bus); +} + +void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, display_byte_type_t data_type, + display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) { + displayio_i2cdisplay_obj_t *self = MP_OBJ_TO_PTR(obj); + if (data_type == DISPLAY_COMMAND) { + uint8_t command_bytes[2 * data_length]; + for (uint32_t i = 0; i < data_length; i++) { + command_bytes[2 * i] = 0x80; + command_bytes[2 * i + 1] = data[i]; + } + common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length); + } else { + uint8_t data_bytes[data_length + 1]; + data_bytes[0] = 0x40; + memcpy(data_bytes + 1, data, data_length); + common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1); + } +} + +void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t obj) { + displayio_i2cdisplay_obj_t *self = MP_OBJ_TO_PTR(obj); + common_hal_busio_i2c_unlock(self->bus); +} diff --git a/circuitpython/shared-module/displayio/I2CDisplay.h b/circuitpython/shared-module/displayio/I2CDisplay.h new file mode 100644 index 0000000..cc5bcf1 --- /dev/null +++ b/circuitpython/shared-module/displayio/I2CDisplay.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H + +#include "common-hal/busio/I2C.h" +#include "common-hal/digitalio/DigitalInOut.h" + +typedef struct { + mp_obj_base_t base; + busio_i2c_obj_t *bus; + busio_i2c_obj_t inline_bus; + digitalio_digitalinout_obj_t reset; + uint16_t address; +} displayio_i2cdisplay_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H diff --git a/circuitpython/shared-module/displayio/OnDiskBitmap.c b/circuitpython/shared-module/displayio/OnDiskBitmap.c new file mode 100644 index 0000000..dd2731a --- /dev/null +++ b/circuitpython/shared-module/displayio/OnDiskBitmap.c @@ -0,0 +1,208 @@ +/* + * 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/OnDiskBitmap.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-module/displayio/ColorConverter.h" +#include "shared-module/displayio/Palette.h" + +#include <string.h> + +#include "py/mperrno.h" +#include "py/runtime.h" + +static uint32_t read_word(uint16_t *bmp_header, uint16_t index) { + return bmp_header[index] | bmp_header[index + 1] << 16; +} + +void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, pyb_file_obj_t *file) { + // Load the wave + self->file = file; + uint16_t bmp_header[69]; + f_rewind(&self->file->fp); + UINT bytes_read; + if (f_read(&self->file->fp, bmp_header, 138, &bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } + if (bytes_read != 138 || + memcmp(bmp_header, "BM", 2) != 0) { + mp_raise_ValueError(translate("Invalid BMP file")); + } + + // We can't cast because we're not aligned. + self->data_offset = read_word(bmp_header, 5); + + uint32_t header_size = read_word(bmp_header, 7); + uint16_t bits_per_pixel = bmp_header[14]; + uint32_t compression = read_word(bmp_header, 15); + uint32_t number_of_colors = read_word(bmp_header, 23); + + bool indexed = bits_per_pixel <= 8; + self->bitfield_compressed = (compression == 3); + self->bits_per_pixel = bits_per_pixel; + self->width = read_word(bmp_header, 9); + self->height = read_word(bmp_header, 11); + + displayio_colorconverter_t *colorconverter = m_new_obj(displayio_colorconverter_t); + colorconverter->base.type = &displayio_colorconverter_type; + common_hal_displayio_colorconverter_construct(colorconverter, false, DISPLAYIO_COLORSPACE_RGB888); + self->colorconverter = colorconverter; + + if (bits_per_pixel == 16) { + if (((header_size >= 56)) || (self->bitfield_compressed)) { + self->r_bitmask = read_word(bmp_header, 27); + self->g_bitmask = read_word(bmp_header, 29); + self->b_bitmask = read_word(bmp_header, 31); + + } else { // no compression or short header means 5:5:5 + self->r_bitmask = 0x7c00; + self->g_bitmask = 0x3e0; + self->b_bitmask = 0x1f; + } + } else if (indexed) { + if (number_of_colors == 0) { + number_of_colors = 1 << bits_per_pixel; + } + + displayio_palette_t *palette = m_new_obj(displayio_palette_t); + palette->base.type = &displayio_palette_type; + common_hal_displayio_palette_construct(palette, number_of_colors); + + if (number_of_colors > 1) { + uint16_t palette_size = number_of_colors * sizeof(uint32_t); + uint16_t palette_offset = 0xe + header_size; + + uint32_t *palette_data = m_malloc(palette_size, false); + + f_rewind(&self->file->fp); + f_lseek(&self->file->fp, palette_offset); + + UINT palette_bytes_read; + if (f_read(&self->file->fp, palette_data, palette_size, &palette_bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } + if (palette_bytes_read != palette_size) { + mp_raise_ValueError(translate("Unable to read color palette data")); + } + for (uint16_t i = 0; i < number_of_colors; i++) { + common_hal_displayio_palette_set_color(palette, i, palette_data[i]); + } + m_free(palette_data); + } else { + common_hal_displayio_palette_set_color(palette, 0, 0x0); + common_hal_displayio_palette_set_color(palette, 1, 0xffffff); + } + self->palette = palette; + + } else if (!(header_size == 12 || header_size == 40 || header_size == 108 || header_size == 124)) { + mp_raise_ValueError_varg(translate("Only Windows format, uncompressed BMP supported: given header size is %d"), header_size); + } + + if (bits_per_pixel == 8 && number_of_colors == 0) { + mp_raise_ValueError_varg(translate("Only monochrome, indexed 4bpp or 8bpp, and 16bpp or greater BMPs supported: %d bpp given"), bits_per_pixel); + } + + uint8_t bytes_per_pixel = (self->bits_per_pixel / 8) ? (self->bits_per_pixel / 8) : 1; + uint8_t pixels_per_byte = 8 / self->bits_per_pixel; + if (pixels_per_byte == 0) { + self->stride = (self->width * bytes_per_pixel); + // Rows are word aligned. + if (self->stride % 4 != 0) { + self->stride += 4 - self->stride % 4; + } + } else { + uint32_t bit_stride = self->width * self->bits_per_pixel; + if (bit_stride % 32 != 0) { + bit_stride += 32 - bit_stride % 32; + } + self->stride = (bit_stride / 8); + } + +} + + +uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *self, + int16_t x, int16_t y) { + if (x < 0 || x >= self->width || y < 0 || y >= self->height) { + return 0; + } + + uint32_t location; + uint8_t bytes_per_pixel = (self->bits_per_pixel / 8) ? (self->bits_per_pixel / 8) : 1; + uint8_t pixels_per_byte = 8 / self->bits_per_pixel; + if (pixels_per_byte == 0) { + location = self->data_offset + (self->height - y - 1) * self->stride + x * bytes_per_pixel; + } else { + location = self->data_offset + (self->height - y - 1) * self->stride + x / pixels_per_byte; + } + // We don't cache here because the underlying FS caches sectors. + f_lseek(&self->file->fp, location); + UINT bytes_read; + uint32_t pixel_data = 0; + uint32_t result = f_read(&self->file->fp, &pixel_data, bytes_per_pixel, &bytes_read); + if (result == FR_OK) { + uint32_t tmp = 0; + uint8_t red; + uint8_t green; + uint8_t blue; + if (bytes_per_pixel == 1) { + uint8_t offset = (x % pixels_per_byte) * self->bits_per_pixel; + uint8_t mask = (1 << self->bits_per_pixel) - 1; + + return (pixel_data >> ((8 - self->bits_per_pixel) - offset)) & mask; + } else if (bytes_per_pixel == 2) { + if (self->g_bitmask == 0x07e0) { // 565 + red = ((pixel_data & self->r_bitmask) >> 11); + green = ((pixel_data & self->g_bitmask) >> 5); + blue = ((pixel_data & self->b_bitmask) >> 0); + } else { // 555 + red = ((pixel_data & self->r_bitmask) >> 10); + green = ((pixel_data & self->g_bitmask) >> 4); + blue = ((pixel_data & self->b_bitmask) >> 0); + } + tmp = (red << 19 | green << 10 | blue << 3); + return tmp; + } else if ((bytes_per_pixel == 4) && (self->bitfield_compressed)) { + return pixel_data & 0x00FFFFFF; + } else { + return pixel_data; + } + } + return 0; +} + +uint16_t common_hal_displayio_ondiskbitmap_get_height(displayio_ondiskbitmap_t *self) { + return self->height; +} + +uint16_t common_hal_displayio_ondiskbitmap_get_width(displayio_ondiskbitmap_t *self) { + return self->width; +} + +mp_obj_t common_hal_displayio_ondiskbitmap_get_pixel_shader(displayio_ondiskbitmap_t *self) { + return MP_OBJ_FROM_PTR(self->pixel_shader_base); +} diff --git a/circuitpython/shared-module/displayio/OnDiskBitmap.h b/circuitpython/shared-module/displayio/OnDiskBitmap.h new file mode 100644 index 0000000..806b13f --- /dev/null +++ b/circuitpython/shared-module/displayio/OnDiskBitmap.h @@ -0,0 +1,56 @@ +/* + * 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_ONDISKBITMAP_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" + +#include "extmod/vfs_fat.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint16_t data_offset; + uint16_t stride; + uint32_t r_bitmask; + uint32_t g_bitmask; + uint32_t b_bitmask; + pyb_file_obj_t *file; + union { + mp_obj_base_t *pixel_shader_base; + struct displayio_palette *palette; + struct displayio_colorconverter *colorconverter; + }; + bool bitfield_compressed; + uint8_t bits_per_pixel; +} displayio_ondiskbitmap_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H diff --git a/circuitpython/shared-module/displayio/Palette.c b/circuitpython/shared-module/displayio/Palette.c new file mode 100644 index 0000000..1bd168b --- /dev/null +++ b/circuitpython/shared-module/displayio/Palette.c @@ -0,0 +1,114 @@ +/* + * 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" + +#include "shared-module/displayio/ColorConverter.h" + +void common_hal_displayio_palette_construct(displayio_palette_t *self, uint16_t color_count) { + self->color_count = color_count; + self->colors = (_displayio_color_t *)m_malloc(color_count * sizeof(_displayio_color_t), false); +} + +void common_hal_displayio_palette_make_opaque(displayio_palette_t *self, uint32_t palette_index) { + self->colors[palette_index].transparent = false; + self->needs_refresh = true; +} + +void common_hal_displayio_palette_make_transparent(displayio_palette_t *self, uint32_t palette_index) { + self->colors[palette_index].transparent = true; + self->needs_refresh = true; +} + +bool common_hal_displayio_palette_is_transparent(displayio_palette_t *self, uint32_t palette_index) { + return self->colors[palette_index].transparent; +} + +uint32_t common_hal_displayio_palette_get_len(displayio_palette_t *self) { + return self->color_count; +} + +void common_hal_displayio_palette_set_color(displayio_palette_t *self, uint32_t palette_index, uint32_t color) { + if (self->colors[palette_index].rgb888 == color) { + return; + } + self->colors[palette_index].rgb888 = color; + self->colors[palette_index].luma = displayio_colorconverter_compute_luma(color); + self->colors[palette_index].rgb565 = displayio_colorconverter_compute_rgb565(color); + + uint8_t chroma = displayio_colorconverter_compute_chroma(color); + self->colors[palette_index].chroma = chroma; + self->colors[palette_index].hue = displayio_colorconverter_compute_hue(color); + self->needs_refresh = true; +} + +uint32_t common_hal_displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index) { + return self->colors[palette_index].rgb888; +} + +bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_colorspace_t *colorspace, uint32_t palette_index, uint32_t *color) { + if (palette_index > self->color_count || self->colors[palette_index].transparent) { + return false; // returns transparent + } + + if (colorspace->tricolor) { + uint8_t luma = self->colors[palette_index].luma; + *color = luma >> (8 - colorspace->depth); + // Chroma 0 means the color is a gray and has no hue so never color based on it. + if (self->colors[palette_index].chroma <= 16) { + if (!colorspace->grayscale) { + *color = 0; + } + return true; + } + uint8_t pixel_hue = self->colors[palette_index].hue; + displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, color); + } else if (colorspace->grayscale) { + size_t bitmask = (1 << colorspace->depth) - 1; + *color = (self->colors[palette_index].luma >> colorspace->grayscale_bit) & bitmask; + } else if (colorspace->depth == 16) { + uint16_t packed = self->colors[palette_index].rgb565; + if (colorspace->reverse_bytes_in_word) { + // swap bytes + packed = __builtin_bswap16(packed); + } + *color = packed; + } else if (colorspace->depth == 32) { + *color = self->colors[palette_index].rgb888; + } else { + return false; + } + + 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/circuitpython/shared-module/displayio/Palette.h b/circuitpython/shared-module/displayio/Palette.h new file mode 100644 index 0000000..49f03b5 --- /dev/null +++ b/circuitpython/shared-module/displayio/Palette.h @@ -0,0 +1,85 @@ +/* + * 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 { + uint8_t depth; + uint8_t bytes_per_cell; + uint8_t tricolor_hue; + uint8_t tricolor_luma; + uint8_t grayscale_bit; // The lowest grayscale bit. Normally 8 - depth. + bool grayscale; + bool tricolor; + bool pixels_in_byte_share_row; + bool reverse_pixels_in_byte; + bool reverse_bytes_in_word; + bool dither; +} _displayio_colorspace_t; + +typedef struct { + uint32_t rgb888; + uint16_t rgb565; + uint8_t luma; + uint8_t hue; + uint8_t chroma; + bool transparent; // This may have additional bits added later for blending. +} _displayio_color_t; + +typedef struct { + uint32_t pixel; + uint16_t x; + uint16_t y; + uint8_t tile; + uint16_t tile_x; + uint16_t tile_y; +} displayio_input_pixel_t; + +typedef struct { + uint32_t pixel; + bool opaque; +} displayio_output_pixel_t; + +typedef struct displayio_palette { + mp_obj_base_t base; + _displayio_color_t *colors; + uint32_t color_count; + bool needs_refresh; +} displayio_palette_t; + +// Returns false if color fetch did not succeed (out of range or transparent). +// Returns true if color is opaque, and sets color. +bool displayio_palette_get_color(displayio_palette_t *palette, const _displayio_colorspace_t *colorspace, uint32_t palette_index, uint32_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/circuitpython/shared-module/displayio/Shape.c b/circuitpython/shared-module/displayio/Shape.c new file mode 100644 index 0000000..4a32c7a --- /dev/null +++ b/circuitpython/shared-module/displayio/Shape.c @@ -0,0 +1,144 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/Shape.h" + +#include <string.h> + +#include "py/runtime.h" +#include "py/misc.h" + +void common_hal_displayio_shape_construct(displayio_shape_t *self, uint32_t width, + uint32_t height, bool mirror_x, bool mirror_y) { + self->mirror_x = mirror_x; + self->mirror_y = mirror_y; + self->width = width; + if (self->mirror_x) { + width /= 2; + width += self->width % 2; + } + self->half_width = width; + + self->height = height; + if (self->mirror_y) { + height /= 2; + height += self->height % 2; + } + self->half_height = height; + + self->data = m_malloc(height * sizeof(uint32_t), false); + + for (uint16_t i = 0; i < height; i++) { + self->data[2 * i] = 0; + self->data[2 * i + 1] = width; + } + + self->dirty_area.x1 = 0; + self->dirty_area.x2 = width; + self->dirty_area.y1 = 0; + self->dirty_area.y2 = height; +} + +void common_hal_displayio_shape_set_boundary(displayio_shape_t *self, uint16_t y, uint16_t start_x, uint16_t end_x) { + if (y < 0 || y >= self->height || (self->mirror_y && y >= self->half_height)) { + mp_raise_ValueError(translate("y value out of bounds")); + } + if (start_x < 0 || start_x >= self->width || end_x < 0 || end_x >= self->width) { + mp_raise_ValueError(translate("x value out of bounds")); + } + if (self->mirror_x && (start_x >= self->half_width || end_x >= self->half_width)) { + mp_raise_ValueError_varg(translate("Maximum x value when mirrored is %d"), self->half_width); + } + + uint16_t lower_x, upper_x, lower_y, upper_y; + + // find x-boundaries for updating based on current data and start_x, end_x, and mirror_x + lower_x = MIN(start_x, self->data[2 * y]); + + if (self->mirror_x) { + upper_x = self->width - lower_x + 1; // dirty rectangles are treated with max value exclusive + } else { + upper_x = MAX(end_x, self->data[2 * y + 1]) + 1; // dirty rectangles are treated with max value exclusive + } + + // find y-boundaries based on y and mirror_y + lower_y = y; + + if (self->mirror_y) { + upper_y = self->height - lower_y + 1; // dirty rectangles are treated with max value exclusive + } else { + upper_y = y + 1; // dirty rectangles are treated with max value exclusive + } + + self->data[2 * y] = start_x; // update the data array with the new boundaries + self->data[2 * y + 1] = end_x; + + if (self->dirty_area.x1 == self->dirty_area.x2) { // Dirty region is empty + self->dirty_area.x1 = lower_x; + self->dirty_area.x2 = upper_x; + self->dirty_area.y1 = lower_y; + self->dirty_area.y2 = upper_y; + + } else { // Dirty region is not empty + self->dirty_area.x1 = MIN(lower_x, self->dirty_area.x1); + self->dirty_area.x2 = MAX(upper_x, self->dirty_area.x2); + + self->dirty_area.y1 = MIN(lower_y, self->dirty_area.y1); + self->dirty_area.y2 = MAX(upper_y, self->dirty_area.y2); + } +} + +uint32_t common_hal_displayio_shape_get_pixel(void *obj, int16_t x, int16_t y) { + displayio_shape_t *self = obj; + if (x >= self->width || x < 0 || y >= self->height || y < 0) { + return 0; + } + if (self->mirror_x && x >= self->half_width) { + x = self->width - x - 1; + } + if (self->mirror_y && y >= self->half_height) { + y = self->height - y - 1; + } + uint16_t start_x = self->data[2 * y]; + uint16_t end_x = self->data[2 * y + 1]; + if (x < start_x || x > end_x) { + return 0; + } + return 1; +} + +displayio_area_t *displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t *tail) { + if (self->dirty_area.x1 == self->dirty_area.x2) { + return tail; + } + self->dirty_area.next = tail; + return &self->dirty_area; +} + +void displayio_shape_finish_refresh(displayio_shape_t *self) { + self->dirty_area.x1 = 0; + self->dirty_area.x2 = 0; +} diff --git a/circuitpython/shared-module/displayio/Shape.h b/circuitpython/shared-module/displayio/Shape.h new file mode 100644 index 0000000..2cac5d7 --- /dev/null +++ b/circuitpython/shared-module/displayio/Shape.h @@ -0,0 +1,51 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" +#include "shared-module/displayio/area.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint16_t half_width; + uint16_t half_height; + uint16_t *data; + bool mirror_x; + bool mirror_y; + displayio_area_t dirty_area; +} displayio_shape_t; + +void displayio_shape_finish_refresh(displayio_shape_t *self); +displayio_area_t *displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t *tail); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H diff --git a/circuitpython/shared-module/displayio/TileGrid.c b/circuitpython/shared-module/displayio/TileGrid.c new file mode 100644 index 0000000..5c968c3 --- /dev/null +++ b/circuitpython/shared-module/displayio/TileGrid.c @@ -0,0 +1,652 @@ +/* + * 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/TileGrid.h" + +#include "py/runtime.h" +#include "shared-bindings/displayio/Bitmap.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/OnDiskBitmap.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/displayio/Shape.h" + +void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap, + uint16_t bitmap_width_in_tiles, uint16_t bitmap_height_in_tiles, + mp_obj_t pixel_shader, uint16_t width, uint16_t height, + uint16_t tile_width, uint16_t tile_height, uint16_t x, uint16_t y, uint8_t default_tile) { + uint32_t total_tiles = width * height; + // Sprites will only have one tile so save a little memory by inlining values in the pointer. + uint8_t inline_tiles = sizeof(uint8_t *); + if (total_tiles <= inline_tiles) { + self->tiles = 0; + // Pack values into the pointer since there are only a few. + for (uint32_t i = 0; i < inline_tiles; i++) { + ((uint8_t *)&self->tiles)[i] = default_tile; + } + self->inline_tiles = true; + } else { + self->tiles = (uint8_t *)m_malloc(total_tiles, false); + for (uint32_t i = 0; i < total_tiles; i++) { + self->tiles[i] = default_tile; + } + self->inline_tiles = false; + } + self->bitmap_width_in_tiles = bitmap_width_in_tiles; + self->tiles_in_bitmap = bitmap_width_in_tiles * bitmap_height_in_tiles; + self->width_in_tiles = width; + self->height_in_tiles = height; + self->x = x; + self->y = y; + self->pixel_width = width * tile_width; + self->pixel_height = height * tile_height; + self->tile_width = tile_width; + self->tile_height = tile_height; + self->bitmap = bitmap; + self->pixel_shader = pixel_shader; + self->in_group = false; + self->hidden = false; + self->hidden_by_parent = false; + self->previous_area.x1 = 0xffff; + self->previous_area.x2 = self->previous_area.x1; + self->flip_x = false; + self->flip_y = false; + self->transpose_xy = false; + self->absolute_transform = NULL; +} + + +bool common_hal_displayio_tilegrid_get_hidden(displayio_tilegrid_t *self) { + return self->hidden; +} + +void common_hal_displayio_tilegrid_set_hidden(displayio_tilegrid_t *self, bool hidden) { + self->hidden = hidden; + if (!hidden) { + self->full_change = true; + } +} + +void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden) { + self->hidden_by_parent = hidden; + if (!hidden) { + self->full_change = true; + } +} + +bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t *area) { + if (self->previous_area.x1 == self->previous_area.x2) { + return false; + } + displayio_area_copy(&self->previous_area, area); + return true; +} + +STATIC void _update_current_x(displayio_tilegrid_t *self) { + uint16_t width; + if (self->transpose_xy) { + width = self->pixel_height; + } else { + width = self->pixel_width; + } + + // If there's no transform, substitute an identity transform so the calculations will work. + const displayio_buffer_transform_t *absolute_transform = + self->absolute_transform == NULL + ? &null_transform + : self->absolute_transform; + + if (absolute_transform->transpose_xy) { + self->current_area.y1 = absolute_transform->y + absolute_transform->dy * self->x; + self->current_area.y2 = absolute_transform->y + absolute_transform->dy * (self->x + width); + if (self->current_area.y2 < self->current_area.y1) { + int16_t temp = self->current_area.y2; + self->current_area.y2 = self->current_area.y1; + self->current_area.y1 = temp; + } + } else { + self->current_area.x1 = absolute_transform->x + absolute_transform->dx * self->x; + self->current_area.x2 = absolute_transform->x + absolute_transform->dx * (self->x + width); + if (self->current_area.x2 < self->current_area.x1) { + int16_t temp = self->current_area.x2; + self->current_area.x2 = self->current_area.x1; + self->current_area.x1 = temp; + } + } +} + +STATIC void _update_current_y(displayio_tilegrid_t *self) { + uint16_t height; + if (self->transpose_xy) { + height = self->pixel_width; + } else { + height = self->pixel_height; + } + + // If there's no transform, substitute an identity transform so the calculations will work. + const displayio_buffer_transform_t *absolute_transform = + self->absolute_transform == NULL + ? &null_transform + : self->absolute_transform; + + if (absolute_transform->transpose_xy) { + self->current_area.x1 = absolute_transform->x + absolute_transform->dx * self->y; + self->current_area.x2 = absolute_transform->x + absolute_transform->dx * (self->y + height); + if (self->current_area.x2 < self->current_area.x1) { + int16_t temp = self->current_area.x2; + self->current_area.x2 = self->current_area.x1; + self->current_area.x1 = temp; + } + } else { + self->current_area.y1 = absolute_transform->y + absolute_transform->dy * self->y; + self->current_area.y2 = absolute_transform->y + absolute_transform->dy * (self->y + height); + if (self->current_area.y2 < self->current_area.y1) { + int16_t temp = self->current_area.y2; + self->current_area.y2 = self->current_area.y1; + self->current_area.y1 = temp; + } + } +} + +void displayio_tilegrid_update_transform(displayio_tilegrid_t *self, + const displayio_buffer_transform_t *absolute_transform) { + self->in_group = absolute_transform != NULL; + self->absolute_transform = absolute_transform; + if (absolute_transform != NULL) { + self->moved = true; + + _update_current_x(self); + _update_current_y(self); + } +} + +mp_int_t common_hal_displayio_tilegrid_get_x(displayio_tilegrid_t *self) { + return self->x; +} +void common_hal_displayio_tilegrid_set_x(displayio_tilegrid_t *self, mp_int_t x) { + if (self->x == x) { + return; + } + + self->moved = true; + + self->x = x; + if (self->absolute_transform != NULL) { + _update_current_x(self); + } +} +mp_int_t common_hal_displayio_tilegrid_get_y(displayio_tilegrid_t *self) { + return self->y; +} + +void common_hal_displayio_tilegrid_set_y(displayio_tilegrid_t *self, mp_int_t y) { + if (self->y == y) { + return; + } + self->moved = true; + self->y = y; + if (self->absolute_transform != NULL) { + _update_current_y(self); + } +} + +mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self) { + return self->pixel_shader; +} + +void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader) { + self->pixel_shader = pixel_shader; + self->full_change = true; +} + +mp_obj_t common_hal_displayio_tilegrid_get_bitmap(displayio_tilegrid_t *self) { + return self->bitmap; +} + +void common_hal_displayio_tilegrid_set_bitmap(displayio_tilegrid_t *self, mp_obj_t bitmap) { + self->bitmap = bitmap; + self->full_change = true; +} + +uint16_t common_hal_displayio_tilegrid_get_width(displayio_tilegrid_t *self) { + return self->width_in_tiles; +} + +uint16_t common_hal_displayio_tilegrid_get_height(displayio_tilegrid_t *self) { + return self->height_in_tiles; +} + +uint16_t common_hal_displayio_tilegrid_get_tile_width(displayio_tilegrid_t *self) { + return self->tile_width; +} + +uint16_t common_hal_displayio_tilegrid_get_tile_height(displayio_tilegrid_t *self) { + return self->tile_height; +} + +uint8_t common_hal_displayio_tilegrid_get_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + uint8_t *tiles = self->tiles; + if (self->inline_tiles) { + tiles = (uint8_t *)&self->tiles; + } + if (tiles == NULL) { + return 0; + } + return tiles[y * self->width_in_tiles + x]; +} + +void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint8_t tile_index) { + if (tile_index >= self->tiles_in_bitmap) { + mp_raise_ValueError(translate("Tile index out of bounds")); + } + uint8_t *tiles = self->tiles; + if (self->inline_tiles) { + tiles = (uint8_t *)&self->tiles; + } + if (tiles == NULL) { + return; + } + tiles[y * self->width_in_tiles + x] = tile_index; + displayio_area_t temp_area; + displayio_area_t *tile_area; + if (!self->partial_change) { + tile_area = &self->dirty_area; + } else { + tile_area = &temp_area; + } + int16_t tx = (x - self->top_left_x) % self->width_in_tiles; + if (tx < 0) { + tx += self->width_in_tiles; + } + tile_area->x1 = tx * self->tile_width; + tile_area->x2 = tile_area->x1 + self->tile_width; + int16_t ty = (y - self->top_left_y) % self->height_in_tiles; + if (ty < 0) { + ty += self->height_in_tiles; + } + tile_area->y1 = ty * self->tile_height; + tile_area->y2 = tile_area->y1 + self->tile_height; + + if (self->partial_change) { + displayio_area_union(&self->dirty_area, &temp_area, &self->dirty_area); + } + + self->partial_change = true; +} + +bool common_hal_displayio_tilegrid_get_flip_x(displayio_tilegrid_t *self) { + return self->flip_x; +} + +void common_hal_displayio_tilegrid_set_flip_x(displayio_tilegrid_t *self, bool flip_x) { + if (self->flip_x == flip_x) { + return; + } + self->flip_x = flip_x; + self->full_change = true; +} + +bool common_hal_displayio_tilegrid_get_flip_y(displayio_tilegrid_t *self) { + return self->flip_y; +} + +void common_hal_displayio_tilegrid_set_flip_y(displayio_tilegrid_t *self, bool flip_y) { + if (self->flip_y == flip_y) { + return; + } + self->flip_y = flip_y; + self->full_change = true; +} + +bool common_hal_displayio_tilegrid_get_transpose_xy(displayio_tilegrid_t *self) { + return self->transpose_xy; +} + +void common_hal_displayio_tilegrid_set_transpose_xy(displayio_tilegrid_t *self, bool transpose_xy) { + if (self->transpose_xy == transpose_xy) { + return; + } + self->transpose_xy = transpose_xy; + + // Square TileGrids do not change dimensions when transposed. + if (self->pixel_width == self->pixel_height) { + self->full_change = true; + return; + } + + _update_current_x(self); + _update_current_y(self); + + self->moved = true; +} + +void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + self->top_left_x = x; + self->top_left_y = y; + self->full_change = true; +} + +bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, + const _displayio_colorspace_t *colorspace, const displayio_area_t *area, + uint32_t *mask, uint32_t *buffer) { + // If no tiles are present we have no impact. + uint8_t *tiles = self->tiles; + if (self->inline_tiles) { + tiles = (uint8_t *)&self->tiles; + } + if (tiles == NULL) { + return false; + } + + bool hidden = self->hidden || self->hidden_by_parent; + if (hidden) { + return false; + } + + displayio_area_t overlap; + if (!displayio_area_compute_overlap(area, &self->current_area, &overlap)) { + return false; + } + + int16_t x_stride = 1; + int16_t y_stride = displayio_area_width(area); + + bool flip_x = self->flip_x; + bool flip_y = self->flip_y; + if (self->transpose_xy != self->absolute_transform->transpose_xy) { + bool temp_flip = flip_x; + flip_x = flip_y; + flip_y = temp_flip; + } + + // How many pixels are outside of our area between us and the start of the row. + uint16_t start = 0; + if ((self->absolute_transform->dx < 0) != flip_x) { + start += (area->x2 - area->x1 - 1) * x_stride; + x_stride *= -1; + } + if ((self->absolute_transform->dy < 0) != flip_y) { + start += (area->y2 - area->y1 - 1) * y_stride; + y_stride *= -1; + } + + // Track if this layer finishes filling in the given area. We can ignore any remaining + // layers at that point. + bool full_coverage = displayio_area_equal(area, &overlap); + + // TODO(tannewt): Skip coverage tracking if all pixels outside the overlap have already been + // set and our palette is all opaque. + + // TODO(tannewt): Check to see if the pixel_shader has any transparency. If it doesn't then we + // can either return full coverage or bulk update the mask. + displayio_area_t transformed; + displayio_area_transform_within(flip_x != (self->absolute_transform->dx < 0), flip_y != (self->absolute_transform->dy < 0), self->transpose_xy != self->absolute_transform->transpose_xy, + &overlap, + &self->current_area, + &transformed); + + int16_t start_x = (transformed.x1 - self->current_area.x1); + int16_t end_x = (transformed.x2 - self->current_area.x1); + int16_t start_y = (transformed.y1 - self->current_area.y1); + int16_t end_y = (transformed.y2 - self->current_area.y1); + + int16_t y_shift = 0; + int16_t x_shift = 0; + if ((self->absolute_transform->dx < 0) != flip_x) { + x_shift = area->x2 - overlap.x2; + } else { + x_shift = overlap.x1 - area->x1; + } + if ((self->absolute_transform->dy < 0) != flip_y) { + y_shift = area->y2 - overlap.y2; + } else { + y_shift = overlap.y1 - area->y1; + } + + // This untransposes x and y so it aligns with bitmap rows. + if (self->transpose_xy != self->absolute_transform->transpose_xy) { + int16_t temp_stride = x_stride; + x_stride = y_stride; + y_stride = temp_stride; + int16_t temp_shift = x_shift; + x_shift = y_shift; + y_shift = temp_shift; + } + + uint8_t pixels_per_byte = 8 / colorspace->depth; + + displayio_input_pixel_t input_pixel; + displayio_output_pixel_t output_pixel; + + for (input_pixel.y = start_y; input_pixel.y < end_y; ++input_pixel.y) { + int16_t row_start = start + (input_pixel.y - start_y + y_shift) * y_stride; // in pixels + int16_t local_y = input_pixel.y / self->absolute_transform->scale; + for (input_pixel.x = start_x; input_pixel.x < end_x; ++input_pixel.x) { + // Compute the destination pixel in the buffer and mask based on the transformations. + int16_t offset = row_start + (input_pixel.x - start_x + x_shift) * x_stride; // in pixels + + // This is super useful for debugging out of range accesses. Uncomment to use. + // if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) { + // asm("bkpt"); + // } + + // Check the mask first to see if the pixel has already been set. + if ((mask[offset / 32] & (1 << (offset % 32))) != 0) { + continue; + } + int16_t local_x = input_pixel.x / self->absolute_transform->scale; + uint16_t tile_location = ((local_y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles; + input_pixel.tile = tiles[tile_location]; + input_pixel.tile_x = (input_pixel.tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width; + input_pixel.tile_y = (input_pixel.tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height; + + output_pixel.pixel = 0; + input_pixel.pixel = 0; + + // We always want to read bitmap pixels by row first and then transpose into the destination + // buffer because most bitmaps are row associated. + if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { + input_pixel.pixel = common_hal_displayio_bitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); + } else if (mp_obj_is_type(self->bitmap, &displayio_shape_type)) { + input_pixel.pixel = common_hal_displayio_shape_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); + } else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + input_pixel.pixel = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); + } + + output_pixel.opaque = true; + if (self->pixel_shader == mp_const_none) { + output_pixel.pixel = input_pixel.pixel; + } else if (mp_obj_is_type(self->pixel_shader, &displayio_palette_type)) { + output_pixel.opaque = displayio_palette_get_color(self->pixel_shader, colorspace, input_pixel.pixel, &output_pixel.pixel); + } else if (mp_obj_is_type(self->pixel_shader, &displayio_colorconverter_type)) { + displayio_colorconverter_convert(self->pixel_shader, colorspace, &input_pixel, &output_pixel); + } + if (!output_pixel.opaque) { + // A pixel is transparent so we haven't fully covered the area ourselves. + full_coverage = false; + } else { + mask[offset / 32] |= 1 << (offset % 32); + if (colorspace->depth == 16) { + *(((uint16_t *)buffer) + offset) = output_pixel.pixel; + } else if (colorspace->depth == 32) { + *(((uint32_t *)buffer) + offset) = output_pixel.pixel; + } else if (colorspace->depth == 8) { + *(((uint8_t *)buffer) + offset) = output_pixel.pixel; + } else if (colorspace->depth < 8) { + // Reorder the offsets to pack multiple rows into a byte (meaning they share a column). + if (!colorspace->pixels_in_byte_share_row) { + uint16_t width = displayio_area_width(area); + uint16_t row = offset / width; + uint16_t col = offset % width; + // Dividing by pixels_per_byte does truncated division even if we multiply it back out. + offset = col * pixels_per_byte + (row / pixels_per_byte) * pixels_per_byte * width + row % pixels_per_byte; + // Also useful for validating that the bitpacking worked correctly. + // if (offset > displayio_area_size(area)) { + // asm("bkpt"); + // } + } + uint8_t shift = (offset % pixels_per_byte) * colorspace->depth; + if (colorspace->reverse_pixels_in_byte) { + // Reverse the shift by subtracting it from the leftmost shift. + shift = (pixels_per_byte - 1) * colorspace->depth - shift; + } + ((uint8_t *)buffer)[offset / pixels_per_byte] |= output_pixel.pixel << shift; + } + } + (void)input_pixel; + } + } + return full_coverage; +} + +void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) { + bool first_draw = self->previous_area.x1 == self->previous_area.x2; + bool hidden = self->hidden || self->hidden_by_parent; + if (!first_draw && hidden) { + self->previous_area.x2 = self->previous_area.x1; + } else if (self->moved || first_draw) { + displayio_area_copy(&self->current_area, &self->previous_area); + } + + self->moved = false; + self->full_change = false; + self->partial_change = false; + if (mp_obj_is_type(self->pixel_shader, &displayio_palette_type)) { + displayio_palette_finish_refresh(self->pixel_shader); + } else if (mp_obj_is_type(self->pixel_shader, &displayio_colorconverter_type)) { + displayio_colorconverter_finish_refresh(self->pixel_shader); + } + if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { + displayio_bitmap_finish_refresh(self->bitmap); + } else if (mp_obj_is_type(self->bitmap, &displayio_shape_type)) { + displayio_shape_finish_refresh(self->bitmap); + } else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + // OnDiskBitmap changes will trigger a complete reload so no need to + // track changes. + } + // 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. +} + +displayio_area_t *displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t *tail) { + bool first_draw = self->previous_area.x1 == self->previous_area.x2; + bool hidden = self->hidden || self->hidden_by_parent; + // Check hidden first because it trumps all other changes. + if (hidden) { + if (!first_draw) { + self->previous_area.next = tail; + return &self->previous_area; + } else { + return tail; + } + } else if (self->moved && !first_draw) { + displayio_area_union(&self->previous_area, &self->current_area, &self->dirty_area); + if (displayio_area_size(&self->dirty_area) <= 2U * self->pixel_width * self->pixel_height) { + self->dirty_area.next = tail; + return &self->dirty_area; + } + self->previous_area.next = tail; + self->current_area.next = &self->previous_area; + return &self->current_area; + } + + // If we have an in-memory bitmap, then check it for modifications. + if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { + displayio_area_t *refresh_area = displayio_bitmap_get_refresh_areas(self->bitmap, tail); + if (refresh_area != tail) { + // Special case a TileGrid that shows a full bitmap and use its + // dirty area. Copy it to ours so we can transform it. + if (self->tiles_in_bitmap == 1) { + displayio_area_copy(refresh_area, &self->dirty_area); + self->partial_change = true; + } else { + self->full_change = true; + } + } + } else if (mp_obj_is_type(self->bitmap, &displayio_shape_type)) { + displayio_area_t *refresh_area = displayio_shape_get_refresh_areas(self->bitmap, tail); + if (refresh_area != tail) { + displayio_area_copy(refresh_area, &self->dirty_area); + self->partial_change = true; + } + } + + self->full_change = self->full_change || + (mp_obj_is_type(self->pixel_shader, &displayio_palette_type) && + displayio_palette_needs_refresh(self->pixel_shader)) || + (mp_obj_is_type(self->pixel_shader, &displayio_colorconverter_type) && + displayio_colorconverter_needs_refresh(self->pixel_shader)); + if (self->full_change || first_draw) { + self->current_area.next = tail; + return &self->current_area; + } + + if (self->partial_change) { + int16_t x = self->x; + int16_t y = self->y; + if (self->absolute_transform->transpose_xy) { + int16_t temp = y; + y = x; + x = temp; + } + int16_t x1 = self->dirty_area.x1; + int16_t x2 = self->dirty_area.x2; + if (self->flip_x) { + x1 = self->pixel_width - x1; + x2 = self->pixel_width - x2; + } + int16_t y1 = self->dirty_area.y1; + int16_t y2 = self->dirty_area.y2; + if (self->flip_y) { + y1 = self->pixel_height - y1; + y2 = self->pixel_height - y2; + } + if (self->transpose_xy != self->absolute_transform->transpose_xy) { + int16_t temp1 = y1, temp2 = y2; + y1 = x1; + x1 = temp1; + y2 = x2; + x2 = temp2; + } + self->dirty_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * (x + x1); + self->dirty_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * (y + y1); + self->dirty_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (x + x2); + self->dirty_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (y + y2); + if (self->dirty_area.y2 < self->dirty_area.y1) { + int16_t temp = self->dirty_area.y2; + self->dirty_area.y2 = self->dirty_area.y1; + self->dirty_area.y1 = temp; + } + if (self->dirty_area.x2 < self->dirty_area.x1) { + int16_t temp = self->dirty_area.x2; + self->dirty_area.x2 = self->dirty_area.x1; + self->dirty_area.x1 = temp; + } + + self->dirty_area.next = tail; + return &self->dirty_area; + } + return tail; +} diff --git a/circuitpython/shared-module/displayio/TileGrid.h b/circuitpython/shared-module/displayio/TileGrid.h new file mode 100644 index 0000000..b5aab51 --- /dev/null +++ b/circuitpython/shared-module/displayio/TileGrid.h @@ -0,0 +1,89 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t bitmap; + mp_obj_t pixel_shader; + int16_t x; + int16_t y; + uint16_t pixel_width; + uint16_t pixel_height; + uint16_t bitmap_width_in_tiles; + ; + uint16_t tiles_in_bitmap; + uint16_t width_in_tiles; + uint16_t height_in_tiles; + uint16_t tile_width; + uint16_t tile_height; + uint16_t top_left_x; + uint16_t top_left_y; + uint8_t *tiles; + const displayio_buffer_transform_t *absolute_transform; + displayio_area_t dirty_area; // Stored as a relative area until the refresh area is fetched. + displayio_area_t previous_area; // Stored as an absolute area. + displayio_area_t current_area; // Stored as an absolute area so it applies across frames. + bool partial_change : 1; + bool full_change : 1; + bool moved : 1; + bool inline_tiles : 1; + bool in_group : 1; + bool flip_x : 1; + bool flip_y : 1; + bool transpose_xy : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + uint8_t padding : 6; +} displayio_tilegrid_t; + +void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden); + +// Updating the screen is a three stage process. + +// The first stage is used to determine i +displayio_area_t *displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t *tail); + +// Area is always in absolute screen coordinates. Update transform is used to inform TileGrids how +// they relate to it. +bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); +void displayio_tilegrid_update_transform(displayio_tilegrid_t *group, const displayio_buffer_transform_t *parent_transform); + +// Fills in area with the maximum bounds of all related pixels in the last rendered frame. Returns +// false if the tilegrid wasn't rendered in the last frame. +bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t *area); +void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H diff --git a/circuitpython/shared-module/displayio/__init__.c b/circuitpython/shared-module/displayio/__init__.c new file mode 100644 index 0000000..87962df --- /dev/null +++ b/circuitpython/shared-module/displayio/__init__.c @@ -0,0 +1,360 @@ +/* + * 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 <string.h> + +#include "shared-module/displayio/__init__.h" + +#include "shared/runtime/interrupt_char.h" +#include "py/runtime.h" +#include "shared-bindings/board/__init__.h" +#include "shared-bindings/displayio/Bitmap.h" +#include "shared-bindings/displayio/Display.h" +#include "shared-bindings/displayio/Group.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-module/displayio/area.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/reload.h" +#include "supervisor/memory.h" + +#include "supervisor/spi_flash_api.h" +#include "py/mpconfig.h" + +#if CIRCUITPY_SHARPDISPLAY +#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h" +#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" +#endif + +primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; + +displayio_buffer_transform_t null_transform = { + .x = 0, + .y = 0, + .dx = 1, + .dy = 1, + .scale = 1, + .width = 0, + .height = 0, + .mirror_x = false, + .mirror_y = false, + .transpose_xy = false +}; + +#if CIRCUITPY_RGBMATRIX || CIRCUITPY_IS31FL3741 || CIRCUITPY_VIDEOCORE +STATIC bool any_display_uses_this_framebuffer(mp_obj_base_t *obj) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].display_base.type == &framebufferio_framebufferdisplay_type) { + framebufferio_framebufferdisplay_obj_t *display = &displays[i].framebuffer_display; + if (display->framebuffer == obj) { + return true; + } + } + } + return false; +} +#endif + + +void displayio_background(void) { + if (mp_hal_is_interrupted()) { + return; + } + if (autoreload_ready()) { + // Reload is about to happen, so don't redisplay. + return; + } + + + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].display.base.type == NULL || displays[i].display.base.type == &mp_type_NoneType) { + // Skip null display. + continue; + } + if (displays[i].display.base.type == &displayio_display_type) { + displayio_display_background(&displays[i].display); + #if CIRCUITPY_FRAMEBUFFERIO + } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { + framebufferio_framebufferdisplay_background(&displays[i].framebuffer_display); + #endif + } else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) { + displayio_epaperdisplay_background(&displays[i].epaper_display); + } + } + +} + +void common_hal_displayio_release_displays(void) { + // Release displays before busses so that they can send any final commands to turn the display + // off properly. + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display.base.type; + if (display_type == NULL || display_type == &mp_type_NoneType) { + continue; + } else if (display_type == &displayio_display_type) { + release_display(&displays[i].display); + } else if (display_type == &displayio_epaperdisplay_type) { + release_epaperdisplay(&displays[i].epaper_display); + #if CIRCUITPY_FRAMEBUFFERIO + } else if (display_type == &framebufferio_framebufferdisplay_type) { + release_framebufferdisplay(&displays[i].framebuffer_display); + #endif + } + displays[i].display.base.type = &mp_type_NoneType; + } + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t bus_type = displays[i].fourwire_bus.base.type; + if (bus_type == NULL || bus_type == &mp_type_NoneType) { + continue; + } else if (bus_type == &displayio_fourwire_type) { + common_hal_displayio_fourwire_deinit(&displays[i].fourwire_bus); + } else if (bus_type == &displayio_i2cdisplay_type) { + common_hal_displayio_i2cdisplay_deinit(&displays[i].i2cdisplay_bus); + #if CIRCUITPY_PARALLELDISPLAY + } else if (bus_type == ¶lleldisplay_parallelbus_type) { + common_hal_paralleldisplay_parallelbus_deinit(&displays[i].parallel_bus); + #endif + #if CIRCUITPY_RGBMATRIX + } else if (bus_type == &rgbmatrix_RGBMatrix_type) { + common_hal_rgbmatrix_rgbmatrix_deinit(&displays[i].rgbmatrix); + #endif + #if CIRCUITPY_IS31FL3741 + } else if (bus_type == &is31fl3741_FrameBuffer_type) { + common_hal_is31fl3741_FrameBuffer_deinit(&displays[i].is31fl3741); + #endif + #if CIRCUITPY_SHARPDISPLAY + } else if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type) { + common_hal_sharpdisplay_framebuffer_deinit(&displays[i].sharpdisplay); + #endif + #if CIRCUITPY_VIDEOCORE + } else if (displays[i].bus_base.type == &videocore_framebuffer_type) { + common_hal_videocore_framebuffer_deinit(&displays[i].videocore); + #endif + } + displays[i].fourwire_bus.base.type = &mp_type_NoneType; + } + + supervisor_stop_terminal(); +} + +void reset_displays(void) { + // The SPI buses used by FourWires may be allocated on the heap so we need to move them inline. + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].fourwire_bus.base.type == &displayio_fourwire_type) { + displayio_fourwire_obj_t *fourwire = &displays[i].fourwire_bus; + if (((size_t)fourwire->bus) < ((size_t)&displays) || + ((size_t)fourwire->bus) > ((size_t)&displays + CIRCUITPY_DISPLAY_LIMIT)) { + busio_spi_obj_t *original_spi = fourwire->bus; + #if CIRCUITPY_BOARD_SPI + // We don't need to move original_spi if it is a board.SPI object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (common_hal_board_is_spi(original_spi)) { + continue; + } + #endif + #ifdef BOARD_USE_INTERNAL_SPI + if (original_spi == (mp_obj_t)(&supervisor_flash_spi_bus)) { + continue; + } + #endif + memcpy(&fourwire->inline_bus, original_spi, sizeof(busio_spi_obj_t)); + fourwire->bus = &fourwire->inline_bus; + // Check for other displays that use the same spi bus and swap them too. + for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { + if (displays[i].fourwire_bus.base.type == &displayio_fourwire_type && + displays[i].fourwire_bus.bus == original_spi) { + displays[i].fourwire_bus.bus = &fourwire->inline_bus; + } + } + } + } else if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type) { + displayio_i2cdisplay_obj_t *i2c = &displays[i].i2cdisplay_bus; + if (((size_t)i2c->bus) < ((size_t)&displays) || + ((size_t)i2c->bus) > ((size_t)&displays + CIRCUITPY_DISPLAY_LIMIT)) { + busio_i2c_obj_t *original_i2c = i2c->bus; + #if CIRCUITPY_BOARD_I2C + // We don't need to move original_i2c if it is a board.I2C object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (common_hal_board_is_i2c(original_i2c)) { + continue; + } + #endif + memcpy(&i2c->inline_bus, original_i2c, sizeof(busio_i2c_obj_t)); + i2c->bus = &i2c->inline_bus; + // Check for other displays that use the same i2c bus and swap them too. + for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { + if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type && + displays[i].i2cdisplay_bus.bus == original_i2c) { + displays[i].i2cdisplay_bus.bus = &i2c->inline_bus; + } + } + } + #if CIRCUITPY_RGBMATRIX + } else if (displays[i].rgbmatrix.base.type == &rgbmatrix_RGBMatrix_type) { + rgbmatrix_rgbmatrix_obj_t *pm = &displays[i].rgbmatrix; + if (!any_display_uses_this_framebuffer(&pm->base)) { + common_hal_rgbmatrix_rgbmatrix_deinit(pm); + } else { + common_hal_rgbmatrix_rgbmatrix_set_paused(pm, true); + } + #endif + #if CIRCUITPY_IS31FL3741 + } else if (displays[i].is31fl3741.base.type == &is31fl3741_FrameBuffer_type) { + is31fl3741_FrameBuffer_obj_t *is31fb = &displays[i].is31fl3741; + + if (((uint32_t)is31fb->is31fl3741->i2c) < ((uint32_t)&displays) || + ((uint32_t)is31fb->is31fl3741->i2c) > ((uint32_t)&displays + CIRCUITPY_DISPLAY_LIMIT)) { + #if CIRCUITPY_BOARD_I2C + // We don't need to move original_i2c if it is the board.I2C object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (common_hal_board_is_i2c(is31fb->is31fl3741->i2c)) { + continue; + } + #endif + + is31fl3741_IS31FL3741_obj_t *original_is31 = is31fb->is31fl3741; + memcpy(&is31fb->inline_is31fl3741, original_is31, sizeof(is31fl3741_IS31FL3741_obj_t)); + is31fb->is31fl3741 = &is31fb->inline_is31fl3741; + + busio_i2c_obj_t *original_i2c = is31fb->is31fl3741->i2c; + memcpy(&is31fb->is31fl3741->inline_i2c, original_i2c, sizeof(busio_i2c_obj_t)); + is31fb->is31fl3741->i2c = &is31fb->is31fl3741->inline_i2c; + } + + if (!any_display_uses_this_framebuffer(&is31fb->base)) { + common_hal_is31fl3741_FrameBuffer_deinit(is31fb); + } else { + common_hal_is31fl3741_FrameBuffer_set_paused(is31fb, true); + } + #endif + #if CIRCUITPY_SHARPDISPLAY + } else if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type) { + sharpdisplay_framebuffer_obj_t *sharp = &displays[i].sharpdisplay; + common_hal_sharpdisplay_framebuffer_reset(sharp); + #endif + #if CIRCUITPY_VIDEOCORE + } else if (displays[i].bus_base.type == &videocore_framebuffer_type) { + videocore_framebuffer_obj_t *vc = &displays[i].videocore; + if (!any_display_uses_this_framebuffer(&vc->base)) { + common_hal_videocore_framebuffer_deinit(vc); + } + // The framebuffer is allocated outside of the heap so it doesn't + // need to be moved. + #endif + } else { + // Not an active display bus. + continue; + } + } + + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + // Reset the displayed group. Only the first will get the terminal but + // that's ok. + if (displays[i].display.base.type == &displayio_display_type) { + reset_display(&displays[i].display); + } else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) { + displayio_epaperdisplay_obj_t *display = &displays[i].epaper_display; + common_hal_displayio_epaperdisplay_show(display, NULL); + #if CIRCUITPY_FRAMEBUFFERIO + } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { + framebufferio_framebufferdisplay_reset(&displays[i].framebuffer_display); + #endif + } + } +} + +void displayio_gc_collect(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + #if CIRCUITPY_RGBMATRIX + if (displays[i].rgbmatrix.base.type == &rgbmatrix_RGBMatrix_type) { + rgbmatrix_rgbmatrix_collect_ptrs(&displays[i].rgbmatrix); + } + #endif + #if CIRCUITPY_IS31FL3741 + if (displays[i].is31fl3741.base.type == &is31fl3741_FrameBuffer_type) { + is31fl3741_FrameBuffer_collect_ptrs(&displays[i].is31fl3741); + } + #endif + #if CIRCUITPY_SHARPDISPLAY + if (displays[i].bus_base.type == &sharpdisplay_framebuffer_type) { + common_hal_sharpdisplay_framebuffer_collect_ptrs(&displays[i].sharpdisplay); + } + #endif + + if (displays[i].display.base.type == NULL) { + continue; + } + + // Alternatively, we could use gc_collect_root over the whole object, + // but this is more precise, and is the only field that needs marking. + if (displays[i].display.base.type == &displayio_display_type) { + displayio_display_collect_ptrs(&displays[i].display); + #if CIRCUITPY_FRAMEBUFFERIO + } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { + framebufferio_framebufferdisplay_collect_ptrs(&displays[i].framebuffer_display); + #endif + } else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) { + displayio_epaperdisplay_collect_ptrs(&displays[i].epaper_display); + } + } +} + +primary_display_t *allocate_display(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display.base.type; + if (display_type == NULL || display_type == &mp_type_NoneType) { + return &displays[i]; + } + } + return NULL; +} + +primary_display_t *allocate_display_or_raise(void) { + primary_display_t *result = allocate_display(); + if (result) { + return result; + } + mp_raise_RuntimeError(translate("Too many displays")); +} +primary_display_t *allocate_display_bus(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_bus_type = displays[i].bus_base.type; + if (display_bus_type == NULL || display_bus_type == &mp_type_NoneType) { + return &displays[i]; + } + } + return NULL; +} + +primary_display_t *allocate_display_bus_or_raise(void) { + primary_display_t *result = allocate_display_bus(); + if (result) { + return result; + } + mp_raise_RuntimeError(translate("Too many display busses")); +} diff --git a/circuitpython/shared-module/displayio/__init__.h b/circuitpython/shared-module/displayio/__init__.h new file mode 100644 index 0000000..c1954b1 --- /dev/null +++ b/circuitpython/shared-module/displayio/__init__.h @@ -0,0 +1,98 @@ +/* + * 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___INIT___H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H + +#include "shared-bindings/displayio/Display.h" +#include "shared-bindings/displayio/EPaperDisplay.h" +#if CIRCUITPY_FRAMEBUFFERIO +#include "shared-bindings/framebufferio/FramebufferDisplay.h" +#endif +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/Group.h" +#include "shared-bindings/displayio/I2CDisplay.h" +#if CIRCUITPY_PARALLELDISPLAY +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#endif +#if CIRCUITPY_RGBMATRIX +#include "shared-bindings/rgbmatrix/RGBMatrix.h" +#endif +#if CIRCUITPY_IS31FL3741 +#include "shared-bindings/is31fl3741/FrameBuffer.h" +#endif +#if CIRCUITPY_SHARPDISPLAY +#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" +#endif +#if CIRCUITPY_VIDEOCORE +#include "bindings/videocore/Framebuffer.h" +#endif + +typedef struct { + union { + mp_obj_base_t bus_base; + displayio_fourwire_obj_t fourwire_bus; + displayio_i2cdisplay_obj_t i2cdisplay_bus; + #if CIRCUITPY_PARALLELDISPLAY + paralleldisplay_parallelbus_obj_t parallel_bus; + #endif + #if CIRCUITPY_RGBMATRIX + rgbmatrix_rgbmatrix_obj_t rgbmatrix; + #endif + #if CIRCUITPY_IS31FL3741 + is31fl3741_FrameBuffer_obj_t is31fl3741; + #endif + #if CIRCUITPY_SHARPDISPLAY + sharpdisplay_framebuffer_obj_t sharpdisplay; + #endif + #if CIRCUITPY_VIDEOCORE + videocore_framebuffer_obj_t videocore; + #endif + }; + union { + mp_obj_base_t display_base; + displayio_display_obj_t display; + displayio_epaperdisplay_obj_t epaper_display; + #if CIRCUITPY_FRAMEBUFFERIO + framebufferio_framebufferdisplay_obj_t framebuffer_display; + #endif + }; +} primary_display_t; + +extern primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; + +extern displayio_group_t circuitpython_splash; + +void displayio_background(void); +void reset_displays(void); +void displayio_gc_collect(void); + +primary_display_t *allocate_display(void); +primary_display_t *allocate_display_or_raise(void); +primary_display_t *allocate_display_bus(void); +primary_display_t *allocate_display_bus_or_raise(void); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H diff --git a/circuitpython/shared-module/displayio/area.c b/circuitpython/shared-module/displayio/area.c new file mode 100644 index 0000000..6d0f94d --- /dev/null +++ b/circuitpython/shared-module/displayio/area.c @@ -0,0 +1,161 @@ +/* + * 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-module/displayio/area.h" + +#include "py/misc.h" + +void displayio_area_copy(const displayio_area_t *src, displayio_area_t *dst) { + dst->x1 = src->x1; + dst->y1 = src->y1; + dst->x2 = src->x2; + dst->y2 = src->y2; +} + +void displayio_area_scale(displayio_area_t *area, uint16_t scale) { + area->x1 *= scale; + area->y1 *= scale; + area->x2 *= scale; + area->y2 *= scale; +} + +void displayio_area_shift(displayio_area_t *area, int16_t dx, int16_t dy) { + area->x1 += dx; + area->y1 += dy; + area->x2 += dx; + area->y2 += dy; +} + +bool displayio_area_compute_overlap(const displayio_area_t *a, + const displayio_area_t *b, + displayio_area_t *overlap) { + overlap->x1 = a->x1; + if (b->x1 > overlap->x1) { + overlap->x1 = b->x1; + } + overlap->x2 = a->x2; + if (b->x2 < overlap->x2) { + overlap->x2 = b->x2; + } + if (overlap->x1 >= overlap->x2) { + return false; + } + overlap->y1 = a->y1; + if (b->y1 > overlap->y1) { + overlap->y1 = b->y1; + } + overlap->y2 = a->y2; + if (b->y2 < overlap->y2) { + overlap->y2 = b->y2; + } + if (overlap->y1 >= overlap->y2) { + return false; + } + return true; +} + +bool displayio_area_empty(const displayio_area_t *a) { + return (a->x1 == a->x2) || (a->y1 == a->y2); +} + +void displayio_area_canon(displayio_area_t *a) { + if (a->x1 > a->x2) { + int16_t t = a->x1; + a->x1 = a->x2; + a->x2 = t; + } + if (a->y1 > a->y2) { + int16_t t = a->y1; + a->y1 = a->y2; + a->y2 = t; + } +} + +void displayio_area_union(const displayio_area_t *a, + const displayio_area_t *b, + displayio_area_t *u) { + + if (displayio_area_empty(a)) { + displayio_area_copy(b, u); + return; + } + if (displayio_area_empty(b)) { + displayio_area_copy(a, u); + return; + } + u->x1 = MIN(a->x1, b->x1); + u->y1 = MIN(a->y1, b->y1); + u->x2 = MAX(a->x2, b->x2); + u->y2 = MAX(a->y2, b->y2); +} + +uint16_t displayio_area_width(const displayio_area_t *area) { + return area->x2 - area->x1; +} + +uint16_t displayio_area_height(const displayio_area_t *area) { + return area->y2 - area->y1; +} + +uint32_t displayio_area_size(const displayio_area_t *area) { + return displayio_area_width(area) * displayio_area_height(area); +} + +bool displayio_area_equal(const displayio_area_t *a, const displayio_area_t *b) { + return a->x1 == b->x1 && + a->y1 == b->y1 && + a->x2 == b->x2 && + a->y2 == b->y2; +} + +// Original and whole must be in the same coordinate space. +void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpose_xy, + const displayio_area_t *original, + const displayio_area_t *whole, + displayio_area_t *transformed) { + if (mirror_x) { + transformed->x1 = whole->x1 + (whole->x2 - original->x2); + transformed->x2 = whole->x2 - (original->x1 - whole->x1); + } else { + transformed->x1 = original->x1; + transformed->x2 = original->x2; + } + if (mirror_y) { + transformed->y1 = whole->y1 + (whole->y2 - original->y2); + transformed->y2 = whole->y2 - (original->y1 - whole->y1); + } else { + transformed->y1 = original->y1; + transformed->y2 = original->y2; + } + if (transpose_xy) { + int16_t y1 = transformed->y1; + int16_t y2 = transformed->y2; + transformed->y1 = whole->y1 + (transformed->x1 - whole->x1); + transformed->y2 = whole->y1 + (transformed->x2 - whole->x1); + transformed->x2 = whole->x1 + (y2 - whole->y1); + transformed->x1 = whole->x1 + (y1 - whole->y1); + } +} diff --git a/circuitpython/shared-module/displayio/area.h b/circuitpython/shared-module/displayio/area.h new file mode 100644 index 0000000..47cb48b --- /dev/null +++ b/circuitpython/shared-module/displayio/area.h @@ -0,0 +1,80 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H + +#include <stdint.h> +#include <stdbool.h> + +// Implementations are in area.c +typedef struct _displayio_area_t displayio_area_t; + +struct _displayio_area_t { + int16_t x1; + int16_t y1; + int16_t x2; // Second point is exclusive. + int16_t y2; + const displayio_area_t *next; // Next area in the linked list. +}; + +typedef struct { + uint16_t x; + uint16_t y; + int8_t dx; + int8_t dy; + uint8_t scale; + uint16_t width; + uint16_t height; + bool mirror_x; + bool mirror_y; + bool transpose_xy; +} displayio_buffer_transform_t; + +extern displayio_buffer_transform_t null_transform; + +bool displayio_area_empty(const displayio_area_t *a); +void displayio_area_copy_coords(const displayio_area_t *src, displayio_area_t *dest); +void displayio_area_canon(displayio_area_t *a); +void displayio_area_union(const displayio_area_t *a, + const displayio_area_t *b, + displayio_area_t *u); +void displayio_area_copy(const displayio_area_t *src, displayio_area_t *dst); +void displayio_area_scale(displayio_area_t *area, uint16_t scale); +void displayio_area_shift(displayio_area_t *area, int16_t dx, int16_t dy); +bool displayio_area_compute_overlap(const displayio_area_t *a, + const displayio_area_t *b, + displayio_area_t *overlap); +uint16_t displayio_area_width(const displayio_area_t *area); +uint16_t displayio_area_height(const displayio_area_t *area); +uint32_t displayio_area_size(const displayio_area_t *area); +bool displayio_area_equal(const displayio_area_t *a, const displayio_area_t *b); +void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpose_xy, + const displayio_area_t *original, + const displayio_area_t *whole, + displayio_area_t *transformed); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H diff --git a/circuitpython/shared-module/displayio/display_core.c b/circuitpython/shared-module/displayio/display_core.c new file mode 100644 index 0000000..8b2f0bf --- /dev/null +++ b/circuitpython/shared-module/displayio/display_core.c @@ -0,0 +1,398 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/Display.h" + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/I2CDisplay.h" +#if CIRCUITPY_PARALLELDISPLAY +#include "shared-bindings/paralleldisplay/ParallelBus.h" +#endif +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/tick.h" + +#include <stdint.h> +#include <string.h> + +#define DISPLAYIO_CORE_DEBUG(...) (void)0 +// #define DISPLAYIO_CORE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__) + +void displayio_display_core_construct(displayio_display_core_t *self, + mp_obj_t bus, uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation, + uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word) { + self->colorspace.depth = color_depth; + self->colorspace.grayscale = grayscale; + self->colorspace.grayscale_bit = 8 - color_depth; + self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row; + self->colorspace.bytes_per_cell = bytes_per_cell; + self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte; + self->colorspace.reverse_bytes_in_word = reverse_bytes_in_word; + self->colorspace.dither = false; + self->current_group = NULL; + self->colstart = colstart; + self->rowstart = rowstart; + self->last_refresh = 0; + + // (framebufferdisplay already validated its 'bus' is a buffer-protocol object) + if (bus) { + #if CIRCUITPY_PARALLELDISPLAY + if (mp_obj_is_type(bus, ¶lleldisplay_parallelbus_type)) { + self->bus_reset = common_hal_paralleldisplay_parallelbus_reset; + self->bus_free = common_hal_paralleldisplay_parallelbus_bus_free; + self->begin_transaction = common_hal_paralleldisplay_parallelbus_begin_transaction; + self->send = common_hal_paralleldisplay_parallelbus_send; + self->end_transaction = common_hal_paralleldisplay_parallelbus_end_transaction; + } else + #endif + if (mp_obj_is_type(bus, &displayio_fourwire_type)) { + self->bus_reset = common_hal_displayio_fourwire_reset; + self->bus_free = common_hal_displayio_fourwire_bus_free; + self->begin_transaction = common_hal_displayio_fourwire_begin_transaction; + self->send = common_hal_displayio_fourwire_send; + self->end_transaction = common_hal_displayio_fourwire_end_transaction; + } else if (mp_obj_is_type(bus, &displayio_i2cdisplay_type)) { + self->bus_reset = common_hal_displayio_i2cdisplay_reset; + self->bus_free = common_hal_displayio_i2cdisplay_bus_free; + self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction; + self->send = common_hal_displayio_i2cdisplay_send; + self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction; + } else { + mp_raise_ValueError(translate("Unsupported display bus type")); + } + } + self->bus = bus; + + + // (offsetof core is equal in all display types) + if (self == &displays[0].display.core) { + supervisor_start_terminal(width, height); + } + + self->width = width; + self->height = height; + self->ram_width = ram_width; + self->ram_height = ram_height; + + displayio_display_core_set_rotation(self, rotation); +} + +void displayio_display_core_set_rotation(displayio_display_core_t *self, + int rotation) { + int height = self->height; + int width = self->width; + + rotation = rotation % 360; + self->rotation = rotation; + self->transform.x = 0; + self->transform.y = 0; + self->transform.scale = 1; + self->transform.mirror_x = false; + self->transform.mirror_y = false; + self->transform.transpose_xy = false; + if (rotation == 0 || rotation == 180) { + if (rotation == 180) { + self->transform.mirror_x = true; + self->transform.mirror_y = true; + } + } else { + self->transform.transpose_xy = true; + if (rotation == 270) { + self->transform.mirror_y = true; + } else { + self->transform.mirror_x = true; + } + } + + self->area.x1 = 0; + self->area.y1 = 0; + self->area.next = NULL; + + self->transform.dx = 1; + self->transform.dy = 1; + if (self->transform.transpose_xy) { + self->area.x2 = height; + self->area.y2 = width; + if (self->transform.mirror_x) { + self->transform.x = height; + self->transform.dx = -1; + } + if (self->transform.mirror_y) { + self->transform.y = width; + self->transform.dy = -1; + } + } else { + self->area.x2 = width; + self->area.y2 = height; + if (self->transform.mirror_x) { + self->transform.x = width; + self->transform.dx = -1; + } + if (self->transform.mirror_y) { + self->transform.y = height; + self->transform.dy = -1; + } + } +} + +bool displayio_display_core_show(displayio_display_core_t *self, displayio_group_t *root_group) { + + if (root_group == NULL) { // set the display to the REPL, reset REPL position and size + circuitpython_splash.in_group = false; + // force the circuit_python_splash out of any group (Note: could cause problems with the parent group) + circuitpython_splash.x = 0; // reset position in case someone moved it. + circuitpython_splash.y = 0; + + supervisor_start_terminal(self->width, self->height); + + root_group = &circuitpython_splash; + } + if (root_group == self->current_group) { + return true; + } + if (root_group != NULL && root_group->in_group) { + return false; + } + if (self->current_group != NULL) { + self->current_group->in_group = false; + } + + if (root_group != NULL) { + displayio_group_update_transform(root_group, &self->transform); + root_group->in_group = true; + } + self->current_group = root_group; + self->full_refresh = true; + return true; +} + +uint16_t displayio_display_core_get_width(displayio_display_core_t *self) { + return self->width; +} + +uint16_t displayio_display_core_get_height(displayio_display_core_t *self) { + return self->height; +} + +void displayio_display_core_set_dither(displayio_display_core_t *self, bool dither) { + self->colorspace.dither = dither; +} + +bool displayio_display_core_get_dither(displayio_display_core_t *self) { + return self->colorspace.dither; +} + +bool displayio_display_core_bus_free(displayio_display_core_t *self) { + return !self->bus || self->bus_free(self->bus); +} + +bool displayio_display_core_begin_transaction(displayio_display_core_t *self) { + return self->begin_transaction(self->bus); +} + +void displayio_display_core_end_transaction(displayio_display_core_t *self) { + self->end_transaction(self->bus); +} + +void displayio_display_core_set_region_to_update(displayio_display_core_t *self, uint8_t column_command, + uint8_t row_command, uint16_t set_current_column_command, uint16_t set_current_row_command, + bool data_as_commands, bool always_toggle_chip_select, + displayio_area_t *area, bool SH1107_addressing) { + uint16_t x1 = area->x1 + self->colstart; + uint16_t x2 = area->x2 + self->colstart; + uint16_t y1 = area->y1 + self->rowstart; + uint16_t y2 = area->y2 + self->rowstart; + + // Collapse down the dimension where multiple pixels are in a byte. + if (self->colorspace.depth < 8) { + uint8_t pixels_per_byte = 8 / self->colorspace.depth; + if (self->colorspace.pixels_in_byte_share_row) { + x1 /= pixels_per_byte * self->colorspace.bytes_per_cell; + x2 /= pixels_per_byte * self->colorspace.bytes_per_cell; + } else { + y1 /= pixels_per_byte * self->colorspace.bytes_per_cell; + y2 /= pixels_per_byte * self->colorspace.bytes_per_cell; + } + } + + x2 -= 1; + y2 -= 1; + + display_chip_select_behavior_t chip_select = CHIP_SELECT_UNTOUCHED; + if (always_toggle_chip_select || data_as_commands) { + chip_select = CHIP_SELECT_TOGGLE_EVERY_BYTE; + } + + // Set column. + displayio_display_core_begin_transaction(self); + uint8_t data[5]; + data[0] = column_command; + uint8_t data_length = 1; + display_byte_type_t data_type = DISPLAY_DATA; + if (!data_as_commands) { + self->send(self->bus, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, 1); + data_length = 0; + } else { + data_type = DISPLAY_COMMAND; + } + + if (self->ram_width < 0x100) { + data[data_length++] = x1; + data[data_length++] = x2; + } else { + data[data_length++] = x1 >> 8; + data[data_length++] = x1 & 0xff; + data[data_length++] = x2 >> 8; + data[data_length++] = x2 & 0xff; + } + + // Quirk for SH1107 "SH1107_addressing" + // Column lower command = 0x00, Column upper command = 0x10 + if (SH1107_addressing) { + data[0] = ((x1 >> 4) & 0x0F) | 0x10; // 0x10 to 0x17 + data[1] = x1 & 0x0F; // 0x00 to 0x0F + data_length = 2; + } + + self->send(self->bus, data_type, chip_select, data, data_length); + displayio_display_core_end_transaction(self); + + if (set_current_column_command != NO_COMMAND) { + uint8_t command = set_current_column_command; + displayio_display_core_begin_transaction(self); + self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1); + self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2); + displayio_display_core_end_transaction(self); + } + + + // Set row. + displayio_display_core_begin_transaction(self); + data[0] = row_command; + data_length = 1; + if (!data_as_commands) { + self->send(self->bus, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, 1); + data_length = 0; + } + + if (self->ram_height < 0x100) { + data[data_length++] = y1; + data[data_length++] = y2; + } else { + data[data_length++] = y1 >> 8; + data[data_length++] = y1 & 0xff; + data[data_length++] = y2 >> 8; + data[data_length++] = y2 & 0xff; + } + + // Quirk for SH1107 "SH1107_addressing" + // Page address command = 0xB0 + if (SH1107_addressing) { + // set the page to our y value + data[0] = 0xB0 | y1; + data_length = 1; + } + + self->send(self->bus, data_type, chip_select, data, data_length); + displayio_display_core_end_transaction(self); + + if (set_current_row_command != NO_COMMAND) { + uint8_t command = set_current_row_command; + displayio_display_core_begin_transaction(self); + self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1); + self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2); + displayio_display_core_end_transaction(self); + } +} + +bool displayio_display_core_start_refresh(displayio_display_core_t *self) { + if (!displayio_display_core_bus_free(self)) { + // Can't acquire display bus; skip updating this display. Try next display. + return false; + } + if (self->refresh_in_progress) { + return false; + } + self->refresh_in_progress = true; + self->last_refresh = supervisor_ticks_ms64(); + return true; +} + +void displayio_display_core_finish_refresh(displayio_display_core_t *self) { + if (self->current_group != NULL) { + DISPLAYIO_CORE_DEBUG("displayiocore group_finish_refresh\n"); + displayio_group_finish_refresh(self->current_group); + } + self->full_refresh = false; + self->refresh_in_progress = false; + self->last_refresh = supervisor_ticks_ms64(); +} + +void release_display_core(displayio_display_core_t *self) { + if (self->current_group != NULL) { + self->current_group->in_group = false; + } +} + +void displayio_display_core_collect_ptrs(displayio_display_core_t *self) { + gc_collect_ptr(self->current_group); +} + +bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { + return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer); +} + +bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped) { + bool overlaps = displayio_area_compute_overlap(&self->area, area, clipped); + if (!overlaps) { + return false; + } + // Expand the area if we have multiple pixels per byte and we need to byte + // align the bounds. + if (self->colorspace.depth < 8) { + uint8_t pixels_per_byte = 8 / self->colorspace.depth * self->colorspace.bytes_per_cell; + if (self->colorspace.pixels_in_byte_share_row) { + if (clipped->x1 % pixels_per_byte != 0) { + clipped->x1 -= clipped->x1 % pixels_per_byte; + } + if (clipped->x2 % pixels_per_byte != 0) { + clipped->x2 += pixels_per_byte - clipped->x2 % pixels_per_byte; + } + } else { + if (clipped->y1 % pixels_per_byte != 0) { + clipped->y1 -= clipped->y1 % pixels_per_byte; + } + if (clipped->y2 % pixels_per_byte != 0) { + clipped->y2 += pixels_per_byte - clipped->y2 % pixels_per_byte; + } + } + } + return true; +} diff --git a/circuitpython/shared-module/displayio/display_core.h b/circuitpython/shared-module/displayio/display_core.h new file mode 100644 index 0000000..8c2ba21 --- /dev/null +++ b/circuitpython/shared-module/displayio/display_core.h @@ -0,0 +1,94 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_CORE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_CORE_H + +#include "shared-bindings/displayio/__init__.h" +#include "shared-bindings/displayio/Group.h" + +#include "shared-module/displayio/area.h" + +#define NO_COMMAND 0x100 + +typedef struct { + mp_obj_t bus; + displayio_group_t *current_group; + uint64_t last_refresh; + display_bus_bus_reset bus_reset; + display_bus_bus_free bus_free; + display_bus_begin_transaction begin_transaction; + display_bus_send send; + display_bus_end_transaction end_transaction; + displayio_buffer_transform_t transform; + displayio_area_t area; + uint16_t width; + uint16_t height; + uint16_t rotation; + uint16_t ram_width; + uint16_t ram_height; + _displayio_colorspace_t colorspace; + int16_t colstart; + int16_t rowstart; + bool full_refresh; // New group means we need to refresh the whole display. + bool refresh_in_progress; +} displayio_display_core_t; + +void displayio_display_core_construct(displayio_display_core_t *self, + mp_obj_t bus, uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation, + uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word); + +bool displayio_display_core_show(displayio_display_core_t *self, displayio_group_t *root_group); + +uint16_t displayio_display_core_get_width(displayio_display_core_t *self); +uint16_t displayio_display_core_get_height(displayio_display_core_t *self); + +void displayio_display_core_set_dither(displayio_display_core_t *self, bool dither); +bool displayio_display_core_get_dither(displayio_display_core_t *self); + +void displayio_display_core_set_rotation(displayio_display_core_t *self, int rotation); + +bool displayio_display_core_bus_free(displayio_display_core_t *self); +bool displayio_display_core_begin_transaction(displayio_display_core_t *self); +void displayio_display_core_end_transaction(displayio_display_core_t *self); + +void displayio_display_core_set_region_to_update(displayio_display_core_t *self, uint8_t column_command, + uint8_t row_command, uint16_t set_current_column_command, uint16_t set_current_row_command, + bool data_as_commands, bool always_toggle_chip_select, + displayio_area_t *area, bool SH1107_addressing); + +void release_display_core(displayio_display_core_t *self); + +bool displayio_display_core_start_refresh(displayio_display_core_t *self); +void displayio_display_core_finish_refresh(displayio_display_core_t *self); + +void displayio_display_core_collect_ptrs(displayio_display_core_t *self); + +bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer); + +bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_CORE_H diff --git a/circuitpython/shared-module/displayio/mipi_constants.h b/circuitpython/shared-module/displayio/mipi_constants.h new file mode 100644 index 0000000..3cb7e42 --- /dev/null +++ b/circuitpython/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 |
