EPaper displays work mostly.

crypto-aes
Scott Shawcroft 4 years ago
parent 8fa8737465
commit 70680d5b22
No known key found for this signature in database
GPG Key ID: 9349BC7E64B1921E

@ -84,10 +84,7 @@ void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* sel
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(4);
common_hal_digitalio_digitalinout_set_value(&self->reset, true);
common_hal_displayio_parallelbus_reset(self);
}
never_reset_pin_number(command->number);
@ -111,13 +108,25 @@ void common_hal_displayio_parallelbus_deinit(displayio_parallelbus_obj_t* self)
reset_pin_number(self->reset.pin->number);
}
void common_hal_displayio_parallelbus_reset(mp_obj_t obj) {
displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj);
common_hal_digitalio_digitalinout_set_value(&self->reset, false);
common_hal_mcu_delay_us(4);
common_hal_digitalio_digitalinout_set_value(&self->reset, true);
}
bool common_hal_displayio_parallelbus_bus_free(mp_obj_t obj) {
return true;
}
bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t obj) {
displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj);
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
return true;
}
void common_hal_displayio_parallelbus_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) {
void common_hal_displayio_parallelbus_send(mp_obj_t obj, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length) {
displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj);
common_hal_digitalio_digitalinout_set_value(&self->command, !command);
uint32_t* clear_write = (uint32_t*) &self->write_group->OUTCLR.reg;

@ -320,6 +320,7 @@ SRC_SHARED_MODULE_ALL = \
displayio/Bitmap.c \
displayio/ColorConverter.c \
displayio/Display.c \
displayio/EPaperDisplay.c \
displayio/FourWire.c \
displayio/Group.c \
displayio/I2CDisplay.c \

@ -24,7 +24,7 @@
* THE SOFTWARE.
*/
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/EPaperDisplay.h"
#include <stdint.h>
@ -41,147 +41,143 @@
//| .. currentmodule:: displayio
//|
//| :class:`Display` -- Manage updating a display over a display bus
//| :class:`EPaperDisplay` -- Manage updating an epaper display over a display bus
//| ==========================================================================
//|
//| This initializes a display and connects it into CircuitPython. Unlike other
//| objects in CircuitPython, Display objects live until `displayio.release_displays()`
//| This initializes an epaper display and connects it into CircuitPython. Unlike other
//| objects in CircuitPython, EPaperDisplay objects live until `displayio.release_displays()`
//| is called. This is done so that CircuitPython can use the display itself.
//|
//| Most people should not use this class directly. Use a specific display driver instead that will
//| contain the initialization sequence at minimum.
//| contain the startup and shutdown sequences at minimum.
//|
//| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, grayscale=False, pixels_in_byte_share_row=True, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness_command=None, brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False)
//| .. class:: EPaperDisplay(display_bus, start_sequence, stop_sequence, *, width, height, ram_width, ram_height, colstart=0, rowstart=0, rotation=0, set_column_window_command=None, set_row_window_command=None, single_byte_bounds=False, write_black_ram_command, black_bits_inverted=False, write_color_ram_command=None, color_bits_inverted=False, refresh_display_command, busy_pin=None, busy_state=True, seconds_per_frame=180, always_toggle_chip_select=False)
//|
//| Create a Display object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`).
//| Create a EPaperDisplay object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`).
//|
//| The ``init_sequence`` is bitpacked to minimize the ram impact. Every command begins with a
//| command byte followed by a byte to determine the parameter count and if a delay is need after.
//| When the top bit of the second byte is 1, the next byte will be the delay time in milliseconds.
//| The remaining 7 bits are the parameter count excluding any delay byte. The third through final
//| bytes are the remaining command parameters. The next byte will begin a new command definition.
//| Here is a portion of ILI9341 init code:
//|
//| .. code-block:: python
//|
//| init_sequence = (b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F" # Set Gamma
//| b"\x11\x80\x78"# Exit Sleep then delay 0x78 (120ms)
//| b"\x29\x80\x78"# Display on then delay 0x78 (120ms)
//| )
//| display = displayio.Display(display_bus, init_sequence, width=320, height=240)
//|
//| The first command is 0xe1 with 15 (0xf) parameters following. The second and third are 0x11 and
//| 0x29 respectively with delays (0x80) of 120ms (0x78) and no parameters. Multiple byte literals
//| (b"") are merged together on load. The parens are needed to allow byte literals on subsequent
//| lines.
//|
//| The initialization sequence should always leave the display memory access inline with the scan
//| of the display to minimize tearing artifacts.
//| The ``start_sequence`` and ``stop_sequence`` bitpacked to minimize the ram impact. Every
//| command begins with a command byte followed by a byte to determine the parameter count and if
//| a delay is need after. When the top bit of the second byte is 1, the next byte will be the
//| delay time in milliseconds. The remaining 7 bits are the parameter count excluding any delay
//| byte. The third through final bytes are the remaining command parameters. The next byte will
//| begin a new command definition.
//|
//| :param display_bus: The bus that the display is connected to
//| :type display_bus: displayio.FourWire or displayio.ParallelBus
//| :param buffer init_sequence: Byte-packed initialization sequence.
//| :param buffer start_sequence: Byte-packed initialization sequence.
//| :param buffer stop_sequence: Byte-packed initialization sequence.
//| :param int width: Width in pixels
//| :param int height: Height in pixels
//| :param int ram_width: RAM width in pixels
//| :param int ram_height: RAM height in pixels
//| :param int colstart: The index if the first visible column
//| :param int rowstart: The index if the first visible row
//| :param int rotation: The rotation of the display in degrees clockwise. Must be in 90 degree increments (0, 90, 180, 270)
//| :param int color_depth: The number of bits of color per pixel transmitted. (Some displays
//| support 18 bit but 16 is easier to transmit. The last bit is extrapolated.)
//| :param bool grayscale: True if the display only shows a single color.
//| :param bool pixels_in_byte_share_row: True when pixels are less than a byte and a byte includes pixels from the same row of the display. When False, pixels share a column.
//| :param int set_column_command: Command used to set the start and end columns to update
//| :param int set_row_command: Command used so set the start and end rows to update
//| :param int write_ram_command: Command used to write pixels values into the update region. Ignored if data_as_commands is set.
//| :param int set_vertical_scroll: Command used to set the first row to show
//| :param microcontroller.Pin backlight_pin: Pin connected to the display's backlight
//| :param int brightness_command: Command to set display brightness. Usually available in OLED controllers.
//| :param bool brightness: Initial display brightness. This value is ignored if auto_brightness is True.
//| :param bool auto_brightness: If True, brightness is controlled via an ambient light sensor or other mechanism.
//| :param bool single_byte_bounds: Display column and row commands use single bytes
//| :param bool data_as_commands: Treat all init and boundary data as SPI commands. Certain displays require this.
//| :param int set_column_window_command: Command used to set the start and end columns to update
//| :param int set_row_window_command: Command used so set the start and end rows to update
//| :param int set_current_column_command: Command used to set the current column location
//| :param int set_current_row_command: Command used to set the current row location
//| :param int write_black_ram_command: Command used to write pixels values into the update region
//| :param bool black_bits_inverted: True if 0 bits are used to show black pixels. Otherwise, 1 means to show black.
//| :param int write_color_ram_command: Command used to write pixels values into the update region
//| :param bool color_bits_inverted: True if 0 bits are used to show the color. Otherwise, 1 means to show color.
//| :param int third_color: Color of third ePaper color in RGB888.
//| :param int refresh_display_command: Command used to start a display refresh
//| :param microcontroller.Pin busy_pin: Pin used to signify the display is busy
//| :param bool busy_state: State of the busy pin when the display is busy
//| :param int seconds_per_frame: Minimum number of seconds between screen refreshes
//| :param bool always_toggle_chip_select: When True, chip select is toggled every byte
//|
STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_grayscale, ARG_pixels_in_byte_share_row, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness_command, ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands };
STATIC mp_obj_t displayio_epaperdisplay_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_display_bus, ARG_start_sequence, ARG_stop_sequence, ARG_width, ARG_height, ARG_ram_width, ARG_ram_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_set_column_window_command, ARG_set_row_window_command, ARG_set_current_column_command, ARG_set_current_row_command, ARG_write_black_ram_command, ARG_black_bits_inverted, ARG_write_color_ram_command, ARG_color_bits_inverted, ARG_third_color, ARG_refresh_display_command, ARG_busy_pin, ARG_busy_state, ARG_seconds_per_frame, ARG_always_toggle_chip_select };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_start_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_stop_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
{ MP_QSTR_ram_width, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
{ MP_QSTR_ram_height, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
{ MP_QSTR_colstart, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_rowstart, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_rotation, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_color_depth, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} },
{ MP_QSTR_grayscale, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
{ MP_QSTR_pixels_in_byte_share_row, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
{ MP_QSTR_set_column_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2a} },
{ MP_QSTR_set_row_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2b} },
{ 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 = 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} },
{ MP_QSTR_data_as_commands, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
{ MP_QSTR_set_column_window_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_COMMAND} },
{ MP_QSTR_set_row_window_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_COMMAND} },
{ MP_QSTR_set_current_column_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_COMMAND} },
{ MP_QSTR_set_current_row_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = NO_COMMAND} },
{ MP_QSTR_write_black_ram_command, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_black_bits_inverted, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
{ MP_QSTR_write_color_ram_command, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
{ MP_QSTR_color_bits_inverted, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
{ MP_QSTR_third_color, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x000000} },
{ MP_QSTR_refresh_display_command, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_busy_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
{ MP_QSTR_busy_state, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
{ MP_QSTR_seconds_per_frame, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(180)} },
{ MP_QSTR_always_toggle_chip_select, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t display_bus = args[ARG_display_bus].u_obj;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ);
mp_buffer_info_t start_bufinfo;
mp_get_buffer_raise(args[ARG_start_sequence].u_obj, &start_bufinfo, MP_BUFFER_READ);
mp_buffer_info_t stop_bufinfo;
mp_get_buffer_raise(args[ARG_stop_sequence].u_obj, &stop_bufinfo, MP_BUFFER_READ);
mp_obj_t backlight_pin_obj = args[ARG_backlight_pin].u_obj;
assert_pin(backlight_pin_obj, true);
const mcu_pin_obj_t* backlight_pin = NULL;
if (backlight_pin_obj != NULL && backlight_pin_obj != mp_const_none) {
backlight_pin = MP_OBJ_TO_PTR(backlight_pin_obj);
assert_pin_free(backlight_pin);
}
mp_float_t brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
mp_obj_t busy_pin_obj = args[ARG_busy_pin].u_obj;
assert_pin(busy_pin_obj, true);
const mcu_pin_obj_t* busy_pin = NULL;
if (busy_pin_obj != NULL && busy_pin_obj != mp_const_none) {
busy_pin = MP_OBJ_TO_PTR(busy_pin_obj);
assert_pin_free(busy_pin);
}
mp_int_t rotation = args[ARG_rotation].u_int;
if (rotation % 90 != 0) {
mp_raise_ValueError(translate("Display rotation must be in 90 degree increments"));
}
displayio_display_obj_t *self = NULL;
displayio_epaperdisplay_obj_t *self = NULL;
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].display.base.type == NULL ||
displays[i].display.base.type == &mp_type_NoneType) {
self = &displays[i].display;
self = &displays[i].epaper_display;
break;
}
}
if (self == NULL) {
mp_raise_RuntimeError(translate("Too many displays"));
}
self->base.type = &displayio_display_type;
common_hal_displayio_display_construct(
mp_float_t seconds_per_frame = mp_obj_get_float(args[ARG_seconds_per_frame].u_obj);
mp_int_t write_color_ram_command = NO_COMMAND;
mp_int_t third_color = args[ARG_third_color].u_int;
if (args[ARG_write_color_ram_command].u_obj != mp_const_none) {
write_color_ram_command = mp_obj_get_int(args[ARG_write_color_ram_command].u_obj);
}
self->base.type = &displayio_epaperdisplay_type;
common_hal_displayio_epaperdisplay_construct(
self,
display_bus, args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
args[ARG_color_depth].u_int, args[ARG_grayscale].u_bool, args[ARG_pixels_in_byte_share_row].u_bool,
args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int,
args[ARG_write_ram_command].u_int,
args[ARG_set_vertical_scroll].u_int,
bufinfo.buf, bufinfo.len,
MP_OBJ_TO_PTR(backlight_pin),
args[ARG_brightness_command].u_int,
brightness,
args[ARG_auto_brightness].u_bool,
args[ARG_single_byte_bounds].u_bool,
args[ARG_data_as_commands].u_bool
display_bus,
start_bufinfo.buf, start_bufinfo.len, stop_bufinfo.buf, stop_bufinfo.len,
args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_ram_width].u_int, args[ARG_ram_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
args[ARG_set_column_window_command].u_int, args[ARG_set_row_window_command].u_int,
args[ARG_set_current_column_command].u_int, args[ARG_set_current_row_command].u_int,
args[ARG_write_black_ram_command].u_int, args[ARG_black_bits_inverted].u_bool, write_color_ram_command, args[ARG_color_bits_inverted].u_bool, third_color, args[ARG_refresh_display_command].u_int,
busy_pin, args[ARG_busy_state].u_bool, seconds_per_frame, args[ARG_always_toggle_chip_select].u_bool
);
return self;
}
// Helper to ensure we have the native super class instead of a subclass.
static displayio_display_obj_t* native_display(mp_obj_t display_obj) {
mp_obj_t native_display = mp_instance_cast_to_native_base(display_obj, &displayio_display_type);
static displayio_epaperdisplay_obj_t* native_display(mp_obj_t display_obj) {
mp_obj_t native_display = mp_instance_cast_to_native_base(display_obj, &displayio_epaperdisplay_type);
mp_obj_assert_native_inited(native_display);
return MP_OBJ_TO_PTR(native_display);
}
@ -192,139 +188,75 @@ static displayio_display_obj_t* native_display(mp_obj_t display_obj) {
//| CircuitPython terminal will be shown.
//|
//| :param Group group: The group to show.
STATIC mp_obj_t displayio_display_obj_show(mp_obj_t self_in, mp_obj_t group_in) {
displayio_display_obj_t *self = native_display(self_in);
STATIC mp_obj_t displayio_epaperdisplay_obj_show(mp_obj_t self_in, mp_obj_t group_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
displayio_group_t* group = NULL;
if (group_in != mp_const_none) {
group = MP_OBJ_TO_PTR(native_group(group_in));
}
common_hal_displayio_display_show(self, group);
bool ok = common_hal_displayio_epaperdisplay_show(self, group);
if (!ok) {
mp_raise_ValueError(translate("Group already used"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_show_obj, displayio_display_obj_show);
MP_DEFINE_CONST_FUN_OBJ_2(displayio_epaperdisplay_show_obj, displayio_epaperdisplay_obj_show);
//| .. method:: refresh_soon()
//|
//| Queues up a display refresh that happens in the background.
//|
STATIC mp_obj_t displayio_display_obj_refresh_soon(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
common_hal_displayio_display_refresh_soon(self);
STATIC mp_obj_t displayio_epaperdisplay_obj_refresh_soon(mp_obj_t self_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
common_hal_displayio_epaperdisplay_refresh_soon(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_refresh_soon_obj, displayio_display_obj_refresh_soon);
MP_DEFINE_CONST_FUN_OBJ_1(displayio_epaperdisplay_refresh_soon_obj, displayio_epaperdisplay_obj_refresh_soon);
//| .. method:: wait_for_frame()
//|
//| Waits until the next frame has been transmitted to the display unless the wait count is
//| behind the rendered frames. In that case, this will return immediately with the wait count.
//|
STATIC mp_obj_t displayio_display_obj_wait_for_frame(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_display_wait_for_frame(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_wait_for_frame_obj, displayio_display_obj_wait_for_frame);
//| .. attribute:: brightness
//|
//| The brightness of the display as a float. 0.0 is off and 1.0 is full brightness. When
//| `auto_brightness` is True, the value of `brightness` will change automatically.
//| If `brightness` is set, `auto_brightness` will be disabled and will be set to False.
//|
STATIC mp_obj_t displayio_display_obj_get_brightness(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
mp_float_t brightness = common_hal_displayio_display_get_brightness(self);
if (brightness < 0) {
mp_raise_RuntimeError(translate("Brightness not adjustable"));
}
return mp_obj_new_float(brightness);
STATIC mp_obj_t displayio_epaperdisplay_obj_wait_for_frame(mp_obj_t self_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_epaperdisplay_wait_for_frame(self));
}
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_obj) {
displayio_display_obj_t *self = native_display(self_in);
common_hal_displayio_display_set_auto_brightness(self, false);
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"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_brightness_obj, displayio_display_obj_set_brightness);
const mp_obj_property_t displayio_display_brightness_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_display_get_brightness_obj,
(mp_obj_t)&displayio_display_set_brightness_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: auto_brightness
//|
//| True when the display brightness is adjusted automatically, based on an ambient
//| light sensor or other method. Note that some displays may have this set to True by default,
//| but not actually implement automatic brightness adjustment. `auto_brightness` is set to False
//| if `brightness` is set manually.
//|
STATIC mp_obj_t displayio_display_obj_get_auto_brightness(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
return mp_obj_new_bool(common_hal_displayio_display_get_auto_brightness(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_auto_brightness_obj, displayio_display_obj_get_auto_brightness);
STATIC mp_obj_t displayio_display_obj_set_auto_brightness(mp_obj_t self_in, mp_obj_t auto_brightness) {
displayio_display_obj_t *self = native_display(self_in);
common_hal_displayio_display_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_auto_brightness_obj, displayio_display_obj_set_auto_brightness);
const mp_obj_property_t displayio_display_auto_brightness_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_display_get_auto_brightness_obj,
(mp_obj_t)&displayio_display_set_auto_brightness_obj,
(mp_obj_t)&mp_const_none_obj},
};
MP_DEFINE_CONST_FUN_OBJ_1(displayio_epaperdisplay_wait_for_frame_obj, displayio_epaperdisplay_obj_wait_for_frame);
//| .. attribute:: width
//|
//| Gets the width of the board
//| Gets the width of the display in pixels
//|
//|
STATIC mp_obj_t displayio_display_obj_get_width(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_display_get_width(self));
STATIC mp_obj_t displayio_epaperdisplay_obj_get_width(mp_obj_t self_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_epaperdisplay_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_width_obj, displayio_display_obj_get_width);
MP_DEFINE_CONST_FUN_OBJ_1(displayio_epaperdisplay_get_width_obj, displayio_epaperdisplay_obj_get_width);
const mp_obj_property_t displayio_display_width_obj = {
const mp_obj_property_t displayio_epaperdisplay_width_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_display_get_width_obj,
.proxy = {(mp_obj_t)&displayio_epaperdisplay_get_width_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: height
//|
//| Gets the height of the board
//| Gets the height of the display in pixels
//|
//|
STATIC mp_obj_t displayio_display_obj_get_height(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_display_get_height(self));
STATIC mp_obj_t displayio_epaperdisplay_obj_get_height(mp_obj_t self_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_epaperdisplay_get_height(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_height_obj, displayio_display_obj_get_height);
MP_DEFINE_CONST_FUN_OBJ_1(displayio_epaperdisplay_get_height_obj, displayio_epaperdisplay_obj_get_height);
const mp_obj_property_t displayio_display_height_obj = {
const mp_obj_property_t displayio_epaperdisplay_height_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_display_get_height_obj,
.proxy = {(mp_obj_t)&displayio_epaperdisplay_get_height_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
@ -334,37 +266,34 @@ const mp_obj_property_t displayio_display_height_obj = {
//| The bus being used by the display
//|
//|
STATIC mp_obj_t displayio_display_obj_get_bus(mp_obj_t self_in) {
displayio_display_obj_t *self = native_display(self_in);
STATIC mp_obj_t displayio_epaperdisplay_obj_get_bus(mp_obj_t self_in) {
displayio_epaperdisplay_obj_t *self = native_display(self_in);
return self->bus;
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_bus_obj, displayio_display_obj_get_bus);
MP_DEFINE_CONST_FUN_OBJ_1(displayio_epaperdisplay_get_bus_obj, displayio_epaperdisplay_obj_get_bus);
const mp_obj_property_t displayio_display_bus_obj = {
const mp_obj_property_t displayio_epaperdisplay_bus_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&displayio_display_get_bus_obj,
.proxy = {(mp_obj_t)&displayio_epaperdisplay_get_bus_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t displayio_display_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&displayio_display_show_obj) },
{ MP_ROM_QSTR(MP_QSTR_refresh_soon), MP_ROM_PTR(&displayio_display_refresh_soon_obj) },
{ MP_ROM_QSTR(MP_QSTR_wait_for_frame), MP_ROM_PTR(&displayio_display_wait_for_frame_obj) },
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&displayio_display_brightness_obj) },
{ MP_ROM_QSTR(MP_QSTR_auto_brightness), MP_ROM_PTR(&displayio_display_auto_brightness_obj) },
STATIC const mp_rom_map_elem_t displayio_epaperdisplay_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&displayio_epaperdisplay_show_obj) },
{ MP_ROM_QSTR(MP_QSTR_refresh_soon), MP_ROM_PTR(&displayio_epaperdisplay_refresh_soon_obj) },
{ MP_ROM_QSTR(MP_QSTR_wait_for_frame), MP_ROM_PTR(&displayio_epaperdisplay_wait_for_frame_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_display_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_display_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_bus), MP_ROM_PTR(&displayio_display_bus_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_epaperdisplay_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_epaperdisplay_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_bus), MP_ROM_PTR(&displayio_epaperdisplay_bus_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_display_locals_dict, displayio_display_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(displayio_epaperdisplay_locals_dict, displayio_epaperdisplay_locals_dict_table);
const mp_obj_type_t displayio_display_type = {
const mp_obj_type_t displayio_epaperdisplay_type = {
{ &mp_type_type },
.name = MP_QSTR_Display,
.make_new = displayio_display_make_new,
.locals_dict = (mp_obj_dict_t*)&displayio_display_locals_dict,
.name = MP_QSTR_EPaperDisplay,
.make_new = displayio_epaperdisplay_make_new,
.locals_dict = (mp_obj_dict_t*)&displayio_epaperdisplay_locals_dict,
};

@ -24,47 +24,52 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_EPAPERDISPLAY_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_EPAPERDISPLAY_H
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_EPAPERDISPLAY_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_EPAPERDISPLAY_H
#include "common-hal/microcontroller/Pin.h"
#include "shared-module/displayio/Display.h"
#include "shared-module/displayio/EPaperDisplay.h"
#include "shared-module/displayio/Group.h"
extern const mp_obj_type_t displayio_epaperdisplay_type;
#define DELAY 0x80
void common_hal_displayio_display_construct(displayio_display_obj_t* self,
mp_obj_t bus, uint16_t width, uint16_t height, uint16_t seconds_per_frame,
uint16_t rotation, uint16_t color_depth, bool grayscale,
uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* busy_pin, uint16_t partial_command);
#define NO_COMMAND 0x100
int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* self);
void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t* self,
mp_obj_t bus, uint8_t* start_sequence, uint16_t start_sequence_len, uint8_t* stop_sequence, uint16_t stop_sequence_len,
uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation,
uint16_t set_column_window_command, uint16_t set_row_window_command,
uint16_t set_current_column_command, uint16_t set_current_row_command,
uint16_t write_black_ram_command, bool black_bits_inverted, uint16_t write_color_ram_command, bool color_bits_inverted, uint32_t third_color, uint16_t refresh_display_command,
const mcu_pin_obj_t* busy_pin, bool busy_state, mp_float_t seconds_per_frame, bool always_toggle_chip_select);
void common_hal_displayio_display_show(displayio_display_obj_t* self, displayio_group_t* root_group);
int32_t common_hal_displayio_epaperdisplay_wait_for_frame(displayio_epaperdisplay_obj_t* self);
void common_hal_displayio_display_refresh_soon(displayio_display_obj_t* self);
bool common_hal_displayio_epaperdisplay_show(displayio_epaperdisplay_obj_t* self, displayio_group_t* root_group);
bool displayio_display_begin_transaction(displayio_display_obj_t* self);
void displayio_display_end_transaction(displayio_display_obj_t* self);
void common_hal_displayio_epaperdisplay_refresh_soon(displayio_epaperdisplay_obj_t* self);
bool displayio_epaperdisplay_begin_transaction(displayio_epaperdisplay_obj_t* self);
void displayio_epaperdisplay_end_transaction(displayio_epaperdisplay_obj_t* self);
// The second point of the region is exclusive.
void displayio_display_set_region_to_update(displayio_display_obj_t* self, displayio_area_t* area);
bool displayio_display_frame_queued(displayio_display_obj_t* self);
void displayio_epaperdisplay_set_region_to_update(displayio_epaperdisplay_obj_t* self, displayio_area_t* area);
bool displayio_epaperdisplay_frame_queued(displayio_epaperdisplay_obj_t* self);
bool displayio_display_refresh_queued(displayio_display_obj_t* self);
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);
bool displayio_epaperdisplay_refresh_queued(displayio_epaperdisplay_obj_t* self);
void displayio_epaperdisplay_finish_refresh(displayio_epaperdisplay_obj_t* self);
void displayio_epaperdisplay_send_pixels(displayio_epaperdisplay_obj_t* self, uint8_t* pixels, uint32_t length);
bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* self);
void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness);
bool common_hal_displayio_epaperdisplay_get_auto_brightness(displayio_epaperdisplay_obj_t* self);
void common_hal_displayio_epaperdisplay_set_auto_brightness(displayio_epaperdisplay_obj_t* self, bool auto_brightness);
uint16_t common_hal_displayio_display_get_width(displayio_display_obj_t* self);
uint16_t common_hal_displayio_display_get_height(displayio_display_obj_t* self);
uint16_t common_hal_displayio_epaperdisplay_get_width(displayio_epaperdisplay_obj_t* self);
uint16_t common_hal_displayio_epaperdisplay_get_height(displayio_epaperdisplay_obj_t* self);
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self);
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness);
mp_float_t common_hal_displayio_epaperdisplay_get_brightness(displayio_epaperdisplay_obj_t* self);
bool common_hal_displayio_epaperdisplay_set_brightness(displayio_epaperdisplay_obj_t* self, mp_float_t brightness);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_EPAPERDISPLAY_H
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_EPAPERDISPLAY_H

@ -103,31 +103,41 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_
return self;
}
//| .. method:: send(command, data)
//| .. method:: send(command, data, *, toggle_every_byte=False)
//|
//| Sends the given command value followed by the full set of data. Display state, such as
//| vertical scroll, set via ``send`` may or may not be reset once the code is done.
//|
STATIC mp_obj_t displayio_fourwire_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) {
STATIC mp_obj_t displayio_fourwire_obj_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_command, ARG_data, ARG_toggle_every_byte };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_command, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_toggle_every_byte, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_int_t command_int = args[ARG_command].u_int;
if (command_int > 255 || command_int < 0) {
mp_raise_ValueError(translate("Command must be an int between 0 and 255"));
}
displayio_fourwire_obj_t *self = pos_args[0];
uint8_t command = command_int;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(data_obj, &bufinfo, MP_BUFFER_READ);
mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_READ);
// Wait for display bus to be available.
while (!common_hal_displayio_fourwire_begin_transaction(self)) {
RUN_BACKGROUND_TASKS;
}
common_hal_displayio_fourwire_send(self, true, &command, 1);
common_hal_displayio_fourwire_send(self, false, ((uint8_t*) bufinfo.buf), bufinfo.len);
common_hal_displayio_fourwire_send(self, true, args[ARG_toggle_every_byte].u_bool, &command, 1);
common_hal_displayio_fourwire_send(self, false, args[ARG_toggle_every_byte].u_bool, ((uint8_t*) bufinfo.buf), bufinfo.len);
common_hal_displayio_fourwire_end_transaction(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_3(displayio_fourwire_send_obj, displayio_fourwire_obj_send);
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_fourwire_send_obj, 3, displayio_fourwire_obj_send);
STATIC const mp_rom_map_elem_t displayio_fourwire_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&displayio_fourwire_send_obj) },

@ -40,9 +40,12 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t* self);
void common_hal_displayio_fourwire_reset(mp_obj_t self);
bool common_hal_displayio_fourwire_bus_free(mp_obj_t self);
bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t self);
void common_hal_displayio_fourwire_send(mp_obj_t self, bool command, uint8_t *data, uint32_t data_length);
void common_hal_displayio_fourwire_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
void common_hal_displayio_fourwire_end_transaction(mp_obj_t self);

@ -116,7 +116,7 @@ STATIC mp_obj_t displayio_i2cdisplay_obj_send(mp_obj_t self, mp_obj_t command_ob
uint8_t full_command[bufinfo.len + 1];
full_command[0] = command;
memcpy(full_command + 1, ((uint8_t*) bufinfo.buf), bufinfo.len);
common_hal_displayio_i2cdisplay_send(self, true, full_command, bufinfo.len + 1);
common_hal_displayio_i2cdisplay_send(self, true, false, full_command, bufinfo.len + 1);
common_hal_displayio_i2cdisplay_end_transaction(self);
return mp_const_none;

@ -37,9 +37,12 @@ void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t* self,
void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t* self);
void common_hal_displayio_i2cdisplay_reset(mp_obj_t self);
bool common_hal_displayio_i2cdisplay_bus_free(mp_obj_t self);
bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t self);
void common_hal_displayio_i2cdisplay_send(mp_obj_t self, bool command, uint8_t *data, uint32_t data_length);
void common_hal_displayio_i2cdisplay_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t self);

