framebufferio: move backlight down to the underlying framebuffer
This commit is contained in:
parent
129c6369cf
commit
ba20bc8b43
@ -317,8 +317,9 @@ STATIC void protomatter_protomatter_deinit_void(mp_obj_t self_in) {
|
||||
protomatter_protomatter_deinit(self_in);
|
||||
}
|
||||
|
||||
STATIC void protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_float_t value) {
|
||||
STATIC bool protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_float_t value) {
|
||||
common_hal_protomatter_protomatter_set_paused(self_in, value <= 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
STATIC const framebuffer_p_t protomatter_protomatter_proto = {
|
||||
|
@ -67,7 +67,7 @@
|
||||
//| :param int native_frames_per_second: Number of display refreshes per second
|
||||
//|
|
||||
STATIC mp_obj_t framebufferio_framebufferdisplay_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_framebuffer, ARG_width, ARG_height, ARG_rotation, ARG_color_depth, ARG_bytes_per_cell, ARG_backlight_pin, ARG_brightness, ARG_auto_brightness, ARG_auto_refresh, ARG_native_frames_per_second, NUM_ARGS };
|
||||
enum { ARG_framebuffer, ARG_width, ARG_height, ARG_rotation, ARG_color_depth, ARG_bytes_per_cell, ARG_auto_refresh, ARG_native_frames_per_second, NUM_ARGS };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_framebuffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
|
||||
@ -75,9 +75,6 @@ STATIC mp_obj_t framebufferio_framebufferdisplay_make_new(const mp_obj_type_t *t
|
||||
{ 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_bytes_per_cell, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
|
||||
{ MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
|
||||
{ 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_auto_refresh, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
|
||||
{ MP_QSTR_native_frames_per_second, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 60} },
|
||||
};
|
||||
@ -87,10 +84,6 @@ STATIC mp_obj_t framebufferio_framebufferdisplay_make_new(const mp_obj_type_t *t
|
||||
|
||||
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
|
||||
|
||||
const mcu_pin_obj_t* backlight_pin = validate_obj_is_free_pin_or_none(args[ARG_backlight_pin].u_obj);
|
||||
|
||||
mp_float_t brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
|
||||
|
||||
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"));
|
||||
@ -106,9 +99,6 @@ STATIC mp_obj_t framebufferio_framebufferdisplay_make_new(const mp_obj_type_t *t
|
||||
rotation,
|
||||
args[ARG_color_depth].u_int,
|
||||
args[ARG_bytes_per_cell].u_int,
|
||||
MP_OBJ_TO_PTR(backlight_pin),
|
||||
brightness,
|
||||
args[ARG_auto_brightness].u_bool,
|
||||
args[ARG_auto_refresh].u_bool,
|
||||
args[ARG_native_frames_per_second].u_int
|
||||
);
|
||||
@ -260,7 +250,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_auto_brightness_o
|
||||
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_auto_brightness(mp_obj_t self_in, mp_obj_t auto_brightness) {
|
||||
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
|
||||
|
||||
common_hal_framebufferio_framebufferdisplay_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
|
||||
bool ok = common_hal_framebufferio_framebufferdisplay_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
|
||||
if (!ok) {
|
||||
mp_raise_RuntimeError(translate("Brightness not adjustable"));
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
@ -42,7 +42,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
|
||||
mp_obj_t framebuffer, uint16_t width, uint16_t height,
|
||||
uint16_t rotation, uint16_t color_depth,
|
||||
uint8_t bytes_per_cell,
|
||||
const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness,
|
||||
bool auto_refresh, uint16_t native_frames_per_second);
|
||||
|
||||
bool common_hal_framebufferio_framebufferdisplay_show(framebufferio_framebufferdisplay_obj_t* self,
|
||||
@ -59,7 +58,7 @@ uint16_t common_hal_framebufferio_framebufferdisplay_get_rotation(framebufferio_
|
||||
void common_hal_framebufferio_framebufferdisplay_set_rotation(framebufferio_framebufferdisplay_obj_t* self, int rotation);
|
||||
|
||||
bool common_hal_framebufferio_framebufferdisplay_get_auto_brightness(framebufferio_framebufferdisplay_obj_t* self);
|
||||
void common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness);
|
||||
bool common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness);
|
||||
|
||||
bool common_hal_framebufferio_framebufferdisplay_get_dither(framebufferio_framebufferdisplay_obj_t* self);
|
||||
void common_hal_framebufferio_framebufferdisplay_set_dither(framebufferio_framebufferdisplay_obj_t* self, bool dither);
|
||||
|
@ -45,7 +45,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
|
||||
mp_obj_t framebuffer, uint16_t width, uint16_t height,
|
||||
uint16_t rotation, uint16_t color_depth,
|
||||
uint8_t bytes_per_cell,
|
||||
const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness,
|
||||
bool auto_refresh, uint16_t native_frames_per_second) {
|
||||
// Turn off auto-refresh as we init.
|
||||
self->auto_refresh = false;
|
||||
@ -58,7 +57,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
|
||||
displayio_display_core_construct(&self->core, NULL, width, height, ram_width, ram_height, 0, 0, rotation,
|
||||
color_depth, false, false, bytes_per_cell, false, false);
|
||||
|
||||
self->auto_brightness = auto_brightness;
|
||||
self->first_manual_refresh = !auto_refresh;
|
||||
|
||||
self->native_frames_per_second = native_frames_per_second;
|
||||
@ -66,25 +64,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
|
||||
|
||||
supervisor_start_terminal(width, height);
|
||||
|
||||
// Always set the backlight type in case we're reusing memory.
|
||||
self->backlight_inout.base.type = &mp_type_NoneType;
|
||||
if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) {
|
||||
pwmout_result_t result = common_hal_pulseio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 50000, false);
|
||||
if (result != PWMOUT_OK) {
|
||||
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);
|
||||
} else {
|
||||
self->backlight_pwm.base.type = &pulseio_pwmout_type;
|
||||
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
|
||||
}
|
||||
}
|
||||
if (!self->auto_brightness && (self->framebuffer_protocol->set_brightness != NULL || self->backlight_inout.base.type != &mp_type_NoneType)) {
|
||||
common_hal_framebufferio_framebufferdisplay_set_brightness(self, brightness);
|
||||
} else {
|
||||
self->current_brightness = -1.0;
|
||||
}
|
||||
|
||||
// Set the group after initialization otherwise we may send pixels while we delay in
|
||||
// initialization.
|
||||
common_hal_framebufferio_framebufferdisplay_show(self, &circuitpython_splash);
|
||||
@ -104,33 +83,31 @@ uint16_t common_hal_framebufferio_framebufferdisplay_get_height(framebufferio_fr
|
||||
}
|
||||
|
||||
bool common_hal_framebufferio_framebufferdisplay_get_auto_brightness(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
return self->auto_brightness;
|
||||
if (self->framebuffer_protocol->get_auto_brightness) {
|
||||
return self->framebuffer_protocol->get_auto_brightness(self->framebuffer);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness) {
|
||||
self->auto_brightness = auto_brightness;
|
||||
bool common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness) {
|
||||
if (self->framebuffer_protocol->set_auto_brightness) {
|
||||
return self->framebuffer_protocol->set_auto_brightness(self->framebuffer, auto_brightness);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
mp_float_t common_hal_framebufferio_framebufferdisplay_get_brightness(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
return self->current_brightness;
|
||||
if (self->framebuffer_protocol->set_brightness) {
|
||||
return self->framebuffer_protocol->get_brightness(self->framebuffer);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool common_hal_framebufferio_framebufferdisplay_set_brightness(framebufferio_framebufferdisplay_obj_t* self, mp_float_t brightness) {
|
||||
self->updating_backlight = true;
|
||||
bool ok = false;
|
||||
if (self->framebuffer_protocol->set_brightness) {
|
||||
self->framebuffer_protocol->set_brightness(self->framebuffer, brightness);
|
||||
ok = true;
|
||||
} else if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
|
||||
common_hal_pulseio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t) (0xffff * brightness));
|
||||
ok = true;
|
||||
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
|
||||
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
|
||||
ok = true;
|
||||
}
|
||||
self->updating_backlight = false;
|
||||
if (ok) {
|
||||
self->current_brightness = brightness;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
@ -295,17 +272,8 @@ void common_hal_framebufferio_framebufferdisplay_set_auto_refresh(framebufferio_
|
||||
}
|
||||
|
||||
STATIC void _update_backlight(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
if (!self->auto_brightness || self->updating_backlight) {
|
||||
return;
|
||||
}
|
||||
if (supervisor_ticks_ms64() - self->last_backlight_refresh < 100) {
|
||||
return;
|
||||
}
|
||||
// TODO(tannewt): Fade the backlight based on it's existing value and a target value. The target
|
||||
// should account for ambient light when possible.
|
||||
common_hal_framebufferio_framebufferdisplay_set_brightness(self, 1.0);
|
||||
|
||||
self->last_backlight_refresh = supervisor_ticks_ms64();
|
||||
}
|
||||
|
||||
void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
@ -318,18 +286,11 @@ void framebufferio_framebufferdisplay_background(framebufferio_framebufferdispla
|
||||
|
||||
void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
release_display_core(&self->core);
|
||||
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);
|
||||
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
|
||||
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
|
||||
}
|
||||
self->framebuffer_protocol->deinit(self->framebuffer);
|
||||
}
|
||||
|
||||
void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
|
||||
self->auto_refresh = true;
|
||||
self->auto_brightness = true;
|
||||
common_hal_framebufferio_framebufferdisplay_show(self, NULL);
|
||||
}
|
||||
|
||||
|
@ -40,22 +40,15 @@
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
displayio_display_core_t core;
|
||||
union {
|
||||
digitalio_digitalinout_obj_t backlight_inout;
|
||||
pulseio_pwmout_obj_t backlight_pwm;
|
||||
};
|
||||
mp_obj_t framebuffer;
|
||||
const struct _framebuffer_p_t *framebuffer_protocol;
|
||||
mp_buffer_info_t bufinfo;
|
||||
uint64_t last_backlight_refresh;
|
||||
uint64_t last_refresh_call;
|
||||
mp_float_t current_brightness;
|
||||
uint16_t native_frames_per_second;
|
||||
uint16_t native_ms_per_frame;
|
||||
bool auto_refresh;
|
||||
bool first_manual_refresh;
|
||||
bool auto_brightness;
|
||||
bool updating_backlight;
|
||||
} framebufferio_framebufferdisplay_obj_t;
|
||||
|
||||
void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self);
|
||||
@ -69,14 +62,20 @@ mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebuffer
|
||||
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 void (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_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 struct _framebuffer_p_t {
|
||||
MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer
|
||||
framebuffer_get_bufinfo_fun get_bufinfo;
|
||||
framebuffer_swapbuffers_fun swapbuffers;
|
||||
framebuffer_deinit_fun deinit;
|
||||
framebuffer_get_brightness_fun get_brightness;
|
||||
framebuffer_set_brightness_fun set_brightness;
|
||||
framebuffer_get_auto_brightness_fun get_auto_brightness;
|
||||
framebuffer_set_auto_brightness_fun set_auto_brightness;
|
||||
} framebuffer_p_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FRAMEBUFFERDISPLAY_H
|
||||
|
Loading…
Reference in New Issue
Block a user