Skip to content

Commit f630b78

Browse files
committed
framebufferio: move backlight down to the underlying framebuffer
1 parent 5ca02e8 commit f630b78

File tree

5 files changed

+28
-75
lines changed

5 files changed

+28
-75
lines changed

shared-bindings/_protomatter/Protomatter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ STATIC void protomatter_protomatter_deinit_void(mp_obj_t self_in) {
317317
protomatter_protomatter_deinit(self_in);
318318
}
319319

320-
STATIC void protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_float_t value) {
320+
STATIC bool protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_float_t value) {
321321
common_hal_protomatter_protomatter_set_paused(self_in, value <= 0);
322+
return true;
322323
}
323324

324325
STATIC const framebuffer_p_t protomatter_protomatter_proto = {

shared-bindings/framebufferio/FramebufferDisplay.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,14 @@
6767
//| :param int native_frames_per_second: Number of display refreshes per second
6868
//|
6969
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) {
70-
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 };
70+
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 };
7171
static const mp_arg_t allowed_args[] = {
7272
{ MP_QSTR_framebuffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
7373
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
7474
{ MP_QSTR_height, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED, },
7575
{ MP_QSTR_rotation, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
7676
{ MP_QSTR_color_depth, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} },
7777
{ MP_QSTR_bytes_per_cell, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
78-
{ MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
79-
{ MP_QSTR_brightness, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },
80-
{ MP_QSTR_auto_brightness, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
8178
{ MP_QSTR_auto_refresh, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
8279
{ MP_QSTR_native_frames_per_second, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 60} },
8380
};
@@ -87,10 +84,6 @@ STATIC mp_obj_t framebufferio_framebufferdisplay_make_new(const mp_obj_type_t *t
8784

8885
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
8986

90-
const mcu_pin_obj_t* backlight_pin = validate_obj_is_free_pin_or_none(args[ARG_backlight_pin].u_obj);
91-
92-
mp_float_t brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
93-
9487
mp_int_t rotation = args[ARG_rotation].u_int;
9588
if (rotation % 90 != 0) {
9689
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
10699
rotation,
107100
args[ARG_color_depth].u_int,
108101
args[ARG_bytes_per_cell].u_int,
109-
MP_OBJ_TO_PTR(backlight_pin),
110-
brightness,
111-
args[ARG_auto_brightness].u_bool,
112102
args[ARG_auto_refresh].u_bool,
113103
args[ARG_native_frames_per_second].u_int
114104
);
@@ -260,7 +250,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(framebufferio_framebufferdisplay_get_auto_brightness_o
260250
STATIC mp_obj_t framebufferio_framebufferdisplay_obj_set_auto_brightness(mp_obj_t self_in, mp_obj_t auto_brightness) {
261251
framebufferio_framebufferdisplay_obj_t *self = native_display(self_in);
262252

263-
common_hal_framebufferio_framebufferdisplay_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
253+
bool ok = common_hal_framebufferio_framebufferdisplay_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
254+
if (!ok) {
255+
mp_raise_RuntimeError(translate("Brightness not adjustable"));
256+
}
264257

265258
return mp_const_none;
266259
}

shared-bindings/framebufferio/FramebufferDisplay.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
4242
mp_obj_t framebuffer, uint16_t width, uint16_t height,
4343
uint16_t rotation, uint16_t color_depth,
4444
uint8_t bytes_per_cell,
45-
const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness,
4645
bool auto_refresh, uint16_t native_frames_per_second);
4746

4847
bool common_hal_framebufferio_framebufferdisplay_show(framebufferio_framebufferdisplay_obj_t* self,
@@ -59,7 +58,7 @@ uint16_t common_hal_framebufferio_framebufferdisplay_get_rotation(framebufferio_
5958
void common_hal_framebufferio_framebufferdisplay_set_rotation(framebufferio_framebufferdisplay_obj_t* self, int rotation);
6059

6160
bool common_hal_framebufferio_framebufferdisplay_get_auto_brightness(framebufferio_framebufferdisplay_obj_t* self);
62-
void common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness);
61+
bool common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness);
6362

6463
bool common_hal_framebufferio_framebufferdisplay_get_dither(framebufferio_framebufferdisplay_obj_t* self);
6564
void common_hal_framebufferio_framebufferdisplay_set_dither(framebufferio_framebufferdisplay_obj_t* self, bool dither);

shared-module/framebufferio/FramebufferDisplay.c

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
4545
mp_obj_t framebuffer, uint16_t width, uint16_t height,
4646
uint16_t rotation, uint16_t color_depth,
4747
uint8_t bytes_per_cell,
48-
const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness,
4948
bool auto_refresh, uint16_t native_frames_per_second) {
5049
// Turn off auto-refresh as we init.
5150
self->auto_refresh = false;
@@ -58,33 +57,13 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu
5857
displayio_display_core_construct(&self->core, NULL, width, height, ram_width, ram_height, 0, 0, rotation,
5958
color_depth, false, false, bytes_per_cell, false, false);
6059

61-
self->auto_brightness = auto_brightness;
6260
self->first_manual_refresh = !auto_refresh;
6361

6462
self->native_frames_per_second = native_frames_per_second;
6563
self->native_ms_per_frame = 1000 / native_frames_per_second;
6664

6765
supervisor_start_terminal(width, height);
6866

69-
// Always set the backlight type in case we're reusing memory.
70-
self->backlight_inout.base.type = &mp_type_NoneType;
71-
if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) {
72-
pwmout_result_t result = common_hal_pulseio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 50000, false);
73-
if (result != PWMOUT_OK) {
74-
self->backlight_inout.base.type = &digitalio_digitalinout_type;
75-
common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin);
76-
common_hal_never_reset_pin(backlight_pin);
77-
} else {
78-
self->backlight_pwm.base.type = &pulseio_pwmout_type;
79-
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
80-
}
81-
}
82-
if (!self->auto_brightness && (self->framebuffer_protocol->set_brightness != NULL || self->backlight_inout.base.type != &mp_type_NoneType)) {
83-
common_hal_framebufferio_framebufferdisplay_set_brightness(self, brightness);
84-
} else {
85-
self->current_brightness = -1.0;
86-
}
87-
8867
// Set the group after initialization otherwise we may send pixels while we delay in
8968
// initialization.
9069
common_hal_framebufferio_framebufferdisplay_show(self, &circuitpython_splash);
@@ -104,33 +83,31 @@ uint16_t common_hal_framebufferio_framebufferdisplay_get_height(framebufferio_fr
10483
}
10584

10685
bool common_hal_framebufferio_framebufferdisplay_get_auto_brightness(framebufferio_framebufferdisplay_obj_t* self) {
107-
return self->auto_brightness;
86+
if (self->framebuffer_protocol->get_auto_brightness) {
87+
return self->framebuffer_protocol->get_auto_brightness(self->framebuffer);
88+
}
89+
return true;
10890
}
10991

110-
void common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness) {
111-
self->auto_brightness = auto_brightness;
92+
bool common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness) {
93+
if (self->framebuffer_protocol->set_auto_brightness) {
94+
return self->framebuffer_protocol->set_auto_brightness(self->framebuffer, auto_brightness);
95+
}
96+
return false;
11297
}
11398

11499
mp_float_t common_hal_framebufferio_framebufferdisplay_get_brightness(framebufferio_framebufferdisplay_obj_t* self) {
115-
return self->current_brightness;
100+
if (self->framebuffer_protocol->set_brightness) {
101+
return self->framebuffer_protocol->get_brightness(self->framebuffer);
102+
}
103+
return -1;
116104
}
117105

118106
bool common_hal_framebufferio_framebufferdisplay_set_brightness(framebufferio_framebufferdisplay_obj_t* self, mp_float_t brightness) {
119-
self->updating_backlight = true;
120107
bool ok = false;
121108
if (self->framebuffer_protocol->set_brightness) {
122109
self->framebuffer_protocol->set_brightness(self->framebuffer, brightness);
123110
ok = true;
124-
} else if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
125-
common_hal_pulseio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t) (0xffff * brightness));
126-
ok = true;
127-
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
128-
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
129-
ok = true;
130-
}
131-
self->updating_backlight = false;
132-
if (ok) {
133-
self->current_brightness = brightness;
134111
}
135112
return ok;
136113
}
@@ -295,17 +272,8 @@ void common_hal_framebufferio_framebufferdisplay_set_auto_refresh(framebufferio_
295272
}
296273