@ -124,8 +124,8 @@ STATIC mp_obj_t displayio_parallelbus_obj_send(mp_obj_t self, mp_obj_t command_o
while (!common_hal_displayio_parallelbus_begin_transaction(self)) {
RUN_BACKGROUND_TASKS;
}
common_hal_displayio_parallelbus_send(self, true, &command, 1);
common_hal_displayio_parallelbus_send(self, false, ((uint8_t*) bufinfo.buf), bufinfo.len);
common_hal_displayio_parallelbus_send(self, true, false, &command, 1);
common_hal_displayio_parallelbus_send(self, false, false, ((uint8_t*) bufinfo.buf), bufinfo.len);
common_hal_displayio_parallelbus_end_transaction(self);
return mp_const_none;

@ -40,9 +40,12 @@ void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* sel
void common_hal_displayio_parallelbus_deinit(displayio_parallelbus_obj_t* self);
void common_hal_displayio_parallelbus_reset(mp_obj_t self);
bool common_hal_displayio_parallelbus_bus_free(mp_obj_t self);
bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t self);
void common_hal_displayio_parallelbus_send(mp_obj_t self, bool command, uint8_t *data, uint32_t data_length);
void common_hal_displayio_parallelbus_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
void common_hal_displayio_parallelbus_end_transaction(mp_obj_t self);

