diff options
author | Joey Castillo <jose.castillo@gmail.com> | 2021-08-02 13:36:53 -0400 |
---|---|---|
committer | Joey Castillo <jose.castillo@gmail.com> | 2021-08-02 13:36:53 -0400 |
commit | 2d1e2e8c76623543817f4c62b26fc300c1fd0d2c (patch) | |
tree | 533c3e54397c492024563f27d3125c09e95b6f0c /Sensor Watch Starter Project | |
parent | 7e330befffdc2783207b83f12f30f328f5f3318d (diff) | |
download | Sensor-Watch-2d1e2e8c76623543817f4c62b26fc300c1fd0d2c.tar.gz Sensor-Watch-2d1e2e8c76623543817f4c62b26fc300c1fd0d2c.tar.bz2 Sensor-Watch-2d1e2e8c76623543817f4c62b26fc300c1fd0d2c.zip |
barebones 'hello world' project
Diffstat (limited to 'Sensor Watch Starter Project')
-rw-r--r-- | Sensor Watch Starter Project/app/app.c | 175 | ||||
-rw-r--r-- | Sensor Watch Starter Project/hw/driver_init.c | 6 | ||||
-rwxr-xr-x | Sensor Watch Starter Project/main.c | 3 | ||||
-rwxr-xr-x | Sensor Watch Starter Project/make/Makefile | 3 | ||||
-rw-r--r-- | Sensor Watch Starter Project/watch/watch.c (renamed from Sensor Watch Starter Project/hw/watch.c) | 660 | ||||
-rw-r--r-- | Sensor Watch Starter Project/watch/watch.h (renamed from Sensor Watch Starter Project/hw/watch.h) | 6 |
6 files changed, 400 insertions, 453 deletions
diff --git a/Sensor Watch Starter Project/app/app.c b/Sensor Watch Starter Project/app/app.c index 1ada8a3f..eb862767 100644 --- a/Sensor Watch Starter Project/app/app.c +++ b/Sensor Watch Starter Project/app/app.c @@ -7,129 +7,30 @@ void uart_putc(char c); void uart_puts(char *s); typedef enum ApplicationMode { - MODE_TIME, - MODE_SENSE, - MODE_SET_HOUR, - MODE_SET_MINUTE, - MODE_SET_SECOND + MODE_HELLO = 0, + MODE_THERE } ApplicationMode; +typedef enum LightColor { + COLOR_OFF = 0, + COLOR_RED = 1, + COLOR_GREEN = 2, + COLOR_YELLOW = 3 +} LightColor; + typedef struct ApplicationState { ApplicationMode mode; - bool light_pressed; - bool mode_pressed; - bool alarm_pressed; - bool light_on; - uint16_t dig_T1; /**< dig_T1 cal register. */ - int16_t dig_T2; /**< dig_T2 cal register. */ - int16_t dig_T3; /**< dig_T3 cal register. */ + LightColor color; } ApplicationState; ApplicationState applicationState; void cb_light_pressed() { - applicationState.light_pressed = true; + applicationState.color = (applicationState.color + 1) % 4; } void cb_mode_pressed() { - applicationState.mode_pressed = true; -} - -void cb_alarm_pressed() { - applicationState.alarm_pressed = true; -} - -void cb_tick() { - ; -} -#define BMP280_REGISTER_DIG_T1 0x88 -#define BMP280_REGISTER_DIG_T2 0x8A -#define BMP280_REGISTER_DIG_T3 0x8C -#define BMP280_REGISTER_SOFTRESET 0xE0 -#define BMP280_REGISTER_STATUS 0xF3 -#define BMP280_REGISTER_CONTROL 0xF4 -#define BMP280_REGISTER_CONFIG 0xF5 -#define BMP280_REGISTER_PRESSUREDATA 0xF7 -#define BMP280_REGISTER_TEMPDATA 0xFA - -uint16_t read8(uint8_t reg) { - uint8_t val; - watch_i2c_send(0x77, ®, 1); - watch_i2c_receive(0x77, &val, 1); - return val; -} - -uint16_t read16(uint8_t reg) { - uart_puts("\nReading 2 bytes... "); - uint8_t buf[2]; - watch_i2c_send(0x77, ®, 1); - watch_i2c_receive(0x77, (uint8_t *)&buf, 2); - uart_puts("received!\n"); - char buf2[32] = {0}; - sprintf(buf2, "buf has values: %#02x, %#02x", buf[0], buf[1]); - uart_puts(buf2); - - return (buf[0] << 8) | buf[1]; -} - -uint32_t read24(uint8_t reg) { - uart_puts("\nReading 3 bytes... "); - uint32_t value; - uint8_t buf[3]; - - watch_i2c_send(0x77, ®, 1); - watch_i2c_receive(0x77, (uint8_t *)&buf, 3); - uart_puts("received!\n"); - char buf2[33] = {0}; - sprintf(buf2, "buf has values: %#02x, %#02x, %#02x", buf[0], buf[1], buf[2]); - uart_puts(buf2); - - value = buf[0]; - value <<= 8; - value |= buf[1]; - value <<= 8; - value |= buf[2]; - - return value; -} - -uint16_t read16_LE(uint8_t reg) { - uint16_t temp = read16(reg); - return (temp >> 8) | (temp << 8); -} - -int16_t readS16(uint8_t reg) { - return (int16_t)read16(reg); -} - -int16_t readS16_LE(uint8_t reg) { - return (int16_t)read16_LE(reg); -} - -void print_temperature() { - int32_t var1, var2; - int32_t t_fine; - - int32_t adc_T = read24(BMP280_REGISTER_TEMPDATA); - adc_T >>= 4; - - var1 = ((((adc_T >> 3) - ((int32_t)applicationState.dig_T1 << 1))) * - ((int32_t)applicationState.dig_T2)) >> - 11; - - var2 = (((((adc_T >> 4) - ((int32_t)applicationState.dig_T1)) * - ((adc_T >> 4) - ((int32_t)applicationState.dig_T1))) >> - 12) * - ((int32_t)applicationState.dig_T3)) >> - 14; - - t_fine = var1 + var2; - - float T = ((t_fine * 5 + 128) >> 8) / 100.0; - - char buf[32] = {0}; - sprintf(buf, "\n\nTemp is %3.2f degrees C", T); - uart_puts(buf); + applicationState.mode = (applicationState.mode + 1) % 2; } /** @@ -143,26 +44,14 @@ void print_temperature() { */ void app_init() { memset(&applicationState, 0, sizeof(applicationState)); + + watch_enable_led(false); + watch_enable_buttons(); watch_register_button_callback(BTN_LIGHT, cb_light_pressed); + watch_register_button_callback(BTN_MODE, cb_mode_pressed); - watch_enable_date_time(); - watch_enable_tick_callback(cb_tick); - watch_enable_display(); - watch_enable_led(); - watch_enable_i2c(); - - uart_puts("\n\nI2C Driver Test\n"); - uint8_t reset_cmd[] = {0xE0, 0xB6}; - watch_i2c_send(0x77, reset_cmd, 2); - uart_puts("Reset BMP280\n"); - applicationState.dig_T1 = read16_LE(BMP280_REGISTER_DIG_T1); - applicationState.dig_T2 = readS16_LE(BMP280_REGISTER_DIG_T2); - applicationState.dig_T3 = readS16_LE(BMP280_REGISTER_DIG_T3); - - uint8_t ctrl_cmd[] = {0xF4, 0xA3}; - watch_i2c_send(0x77, ctrl_cmd, 2); } /** @@ -172,9 +61,6 @@ void app_init() { * a press on one of the buttons). */ void app_prepare_for_sleep() { - applicationState.light_pressed = false; - applicationState.mode_pressed = false; - applicationState.alarm_pressed = false; } /** @@ -182,12 +68,33 @@ void app_prepare_for_sleep() { * STANDBY sleep mode. */ void app_wake_from_sleep() { - } +/** + * @brief the app_loop function is called once on app startup and then again each time + * the watch STANDBY sleep mode. + */ void app_loop() { - if (applicationState.light_pressed) { - print_temperature(); + switch (applicationState.color) { + case COLOR_RED: + watch_set_led_red(); + break; + case COLOR_GREEN: + watch_set_led_green(); + break; + case COLOR_YELLOW: + watch_set_led_yellow(); + break; + default: + applicationState.color = COLOR_OFF; + watch_set_led_off(); + } + switch (applicationState.mode) { + case MODE_HELLO: + watch_display_string("Hello", 5); + break; + case MODE_THERE: + watch_display_string("there", 5); + break; } - uart_putc('.'); } diff --git a/Sensor Watch Starter Project/hw/driver_init.c b/Sensor Watch Starter Project/hw/driver_init.c index 24f552ad..4f1f8eee 100644 --- a/Sensor Watch Starter Project/hw/driver_init.c +++ b/Sensor Watch Starter Project/hw/driver_init.c @@ -25,14 +25,12 @@ struct pwm_descriptor PWM_0; struct pwm_descriptor PWM_1; -void ADC_0_CLOCK_init(void) -{ +void ADC_0_CLOCK_init(void) { hri_mclk_set_APBCMASK_ADC_bit(MCLK); hri_gclk_write_PCHCTRL_reg(GCLK, ADC_GCLK_ID, CONF_GCLK_ADC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); } -void ADC_0_init(void) -{ +void ADC_0_init(void) { ADC_0_CLOCK_init(); adc_sync_init(&ADC_0, ADC, (void *)NULL); } diff --git a/Sensor Watch Starter Project/main.c b/Sensor Watch Starter Project/main.c index 052bbfa0..0d457b5f 100755 --- a/Sensor Watch Starter Project/main.c +++ b/Sensor Watch Starter Project/main.c @@ -85,7 +85,7 @@ void uart_puts(char *s) { //----------------------------------------------------------------------------- static void sys_init(void) { uart_puts("init_mcu\n"); - init_mcu(); + init_mcu(); uart_puts("watch_init\n"); watch_init(); uart_puts("app_init\n"); @@ -101,6 +101,7 @@ int main(void) { app_loop(); app_prepare_for_sleep(); sleep(4); + app_wake_from_sleep(); } return 0; diff --git a/Sensor Watch Starter Project/make/Makefile b/Sensor Watch Starter Project/make/Makefile index 900e51fb..a40eb7ca 100755 --- a/Sensor Watch Starter Project/make/Makefile +++ b/Sensor Watch Starter Project/make/Makefile @@ -56,6 +56,7 @@ INCLUDES += \ -I../hri/ \
-I../config/ \
-I../hw/ \
+ -I../watch/ \
-I../app/ \
-I..
@@ -63,7 +64,7 @@ SRCS += \ ../main.c \
../startup_saml22.c \
../hw/driver_init.c \
- ../hw/watch.c \
+ ../watch/watch.c \
../app/app.c \
../hal/src/hal_adc_sync.c \
../hal/src/hal_atomic.c \
diff --git a/Sensor Watch Starter Project/hw/watch.c b/Sensor Watch Starter Project/watch/watch.c index e7a80339..6ab093bd 100644 --- a/Sensor Watch Starter Project/hw/watch.c +++ b/Sensor Watch Starter Project/watch/watch.c @@ -1,310 +1,350 @@ -/*
- * watch.c
- *
- * Created: 4/25/2021 10:22:10 AM
- * Author: joeycastillo
- */
-
-#include "watch.h"
-#include <stdlib.h>
-
-void watch_init() {
- // use switching regulator
- SUPC->VREG.bit.SEL = 1;
- while(!SUPC->STATUS.bit.VREGRDY);
- // TODO: use performance level 0?
-// _set_performance_level(0);
-// hri_pm_write_PLCFG_PLDIS_bit(PM, true);
-}
-
-static const uint8_t Character_Set[] =
-{
- 0b00000000, //
- 0b00000000, // !
- 0b00100010, // "
- 0b00000000, // #
- 0b00000000, // $
- 0b00000000, // %
- 0b01000100, // &
- 0b00100000, // '
- 0b00000000, // (
- 0b00000000, // )
- 0b00000000, // *
- 0b11000000, // +
- 0b00010000, // ,
- 0b01000000, // -
- 0b00000100, // .
- 0b00010010, // /
- 0b00111111, // 0
- 0b00000110, // 1
- 0b01011011, // 2
- 0b01001111, // 3
- 0b01100110, // 4
- 0b01101101, // 5
- 0b01111101, // 6
- 0b00000111, // 7
- 0b01111111, // 8
- 0b01101111, // 9
- 0b00000000, // :
- 0b00000000, // ;
- 0b01011000, // <
- 0b01001000, // =
- 0b01001100, // >
- 0b01010011, // ?
- 0b11111111, // @
- 0b01110111, // A
- 0b01111111, // B
- 0b00111001, // C
- 0b00111111, // D
- 0b01111001, // E
- 0b01110001, // F
- 0b00111101, // G
- 0b01110110, // H
- 0b10001001, // I
- 0b00001110, // J
- 0b11101010, // K
- 0b00111000, // L
- 0b10110111, // M
- 0b00110111, // N
- 0b00111111, // O
- 0b01110011, // P
- 0b01100111, // Q
- 0b11110111, // R
- 0b01101101, // S
- 0b10000001, // T
- 0b00111110, // U
- 0b00111110, // V
- 0b10111110, // W
- 0b01111110, // X
- 0b01101110, // Y
- 0b00011011, // Z
- 0b00111001, // [
- 0b00100100, // backslash
- 0b00001111, // ]
- 0b00100110, // ^
- 0b00001000, // _
- 0b00000010, // `
- 0b01011111, // a
- 0b01111100, // b
- 0b01011000, // c
- 0b01011110, // d
- 0b01111011, // e
- 0b01110001, // f
- 0b01101111, // g
- 0b01110100, // h
- 0b00010000, // i
- 0b01000010, // j
- 0b11101010, // k
- 0b00110000, // l
- 0b10110111, // m
- 0b01010100, // n
- 0b01011100, // o
- 0b01110011, // p
- 0b01100111, // q
- 0b01010000, // r
- 0b01101101, // s
- 0b01111000, // t
- 0b01100010, // u
- 0b01100010, // v
- 0b10111110, // w
- 0b01111110, // x
- 0b01101110, // y
- 0b00011011, // z
- 0b00111001, // {
- 0b00110000, // |
- 0b00001111, // }
- 0b00000001, // ~
-};
-
-static const uint64_t Segment_Map[] = {
- 0x4e4f0e8e8f8d4d0d, // Position 8
- 0xc8c4c4c8b4b4b0b, // Position 9
- 0xc049c00a49890949, // Position 6
- 0xc048088886874707, // Position 7
- 0xc053921252139352, // Position 0
- 0xc054511415559594, // Position 1
- 0xc057965616179716, // Position 2
- 0xc041804000018a81, // Position 3
- 0xc043420203048382, // Position 4
- 0xc045440506468584, // Position 5
-};
-
-static const uint8_t Num_Chars = 10;
-
-void watch_enable_display() {
- SEGMENT_LCD_0_init();
- slcd_sync_enable(&SEGMENT_LCD_0);
-}
-
-void watch_display_pixel(uint8_t com, uint8_t seg) {
- slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
-}
-
-void watch_clear_pixel(uint8_t com, uint8_t seg) {
- slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
-}
-
-void watch_display_character(uint8_t character, uint8_t position) {
- uint64_t segmap = Segment_Map[position];
- uint64_t segdata = Character_Set[character - 0x20];
-
- for (int i = 0; i < 8; i++) {
- uint8_t com = (segmap & 0xFF) >> 6;
- if (com > 2) {
- // COM3 means no segment exists; skip it.
- segmap = segmap >> 8;
- segdata = segdata >> 1;
- continue;
- }
- uint8_t seg = segmap & 0x3F;
- slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
- if (segdata & 1) slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
- segmap = segmap >> 8;
- segdata = segdata >> 1;
- }
-}
-
-void watch_display_string(char *string, uint8_t position) {
- size_t i = 0;
- while(string[i] != 0) {
- watch_display_character(string[i], position + i);
- i++;
- if (i >= Num_Chars) break;
- }
-}
-
-void watch_enable_buttons() {
- EXTERNAL_IRQ_0_init();
-}
-
-void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback) {
- ext_irq_register(pin, callback);
-}
-
-static bool PWM_0_enabled = false;
-
-void watch_enable_led() {
- if (!PWM_0_enabled) PWM_0_init();
- PWM_0_enabled = true;
- pwm_set_parameters(&PWM_0, 10000, 0);
- pwm_enable(&PWM_0);
-
- watch_set_led_off();
-}
-
-void watch_disable_led() {
- gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF);
- gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF);
-
- pwm_disable(&PWM_0);
- PWM_0_enabled = false;
-}
-
-void watch_set_led_color(uint16_t red, uint16_t green) {
- TC3->COUNT16.CC[0].reg = red;
- TC3->COUNT16.CC[1].reg = green;
-}
-
-void watch_set_led_red() {
- watch_set_led_color(65535, 0);
-}
-
-void watch_set_led_green() {
- watch_set_led_color(0, 65535);
-}
-
-void watch_set_led_off() {
- watch_set_led_color(0, 0);
-}
-
-void watch_enable_date_time() {
- CALENDAR_0_init();
- calendar_enable(&CALENDAR_0);
-}
-
-void watch_set_date_time(struct calendar_date_time date_time) {
- calendar_set_date(&CALENDAR_0, &date_time.date);
- calendar_set_time(&CALENDAR_0, &date_time.time);
-}
-
-void watch_get_date_time(struct calendar_date_time *date_time) {
- calendar_get_date_time(&CALENDAR_0, date_time);
-}
-
-static ext_irq_cb_t tick_user_callback;
-
-static void tick_callback(struct calendar_dev *const dev) {
- tick_user_callback();
-}
-
-void watch_enable_tick_callback(ext_irq_cb_t callback) {
- tick_user_callback = callback;
- // TODO: rename this method to reflect that it now sets the PER7 interrupt.
- _tamper_register_callback(&CALENDAR_0.device, &tick_callback);
-}
-
-static bool ADC_0_ENABLED = false;
-
-void watch_enable_analog(const uint8_t pin) {
- if (!ADC_0_ENABLED) ADC_0_init();
- ADC_0_ENABLED = true;
-
- gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
- switch (pin) {
- case A0:
- gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12);
- break;
- case A1:
- gpio_set_pin_function(A1, PINMUX_PB01B_ADC_AIN9);
- break;
- case A2:
- gpio_set_pin_function(A2, PINMUX_PB02B_ADC_AIN10);
- break;
- default:
- return;
- }
-}
-
-void watch_enable_digital_input(const uint8_t pin) {
- gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
- gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
-}
-
-void watch_enable_pull_up(const uint8_t pin) {
- gpio_set_pin_pull_mode(pin, GPIO_PULL_UP);
-}
-
-void watch_enable_pull_down(const uint8_t pin) {
- gpio_set_pin_pull_mode(pin, GPIO_PULL_DOWN);
-}
-
-bool watch_get_pin_level(const uint8_t pin, const bool level) {
- return gpio_get_pin_level(pin);
-}
-
-void watch_enable_digital_output(const uint8_t pin) {
- gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
- gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
-}
-
-void watch_set_pin_level(const uint8_t pin, const bool level) {
- gpio_set_pin_level(pin, level);
-}
-
-struct io_descriptor *I2C_0_io;
-
-void watch_enable_i2c() {
- I2C_0_init();
- i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io);
- i2c_m_sync_enable(&I2C_0);
-}
-
-void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length) {
- i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN);
- io_write(I2C_0_io, buf, length);
-}
-
-void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length) {
- i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN);
- io_read(I2C_0_io, buf, length);
-}
+/* + * watch.c + * + * Created: 4/25/2021 10:22:10 AM + * Author: joeycastillo + */ + +#include "watch.h" +#include <stdlib.h> + +void watch_init() { + // use switching regulator + SUPC->VREG.bit.SEL = 1; + while(!SUPC->STATUS.bit.VREGRDY); + + // External wake depends on RTC; calendar is a required module. + CALENDAR_0_init(); + calendar_enable(&CALENDAR_0); + + // TODO: use performance level 0? +// _set_performance_level(0); +// hri_pm_write_PLCFG_PLDIS_bit(PM, true); +} + +static const uint8_t Character_Set[] = +{ + 0b00000000, // + 0b00000000, // ! + 0b00100010, // " + 0b00000000, // # + 0b00000000, // $ + 0b00000000, // % + 0b01000100, // & + 0b00100000, // ' + 0b00000000, // ( + 0b00000000, // ) + 0b00000000, // * + 0b11000000, // + + 0b00010000, // , + 0b01000000, // - + 0b00000100, // . + 0b00010010, // / + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b00000000, // : + 0b00000000, // ; + 0b01011000, // < + 0b01001000, // = + 0b01001100, // > + 0b01010011, // ? + 0b11111111, // @ + 0b01110111, // A + 0b01111111, // B + 0b00111001, // C + 0b00111111, // D + 0b01111001, // E + 0b01110001, // F + 0b00111101, // G + 0b01110110, // H + 0b10001001, // I + 0b00001110, // J + 0b11101010, // K + 0b00111000, // L + 0b10110111, // M + 0b00110111, // N + 0b00111111, // O + 0b01110011, // P + 0b01100111, // Q + 0b11110111, // R + 0b01101101, // S + 0b10000001, // T + 0b00111110, // U + 0b00111110, // V + 0b10111110, // W + 0b01111110, // X + 0b01101110, // Y + 0b00011011, // Z + 0b00111001, // [ + 0b00100100, // backslash + 0b00001111, // ] + 0b00100110, // ^ + 0b00001000, // _ + 0b00000010, // ` + 0b01011111, // a + 0b01111100, // b + 0b01011000, // c + 0b01011110, // d + 0b01111011, // e + 0b01110001, // f + 0b01101111, // g + 0b01110100, // h + 0b00010000, // i + 0b01000010, // j + 0b11101010, // k + 0b00110000, // l + 0b10110111, // m + 0b01010100, // n + 0b01011100, // o + 0b01110011, // p + 0b01100111, // q + 0b01010000, // r + 0b01101101, // s + 0b01111000, // t + 0b01100010, // u + 0b01100010, // v + 0b10111110, // w + 0b01111110, // x + 0b01101110, // y + 0b00011011, // z + 0b00111001, // { + 0b00110000, // | + 0b00001111, // } + 0b00000001, // ~ +}; + +static const uint64_t Segment_Map[] = { + 0x4e4f0e8e8f8d4d0d, // Position 8 + 0xc8c4c4c8b4b4b0b, // Position 9 + 0xc049c00a49890949, // Position 6 + 0xc048088886874707, // Position 7 + 0xc053921252139352, // Position 0 + 0xc054511415559594, // Position 1 + 0xc057965616179716, // Position 2 + 0xc041804000018a81, // Position 3 + 0xc043420203048382, // Position 4 + 0xc045440506468584, // Position 5 +}; + +static const uint8_t Num_Chars = 10; + +void watch_enable_display() { + SEGMENT_LCD_0_init(); + slcd_sync_enable(&SEGMENT_LCD_0); +} + +void watch_display_pixel(uint8_t com, uint8_t seg) { + slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); +} + +void watch_clear_pixel(uint8_t com, uint8_t seg) { + slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); +} + +void watch_display_character(uint8_t character, uint8_t position) { + uint64_t segmap = Segment_Map[position]; + uint64_t segdata = Character_Set[character - 0x20]; + + for (int i = 0; i < 8; i++) { + uint8_t com = (segmap & 0xFF) >> 6; + if (com > 2) { + // COM3 means no segment exists; skip it. + segmap = segmap >> 8; + segdata = segdata >> 1; + continue; + } + uint8_t seg = segmap & 0x3F; + slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); + if (segdata & 1) slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); + segmap = segmap >> 8; + segdata = segdata >> 1; + } +} + +void watch_display_string(char *string, uint8_t position) { + size_t i = 0; + while(string[i] != 0) { + watch_display_character(string[i], position + i); + i++; + if (i >= Num_Chars) break; + } +} + +void watch_enable_buttons() { + EXTERNAL_IRQ_0_init(); +} + +void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback) { + ext_irq_register(pin, callback); +} + +bool PWM_0_enabled = false; + +void watch_enable_led(bool pwm) { + if (pwm) { + if (PWM_0_enabled) return; + + PWM_0_init(); + pwm_set_parameters(&PWM_0, 10000, 0); + pwm_enable(&PWM_0); + + PWM_0_enabled = true; + } else { + watch_enable_digital_output(RED); + watch_enable_digital_output(GREEN); + } + watch_set_led_off(); +} + +void watch_disable_led(bool pwm) { + if (pwm) { + if (!PWM_0_enabled) return; + pwm_disable(&PWM_0); + PWM_0_enabled = false; + } + + watch_disable_digital_output(RED); + watch_disable_digital_output(GREEN); +} + +void watch_set_led_color(uint16_t red, uint16_t green) { + if (PWM_0_enabled) { + TC3->COUNT16.CC[0].reg = red; + TC3->COUNT16.CC[1].reg = green; + } +} + +void watch_set_led_red() { + if (PWM_0_enabled) { + watch_set_led_color(65535, 0); + } else { + watch_set_pin_level(RED, true); + watch_set_pin_level(GREEN, false); + } +} + +void watch_set_led_green() { + if (PWM_0_enabled) { + watch_set_led_color(65535, 0); + } else { + watch_set_pin_level(RED, false); + watch_set_pin_level(GREEN, true); + } +} + +void watch_set_led_yellow() { + if (PWM_0_enabled) { + watch_set_led_color(65535, 65535); + } else { + watch_set_pin_level(RED, true); + watch_set_pin_level(GREEN, true); + } +} + +void watch_set_led_off() { + if (PWM_0_enabled) { + watch_set_led_color(0, 0); + } else { + watch_set_pin_level(RED, false); + watch_set_pin_level(GREEN, false); + } +} + +void watch_set_date_time(struct calendar_date_time date_time) { + calendar_set_date(&CALENDAR_0, &date_time.date); + calendar_set_time(&CALENDAR_0, &date_time.time); +} + +void watch_get_date_time(struct calendar_date_time *date_time) { + calendar_get_date_time(&CALENDAR_0, date_time); +} + +static ext_irq_cb_t tick_user_callback; + +static void tick_callback(struct calendar_dev *const dev) { + tick_user_callback(); +} + +void watch_enable_tick_callback(ext_irq_cb_t callback) { + tick_user_callback = callback; + // TODO: rename this method to reflect that it now sets the PER7 interrupt. + _tamper_register_callback(&CALENDAR_0.device, &tick_callback); +} + +static bool ADC_0_ENABLED = false; + +void watch_enable_analog(const uint8_t pin) { + if (!ADC_0_ENABLED) ADC_0_init(); + ADC_0_ENABLED = true; + + gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF); + switch (pin) { + case A0: + gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12); + break; + case A1: + gpio_set_pin_function(A1, PINMUX_PB01B_ADC_AIN9); + break; + case A2: + gpio_set_pin_function(A2, PINMUX_PB02B_ADC_AIN10); + break; + default: + return; + } +} + +void watch_enable_digital_input(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); + gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); +} + +void watch_enable_pull_up(const uint8_t pin) { + gpio_set_pin_pull_mode(pin, GPIO_PULL_UP); +} + +void watch_enable_pull_down(const uint8_t pin) { + gpio_set_pin_pull_mode(pin, GPIO_PULL_DOWN); +} + +bool watch_get_pin_level(const uint8_t pin, const bool level) { + return gpio_get_pin_level(pin); +} + +void watch_enable_digital_output(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); + gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); +} + +void watch_disable_digital_output(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF); +} + +void watch_set_pin_level(const uint8_t pin, const bool level) { + gpio_set_pin_level(pin, level); +} + +struct io_descriptor *I2C_0_io; + +void watch_enable_i2c() { + I2C_0_init(); + i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io); + i2c_m_sync_enable(&I2C_0); +} + +void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length) { + i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN); + io_write(I2C_0_io, buf, length); +} + +void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length) { + i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN); + io_read(I2C_0_io, buf, length); +} diff --git a/Sensor Watch Starter Project/hw/watch.h b/Sensor Watch Starter Project/watch/watch.h index be8d7936..f43ffa54 100644 --- a/Sensor Watch Starter Project/hw/watch.h +++ b/Sensor Watch Starter Project/watch/watch.h @@ -19,14 +19,13 @@ void watch_enable_display(); void watch_display_pixel(uint8_t com, uint8_t seg);
void watch_display_string(char *string, uint8_t position);
-void watch_enable_led();
-void watch_disable_led();
+void watch_enable_led(bool pwm);
+void watch_disable_led(bool pwm);
void watch_set_led_color(uint16_t red, uint16_t green);
void watch_set_led_red();
void watch_set_led_green();
void watch_set_led_off();
-void watch_enable_date_time();
void watch_set_date_time(struct calendar_date_time date_time);
void watch_get_date_time(struct calendar_date_time *date_time);
@@ -43,6 +42,7 @@ void watch_enable_pull_down(const uint8_t pin); bool watch_get_pin_level(const uint8_t pin, const bool level);
void watch_enable_digital_output(const uint8_t pin);
+void watch_disable_digital_output(const uint8_t pin);
void watch_set_pin_level(const uint8_t pin, const bool level);
struct io_descriptor *I2C_0_io;
|