Skip to content

Commit ff39fbf

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 ff39fbf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/codal_port/microbit_display.c

+20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <math.h>
2728
#include "py/runtime.h"
2829
#include "py/objstr.h"
2930
#include "py/mphal.h"
@@ -199,6 +200,24 @@ static mp_obj_t microbit_display_get_pixel_func(mp_obj_t self_in, mp_obj_t x_in,
199200
}
200201
MP_DEFINE_CONST_FUN_OBJ_3(microbit_display_get_pixel_obj, microbit_display_get_pixel_func);
201202

203+
static mp_obj_t microbit_display_rotate(mp_obj_t self_in, mp_obj_t angle_in) {
204+
(void)self_in;
205+
mp_float_t angle_degrees = mp_obj_get_float(angle_in);
206+
207+
// Round angle towards nearest multiple of 90 degrees, within 0..359.
208+
angle_degrees = MICROPY_FLOAT_C_FUN(fmod)(angle_degrees, 360);
209+
if (angle_degrees < 0) {
210+
angle_degrees += 360;
211+
}
212+
unsigned int rotation = (unsigned int)((angle_degrees + 45) / 90);
213+
214+
// Set the display rotation.
215+
microbit_hal_display_rotate(rotation);
216+
217+
return mp_const_none;
218+
}
219+
MP_DEFINE_CONST_FUN_OBJ_2(microbit_display_rotate_obj, microbit_display_rotate);
220+
202221
static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = {
203222
{ MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(&microbit_display_get_pixel_obj) },
204223
{ MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&microbit_display_set_pixel_obj) },
@@ -209,6 +228,7 @@ static const mp_rom_map_elem_t microbit_display_locals_dict_table[] = {
209228
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&microbit_display_off_obj) },
210229
{ MP_ROM_QSTR(MP_QSTR_is_on), MP_ROM_PTR(&microbit_display_is_on_obj) },
211230
{ MP_ROM_QSTR(MP_QSTR_read_light_level),MP_ROM_PTR(&microbit_display_read_light_level_obj) },
231+
{ MP_ROM_QSTR(MP_QSTR_rotate),MP_ROM_PTR(&microbit_display_rotate_obj) },
212232
};
213233
static MP_DEFINE_CONST_DICT(microbit_display_locals_dict, microbit_display_locals_dict_table);
214234

0 commit comments

Comments
 (0)