@ -33,6 +33,7 @@
#include "shared-bindings/displayio/Bitmap.h"
#include "shared-bindings/displayio/ColorConverter.h"
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/EPaperDisplay.h"
#include "shared-bindings/displayio/FourWire.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/I2CDisplay.h"
@ -60,6 +61,7 @@
//| Bitmap
//| ColorConverter
//| Display
//| EPaperDisplay
//| FourWire
//| Group
//| I2CDisplay
@ -91,6 +93,7 @@ STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Bitmap), MP_ROM_PTR(&displayio_bitmap_type) },
{ MP_ROM_QSTR(MP_QSTR_ColorConverter), MP_ROM_PTR(&displayio_colorconverter_type) },
{ MP_ROM_QSTR(MP_QSTR_Display), MP_ROM_PTR(&displayio_display_type) },
{ MP_ROM_QSTR(MP_QSTR_EPaperDisplay), MP_ROM_PTR(&displayio_epaperdisplay_type) },
{ MP_ROM_QSTR(MP_QSTR_Group), MP_ROM_PTR(&displayio_group_type) },
{ MP_ROM_QSTR(MP_QSTR_OnDiskBitmap), MP_ROM_PTR(&displayio_ondiskbitmap_type) },
{ MP_ROM_QSTR(MP_QSTR_Palette), MP_ROM_PTR(&displayio_palette_type) },

