From eb2c26d85f1c39e3bbd1c2ab4a12d2421534e292 Mon Sep 17 00:00:00 2001 From: Th3maz1ng Date: Sun, 15 Jan 2023 20:36:32 +0100 Subject: [PATCH] Added new folder/module containing the API to interract with some of the watch feature like measuring the battery voltage or activating the vibration motor --- .../app_drivers/watch_peripherals/Makefile | 15 ++++ .../watch_peripherals/watch_peripherals.c | 77 +++++++++++++++++++ .../watch_peripherals/watch_peripherals.h | 19 +++++ 3 files changed, 111 insertions(+) create mode 100644 src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/Makefile create mode 100644 src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c create mode 100644 src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h diff --git a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/Makefile b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/Makefile new file mode 100644 index 0000000..4477528 --- /dev/null +++ b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/Makefile @@ -0,0 +1,15 @@ +TOP_DIR = ../../.. +sinclude $(TOP_DIR)/tools/w800/conf.mk + +ifndef PDIR +GEN_LIBS = libappdriverswatch_peripherals$(LIB_EXT) +endif + +#DEFINES += + +sinclude $(TOP_DIR)/tools/w800/rules.mk + +INCLUDES := $(INCLUDES) -I $(PDIR)include + +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile \ No newline at end of file diff --git a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c new file mode 100644 index 0000000..faa784e --- /dev/null +++ b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.c @@ -0,0 +1,77 @@ +#include "watch_peripherals.h" +#include "app_config.h" +#include "app_log.h" +#include "wm_gpio_afsel.h" +#include "wm_adc.h" +#include "wm_timer.h" +#include "wm_pwm.h" + + +static uint8_t _adc_offset = 0; +static uint8_t _vibration_motor_timer_id = WM_TIMER_ID_INVALID; + +static void vibration_motor_timer_irq_cb(void *p) +{ + (void)p; + //tls_gpio_write(VIBRATION_MOTOR_ENABLE, 0); + tls_pwm_stop(3); + tls_gpio_cfg(VIBRATION_MOTOR_ENABLE, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); + tls_gpio_write(VIBRATION_MOTOR_ENABLE, 0); + APP_LOG_DEBUG("Vibration stopped"); +} + +void watch_peripherals_io_init(void) +{ + /* We initialize the ADC input as well as the gpio used to enabled the voltage divider bridge */ + wm_adc_config(BATTERY_VOLTAGE_ADC_CHANNEL); + tls_gpio_cfg(BATTERY_VOLTAGE_DIVIDER_ENABLE, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); + tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 0); + + /* We initialize the output pin for the vibration motor enable pin */ + /*tls_gpio_cfg(VIBRATION_MOTOR_ENABLE, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING); + tls_gpio_write(VIBRATION_MOTOR_ENABLE, 0);*/ +} + +void watch_peripherals_init(uint8_t adcOffset) +{ + watch_peripherals_io_init(); + _adc_offset = adcOffset; + + /* Prevent multiple init call !*/ + if(WM_TIMER_ID_INVALID == _vibration_motor_timer_id) + { + /* Let's initialize the timer w'll use to stop the vibration motor */ + struct tls_timer_cfg vibration_motor_timer_cfg = + { + .arg = NULL, + .callback = &(vibration_motor_timer_irq_cb), + .unit = TLS_TIMER_UNIT_MS, + .is_repeat = false, + .timeout = 0 + }; + _vibration_motor_timer_id = tls_timer_create(&vibration_motor_timer_cfg); + + if(WM_TIMER_ID_INVALID == _vibration_motor_timer_id) + APP_LOG_ERROR("Failed to create timer"); + } +} + +uint16_t watch_peripherals_get_battery_voltage(void) +{ + tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 1); + int batteryVoltage = adc_get_inputVolt(BATTERY_VOLTAGE_ADC_CHANNEL, 1, 1) * 2 + _adc_offset; + tls_gpio_write(BATTERY_VOLTAGE_DIVIDER_ENABLE, 0); + return batteryVoltage < 0 ? 0 : batteryVoltage; +} + +void watch_peripherals_vibrate(uint8_t strength, uint32_t durationMs) +{ + APP_LOG_DEBUG("Vibration started"); + /* We start the timer which will stop the vibration after durationMs time */ + tls_timer_change(_vibration_motor_timer_id, durationMs); + //tls_gpio_write(VIBRATION_MOTOR_ENABLE, 1); + wm_pwm3_config(VIBRATION_MOTOR_ENABLE); + tls_pwm_init(3, 10000, 0, 0); + tls_pwm_duty_set(3, strength); + tls_timer_start(_vibration_motor_timer_id); +} diff --git a/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h new file mode 100644 index 0000000..e3eaff9 --- /dev/null +++ b/src/W800 SDK v1.00.08/app/app_drivers/watch_peripherals/watch_peripherals.h @@ -0,0 +1,19 @@ +/** + * Declares various functions to interract with some of the watch's + * peripherals like : reading the battery voltage, using the vibration motor etc. + */ +#ifndef WATCH_PERIPHERALS_H +#define WATCH_PERIPHERALS_H +#include "wm_type_def.h" + +void watch_peripherals_io_init(void); + +void watch_peripherals_init(uint8_t adcOffset); + +uint16_t watch_peripherals_get_battery_voltage(void); + +uint8_t watch_peripherals_battery_voltage_to_level(uint16_t voltage); + +void watch_peripherals_vibrate(uint8_t strength, uint32_t durationMs); + +#endif //WATCH_PERIPHERALS_H \ No newline at end of file