From 6797ec6ed396f65916fe816f8b2b49114253dd86 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 5 Jul 2019 19:01:54 -0700 Subject: Add support for grayscale displays that are < 8 bit depth. This also improves Palette so it stores the original RGB888 colors. Lastly, it adds I2CDisplay as a display bus to talk over I2C. Particularly useful for the SSD1306. Fixes #1828. Fixes #1956 --- shared-module/displayio/ColorConverter.c | 35 +++++-- shared-module/displayio/ColorConverter.h | 4 + shared-module/displayio/Display.c | 173 ++++++++++++++++++++++--------- shared-module/displayio/Display.h | 4 +- shared-module/displayio/FourWire.c | 21 +++- shared-module/displayio/FourWire.h | 6 +- shared-module/displayio/Group.c | 6 +- shared-module/displayio/Group.h | 3 +- shared-module/displayio/I2CDisplay.c | 105 +++++++++++++++++++ shared-module/displayio/I2CDisplay.h | 41 ++++++++ shared-module/displayio/Palette.c | 50 ++++----- shared-module/displayio/Palette.h | 19 +++- shared-module/displayio/TileGrid.c | 50 ++++++--- shared-module/displayio/TileGrid.h | 3 +- shared-module/displayio/__init__.c | 135 +++++++++++++++--------- shared-module/displayio/__init__.h | 2 + 16 files changed, 496 insertions(+), 161 deletions(-) create mode 100644 shared-module/displayio/I2CDisplay.c create mode 100644 shared-module/displayio/I2CDisplay.h (limited to 'shared-module/displayio') diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index 3928e115a..563d40c51 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; +} + +bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { + return 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 #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..9aee70e32 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 +#include #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,13 @@ 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 <= 0xff)) { + common_hal_displayio_display_set_brightness(self, brightness); + } else { + self->current_brightness = -1.0; + } self->area.x1 = 0; self->area.y1 = 0; @@ -234,17 +246,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 +258,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 < 0x100) { + 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 +289,67 @@ 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 + 1] = 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 + 1] = x1 & 0xff; + data[data_length + 2] = x2 >> 8; + data[data_length + 3] = x2 & 0xff; + data_length += 4; } + 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 + 1] = y2 - 1 + self->rowstart; + data_length += 2; + } else { + y1 += self->rowstart; + y2 += self->rowstart - 1; + data[data_length] = y1 >> 8; + data[data_length + 1] = y1 & 0xff; + data[data_length + 2] = y2 >> 8; + data[data_length + 3] = y2 & 0xff; + data_length += 4; + } + self->send(self->bus, self->data_as_commands, data, data_length); } void displayio_display_start_refresh(displayio_display_obj_t* self) { @@ -322,6 +374,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 +404,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 +#include + +#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..6f5e2774c 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -32,15 +32,28 @@ #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); +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..77e6daf6f 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,12 +371,13 @@ 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. // if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) { @@ -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; -- cgit v1.2.3 From 1d1b8703b6c108b72a1abda9312646895f542672 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 19 Jul 2019 16:05:13 -0700 Subject: Review feedback including NO_BRIGHTNESS_COMMAND macro --- locale/ID.po | 13 +++++++-- locale/circuitpython.pot | 13 +++++++-- locale/de_DE.po | 13 +++++++-- locale/en_US.po | 13 +++++++-- locale/en_x_pirate.po | 13 +++++++-- locale/es.po | 13 +++++++-- locale/fil.po | 13 +++++++-- locale/fr.po | 13 +++++++-- locale/it_IT.po | 13 +++++++-- locale/pl.po | 13 +++++++-- locale/pt_BR.po | 13 +++++++-- locale/zh_Latn_pinyin.po | 13 +++++++-- .../atmel-samd/boards/hallowing_m0_express/board.c | 2 +- ports/atmel-samd/boards/pybadge/board.c | 2 +- ports/atmel-samd/boards/pybadge_airlift/board.c | 2 +- ports/atmel-samd/boards/pygamer/board.c | 2 +- ports/atmel-samd/boards/pygamer_advance/board.c | 2 +- ports/atmel-samd/boards/pyportal/board.c | 2 +- ports/atmel-samd/boards/ugame10/board.c | 2 +- shared-bindings/displayio/ColorConverter.c | 2 ++ shared-bindings/displayio/ColorConverter.h | 2 +- shared-bindings/displayio/Display.c | 10 +++++-- shared-bindings/displayio/Display.h | 2 ++ shared-bindings/displayio/I2CDisplay.c | 2 +- shared-bindings/displayio/Palette.c | 2 +- shared-module/displayio/ColorConverter.c | 4 +-- shared-module/displayio/Display.c | 32 ++++++++++------------ shared-module/displayio/Palette.h | 2 ++ shared-module/displayio/TileGrid.c | 2 +- 29 files changed, 161 insertions(+), 69 deletions(-) (limited to 'shared-module/displayio') diff --git a/locale/ID.po b/locale/ID.po index 5e555a025..b211d5379 100644 --- a/locale/ID.po +++ b/locale/ID.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -312,6 +312,10 @@ msgstr "" msgid "Both pins must support hardware interrupts" msgstr "Kedua pin harus mendukung hardware interrut" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "" @@ -443,8 +447,11 @@ msgstr "Clock unit sedang digunakan" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "" diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 67271258a..d91e2b3ac 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -308,6 +308,10 @@ msgstr "" msgid "Both pins must support hardware interrupts" msgstr "" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "" @@ -433,8 +437,11 @@ msgstr "" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "" diff --git a/locale/de_DE.po b/locale/de_DE.po index b0ebf9bcb..748833147 100644 --- a/locale/de_DE.po +++ b/locale/de_DE.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: Pascal Deneaux\n" "Language-Team: Sebastian Plamauer, Pascal Deneaux\n" @@ -312,6 +312,10 @@ msgstr "Bit depth muss ein Vielfaches von 8 sein." msgid "Both pins must support hardware interrupts" msgstr "Beide pins müssen Hardware Interrupts unterstützen" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "Die Helligkeit muss zwischen 0 und 255 liegen" @@ -437,8 +441,11 @@ msgstr "Clock unit wird benutzt" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "Der Befehl muss ein int zwischen 0 und 255 sein" diff --git a/locale/en_US.po b/locale/en_US.po index 91fd15ac8..38074de98 100644 --- a/locale/en_US.po +++ b/locale/en_US.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: \n" @@ -308,6 +308,10 @@ msgstr "" msgid "Both pins must support hardware interrupts" msgstr "" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "" @@ -433,8 +437,11 @@ msgstr "" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "" diff --git a/locale/en_x_pirate.po b/locale/en_x_pirate.po index 5f0ab319f..9bee12d2a 100644 --- a/locale/en_x_pirate.po +++ b/locale/en_x_pirate.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: @sommersoft, @MrCertainly\n" @@ -312,6 +312,10 @@ msgstr "" msgid "Both pins must support hardware interrupts" msgstr "" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "" @@ -437,8 +441,11 @@ msgstr "" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "" diff --git a/locale/es.po b/locale/es.po index ff44238df..33cdfe348 100644 --- a/locale/es.po +++ b/locale/es.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-08-24 22:56-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -316,6 +316,10 @@ msgstr "Bits depth debe ser múltiplo de 8." msgid "Both pins must support hardware interrupts" msgstr "Ambos pines deben soportar interrupciones por hardware" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "El brillo debe estar entro 0 y 255" @@ -441,8 +445,11 @@ msgstr "Clock unit está siendo utilizado" msgid "Column entry must be digitalio.DigitalInOut" msgstr "Entrada de columna debe ser digitalio.DigitalInOut" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "Command debe estar entre 0 y 255." diff --git a/locale/fil.po b/locale/fil.po index 4693cde64..4f05812f7 100644 --- a/locale/fil.po +++ b/locale/fil.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-12-20 22:15-0800\n" "Last-Translator: Timothy \n" "Language-Team: fil\n" @@ -314,6 +314,10 @@ msgstr "Bit depth ay dapat multiple ng 8." msgid "Both pins must support hardware interrupts" msgstr "Ang parehong mga pin ay dapat na sumusuporta sa hardware interrupts" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "Ang liwanag ay dapat sa gitna ng 0 o 255" @@ -441,8 +445,11 @@ msgstr "Clock unit ginagamit" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c #, fuzzy msgid "Command must be an int between 0 and 255" msgstr "Sa gitna ng 0 o 255 dapat ang bytes." diff --git a/locale/fr.po b/locale/fr.po index db2b659dd..a948630ee 100644 --- a/locale/fr.po +++ b/locale/fr.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2019-04-14 20:05+0100\n" "Last-Translator: Pierrick Couturier \n" "Language-Team: fr\n" @@ -319,6 +319,10 @@ msgstr "La profondeur de bit doit être un multiple de 8." msgid "Both pins must support hardware interrupts" msgstr "Les deux entrées doivent supporter les interruptions matérielles" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "La luminosité doit être entre 0 et 255" @@ -447,8 +451,11 @@ msgstr "Horloge en cours d'utilisation" msgid "Column entry must be digitalio.DigitalInOut" msgstr "L'entrée 'Column' doit être un digitalio.DigitalInOut" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c #, fuzzy msgid "Command must be an int between 0 and 255" msgstr "La commande doit être un entier entre 0 et 255" diff --git a/locale/it_IT.po b/locale/it_IT.po index 0e717f15b..40a685f75 100644 --- a/locale/it_IT.po +++ b/locale/it_IT.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-10-02 16:27+0200\n" "Last-Translator: Enrico Paganin \n" "Language-Team: \n" @@ -314,6 +314,10 @@ msgstr "La profondità di bit deve essere multipla di 8." msgid "Both pins must support hardware interrupts" msgstr "Entrambi i pin devono supportare gli interrupt hardware" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "La luminosità deve essere compreso tra 0 e 255" @@ -442,8 +446,11 @@ msgstr "Unità di clock in uso" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c #, fuzzy msgid "Command must be an int between 0 and 255" msgstr "I byte devono essere compresi tra 0 e 255" diff --git a/locale/pl.po b/locale/pl.po index 2bc1a7557..896785126 100644 --- a/locale/pl.po +++ b/locale/pl.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2019-03-19 18:37-0700\n" "Last-Translator: Radomir Dopieralski \n" "Language-Team: pl\n" @@ -311,6 +311,10 @@ msgstr "Głębia musi być wielokrotnością 8." msgid "Both pins must support hardware interrupts" msgstr "Obie nóżki muszą wspierać przerwania sprzętowe" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "Jasność musi być pomiędzy 0 a 255" @@ -436,8 +440,11 @@ msgstr "Jednostka zegara w użyciu" msgid "Column entry must be digitalio.DigitalInOut" msgstr "Kolumny muszą być typu digitalio.DigitalInOut" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "Komenda musi być int pomiędzy 0 a 255" diff --git a/locale/pt_BR.po b/locale/pt_BR.po index 7af2e3d60..7e052bfb7 100644 --- a/locale/pt_BR.po +++ b/locale/pt_BR.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2018-10-02 21:14-0000\n" "Last-Translator: \n" "Language-Team: \n" @@ -311,6 +311,10 @@ msgstr "" msgid "Both pins must support hardware interrupts" msgstr "Ambos os pinos devem suportar interrupções de hardware" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "O brilho deve estar entre 0 e 255" @@ -438,8 +442,11 @@ msgstr "Unidade de Clock em uso" msgid "Column entry must be digitalio.DigitalInOut" msgstr "" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c #, fuzzy msgid "Command must be an int between 0 and 255" msgstr "Os bytes devem estar entre 0 e 255." diff --git a/locale/zh_Latn_pinyin.po b/locale/zh_Latn_pinyin.po index 7f6e80236..258a4d3df 100644 --- a/locale/zh_Latn_pinyin.po +++ b/locale/zh_Latn_pinyin.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: circuitpython-cn\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-07-19 16:09-0700\n" +"POT-Creation-Date: 2019-07-19 16:10-0700\n" "PO-Revision-Date: 2019-04-13 10:10-0700\n" "Last-Translator: hexthat\n" "Language-Team: Chinese Hanyu Pinyin\n" @@ -312,6 +312,10 @@ msgstr "Bǐtè shēndù bìxū shì 8 bèi yǐshàng." msgid "Both pins must support hardware interrupts" msgstr "Liǎng gè yǐn jiǎo dōu bìxū zhīchí yìngjiàn zhōngduàn" +#: shared-bindings/displayio/Display.c +msgid "Brightness must be 0-1.0" +msgstr "" + #: shared-bindings/supervisor/__init__.c msgid "Brightness must be between 0 and 255" msgstr "Liàngdù bìxū jiè yú 0 dào 255 zhī jiān" @@ -437,8 +441,11 @@ msgstr "Shǐyòng shízhōng dānwèi" msgid "Column entry must be digitalio.DigitalInOut" msgstr "Liè tiáomù bìxū shì digitalio.DigitalInOut" -#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/I2CDisplay.c -#: shared-bindings/displayio/ParallelBus.c +#: shared-bindings/displayio/I2CDisplay.c +msgid "Command must be 0-255" +msgstr "" + +#: shared-bindings/displayio/FourWire.c shared-bindings/displayio/ParallelBus.c msgid "Command must be an int between 0 and 255" msgstr "Mìnglìng bìxū shì 0 dào 255 zhī jiān de int" diff --git a/ports/atmel-samd/boards/hallowing_m0_express/board.c b/ports/atmel-samd/boards/hallowing_m0_express/board.c index 1bbbfbaf8..be1741c87 100644 --- a/ports/atmel-samd/boards/hallowing_m0_express/board.c +++ b/ports/atmel-samd/boards/hallowing_m0_express/board.c @@ -100,7 +100,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA00, - 0x100, // Brightness command. Only available when <= 0xff + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pybadge/board.c b/ports/atmel-samd/boards/pybadge/board.c index 7e6c11dd4..c9dca0329 100644 --- a/ports/atmel-samd/boards/pybadge/board.c +++ b/ports/atmel-samd/boards/pybadge/board.c @@ -102,7 +102,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin - 0x100, // no brightness command + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pybadge_airlift/board.c b/ports/atmel-samd/boards/pybadge_airlift/board.c index d7f291d7d..eeab3f2b2 100644 --- a/ports/atmel-samd/boards/pybadge_airlift/board.c +++ b/ports/atmel-samd/boards/pybadge_airlift/board.c @@ -80,7 +80,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin - 0x100, // brightness command, only valid <= 0xff + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pygamer/board.c b/ports/atmel-samd/boards/pygamer/board.c index e59b7c458..d413c8e34 100644 --- a/ports/atmel-samd/boards/pygamer/board.c +++ b/ports/atmel-samd/boards/pygamer/board.c @@ -102,7 +102,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin - 0x100, // Brightness command. Only available when < 0xff + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pygamer_advance/board.c b/ports/atmel-samd/boards/pygamer_advance/board.c index b23353998..498a29db2 100644 --- a/ports/atmel-samd/boards/pygamer_advance/board.c +++ b/ports/atmel-samd/boards/pygamer_advance/board.c @@ -80,7 +80,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin - 0x100, // Brightness command. Only available when < 0xff + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pyportal/board.c b/ports/atmel-samd/boards/pyportal/board.c index 7fffb764f..938072158 100644 --- a/ports/atmel-samd/boards/pyportal/board.c +++ b/ports/atmel-samd/boards/pyportal/board.c @@ -92,7 +92,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PB31, // Backlight pin - 0x100, // Brightness command > 0xff is none s + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/ugame10/board.c b/ports/atmel-samd/boards/ugame10/board.c index 27a03ad91..15e22a72c 100644 --- a/ports/atmel-samd/boards/ugame10/board.c +++ b/ports/atmel-samd/boards/ugame10/board.c @@ -100,7 +100,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), NULL, - 0x100, // brightness command. Only valid <=0xff + NO_BRIGHTNESS_COMMAND, 1.0f, // brightness false, // auto_brightness false, // single_byte_bounds diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index e170e0340..d9524870d 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -62,6 +62,8 @@ STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, siz //| .. method:: convert(color) //| +//| Converts the given RGB888 color to RGB565 +//| STATIC mp_obj_t displayio_colorconverter_obj_convert(mp_obj_t self_in, mp_obj_t color_obj) { displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-bindings/displayio/ColorConverter.h b/shared-bindings/displayio/ColorConverter.h index 8f61e6642..24895500e 100644 --- a/shared-bindings/displayio/ColorConverter.h +++ b/shared-bindings/displayio/ColorConverter.h @@ -34,6 +34,6 @@ extern const mp_obj_type_t displayio_colorconverter_type; void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self); -bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color); +void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c index 2586e562f..361a37ece 100644 --- a/shared-bindings/displayio/Display.c +++ b/shared-bindings/displayio/Display.c @@ -119,7 +119,7 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a { MP_QSTR_write_ram_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2c} }, { MP_QSTR_set_vertical_scroll, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x0} }, { MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, - { MP_QSTR_brightness_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x100} }, + { MP_QSTR_brightness_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_BRIGHTNESS_COMMAND} }, { MP_QSTR_brightness, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, { MP_QSTR_auto_brightness, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, { MP_QSTR_single_byte_bounds, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, @@ -242,10 +242,14 @@ STATIC mp_obj_t displayio_display_obj_get_brightness(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_brightness_obj, displayio_display_obj_get_brightness); -STATIC mp_obj_t displayio_display_obj_set_brightness(mp_obj_t self_in, mp_obj_t brightness) { +STATIC mp_obj_t displayio_display_obj_set_brightness(mp_obj_t self_in, mp_obj_t brightness_obj) { displayio_display_obj_t *self = native_display(self_in); common_hal_displayio_display_set_auto_brightness(self, false); - bool ok = common_hal_displayio_display_set_brightness(self, mp_obj_get_float(brightness)); + mp_float_t brightness = mp_obj_get_float(brightness_obj); + if (brightness < 0 || brightness > 1.0) { + mp_raise_ValueError(translate("Brightness must be 0-1.0")); + } + bool ok = common_hal_displayio_display_set_brightness(self, brightness); if (!ok) { mp_raise_RuntimeError(translate("Brightness not adjustable")); } diff --git a/shared-bindings/displayio/Display.h b/shared-bindings/displayio/Display.h index a60a0cf5a..e765e6f25 100644 --- a/shared-bindings/displayio/Display.h +++ b/shared-bindings/displayio/Display.h @@ -36,6 +36,8 @@ extern const mp_obj_type_t displayio_display_type; #define DELAY 0x80 +#define NO_BRIGHTNESS_COMMAND 0x100 + 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, diff --git a/shared-bindings/displayio/I2CDisplay.c b/shared-bindings/displayio/I2CDisplay.c index 2aac49b4d..1f74e8c9e 100644 --- a/shared-bindings/displayio/I2CDisplay.c +++ b/shared-bindings/displayio/I2CDisplay.c @@ -103,7 +103,7 @@ STATIC mp_obj_t displayio_i2cdisplay_make_new(const mp_obj_type_t *type, size_t STATIC mp_obj_t displayio_i2cdisplay_obj_send(mp_obj_t self, mp_obj_t command_obj, mp_obj_t data_obj) { mp_int_t command_int = MP_OBJ_SMALL_INT_VALUE(command_obj); if (!MP_OBJ_IS_SMALL_INT(command_obj) || command_int > 255 || command_int < 0) { - mp_raise_ValueError(translate("Command must be an int between 0 and 255")); + mp_raise_ValueError(translate("Command must be 0-255")); } uint8_t command = command_int; mp_buffer_info_t bufinfo; diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index 974cadb02..dda58e3c5 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -74,7 +74,7 @@ STATIC mp_obj_t displayio_palette_make_new(const mp_obj_type_t *type, size_t n_a STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) { displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); switch (op) { - case MP_UNARY_OP_BOOL: return mp_obj_new_bool(true); + case MP_UNARY_OP_BOOL: return mp_const_true; case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_len(self)); default: return MP_OBJ_NULL; // op not supported diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index 563d40c51..6b9bd5840 100644 --- a/shared-module/displayio/ColorConverter.c +++ b/shared-module/displayio/ColorConverter.c @@ -57,8 +57,8 @@ bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d return false; } -bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { - return displayio_colorconverter_convert(self, colorspace, input_color, output_color); +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/Display.c b/shared-module/displayio/Display.c index 9aee70e32..a8515b9e5 100644 --- a/shared-module/displayio/Display.c +++ b/shared-module/displayio/Display.c @@ -153,7 +153,8 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm); } } - if (!self->auto_brightness && (self->backlight_inout.base.type != &mp_type_NoneType || brightness_command <= 0xff)) { + 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; @@ -258,7 +259,7 @@ 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 < 0x100) { + } else if (self->brightness_command != NO_BRIGHTNESS_COMMAND) { ok = self->begin_transaction(self->bus); if (ok) { if (self->data_as_commands) { @@ -315,17 +316,16 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, displ data_length = 0; } if (self->single_byte_bounds) { - data[data_length] = x1 + self->colstart; - data[data_length + 1] = x2 - 1 + self->colstart; + data[data_length++] = x1 + self->colstart; + data[data_length++] = x2 - 1 + self->colstart; data_length += 2; } else { x1 += self->colstart; x2 += self->colstart - 1; - data[data_length] = x1 >> 8; - data[data_length + 1] = x1 & 0xff; - data[data_length + 2] = x2 >> 8; - data[data_length + 3] = x2 & 0xff; - data_length += 4; + 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); @@ -337,17 +337,15 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, displ data_length = 0; } if (self->single_byte_bounds) { - data[data_length] = y1 + self->rowstart; - data[data_length + 1] = y2 - 1 + self->rowstart; - data_length += 2; + 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 + 1] = y1 & 0xff; - data[data_length + 2] = y2 >> 8; - data[data_length + 3] = y2 & 0xff; - data_length += 4; + 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); } diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h index 6f5e2774c..19c05baf5 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -53,6 +53,8 @@ typedef struct { bool needs_refresh; } displayio_palette_t; +// Returns false if color fetch did not succeed (out of range or transparent). +// Returns true if color is opaque, and sets color. bool displayio_palette_get_color(displayio_palette_t *palette, const _displayio_colorspace_t* colorspace, uint32_t palette_index, uint32_t* color); bool displayio_palette_needs_refresh(displayio_palette_t *self); void displayio_palette_finish_refresh(displayio_palette_t *self); diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index 77e6daf6f..88873d3a9 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -379,7 +379,7 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c // 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; // 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"); // } -- cgit v1.2.3