2019-08-02 19:17:38 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2019-11-26 15:48:36 -05:00
|
|
|
#include "py/gc.h"
|
2019-08-02 19:17:38 -04:00
|
|
|
#include "py/runtime.h"
|
2022-07-30 01:09:49 -04:00
|
|
|
#include "shared/runtime/interrupt_char.h"
|
2019-08-02 19:17:38 -04:00
|
|
|
#include "shared-bindings/displayio/ColorConverter.h"
|
|
|
|
#include "shared-bindings/displayio/FourWire.h"
|
|
|
|
#include "shared-bindings/displayio/I2CDisplay.h"
|
2021-08-29 12:49:47 -04:00
|
|
|
#if CIRCUITPY_PARALLELDISPLAY
|
2021-08-29 08:26:47 -04:00
|
|
|
#include "shared-bindings/paralleldisplay/ParallelBus.h"
|
2021-08-29 12:49:47 -04:00
|
|
|
#endif
|
2019-08-02 19:17:38 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
#include "shared-bindings/time/__init__.h"
|
|
|
|
#include "shared-module/displayio/__init__.h"
|
|
|
|
#include "supervisor/shared/display.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/shared/tick.h"
|
2019-08-02 19:17:38 -04:00
|
|
|
#include "supervisor/usb.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-11-23 20:23:13 -05:00
|
|
|
#define DELAY 0x80
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t *self,
|
2022-07-30 01:09:49 -04:00
|
|
|
mp_obj_t bus, const uint8_t *start_sequence, uint16_t start_sequence_len, mp_float_t start_up_time,
|
2021-03-15 09:57:36 -04:00
|
|
|
const uint8_t *stop_sequence, uint16_t stop_sequence_len,
|
|
|
|
uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height,
|
|
|
|
int16_t colstart, int16_t rowstart, uint16_t rotation,
|
|
|
|
uint16_t set_column_window_command, uint16_t set_row_window_command,
|
|
|
|
uint16_t set_current_column_command, uint16_t set_current_row_command,
|
2022-07-30 01:09:49 -04:00
|
|
|
uint16_t write_black_ram_command, bool black_bits_inverted,
|
|
|
|
uint16_t write_color_ram_command, bool color_bits_inverted, uint32_t highlight_color,
|
|
|
|
const uint8_t *refresh_sequence, uint16_t refresh_sequence_len, mp_float_t refresh_time,
|
|
|
|
const mcu_pin_obj_t *busy_pin, bool busy_state, mp_float_t seconds_per_frame,
|
2023-04-20 19:15:06 -04:00
|
|
|
bool chip_select, bool grayscale, bool acep, bool two_byte_sequence_length, bool address_little_endian) {
|
2022-07-30 01:09:49 -04:00
|
|
|
uint16_t color_depth = 1;
|
2023-03-22 14:14:38 -04:00
|
|
|
bool core_grayscale = true;
|
2019-08-14 17:17:35 -04:00
|
|
|
if (highlight_color != 0x000000) {
|
2019-08-16 21:34:00 -04:00
|
|
|
self->core.colorspace.tricolor = true;
|
|
|
|
self->core.colorspace.tricolor_hue = displayio_colorconverter_compute_hue(highlight_color);
|
|
|
|
self->core.colorspace.tricolor_luma = displayio_colorconverter_compute_luma(highlight_color);
|
2023-04-20 19:15:06 -04:00
|
|
|
} else {
|
|
|
|
self->core.colorspace.tricolor = false;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
2023-04-20 19:15:06 -04:00
|
|
|
self->acep = acep;
|
|
|
|
self->core.colorspace.sevencolor = acep;
|
2022-07-30 01:09:49 -04:00
|
|
|
if (acep) {
|
|
|
|
color_depth = 4; // bits. 7 colors + clean
|
|
|
|
grayscale = false;
|
2023-03-22 14:14:38 -04:00
|
|
|
core_grayscale = false;
|
2022-07-30 01:09:49 -04:00
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
|
2023-04-20 19:15:06 -04:00
|
|
|
displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height,
|
|
|
|
colstart, rowstart, rotation, color_depth, core_grayscale, true, 1, true, true,
|
|
|
|
set_column_window_command, set_row_window_command, set_current_column_command, set_current_row_command,
|
|
|
|
false /* data_as_commands */, chip_select,
|
|
|
|
false /* SH1107_addressing */, address_little_endian);
|
2019-08-16 21:34:00 -04:00
|
|
|
|
2019-08-02 19:17:38 -04:00
|
|
|
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;
|
2019-08-21 16:15:35 -04:00
|
|
|
self->refresh_time = refresh_time * 1000;
|
2019-08-02 19:17:38 -04:00
|
|
|
self->busy_state = busy_state;
|
|
|
|
self->refreshing = false;
|
|
|
|
self->milliseconds_per_frame = seconds_per_frame * 1000;
|
2019-08-26 19:37:59 -04:00
|
|
|
self->chip_select = chip_select ? CHIP_SELECT_TOGGLE_EVERY_BYTE : CHIP_SELECT_UNTOUCHED;
|
2020-10-26 19:58:00 -04:00
|
|
|
self->grayscale = grayscale;
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
self->start_sequence = start_sequence;
|
|
|
|
self->start_sequence_len = start_sequence_len;
|
2022-07-30 01:09:49 -04:00
|
|
|
self->start_up_time_ms = start_up_time * 1000;
|
2019-08-02 19:17:38 -04:00
|
|
|
self->stop_sequence = stop_sequence;
|
|
|
|
self->stop_sequence_len = stop_sequence_len;
|
2022-07-30 01:09:49 -04:00
|
|
|
self->refresh_sequence = refresh_sequence;
|
|
|
|
self->refresh_sequence_len = refresh_sequence_len;
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
self->busy.base.type = &mp_type_NoneType;
|
2022-01-12 22:45:35 -05:00
|
|
|
self->two_byte_sequence_length = two_byte_sequence_length;
|
2019-08-02 19:17:38 -04:00
|
|
|
if (busy_pin != NULL) {
|
|
|
|
self->busy.base.type = &digitalio_digitalinout_type;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->busy, busy_pin);
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_never_reset_pin(busy_pin);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2019-08-14 17:17:35 -04:00
|
|
|
// Clear the color memory if it isn't in use.
|
|
|
|
if (highlight_color == 0x00 && write_color_ram_command != NO_COMMAND) {
|
|
|
|
// TODO: Clear
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:17:38 -04:00
|
|
|
// Set the group after initialization otherwise we may send pixels while we delay in
|
|
|
|
// initialization.
|
|
|
|
common_hal_displayio_epaperdisplay_show(self, &circuitpython_splash);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_epaperdisplay_show(displayio_epaperdisplay_obj_t *self, displayio_group_t *root_group) {
|
2022-12-01 18:25:36 -05:00
|
|
|
if (root_group == NULL) {
|
|
|
|
root_group = &circuitpython_splash;
|
|
|
|
}
|
2022-11-18 17:53:18 -05:00
|
|
|
return displayio_display_core_set_root_group(&self->core, root_group);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2022-11-18 18:34:03 -05:00
|
|
|
bool common_hal_displayio_epaperdisplay_set_root_group(displayio_epaperdisplay_obj_t *self, displayio_group_t *root_group) {
|
|
|
|
return displayio_display_core_set_root_group(&self->core, root_group);
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
STATIC const displayio_area_t *displayio_epaperdisplay_get_refresh_areas(displayio_epaperdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
if (self->core.full_refresh) {
|
2019-08-22 03:33:27 -04:00
|
|
|
self->core.area.next = NULL;
|
|
|
|
return &self->core.area;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
const displayio_area_t *first_area = NULL;
|
2019-08-22 03:33:27 -04:00
|
|
|
if (self->core.current_group != NULL) {
|
2019-08-16 21:34:00 -04:00
|
|
|
first_area = displayio_group_get_refresh_areas(self->core.current_group, NULL);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
2023-04-24 19:24:33 -04:00
|
|
|
if (first_area != NULL && self->core.row_command == NO_COMMAND) {
|
|
|
|
// Do a full refresh if the display doesn't support partial updates.
|
2019-08-16 21:34:00 -04:00
|
|
|
self->core.area.next = NULL;
|
|
|
|
return &self->core.area;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
return first_area;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_epaperdisplay_get_width(displayio_epaperdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_width(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_epaperdisplay_get_height(displayio_epaperdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_height(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void wait_for_busy(displayio_epaperdisplay_obj_t *self) {
|
2019-08-02 19:17:38 -04:00
|
|
|
if (self->busy.base.type == &mp_type_NoneType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (common_hal_digitalio_digitalinout_get_value(&self->busy) == self->busy_state) {
|
2019-08-23 18:27:21 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void send_command_sequence(displayio_epaperdisplay_obj_t *self,
|
|
|
|
bool should_wait_for_busy, const uint8_t *sequence, uint32_t sequence_len) {
|
2019-08-02 19:17:38 -04:00
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < sequence_len) {
|
2020-11-10 21:02:16 -05:00
|
|
|
const uint8_t *cmd = sequence + i;
|
2019-08-02 19:17:38 -04:00
|
|
|
uint8_t data_size = *(cmd + 1);
|
|
|
|
bool delay = (data_size & DELAY) != 0;
|
2020-11-10 21:02:16 -05:00
|
|
|
const uint8_t *data = cmd + 2;
|
2022-01-12 22:45:35 -05:00
|
|
|
data_size &= ~DELAY;
|
|
|
|
if (self->two_byte_sequence_length) {
|
2022-01-14 17:24:08 -05:00
|
|
|
data_size = ((data_size & ~DELAY) << 8) + *(cmd + 2);
|
2022-01-12 22:45:35 -05:00
|
|
|
data = cmd + 3;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_begin_transaction(&self->core);
|
2019-08-26 19:37:59 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, cmd, 1);
|
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, self->chip_select, data, data_size);
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
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;
|
2022-01-14 17:24:08 -05:00
|
|
|
if (self->two_byte_sequence_length) {
|
|
|
|
i++;
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 18:07:19 -04:00
|
|
|
void displayio_epaperdisplay_change_refresh_mode_parameters(displayio_epaperdisplay_obj_t *self,
|
|
|
|
mp_buffer_info_t *start_sequence, float seconds_per_frame) {
|
|
|
|
self->start_sequence = (uint8_t *)start_sequence->buf;
|
|
|
|
self->start_sequence_len = start_sequence->len;
|
|
|
|
self->milliseconds_per_frame = seconds_per_frame * 1000;
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
STATIC void displayio_epaperdisplay_start_refresh(displayio_epaperdisplay_obj_t *self) {
|
2019-08-02 19:17:38 -04:00
|
|
|
// run start sequence
|
2019-08-16 21:34:00 -04:00
|
|
|
self->core.bus_reset(self->core.bus);
|
2019-08-02 19:17:38 -04:00
|
|
|
|
2022-07-30 01:09:49 -04:00
|
|
|
common_hal_time_delay_ms(self->start_up_time_ms);
|
|
|
|
|
2019-08-02 19:17:38 -04:00
|
|
|
send_command_sequence(self, true, self->start_sequence, self->start_sequence_len);
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_start_refresh(&self->core);
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t common_hal_displayio_epaperdisplay_get_time_to_refresh(displayio_epaperdisplay_obj_t *self) {
|
2019-08-21 00:35:42 -04:00
|
|
|
if (self->core.last_refresh == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
// Refresh at seconds per frame rate.
|
2019-11-18 09:22:41 -05:00
|
|
|
uint32_t elapsed_time = supervisor_ticks_ms64() - self->core.last_refresh;
|
2019-08-14 17:17:35 -04:00
|
|
|
if (elapsed_time > self->milliseconds_per_frame) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return self->milliseconds_per_frame - elapsed_time;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
STATIC void displayio_epaperdisplay_finish_refresh(displayio_epaperdisplay_obj_t *self) {
|
2019-08-02 19:17:38 -04:00
|
|
|
// Actually refresh the display now that all pixel RAM has been updated.
|
2022-07-30 01:09:49 -04:00
|
|
|
send_command_sequence(self, false, self->refresh_sequence, self->refresh_sequence_len);
|
|
|
|
|
2020-07-08 11:32:06 -04:00
|
|
|
supervisor_enable_tick();
|
2019-08-02 19:17:38 -04:00
|
|
|
self->refreshing = true;
|
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_finish_refresh(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_t common_hal_displayio_epaperdisplay_get_bus(displayio_epaperdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->core.bus;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_displayio_epaperdisplay_set_rotation(displayio_epaperdisplay_obj_t *self, int rotation) {
|
2020-11-18 19:06:31 -05:00
|
|
|
bool transposed = (self->core.rotation == 90 || self->core.rotation == 270);
|
|
|
|
bool will_transposed = (rotation == 90 || rotation == 270);
|
2021-03-15 09:57:36 -04:00
|
|
|
if (transposed != will_transposed) {
|
2020-11-18 19:06:31 -05:00
|
|
|
int tmp = self->core.width;
|
|
|
|
self->core.width = self->core.height;
|
|
|
|
self->core.height = tmp;
|
|
|
|
}
|
|
|
|
displayio_display_core_set_rotation(&self->core, rotation);
|
|
|
|
if (self == &displays[0].epaper_display) {
|
|
|
|
supervisor_stop_terminal();
|
|
|
|
supervisor_start_terminal(self->core.width, self->core.height);
|
|
|
|
}
|
|
|
|
if (self->core.current_group != NULL) {
|
|
|
|
displayio_group_update_transform(self->core.current_group, &self->core.transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_epaperdisplay_get_rotation(displayio_epaperdisplay_obj_t *self) {
|
2020-11-18 19:06:31 -05:00
|
|
|
return self->core.rotation;
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:34:03 -05:00
|
|
|
mp_obj_t common_hal_displayio_epaperdisplay_get_root_group(displayio_epaperdisplay_obj_t *self) {
|
2022-12-01 18:34:43 -05:00
|
|
|
if (self->core.current_group == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-11-18 18:34:03 -05:00
|
|
|
return self->core.current_group;
|
|
|
|
}
|
2020-11-18 19:06:31 -05:00
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
STATIC bool displayio_epaperdisplay_refresh_area(displayio_epaperdisplay_obj_t *self, const displayio_area_t *area) {
|
2019-08-02 19:17:38 -04:00
|
|
|
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.
|
2019-08-16 21:34:00 -04:00
|
|
|
if (!displayio_display_core_clip_area(&self->core, area, &clipped)) {
|
2019-08-02 19:17:38 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint16_t subrectangles = 1;
|
|
|
|
uint16_t rows_per_buffer = displayio_area_height(&clipped);
|
2019-08-16 21:34:00 -04:00
|
|
|
uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / self->core.colorspace.depth;
|
2019-08-02 19:17:38 -04:00
|
|
|
uint16_t pixels_per_buffer = displayio_area_size(&clipped);
|
|
|
|
if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) {
|
|
|
|
rows_per_buffer = buffer_size * pixels_per_word / displayio_area_width(&clipped);
|
|
|
|
if (rows_per_buffer == 0) {
|
|
|
|
rows_per_buffer = 1;
|
|
|
|
}
|
|
|
|
subrectangles = displayio_area_height(&clipped) / rows_per_buffer;
|
|
|
|
if (displayio_area_height(&clipped) % rows_per_buffer != 0) {
|
|
|
|
subrectangles++;
|
|
|
|
}
|
|
|
|
pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped);
|
|
|
|
buffer_size = pixels_per_buffer / pixels_per_word;
|
|
|
|
if (pixels_per_buffer % pixels_per_word) {
|
|
|
|
buffer_size += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocated and shared as a uint32_t array so the compiler knows the
|
|
|
|
// alignment everywhere.
|
|
|
|
uint32_t buffer[buffer_size];
|
|
|
|
volatile uint32_t mask_length = (pixels_per_buffer / 32) + 1;
|
|
|
|
uint32_t mask[mask_length];
|
|
|
|
|
|
|
|
uint8_t passes = 1;
|
2022-09-06 13:32:15 -04:00
|
|
|
if (self->write_color_ram_command != NO_COMMAND) {
|
2019-08-02 19:17:38 -04:00
|
|
|
passes = 2;
|
|
|
|
}
|
|
|
|
for (uint8_t pass = 0; pass < passes; pass++) {
|
|
|
|
uint16_t remaining_rows = displayio_area_height(&clipped);
|
|
|
|
|
2023-04-20 19:15:06 -04:00
|
|
|
if (self->core.row_command != NO_COMMAND) {
|
|
|
|
displayio_display_core_set_region_to_update(&self->core, &clipped);
|
2019-08-16 21:34:00 -04:00
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
uint8_t write_command = self->write_black_ram_command;
|
|
|
|
if (pass == 1) {
|
|
|
|
write_command = self->write_color_ram_command;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_begin_transaction(&self->core);
|
2019-08-26 19:37:59 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, &write_command, 1);
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
for (uint16_t j = 0; j < subrectangles; j++) {
|
|
|
|
displayio_area_t subrectangle = {
|
|
|
|
.x1 = clipped.x1,
|
|
|
|
.y1 = clipped.y1 + rows_per_buffer * j,
|
|
|
|
.x2 = clipped.x2,
|
|
|
|
.y2 = clipped.y1 + rows_per_buffer * (j + 1)
|
|
|
|
};
|
|
|
|
if (remaining_rows < rows_per_buffer) {
|
|
|
|
subrectangle.y2 = subrectangle.y1 + remaining_rows;
|
|
|
|
}
|
|
|
|
remaining_rows -= rows_per_buffer;
|
|
|
|
|
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
uint16_t subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / self->core.colorspace.depth);
|
2019-08-02 19:17:38 -04:00
|
|
|
|
2019-08-26 19:37:59 -04:00
|
|
|
memset(mask, 0, mask_length * sizeof(mask[0]));
|
|
|
|
memset(buffer, 0, buffer_size * sizeof(buffer[0]));
|
2019-08-02 19:17:38 -04:00
|
|
|
|
2023-03-22 14:14:38 -04:00
|
|
|
if (!self->acep) {
|
2022-07-30 01:09:49 -04:00
|
|
|
self->core.colorspace.grayscale = true;
|
|
|
|
self->core.colorspace.grayscale_bit = 7;
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
if (pass == 1) {
|
2020-10-26 19:58:00 -04:00
|
|
|
if (self->grayscale) { // 4-color grayscale
|
|
|
|
self->core.colorspace.grayscale_bit = 6;
|
2022-09-06 13:32:15 -04:00
|
|
|
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
|
|
|
|
} else if (self->core.colorspace.tricolor) {
|
2020-10-26 19:58:00 -04:00
|
|
|
self->core.colorspace.grayscale = false;
|
2022-09-06 13:32:15 -04:00
|
|
|
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
|
2022-07-30 01:09:49 -04:00
|
|
|
} else if (self->core.colorspace.sevencolor) {
|
|
|
|
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
|
2020-10-26 19:58:00 -04:00
|
|
|
}
|
2022-09-06 13:32:15 -04:00
|
|
|
} else {
|
|
|
|
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert it all.
|
|
|
|
if ((pass == 1 && self->color_bits_inverted) ||
|
|
|
|
(pass == 0 && self->black_bits_inverted)) {
|
|
|
|
for (uint16_t k = 0; k < buffer_size; k++) {
|
|
|
|
buffer[k] = ~buffer[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
if (!displayio_display_core_begin_transaction(&self->core)) {
|
2019-08-02 19:17:38 -04:00
|
|
|
// Can't acquire display bus; skip the rest of the data. Try next display.
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, self->chip_select, (uint8_t *)buffer, subrectangle_size_bytes);
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
// TODO(tannewt): Make refresh displays faster so we don't starve other
|
|
|
|
// background tasks.
|
2021-09-25 14:30:00 -04:00
|
|
|
#if CIRCUITPY_USB
|
2019-08-02 19:17:38 -04:00
|
|
|
usb_background();
|
2021-09-25 14:30:00 -04:00
|
|
|
#endif
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-30 01:09:49 -04:00
|
|
|
STATIC bool _clean_area(displayio_epaperdisplay_obj_t *self) {
|
|
|
|
uint16_t width = displayio_display_core_get_width(&self->core);
|
|
|
|
uint16_t height = displayio_display_core_get_height(&self->core);
|
|
|
|
|
|
|
|
uint8_t buffer[width / 2];
|
|
|
|
memset(buffer, 0x77, width / 2);
|
|
|
|
|
|
|
|
uint8_t write_command = self->write_black_ram_command;
|
|
|
|
displayio_display_core_begin_transaction(&self->core);
|
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, self->chip_select, &write_command, 1);
|
|
|
|
displayio_display_core_end_transaction(&self->core);
|
|
|
|
|
|
|
|
for (uint16_t j = 0; j < height; j++) {
|
|
|
|
if (!displayio_display_core_begin_transaction(&self->core)) {
|
|
|
|
// Can't acquire display bus; skip the rest of the data. Try next display.
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-23 17:34:54 -05:00
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, self->chip_select, buffer, width / 2);
|
2022-07-30 01:09:49 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
|
|
|
|
|
|
|
// TODO(tannewt): Make refresh displays faster so we don't starve other
|
|
|
|
// background tasks.
|
|
|
|
#if CIRCUITPY_USB
|
|
|
|
usb_background();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_epaperdisplay_refresh(displayio_epaperdisplay_obj_t *self) {
|
2019-11-26 15:48:36 -05:00
|
|
|
|
2019-08-14 17:17:35 -04:00
|
|
|
if (self->refreshing && self->busy.base.type == &digitalio_digitalinout_type) {
|
|
|
|
if (common_hal_digitalio_digitalinout_get_value(&self->busy) != self->busy_state) {
|
2020-07-08 11:32:06 -04:00
|
|
|
supervisor_disable_tick();
|
2019-08-14 17:17:35 -04:00
|
|
|
self->refreshing = false;
|
|
|
|
// Run stop sequence but don't wait for busy because busy is set when sleeping.
|
|
|
|
send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
if (self->core.current_group == NULL) {
|
|
|
|
return true;
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
|
|
|
// Refresh at seconds per frame rate.
|
2019-08-16 21:34:00 -04:00
|
|
|
if (common_hal_displayio_epaperdisplay_get_time_to_refresh(self) > 0) {
|
2019-08-14 17:17:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
if (!displayio_display_core_bus_free(&self->core)) {
|
2019-08-14 17:17:35 -04:00
|
|
|
// Can't acquire display bus; skip updating this display. Try next display.
|
2019-08-16 21:34:00 -04:00
|
|
|
return false;
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
const displayio_area_t *current_area = displayio_epaperdisplay_get_refresh_areas(self);
|
2019-08-14 17:17:35 -04:00
|
|
|
if (current_area == NULL) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return true;
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
2022-07-30 01:09:49 -04:00
|
|
|
if (self->acep) {
|
|
|
|
displayio_epaperdisplay_start_refresh(self);
|
|
|
|
_clean_area(self);
|
|
|
|
displayio_epaperdisplay_finish_refresh(self);
|
|
|
|
while (self->refreshing && !mp_hal_is_interrupted()) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_epaperdisplay_start_refresh(self);
|
2019-08-14 17:17:35 -04:00
|
|
|
while (current_area != NULL) {
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_epaperdisplay_refresh_area(self, current_area);
|
2019-08-14 17:17:35 -04:00
|
|
|
current_area = current_area->next;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_epaperdisplay_finish_refresh(self);
|
|
|
|
return true;
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void displayio_epaperdisplay_background(displayio_epaperdisplay_obj_t *self) {
|
2019-08-21 16:15:35 -04:00
|
|
|
if (self->refreshing) {
|
|
|
|
bool refresh_done = false;
|
|
|
|
if (self->busy.base.type == &digitalio_digitalinout_type) {
|
|
|
|
bool busy = common_hal_digitalio_digitalinout_get_value(&self->busy);
|
|
|
|
refresh_done = busy != self->busy_state;
|
|
|
|
} else {
|
2019-11-18 09:22:41 -05:00
|
|
|
refresh_done = supervisor_ticks_ms64() - self->core.last_refresh > self->refresh_time;
|
2019-08-21 16:15:35 -04:00
|
|
|
}
|
|
|
|
if (refresh_done) {
|
2020-07-08 11:32:06 -04:00
|
|
|
supervisor_disable_tick();
|
2019-08-16 21:34:00 -04:00
|
|
|
self->refreshing = false;
|
|
|
|
// Run stop sequence but don't wait for busy because busy is set when sleeping.
|
|
|
|
send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len);
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_epaperdisplay_get_busy(displayio_epaperdisplay_obj_t *self) {
|
2020-11-11 19:53:45 -05:00
|
|
|
displayio_epaperdisplay_background(self);
|
|
|
|
return self->refreshing;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void release_epaperdisplay(displayio_epaperdisplay_obj_t *self) {
|
2019-08-21 00:35:42 -04:00
|
|
|
if (self->refreshing) {
|
|
|
|
wait_for_busy(self);
|
2020-07-08 11:32:06 -04:00
|
|
|
supervisor_disable_tick();
|
2019-08-21 00:35:42 -04:00
|
|
|
self->refreshing = false;
|
|
|
|
// Run stop sequence but don't wait for busy because busy is set when sleeping.
|
|
|
|
send_command_sequence(self, false, self->stop_sequence, self->stop_sequence_len);
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
release_display_core(&self->core);
|
2019-08-02 19:17:38 -04:00
|
|
|
if (self->busy.base.type == &digitalio_digitalinout_type) {
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->busy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void displayio_epaperdisplay_collect_ptrs(displayio_epaperdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_collect_ptrs(&self->core);
|
2021-03-15 09:57:36 -04:00
|
|
|
gc_collect_ptr((void *)self->start_sequence);
|
|
|
|
gc_collect_ptr((void *)self->stop_sequence);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
2019-08-21 00:35:42 -04:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
size_t maybe_refresh_epaperdisplay(void) {
|
2019-08-21 00:35:42 -04:00
|
|
|
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
|
|
|
|
if (displays[i].epaper_display.base.type != &displayio_epaperdisplay_type ||
|
|
|
|
displays[i].epaper_display.core.current_group != &circuitpython_splash) {
|
|
|
|
// Skip regular displays and those not showing the splash.
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
displayio_epaperdisplay_obj_t *display = &displays[i].epaper_display;
|
2021-04-16 19:18:01 -04:00
|
|
|
size_t time_to_refresh = common_hal_displayio_epaperdisplay_get_time_to_refresh(display);
|
|
|
|
if (time_to_refresh > 0) {
|
|
|
|
return time_to_refresh;
|
|
|
|
}
|
|
|
|
if (common_hal_displayio_epaperdisplay_refresh(display)) {
|
|
|
|
return 0;
|
2019-08-21 00:35:42 -04:00
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
// If we could refresh but it failed, then we want to retry.
|
|
|
|
return 1;
|
2019-08-21 00:35:42 -04:00
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
// Return 0 if no ePaper displays are available to pretend it was updated.
|
|
|
|
return 0;
|
2019-08-21 00:35:42 -04:00
|
|
|
}
|