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//|
8889STATIC 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}
171175MP_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
174236STATIC 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};
179244STATIC MP_DEFINE_CONST_DICT (displayio_display_locals_dict , displayio_display_locals_dict_table );
180245
0 commit comments