Skip to content

Commit f548305

Browse files
authored
Merge pull request adafruit#1815 from dhalbert/stop-flicker
Turn off auto_brightness if brightness is set
2 parents 70dd616 + 0113e09 commit f548305

File tree

7 files changed

+46
-21
lines changed

7 files changed

+46
-21
lines changed

ports/atmel-samd/boards/hallowing_m0_express/board.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ void board_init(void) {
9898
display_init_sequence,
9999
sizeof(display_init_sequence),
100100
&pin_PA00,
101+
1.0f, // brightness (ignored)
102+
true, // auto_brightness
101103
false, // single_byte_bounds
102104
false); // data_as_commands
103-
common_hal_displayio_display_set_auto_brightness(display, true);
104105
}
105106

106107
bool board_requests_safe_mode(void) {

ports/atmel-samd/boards/pybadge/board.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ void board_init(void) {
100100
display_init_sequence,
101101
sizeof(display_init_sequence),
102102
&pin_PA00,
103+
1.0f, // brightness (ignored)
104+
true, // auto_brightness
103105
false, // single_byte_bounds
104106
false); // data_as_commands
105-
common_hal_displayio_display_set_auto_brightness(display, true);
106107
}
107108

108109
bool board_requests_safe_mode(void) {

ports/atmel-samd/boards/pyportal/board.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ void board_init(void) {
9090
display_init_sequence,
9191
sizeof(display_init_sequence),
9292
&pin_PB31,
93+
1.0f, // brightness (ignored)
94+
true, // auto_brightness
9395
false, // single_byte_bounds
9496
false); // data_as_commands
95-
96-
common_hal_displayio_display_set_auto_brightness(display, true);
9797
}
9898

9999
bool board_requests_safe_mode(void) {

ports/atmel-samd/boards/ugame10/board.c

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ void board_init(void) {
9898
display_init_sequence,
9999
sizeof(display_init_sequence),
100100
NULL,
101+
1.0f, // brightness
102+
false, // auto_brightness
101103
false, // single_byte_bounds
102104
false); // data as commands
103105
}

shared-bindings/displayio/Display.c