297274
STATIC void _update_backlight(framebufferio_framebufferdisplay_obj_t* self) {
298-
if (!self->auto_brightness || self->updating_backlight) {
299-
return;
300-
}
301-
if (supervisor_ticks_ms64() - self->last_backlight_refresh < 100) {
302-
return;
303-
}
304275
// TODO(tannewt): Fade the backlight based on it's existing value and a target value. The target
305276
// should account for ambient light when possible.
306-
common_hal_framebufferio_framebufferdisplay_set_brightness(self, 1.0);
307-
308-
self->last_backlight_refresh = supervisor_ticks_ms64();
309277
}
310278

311279
void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self) {
@@ -318,18 +286,11 @@ void framebufferio_framebufferdisplay_background(framebufferio_framebufferdispla
318286

319287
void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
320288
release_display_core(&self->core);
321-
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
322-
common_hal_pulseio_pwmout_reset_ok(&self->backlight_pwm);
323-
common_hal_pulseio_pwmout_deinit(&self->backlight_pwm);
324-
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
325-
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
326-
}
327289
self->framebuffer_protocol->deinit(self->framebuffer);
328290
}
329291

330292
void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
331293
self->auto_refresh = true;
332-
self->auto_brightness = true;
333294
common_hal_framebufferio_framebufferdisplay_show(self, NULL);
334295
}
335296

