# HG changeset patch # User matthewreed # Date 2017-05-26 22:16:53 # Node ID 021012fc2f310b2574f012b59bef0c9e3198ddbb # Parent eaddb578c329d868ace58bdad741b1c95234b688 Updated protocol features including config and fixed CAN issue. diff --git a/inc/led.h b/inc/led.h --- a/inc/led.h +++ b/inc/led.h @@ -12,13 +12,16 @@ typedef enum { } led_name_t; void led_init(void); -void led_start(led_name_t led); -void led_start_time(led_name_t led, uint16_t time); +void led_blink(led_name_t led, uint16_t period); +void led_stop(led_name_t led); +void led_blink_once(led_name_t led, uint16_t period); void led_update(led_name_t led); void led_update_all(void); void led_set(led_name_t led, bool value); void led_toggle(led_name_t led); +bool led_set_brightness(uint8_t brightness); +bool gpio_set_led_brightness(uint8_t brightness); bool gpio_set_led(led_name_t led, bool value); bool gpio_toggle_led(led_name_t led); diff --git a/inc/protocol.h b/inc/protocol.h --- a/inc/protocol.h +++ b/inc/protocol.h @@ -60,11 +60,13 @@ typedef enum { } protocol_data_key_t; void protocol_init(protocol_device_t device); +flash_settings_t* protocol_get_settings(void); bool protocol_receive_message(CanRxMsgTypeDef* can_message); bool protocol_send_message(protocol_message_t* message); bool protocol_process_message(protocol_message_t* message); bool protocol_send_data(protocol_data_key_t key, uint8_t sensor, float data); bool protocol_send_test(); +bool _protocol_config(protocol_message_t* message); bool protocol_estop(bool value); bool protocol_set_output(protocol_message_t* message); diff --git a/src/can.c b/src/can.c --- a/src/can.c +++ b/src/can.c @@ -125,9 +125,10 @@ void HAL_CAN_RxCpltCallback(CAN_HandleTy if ((hcan->pRxMsg->StdId == can_rx_id) | (hcan->pRxMsg->StdId == can_broadcast_id)) { protocol_receive_message(hcan->pRxMsg); - HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0); } - led_start_time(LED_CAN, 500); + + led_blink_once(LED_CAN, 500); + HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0); } void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan) {} diff --git a/src/flash.c b/src/flash.c --- a/src/flash.c +++ b/src/flash.c @@ -39,6 +39,8 @@ void flash_load_defaults(flash_settings_ static void __flash_write(flash_settings_t* tosave) { + //TODO: Check to see if anything has changed before saving + // Erase mem HAL_FLASH_Unlock(); diff --git a/src/led.c b/src/led.c --- a/src/led.c +++ b/src/led.c @@ -1,36 +1,37 @@ #include "led.h" -#define LED_CYCLE_STATUS 2000 -#define LED_CYCLE_CAN 0 -#define LED_CYCLE_ERROR 0 - uint16_t led_timers[3]; uint16_t led_thresholds[3]; uint16_t led_cycles[3]; void led_init(void) { - memset(led_timers, 0, sizeof led_timers); - memset(led_thresholds, 0, sizeof led_thresholds); - memset(led_cycles, 0, sizeof led_cycles); - - led_cycles[LED_STATUS] = LED_CYCLE_STATUS; - led_cycles[LED_CAN] = LED_CYCLE_CAN; - led_cycles[LED_ERROR] = LED_CYCLE_ERROR; + memset(led_timers, 0, sizeof(led_timers)); + memset(led_thresholds, 0, sizeof(led_thresholds)); + memset(led_cycles, 0, sizeof(led_cycles)); } -void led_start(led_name_t led) +void led_blink(led_name_t led, uint16_t period) { - led_timers[led] = led_cycles[led]; - led_thresholds[led] = led_cycles[led]/2; + led_timers[led] = period; + led_thresholds[led] = period/2; + led_cycles[led] = period; } -void led_start_time(led_name_t led, uint16_t time) +void led_stop(led_name_t led) +{ + led_timers[led] = 0; + led_thresholds[led] = 0; + led_cycles[led] = 0; + led_set(led, 0); +} + +void led_blink_once(led_name_t led, uint16_t period) { if (led_timers[led] == 0) { - led_timers[led] = time; - led_thresholds[led] = time/2; + led_timers[led] = period; + led_thresholds[led] = period/2; } } @@ -70,6 +71,11 @@ void led_toggle(led_name_t led) gpio_toggle_led(led); } +bool led_set_brightness(uint8_t brightness) +{ + return gpio_set_led_brightness(brightness); +} + __weak bool gpio_set_led(led_name_t led, bool value) { return false; @@ -80,3 +86,8 @@ void led_toggle(led_name_t led) return false; } +__weak bool gpio_set_led_brightness(uint8_t brightness) +{ + return false; +} + diff --git a/src/protocol.c b/src/protocol.c --- a/src/protocol.c +++ b/src/protocol.c @@ -11,6 +11,11 @@ void protocol_init(protocol_device_t dev can_init(protocol_settings.val.can_id, DEFAULT_BROADCAST_ID); } +flash_settings_t* protocol_get_settings(void) +{ + return &protocol_settings; +} + bool protocol_send_test() { bool result = true; @@ -90,8 +95,7 @@ bool protocol_process_message(protocol_m break; case CONFIG: { - //call config weak function - result = protocol_config(message); + result = _protocol_config(message); } break; default: @@ -119,6 +123,29 @@ bool protocol_send_data(protocol_data_ke return result; } +bool _protocol_config(protocol_message_t* message) +{ + bool result = false; + + if (message->key == LED_BRIGHTNESS) + { + uint8_t brightness = (uint8_t)message->data.float_data; + result = led_set_brightness(brightness); + protocol_settings.val.led_brightness = brightness; + } + else if (message->key == DATA_RATE) + { + uint16_t data_rate = (uint16_t) message->data.float_data; + protocol_settings.val.data_rate = data_rate; + } + else + { + //call config weak function + result = protocol_config(message); + } + return result; +} + __weak bool protocol_estop(bool value) { return false;