@ -45,14 +45,104 @@ uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) {
return (r8 * 19) / 255 + (g8 * 182) / 255 + (b8 + 54) / 255;
}
void compute_bounds(uint8_t r8, uint8_t g8, uint8_t b8, uint8_t* min, uint8_t* max) {
if (r8 > g8) {
if (b8 > r8) {
*max = b8;
} else {
*max = r8;
}
if (b8 < g8) {
*min = b8;
} else {
*min = g8;
}
} else {
if (b8 > g8) {
*max = b8;
} else {
*max = g8;
}
if (b8 < r8) {
*min = b8;
} else {
*min = r8;
}
}
}
uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888) {
uint32_t r8 = (color_rgb888 >> 16);
uint32_t g8 = (color_rgb888 >> 8) & 0xff;
uint32_t b8 = color_rgb888 & 0xff;
uint8_t max;
uint8_t min;
compute_bounds(r8, g8, b8, &min, &max);
return max - min;
}
uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888) {
uint32_t r8 = (color_rgb888 >> 16);
uint32_t g8 = (color_rgb888 >> 8) & 0xff;
uint32_t b8 = color_rgb888 & 0xff;
uint8_t max;
uint8_t min;
compute_bounds(r8, g8, b8, &min, &max);
uint8_t c = max - min;
if (c == 0) {
return 0;
}
int32_t hue = 0;
if (max == r8) {
hue = (((int32_t) (g8 - b8) * 40) / c) % 240;
} else if (max == g8) {
hue = (((int32_t) (b8 - r8) + (2 * c)) * 40) / c;
} else if (max == b8) {
hue = (((int32_t) (r8 - g8) + (4 * c)) * 40) / c;
}
if (hue < 0) {
hue += 240;
}
return hue;
}
void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t* colorspace, uint8_t pixel_hue, uint8_t pixel_luma, uint32_t* color) {
int16_t hue_diff = colorspace->tricolor_hue - pixel_hue;
if ((-10 <= hue_diff && hue_diff <= 10) || hue_diff <= -220 || hue_diff >= 220) {
if (colorspace->grayscale) {
*color = 0;
} else {
*color = 1;
}
} else if (!colorspace->grayscale) {
*color = 0;
}
}
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->tricolor) {
uint8_t luma = displayio_colorconverter_compute_luma(input_color);
*output_color = luma >> (8 - colorspace->depth);
if (displayio_colorconverter_compute_chroma(input_color) <= 16) {
if (!colorspace->grayscale) {
*output_color = 0;
}
return true;
}
uint8_t pixel_hue = displayio_colorconverter_compute_hue(input_color);
displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, output_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;
} else if (!colorspace->grayscale && colorspace->depth == 1) {
}
return false;
}

