summaryrefslogtreecommitdiff
path: root/shared-module/displayio
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module/displayio')
-rw-r--r--shared-module/displayio/Bitmap.c152
-rw-r--r--shared-module/displayio/Bitmap.h12
-rw-r--r--shared-module/displayio/ColorConverter.c178
-rw-r--r--shared-module/displayio/ColorConverter.h15
-rw-r--r--shared-module/displayio/Display.c433
-rw-r--r--shared-module/displayio/Display.h67
-rw-r--r--shared-module/displayio/EPaperDisplay.c387
-rw-r--r--shared-module/displayio/EPaperDisplay.h68
-rw-r--r--shared-module/displayio/FourWire.c135
-rw-r--r--shared-module/displayio/FourWire.h46
-rw-r--r--shared-module/displayio/Group.c394
-rw-r--r--shared-module/displayio/Group.h31
-rw-r--r--shared-module/displayio/I2CDisplay.c123
-rw-r--r--shared-module/displayio/I2CDisplay.h41
-rw-r--r--shared-module/displayio/OnDiskBitmap.c135
-rw-r--r--shared-module/displayio/OnDiskBitmap.h7
-rw-r--r--shared-module/displayio/Palette.c71
-rw-r--r--shared-module/displayio/Palette.h45
-rw-r--r--shared-module/displayio/Shape.c90
-rw-r--r--shared-module/displayio/Shape.h (renamed from shared-module/displayio/Sprite.h)24
-rw-r--r--shared-module/displayio/Sprite.c99
-rw-r--r--shared-module/displayio/TileGrid.c587
-rw-r--r--shared-module/displayio/TileGrid.h88
-rw-r--r--shared-module/displayio/__init__.c430
-rw-r--r--shared-module/displayio/__init__.h48
-rw-r--r--shared-module/displayio/area.h73
-rw-r--r--shared-module/displayio/display_core.c351
-rw-r--r--shared-module/displayio/display_core.h90
28 files changed, 3893 insertions, 327 deletions
diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c
index addc341a7..8bcda6086 100644
--- a/shared-module/displayio/Bitmap.c
+++ b/shared-module/displayio/Bitmap.c
@@ -33,20 +33,21 @@
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;
- // word align
- if (row_width % 32 != 0) {
- self->stride = (row_width / 32 + 1);
+ // align to size_t
+ uint8_t align_bits = 8 * sizeof(size_t);
+ if (row_width % align_bits != 0) {
+ self->stride = (row_width / align_bits + 1);
} else {
- self->stride = row_width / 32;
+ self->stride = row_width / align_bits;
}
self->width = width;
self->height = height;
- self->data = m_malloc(self->stride * height * sizeof(uint32_t), false);
-
+ self->data = m_malloc(self->stride * height * sizeof(size_t), false);
+ self->read_only = false;
self->bits_per_value = bits_per_value;
- if (bits_per_value > 8) {
- mp_raise_NotImplementedError(translate("Only bit maps of 8 bit color or less are supported"));
+ 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
@@ -56,46 +57,129 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi
self->x_shift = 0; // Used to divide the index by the number of pixels per word. Its used in a
// shift which effectively divides by 2 ** x_shift.
uint32_t power_of_two = 1;
- while (power_of_two < 32 / bits_per_value ) {
+ 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;
}
-void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len) {
- if (len != self->stride * sizeof(uint32_t)) {
- mp_raise_ValueError(translate("row must be packed and word aligned"));
- }
- uint32_t* row_value = self->data + (y * self->stride);
- // Do the memcpy ourselves since we may want to flip endianness.
- for (uint32_t i = 0; i < self->stride; i++) {
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wcast-align"
- uint32_t value = ((uint32_t *)data)[i];
- #pragma GCC diagnostic pop
- if (self->bits_per_value < 16) {
- value = ((value >> 24) & 0xff) |
- ((value << 8) & 0xff0000) |
- ((value >> 8) & 0xff00) |
- ((value << 24) & 0xff000000);
- }
- *row_value = value;
- row_value++;
- }
+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;
- if (self->bits_per_value < 8) {
- uint32_t word = self->data[row_start + (x >> self->x_shift)];
+ uint32_t bytes_per_value = self->bits_per_value / 8;
+ if (bytes_per_value < 1) {
+ size_t word = self->data[row_start + (x >> self->x_shift)];
+
+ return (word >> (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask;
+ } else {
+ size_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 common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
+ if (self->read_only) {
+ mp_raise_RuntimeError(translate("Read-only object"));
+ }
+ // Update the dirty area.
+ if (self->dirty_area.x1 == self->dirty_area.x2) {
+ self->dirty_area.x1 = x;
+ self->dirty_area.x2 = x + 1;
+ self->dirty_area.y1 = y;
+ self->dirty_area.y2 = y + 1;
+ } else {
+ if (x < self->dirty_area.x1) {
+ self->dirty_area.x1 = x;
+ } else if (x >= self->dirty_area.x2) {
+ self->dirty_area.x2 = x + 1;
+ }
+ if (y < self->dirty_area.y1) {
+ self->dirty_area.y1 = y;
+ } else if (y >= self->dirty_area.y2) {
+ self->dirty_area.y2 = y + 1;
+ }
+ }
- return (word >> (32 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask;
+ // Update our 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(size_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 bytes_per_value = self->bits_per_value / 8;
- return self->data[row_start + x * bytes_per_value];
+ size_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;
+ }
+ }
+}
+
+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) {
+ if (self->read_only) {
+ mp_raise_RuntimeError(translate("Read-only object"));
+ }
+ // Update the dirty area.
+ self->dirty_area.x1 = 0;
+ self->dirty_area.x2 = self->width;
+ self->dirty_area.y1 = 0;
+ self->dirty_area.y2 = self->height;
+
+ // 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;
}
}
diff --git a/shared-module/displayio/Bitmap.h b/shared-module/displayio/Bitmap.h
index 781a4d12a..f4bd7ce4d 100644
--- a/shared-module/displayio/Bitmap.h
+++ b/shared-module/displayio/Bitmap.h
@@ -31,17 +31,23 @@
#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; // words
+ size_t* data;
+ uint16_t stride; // size_t's
uint8_t bits_per_value;
uint8_t x_shift;
- uint8_t x_mask;
+ 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);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H
diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c
index d20b24c01..c2c3214aa 100644
--- a/shared-module/displayio/ColorConverter.c
+++ b/shared-module/displayio/ColorConverter.c
@@ -26,16 +26,170 @@
#include "shared-bindings/displayio/ColorConverter.h"
-void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) {
-}
-
-bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, uint32_t input_color, uint16_t* output_color) {
- // TODO(tannewt): Validate the color input against the input format.
- uint32_t r5 = (input_color >> 19);
- uint32_t g6 = (input_color >> 10) & 0x3f;
- uint32_t b5 = (input_color >> 3) & 0x1f;
- uint32_t packed = r5 << 11 | g6 << 5 | b5;
- // swap bytes
- *output_color = __builtin_bswap16(packed);
- return true;
+#include "py/misc.h"
+
+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) {
+ self->dither = dither;
+}
+
+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) / 255 + (g8 * 182) / 255 + (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, uint8_t pixel_luma, 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 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->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, luma, &output_color->pixel);
+ return;
+ } else if (colorspace->grayscale && colorspace->depth <= 8) {
+ uint8_t luma = displayio_colorconverter_compute_luma(pixel);
+ output_color->pixel = luma >> (8 - colorspace->depth);
+ 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/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h
index 62ef2eaab..10b1604a4 100644
--- a/shared-module/displayio/ColorConverter.h
+++ b/shared-module/displayio/ColorConverter.h
@@ -31,9 +31,24 @@
#include <stdint.h>
#include "py/obj.h"
+#include "shared-module/displayio/Palette.h"
typedef struct {
mp_obj_base_t base;
+ bool dither;
} 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, uint8_t pixel_luma, uint32_t* color);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c
new file mode 100644
index 000000000..a3d877f1a
--- /dev/null
+++ b/shared-module/displayio/Display.c
@@ -0,0 +1,433 @@
+/*
+ * 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"
+#include "shared-bindings/displayio/ParallelBus.h"
+#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>
+
+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 set_vertical_scroll,
+ 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) {
+ // 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->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;
+ }
+
+ supervisor_start_terminal(width, height);
+
+ // 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_PULSEIO)
+ pwmout_result_t result = common_hal_pulseio_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 = &pulseio_pwmout_type;
+ common_hal_pulseio_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);
+ self->auto_refresh = 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_PULSEIO)
+ bool ispwm = (self->backlight_pwm.base.type == &pulseio_pwmout_type) ? true : false;
+ #else
+ bool ispwm = false;
+ #endif
+
+ if (ispwm) {
+ #if (CIRCUITPY_PULSEIO)
+ common_hal_pulseio_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;
+}
+
+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 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;
+ }
+ // 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);
+
+ 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.
+ usb_background();
+ }
+ return true;
+}
+
+STATIC void _refresh_display(displayio_display_obj_t* self) {
+ if (!displayio_display_core_bus_free(&self->core)) {
+ // Can't acquire display bus; skip updating this display. Try next display.
+ return;
+ }
+ displayio_display_core_start_refresh(&self->core);
+ 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);
+ 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) {
+ 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;
+ 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 it's 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) {
+ release_display_core(&self->core);
+ #if (CIRCUITPY_PULSEIO)
+ if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
+ common_hal_pulseio_pwmout_reset_ok(&self->backlight_pwm);
+ common_hal_pulseio_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) {
+ self->auto_refresh = 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/shared-module/displayio/Display.h b/shared-module/displayio/Display.h
new file mode 100644
index 000000000..8adaf597f
--- /dev/null
+++ b/shared-module/displayio/Display.h
@@ -0,0 +1,67 @@
+/*
+ * 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"
+#include "shared-bindings/pulseio/PWMOut.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;
+ union {
+ digitalio_digitalinout_obj_t backlight_inout;
+ pulseio_pwmout_obj_t backlight_pwm;
+ };
+ 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;
+} 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/shared-module/displayio/EPaperDisplay.c b/shared-module/displayio/EPaperDisplay.c
new file mode 100644
index 000000000..6a55687b9
--- /dev/null
+++ b/shared-module/displayio/EPaperDisplay.c
@@ -0,0 +1,387 @@
+/*
+ * 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"
+#include "shared-bindings/displayio/ParallelBus.h"
+#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>
+
+void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t* self,
+ mp_obj_t bus, uint8_t* start_sequence, uint16_t start_sequence_len, 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) {
+ 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->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;
+ 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
+ }
+
+ supervisor_start_terminal(width, height);
+
+ // 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);
+}
+
+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, uint8_t* sequence, uint32_t sequence_len) {
+ uint32_t i = 0;
+ while (i < sequence_len) {
+ uint8_t *cmd = sequence + i;
+ uint8_t data_size = *(cmd + 1);
+ bool delay = (data_size & DELAY) != 0;
+ data_size &= ~DELAY;
+ uint8_t *data = cmd + 2;
+ 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;
+ }
+}
+
+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;
+}
+
+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);
+ 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;
+}
+
+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) {
+ 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);
+ }
+
+ 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;
+ if (pass == 1) {
+ 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.
+ usb_background();
+ }
+ }
+
+ 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) {
+ 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) {
+ 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);
+ }
+ }
+}
+
+void release_epaperdisplay(displayio_epaperdisplay_obj_t* self) {
+ if (self->refreshing) {
+ wait_for_busy(self);
+ 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(self->start_sequence);
+ gc_collect_ptr(self->stop_sequence);
+}
+
+bool 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;
+ if (common_hal_displayio_epaperdisplay_get_time_to_refresh(display) != 0) {
+ return false;
+ }
+ return common_hal_displayio_epaperdisplay_refresh(display);
+ }
+ // Return true if no ePaper displays are available to pretend it was updated.
+ return true;
+}
diff --git a/shared-module/displayio/EPaperDisplay.h b/shared-module/displayio/EPaperDisplay.h
new file mode 100644
index 000000000..d08bed546
--- /dev/null
+++ b/shared-module/displayio/EPaperDisplay.h
@@ -0,0 +1,68 @@
+/*
+ * 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-bindings/pulseio/PWMOut.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;
+ uint8_t* start_sequence;
+ uint32_t start_sequence_len;
+ 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;
+ display_chip_select_behavior_t chip_select;
+} displayio_epaperdisplay_obj_t;
+
+void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t* self);
+void release_epaperdisplay(displayio_epaperdisplay_obj_t* self);
+bool 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/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c
new file mode 100644
index 000000000..17e8cfa1d
--- /dev/null
+++ b/shared-module/displayio/FourWire.c
@@ -0,0 +1,135 @@
+/*
+ * 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->command, command);
+ common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
+ common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
+ common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
+
+ 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(command);
+ 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_time_delay_ms(1);
+ common_hal_digitalio_digitalinout_set_value(&self->reset, true);
+ common_hal_time_delay_ms(1);
+ 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, uint8_t *data, uint32_t data_length) {
+ displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
+ common_hal_digitalio_digitalinout_set_value(&self->command, 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/shared-module/displayio/FourWire.h b/shared-module/displayio/FourWire.h
new file mode 100644
index 000000000..a4260a3ac
--- /dev/null
+++ b/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/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index 655376da0..15cf5b8e4 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -27,79 +27,385 @@
#include "shared-bindings/displayio/Group.h"
#include "py/runtime.h"
-#include "shared-bindings/displayio/Sprite.h"
+#include "shared-bindings/displayio/TileGrid.h"
-void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size) {
- mp_obj_t* children = m_new(mp_obj_t, max_size);
- displayio_group_construct(self, children, max_size);
+#if CIRCUITPY_VECTORIO
+#include "shared-bindings/vectorio/VectorShape.h"
+#endif
+
+
+void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
+ displayio_group_child_t* children = m_new(displayio_group_child_t, max_size);
+ displayio_group_construct(self, children, max_size, 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->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_set_hidden_by_parent(layer, hidden);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_set_hidden_by_parent(layer, hidden);
+ }
+ }
+}
+
+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->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_set_hidden_by_parent(layer, hidden);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_set_hidden_by_parent(layer, hidden);
+ }
+ }
+}
+
+uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
+ return self->scale;
}
-void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer) {
+bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t* area) {
+ bool first = true;
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ displayio_area_t layer_area;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ if (!displayio_tilegrid_get_previous_area(layer, &layer_area)) {
+ continue;
+ }
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ if (!displayio_group_get_previous_area(layer, &layer_area)) {
+ continue;
+ }
+ }
+ if (first) {
+ displayio_area_copy(&layer_area, area);
+ first = false;
+ } else {
+ displayio_area_expand(area, &layer_area);
+ }
+ }
+ if (self->item_removed) {
+ if (first) {
+ displayio_area_copy(&self->dirty_area, area);
+ first = false;
+ } else {
+ displayio_area_expand(area, &self->dirty_area);
+ }
+ }
+ return !first;
+}
+
+static void _update_child_transforms(displayio_group_t* self) {
+ if (!self->in_group) {
+ return;
+ }
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ vectorio_vector_shape_update_transform(layer, &self->absolute_transform);
+ }
+ else
+#endif
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_update_transform(layer, &self->absolute_transform);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_update_transform(layer, &self->absolute_transform);
+ }
+ }
+}
+
+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 mp_obj_t _add_layer(displayio_group_t* self, mp_obj_t layer) {
+ mp_obj_t native_layer;
+#if CIRCUITPY_VECTORIO
+ native_layer = mp_instance_cast_to_native_base(layer, &vectorio_vector_shape_type);
+ if (native_layer != MP_OBJ_NULL) {
+ vectorio_vector_shape_update_transform(native_layer, &self->absolute_transform);
+ return native_layer;
+ }
+#endif
+ native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
+ if (native_layer == MP_OBJ_NULL) {
+ native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
+ if (native_layer == MP_OBJ_NULL) {
+ mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
+ }
+ 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);
+ } else {
+ 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);
+ }
+ return native_layer;
+}
+
+static void _remove_layer(displayio_group_t* self, size_t index) {
+ mp_obj_t layer = self->children[index].native;
+ displayio_area_t layer_area;
+ bool rendered_last_frame = false;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ bool has_dirty_area = vectorio_vector_shape_get_dirty_area(layer, &layer_area);
+ rendered_last_frame = has_dirty_area;
+ vectorio_vector_shape_update_transform(layer, NULL);
+ }
+ else
+#endif
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_t* tilegrid = layer;
+ rendered_last_frame = displayio_tilegrid_get_previous_area(tilegrid, &layer_area);
+ displayio_tilegrid_update_transform(tilegrid, NULL);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ 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_expand(&self->dirty_area, &layer_area);
+ }
+ self->item_removed = true;
+}
+
+void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
if (self->size == self->max_size) {
mp_raise_RuntimeError(translate("Group full"));
}
- self->children[self->size] = layer;
+ mp_obj_t native_layer = _add_layer(self, layer);
+ // Shift everything right.
+ for (size_t i = self->size; i > index; i--) {
+ self->children[i] = self->children[i - 1];
+ }
+ self->children[index].native = native_layer;
+ self->children[index].original = layer;
self->size++;
- self->needs_refresh = true;
}
-mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self) {
- if (self->size == 0) {
- mp_raise_IndexError(translate("Group empty"));
- }
+mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
self->size--;
- mp_obj_t item = self->children[self->size];
- self->children[self->size] = NULL;
- self->needs_refresh = true;
+ mp_obj_t item = self->children[index].original;
+ _remove_layer(self, index);
+
+ // Shift everything left.
+ for (size_t i = index; i < self->size; i++) {
+ self->children[i] = self->children[i + 1];
+ }
+ self->children[self->size].native = NULL;
+ self->children[self->size].original = NULL;
return item;
}
-void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, uint32_t max_size) {
- self->x = 0;
- self->y = 0;
+mp_int_t common_hal_displayio_group_index(displayio_group_t* self, mp_obj_t layer) {
+ for (size_t i = 0; i < self->size; i++) {
+ if (self->children[i].original == layer) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+size_t common_hal_displayio_group_get_len(displayio_group_t* self) {
+ return self->size;
+}
+
+mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index) {
+ return self->children[index].original;
+}
+
+void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer) {
+ mp_obj_t native_layer = _add_layer(self, layer);
+ _remove_layer(self, index);
+ self->children[index].native = native_layer;
+ self->children[index].original = layer;
+}
+
+void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
+ self->x = x;
+ self->y = y;
self->children = child_array;
self->max_size = max_size;
- self->needs_refresh = false;
+ self->item_removed = false;
+ self->scale = scale;
+ self->in_group = false;
}
-bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, uint16_t* pixel) {
- x -= self->x;
- y -= self->y;
+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.
+ bool full_coverage = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
- mp_obj_t layer = self->children[i];
- if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
- if (displayio_sprite_get_pixel(layer, x, y, pixel)) {
- return true;
+ mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ if (vectorio_vector_shape_fill_area(layer, colorspace, area, mask, buffer)) {
+ full_coverage = true;
+ break;
+ }
+ }
+ else
+#endif
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ if (displayio_tilegrid_fill_area(layer, colorspace, area, mask, buffer)) {
+ full_coverage = true;
+ break;
+ }
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ if (displayio_group_fill_area(layer, colorspace, area, mask, buffer)) {
+ full_coverage = true;
+ break;
}
}
- // TODO: Tiled layer
}
- return false;
+ return full_coverage;
}
-bool displayio_group_needs_refresh(displayio_group_t *self) {
- if (self->needs_refresh) {
- return true;
- }
+void displayio_group_finish_refresh(displayio_group_t *self) {
+ self->item_removed = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
- mp_obj_t layer = self->children[i];
- if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
- if (displayio_sprite_needs_refresh(layer)) {
- return true;
- }
+ mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ vectorio_vector_shape_finish_refresh(layer);
+ }
+ else
+#endif
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_finish_refresh(layer);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_finish_refresh(layer);
}
- // TODO: Tiled layer
}
- return false;
}
-void displayio_group_finish_refresh(displayio_group_t *self) {
- self->needs_refresh = false;
+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->size - 1; i >= 0 ; i--) {
- mp_obj_t layer = self->children[i];
- if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
- displayio_sprite_finish_refresh(layer);
+ mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ tail = vectorio_vector_shape_get_refresh_areas(layer, tail);
+ }
+ else
+#endif
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ tail = displayio_tilegrid_get_refresh_areas(layer, tail);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ tail = displayio_group_get_refresh_areas(layer, tail);
}
- // TODO: Tiled layer
}
+
+ return tail;
}
diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h
index 272f3114c..5afaac1ba 100644
--- a/shared-module/displayio/Group.h
+++ b/shared-module/displayio/Group.h
@@ -31,24 +31,37 @@
#include <stdint.h>
#include "py/obj.h"
+#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/Palette.h"
+
+typedef struct {
+ mp_obj_t native;
+ mp_obj_t original;
+} displayio_group_child_t;
typedef struct {
mp_obj_base_t base;
+ displayio_group_child_t* children;
+ 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;
uint16_t size;
uint16_t max_size;
- mp_obj_t* children;
- bool needs_refresh;
+ bool item_removed :1;
+ bool in_group :1;
+ bool hidden :1;
+ bool hidden_by_parent :1;
+ uint8_t padding :4;
} displayio_group_t;
-
-void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size);
-void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer);
-
-void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, uint32_t max_size);
-bool displayio_group_get_pixel(displayio_group_t *group, int16_t x, int16_t y, uint16_t *pixel);
-bool displayio_group_needs_refresh(displayio_group_t *self);
+void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, 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/shared-module/displayio/I2CDisplay.c b/shared-module/displayio/I2CDisplay.c
new file mode 100644
index 000000000..0c8f2e69a
--- /dev/null
+++ b/shared-module/displayio/I2CDisplay.c
@@ -0,0 +1,123 @@
+/*
+ * 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)) {
+ 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);
+ }
+
+ common_hal_reset_pin(self->reset.pin);
+}
+
+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, 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, true);
+ } 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, true);
+ }
+}
+
+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/shared-module/displayio/I2CDisplay.h b/shared-module/displayio/I2CDisplay.h
new file mode 100644
index 000000000..4636c3f73
--- /dev/null
+++ b/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/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c
index 6e4a4c6f0..993fc03de 100644
--- a/shared-module/displayio/OnDiskBitmap.c
+++ b/shared-module/displayio/OnDiskBitmap.c
@@ -38,13 +38,13 @@ static uint32_t read_word(uint16_t* bmp_header, uint16_t index) {
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[24];
+ uint16_t bmp_header[69];
f_rewind(&self->file->fp);
UINT bytes_read;
- if (f_read(&self->file->fp, bmp_header, 48, &bytes_read) != FR_OK) {
+ if (f_read(&self->file->fp, bmp_header, 138, &bytes_read) != FR_OK) {
mp_raise_OSError(MP_EIO);
}
- if (bytes_read != 48 ||
+ if (bytes_read != 138 ||
memcmp(bmp_header, "BM", 2) != 0) {
mp_raise_ValueError(translate("Invalid BMP file"));
}
@@ -53,25 +53,67 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
self->data_offset = read_word(bmp_header, 5);
uint32_t header_size = read_word(bmp_header, 7);
- uint32_t compression = read_word(bmp_header, 15);
- if (!(header_size == 12 || header_size == 40 || header_size == 108 || header_size == 124) ||
- !(compression == 0)) {
- mp_raise_ValueError_varg(translate("Only Windows format, uncompressed BMP supported %d"), header_size);
- }
- // TODO(tannewt): Support bitfield compressed colors since RGB565 can be produced by the GIMP.
uint16_t bits_per_pixel = bmp_header[14];
- if (bits_per_pixel < 24) {
- mp_raise_ValueError_varg(translate("Only true color (24 bpp or higher) BMP supported %x"), bits_per_pixel);
- }
- self->bytes_per_pixel = bits_per_pixel / 8;
+ uint32_t compression = read_word(bmp_header, 15);
+ uint32_t number_of_colors = read_word(bmp_header, 23);
+
+ bool indexed = ((bits_per_pixel <= 8) && (number_of_colors != 0));
+ 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);
- uint32_t byte_width = self->width * self->bytes_per_pixel;
- self->stride = byte_width;
- // Rows are word aligned.
- if (self->stride % 4 != 0) {
- self->stride += 4 - self->stride % 4;
+
+ 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 && self->bits_per_pixel != 1) {
+ uint16_t palette_size = number_of_colors * sizeof(uint32_t);
+ uint16_t palette_offset = 0xe + header_size;
+
+ self->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, self->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"));
+ }
+ } 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);
}
+
}
@@ -80,14 +122,63 @@ uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *s
if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
return 0;
}
- uint32_t location = self->data_offset + (self->height - y) * self->stride + x * self->bytes_per_pixel;
+
+ 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 = 0;
- uint32_t result = f_read(&self->file->fp, &pixel, self->bytes_per_pixel, &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) {
- return pixel;
+ 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;
+
+ uint8_t index = (pixel_data >> ((8 - self->bits_per_pixel) - offset)) & mask;
+ if (self->bits_per_pixel == 1) {
+ if (index == 1) {
+ return 0xFFFFFF;
+ } else {
+ return 0x000000;
+ }
+ }
+ return self->palette_data[index];
+ } 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;
+}
diff --git a/shared-module/displayio/OnDiskBitmap.h b/shared-module/displayio/OnDiskBitmap.h
index 31d6c9550..28426f11b 100644
--- a/shared-module/displayio/OnDiskBitmap.h
+++ b/shared-module/displayio/OnDiskBitmap.h
@@ -40,8 +40,13 @@ typedef struct {
uint16_t height;
uint16_t data_offset;
uint16_t stride;
+ uint32_t r_bitmask;
+ uint32_t g_bitmask;
+ uint32_t b_bitmask;
+ bool bitfield_compressed;
pyb_file_obj_t* file;
- uint8_t bytes_per_pixel;
+ uint8_t bits_per_pixel;
+ uint32_t* palette_data;
} displayio_ondiskbitmap_t;
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H
diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c
index 5f410d786..1ef03aab4 100644
--- a/shared-module/displayio/Palette.c
+++ b/shared-module/displayio/Palette.c
@@ -26,45 +26,70 @@
#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 = (uint32_t *) m_malloc(color_count * sizeof(uint16_t), false);
- uint32_t opaque_byte_count = color_count / 8;
- if (color_count % 8 > 0) {
- opaque_byte_count += 1;
- }
- self->opaque = (uint32_t *) m_malloc(opaque_byte_count, false);
+ 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->opaque[palette_index / 32] &= ~(0x1 << (palette_index % 32));
+ self->colors[palette_index].transparent = false;
}
void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index) {
- self->opaque[palette_index / 32] |= (0x1 << (palette_index % 32));
+ self->colors[palette_index].transparent = true;
+}
+
+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) {
- uint32_t shift = (palette_index % 2) * 16;
- uint32_t masked = self->colors[palette_index / 2] & ~(0xffff << shift);
- uint32_t b5 = (color >> 19);
- uint32_t g6 = (color >> 10) & 0x3f;
- uint32_t r5 = (color >> 3) & 0x1f;
- uint32_t packed = r5 << 11 | g6 << 5 | b5;
- // swap bytes
- packed = __builtin_bswap16(packed);
- self->colors[palette_index / 2] = masked | packed << shift;
+ 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;
}
-bool displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index, uint16_t* color) {
- if (palette_index > self->color_count) {
- return false;
+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 opaque
}
- if ((self->opaque[palette_index / 32] & (0x1 << (palette_index % 32))) != 0) {
- return false;
+
+ 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, luma, color);
+ } else if (colorspace->grayscale) {
+ *color = self->colors[palette_index].luma >> (8 - colorspace->depth);
+ } else {
+ uint16_t packed = self->colors[palette_index].rgb565;
+ if (colorspace->reverse_bytes_in_word) {
+ // swap bytes
+ packed = __builtin_bswap16(packed);
+ }
+ *color = packed;
}
- *color = (self->colors[palette_index / 2] >> (16 * (palette_index % 2))) & 0xffff;
return true;
}
diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h
index 5a5f1d287..da72f250f 100644
--- a/shared-module/displayio/Palette.h
+++ b/shared-module/displayio/Palette.h
@@ -33,14 +33,51 @@
#include "py/obj.h"
typedef struct {
+ uint8_t depth;
+ uint8_t bytes_per_cell;
+ uint8_t tricolor_hue;
+ uint8_t tricolor_luma;
+ 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 {
mp_obj_base_t base;
- uint32_t* opaque;
- uint32_t* colors;
- uint8_t color_count;
+ _displayio_color_t* colors;
+ uint32_t color_count;
bool needs_refresh;
} displayio_palette_t;
-bool displayio_palette_get_color(displayio_palette_t *palette, uint32_t palette_index, uint16_t* color);
+// 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);
diff --git a/shared-module/displayio/Shape.c b/shared-module/displayio/Shape.c
new file mode 100644
index 000000000..ab9ca735b
--- /dev/null
+++ b/shared-module/displayio/Shape.c
@@ -0,0 +1,90 @@
+/*
+ * 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"
+
+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 - 1;
+ }
+ self->half_width = width;
+
+ self->height = height;
+ if (self->mirror_y) {
+ height /= 2;
+ height += self->height % 2 - 1;
+ }
+ 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;
+ }
+}
+
+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"));
+ }
+ uint16_t half_width = self->width / 2 - 1 + self->width % 2;
+ if (self->mirror_x && (start_x > half_width || end_x > half_width)) {
+ mp_raise_ValueError_varg(translate("Maximum x value when mirrored is %d"), half_width);
+ }
+ self->data[2 * y] = start_x;
+ self->data[2 * y + 1] = end_x;
+}
+
+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 - 1 - x;
+ }
+ 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;
+}
diff --git a/shared-module/displayio/Sprite.h b/shared-module/displayio/Shape.h
index dc368c4b7..ca054fe00 100644
--- a/shared-module/displayio/Sprite.h
+++ b/shared-module/displayio/Shape.h
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ * 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
@@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SPRITE_H
-#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SPRITE_H
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H
#include <stdbool.h>
#include <stdint.h>
@@ -34,17 +34,13 @@
typedef struct {
mp_obj_base_t base;
- mp_obj_t bitmap;
- mp_obj_t pixel_shader;
- uint16_t x;
- uint16_t y;
uint16_t width;
uint16_t height;
- bool needs_refresh;
-} displayio_sprite_t;
+ uint16_t half_width;
+ uint16_t half_height;
+ uint16_t* data;
+ bool mirror_x;
+ bool mirror_y;
+} displayio_shape_t;
-bool displayio_sprite_get_pixel(displayio_sprite_t *sprite, int16_t x, int16_t y, uint16_t *pixel);
-bool displayio_sprite_needs_refresh(displayio_sprite_t *self);
-void displayio_sprite_finish_refresh(displayio_sprite_t *self);
-
-#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SPRITE_H
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H
diff --git a/shared-module/displayio/Sprite.c b/shared-module/displayio/Sprite.c
deleted file mode 100644
index 87600f721..000000000
--- a/shared-module/displayio/Sprite.c
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * This file is part of the Micro Python project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "shared-bindings/displayio/Sprite.h"
-
-#include "shared-bindings/displayio/Bitmap.h"
-#include "shared-bindings/displayio/ColorConverter.h"
-#include "shared-bindings/displayio/OnDiskBitmap.h"
-#include "shared-bindings/displayio/Palette.h"
-
-void common_hal_displayio_sprite_construct(displayio_sprite_t *self, mp_obj_t bitmap,
- mp_obj_t pixel_shader, uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
- self->width = width;
- self->height = height;
- self->bitmap = bitmap;
- self->pixel_shader = pixel_shader;
- self->x = x;
- self->y = y;
-}
-
-void common_hal_displayio_sprite_get_position(displayio_sprite_t *self, int16_t* x, int16_t* y) {
- *x = self->x;
- *y = self->y;
-}
-
-void common_hal_displayio_sprite_set_position(displayio_sprite_t *self, int16_t x, int16_t y) {
- self->x = x;
- self->y = y;
- self->needs_refresh = true;
-}
-
-
-mp_obj_t common_hal_displayio_sprite_get_pixel_shader(displayio_sprite_t *self) {
- return self->pixel_shader;
-}
-
-void common_hal_displayio_sprite_set_pixel_shader(displayio_sprite_t *self, mp_obj_t pixel_shader) {
- self->pixel_shader = pixel_shader;
- self->needs_refresh = true;
-}
-
-bool displayio_sprite_get_pixel(displayio_sprite_t *self, int16_t x, int16_t y, uint16_t* pixel) {
- x -= self->x;
- y -= self->y;
- if (y < 0 || y >= self->height || x >= self->width || x < 0) {
- return false;
- }
- uint32_t value = 0;
- if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
- value = common_hal_displayio_bitmap_get_pixel(self->bitmap, x, y);
- } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
- value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, x, y);
- }
-
- if (self->pixel_shader == mp_const_none) {
- *pixel = value;
- return true;
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_get_color(self->pixel_shader, value, pixel)) {
- return true;
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
- return true;
- }
-
- return false;
-}
-
-bool displayio_sprite_needs_refresh(displayio_sprite_t *self) {
- return self->needs_refresh || displayio_palette_needs_refresh(self->pixel_shader);
-}
-
-void displayio_sprite_finish_refresh(displayio_sprite_t *self) {
- self->needs_refresh = false;
- displayio_palette_finish_refresh(self->pixel_shader);
- // TODO(tannewt): We could double buffer changes to position and move them over here.
- // That way they won't change during a refresh and tear.
-}
diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c
new file mode 100644
index 000000000..2766cbecd
--- /dev/null
+++ b/shared-module/displayio/TileGrid.c
@@ -0,0 +1,587 @@
+/*
+ * 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;
+}
+
+
+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;
+}
+
+void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden) {
+ self->hidden_by_parent = hidden;
+}
+
+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;
+}
+
+void _update_current_x(displayio_tilegrid_t *self) {
+ int16_t width;
+ if (self->transpose_xy) {
+ width = self->pixel_height;
+ } else {
+ width = self->pixel_width;
+ }
+ if (self->absolute_transform->transpose_xy) {
+ self->current_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * self->x;
+ self->current_area.y2 = self->absolute_transform->y + self->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 = self->absolute_transform->x + self->absolute_transform->dx * self->x;
+ self->current_area.x2 = self->absolute_transform->x + self->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;
+ }
+ }
+}
+
+void _update_current_y(displayio_tilegrid_t *self) {
+ int16_t height;
+ if (self->transpose_xy) {
+ height = self->pixel_width;
+ } else {
+ height = self->pixel_height;
+ }
+ if (self->absolute_transform->transpose_xy) {
+ self->current_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * self->y;
+ self->current_area.x2 = self->absolute_transform->x + self->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 = self->absolute_transform->y + self->absolute_transform->dy * self->y;
+ self->current_area.y2 = self->absolute_transform->y + self->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;
+}
+
+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;
+}
+
+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_expand(&self->dirty_area, &temp_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;
+
+ //uint32_t value = 0;
+ 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 == 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;
+ }
+ }
+ }
+ }
+ 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)) {
+ // TODO: Support shape changes.
+ } 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;
+ }
+ }
+ }
+
+ 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) {
+ if (self->absolute_transform->transpose_xy) {
+ int16_t x1 = self->dirty_area.x1;
+ self->dirty_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * (self->y + self->dirty_area.y1);
+ self->dirty_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * (self->x + x1);
+ int16_t x2 = self->dirty_area.x2;
+ self->dirty_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->y + self->dirty_area.y2);
+ self->dirty_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->x + x2);
+ } else {
+ self->dirty_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * (self->x + self->dirty_area.x1);
+ self->dirty_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * (self->y + self->dirty_area.y1);
+ self->dirty_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->x + self->dirty_area.x2);
+ self->dirty_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->y + self->dirty_area.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/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h
new file mode 100644
index 000000000..e97d3dfd4
--- /dev/null
+++ b/shared-module/displayio/TileGrid.h
@@ -0,0 +1,88 @@
+/*
+ * 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/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index df9c2963a..c898bbb98 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -1,80 +1,394 @@
-#include "shared-bindings/displayio/FourWire.h"
-extern displayio_fourwire_obj_t board_display_obj;
+#include <string.h>
-void start_region_update(displayio_fourwire_obj_t* display, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
- // TODO delegate between different display types
- displayio_fourwire_start_region_update(display, x0, y0, x1, y1);
+#include "shared-module/displayio/__init__.h"
+
+#include "lib/utils/interrupt_char.h"
+#include "py/reload.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/autoreload.h"
+#include "supervisor/shared/display.h"
+#include "supervisor/memory.h"
+
+#include "supervisor/spi_flash_api.h"
+#include "py/mpconfig.h"
+
+primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
+
+#if CIRCUITPY_RGBMATRIX
+STATIC bool any_display_uses_this_rgbmatrix(rgbmatrix_rgbmatrix_obj_t* pm) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
+ if (display->framebuffer == pm) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+#endif
+
+// Check for recursive calls to displayio_background.
+bool displayio_background_in_progress = false;
+
+void displayio_background(void) {
+ if (mp_hal_is_interrupted()) {
+ return;
+ }
+ if (reload_requested) {
+ // Reload is about to happen, so don't redisplay.
+ return;
+ }
+
+ if (displayio_background_in_progress) {
+ // Don't allow recursive calls to this routine.
+ return;
+ }
+
+ displayio_background_in_progress = true;
+
+ 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);
+ }
+ }
+
+ // All done.
+ displayio_background_in_progress = false;
+}
+
+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);
+ } else if (bus_type == &displayio_parallelbus_type) {
+ common_hal_displayio_parallelbus_deinit(&displays[i].parallel_bus);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (bus_type == &rgbmatrix_RGBMatrix_type) {
+ common_hal_rgbmatrix_rgbmatrix_deinit(&displays[i].rgbmatrix);
+#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 (((uint32_t) fourwire->bus) < ((uint32_t) &displays) ||
+ ((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) {
+ busio_spi_obj_t* original_spi = fourwire->bus;
+ #if BOARD_SPI
+ // We don't need to move original_spi if it is the board.SPI object because it is
+ // statically allocated already. (Doing so would also make it impossible to reference in
+ // a subsequent VM run.)
+ if (original_spi == common_hal_board_get_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 (((uint32_t) i2c->bus) < ((uint32_t) &displays) ||
+ ((uint32_t) i2c->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) {
+ busio_i2c_obj_t* original_i2c = i2c->bus;
+ #if 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 (original_i2c == common_hal_board_get_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_rgbmatrix(pm)) {
+ common_hal_rgbmatrix_rgbmatrix_deinit(pm);
+ }
+#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_obj_t* display = &displays[i].framebuffer_display;
+ display->auto_refresh = true;
+ common_hal_framebufferio_framebufferdisplay_show(display, NULL);
+#endif
+ }
+ }
}
-void finish_region_update(displayio_fourwire_obj_t* display) {
- // TODO delegate between different display types
- displayio_fourwire_finish_region_update(display);
+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 (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);
+ }
+ }
}
-void finish_refresh(displayio_fourwire_obj_t* display) {
- // TODO delegate between different display types
- displayio_fourwire_finish_refresh(display);
+void displayio_area_expand(displayio_area_t* original, const displayio_area_t* addition) {
+ if (addition->x1 < original->x1) {
+ original->x1 = addition->x1;
+ }
+ if (addition->y1 < original->y1) {
+ original->y1 = addition->y1;
+ }
+ if (addition->x2 > original->x2) {
+ original->x2 = addition->x2;
+ }
+ if (addition->y2 > original->y2) {
+ original->y2 = addition->y2;
+ }
}
-bool frame_queued(displayio_fourwire_obj_t* display) {
- // TODO delegate between different display types
- return displayio_fourwire_frame_queued(display);
+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;
}
-bool refresh_queued(displayio_fourwire_obj_t* display) {
- // TODO delegate between different display types
- return displayio_fourwire_refresh_queued(display);
+void displayio_area_scale(displayio_area_t* area, uint16_t scale) {
+ area->x1 *= scale;
+ area->y1 *= scale;
+ area->x2 *= scale;
+ area->y2 *= scale;
}
-bool send_pixels(displayio_fourwire_obj_t* display, uint32_t* pixels, uint32_t length) {
- // TODO delegate between different display types
- return displayio_fourwire_send_pixels(display, pixels, length);
+void displayio_area_shift(displayio_area_t* area, int16_t dx, int16_t dy) {
+ area->x1 += dx;
+ area->y1 += dy;
+ area->x2 += dx;
+ area->y2 += dy;
}
-void displayio_refresh_display(void) {
- displayio_fourwire_obj_t* display = &board_display_obj;
+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;
+}
- if (!frame_queued(display)) {
- return;
+void displayio_area_union(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* u) {
+ u->x1 = a->x1;
+ if (b->x1 < u->x1) {
+ u->x1 = b->x1;
+ }
+ u->x2 = a->x2;
+ if (b->x2 > u->x2) {
+ u->x2 = b->x2;
}
- if (refresh_queued(display)) {
- // We compute the pixels
- uint16_t x0 = 0;
- uint16_t y0 = 0;
- uint16_t x1 = display->width;
- uint16_t y1 = display->height;
- size_t index = 0;
- //size_t row_size = (x1 - x0);
- uint16_t buffer_size = 256;
- uint32_t buffer[buffer_size / 2];
- start_region_update(display, x0, y0, x1, y1);
- for (uint16_t y = y0; y < y1; ++y) {
- for (uint16_t x = x0; x < x1; ++x) {
- uint16_t* pixel = &(((uint16_t*)buffer)[index]);
- *pixel = 0;
- if (display->current_group != NULL) {
- displayio_group_get_pixel(display->current_group, x, y, pixel);
- }
+ u->y1 = a->y1;
+ if (b->y1 < u->y1) {
+ u->y1 = b->y1;
+ }
+ u->y2 = a->y2;
+ if (b->y2 > u->y2) {
+ u->y2 = b->y2;
+ }
+}
- index += 1;
- // The buffer is full, send it.
- if (index >= buffer_size) {
- if (!send_pixels(display, buffer, buffer_size / 2)) {
- finish_region_update(display);
- return;
- }
- index = 0;
- }
- }
+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);
+ }
+}
+
+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];
}
- // Send the remaining data.
- if (index && !send_pixels(display, buffer, index * 2)) {
- finish_region_update(display);
- return;
+ }
+ 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_type = displays[i].display.base.type;
+ if (display_type == NULL || display_type == &mp_type_NoneType) {
+ return &displays[i];
}
- finish_region_update(display);
}
- finish_refresh(display);
+ 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/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h
index 556af430d..9eb10c28b 100644
--- a/shared-module/displayio/__init__.h
+++ b/shared-module/displayio/__init__.h
@@ -24,9 +24,49 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
-#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
-void displayio_refresh_display(void);
+#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"
+#include "shared-bindings/displayio/ParallelBus.h"
+#include "shared-bindings/rgbmatrix/RGBMatrix.h"
-#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
+typedef struct {
+ union {
+ displayio_fourwire_obj_t fourwire_bus;
+ displayio_i2cdisplay_obj_t i2cdisplay_bus;
+ displayio_parallelbus_obj_t parallel_bus;
+#if CIRCUITPY_RGBMATRIX
+ rgbmatrix_rgbmatrix_obj_t rgbmatrix;
+#endif
+ };
+ union {
+ 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/shared-module/displayio/area.h b/shared-module/displayio/area.h
new file mode 100644
index 000000000..ec7c389b4
--- /dev/null
+++ b/shared-module/displayio/area.h
@@ -0,0 +1,73 @@
+/*
+ * 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
+
+// Implementations are in __init__.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;
+
+void displayio_area_union(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* u);
+void displayio_area_expand(displayio_area_t* original, const displayio_area_t* addition);
+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/shared-module/displayio/display_core.c b/shared-module/displayio/display_core.c
new file mode 100644
index 000000000..43f2d1937
--- /dev/null
+++ b/shared-module/displayio/display_core.c
@@ -0,0 +1,351 @@
+/*
+ * 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"
+#include "shared-bindings/displayio/ParallelBus.h"
+#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.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 (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
+ self->bus_reset = common_hal_displayio_parallelbus_reset;
+ self->bus_free = common_hal_displayio_parallelbus_bus_free;
+ self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction;
+ self->send = common_hal_displayio_parallelbus_send;
+ self->end_transaction = common_hal_displayio_parallelbus_end_transaction;
+ } else if (MP_OBJ_IS_TYPE(bus, &displayio_fourwire_type)) {
+ self->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;
+
+
+ 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) {
+ if (!circuitpython_splash.in_group) {
+ root_group = &circuitpython_splash;
+ } else if (self->current_group == &circuitpython_splash) {
+ return true;
+ }
+ }
+ 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_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) {
+ uint16_t x1 = area->x1;
+ uint16_t x2 = area->x2;
+ uint16_t y1 = area->y1;
+ uint16_t y2 = area->y2;
+ // 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;
+ }
+ }
+
+ 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 + self->colstart;
+ data[data_length++] = x2 - 1 + self->colstart;
+ } else {
+ x1 += self->colstart;
+ x2 += self->colstart - 1;
+ data[data_length++] = x1 >> 8;
+ data[data_length++] = x1 & 0xff;
+ data[data_length++] = x2 >> 8;
+ data[data_length++] = x2 & 0xff;
+ }
+ 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 + self->rowstart;
+ data[data_length++] = y2 - 1 + self->rowstart;
+ } else {
+ y1 += self->rowstart;
+ y2 += self->rowstart - 1;
+ data[data_length++] = y1 >> 8;
+ data[data_length++] = y1 & 0xff;
+ data[data_length++] = y2 >> 8;
+ data[data_length++] = y2 & 0xff;
+ }
+ 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);
+ }
+}
+
+void displayio_display_core_start_refresh(displayio_display_core_t* self) {
+ self->last_refresh = supervisor_ticks_ms64();
+}
+
+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->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/shared-module/displayio/display_core.h b/shared-module/displayio/display_core.h
new file mode 100644
index 000000000..e4fd62d4a
--- /dev/null
+++ b/shared-module/displayio/display_core.h
@@ -0,0 +1,90 @@
+/*
+ * 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.
+} 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);
+
+void release_display_core(displayio_display_core_t* self);
+
+void 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