Skip to content

Commit 74e32fb

Browse files
committed
codal_port/microbit_display: Add display.rotate(angle) method.
This takes an angle in degrees and rounds it towards the nearest multiple of 90. Signed-off-by: Damien George <[email protected]>
1 parent 0df07b1 commit 74e32fb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/codal_port/microbit_display.c

+19
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ static mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in,
199199
}
200200
MP_DEFINE_CONST_FUN_OBJ_3(microbit_display_get_pixel_obj, microbit_display_get_pixel_func);
201201

202+
static mp_obj_t microbit_display_rotate(mp_obj_t self_in, mp_obj_t angle_in) {
203+
(void)self_in;
204+
mp_int_t angle_degrees = mp_obj_get_int(angle_in);
205+
206+
// Round angle towards nearest multiple of 90 degrees, within 0..359.
207+
angle_degrees %= 360;
208+
if (angle_degrees < 0) {
209+
angle_degrees += 360;
210+
}
211+
unsigned int rotation = (angle_degrees + 45) / 90;
212+
213+
// Set the display rotation.
214+
microbit_hal_display_rotate(rotation);
215+
216+
return mp_const_none;
217+
}
218+
MP_DEFINE_CONST_FUN_OBJ_2(microbit_display_rotate_obj, microbit_display_rotate);
219+
202220
static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = {
203221
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(&microbit_display_get_pixel_obj) },
204222
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&microbit_display_set_pixel_obj) },
@@ -209,6 +227,7 @@ static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = {
209227
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&microbit_display_off_obj) },
210228
{ MP_ROM_QSTR(MP_QSTR_is_on), MP_ROM_PTR(&microbit_display_is_on_obj) },
211229
{ MP_ROM_QSTR(MP_QSTR_read_light_level),MP_ROM_PTR(&microbit_display_read_light_level_obj) },
230+
{ MP_ROM_QSTR(MP_QSTR_rotate),MP_ROM_PTR(&microbit_display_rotate_obj) },
212231
};
213232
static MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table);
214233

0 commit comments

Comments
 (0)