+29-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
//| Most people should not use this class directly. Use a specific display driver instead that will
5151
//| contain the initialization sequence at minimum.
5252
//|
53-
//| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, single_byte_bounds=False, data_as_commands=False)
53+
//| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False)
5454
//|
5555
//| Create a Display object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`).
5656
//|
@@ -91,11 +91,13 @@
9191
//| :param int write_ram_command: Command used to write pixels values into the update region
9292
//| :param int set_vertical_scroll: Command used to set the first row to show
9393
//| :param microcontroller.Pin backlight_pin: Pin connected to the display's backlight
94+
//| :param bool brightness: Initial display brightness. This value is ignored if auto_brightness is True.
95+
//| :param bool auto_brightness: If True, brightness is controlled via an ambient light sensor or other mechanism.
9496
//| :param bool single_byte_bounds: Display column and row commands use single bytes
9597
//| :param bool data_as_commands: Treat all init and boundary data as SPI commands. Certain displays require this.
9698
//|
9799
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) {
98-
enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_single_byte_bounds, ARG_data_as_commands };
100+
enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands };
99101
static const mp_arg_t allowed_args[] = {
100102
{ MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
101103
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -110,6 +112,8 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
110112
{ MP_QSTR_write_ram_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2c} },
111113
{ MP_QSTR_set_vertical_scroll, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x0} },
112114
{ MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
115+
{ MP_QSTR_brightness, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },
116+
{ MP_QSTR_auto_brightness, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
113117
{ MP_QSTR_single_byte_bounds, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
114118
{ MP_QSTR_data_as_commands, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
115119
};
@@ -128,6 +132,9 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
128132
backlight_pin = MP_OBJ_TO_PTR(backlight_pin_obj);
129133
assert_pin_free(backlight_pin);
130134
}
135+
136+
mp_float_t brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
137+
131138
mp_int_t rotation = args[ARG_rotation].u_int;
132139
if (rotation % 90 != 0) {
133140
mp_raise_ValueError(translate("Display rotation must be in 90 degree increments"));
@@ -145,14 +152,19 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
145152
mp_raise_RuntimeError(translate("Too many displays"));
146153
}
147154
self->base.type = &displayio_display_type;
148-
common_hal_displayio_display_construct(self,
149-
display_bus, args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
150-
args[ARG_color_depth].u_int, args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int,
151-
args[ARG_write_ram_command].u_int,
152-
args[ARG_set_vertical_scroll].u_int,
153-
bufinfo.buf, bufinfo.len, MP_OBJ_TO_PTR(backlight_pin),
154-
args[ARG_single_byte_bounds].u_bool,
155-
args[ARG_data_as_commands].u_bool);
155+
common_hal_displayio_display_construct(
156+
self,
157+
display_bus, args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
158+
args[ARG_color_depth].u_int, args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int,
159+
args[ARG_write_ram_command].u_int,
160+
args[ARG_set_vertical_scroll].u_int,
161+
bufinfo.buf, bufinfo.len,
162+
MP_OBJ_TO_PTR(backlight_pin),
163+
brightness,
164+
args[ARG_auto_brightness].u_bool,
165+
args[ARG_single_byte_bounds].u_bool,
166+
args[ARG_data_as_commands].u_bool
167+
);
156168

157169
return self;
158170
}
@@ -209,8 +221,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_wait_for_frame_obj, displayio_displa
209221
//| .. attribute:: brightness
210222
//|
211223
//| The brightness of the display as a float. 0.0 is off and 1.0 is full brightness. When
212-
//| `auto_brightness` is True this value will change automatically and setting it will have no
213-
//| effect. To control the brightness, auto_brightness must be false.
224+
//| `auto_brightness` is True, the value of `brightness` will change automatically.
225+
//| If `brightness` is set, `auto_brightness` will be disabled and will be set to False.
214226
//|
215227
STATIC mp_obj_t displayio_display_obj_get_brightness(mp_obj_t self_in) {
216228
displayio_display_obj_t *self = native_display(self_in);
@@ -224,6 +236,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_brightness_obj, displayio_displa
224236

225237
STATIC mp_obj_t displayio_display_obj_set_brightness(mp_obj_t self_in, mp_obj_t brightness) {
226238
displayio_display_obj_t *self = native_display(self_in);
239+
common_hal_displayio_display_set_auto_brightness(self, false);
227240
bool ok = common_hal_displayio_display_set_brightness(self, mp_obj_get_float(brightness));
228241
if (!ok) {
229242
mp_raise_RuntimeError(translate("Brightness not adjustable"));
@@ -241,7 +254,10 @@ const mp_obj_property_t displayio_display_brightness_obj = {
241254

242255
//| .. attribute:: auto_brightness
243256
//|
244-
//| True when the display brightness is auto adjusted.
257+
//| True when the display brightness is adjusted automatically, based on an ambient
258+
//| light sensor or other method. Note that some displays may have this set to True by default,
259+
//| but not actually implement automatic brightness adjustment. `auto_brightness` is set to False
260+
//| if `brightness` is set manually.
245261
//|
246262
STATIC mp_obj_t displayio_display_obj_get_auto_brightness(mp_obj_t self_in) {
247263
displayio_display_obj_t *self = native_display(self_in);

shared-bindings/displayio/Display.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
4040
mp_obj_t bus, uint16_t width, uint16_t height,
4141
int16_t colstart, int16_t rowstart, uint16_t rotation, uint16_t color_depth,
4242
uint8_t set_column_command, uint8_t set_row_command, uint8_t write_ram_command, uint8_t set_vertical_scroll,
43-
uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* backlight_pin, bool single_byte_bounds,
44-
bool data_as_commands);
43+
uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* backlight_pin,
44+
mp_float_t brightness, bool auto_brightness,
45+
bool single_byte_bounds, bool data_as_commands);
4546

4647
int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* self);
4748

shared-module/displayio/Display.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
4444
mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, uint16_t rotation,
4545
uint16_t color_depth, uint8_t set_column_command, uint8_t set_row_command,
4646
uint8_t write_ram_command, uint8_t set_vertical_scroll, uint8_t* init_sequence, uint16_t init_sequence_len,
47-
const mcu_pin_obj_t* backlight_pin, bool single_byte_bounds, bool data_as_commands) {
47+
const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness,
48+
bool single_byte_bounds, bool data_as_commands) {
4849
self->color_depth = color_depth;
4950
self->set_column_command = set_column_command;
5051
self->set_row_command = set_row_command;
@@ -53,7 +54,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
5354
self->current_group = NULL;
5455
self->colstart = colstart;
5556
self->rowstart = rowstart;
56-
self->auto_brightness = false;
57+
self->auto_brightness = auto_brightness;
5758
self->data_as_commands = data_as_commands;
5859
self->single_byte_bounds = single_byte_bounds;
5960

@@ -142,6 +143,9 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
142143
} else {
143144
self->backlight_pwm.base.type = &pulseio_pwmout_type;
144145
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
146+
if (!self->auto_brightness) {
147+
common_hal_displayio_display_set_brightness(self, brightness);
148+
}
145149
}
146150
}
147151
}

0 commit comments

Comments
 (0)