1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "microbit/memory.h"
#include "microbit/microbitdal.h"
extern "C" {
#include "py/runtime.h"
#include "lib/ticker.h"
#include "microbit/modmicrobit.h"
#define COMPASS_CALIBRATION_MAGIC (0xc011ba55)
typedef struct _microbit_compass_obj_t {
mp_obj_base_t base;
} microbit_compass_obj_t;
void microbit_compass_init(void) {
// load any peristent calibration data if it exists
uint32_t *persist = (uint32_t*)microbit_compass_calibration_page();
if (persist[0] == COMPASS_CALIBRATION_MAGIC) {
CompassCalibration calib;
calib.centre = {(int)persist[1], (int)persist[2], (int)persist[3]};
calib.scale = {(int)persist[4], (int)persist[5], (int)persist[6]};
calib.radius = (int)persist[7];
ubit_compass->setCalibration(calib);
}
}
mp_obj_t microbit_compass_is_calibrated(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_bool(ubit_compass->isCalibrated());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_is_calibrated_obj, microbit_compass_is_calibrated);
mp_obj_t microbit_compass_calibrate(mp_obj_t self_in) {
// Calibration requires to pass control over to the DAL so it
// can use the display to collect samples for the calibration.
// It will do the calibration and then return here.
(void)self_in;
ticker_stop();
//uBit.systemTicker.attach_us(&uBit, &MicroBit::systemTick, MICROBIT_DEFAULT_TICK_PERIOD * 1000); TODO what to replace with?
ubit_display.enable();
ubit_compass->calibrate();
ubit_display.disable();
//uBit.systemTicker.detach(); TODO what to replace with?
ticker_start();
microbit_display_init();
// store the calibration data
uint32_t *persist = (uint32_t*)microbit_compass_calibration_page();
CompassCalibration calib = ubit_compass->getCalibration();
uint32_t data[8] = {
COMPASS_CALIBRATION_MAGIC,
(uint32_t)calib.centre.x, (uint32_t)calib.centre.y, (uint32_t)calib.centre.z,
(uint32_t)calib.scale.x, (uint32_t)calib.scale.y, (uint32_t)calib.scale.z,
(uint32_t)calib.radius,
};
persistent_erase_page(persist);
persistent_write_unchecked(persist, data, sizeof(data));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_calibrate_obj, microbit_compass_calibrate);
mp_obj_t microbit_compass_clear_calibration(mp_obj_t self_in) {
(void)self_in;
ubit_compass->clearCalibration();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_clear_calibration_obj, microbit_compass_clear_calibration);
volatile bool compass_up_to_date = false;
volatile bool compass_updating = false;
static void update(microbit_compass_obj_t *self) {
/* The only time it is possible for compass_updating to be true here
* is if this is called in an interrupt when it is already updating in
* the main execution thread. This is extremely unlikely, so we just
* accept that a slightly out-of-date result will be returned
*/
(void)self;
if (!compass_up_to_date && !compass_updating) {
compass_updating = true;
ubit_compass->idleTick();
compass_updating = false;
compass_up_to_date = true;
}
}
mp_obj_t microbit_compass_heading(mp_obj_t self_in) {
microbit_compass_obj_t *self = (microbit_compass_obj_t*)self_in;
// Upon calling heading(), the DAL will automatically calibrate the compass
// if it's not already calibrated. Since we need to first enable the display
// for calibration to work, we must check for non-calibration here and call
// our own calibration function.
if (!ubit_compass->isCalibrated()) {
microbit_compass_calibrate(self_in);
}
update(self);
return mp_obj_new_int(ubit_compass->heading());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_heading_obj, microbit_compass_heading);
mp_obj_t microbit_compass_get_x(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_int(ubit_compass->getX());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_x_obj, microbit_compass_get_x);
mp_obj_t microbit_compass_get_y(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_int(ubit_compass->getY());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_y_obj, microbit_compass_get_y);
mp_obj_t microbit_compass_get_z(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_int(ubit_compass->getZ());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_z_obj, microbit_compass_get_z);
mp_obj_t microbit_compass_get_field_strength(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_int(ubit_compass->getFieldStrength());
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_compass_get_field_strength_obj, microbit_compass_get_field_strength);
STATIC const mp_map_elem_t microbit_compass_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_heading), (mp_obj_t)µbit_compass_heading_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_calibrated), (mp_obj_t)µbit_compass_is_calibrated_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_calibrate), (mp_obj_t)µbit_compass_calibrate_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_clear_calibration), (mp_obj_t)µbit_compass_clear_calibration_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_x), (mp_obj_t)µbit_compass_get_x_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_y), (mp_obj_t)µbit_compass_get_y_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_z), (mp_obj_t)µbit_compass_get_z_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_field_strength), (mp_obj_t)µbit_compass_get_field_strength_obj },
};
STATIC MP_DEFINE_CONST_DICT(microbit_compass_locals_dict, microbit_compass_locals_dict_table);
STATIC const mp_obj_type_t microbit_compass_type = {
{ &mp_type_type },
.name = MP_QSTR_MicroBitCompass,
.print = NULL,
.make_new = NULL,
.call = NULL,
.unary_op = NULL,
.binary_op = NULL,
.attr = NULL,
.subscr = NULL,
.getiter = NULL,
.iternext = NULL,
.buffer_p = {NULL},
.protocol = NULL,
.parent = NULL,
.locals_dict = (mp_obj_dict_t*)µbit_compass_locals_dict,
};
const microbit_compass_obj_t microbit_compass_obj = {
{µbit_compass_type},
};
}
|