shared-module/framebufferio/FramebufferDisplay.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,15 @@
4040
typedef struct {
4141
mp_obj_base_t base;
4242
displayio_display_core_t core;
43-
union {
44-
digitalio_digitalinout_obj_t backlight_inout;
45-
pulseio_pwmout_obj_t backlight_pwm;
46-
};
4743
mp_obj_t framebuffer;
4844
const struct _framebuffer_p_t *framebuffer_protocol;
4945
mp_buffer_info_t bufinfo;
5046
uint64_t last_backlight_refresh;
5147
uint64_t last_refresh_call;
52-
mp_float_t current_brightness;
5348
uint16_t native_frames_per_second;
5449
uint16_t native_ms_per_frame;
5550
bool auto_refresh;
5651
bool first_manual_refresh;
57-
bool auto_brightness;
58-
bool updating_backlight;
5952
} framebufferio_framebufferdisplay_obj_t;
6053

6154
void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self);
@@ -69,14 +62,20 @@ mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebuffer
6962
typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo);
7063
typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t);
7164
typedef void (*framebuffer_deinit_fun)(mp_obj_t);
72-
typedef void (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t);
65+
typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t);
66+
typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t);
67+
typedef bool (*framebuffer_set_auto_brightness_fun)(mp_obj_t, bool);
68+
typedef bool (*framebuffer_get_auto_brightness_fun)(mp_obj_t);
7369

7470
typedef struct _framebuffer_p_t {
7571
MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer
7672
framebuffer_get_bufinfo_fun get_bufinfo;
7773
framebuffer_swapbuffers_fun swapbuffers;
7874
framebuffer_deinit_fun deinit;
75+
framebuffer_get_brightness_fun get_brightness;
7976
framebuffer_set_brightness_fun set_brightness;
77+
framebuffer_get_auto_brightness_fun get_auto_brightness;
78+
framebuffer_set_auto_brightness_fun set_auto_brightness;
8079
} framebuffer_p_t;
8180

8281
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FRAMEBUFFERDISPLAY_H

0 commit comments

Comments
 (0)