Compare commits

..

3 Commits

7 changed files with 236 additions and 68 deletions

View File

@ -2,10 +2,6 @@
#include "i2c.h"
#include "QMC5883L.h"
#ifndef PI
#define PI 3.14159265358979f
#endif
typedef struct
{
int16_t x_min;
@ -14,6 +10,7 @@ typedef struct
int16_t y_max;
int16_t z_min;
int16_t z_max;
float temperature_offset;
} _QMC5883L_calibration_data;
static _QMC5883L_calibration_data calibration_data = {0};
@ -42,7 +39,7 @@ float QMC5883L_get_temperature(void)
raw_temp |= data;
return (float) raw_temp / 100.0;
return (float) raw_temp / 100.0 + calibration_data.temperature_offset;
}
bool QMC5883L_is_data_available(void)
@ -54,7 +51,7 @@ bool QMC5883L_is_data_available(void)
return data & QMC5883L_DRDY_BIT ? true : false;
}
void QMC5883L_set_calibration_data(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max, int16_t z_min, int16_t z_max)
void QMC5883L_set_calibration_data(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max, int16_t z_min, int16_t z_max, float temperature_offset)
{
calibration_data.x_min = x_min;
calibration_data.x_max = x_max;
@ -62,6 +59,7 @@ void QMC5883L_set_calibration_data(int16_t x_min, int16_t x_max, int16_t y_min,
calibration_data.y_max = y_max;
calibration_data.z_min = z_min;
calibration_data.z_max = z_max;
calibration_data.temperature_offset = temperature_offset;
}
QMC5883L_MData_t QMC5883L_get_MFields_raw(void)
@ -111,9 +109,9 @@ QMC5883L_MData_calibrated_t QMC5883L_get_MFields_calibrated(void)
return Mdata;
}
int16_t QMC5883L_get_azimuth(const QMC5883L_MData_calibrated_t data)
uint16_t QMC5883L_get_azimuth(const QMC5883L_MData_calibrated_t data)
{
int16_t angle = atan2(data.MFieldY, data.MFieldX) * 180.0 / PI;
int16_t angle = atan2(data.MFieldY, data.MFieldX) * 180.0 / M_PI;
return angle < 0 ? 360 + angle : angle;
}

View File

