2019-01-16 15:04:42 -05:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
#include "shared-bindings/busdisplay/BusDisplay.h"
|
2019-01-16 15:04:42 -05:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2023-10-17 19:09:47 -04:00
|
|
|
#if CIRCUITPY_FOURWIRE
|
|
|
|
#include "shared-bindings/fourwire/FourWire.h"
|
|
|
|
#endif
|
|
|
|
#if CIRCUITPY_I2CDISPLAYBUS
|
|
|
|
#include "shared-bindings/i2cdisplaybus/I2CDisplayBus.h"
|
|
|
|
#endif
|
|
|
|
#if CIRCUITPY_PARALLELDISPLAYBUS
|
|
|
|
#include "shared-bindings/paralleldisplaybus/ParallelBus.h"
|
2021-08-29 12:49:47 -04:00
|
|
|
#endif
|
2019-01-25 21:31:27 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2019-01-23 17:43:54 -05:00
|
|
|
#include "shared-bindings/time/__init__.h"
|
2019-01-25 19:59:18 -05:00
|
|
|
#include "shared-module/displayio/__init__.h"
|
2019-08-16 21:34:00 -04:00
|
|
|
#include "shared-module/displayio/display_core.h"
|
2019-01-25 19:59:18 -05:00
|
|
|
#include "supervisor/shared/display.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/shared/tick.h"
|
2019-08-16 21:34:00 -04:00
|
|
|
#include "supervisor/usb.h"
|
2019-01-16 15:04:42 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-07-05 22:01:54 -04:00
|
|
|
#include <string.h>
|
2019-01-16 15:04:42 -05:00
|
|
|
|
2021-11-23 20:23:13 -05:00
|
|
|
#define DELAY 0x80
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_busdisplay_busdisplay_construct(busdisplay_busdisplay_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart,
|
|
|
|
uint16_t rotation, uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row,
|
|
|
|
uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word, uint8_t set_column_command,
|
2021-08-23 10:17:59 -04:00
|
|
|
uint8_t set_row_command, uint8_t write_ram_command,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t *init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t *backlight_pin,
|
2022-08-09 22:24:57 -04:00
|
|
|
uint16_t brightness_command, mp_float_t brightness,
|
2021-03-15 09:57:36 -04:00
|
|
|
bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second,
|
2022-05-28 13:01:05 -04:00
|
|
|
bool backlight_on_high, bool SH1107_addressing, uint16_t backlight_pwm_frequency) {
|
2020-09-24 22:07:33 -04:00
|
|
|
|
2019-08-27 19:03:05 -04:00
|
|
|
// Turn off auto-refresh as we init.
|
|
|
|
self->auto_refresh = false;
|
2019-08-16 21:34:00 -04:00
|
|
|
uint16_t ram_width = 0x100;
|
|
|
|
uint16_t ram_height = 0x100;
|
|
|
|
if (single_byte_bounds) {
|
|
|
|
ram_width = 0xff;
|
|
|
|
ram_height = 0xff;
|
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_core_construct(&self->core, width, height, rotation,
|
|
|
|
color_depth, grayscale, pixels_in_byte_share_row, bytes_per_cell, reverse_pixels_in_byte, reverse_bytes_in_word);
|
|
|
|
displayio_display_bus_construct(&self->bus, bus, ram_width, ram_height, colstart, rowstart,
|
2023-04-25 13:36:34 -04:00
|
|
|
set_column_command, set_row_command, NO_COMMAND, NO_COMMAND, data_as_commands, false /* always_toggle_chip_select */,
|
2023-04-20 19:15:06 -04:00
|
|
|
SH1107_addressing && color_depth == 1, false /*address_little_endian */);
|
2019-08-16 21:34:00 -04:00
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
self->write_ram_command = write_ram_command;
|
2019-07-05 22:01:54 -04:00
|
|
|
self->brightness_command = brightness_command;
|
2019-08-22 03:33:27 -04:00
|
|
|
self->first_manual_refresh = !auto_refresh;
|
2020-04-08 13:32:54 -04:00
|
|
|
self->backlight_on_high = backlight_on_high;
|
2019-08-16 21:34:00 -04:00
|
|
|
|
|
|
|
self->native_frames_per_second = native_frames_per_second;
|
2019-08-23 18:27:21 -04:00
|
|
|
self->native_ms_per_frame = 1000 / native_frames_per_second;
|
2019-01-16 15:04:42 -05:00
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < init_sequence_len) {
|
|
|
|
uint8_t *cmd = init_sequence + i;
|
|
|
|
uint8_t data_size = *(cmd + 1);
|
|
|
|
bool delay = (data_size & DELAY) != 0;
|
|
|
|
data_size &= ~DELAY;
|
|
|
|
uint8_t *data = cmd + 2;
|
2023-10-17 19:09:47 -04:00
|
|
|
while (!displayio_display_bus_begin_transaction(&self->bus)) {
|
2019-08-22 03:33:27 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
if (self->bus.data_as_commands) {
|
2019-07-05 22:01:54 -04:00
|
|
|
uint8_t full_command[data_size + 1];
|
|
|
|
full_command[0] = cmd[0];
|
|
|
|
memcpy(full_command + 1, data, data_size);
|
2023-10-17 19:09:47 -04:00
|
|
|
self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, full_command, data_size + 1);
|
2019-04-05 02:15:00 -04:00
|
|
|
} else {
|
2023-10-17 19:09:47 -04:00
|
|
|
self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, cmd, 1);
|
|
|
|
self->bus.send(self->bus.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, data, data_size);
|
2019-04-05 02:15:00 -04:00
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_bus_end_transaction(&self->bus);
|
2019-01-23 17:43:54 -05:00
|
|
|
uint16_t delay_length_ms = 10;
|
2019-01-16 15:04:42 -05:00
|
|
|
if (delay) {
|
|
|
|
data_size++;
|
2019-01-23 17:43:54 -05:00
|
|
|
delay_length_ms = *(cmd + 1 + data_size);
|
2019-01-16 15:04:42 -05:00
|
|
|
if (delay_length_ms == 255) {
|
|
|
|
delay_length_ms = 500;
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 17:43:54 -05:00
|
|
|
common_hal_time_delay_ms(delay_length_ms);
|
2019-01-16 15:04:42 -05:00
|
|
|
i += 2 + data_size;
|
|
|
|
}
|
2019-01-25 19:59:18 -05:00
|
|
|
|
2019-01-31 14:00:12 -05:00
|
|
|
// Always set the backlight type in case we're reusing memory.
|
|
|
|
self->backlight_inout.base.type = &mp_type_NoneType;
|
2019-01-25 21:31:27 -05:00
|
|
|
if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) {
|
2020-04-30 14:46:51 -04:00
|
|
|
// Avoid PWM types and functions when the module isn't enabled
|
2021-03-19 09:57:21 -04:00
|
|
|
#if (CIRCUITPY_PWMIO)
|
2022-05-28 13:01:05 -04:00
|
|
|
pwmout_result_t result = common_hal_pwmio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, backlight_pwm_frequency, false);
|
2019-01-25 21:31:27 -05:00
|
|
|
if (result != PWMOUT_OK) {
|
|
|
|
self->backlight_inout.base.type = &digitalio_digitalinout_type;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin);
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_never_reset_pin(backlight_pin);
|
2019-01-25 21:31:27 -05:00
|
|
|
} else {
|
2020-08-18 16:08:33 -04:00
|
|
|
self->backlight_pwm.base.type = &pwmio_pwmout_type;
|
|
|
|
common_hal_pwmio_pwmout_never_reset(&self->backlight_pwm);
|
2019-01-25 21:31:27 -05:00
|
|
|
}
|
2020-04-30 14:46:51 -04:00
|
|
|
#else
|
|
|
|
// Otherwise default to digital
|
|
|
|
self->backlight_inout.base.type = &digitalio_digitalinout_type;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin);
|
|
|
|
common_hal_never_reset_pin(backlight_pin);
|
|
|
|
#endif
|
2019-01-25 21:31:27 -05:00
|
|
|
}
|
2022-08-09 22:24:57 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_busdisplay_busdisplay_set_brightness(self, brightness);
|
2019-06-06 18:11:02 -04:00
|
|
|
|
|
|
|
// Set the group after initialization otherwise we may send pixels while we delay in
|
|
|
|
// initialization.
|
2023-04-03 16:43:29 -04:00
|
|
|
if (!circuitpython_splash.in_group) {
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_busdisplay_busdisplay_set_root_group(self, &circuitpython_splash);
|
2023-04-03 16:43:29 -04:00
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_busdisplay_busdisplay_set_auto_refresh(self, auto_refresh);
|
2022-11-21 21:21:39 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
uint16_t common_hal_busdisplay_busdisplay_get_width(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_width(&self->core);
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
uint16_t common_hal_busdisplay_busdisplay_get_height(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_height(&self->core);
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
mp_float_t common_hal_busdisplay_busdisplay_get_brightness(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->current_brightness;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_busdisplay_busdisplay_set_brightness(busdisplay_busdisplay_obj_t *self, mp_float_t brightness) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (!self->backlight_on_high) {
|
|
|
|
brightness = 1.0 - brightness;
|
2020-03-26 01:43:18 -04:00
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
bool ok = false;
|
2020-05-30 05:44:13 -04:00
|
|
|
|
2020-04-30 14:46:51 -04:00
|
|
|
// Avoid PWM types and functions when the module isn't enabled
|
2021-03-19 09:57:21 -04:00
|
|
|
#if (CIRCUITPY_PWMIO)
|
2020-08-18 16:08:33 -04:00
|
|
|
bool ispwm = (self->backlight_pwm.base.type == &pwmio_pwmout_type) ? true : false;
|
2020-04-30 14:46:51 -04:00
|
|
|
#else
|
|
|
|
bool ispwm = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ispwm) {
|
2021-03-19 09:57:21 -04:00
|
|
|
#if (CIRCUITPY_PWMIO)
|
2021-03-15 09:57:36 -04:00
|
|
|
common_hal_pwmio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t)(0xffff * brightness));
|
2019-08-16 21:34:00 -04:00
|
|
|
ok = true;
|
2020-04-30 14:46:51 -04:00
|
|
|
#else
|
|
|
|
ok = false;
|
|
|
|
#endif
|
2019-08-16 21:34:00 -04:00
|
|
|
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
|
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
|
|
|
|
ok = true;
|
|
|
|
} else if (self->brightness_command != NO_BRIGHTNESS_COMMAND) {
|
2023-10-17 19:09:47 -04:00
|
|
|
ok = displayio_display_bus_begin_transaction(&self->bus);
|
2019-08-16 21:34:00 -04:00
|
|
|
if (ok) {
|
2023-10-17 19:09:47 -04:00
|
|
|
if (self->bus.data_as_commands) {
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t set_brightness[2] = {self->brightness_command, (uint8_t)(0xff * brightness)};
|
2023-10-17 19:09:47 -04:00
|
|
|
self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, set_brightness, 2);
|
2019-08-16 21:34:00 -04:00
|
|
|
} else {
|
|
|
|
uint8_t command = self->brightness_command;
|
|
|
|
uint8_t hex_brightness = 0xff * brightness;
|
2023-10-17 19:09:47 -04:00
|
|
|
self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &command, 1);
|
|
|
|
self->bus.send(self->bus.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, &hex_brightness, 1);
|
2019-08-16 21:34:00 -04:00
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_bus_end_transaction(&self->bus);
|
2019-08-14 18:53:58 -04:00
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
|
2019-01-25 19:59:18 -05:00
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
if (ok) {
|
|
|
|
self->current_brightness = brightness;
|
2019-06-06 18:11:02 -04:00
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
return ok;
|
|
|
|
}
|
2019-08-14 18:53:58 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
mp_obj_t common_hal_busdisplay_busdisplay_get_bus(busdisplay_busdisplay_obj_t *self) {
|
|
|
|
return self->bus.bus;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
mp_obj_t common_hal_busdisplay_busdisplay_get_root_group(busdisplay_busdisplay_obj_t *self) {
|
2022-11-28 10:03:03 -05:00
|
|
|
if (self->core.current_group == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-02-01 09:28:09 -05:00
|
|
|
return self->core.current_group;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
STATIC const displayio_area_t *_get_refresh_areas(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
if (self->core.full_refresh) {
|
|
|
|
self->core.area.next = NULL;
|
|
|
|
return &self->core.area;
|
2019-08-22 03:33:27 -04:00
|
|
|
} else if (self->core.current_group != NULL) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_group_get_refresh_areas(self->core.current_group, NULL);
|
2019-06-06 18:11:02 -04:00
|
|
|
}
|
2019-08-22 03:33:27 -04:00
|
|
|
return NULL;
|
2019-06-06 18:11:02 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
STATIC void _send_pixels(busdisplay_busdisplay_obj_t *self, uint8_t *pixels, uint32_t length) {
|
|
|
|
if (!self->bus.data_as_commands) {
|
|
|
|
self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &self->write_ram_command, 1);
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
self->bus.send(self->bus.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, pixels, length);
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
STATIC bool _refresh_area(busdisplay_busdisplay_obj_t *self, const displayio_area_t *area) {
|
2019-08-14 17:17:35 -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-14 17:17:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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-14 17:17:35 -04:00
|
|
|
uint16_t pixels_per_buffer = displayio_area_size(&clipped);
|
2020-09-29 15:37:06 -04:00
|
|
|
|
2020-09-24 22:07:33 -04:00
|
|
|
uint16_t subrectangles = 1;
|
|
|
|
// for SH1107 and other boundary constrained controllers
|
|
|
|
// write one single row at a time
|
2023-10-17 19:09:47 -04:00
|
|
|
if (self->bus.SH1107_addressing) {
|
2021-06-07 19:51:57 -04:00
|
|
|
subrectangles = rows_per_buffer / 8; // page addressing mode writes 8 rows at a time
|
2021-06-06 18:25:40 -04:00
|
|
|
rows_per_buffer = 8;
|
2020-09-25 21:27:29 -04:00
|
|
|
} else if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) {
|
2019-08-14 17:17:35 -04:00
|
|
|
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.
|
2019-08-16 21:34:00 -04:00
|
|
|
if (self->core.colorspace.depth < 8 && !self->core.colorspace.pixels_in_byte_share_row) {
|
|
|
|
uint8_t pixels_per_byte = 8 / self->core.colorspace.depth;
|
2019-08-14 17:17:35 -04:00
|
|
|
if (rows_per_buffer % pixels_per_byte != 0) {
|
|
|
|
rows_per_buffer -= rows_per_buffer % pixels_per_byte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subrectangles = displayio_area_height(&clipped) / rows_per_buffer;
|
|
|
|
if (displayio_area_height(&clipped) % rows_per_buffer != 0) {
|
|
|
|
subrectangles++;
|
|
|
|
}
|
|
|
|
pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped);
|
|
|
|
buffer_size = pixels_per_buffer / pixels_per_word;
|
|
|
|
if (pixels_per_buffer % pixels_per_word) {
|
|
|
|
buffer_size += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocated and shared as a uint32_t array so the compiler knows the
|
|
|
|
// alignment everywhere.
|
|
|
|
uint32_t buffer[buffer_size];
|
2019-08-22 03:33:27 -04:00
|
|
|
uint32_t mask_length = (pixels_per_buffer / 32) + 1;
|
2019-08-14 17:17:35 -04:00
|
|
|
uint32_t mask[mask_length];
|
|
|
|
uint16_t remaining_rows = displayio_area_height(&clipped);
|
|
|
|
|
|
|
|
for (uint16_t j = 0; j < subrectangles; j++) {
|
|
|
|
displayio_area_t subrectangle = {
|
|
|
|
.x1 = clipped.x1,
|
|
|
|
.y1 = clipped.y1 + rows_per_buffer * j,
|
|
|
|
.x2 = clipped.x2,
|
|
|
|
.y2 = clipped.y1 + rows_per_buffer * (j + 1)
|
|
|
|
};
|
2020-09-29 19:48:14 -04:00
|
|
|
if (remaining_rows < rows_per_buffer) {
|
2019-08-14 17:17:35 -04:00
|
|
|
subrectangle.y2 = subrectangle.y1 + remaining_rows;
|
|
|
|
}
|
2020-09-25 21:27:29 -04:00
|
|
|
remaining_rows -= rows_per_buffer;
|
2019-08-14 17:17:35 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_bus_set_region_to_update(&self->bus, &self->core, &subrectangle);
|
2019-08-14 17:17:35 -04:00
|
|
|
|
|
|
|
uint16_t subrectangle_size_bytes;
|
2019-08-16 21:34:00 -04:00
|
|
|
if (self->core.colorspace.depth >= 8) {
|
|
|
|
subrectangle_size_bytes = displayio_area_size(&subrectangle) * (self->core.colorspace.depth / 8);
|
2019-08-14 17:17:35 -04:00
|
|
|
} else {
|
2019-08-16 21:34:00 -04:00
|
|
|
subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / self->core.colorspace.depth);
|
2019-08-14 17:17:35 -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-14 17:17:35 -04:00
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
|
2019-08-14 17:17:35 -04:00
|
|
|
|
2019-08-16 21:34:00 -04:00
|
|
|
// Can't acquire display bus; skip the rest of the data.
|
2023-10-17 19:09:47 -04:00
|
|
|
if (!displayio_display_bus_is_free(&self->bus)) {
|
2019-08-14 17:17:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_bus_begin_transaction(&self->bus);
|
2021-03-15 09:57:36 -04:00
|
|
|
_send_pixels(self, (uint8_t *)buffer, subrectangle_size_bytes);
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_bus_end_transaction(&self->bus);
|
2019-08-14 17:17:35 -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-14 17:17:35 -04:00
|
|
|
usb_background();
|
2021-09-25 14:30:00 -04:00
|
|
|
#endif
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
STATIC void _refresh_display(busdisplay_busdisplay_obj_t *self) {
|
|
|
|
if (!displayio_display_bus_is_free(&self->bus)) {
|
2020-09-11 22:37:00 -04:00
|
|
|
// A refresh on this bus is already in progress. Try next display.
|
2019-08-16 21:34:00 -04:00
|
|
|
return;
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
2023-10-17 19:09:47 -04:00
|
|
|
displayio_display_core_start_refresh(&self->core);
|
2021-03-15 09:57:36 -04:00
|
|
|
const displayio_area_t *current_area = _get_refresh_areas(self);
|
2019-08-14 17:17:35 -04:00
|
|
|
while (current_area != NULL) {
|
2019-08-16 21:34:00 -04:00
|
|
|
_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_display_core_finish_refresh(&self->core);
|
2019-02-25 19:39:20 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_busdisplay_busdisplay_set_rotation(busdisplay_busdisplay_obj_t *self, int rotation) {
|
2019-12-16 16:23:41 -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) {
|
2019-12-16 16:23:41 -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);
|
2020-10-13 17:57:09 -04:00
|
|
|
if (self == &displays[0].display) {
|
|
|
|
supervisor_stop_terminal();
|
|
|
|
supervisor_start_terminal(self->core.width, self->core.height);
|
|
|
|
}
|
2019-12-16 16:23:41 -05:00
|
|
|
if (self->core.current_group != NULL) {
|
|
|
|
displayio_group_update_transform(self->core.current_group, &self->core.transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
uint16_t common_hal_busdisplay_busdisplay_get_rotation(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->core.rotation;
|
2019-01-28 21:23:32 -05:00
|
|
|
}
|
|
|
|
|
2019-12-16 16:23:41 -05:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_busdisplay_busdisplay_refresh(busdisplay_busdisplay_obj_t *self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) {
|
2023-10-02 16:44:28 -04:00
|
|
|
if (!self->auto_refresh && !self->first_manual_refresh && (target_ms_per_frame != NO_FPS_LIMIT)) {
|
2019-11-18 09:22:41 -05:00
|
|
|
uint64_t current_time = supervisor_ticks_ms64();
|
2019-08-23 18:27:21 -04:00
|
|
|
uint32_t current_ms_since_real_refresh = current_time - self->core.last_refresh;
|
2019-08-16 21:34:00 -04:00
|
|
|
// Test to see if the real frame time is below our minimum.
|
2019-08-23 18:27:21 -04:00
|
|
|
if (current_ms_since_real_refresh > maximum_ms_per_real_frame) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_RuntimeError(MP_ERROR_TEXT("Below minimum frame rate"));
|
2019-07-05 22:01:54 -04:00
|
|
|
}
|
2019-08-23 18:27:21 -04:00
|
|
|
uint32_t current_ms_since_last_call = current_time - self->last_refresh_call;
|
2019-08-16 21:34:00 -04:00
|
|
|
self->last_refresh_call = current_time;
|
|
|
|
// Skip the actual refresh to help catch up.
|
2019-08-23 18:27:21 -04:00
|
|
|
if (current_ms_since_last_call > target_ms_per_frame) {
|
|
|
|
return false;
|
2019-08-16 21:34:00 -04:00
|
|
|
}
|
2019-08-23 18:27:21 -04:00
|
|
|
uint32_t remaining_time = target_ms_per_frame - (current_ms_since_real_refresh % target_ms_per_frame);
|
2019-08-16 21:34:00 -04:00
|
|
|
// We're ahead of the game so wait until we align with the frame rate.
|
2019-11-18 09:22:41 -05:00
|
|
|
while (supervisor_ticks_ms64() - self->last_refresh_call < remaining_time) {
|
2019-08-23 18:27:21 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2019-07-05 22:01:54 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 03:33:27 -04:00
|
|
|
self->first_manual_refresh = false;
|
2019-08-16 21:34:00 -04:00
|
|
|
_refresh_display(self);
|
2019-08-23 18:27:21 -04:00
|
|
|
return true;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_busdisplay_busdisplay_get_auto_refresh(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->auto_refresh;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_busdisplay_busdisplay_set_auto_refresh(busdisplay_busdisplay_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
bool auto_refresh) {
|
2019-08-22 03:33:27 -04:00
|
|
|
self->first_manual_refresh = !auto_refresh;
|
2020-07-08 11:32:06 -04:00
|
|
|
if (auto_refresh != self->auto_refresh) {
|
|
|
|
if (auto_refresh) {
|
|
|
|
supervisor_enable_tick();
|
|
|
|
} else {
|
|
|
|
supervisor_disable_tick();
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
self->auto_refresh = auto_refresh;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
2019-01-25 21:31:27 -05:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
mp_obj_t common_hal_busdisplay_busdisplay_set_root_group(busdisplay_busdisplay_obj_t *self, displayio_group_t *root_group) {
|
2022-11-18 17:53:18 -05:00
|
|
|
bool ok = displayio_display_core_set_root_group(&self->core, root_group);
|
|
|
|
if (!ok) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("Group already used"));
|
2022-11-18 17:53:18 -05:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self) {
|
2019-11-18 09:22:41 -05:00
|
|
|
if (self->auto_refresh && (supervisor_ticks_ms64() - self->core.last_refresh) > self->native_ms_per_frame) {
|
2019-08-16 21:34:00 -04:00
|
|
|
_refresh_display(self);
|
2019-08-14 17:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void release_busdisplay(busdisplay_busdisplay_obj_t *self) {
|
|
|
|
common_hal_busdisplay_busdisplay_set_auto_refresh(self, false);
|
2019-08-16 21:34:00 -04:00
|
|
|
release_display_core(&self->core);
|
2021-03-19 09:57:21 -04:00
|
|
|
#if (CIRCUITPY_PWMIO)
|
2020-08-18 16:08:33 -04:00
|
|
|
if (self->backlight_pwm.base.type == &pwmio_pwmout_type) {
|
|
|
|
common_hal_pwmio_pwmout_deinit(&self->backlight_pwm);
|
2019-01-25 21:31:27 -05:00
|
|
|
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
|
|
|
|
}
|
2020-04-30 14:46:51 -04:00
|
|
|
#else
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
|
|
|
|
#endif
|
2019-01-25 21:31:27 -05:00
|
|
|
}
|
2019-06-06 18:11:02 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void reset_busdisplay(busdisplay_busdisplay_obj_t *self) {
|
|
|
|
common_hal_busdisplay_busdisplay_set_auto_refresh(self, true);
|
2022-11-27 14:54:53 -05:00
|
|
|
circuitpython_splash.x = 0; // reset position in case someone moved it.
|
|
|
|
circuitpython_splash.y = 0;
|
|
|
|
supervisor_start_terminal(self->core.width, self->core.height);
|
2023-04-04 21:31:42 -04:00
|
|
|
if (!circuitpython_splash.in_group) {
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_busdisplay_busdisplay_set_root_group(self, &circuitpython_splash);
|
2023-04-04 21:31:42 -04:00
|
|
|
}
|
2019-08-22 03:33:27 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void busdisplay_busdisplay_collect_ptrs(busdisplay_busdisplay_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_collect_ptrs(&self->core);
|
2019-06-13 03:34:19 -04:00
|
|
|
}
|