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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/displayio/Display.h"
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/displayio/FourWire.h"
|
2019-07-05 22:01:54 -04:00
|
|
|
#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-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-03-15 09:57:36 -04:00
|
|
|
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, 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,
|
|
|
|
uint16_t brightness_command, mp_float_t brightness, bool auto_brightness,
|
|
|
|
bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second,
|
|
|
|
bool backlight_on_high, bool SH1107_addressing) {
|
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;
|
|
|
|
}
|
|
|
|
displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation,
|
2020-03-10 14:12:01 -04:00
|
|
|
color_depth, grayscale, pixels_in_byte_share_row, bytes_per_cell, reverse_pixels_in_byte, reverse_bytes_in_word);
|
2019-08-16 21:34:00 -04:00
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
self->set_column_command = set_column_command;
|
|
|
|
self->set_row_command = set_row_command;
|
|
|
|
self->write_ram_command = write_ram_command;
|
2019-07-05 22:01:54 -04:00
|
|
|
self->brightness_command = brightness_command;
|
2019-04-18 15:59:16 -04:00
|
|
|
self->auto_brightness = auto_brightness;
|
2019-08-22 03:33:27 -04:00
|
|
|
self->first_manual_refresh = !auto_refresh;
|
2019-04-05 02:15:00 -04:00
|
|
|
self->data_as_commands = data_as_commands;
|
2020-04-08 13:32:54 -04:00
|
|
|
self->backlight_on_high = backlight_on_high;
|
2020-09-24 22:07:33 -04:00
|
|
|
self->SH1107_addressing = SH1107_addressing;
|
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;
|
2019-08-22 03:33:27 -04:00
|
|
|
while (!displayio_display_core_begin_transaction(&self->core)) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
2019-04-05 02:15:00 -04:00
|
|
|
if (self->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);
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, full_command, data_size + 1);
|
2019-04-05 02:15:00 -04:00
|
|
|
} else {
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, cmd, 1);
|
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, data, data_size);
|
2019-04-05 02:15:00 -04:00
|
|
|
}
|
2019-09-03 17:46:47 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
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)
|
2020-08-18 16:08:33 -04:00
|
|
|
pwmout_result_t result = common_hal_pwmio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 50000, 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
|
|
|
}
|
2019-07-19 19:05:13 -04:00
|
|
|
if (!self->auto_brightness && (self->backlight_inout.base.type != &mp_type_NoneType ||
|
|
|
|
brightness_command != NO_BRIGHTNESS_COMMAND)) {
|
2019-07-05 22:01:54 -04:00
|
|
|
common_hal_displayio_display_set_brightness(self, brightness);
|
|
|
|
} else {
|
|
|
|
self->current_brightness = -1.0;
|
|
|
|
}
|
2019-06-06 18:11:02 -04:00
|
|
|
|
|
|
|
// Set the group after initialization otherwise we may send pixels while we delay in
|
|
|
|
// initialization.
|
|
|
|
common_hal_displayio_display_show(self, &circuitpython_splash);
|
2020-07-08 11:32:06 -04:00
|
|
|
common_hal_displayio_display_set_auto_refresh(self, auto_refresh);
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_display_show(displayio_display_obj_t *self, displayio_group_t *root_group) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_show(&self->core, root_group);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_display_get_width(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_width(&self->core);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_display_get_height(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return displayio_display_core_get_height(&self->core);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->auto_brightness;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t *self, bool auto_brightness) {
|
2019-08-16 21:34:00 -04:00
|
|
|
self->auto_brightness = auto_brightness;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->current_brightness;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t *self, mp_float_t brightness) {
|
2019-08-16 21:34:00 -04:00
|
|
|
self->updating_backlight = true;
|
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) {
|
|
|
|
ok = displayio_display_core_begin_transaction(&self->core);
|
|
|
|
if (ok) {
|
|
|
|
if (self->data_as_commands) {
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t set_brightness[2] = {self->brightness_command, (uint8_t)(0xff * brightness)};
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.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;
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &command, 1);
|
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, &hex_brightness, 1);
|
2019-08-16 21:34:00 -04:00
|
|
|
}
|
|
|
|
displayio_display_core_end_transaction(&self->core);
|
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
|
|
|
self->updating_backlight = false;
|
|
|
|
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
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_t common_hal_displayio_display_get_bus(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->core.bus;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC const displayio_area_t *_get_refresh_areas(displayio_display_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
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void _send_pixels(displayio_display_obj_t *self, uint8_t *pixels, uint32_t length) {
|
2019-08-16 21:34:00 -04:00
|
|
|
if (!self->data_as_commands) {
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &self->write_ram_command, 1);
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
2019-08-23 18:27:21 -04:00
|
|
|
self->core.send(self->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, pixels, length);
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC bool _refresh_area(displayio_display_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
|
|
|
|
if (self->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
|
|
|
|
2020-09-29 15:37:06 -04:00
|
|
|
displayio_display_core_set_region_to_update(&self->core, self->set_column_command,
|
2021-03-15 09:57:36 -04:00
|
|
|
self->set_row_command, NO_COMMAND, NO_COMMAND, self->data_as_commands, false,
|
|
|
|
&subrectangle, self->SH1107_addressing);
|
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.
|
|
|
|
if (!displayio_display_core_bus_free(&self->core)) {
|
2019-08-14 17:17:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-16 21:34:00 -04:00
|
|
|
|
|
|
|
displayio_display_core_begin_transaction(&self->core);
|
2021-03-15 09:57:36 -04:00
|
|
|
_send_pixels(self, (uint8_t *)buffer, subrectangle_size_bytes);
|
2019-08-16 21:34:00 -04:00
|
|
|
displayio_display_core_end_transaction(&self->core);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void _refresh_display(displayio_display_obj_t *self) {
|
2020-09-11 22:37:00 -04:00
|
|
|
if (!displayio_display_core_start_refresh(&self->core)) {
|
|
|
|
// 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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_displayio_display_set_rotation(displayio_display_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t common_hal_displayio_display_get_rotation(displayio_display_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
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_display_refresh(displayio_display_obj_t *self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) {
|
|
|
|
if (!self->auto_refresh && !self->first_manual_refresh && (target_ms_per_frame != 0xffffffff)) {
|
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) {
|
2019-08-16 21:34:00 -04:00
|
|
|
mp_raise_RuntimeError(translate("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
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_displayio_display_get_auto_refresh(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
return self->auto_refresh;
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_displayio_display_set_auto_refresh(displayio_display_obj_t *self,
|
|
|
|
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
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void _update_backlight(displayio_display_obj_t *self) {
|
2019-01-25 21:31:27 -05:00
|
|
|
if (!self->auto_brightness || self->updating_backlight) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-18 09:22:41 -05:00
|
|
|
if (supervisor_ticks_ms64() - self->last_backlight_refresh < 100) {
|
2019-01-25 21:31:27 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-03-19 09:57:21 -04:00
|
|
|
// TODO(tannewt): Fade the backlight based on its existing value and a target value. The target
|
2019-01-28 21:23:32 -05:00
|
|
|
// should account for ambient light when possible.
|
|
|
|
common_hal_displayio_display_set_brightness(self, 1.0);
|
|
|
|
|
2019-11-18 09:22:41 -05:00
|
|
|
self->last_backlight_refresh = supervisor_ticks_ms64();
|
2019-01-25 21:31:27 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void displayio_display_background(displayio_display_obj_t *self) {
|
2019-08-16 21:34:00 -04:00
|
|
|
_update_backlight(self);
|
2019-08-14 17:17:35 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void release_display(displayio_display_obj_t *self) {
|
2020-07-08 11:32:06 -04:00
|
|
|
common_hal_displayio_display_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_reset_ok(&self->backlight_pwm);
|
|
|
|
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
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void reset_display(displayio_display_obj_t *self) {
|
2020-07-08 11:32:06 -04:00
|
|
|
common_hal_displayio_display_set_auto_refresh(self, true);
|
2019-08-22 03:33:27 -04:00
|
|
|
self->auto_brightness = true;
|
|
|
|
common_hal_displayio_display_show(self, NULL);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void displayio_display_collect_ptrs(displayio_display_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
|
|
|
}
|