@ -42,5 +42,8 @@ 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);
uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888);
uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888);
void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t* colorspace, uint8_t pixel_hue, uint8_t pixel_luma, uint32_t* color);
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H

@ -40,8 +40,6 @@
#include "tick.h"
#define DELAY 0x80
void common_hal_displayio_display_construct(displayio_display_obj_t* self,
mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, uint16_t rotation,
uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte,
@ -97,10 +95,10 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
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);
self->send(self->bus, true, true, full_command, data_size + 1);
} else {
self->send(self->bus, true, cmd, 1);
self->send(self->bus, false, data, data_size);
self->send(self->bus, true, true, cmd, 1);
self->send(self->bus, false, false, data, data_size);
}
uint16_t delay_length_ms = 10;
if (delay) {
@ -232,6 +230,9 @@ const displayio_area_t* displayio_display_get_refresh_areas(displayio_display_ob
self->area.next = NULL;
return &self->area;
} else {
if (self->current_group == NULL || self->current_group->base.type != &displayio_group_type) {
asm("bkpt");
}
return displayio_group_get_refresh_areas(self->current_group, NULL);
}
}
@ -283,12 +284,12 @@ bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self,
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);
self->send(self->bus, true, 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->send(self->bus, true, true, &command, 1);
self->send(self->bus, false, false, &hex_brightness, 1);
}
self->end_transaction(self->bus);
}
@ -331,7 +332,7 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, displ
data[0] = self->set_column_command;
uint8_t data_length = 1;
if (!self->data_as_commands) {
self->send(self->bus, true, data, 1);
self->send(self->bus, true, true, data, 1);
data_length = 0;
}
if (self->single_byte_bounds) {
@ -345,13 +346,13 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, displ
data[data_length++] = x2 >> 8;
data[data_length++] = x2 & 0xff;
}
self->send(self->bus, self->data_as_commands, data, data_length);
self->send(self->bus, self->data_as_commands, 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, data, 1);
self->send(self->bus, true, true, data, 1);
data_length = 0;
}
if (self->single_byte_bounds) {
@ -365,7 +366,7 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, displ
data[data_length++] = y2 >> 8;
data[data_length++] = y2 & 0xff;
}
self->send(self->bus, self->data_as_commands, data, data_length);
self->send(self->bus, self->data_as_commands, self->data_as_commands, data, data_length);
}
void displayio_display_start_refresh(displayio_display_obj_t* self) {
@ -391,9 +392,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, true, true, &self->write_ram_command, 1);
}
self->send(self->bus, false, pixels, length);
self->send(self->bus, false, false, pixels, length);
}
void displayio_display_update_backlight(displayio_display_obj_t* self) {
@ -414,7 +415,6 @@ void release_display(displayio_display_obj_t* self) {
if (self->current_group != NULL) {
self->current_group->in_group = false;
}
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
common_hal_pulseio_pwmout_reset_ok(&self->backlight_pwm);
common_hal_pulseio_pwmout_deinit(&self->backlight_pwm);

@ -34,7 +34,7 @@
#include "shared-module/displayio/area.h"
typedef bool (*display_bus_begin_transaction)(mp_obj_t bus);
typedef void (*display_bus_send)(mp_obj_t bus, bool command, uint8_t *data, uint32_t data_length);
typedef void (*display_bus_send)(mp_obj_t bus, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
typedef void (*display_bus_end_transaction)(mp_obj_t bus);
typedef struct {

@ -0,0 +1,531 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/displayio/EPaperDisplay.h"
#include "py/runtime.h"
#include "shared-bindings/displayio/ColorConverter.h"
#include "shared-bindings/displayio/FourWire.h"
#include "shared-bindings/displayio/I2CDisplay.h"
#include "shared-bindings/displayio/ParallelBus.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/time/__init__.h"
#include "shared-module/displayio/__init__.h"
#include "supervisor/shared/display.h"
#include "supervisor/usb.h"
#include <stdint.h>
#include <string.h>
#include "tick.h"
void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t* self,
mp_obj_t bus, uint8_t* start_sequence, uint16_t start_sequence_len, uint8_t* stop_sequence, uint16_t stop_sequence_len,
uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height,
int16_t colstart, int16_t rowstart, uint16_t rotation,
uint16_t set_column_window_command, uint16_t set_row_window_command,
uint16_t set_current_column_command, uint16_t set_current_row_command,
uint16_t write_black_ram_command, bool black_bits_inverted, uint16_t write_color_ram_command, bool color_bits_inverted, uint32_t third_color, uint16_t refresh_display_command,
const mcu_pin_obj_t* busy_pin, bool busy_state, mp_float_t seconds_per_frame, bool always_toggle_chip_select) {
self->colorspace.depth = 1;
self->colorspace.grayscale = true;
self->colorspace.pixels_in_byte_share_row = true;
self->colorspace.bytes_per_cell = 1;
self->colorspace.reverse_pixels_in_byte = true;
if (third_color != 0x000000) {
self->colorspace.tricolor = true;
self->colorspace.tricolor_hue = displayio_colorconverter_compute_hue(third_color);
self->colorspace.tricolor_luma = displayio_colorconverter_compute_luma(third_color);
}
self->set_column_window_command = set_column_window_command;
self->set_row_window_command = set_row_window_command;
self->set_current_column_command = set_current_column_command;
self->set_current_row_command = set_current_row_command;
self->write_black_ram_command = write_black_ram_command;
self->black_bits_inverted = black_bits_inverted;
self->write_color_ram_command = write_color_ram_command;
self->color_bits_inverted = color_bits_inverted;
self->refresh_display_command = refresh_display_command;
self->busy_state = busy_state;
self->refresh = true;
self->current_group = NULL;
self->colstart = colstart;
self->rowstart = rowstart;
self->refreshing = false;
self->milliseconds_per_frame = seconds_per_frame * 1000;
self->always_toggle_chip_select = always_toggle_chip_select;
self->start_sequence = start_sequence;
self->start_sequence_len = start_sequence_len;
self->stop_sequence = stop_sequence;
self->stop_sequence_len = stop_sequence_len;
if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
self->bus_reset = common_hal_displayio_parallelbus_reset;
self->bus_free = common_hal_displayio_parallelbus_bus_free;
self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction;
self->send = common_hal_displayio_parallelbus_send;
self->end_transaction = common_hal_displayio_parallelbus_end_transaction;
} else if (MP_OBJ_IS_TYPE(bus, &displayio_fourwire_type)) {
self->bus_reset = common_hal_displayio_fourwire_reset;
self->bus_free = common_hal_displayio_fourwire_bus_free;
self->begin_transaction = common_hal_displayio_fourwire_begin_transaction;
self->send = common_hal_displayio_fourwire_send;
self->end_transaction = common_hal_displayio_fourwire_end_transaction;
} else if (MP_OBJ_IS_TYPE(bus, &displayio_i2cdisplay_type)) {
self->bus_reset = common_hal_displayio_i2cdisplay_reset;
self->bus_free = common_hal_displayio_i2cdisplay_bus_free;
self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction;
self->send = common_hal_displayio_i2cdisplay_send;
self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction;
} else {
mp_raise_ValueError(translate("Unsupported display bus type"));
}
self->bus = bus;
supervisor_start_terminal(width, height);
self->width = width;
self->height = height;
rotation = rotation % 360;
self->transform.x = 0;
self->transform.y = 0;
self->transform.scale = 1;
self->transform.mirror_x = false;
self->transform.mirror_y = false;
self->transform.transpose_xy = false;
if (rotation == 0 || rotation == 180) {
if (rotation == 180) {
self->transform.mirror_x = true;
self->transform.mirror_y = true;
}
} else {
self->transform.transpose_xy = true;
if (rotation == 270) {
self->transform.mirror_y = true;
} else {
self->transform.mirror_x = true;
}
}
self->ram_width = ram_width;
self->ram_height = ram_height;
self->area.x1 = 0;
self->area.y1 = 0;
self->area.next = NULL;
self->transform.dx = 1;
self->transform.dy = 1;
if (self->transform.transpose_xy) {
self->area.x2 = height;
self->area.y2 = width;
if (self->transform.mirror_x) {
self->transform.x = height;
self->transform.dx = -1;
}
if (self->transform.mirror_y) {
self->transform.y = width;
self->transform.dy = -1;
}
} else {
self->area.x2 = width;
self->area.y2 = height;
if (self->transform.mirror_x) {
self->transform.x = width;
self->transform.dx = -1;
}
if (self->transform.mirror_y) {
self->transform.y = height;
self->transform.dy = -1;
}
}
self->busy.base.type = &mp_type_NoneType;
if (busy_pin != NULL) {
self->busy.base.type = &digitalio_digitalinout_type;
common_hal_digitalio_digitalinout_construct(&self->busy, busy_pin);
never_reset_pin_number(busy_pin->number);
}
// Set the group after initialization otherwise we may send pixels while we delay in
// initialization.
common_hal_displayio_epaperdisplay_show(self, &circuitpython_splash);
}
bool common_hal_displayio_epaperdisplay_show(displayio_epaperdisplay_obj_t* self, displayio_group_t* root_group) {
if (root_group == NULL && !circuitpython_splash.in_group) {
root_group = &circuitpython_splash;
}
if (root_group == self->current_group) {
return true;
}
if (root_group != NULL && root_group->in_group) {
return false;
}
if (self->current_group != NULL) {
self->current_group->in_group = false;
}
if (root_group != NULL) {
displayio_group_update_transform(root_group, &self->transform);
root_group->in_group = true;
self->current_group = root_group;
}
self->full_refresh = true;
common_hal_displayio_epaperdisplay_refresh_soon(self);
return true;
}
void common_hal_displayio_epaperdisplay_refresh_soon(displayio_epaperdisplay_obj_t* self) {
self->refresh = true;
}
const displayio_area_t* displayio_epaperdisplay_get_refresh_areas(displayio_epaperdisplay_obj_t *self) {
const displayio_area_t* first_area;
if (self->current_group == NULL || self->current_group->base.type != &displayio_group_type) {
asm("bkpt");
}
if (self->full_refresh) {
first_area = &self->area;
} else {
first_area = displayio_group_get_refresh_areas(self->current_group, NULL);
}
if (first_area != NULL && self->set_row_window_command == NO_COMMAND) {
self->area.next = NULL;
return &self->area;
}
return first_area;
}
int32_t common_hal_displayio_epaperdisplay_wait_for_frame(displayio_epaperdisplay_obj_t* self) {
uint64_t last_refresh = self->last_refresh;
// Don't try to refresh if we got an exception.
while (last_refresh == self->last_refresh && MP_STATE_VM(mp_pending_exception) == NULL) {
MICROPY_VM_HOOK_LOOP
}
return 0;
}
uint16_t common_hal_displayio_epaperdisplay_get_width(displayio_epaperdisplay_obj_t* self){
return self->width;
}
uint16_t common_hal_displayio_epaperdisplay_get_height(displayio_epaperdisplay_obj_t* self){
return self->height;
}
bool displayio_epaperdisplay_bus_free(displayio_epaperdisplay_obj_t *self) {
return self->bus_free(self->bus);
}
bool displayio_epaperdisplay_begin_transaction(displayio_epaperdisplay_obj_t* self) {
return self->begin_transaction(self->bus);
}
void displayio_epaperdisplay_end_transaction(displayio_epaperdisplay_obj_t* self) {
self->end_transaction(self->bus);
}
STATIC void wait_for_busy(displayio_epaperdisplay_obj_t* self) {
if (self->busy.base.type == &mp_type_NoneType) {
return;
}
while (common_hal_digitalio_digitalinout_get_value(&self->busy) == self->busy_state) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
}
}
STATIC void send_command_sequence(displayio_epaperdisplay_obj_t* self, bool should_wait_for_busy, uint8_t* sequence, uint32_t sequence_len) {
uint32_t i = 0;
while (i < sequence_len) {
uint8_t *cmd = sequence + i;
uint8_t data_size = *(cmd + 1);
bool delay = (data_size & DELAY) != 0;
data_size &= ~DELAY;
uint8_t *data = cmd + 2;
self->begin_transaction(self->bus);
self->send(self->bus, true, self->always_toggle_chip_select, cmd, 1);
self->send(self->bus, false, self->always_toggle_chip_select, data, data_size);
self->end_transaction(self->bus);
uint16_t delay_length_ms = 0;
if (delay) {
data_size++;
delay_length_ms = *(cmd + 1 + data_size);
if (delay_length_ms == 255) {
delay_length_ms = 500;
}
}
common_hal_time_delay_ms(delay_length_ms);
if (should_wait_for_busy) {
wait_for_busy(self);
}
i += 2 + data_size;
}
}
void displayio_epaperdisplay_set_region_to_update(displayio_epaperdisplay_obj_t* self, displayio_area_t* area) {
if (self->set_row_window_command == NO_COMMAND) {
return;
}
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.
uint8_t pixels_per_byte = 8 / self->colorspace.depth;
x1 /= pixels_per_byte * self->colorspace.bytes_per_cell;
x2 /= pixels_per_byte * self->colorspace.bytes_per_cell;
// Set column.
uint8_t data[5];
data[0] = self->set_column_window_command;
self->send(self->bus, true, self->always_toggle_chip_select, data, 1);
uint8_t data_length = 0;
if (self->ram_width / pixels_per_byte < 0x100) {
data[data_length++] = x1 + self->colstart;
data[data_length++] = x2 - 1 + self->colstart;
} else {
x1 += self->colstart;
x2 += self->colstart - 1;
data[data_length++] = x1 >> 8;