24
24
* THE SOFTWARE.
25
25
*/
26
26
27
+ #include <math.h>
27
28
#include "py/runtime.h"
28
29
#include "py/objstr.h"
29
30
#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,
199
200
}
200
201
MP_DEFINE_CONST_FUN_OBJ_3 (microbit_display_get_pixel_obj , microbit_display_get_pixel_func );
201
202
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
+
202
221
static const mp_rom_map_elem_t microbit_display_locals_dict_table [] = {
203
222
{ MP_ROM_QSTR (MP_QSTR_get_pixel ), MP_ROM_PTR (& microbit_display_get_pixel_obj ) },
204
223
{ 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[] = {
209
228
{ MP_ROM_QSTR (MP_QSTR_off ), MP_ROM_PTR (& microbit_display_off_obj ) },
210
229
{ MP_ROM_QSTR (MP_QSTR_is_on ), MP_ROM_PTR (& microbit_display_is_on_obj ) },
211
230
{ 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 ) },
212
232
};
213
233
static MP_DEFINE_CONST_DICT (microbit_display_locals_dict , microbit_display_locals_dict_table ) ;
214
234
0 commit comments