Did some cleanup by removing direct access to the watch sensor for initialization, added the gadget bridge parser I coded, and other minor stuff

This commit is contained in:
Th3maz1ng 2023-04-10 21:20:38 +02:00
parent 22621219f3
commit 288a64d1ce

View File

@ -9,16 +9,13 @@
#include "menu_screen.h"
#include "compass_screen.h"
#include "settings_screen.h"
#include "i2c.h"
#include "QMC5883L.h"
#include "BMP280.h"
#include "bma456w.h"
#include "CST816D.h"
#include "watch_peripherals.h"
#include "watch_settings.h"
#include "ble_modem.h"
#include "ble_service.h"
#include "gadget_bridge.h"
static void date_time_cb(struct tm * const dateTime)
{
@ -34,16 +31,10 @@ MenuScreen_t menuScreen;
CompassScreen_t compassScreen;
SettingsScreen_t settingsScreen;
struct bma4_dev bma;
struct bma4_accel_config accel_conf;
struct bma456w_wrist_wear_wakeup_params setting;
struct bma4_int_pin_config pin_config;
static struct
{
uint16_t int_status;
bool battery_controller_status;
} _interrupts_statuses = {.int_status = 0, .battery_controller_status = false};
} _interrupts_statuses = {.battery_controller_status = false};
static struct
{
@ -60,10 +51,8 @@ static void battery_indicator_cb(uint8_t *levelInPercent, BatteryState_e *batter
static void step_count_cb(uint32_t *steps)
{
if(bma456w_step_counter_output(steps, &bma) != BMA4_OK)
if(!watch_peripherals_accelerometer_step_count_read(steps))
APP_LOG_DEBUG("Failed to read step counts");
else
watch_face_set_step_count_indicator(&watchFace, *steps);
}
static void battery_controller_status_on_change_cb(battery_controller_status_e old, battery_controller_status_e new)
@ -87,6 +76,18 @@ static void setGetBrightnessCb(uint8_t *brightness, SettingMode_e mode)
}
}
static void setAutomaticTimeCb(bool *enabled, SettingMode_e mode)
{
if(SETTING_MODE_GET == mode)
{
*enabled = persistency_get_settings()->timeAndDate.time_and_date_automatic;
}
else
{
watch_settings_time_and_date_set_automatic(*enabled);
}
}
static void setTimeCb(uint8_t *hour, uint8_t *minute, uint8_t *second, uint8_t *day, uint8_t *month, uint8_t *year, SettingMode_e mode)
{
struct tm timeToSet;
@ -190,6 +191,19 @@ static void setOrientationCb(uint8_t *orientation, SettingMode_e mode)
}
}
static void setWristTiltCb(bool *enabled, SettingMode_e mode)
{
if(SETTING_MODE_GET == mode)
{
*enabled = persistency_get_settings()->display.display_wrist_wakeup;
}
else
{
watch_peripherals_accelerometer_wrist_wakeup_enable(*enabled);
watch_settings_display_set_wrist_wakeup(*enabled);
}
}
static void setBLEEnabledCb(bool *enabled, SettingMode_e mode)
{
if(SETTING_MODE_GET == mode)
@ -277,6 +291,7 @@ static void performFactoryResetCb()
SettingsScreenAPIInterface_t settingsScreenAPIInterface =
{
.setAutomaticTimeSettingsCb = &(setAutomaticTimeCb),
.setTimeSettingsCb = &(setTimeCb),
.setTimeFormatSettingsCb = &(setTimeFormatCb),
.setBrightnessSettingsCb = &(setGetBrightnessCb),
@ -284,6 +299,7 @@ SettingsScreenAPIInterface_t settingsScreenAPIInterface =
.setDisplayVibrationDurationSettingsCb = &(setDisplayVibrationDuration),
.setDisplayVibrationStrengthSettingsCb = &(setDisplayVibrationStrength),
.setOrientationSettingsCb = &(setOrientationCb),
.setWristTiltSettingsCb = &(setWristTiltCb),
.setBLEEnabledSettingsCb = &(setBLEEnabledCb),
.setWiFiEnabledSettingsCb = &(setWiFiEnabledCb),
.setLanguageSettingsCb = &(setLanguageCb),
@ -298,39 +314,38 @@ static uint16_t angle_with_offset(uint16_t angle, uint16_t offset)
return (angle + offset) >= 360 ? angle + offset - 360 : angle + offset;
}
static BMA4_INTF_RET_TYPE bma4_i2c_read(uint8_t reg_addr, uint8_t *read_data, uint32_t len, void *intf_ptr)
static void parser_event_cb(const gadget_bridge_event_data_t *gadget_bridge_event_data)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
APP_LOG_INFO("[GB]Event of type : %s\n", gadget_bridge_event_type_2_str(gadget_bridge_event_data->event_type));
return !i2c_read(dev_address, reg_addr, read_data, len);
}
static BMA4_INTF_RET_TYPE bma4_i2c_write(uint8_t reg_addr, const uint8_t *read_data, uint32_t len, void *intf_ptr)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
return !i2c_write(dev_address, reg_addr, read_data, len);
}
static void delay_us(uint32_t period, void *intf_ptr)
{
(void) intf_ptr;
if(period < 1000)
tls_os_time_delay(1);
else
tls_os_time_delay(pdMS_TO_TICKS(period / 1000));
switch(gadget_bridge_event_data->event_type)
{
case GADGET_BRIDGE_EVENT_TYPE_SET_TIME:
if(persistency_get_settings()->timeAndDate.time_and_date_automatic)
{
// Only in this case we set the time
tls_set_rtc((struct tm *)&gadget_bridge_event_data->time.local_time);
// We force the sync of the watch face :)
watch_face_force_sync(&watchFace);
}
break;
default:
APP_LOG_INFO("Not yet handled\n");
}
}
static void ble_service_nus_data_rx_cb(const uint8_t *data, uint16_t length)
{
for (uint16_t i = 0; i < length; i++)
/*for (uint16_t i = 0; i < length; i++)
{
if (data[i] < 32)
printf("[%u]", data[i]);
else
printf("%c", data[i]);
}
}*/
gadget_bridge_parser_feed((const char *)data, length);
while(gadget_bridge_parser_run() == GADGET_BRIDGE_PARSER_CODE_PARSING);
}
static void ble_service_state_change_cb(ble_service_state_e ble_service_state)
@ -405,6 +420,14 @@ void gfx_task(void *param)
watch_peripherals_init(27);
watch_peripherals_register_battery_controller_status_change_cb(&(battery_controller_status_on_change_cb));
/* Let's init all the watch's sensors */
watch_peripherals_pressure_sensor_init();
watch_peripherals_magnetometer_init();
watch_peripherals_magnetometer_calibration_data_set(4812, 8550, -3200, 20, -2200, 3500, 0.0);
watch_peripherals_accelerometer_init();
watch_peripherals_accelerometer_wrist_wakeup_enable(persistency_get_settings()->display.display_wrist_wakeup);
watch_peripherals_accelerometer_step_counter_enable(true);
/* Make the first battery voltage reading here */
_battery_stats.battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv);
_battery_stats.battery_percentage = battery_voltage_to_percentage(_battery_stats.battery_voltage);
@ -440,114 +463,10 @@ void gfx_task(void *param)
watch_face_create(&watchFace);
lv_scr_load(watchFace.display);
/* Let's init the I2C interface */
i2c_init(I2C_SDA, I2C_SCL, I2C_CLOCK_SPEED);
/* Init the magnetometer */
if(!QMC5883L_init())
APP_LOG_INFO("Failed to init QMC5883L");
else
APP_LOG_INFO("Inited QMC5883L");
tls_os_time_delay(2);
if(!QMC5883L_set_power_mode(Continuous))
APP_LOG_INFO("Failed to set QMC5883L mode");
else
APP_LOG_INFO("QMC5883L Mode set");
tls_os_time_delay(2);
if(!QMC5883L_configure_1(ODR_10HZ, FS_2G, OSR_512))
APP_LOG_INFO("Failed to configure 1 QMC5883L");
else
APP_LOG_INFO("QMC5883L configured");
//QMC5883L_set_calibration_data(-900, 2500, -1400, 1400, 2300, 7500, 0.0);
QMC5883L_set_calibration_data(4812, 8550, -3200, 20, -2200, 3500, 0.0);
/* Init the BMP280 */
if(!BMP280_init())
APP_LOG_INFO("Failed to init BMP280");
else
APP_LOG_INFO("Inited BMP280");
if(!BMP280_configure(BMP280_Forced, BMP280_Oversampling_x16, BMP280_Oversampling_x16, BMP280_Filter_x16, BMP280_Standby_4000MS))
APP_LOG_INFO("Failed to configure BMP280");
else
APP_LOG_INFO("BMP280 configured");
/* Init the BMA456 */
bma.intf = BMA4_I2C_INTF;
uint8_t dev_addr = BMA4_I2C_ADDR_SECONDARY;
bma.intf_ptr = &dev_addr;
bma.bus_read = &(bma4_i2c_read);
bma.bus_write = &(bma4_i2c_write);
bma.variant = BMA45X_VARIANT;
bma.delay_us = &(delay_us);
bma.read_write_len = 46;
bma.perf_mode_status = BMA4_DISABLE;
if(bma456w_init(&bma) == BMA4_OK)
APP_LOG_INFO("BMA456 init");
else
APP_LOG_INFO("Failed to init BMA456");
bma4_soft_reset(&bma);
tls_os_time_delay(2);
if(bma456w_write_config_file(&bma) == BMA4_OK)
APP_LOG_INFO("BMA456 config ok");
else
APP_LOG_INFO("BMA456 config failed");
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
if(bma4_set_accel_config(&accel_conf, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 accel conf ok");
else
APP_LOG_INFO("BMA456 accel conf failed");
if(bma4_set_accel_enable(1, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 accel en ok");
else
APP_LOG_INFO("BMA456 accel en failed");
bma456w_feature_enable(BMA456W_WRIST_WEAR_WAKEUP, BMA4_ENABLE, &bma);
bma456w_get_wrist_wear_wakeup_param_config(&setting, &bma);
APP_LOG_DEBUG("%d %d %d %d %d %d %d %d", setting.min_angle_focus, setting.min_angle_non_focus, setting.angle_landscape_right, setting.angle_landscape_left, setting.angle_portrait_up, setting.angle_portrait_down, setting.min_dur_moved, setting.min_dur_quite);
if(bma4_get_int_pin_config(&pin_config, BMA4_INTR1_MAP, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 get pin conf ok");
else
APP_LOG_INFO("BMA456 get pin conf failed");
if(bma456w_map_interrupt(BMA4_INTR1_MAP, BMA456W_WRIST_WEAR_WAKEUP_INT, BMA4_ENABLE, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 map int ok");
else
APP_LOG_INFO("BMA456 map int failed");
pin_config.edge_ctrl = BMA4_EDGE_TRIGGER;
pin_config.output_en = BMA4_OUTPUT_ENABLE;
pin_config.lvl = BMA4_ACTIVE_LOW;
pin_config.od = BMA4_PUSH_PULL;
pin_config.input_en = BMA4_INPUT_DISABLE;
if(bma4_set_int_pin_config(&pin_config, BMA4_INTR1_MAP, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 set pin conf ok");
else
APP_LOG_INFO("BMA456 set pin conf failed");
/* Configure BMA's step counter */
if(bma456w_feature_enable(BMA456W_STEP_CNTR, BMA4_ENABLE, &bma) == BMA4_OK)
APP_LOG_INFO("BMA456 step cnter feature enable ok");
else
APP_LOG_INFO("BMA456 step cnter feature enable failed");
gadget_bridge_parser_register_event_callback(&(parser_event_cb));
/* Configure and register BLE stack and services callbacks */
ble_service_register_nus_data_rx_cb(&(ble_service_nus_data_rx_cb));
ble_service_register_state_change_cb(&(ble_service_state_change_cb));
@ -570,34 +489,31 @@ void gfx_task(void *param)
if(compass_screen_is_in_use(&compassScreen))
{
if(QMC5883L_is_data_available())
bool is_data_available = false;
uint16_t azimuth = watch_peripherals_magnetometer_azimuth_read(&is_data_available);
if(is_data_available)
{
/*
QMC5883L_MData_t MDataRaw = QMC5883L_get_MFields_raw();
QMC5883L_MData_t MDataRaw = watch_peripherals_magnetometer_raw_data_read();
APP_LOG_TRACE("X %d Y %d Z %d", MDataRaw.MFieldX, MDataRaw.MFieldY, MDataRaw.MFieldZ);
*/
QMC5883L_MData_calibrated_t MData = QMC5883L_get_MFields_calibrated();
compass_screen_set_azimuth(&compassScreen, angle_with_offset(QMC5883L_get_azimuth(MData), 180));
compass_screen_set_azimuth(&compassScreen, angle_with_offset(azimuth, 180));
}
compass_screen_set_temperature(&compassScreen, temperature);
}
uint8_t rslt = bma456w_read_int_status(&_interrupts_statuses.int_status, &bma);
if(rslt != BMA4_OK)
APP_LOG_DEBUG("Failed to read int status");
if((BMA4_OK == rslt) && (_interrupts_statuses.int_status & BMA456W_WRIST_WEAR_WAKEUP_INT))
if(watch_peripherals_accelerometer_wrist_wakeup_interrupt())
{
APP_LOG_DEBUG("Wrist tilt");
}
if(lv_tick_elaps(update_tick) > 5000)
{
pressure = BMP280_get_pressure(&temperature);
BMP280_trigger_measurement();
pressure = watch_peripherals_pressure_sensor_get_pressure(&temperature);
_battery_stats.battery_voltage = watch_peripherals_get_battery_voltage(battery_unit_mv);
_battery_stats.battery_percentage = battery_voltage_to_percentage(_battery_stats.battery_voltage);
APP_LOG_DEBUG("GFX thread, temp : %0.2f °C, press : %0.2f hPa, battery(%s) : %u mV <-> %u %%",
@ -619,7 +535,7 @@ void gfx_task(void *param)
watch_peripherals_set_brightness(0);
//lcd_on(&LCDConfig, false);
lcd_sleep(&LCDConfig, true);
QMC5883L_set_power_mode(Standby);
watch_peripherals_magnetometer_power_mode_set(QMC5883L_Mode_Control_Standby);
if(CST816D_sleep())
APP_LOG_DEBUG("CST816D Sleep cmd ok");
else
@ -630,7 +546,7 @@ void gfx_task(void *param)
tls_os_time_delay(1);
watch_face_force_sync(&watchFace);
lv_disp_trig_activity(NULL);
QMC5883L_set_power_mode(Continuous);
watch_peripherals_magnetometer_power_mode_set(QMC5883L_Mode_Control_Continuous);
//lcd_on(&LCDConfig, true);
lcd_sleep(&LCDConfig, false);
//watch_peripherals_set_brightness(persistency_get_settings()->display.display_brightness);