diff --git a/shared-module/framebufferio/FramebufferDisplay.c b/shared-module/framebufferio/FramebufferDisplay.c index 230dd6991d..37989c29f7 100644 --- a/shared-module/framebufferio/FramebufferDisplay.c +++ b/shared-module/framebufferio/FramebufferDisplay.c @@ -40,6 +40,11 @@ #include #include +#define fb_getter_default(method, default_value) \ + (self->framebuffer_protocol->method \ + ? self->framebuffer_protocol->method(self->framebuffer) \ + : (default_value)) + void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebufferdisplay_obj_t* self, mp_obj_t framebuffer, uint16_t rotation, @@ -51,7 +56,7 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu uint16_t ram_width = 0x100; uint16_t ram_height = 0x100; - uint16_t depth = self->framebuffer_protocol->get_color_depth(self->framebuffer); + uint16_t depth = fb_getter_default(get_color_depth, 16); displayio_display_core_construct( &self->core, NULL, @@ -63,19 +68,15 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu 0, rotation, depth, - (depth < 12), // grayscale - true, // pixels_in_byte_share_row - self->framebuffer_protocol->get_bytes_per_cell(self->framebuffer), - true, // reverse_pixels_in_byte - false // reverse_bytes_in_word + fb_getter_default(get_grayscale, (depth < 8)), + fb_getter_default(get_pixels_in_byte_share_row, false), + fb_getter_default(get_bytes_per_cell, 2), + fb_getter_default(get_reverse_pixels_in_byte, false), + fb_getter_default(get_reverse_pixels_in_word, false) ); - self->first_pixel_offset = self->framebuffer_protocol->get_first_pixel_offset - ? self->framebuffer_protocol->get_first_pixel_offset(self->framebuffer) - : 0; - self->row_stride = self->framebuffer_protocol->get_row_stride - ? self->framebuffer_protocol->get_row_stride(self->framebuffer) - : 0; + self->first_pixel_offset = fb_getter_default(get_first_pixel_offset, 0); + self->row_stride = fb_getter_default(get_row_stride, 0); if (self->row_stride == 0) { self->row_stride = self->core.width * self->core.colorspace.depth/8; } @@ -88,7 +89,7 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu self->first_manual_refresh = !auto_refresh; - self->native_frames_per_second = self->framebuffer_protocol->get_native_frames_per_second(self->framebuffer); + self->native_frames_per_second = fb_getter_default(get_native_frames_per_second, 60); self->native_ms_per_frame = 1000 / self->native_frames_per_second; supervisor_start_terminal(self->core.width, self->core.height); diff --git a/shared-module/framebufferio/FramebufferDisplay.h b/shared-module/framebufferio/FramebufferDisplay.h index 1d207039ac..93ad0118af 100644 --- a/shared-module/framebufferio/FramebufferDisplay.h +++ b/shared-module/framebufferio/FramebufferDisplay.h @@ -62,35 +62,51 @@ void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisp mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t* self); +typedef bool (*framebuffer_get_auto_brightness_fun)(mp_obj_t); +typedef bool (*framebuffer_get_reverse_pixels_in_byte_fun)(mp_obj_t); +typedef bool (*framebuffer_get_reverse_pixels_in_word_fun)(mp_obj_t); +typedef bool (*framebuffer_set_auto_brightness_fun)(mp_obj_t, bool); +typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t); +typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t); +typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t); +typedef int (*framebuffer_get_first_pixel_offset_fun)(mp_obj_t); +typedef int (*framebuffer_get_grayscale_fun)(mp_obj_t); +typedef int (*framebuffer_get_height_fun)(mp_obj_t); +typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t); +typedef int (*framebuffer_get_pixels_in_byte_share_row_fun)(mp_obj_t); +typedef int (*framebuffer_get_row_stride_fun)(mp_obj_t); +typedef int (*framebuffer_get_width_fun)(mp_obj_t); +typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t); +typedef void (*framebuffer_deinit_fun)(mp_obj_t); typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo); typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t); -typedef void (*framebuffer_deinit_fun)(mp_obj_t); -typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t); -typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t); -typedef bool (*framebuffer_set_auto_brightness_fun)(mp_obj_t, bool); -typedef bool (*framebuffer_get_auto_brightness_fun)(mp_obj_t); -typedef int (*framebuffer_get_width_fun)(mp_obj_t); -typedef int (*framebuffer_get_height_fun)(mp_obj_t); -typedef int (*framebuffer_get_row_stride_fun)(mp_obj_t); -typedef int (*framebuffer_get_first_pixel_offset_fun)(mp_obj_t); -typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t); -typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t); -typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t); typedef struct _framebuffer_p_t { MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer + + // Mandatory framebuffer_get_bufinfo_fun get_bufinfo; framebuffer_swapbuffers_fun swapbuffers; framebuffer_deinit_fun deinit; framebuffer_get_width_fun get_width; framebuffer_get_height_fun get_height; - framebuffer_get_color_depth_fun get_color_depth; - framebuffer_get_bytes_per_cell_fun get_bytes_per_cell; - framebuffer_get_native_frames_per_second_fun get_native_frames_per_second; + + // Optional getters + framebuffer_get_bytes_per_cell_fun get_bytes_per_cell; // default: 2 + framebuffer_get_color_depth_fun get_color_depth; // default: 16 + framebuffer_get_first_pixel_offset_fun get_first_pixel_offset; // default: 0 + framebuffer_get_grayscale_fun get_grayscale; // default: grayscale if depth < 8 + framebuffer_get_native_frames_per_second_fun get_native_frames_per_second; // default: 60 + framebuffer_get_pixels_in_byte_share_row_fun get_pixels_in_byte_share_row; // default: false + framebuffer_get_reverse_pixels_in_byte_fun get_reverse_pixels_in_byte; // default: false + framebuffer_get_reverse_pixels_in_word_fun get_reverse_pixels_in_word; // default: false + framebuffer_get_row_stride_fun get_row_stride; // default: 0 (no extra row padding) + + // Optional -- default is no brightness control framebuffer_get_brightness_fun get_brightness; framebuffer_set_brightness_fun set_brightness; - framebuffer_get_row_stride_fun get_row_stride; - framebuffer_get_first_pixel_offset_fun get_first_pixel_offset; + + // Optional -- default is no automatic brightness control framebuffer_get_auto_brightness_fun get_auto_brightness; framebuffer_set_auto_brightness_fun set_auto_brightness; } framebuffer_p_t;