W800_Smart_Watch/src/W800 SDK v1.00.08/app/gfx/gfx_task.c

350 lines
11 KiB
C

#include "app_common.h"
#include "lvgl.h"
#include "FreeRTOS.h"
#include "wm_include.h"
#include "lcd.h"
#include "watch_face.h"
#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 "lv_port_indev.h"
#define LVGL_GFX_BUFFER_SIZE (LCD_PIXEL_WIDTH * LCD_PIXEL_HEIGHT / 2)
static lv_color_t lvgl_draw_buffer[LVGL_GFX_BUFFER_SIZE];
static lv_disp_draw_buf_t lvgl_draw_buffer_dsc;
static lv_disp_drv_t display_driver;
static LCDConfig_t LCDConfig;
static void lvgl_display_flush_cb(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
lcd_draw_rect_frame(&LCDConfig, area->x1, area->y1, area->x2 - area->x1 + 1, area->y2 - area->y1 + 1, (uint8_t *)color_p);
/* lv_disp_flush_ready is called in the LCD driver registered callback */
}
static void lcd_draw_finished_cb(void *arg)
{
lv_disp_flush_ready((lv_disp_drv_t *)arg);
}
static void date_time_cb(struct tm * const dateTime)
{
if(!dateTime)return;
tls_get_rtc(dateTime);
//APP_LOG_DEBUG("RTC time : %d:%d:%d", dateTime->tm_hour, dateTime->tm_min, dateTime->tm_sec);
}
WatchFace_t watchFace;
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;
uint16_t int_status;
static void setBrightnessCb(uint8_t brightness)
{
lcd_set_backlight(&LCDConfig, brightness);
}
static void setTimeCb(uint8_t hour, uint8_t minute, uint8_t second, uint8_t day, uint8_t month, uint8_t year)
{
struct tm timeToSet;
//First we get the current time from the RTC
tls_get_rtc(&timeToSet);
if(hour != 0xFF)
timeToSet.tm_hour = hour;
if(minute != 0xFF)
timeToSet.tm_min = minute;
if(second != 0xFF)
timeToSet.tm_sec = second;
if(day != 0xFF)
timeToSet.tm_mday = day;
if(month != 0xFF)
timeToSet.tm_mon = month;
if(year != 0xFF)
timeToSet.tm_year = year + 100;
tls_set_rtc(&timeToSet);
}
SettingsScreenAPIInterface_t settingsScreenAPIInterface =
{
.setBrightnessSettingsCb = setBrightnessCb,
.setTimeSettingsCb = setTimeCb
};
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)
{
uint8_t dev_address = *(uint8_t*)intf_ptr;
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));
}
/*uint16_t x = 0, y = 0, action = 0;
static void touch_screen_isr(void *arg)
{
tls_clr_gpio_irq_status(WM_IO_PB_00);
uint8_t tc_data[9];
i2c_read(0x15, 0x00, tc_data, sizeof tc_data);
x = ((tc_data[3] & 0x0f) << 8) | tc_data[4];
y = ((tc_data[5] & 0x0f) << 8) | tc_data[6];
action = tc_data[3] >> 6;
}*/
void gfx_task(void *param)
{
APP_LOG_TRACE("starting");
/* Initialize the lvgl library*/
lv_init();
/* Create, initialize and register the display driver*/
lv_disp_drv_init(&display_driver);
display_driver.hor_res = LCD_PIXEL_WIDTH;
display_driver.ver_res = LCD_PIXEL_HEIGHT;
display_driver.rotated = LV_DISP_ROT_NONE;
lv_disp_draw_buf_init(&lvgl_draw_buffer_dsc, lvgl_draw_buffer, NULL, LVGL_GFX_BUFFER_SIZE);
display_driver.draw_buf = &lvgl_draw_buffer_dsc;
display_driver.flush_cb = &(lvgl_display_flush_cb);
/* Initialize the LCD display */
lcd_config_init(&LCDConfig);
lcd_register_draw_finished_cb(&LCDConfig, &(lcd_draw_finished_cb), &display_driver);
#if defined(W800)
LCDConfig.LCDPWMBacklightPin = WM_IO_PA_07;
LCDConfig.LCDClockPin = WM_IO_PB_06;
LCDConfig.LCDDataPin = WM_IO_PB_07;
LCDConfig.LCDChipSelectPin = WM_IO_PB_10;
LCDConfig.LCDDataCommandPin = WM_IO_PB_08;
LCDConfig.LCDResetPin = WM_IO_PB_09;
#elif defined(W801)
LCDConfig.LCDPWMBacklightPin = WM_IO_PA_08;
LCDConfig.LCDClockPin = WM_IO_PA_09;
LCDConfig.LCDDataPin = WM_IO_PA_10;
LCDConfig.LCDChipSelectPin = WM_IO_PA_13;
LCDConfig.LCDDataCommandPin = WM_IO_PA_11;
LCDConfig.LCDResetPin = WM_IO_PA_12;
#endif
lcd_init(&LCDConfig);
if(!lv_disp_drv_register(&display_driver))
{
APP_LOG_ERROR("Failed to register display driver");
}
lv_port_indev_init();
watch_face_init(&watchFace);
menu_screen_init(&menuScreen);
compass_screen_init(&compassScreen);
settings_screen_init(&settingsScreen);
settings_screen_register_API_interface(&settingsScreen, &settingsScreenAPIInterface);
watch_face_register_cb(&watchFace, &(date_time_cb));
watch_face_create(&watchFace);
lv_scr_load(watchFace.display);
/* Let's init the I2C interface */
i2c_init(WM_IO_PA_04, WM_IO_PA_01, 100000);
uint8_t aliveCounter = 0;
/* 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, 1, &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");
/*tls_gpio_cfg(WM_IO_PB_00, WM_GPIO_DIR_INPUT, WM_GPIO_ATTR_PULLHIGH);
tls_gpio_isr_register(WM_IO_PB_00, &(touch_screen_isr), NULL);
tls_gpio_irq_enable(WM_IO_PB_00, WM_GPIO_IRQ_TRIG_FALLING_EDGE);*/
for(;;)
{
lv_timer_handler();
tls_os_time_delay(5);
if(compass_screen_is_in_use(&compassScreen) && QMC5883L_is_data_available())
{
/*__disable_irq();
QMC5883L_MData_t MDataRaw = QMC5883L_get_MFields_raw();
APP_LOG_TRACE("X %d Y %d Z %d", MDataRaw.MFieldX, MDataRaw.MFieldY, MDataRaw.MFieldZ);
__enable_irq();*/
QMC5883L_MData_calibrated_t MData = QMC5883L_get_MFields_calibrated();
compass_screen_set_azimuth(&compassScreen, angle_with_offset(QMC5883L_get_azimuth(MData), 180));
}
/*uint8_t rslt = bma456w_read_int_status(&int_status, &bma);
if(rslt != BMA4_OK)
APP_LOG_DEBUG("Failed to read int status");
if((BMA4_OK == rslt) && (int_status & BMA456W_WRIST_WEAR_WAKEUP_INT))
{
APP_LOG_DEBUG("Wrist tilt");
}*/
if(++aliveCounter % 200 == 0)
{
float temp = BMP280_get_temperature();
BMP280_trigger_measurement();
APP_LOG_DEBUG("GFX thread, temp : %0.2f", temp);
//APP_LOG_DEBUG("------------ x %d y %d, action : %d", x, y, action);
aliveCounter = 0;
}
/* Handle inactivity periods : */
/*if(lv_disp_get_inactive_time(NULL) > 10000)
{
// First, we disable the display backlight and we set all the peripherals in their low power mode
lcd_set_backlight(&LCDConfig, 0);
// Let's sleep
tls_pmu_sleep_start();
// On wake up, we force the watch face to sync up with the rtc /!\ RTC update delay WTF ?
tls_os_time_delay(1);
watch_face_force_sync(&watchFace);
lv_disp_trig_activity(NULL);
lcd_set_backlight(&LCDConfig, 255);
}*/
}
}