@ -63,27 +63,116 @@ typedef struct
} QMC5883L_MData_calibrated_t;
/* QMC5883L Driver API */
/**
* @brief Initializes the QMC5883L sensor. This must be called before any other API functions.
* Don't forget to initialize the I2C peripheral using the i2c_init function first !
*
* @return true on success
* @return false on failure
*/
bool QMC5883L_init(void);
/**
* @brief Issues a software reset of the sensor.
*
* @return true on success
* @return false on failure
*/
bool QMC5883L_software_reset(void);
/**
* @brief Gets the temperature measured by the sensor in degrees celsius.
* Only the relative temperature value is accurate !
* If you need the real temperature, a calibration offset IS NEEDED using the @ref QMC5883L_set_calibration_data function !
*
* @return float the temperature in °C
*/
float QMC5883L_get_temperature(void);
/**
* @brief Indicates if data is ready to be read from the sensor or not.
* You can check the ouput of this function before calling @ref QMC5883L_get_MFields_raw or @ref QMC5883L_get_MFields_calibrated.
*
* @return true if data is available
* @return false if no data is available
*/
bool QMC5883L_is_data_available(void);
void QMC5883L_set_calibration_data(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max, int16_t z_min, int16_t z_max);
/**
* @brief Sets all the calibration data needed to get accurate readings from the sensor.
* Default value if not set is 0.
* You can use the @ref QMC5883L_get_MFields_raw function to get raw readings from the sensor in order to find the min and max values of the three axes.
*
* @param x_min the min value of the magnetic field on the X axis
* @param x_max the max value of the magnetic field on the X axis
* @param y_min the min value of the magnetic field on the Y axis
* @param y_max the max value of the magnetic field on the Y axis
* @param z_min the min value of the magnetic field on the Z axis
* @param z_max the max value of the magnetic field on the Z axis
* @param temperature_offset the temperature offset needed to be applied to get an accurate reading
*/
void QMC5883L_set_calibration_data(int16_t x_min, int16_t x_max, int16_t y_min, int16_t y_max, int16_t z_min, int16_t z_max, float temperature_offset);
/**
* @brief Reads the latest data available from the sensor and returns a structure containing :
* The magnetic field value for the X axis as a signed integer on 16 bits
* The magnetic field value for the Y axis as a signed integer on 16 bits
* The magnetic field value for the Z axis as a signed integer on 16 bits
* These values are raw data, this means that they are not calibrated ! Use the @ref QMC5883L_get_MFields_calibrated instead.
*
* @return QMC5883L_MData_t the structure containing the fields
*/
QMC5883L_MData_t QMC5883L_get_MFields_raw(void);
/**
* @brief Reads the latest data available from the sensor, calibrates them and returns a structure containing :
* The calibrated magnetic field value for the X axis as a signed integer on 16 bits
* The calibrated magnetic field value for the Y axis as a signed integer on 16 bits
* The calibrated magnetic field value for the Z axis as a signed integer on 16 bits
*
* @return QMC5883L_MData_calibrated_t the structure containing the calibrated fields
*/
QMC5883L_MData_calibrated_t QMC5883L_get_MFields_calibrated(void);
int16_t QMC5883L_get_azimuth(const QMC5883L_MData_calibrated_t data);
/**
* @brief Returns the azimuth (the angle to north) in degrees.
*
* @param data a previously retrieved calibrated data using the @ref QMC5883L_get_MFields_calibrated function
* @return uint16_t the angle in degrees between 0 and 359.
*/
uint16_t QMC5883L_get_azimuth(const QMC5883L_MData_calibrated_t data);
/**
* @brief Configures the sensor by setting the Output Data Rate, the Range of Scale and the Over Sampling Rate.
*
* @param ODR the output data rate, can be ODR_10HZ, ODR_50HZ, ODR_100HZ or ODR_200HZ
* @param RNG the range or scale, can be FS_2G or FS_8G.
* @param OSR the over sampling rate, can be OSR_512, OSR_256, OSR_128 or OSR_64.
* @return true on success
* @return false on failure
*/
bool QMC5883L_configure_1(QMC5883L_Output_Data_Rate_e ODR, QMC5883L_Full_Scale_e RNG, QMC5883L_Over_Sample_Ratio_e OSR);
/**
* @brief Configures the sensor by enabling the rolling pointer functionality (not useful) and the data ready interrupt pin.
*
* @param rolling_ptr true to enable or false to disable
* @param data_ready_int true to enable or false to disable
* @return true on success
* @return false on failure
*/
bool QMC5883L_configure_2(bool rolling_ptr, bool data_ready_int);
/**
* @brief Toggles between the two availables power modes :
* Standby : puts the sensor in standby modes. In this mode, no measurements are performed and the power consumption is lowest.
* Continuous : puts the sensor in active mode by continuously performing measurement that can be retrieved using the @ref QMC5883L_get_MFields_raw
* or @ref QMC5883L_get_MFields_calibrated functions.
*
* @param MC
* @return true on success
* @return false on failure
*/
bool QMC5883L_set_power_mode(QMC5883L_Mode_Control_e MC);
#endif //QMC5883L_H

View File

@ -6,12 +6,47 @@
#include "wm_type_def.h"
#include "wm_io.h"
/**
* @brief Initializes the I2C peripheral by defining the SDA and SCL pin used as well as the clock frequency.
*
* @param SDAPin the Serial DAta pin to use (check the datasheet to find an I2C capable pin)
* @param SCLPin the Serial CLock pin to use (check the datasheet to find an I2C capable pin)
* @param frequency the clock frequency between 100000 and 400000 Hz
*/
void i2c_init(enum tls_io_name SDAPin, enum tls_io_name SCLPin, uint32_t frequency);
/**
* @brief Writes the given data to the provided register of the slave device.
*
* @param address the 7 bit address of the slave device
* @param reg the address of the register
* @param data the data to write
* @return true on success
* @return false on failure
*/
bool i2c_write_reg(uint8_t address, uint8_t reg, uint8_t data);
/**
* @brief Reads multiple data in one I2C transaction from the slave device.
*
* @param address the 7 bit address of the slave device
* @param reg the address of the register from where the read will start (the device should have read with autoincrement register functionality)
* @param data a pointer to an array of uint8_t to store the read data
* @param length the size in bytes of the data array
* @return true on success
* @return false on failure
*/
bool i2c_read(uint8_t address, uint8_t reg, uint8_t * const data, size_t length);
/**
* @brief Reads a single data from the provided register of the slave device.
*
* @param address the 7 bit address of the slave device
* @param reg the address of the register
* @param data a pointer to an uint8_t variable for storing the data to be read
* @return true on success
* @return false on failure
*/
bool i2c_read_reg(uint8_t address, uint8_t reg, uint8_t * const data);

View File

