Skip to content

Commit 6145f08

Browse files
committed
Support adjustable backlight brightness
1 parent 69bc5e1 commit 6145f08

6 files changed

Lines changed: 118 additions & 12 deletions

File tree

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ bool run_code_py(safe_mode_t safe_mode) {
223223

224224
// Wait for connection or character.
225225
if (!serial_connected_at_start) {
226-
serial_write_compressed(translate("\nCode done running. Waiting for USB.\n"));
226+
serial_write_compressed(translate("\nCode done running. Waiting for reload.\n"));
227227
}
228228

229229
bool serial_connected_before_animation = false;

shared-bindings/displayio/Display.c

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@
8484
//| :param int set_column_command: Command used to set the start and end columns to update
8585
//| :param int set_row_command: Command used so set the start and end rows to update
8686
//| :param int write_ram_command: Command used to write pixels values into the update region
87+
//| :param microcontroller.Pin backlight_pin: Pin connected to the display's backlight
8788
//|
8889
STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
89-
enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_color_depth, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command };
90+
enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_color_depth, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_backlight_pin };
9091
static const mp_arg_t allowed_args[] = {
9192
{ MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
9293
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -98,6 +99,7 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
9899
{ MP_QSTR_set_column_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2a} },
99100
{ MP_QSTR_set_row_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2b} },
100101
{ MP_QSTR_write_ram_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2c} },
102+
{ MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
101103
};
102104
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
103105
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -107,6 +109,9 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
107109
mp_buffer_info_t bufinfo;
108110
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ);
109111

112+
mp_obj_t backlight_pin = args[ARG_backlight_pin].u_obj;
113+
assert_pin_free(backlight_pin);
114+
110115
displayio_display_obj_t *self = NULL;
111116
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
112117
if (displays[i].display.base.type == NULL ||
@@ -119,11 +124,10 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
119124
mp_raise_RuntimeError(translate("Too many displays"));
120125
}
121126
self->base.type = &displayio_display_type;
122-
// TODO(tannewt): Support backlight pin.
123127
common_hal_displayio_display_construct(self,
124128
display_bus, args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int,
125129
args[ARG_color_depth].u_int, args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int,
126-
args[ARG_write_ram_command].u_int, bufinfo.buf, bufinfo.len, NULL);
130+
args[ARG_write_ram_command].u_int, bufinfo.buf, bufinfo.len, backlight_pin);
127131

128132
return self;
129133
}
@@ -170,11 +174,72 @@ STATIC mp_obj_t displayio_display_obj_wait_for_frame(mp_obj_t self_in) {
170174
}
171175
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_wait_for_frame_obj, displayio_display_obj_wait_for_frame);
172176

177+
//| .. attribute:: brightness
178+
//|
179+
//| The brightness of the display as a float. 0.0 is off and 1.0 is full brightness. When
180+
//| `auto_brightness` is True this value will change automatically and setting it will have no
181+
//| effect. To control the brightness, auto_brightness must be false.
182+
//|
183+
STATIC mp_obj_t displayio_display_obj_get_brightness(mp_obj_t self_in) {
184+
displayio_display_obj_t *self = MP_OBJ_TO_PTR(self_in);
185+
mp_float_t brightness = common_hal_displayio_display_get_brightness(self);
186+
if (brightness < 0) {
187+
mp_raise_RuntimeError(translate("Brightness not adjustable"));
188+
}
189+
return mp_obj_new_float(brightness);
190+
}
191+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_brightness_obj, displayio_display_obj_get_brightness);
192+
193+
STATIC mp_obj_t displayio_display_obj_set_brightness(mp_obj_t self_in, mp_obj_t brightness) {
194+
displayio_display_obj_t *self = MP_OBJ_TO_PTR(self_in);
195+
bool ok = common_hal_displayio_display_set_brightness(self, mp_obj_get_float(brightness));
196+
if (!ok) {
197+
mp_raise_RuntimeError(translate("Brightness not adjustable"));
198+
}
199+
return mp_const_none;
200+
}
201+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_brightness_obj, displayio_display_obj_set_brightness);
202+
203+
const mp_obj_property_t displayio_display_brightness_obj = {
204+
.base.type = &mp_type_property,
205+
.proxy = {(mp_obj_t)&displayio_display_get_brightness_obj,
206+
(mp_obj_t)&displayio_display_set_brightness_obj,
207+
(mp_obj_t)&mp_const_none_obj},
208+
};
209+
210+
//| .. attribute:: auto_brightness
211+
//|
212+
//| True when the display brightness is auto adjusted.
213+
//|
214+
STATIC mp_obj_t displayio_display_obj_get_auto_brightness(mp_obj_t self_in) {
215+
displayio_display_obj_t *self = MP_OBJ_TO_PTR(self_in);
216+
return mp_obj_new_bool(common_hal_displayio_display_get_auto_brightness(self));
217+
}
218+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_auto_brightness_obj, displayio_display_obj_get_auto_brightness);
219+
220+
STATIC mp_obj_t displayio_display_obj_set_auto_brightness(mp_obj_t self_in, mp_obj_t auto_brightness) {
221+
displayio_display_obj_t *self = MP_OBJ_TO_PTR(self_in);
222+
223+
common_hal_displayio_display_set_auto_brightness(self, mp_obj_is_true(auto_brightness));
224+
225+
return mp_const_none;
226+
}
227+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_auto_brightness_obj, displayio_display_obj_set_auto_brightness);
228+
229+
const mp_obj_property_t displayio_display_auto_brightness_obj = {
230+
.base.type = &mp_type_property,
231+
.proxy = {(mp_obj_t)&displayio_display_get_auto_brightness_obj,
232+
(mp_obj_t)&displayio_display_set_auto_brightness_obj,
233+
(mp_obj_t)&mp_const_none_obj},
234+
};
173235

