summaryrefslogtreecommitdiff
path: root/shared-module/displayio
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module/displayio')
-rw-r--r--shared-module/displayio/ColorConverter.c35
-rw-r--r--shared-module/displayio/ColorConverter.h4
-rw-r--r--shared-module/displayio/Display.c171
-rw-r--r--shared-module/displayio/Display.h4
-rw-r--r--shared-module/displayio/FourWire.c21
-rw-r--r--shared-module/displayio/FourWire.h6
-rw-r--r--shared-module/displayio/Group.c6
-rw-r--r--shared-module/displayio/Group.h3
-rw-r--r--shared-module/displayio/I2CDisplay.c105
-rw-r--r--shared-module/displayio/I2CDisplay.h41
-rw-r--r--shared-module/displayio/Palette.c50
-rw-r--r--shared-module/displayio/Palette.h21
-rw-r--r--shared-module/displayio/TileGrid.c52
-rw-r--r--shared-module/displayio/TileGrid.h3
-rw-r--r--shared-module/displayio/__init__.c135
-rw-r--r--shared-module/displayio/__init__.h2
16 files changed, 497 insertions, 162 deletions
diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c
index 3928e115a..6b9bd5840 100644
--- a/shared-module/displayio/ColorConverter.c
+++ b/shared-module/displayio/ColorConverter.c
@@ -29,15 +29,36 @@
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;
+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;
uint32_t packed = r5 << 11 | g6 << 5 | b5;
// swap bytes
- *output_color = __builtin_bswap16(packed);
- return true;
+ return __builtin_bswap16(packed);
+}
+
+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;
+}
+
+bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) {
+ if (colorspace->depth == 16) {
+ *output_color = displayio_colorconverter_compute_rgb565(input_color);
+ return true;
+ } else if (colorspace->grayscale && colorspace->depth <= 8) {
+ uint8_t luma = displayio_colorconverter_compute_luma(input_color);
+ *output_color = luma >> (8 - colorspace->depth);
+ return true;
+ }
+ return false;
+}
+
+void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) {
+ displayio_colorconverter_convert(self, colorspace, input_color, output_color);
}
// Currently no refresh logic is needed for a ColorConverter.
diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h
index 7f3c1a0a0..c48b98e6d 100644
--- a/shared-module/displayio/ColorConverter.h
+++ b/shared-module/displayio/ColorConverter.h
@@ -31,6 +31,7 @@
#include <stdint.h>
#include "py/obj.h"
+#include "shared-module/displayio/Palette.h"
typedef struct {
mp_obj_base_t base;
@@ -38,5 +39,8 @@ typedef struct {
bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self);
void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self);
+bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color);
+uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888);
+uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888);
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c
index 0ae88300a..a8515b9e5 100644
--- a/shared-module/displayio/Display.c
+++ b/shared-module/displayio/Display.c
@@ -28,6 +28,7 @@
#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"
@@ -35,6 +36,7 @@
#include "supervisor/shared/display.h"
#include <stdint.h>
+#include <string.h>
#include "tick.h"
@@ -42,11 +44,14 @@
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, uint8_t set_column_command, uint8_t set_row_command,
+ uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row,
+ 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, mp_float_t brightness, bool auto_brightness,
+ 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) {
- self->color_depth = color_depth;
+ self->colorspace.depth = color_depth;
+ self->colorspace.grayscale = grayscale;
+ self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row;
self->set_column_command = set_column_command;
self->set_row_command = set_row_command;
self->write_ram_command = write_ram_command;
@@ -54,6 +59,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
self->current_group = NULL;
self->colstart = colstart;
self->rowstart = rowstart;
+ self->brightness_command = brightness_command;
self->auto_brightness = auto_brightness;
self->data_as_commands = data_as_commands;
self->single_byte_bounds = single_byte_bounds;
@@ -66,6 +72,10 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
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->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"));
}
@@ -83,13 +93,13 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
bool delay = (data_size & DELAY) != 0;
data_size &= ~DELAY;
uint8_t *data = cmd + 2;
- self->send(self->bus, true, cmd, 1);
if (self->data_as_commands) {
- // Loop through each parameter to force a CS toggle
- for (uint32_t j=0; j < data_size; j++) {
- self->send(self->bus, true, data + j, 1);
- }
+ uint8_t full_command[data_size + 1];
+ full_command[0] = cmd[0];
+ memcpy(full_command + 1, data, data_size);
+ self->send(self->bus, true, full_command, data_size + 1);
} else {
+ self->send(self->bus, true, cmd, 1);
self->send(self->bus, false, data, data_size);
}
uint16_t delay_length_ms = 10;
@@ -141,11 +151,14 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
} else {
self->backlight_pwm.base.type = &pulseio_pwmout_type;
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
- if (!self->auto_brightness) {
- common_hal_displayio_display_set_brightness(self, brightness);
- }
}
}
+ 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;
+ }
self->area.x1 = 0;
self->area.y1 = 0;
@@ -234,17 +247,7 @@ void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* s
}
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) {
- if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
- uint16_t duty_cycle = common_hal_pulseio_pwmout_get_duty_cycle(&self->backlight_pwm);
- return duty_cycle / ((mp_float_t) 0xffff);
- } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
- if (common_hal_digitalio_digitalinout_get_value(&self->backlight_inout)) {
- return 1.0;
- } else {
- return 0.0;
- }
- }
- return -1.0;
+ return self->current_brightness;
}
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness) {
@@ -256,8 +259,26 @@ bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self,
} 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 = self->begin_transaction(self->bus);
+ if (ok) {
+ if (self->data_as_commands) {
+ uint8_t set_brightness[2] = {self->brightness_command, (uint8_t) (0xff * brightness)};
+ self->send(self->bus, true, set_brightness, 2);
+ } else {
+ uint8_t command = self->brightness_command;
+ uint8_t hex_brightness = 0xff * brightness;
+ self->send(self->bus, true, &command, 1);
+ self->send(self->bus, false, &hex_brightness, 1);
+ }
+ self->end_transaction(self->bus);
+ }
+
}
self->updating_backlight = false;
+ if (ok) {
+ self->current_brightness = brightness;
+ }
return ok;
}
@@ -269,35 +290,64 @@ void displayio_display_end_transaction(displayio_display_obj_t* self) {
self->end_transaction(self->bus);
}
-void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
- self->send(self->bus, true, &self->set_column_command, 1);
- bool isCommand = self->data_as_commands;
- if (self->single_byte_bounds) {
- uint8_t data[2];
- data[0] = x0 + self->colstart;
- data[1] = x1 - 1 + self->colstart;
- self->send(self->bus, isCommand, (uint8_t*) data, 2);
- } else {
- uint16_t data[2];
- data[0] = __builtin_bswap16(x0 + self->colstart);
- data[1] = __builtin_bswap16(x1 - 1 + self->colstart);
- self->send(self->bus, isCommand, (uint8_t*) data, 4);
+void displayio_display_set_region_to_update(displayio_display_obj_t* self, 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;
+ x2 /= pixels_per_byte;
+ } else {
+ y1 /= pixels_per_byte;
+ y2 /= pixels_per_byte;
+ }
+ }
+
+ // Set column.
+ uint8_t data[5];
+ data[0] = self->set_column_command;
+ uint8_t data_length = 1;
+ if (!self->data_as_commands) {
+ self->send(self->bus, true, data, 1);
+ data_length = 0;
}
- self->send(self->bus, true, &self->set_row_command, 1);
if (self->single_byte_bounds) {
- uint8_t data[2];
- data[0] = y0 + self->rowstart;
- data[1] = y1 - 1 + self->rowstart;
- self->send(self->bus, isCommand, (uint8_t*) data, 2);
+ data[data_length++] = x1 + self->colstart;
+ data[data_length++] = x2 - 1 + self->colstart;
+ data_length += 2;
} else {
- uint16_t data[2];
- data[0] = __builtin_bswap16(y0 + self->rowstart);
- data[1] = __builtin_bswap16(y1 - 1 + self->rowstart);
- self->send(self->bus, isCommand, (uint8_t*) data, 4);
+ 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, self->data_as_commands, data, data_length);
+
+ // Set row.
+ data[0] = self->set_row_command;
+ data_length = 1;
if (!self->data_as_commands) {
- self->send(self->bus, true, &self->write_ram_command, 1);
+ self->send(self->bus, true, data, 1);
+ data_length = 0;
}
+ if (self->single_byte_bounds) {
+ 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, self->data_as_commands, data, data_length);
}
void displayio_display_start_refresh(displayio_display_obj_t* self) {
@@ -322,6 +372,9 @@ void displayio_display_finish_refresh(displayio_display_obj_t* self) {
}
void displayio_display_send_pixels(displayio_display_obj_t* self, uint8_t* pixels, uint32_t length) {
+ if (!self->data_as_commands) {
+ self->send(self->bus, true, &self->write_ram_command, 1);
+ }
self->send(self->bus, false, pixels, length);
}
@@ -349,9 +402,33 @@ void release_display(displayio_display_obj_t* self) {
}
bool displayio_display_fill_area(displayio_display_obj_t *self, displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
- return displayio_group_fill_area(self->current_group, area, mask, buffer);
+ return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer);
}
bool displayio_display_clip_area(displayio_display_obj_t *self, const displayio_area_t* area, displayio_area_t* clipped) {
- return displayio_area_compute_overlap(&self->area, area, 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;
+ 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.h b/shared-module/displayio/Display.h
index fa8902ced..96d1c06d5 100644
--- a/shared-module/displayio/Display.h
+++ b/shared-module/displayio/Display.h
@@ -52,11 +52,13 @@ typedef struct {
uint64_t last_backlight_refresh;
displayio_buffer_transform_t transform;
displayio_area_t area;
+ mp_float_t current_brightness;
uint16_t width;
uint16_t height;
- uint16_t color_depth;
+ _displayio_colorspace_t colorspace;
int16_t colstart;
int16_t rowstart;
+ uint16_t brightness_command;
uint8_t set_column_command;
uint8_t set_row_command;
uint8_t write_ram_command;
diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c
index d38da56fc..913635db8 100644
--- a/shared-module/displayio/FourWire.c
+++ b/shared-module/displayio/FourWire.c
@@ -59,6 +59,11 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
common_hal_digitalio_digitalinout_construct(&self->reset, reset);
common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL);
never_reset_pin_number(reset->number);
+
+ common_hal_digitalio_digitalinout_set_value(&self->reset, false);
+ common_hal_mcu_delay_us(10);
+ common_hal_digitalio_digitalinout_set_value(&self->reset, true);
+ common_hal_mcu_delay_us(10);
}
never_reset_pin_number(command->number);
@@ -88,13 +93,19 @@ bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) {
void common_hal_displayio_fourwire_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) {
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
+ common_hal_digitalio_digitalinout_set_value(&self->command, !command);
if (command) {
- 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);
+ // 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);
}
- common_hal_digitalio_digitalinout_set_value(&self->command, !command);
- common_hal_busio_spi_write(self->bus, data, data_length);
}
void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) {
diff --git a/shared-module/displayio/FourWire.h b/shared-module/displayio/FourWire.h
index 743139e62..a4260a3ac 100644
--- a/shared-module/displayio/FourWire.h
+++ b/shared-module/displayio/FourWire.h
@@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H
-#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H
+#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"
@@ -43,4 +43,4 @@ typedef struct {
uint8_t phase;
} displayio_fourwire_obj_t;
-#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index 15060e87b..38418a029 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -270,19 +270,19 @@ void displayio_group_construct(displayio_group_t* self, displayio_group_child_t*
self->in_group = false;
}
-bool displayio_group_fill_area(displayio_group_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t* buffer) {
+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].native;
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
- if (displayio_tilegrid_fill_area(layer, area, mask, buffer)) {
+ 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, area, mask, buffer)) {
+ if (displayio_group_fill_area(layer, colorspace, area, mask, buffer)) {
full_coverage = true;
break;
}
diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h
index 7ce19a4cd..4e2308e56 100644
--- a/shared-module/displayio/Group.h
+++ b/shared-module/displayio/Group.h
@@ -32,6 +32,7 @@
#include "py/obj.h"
#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/Palette.h"
typedef struct {
mp_obj_t native;
@@ -54,7 +55,7 @@ typedef struct {
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);
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_area_t* area, uint32_t* mask, uint32_t *buffer);
+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);
diff --git a/shared-module/displayio/I2CDisplay.c b/shared-module/displayio/I2CDisplay.c
new file mode 100644
index 000000000..9d8949528
--- /dev/null
+++ b/shared-module/displayio/I2CDisplay.c
@@ -0,0 +1,105 @@
+/*
+ * 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/__init__.h"
+#include "shared-bindings/time/__init__.h"
+
+#include "tick.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) {
+
+ // 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;
+
+ if (reset != NULL) {
+ common_hal_digitalio_digitalinout_construct(&self->reset, reset);
+ common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL);
+ never_reset_pin_number(reset->number);
+
+ common_hal_digitalio_digitalinout_set_value(&self->reset, false);
+ common_hal_mcu_delay_us(1);
+ common_hal_digitalio_digitalinout_set_value(&self->reset, true);
+ }
+}
+
+void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t* self) {
+ if (self->bus == &self->inline_bus) {
+ common_hal_busio_i2c_deinit(self->bus);
+ }
+
+ reset_pin_number(self->reset.pin->number);
+}
+
+bool common_hal_displayio_i2cdisplay_begin_transaction(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;
+ }
+ return true;
+}
+
+void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) {
+ displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj);
+ if (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/Palette.c b/shared-module/displayio/Palette.c
index 8dc6e766b..444151b3c 100644
--- a/shared-module/displayio/Palette.c
+++ b/shared-module/displayio/Palette.c
@@ -26,49 +26,49 @@
#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 r5 = (color >> 19);
- uint32_t g6 = (color >> 10) & 0x3f;
- uint32_t b5 = (color >> 3) & 0x1f;
- uint32_t packed = r5 << 11 | g6 << 5 | b5;
- // swap bytes
- packed = __builtin_bswap16(packed);
- uint32_t final_color = masked | packed << shift;
- if (self->colors[palette_index / 2] == final_color) {
+ if (self->colors[palette_index].rgb888 == color) {
return;
}
- self->colors[palette_index / 2] = final_color;
+ 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);
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->grayscale) {
+ *color = self->colors[palette_index].luma >> (8 - colorspace->depth);
+ } else {
+ *color = self->colors[palette_index].rgb565;
}
- *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 3813e8bdd..19c05baf5 100644
--- a/shared-module/displayio/Palette.h
+++ b/shared-module/displayio/Palette.h
@@ -33,14 +33,29 @@
#include "py/obj.h"
typedef struct {
+ uint8_t depth;
+ bool grayscale;
+ bool pixels_in_byte_share_row;
+ uint8_t hue;
+} _displayio_colorspace_t;
+
+typedef struct {
+ uint32_t rgb888;
+ uint16_t rgb565;
+ uint8_t luma;
+ bool transparent; // This may have additional bits added later for blending.
+} _displayio_color_t;
+
+typedef struct {
mp_obj_base_t base;
- uint32_t* opaque;
- uint32_t* colors;
+ _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/TileGrid.c b/shared-module/displayio/TileGrid.c
index 0cd00e0ce..88873d3a9 100644
--- a/shared-module/displayio/TileGrid.c
+++ b/shared-module/displayio/TileGrid.c
@@ -291,7 +291,7 @@ void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint
self->full_change = true;
}
-bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
+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) {
@@ -371,14 +371,15 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_ar
y_shift = temp_shift;
}
+ uint8_t pixels_per_byte = 8 / colorspace->depth;
for (int16_t y = start_y; y < end_y; y++) {
- int16_t row_start = start + (y - start_y + y_shift) * y_stride;
+ int16_t row_start = start + (y - start_y + y_shift) * y_stride; // in pixels
int16_t local_y = y / self->absolute_transform->scale;
for (int16_t x = start_x; x < end_x; x++) {
// Compute the destination pixel in the buffer and mask based on the transformations.
- int16_t offset = row_start + (x - start_x + x_shift) * x_stride;
+ int16_t offset = row_start + (x - start_x + x_shift) * x_stride; // in pixels
- // This is super useful for debugging out range accesses. Uncomment to use.
+ // 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");
// }
@@ -404,23 +405,38 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_ar
value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y);
}
- uint16_t* pixel = ((uint16_t*) buffer) + offset;
+ uint32_t pixel;
+ bool opaque = true;
if (self->pixel_shader == mp_const_none) {
- *pixel = value;
- return true;
+ pixel = value;
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
- if (!displayio_palette_get_color(self->pixel_shader, value, pixel)) {
- // A pixel is transparent so we haven't fully covered the area ourselves.
- full_coverage = false;
- } else {
- mask[offset / 32] |= 1 << (offset % 32);
- }
+ opaque = displayio_palette_get_color(self->pixel_shader, colorspace, value, &pixel);
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
- if (!common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
- // A pixel is transparent so we haven't fully covered the area ourselves.
- full_coverage = false;
- } else {
- mask[offset / 32] |= 1 << (offset % 32);
+ opaque = displayio_colorconverter_convert(self->pixel_shader, colorspace, value, &pixel);
+ }
+ if (!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) = pixel;
+ } else if (colorspace->depth == 8) {
+ *(((uint8_t*) buffer) + offset) = 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*)buffer)[offset / pixels_per_byte] |= pixel << ((offset % pixels_per_byte) * colorspace->depth);
}
}
}
diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h
index 4d97eccc2..fb6033e9c 100644
--- a/shared-module/displayio/TileGrid.h
+++ b/shared-module/displayio/TileGrid.h
@@ -32,6 +32,7 @@
#include "py/obj.h"
#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/Palette.h"
typedef struct {
mp_obj_base_t base;
@@ -71,7 +72,7 @@ displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel
// 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_area_t* area, uint32_t* mask, uint32_t *buffer);
+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
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index b6709e0eb..97060aaeb 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -19,9 +19,10 @@
#include "supervisor/usb.h"
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
+uint32_t frame_count = 0;
bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area) {
- uint16_t buffer_size = 512;
+ 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.
@@ -30,15 +31,36 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area
}
uint16_t subrectangles = 1;
uint16_t rows_per_buffer = displayio_area_height(&clipped);
- if (displayio_area_size(area) > buffer_size) {
- rows_per_buffer = buffer_size / displayio_area_width(&clipped);
+ uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / display->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 (display->colorspace.depth < 8 && !display->colorspace.pixels_in_byte_share_row) {
+ uint8_t pixels_per_byte = 8 / display->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++;
}
- buffer_size = rows_per_buffer * displayio_area_width(&clipped);
+ 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;
+ }
}
- uint32_t buffer[buffer_size / 2];
+
+ // 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];
uint16_t remaining_rows = displayio_area_height(&clipped);
for (uint16_t j = 0; j < subrectangles; j++) {
@@ -54,37 +76,30 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area
remaining_rows -= rows_per_buffer;
displayio_display_begin_transaction(display);
- displayio_display_set_region_to_update(display, subrectangle.x1, subrectangle.y1,
- subrectangle.x2, subrectangle.y2);
+ displayio_display_set_region_to_update(display, &subrectangle);
displayio_display_end_transaction(display);
- uint32_t mask[(buffer_size / 32) + 1];
- for (uint16_t k = 0; k < (buffer_size / 32) + 1; k++) {
- mask[k] = 0x00000000;
+ uint16_t subrectangle_size_bytes;
+ if (display->colorspace.depth >= 8) {
+ subrectangle_size_bytes = displayio_area_size(&subrectangle) * (display->colorspace.depth / 8);
+ } else {
+ subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / display->colorspace.depth);
}
- bool full_coverage = displayio_display_fill_area(display, &subrectangle, mask, buffer);
- if (!full_coverage) {
- uint32_t index = 0;
- uint32_t current_mask = 0;
- for (int16_t y = subrectangle.y1; y < subrectangle.y2; y++) {
- for (int16_t x = subrectangle.x1; x < subrectangle.x2; x++) {
- if (index % 32 == 0) {
- current_mask = mask[index / 32];
- }
- if ((current_mask & (1 << (index % 32))) == 0) {
- ((uint16_t*) buffer)[index] = 0x0000;
- }
- index++;
- }
- }
+ for (uint16_t k = 0; k < mask_length; k++) {
+ mask[k] = 0x00000000;
+ }
+ for (uint16_t k = 0; k < buffer_size; k++) {
+ buffer[k] = 0x00000000;
}
+ displayio_display_fill_area(display, &subrectangle, mask, buffer);
+
if (!displayio_display_begin_transaction(display)) {
// Can't acquire display bus; skip the rest of the data. Try next display.
return false;
}
- displayio_display_send_pixels(display, (uint8_t*) buffer, displayio_area_size(&subrectangle) * sizeof(uint16_t));
+ displayio_display_send_pixels(display, (uint8_t*) buffer, subrectangle_size_bytes);
displayio_display_end_transaction(display);
// TODO(tannewt): Make refresh displays faster so we don't starve other
@@ -96,7 +111,6 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area
// Check for recursive calls to displayio_refresh_displays.
bool refresh_displays_in_progress = false;
-uint32_t frame_count = 0;
void displayio_refresh_displays(void) {
if (mp_hal_is_interrupted()) {
@@ -153,6 +167,8 @@ void common_hal_displayio_release_displays(void) {
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);
}
@@ -169,30 +185,53 @@ void common_hal_displayio_release_displays(void) {
void reset_displays(void) {
// The SPI buses used by FourWires may be allocated on the heap so we need to move them inline.
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
- if (displays[i].fourwire_bus.base.type != &displayio_fourwire_type) {
- continue;
- }
- displayio_fourwire_obj_t* fourwire = &displays[i].fourwire_bus;
- if (((uint32_t) fourwire->bus) < ((uint32_t) &displays) ||
- ((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) {
- busio_spi_obj_t* original_spi = fourwire->bus;
- #if 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;
+ 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
+ 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;
+ }
}
- #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.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.SPI 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;
+ }
+ }
}
}
- }
}
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h
index 18c1a4f4a..caa5ce36d 100644
--- a/shared-module/displayio/__init__.h
+++ b/shared-module/displayio/__init__.h
@@ -30,11 +30,13 @@
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/FourWire.h"
#include "shared-bindings/displayio/Group.h"
+#include "shared-bindings/displayio/I2CDisplay.h"
#include "shared-bindings/displayio/ParallelBus.h"
typedef struct {
union {
displayio_fourwire_obj_t fourwire_bus;
+ displayio_i2cdisplay_obj_t i2cdisplay_bus;
displayio_parallelbus_obj_t parallel_bus;
};
displayio_display_obj_t display;