@ -51,14 +51,6 @@ static void cleanup_event_cb(lv_event_t * e)
LV_LOG_USER("cleanup");
}
//To delete
/*static void timer_anim_cb(lv_timer_t *timer)
{
CompassScreen_t *compassScreen = timer->user_data;
static uint16_t azimuth = 0;
compass_screen_set_azimuth(compassScreen, azimuth++);
}*/
static void create_cardinal(CompassCardinal_t *cardidanl, const char *cardinalText, lv_color_t textColor, lv_coord_t x, lv_coord_t y, lv_obj_t *parent)
{
cardidanl->label = lv_label_create(parent);
@ -110,33 +102,36 @@ void compass_screen_set_azimuth(CompassScreen_t * const compassScreen, uint16_t
//We make sure the azimuth is in the right range ie 0 to 359
azimuth %= 360;
uint16_t azimuthAdjusted = 359 - azimuth;
uint8_t index;
if(azimuth >= 23 && azimuth <= 67)
if(azimuthAdjusted >= 23 && azimuthAdjusted <= 67)
index = 1;//NE
else if(azimuth >= 68 && azimuth <= 112)
else if(azimuthAdjusted >= 68 && azimuthAdjusted <= 112)
index = 2;//E
else if(azimuth >= 113 && azimuth <= 157)
else if(azimuthAdjusted >= 113 && azimuthAdjusted <= 157)
index = 3;//SE
else if(azimuth >= 158 && azimuth <= 202)
else if(azimuthAdjusted >= 158 && azimuthAdjusted <= 202)
index = 4;//S
else if(azimuth >= 203 && azimuth <= 247)
else if(azimuthAdjusted >= 203 && azimuthAdjusted <= 247)
index = 5;//SW
else if(azimuth >= 248 && azimuth <= 292)
else if(azimuthAdjusted >= 248 && azimuthAdjusted <= 292)
index = 6;//W
else if(azimuth >= 293 && azimuth <= 337)
else if(azimuthAdjusted >= 293 && azimuthAdjusted <= 337)
index = 7;//NW
else
index = 0;//N
//Update the center label
sprintf(compassScreen->compassAzimuth.text, "%u° %s", azimuth, cardinals[index]);
sprintf(compassScreen->compassAzimuth.text, "%u° %s", azimuthAdjusted, cardinals[index]);
lv_label_set_text_static(compassScreen->compassAzimuth.label, compassScreen->compassAzimuth.text);
//Rotate each cardinal with the current azimuth
rotate_cardinal(&compassScreen->northCardinal, azimuth);
lv_img_set_angle(compassScreen->northMarker, azimuth*10);
//lv_meter_set_scale_range(compassScreen->compassGraduation.meter, compassScreen->compassGraduation.scale, 0, 330, 330, azimuth-90);
rotate_cardinal(&compassScreen->eastCardinal, azimuth);
rotate_cardinal(&compassScreen->southCardinal, azimuth);
rotate_cardinal(&compassScreen->westCardinal, azimuth);
@ -165,22 +160,32 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
compassScreen->display = lv_obj_create(NULL);
lv_obj_set_style_bg_color(compassScreen->display, lv_color_white(), LV_PART_MAIN);
//Let's add some arcs
lv_obj_t *arc = lv_arc_create(compassScreen->display);
lv_arc_set_angles(arc, 0, 360);
lv_obj_set_style_arc_width(arc, 2, LV_PART_INDICATOR);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_set_style_arc_width(arc, 0, LV_PART_MAIN);
lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE);
lv_obj_set_size(arc, 168, 168);
lv_obj_center(arc);
arc = lv_arc_create(compassScreen->display);
lv_arc_set_angles(arc, 0, 360);
lv_obj_set_style_arc_width(arc, 2, LV_PART_INDICATOR);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_set_style_arc_width(arc, 0, LV_PART_MAIN);
lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE);
lv_obj_set_size(arc, 210, 210);
lv_obj_center(arc);
//Marker images are created here
lv_obj_t *compassMarker = lv_img_create(compassScreen->display);
lv_img_set_src(compassMarker, &compass_marker);
lv_obj_set_pos(compassMarker, 113, 0);
if(compassScreen->northMarker)
{
LV_LOG_ERROR("img should be NULL here !");
lv_obj_del(compassScreen->northMarker);
compassScreen->northMarker = NULL;
}
compassScreen->northMarker = lv_img_create(compassScreen->display);
lv_img_set_src(compassScreen->northMarker, &compass_marker_north);
lv_obj_set_pos(compassScreen->northMarker, 113, 20);
lv_img_set_pivot(compassScreen->northMarker, 7, 100);
//Azimuth label is created here
if(compassScreen->compassAzimuth.label)
{
@ -193,6 +198,35 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
lv_obj_set_style_text_font(compassScreen->compassAzimuth.label, &lv_font_montserrat_28, LV_PART_MAIN);
lv_obj_center(compassScreen->compassAzimuth.label);
//Add some graduation : ( was nice but is too CPU intensive :( )
/*if(compassScreen->compassGraduation.meter)
{
LV_LOG_ERROR("meter should be NULL here !");
lv_obj_del(compassScreen->compassGraduation.meter);
compassScreen->compassGraduation.meter = NULL;
}
compassScreen->compassGraduation.meter = lv_meter_create(compassScreen->display);
lv_obj_remove_style(compassScreen->compassGraduation.meter, NULL, LV_PART_INDICATOR);
lv_obj_remove_style(compassScreen->compassGraduation.meter, NULL, LV_PART_MAIN);
lv_obj_set_size(compassScreen->compassGraduation.meter, 204, 204);
lv_obj_center(compassScreen->compassGraduation.meter);
compassScreen->compassGraduation.scale = lv_meter_add_scale(compassScreen->compassGraduation.meter);
lv_meter_set_scale_ticks(compassScreen->compassGraduation.meter, compassScreen->compassGraduation.scale, 12, 1, 0, lv_color_black());
lv_meter_set_scale_major_ticks(compassScreen->compassGraduation.meter, compassScreen->compassGraduation.scale, 1, 1, 0, lv_color_black(), 11);
lv_meter_set_scale_range(compassScreen->compassGraduation.meter, compassScreen->compassGraduation.scale, 0, 330, 330, -90);*/
if(compassScreen->northMarker)
{
LV_LOG_ERROR("img should be NULL here !");
lv_obj_del(compassScreen->northMarker);
compassScreen->northMarker = NULL;
}
compassScreen->northMarker = lv_img_create(compassScreen->display);
lv_img_set_src(compassScreen->northMarker, &compass_marker_north);
lv_obj_set_pos(compassScreen->northMarker, 113, 18);
lv_img_set_pivot(compassScreen->northMarker, 7, 101);
//Cardinal labels are created here
if(compassScreen->northCardinal.label)
{
@ -200,8 +234,7 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
lv_obj_del(compassScreen->northCardinal.label);
compassScreen->northCardinal.label = NULL;
}
create_cardinal(&compassScreen->northCardinal, cardinals[0], lv_palette_main(LV_PALETTE_RED), 120, 50, compassScreen->display);
create_cardinal(&compassScreen->northCardinal, cardinals[0], lv_palette_main(LV_PALETTE_RED), 120, 53, compassScreen->display);
if(compassScreen->eastCardinal.label)
{
@ -209,7 +242,7 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
lv_obj_del(compassScreen->eastCardinal.label);
compassScreen->eastCardinal.label = NULL;
}
create_cardinal(&compassScreen->eastCardinal, cardinals[2], lv_color_black(), 190, 120, compassScreen->display);
create_cardinal(&compassScreen->eastCardinal, cardinals[2], lv_color_black(), 187, 120, compassScreen->display);
if(compassScreen->southCardinal.label)
{
@ -217,7 +250,7 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
lv_obj_del(compassScreen->southCardinal.label);
compassScreen->southCardinal.label = NULL;
}
create_cardinal(&compassScreen->southCardinal, cardinals[4], lv_color_black(), 120, 190, compassScreen->display);
create_cardinal(&compassScreen->southCardinal, cardinals[4], lv_color_black(), 120, 187, compassScreen->display);
if(compassScreen->westCardinal.label)
{
@ -225,15 +258,12 @@ void compass_screen_create(CompassScreen_t * const compassScreen)
lv_obj_del(compassScreen->westCardinal.label);
compassScreen->westCardinal.label = NULL;
}
create_cardinal(&compassScreen->westCardinal, cardinals[6], lv_color_black(), 50, 120, compassScreen->display);
create_cardinal(&compassScreen->westCardinal, cardinals[6], lv_color_black(), 53, 120, compassScreen->display);
//We register the event callback to handle gestures
lv_obj_add_event_cb(compassScreen->display, &(gesture_event_cb), LV_EVENT_GESTURE, compassScreen);
//We register the event callback to handle the cleanup
lv_obj_add_event_cb(compassScreen->display, &(cleanup_event_cb), LV_EVENT_DELETE, compassScreen);
//To delete
//lv_timer_create(&(timer_anim_cb), 2, compassScreen);
}
void compass_screen_destroy(CompassScreen_t * const compassScreen)
@ -251,4 +281,6 @@ void compass_screen_destroy(CompassScreen_t * const compassScreen)
compassScreen->southCardinal.label = NULL;
compassScreen->westCardinal.label = NULL;
compassScreen->northMarker = NULL;
//compassScreen->compassGraduation.meter = NULL;
//compassScreen->compassGraduation.scale = NULL;
}