174236
STATIC const mp_rom_map_elem_t displayio_display_locals_dict_table[] = {
175237
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&displayio_display_show_obj) },
176238
{ MP_ROM_QSTR(MP_QSTR_refresh_soon), MP_ROM_PTR(&displayio_display_refresh_soon_obj) },
177239
{ MP_ROM_QSTR(MP_QSTR_wait_for_frame), MP_ROM_PTR(&displayio_display_wait_for_frame_obj) },
240+
241+
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&displayio_display_brightness_obj) },
242+
{ MP_ROM_QSTR(MP_QSTR_auto_brightness), MP_ROM_PTR(&displayio_display_auto_brightness_obj) },
178243
};
179244
STATIC MP_DEFINE_CONST_DICT(displayio_display_locals_dict, displayio_display_locals_dict_table);
180245

shared-bindings/displayio/Display.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ bool displayio_display_refresh_queued(displayio_display_obj_t* self);
5656
void displayio_display_finish_refresh(displayio_display_obj_t* self);
5757
bool displayio_display_send_pixels(displayio_display_obj_t* self, uint32_t* pixels, uint32_t length);
5858

59+
bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* self);
60+
void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness);
61+
62+
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self);
63+
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness);
64+
5965
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_DISPLAY_H

shared-module/displayio/Display.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
5555
self->current_group = NULL;
5656
self->colstart = colstart;
5757
self->rowstart = rowstart;
58+
self->auto_brightness = false;
5859

5960
if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
6061
self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction;
@@ -110,7 +111,6 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
110111
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
111112
}
112113
}
113-
self->auto_brightness = true;
114114
}
115115

116116
void common_hal_displayio_display_show(displayio_display_obj_t* self, displayio_group_t* root_group) {
@@ -133,6 +133,42 @@ int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* sel
133133
return 0;
134134
}
135135

136+
bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* self) {
137+
return self->auto_brightness;
138+
}
139+
140+
void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness) {
141+
self->auto_brightness = auto_brightness;
142+
}
143+
144+
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) {
145+
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
146+
uint16_t duty_cycle = common_hal_pulseio_pwmout_get_duty_cycle(&self->backlight_pwm);
147+
return duty_cycle / ((mp_float_t) 0xffff);
148+
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
149+
if (common_hal_digitalio_digitalinout_get_value(&self->backlight_inout)) {
150+
return 1.0;
151+
} else {
152+
return 0.0;
153+
}
154+
}
155+
return -1.0;
156+
}
157+
158+
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness) {
159+
self->updating_backlight = true;
160+
bool ok = false;
161+
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
162+
common_hal_pulseio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t) (0xffff * brightness));
163+
ok = true;
164+
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
165+
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
166+
ok = true;
167+
}
168+
self->updating_backlight = false;
169+
return ok;
170+
}
171+
136172
void displayio_display_start_region_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
137173
// TODO(tannewt): Handle displays with single byte bounds.
138174
self->begin_transaction(self->bus);
@@ -181,13 +217,10 @@ void displayio_display_update_backlight(displayio_display_obj_t* self) {
181217
if (ticks_ms - self->last_backlight_refresh < 100) {
182218
return;
183219
}
184-
self->updating_backlight = true;
185-
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
186-
common_hal_pulseio_pwmout_set_duty_cycle(&self->backlight_pwm, 0xffff);
187-
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
188-
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, true);
189-
}
190-
self->updating_backlight = false;
220+
// TODO(tannewt): Fade the backlight based on it's existing value and a target value. The target
221+
// should account for ambient light when possible.
222+
common_hal_displayio_display_set_brightness(self, 1.0);
223+
191224
self->last_backlight_refresh = ticks_ms;
192225
}
193226

shared-module/terminalio/Terminal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, d
3434
self->tilegrid = tilegrid;
3535
self->unicode_characters = unicode_characters;
3636
self->unicode_characters_len = unicode_characters_len;
37+
self->first_row = 0;
3738
}
3839

3940
size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, const byte *data, size_t len, int *errcode) {

shared-module/terminalio/Terminal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct {
4040
displayio_tilegrid_t* tilegrid;
4141
const byte* unicode_characters;
4242
uint16_t unicode_characters_len;
43+
uint16_t first_row;
4344
} terminalio_terminal_obj_t;
4445

4546
#endif /* SHARED_MODULE_TERMINALIO_TERMINAL_H */

0 commit comments

Comments
 (0)