View File

@ -16,6 +16,12 @@ typedef struct CompassCardinal
lv_point_t offset;
} CompassCardinal_t;
typedef struct CompassGraduation
{
lv_obj_t *meter;
lv_meter_scale_t *scale;
} CompassGraduation_t;
/* Compass screen context object */
typedef struct CompassScreen
{
@ -24,6 +30,8 @@ typedef struct CompassScreen
CompassCardinal_t southCardinal;
CompassCardinal_t westCardinal;
//CompassGraduation_t compassGraduation;
lv_obj_t *northMarker;
lv_obj_t *display;

View File

@ -61,6 +61,11 @@ WatchFace_t watchFace;
MenuScreen_t menuScreen;
CompassScreen_t compassScreen;
static uint16_t angle_with_offset(uint16_t angle, uint16_t offset)
{
return (angle + offset) >= 360 ? angle + offset - 360 : angle + offset;
}
void gfx_task(void *param)
{
@ -124,8 +129,9 @@ void gfx_task(void *param)
uint8_t aliveCounter = 0;
QMC5883L_MData_calibrated_t temp;
uint8_t average = 0;
//QMC5883L_MData_calibrated_t temp;
//uint8_t average = 0;
//uint16_t angle;
if(!QMC5883L_init())
APP_LOG_INFO("Failed to init QMC5883L");
@ -142,7 +148,7 @@ void gfx_task(void *param)
else
APP_LOG_INFO("QMC5883L configured");
QMC5883L_set_calibration_data(-900, 2500, -1400, 1400, 2300, 7500);
QMC5883L_set_calibration_data(-900, 2500, -1400, 1400, 2300, 7500, 0.0);
for(;;)
{
@ -156,7 +162,7 @@ void gfx_task(void *param)
QMC5883L_MData_calibrated_t MData = QMC5883L_get_MFields_calibrated();
if(average++ == 0)
/*if(average++ == 0)
{
temp.MFieldX = MData.MFieldX;
temp.MFieldY = MData.MFieldY;
@ -165,19 +171,20 @@ void gfx_task(void *param)
else
{
temp.MFieldX += MData.MFieldX;
temp.MFieldX /= 2;
temp.MFieldX >>= 1;
temp.MFieldY += MData.MFieldY;
temp.MFieldY /= 2;
temp.MFieldY >>= 1;
temp.MFieldZ += MData.MFieldZ;
temp.MFieldZ /= 2;
}
temp.MFieldZ >>= 1;
}*/
if(average == 1)
/*if(average == 1)
{
average = 0;
int16_t angle = QMC5883L_get_azimuth(temp);
compass_screen_set_azimuth(&compassScreen, angle-180);
}
angle = angle_with_offset(QMC5883L_get_azimuth(MData), 180);
compass_screen_set_azimuth(&compassScreen, angle);
}*/
compass_screen_set_azimuth(&compassScreen, angle_with_offset(QMC5883L_get_azimuth(MData), 180));
}
if(++aliveCounter % 200 == 0)
@ -185,6 +192,5 @@ void gfx_task(void *param)
APP_LOG_DEBUG("GFX thread");
aliveCounter = 0;
}
}
}

View File

@ -17,14 +17,14 @@ static void menu_item_cb(lv_event_t *e)
{
extern WatchFace_t watchFace;
watch_face_create(&watchFace);
lv_scr_load_anim(watchFace.display, LV_SCR_LOAD_ANIM_FADE_ON, 400, 0, true);
lv_scr_load_anim(watchFace.display, LV_SCR_LOAD_ANIM_MOVE_LEFT, 400, 0, true);
}
break;
case 1:
{
extern CompassScreen_t compassScreen;
compass_screen_create(&compassScreen);
lv_scr_load_anim(compassScreen.display, LV_SCR_LOAD_ANIM_FADE_ON, 400, 0, true);
lv_scr_load_anim(compassScreen.display, LV_SCR_LOAD_ANIM_MOVE_LEFT, 400, 0, true);
}
break;
case 7: