From f337ec80c0393ad6ea6cd695d42923ff5a2e3aa8 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Wed, 9 Apr 2025 22:34:39 -0400 Subject: [PATCH 1/5] Dial constant read --- Core/Inc/steering_io.h | 2 +- Core/Src/main.c | 3 +++ Core/Src/steering_io.c | 33 ++++++++++++++++++--------------- Drivers/Embedded-Base | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Core/Inc/steering_io.h b/Core/Inc/steering_io.h index f053ff8..d066a0b 100644 --- a/Core/Inc/steering_io.h +++ b/Core/Inc/steering_io.h @@ -44,5 +44,5 @@ void dial_switched(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t switch_id, /* Debounce Config */ #define DEBOUNCE_BUTTON_TIME 8 // unit is ms #define DEBOUNCE_BUTTON_ON 0 // 0 for off, 1 for on -#define DEBOUNCE_DIAL_TIME 8 // unit is ms +#define DEBOUNCE_DIAL_TIME 50 // unit is ms #define DEBOUNCE_DIAL_ON 0 // 0 for off, 1 for on \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 6bca88d..b938321 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -36,6 +36,7 @@ /* USER CODE BEGIN PD */ volatile uint8_t flag; volatile uint16_t gpio_pin; +volatile int switch_state = -1; /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -136,6 +137,8 @@ int main(void) if(flag) { flag = 0; determine_action(gpio_pin); + switch_state = which_pin(); + printf("CURRENT SWITCH STATE: %d",switch_state); } /* USER CODE END WHILE */ diff --git a/Core/Src/steering_io.c b/Core/Src/steering_io.c index a3245b5..43289ec 100644 --- a/Core/Src/steering_io.c +++ b/Core/Src/steering_io.c @@ -1,6 +1,8 @@ #include "steering_io.h" #include +extern volatile int switch_state; + void button_pressed(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t button_id, can_t *can) { @@ -21,22 +23,23 @@ void button_pressed(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t button_id, printf("Button %d pressed\n", button_id + 1); } +int which_pin() +{ + /* Check if the pin is pressed */ + if (HAL_GPIO_ReadPin(GPIOB, Switch_1_Pin)) return 1; // Switch 1 + if (HAL_GPIO_ReadPin(GPIOB, Switch_2_Pin)) return 2; // Switch 2 + if (HAL_GPIO_ReadPin(GPIOB, Switch_3_Pin)) return 3; // Switch 3 + if (HAL_GPIO_ReadPin(GPIOB, Switch_4_Pin)) return 4; // Switch 4 + if (HAL_GPIO_ReadPin(GPIOB, Switch_5_Pin)) return 5; // Switch 5 + return -1; +} + void dial_switched(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t switch_id, can_t *can) { - /* Debounce Logic */ - if (DEBOUNCE_DIAL_ON) { - HAL_Delay(DEBOUNCE_DIAL_TIME); - if (HAL_GPIO_ReadPin(port, GPIO_Pin) == GPIO_PIN_RESET) { - printf("Failed to read the pin for dial switch %d when doing debounce check.\n", - switch_id); - return; - } - } - /* Send CAN Message */ - can_msg_t can_msg = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO }; - memcpy(&can_msg.data, &switch_id, 1); - can_send_msg(can, &can_msg); - printf("Dial switched to id %d\n", switch_id + 1); -} \ No newline at end of file + //can_msg_t can_msg = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO }; + //memcpy(&can_msg.data, &pin, 1); + //can_send_msg(can, &can_msg); + //printf("Dial switched to id %d\n", pin + 1); +} diff --git a/Drivers/Embedded-Base b/Drivers/Embedded-Base index 3b7ab39..2504f98 160000 --- a/Drivers/Embedded-Base +++ b/Drivers/Embedded-Base @@ -1 +1 @@ -Subproject commit 3b7ab39c7695fbc72a871600aa4cbfe7312d3682 +Subproject commit 2504f985588d1f6305381a671a68b484d00a6aa9 From 7e3a721e684cac84e2752c2403b775c275b573b8 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Wed, 9 Apr 2025 22:40:40 -0400 Subject: [PATCH 2/5] thing often --- Core/Src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index b938321..8033df6 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -137,9 +137,9 @@ int main(void) if(flag) { flag = 0; determine_action(gpio_pin); - switch_state = which_pin(); - printf("CURRENT SWITCH STATE: %d",switch_state); } + switch_state = which_pin(); + printf("CURRENT SWITCH STATE: %d",switch_state); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ From 18b027fa628ec379e32f3249b23bbf3002f517e9 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Wed, 9 Apr 2025 22:44:00 -0400 Subject: [PATCH 3/5] Print --- Core/Src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 8033df6..0f17818 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -139,7 +139,7 @@ int main(void) determine_action(gpio_pin); } switch_state = which_pin(); - printf("CURRENT SWITCH STATE: %d",switch_state); + printf("CURRENT SWITCH STATE: %d \n",switch_state); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ From 2de9bd3fb2972c730be1f51588acb83367c457a1 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Thu, 10 Apr 2025 11:00:01 -0400 Subject: [PATCH 4/5] Constantly check dial setting (instead of interrupts) --- .vscode/settings.json | 5 ++- Core/Inc/{steering_io.h => buttons.h} | 22 ++----------- Core/Inc/dial.h | 30 ++++++++++++++++++ Core/Src/buttons.c | 22 +++++++++++++ Core/Src/dial.c | 34 ++++++++++++++++++++ Core/Src/main.c | 33 +++++--------------- Core/Src/steering_io.c | 45 --------------------------- 7 files changed, 100 insertions(+), 91 deletions(-) rename Core/Inc/{steering_io.h => buttons.h} (51%) create mode 100644 Core/Inc/dial.h create mode 100644 Core/Src/buttons.c create mode 100644 Core/Src/dial.c delete mode 100644 Core/Src/steering_io.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 27198b0..d2d7145 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "files.associations": { - "steering_io.h": "c" + "steering_io.h": "c", + "buttons.h": "c", + "stdlib.h": "c", + "string.h": "c" } } \ No newline at end of file diff --git a/Core/Inc/steering_io.h b/Core/Inc/buttons.h similarity index 51% rename from Core/Inc/steering_io.h rename to Core/Inc/buttons.h index d066a0b..b5a9684 100644 --- a/Core/Inc/steering_io.h +++ b/Core/Inc/buttons.h @@ -16,33 +16,15 @@ typedef enum { MAX_BUTTON_SIZE, } steeringio_button_t; -typedef enum { - DIAL_SWITCH_1, - DIAL_SWITCH_2, - DIAL_SWITCH_3, - DIAL_SWITCH_4, - DIAL_SWITCH_5, - MAX_DIAL_SIZE, -} steeringio_dial_t; - /** * @brief Called when a button interrupt is triggered. Sends a CAN message with the button's id. */ void button_pressed(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t button_id, can_t *can); -/** - * @brief Called when a dial switch interrupt is triggered. Sends a CAN message with the dial switch's id. - */ -void dial_switched(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t switch_id, - can_t *can); - -/* CAN IDs */ +/* CAN ID */ #define BUTTON_CANID_IO 0x680 -#define DIAL_CANID_IO 0x681 /* Debounce Config */ #define DEBOUNCE_BUTTON_TIME 8 // unit is ms -#define DEBOUNCE_BUTTON_ON 0 // 0 for off, 1 for on -#define DEBOUNCE_DIAL_TIME 50 // unit is ms -#define DEBOUNCE_DIAL_ON 0 // 0 for off, 1 for on \ No newline at end of file +#define DEBOUNCE_BUTTON_ON 0 // 0 for off, 1 for on \ No newline at end of file diff --git a/Core/Inc/dial.h b/Core/Inc/dial.h new file mode 100644 index 0000000..7ae0b45 --- /dev/null +++ b/Core/Inc/dial.h @@ -0,0 +1,30 @@ +#include "main.h" +#include "can.h" +#include +#include +#include +#include + +/* DIAL CONFIG */ +#define DIAL_FREQUENCY 100 // How often to check the dial status (unit is ms) + +/* CAN ID */ +#define DIAL_CANID_IO 0x681 + +/* +* @brief Check if the dial timer has expired. +* @return true if the timer has expired, false otherwise. +*/ +bool dial_timer(); + +/* +* @brief Check which dial switch is currently active. +* @return The active switch number (1-5) or -1 if none are active. +*/ +int8_t which_switch(); + +/* +* @brief Check the status of the dial and send a CAN message with the current dial setting. +* @param can Pointer to the CAN interface structure. +*/ +void check_dial_status(can_t *can); \ No newline at end of file diff --git a/Core/Src/buttons.c b/Core/Src/buttons.c new file mode 100644 index 0000000..55f2c71 --- /dev/null +++ b/Core/Src/buttons.c @@ -0,0 +1,22 @@ +#include "buttons.h" +#include + +void button_pressed(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t button_id, + can_t *can) +{ + /* Debounce Logic */ + if (DEBOUNCE_BUTTON_ON) { + HAL_Delay(DEBOUNCE_BUTTON_TIME); + if (HAL_GPIO_ReadPin(port, GPIO_Pin) == GPIO_PIN_RESET) { + printf("Failed to read the pin for button %d when doing debounce check.\n", + button_id); + return; + } + } + + /* Send CAN Message */ + can_msg_t can_msg = { .len = sizeof(uint8_t), .id = BUTTON_CANID_IO }; + memcpy(&can_msg.data, &button_id, 1); + can_send_msg(can, &can_msg); + printf("Button %d pressed\n", button_id + 1); +} \ No newline at end of file diff --git a/Core/Src/dial.c b/Core/Src/dial.c new file mode 100644 index 0000000..c721f33 --- /dev/null +++ b/Core/Src/dial.c @@ -0,0 +1,34 @@ +#include "dial.h" +#include + +bool dial_timer() { + static uint32_t last_time = 0; + /* Check if the timer has expired */ + if (HAL_GetTick() - last_time >= DIAL_FREQUENCY) { + last_time = HAL_GetTick(); // Update the last time + return true; // Timer expired + } + return false; // Timer not expired +} + +int8_t which_switch() +{ + /* Check which dial pin is currently active */ + if (HAL_GPIO_ReadPin(Switch_1_GPIO_Port, Switch_1_Pin)) return 1; // Switch 1 + if (HAL_GPIO_ReadPin(Switch_2_GPIO_Port, Switch_2_Pin)) return 2; // Switch 2 + if (HAL_GPIO_ReadPin(Switch_3_GPIO_Port, Switch_3_Pin)) return 3; // Switch 3 + if (HAL_GPIO_ReadPin(Switch_4_GPIO_Port, Switch_4_Pin)) return 4; // Switch 4 + if (HAL_GPIO_ReadPin(Switch_5_GPIO_Port, Switch_5_Pin)) return 5; // Switch 5 + return -1; // None active?? should not be possible unless hardware wrong +} + +void check_dial_status(can_t *can) { + + int8_t current_switch = which_switch(); // Check which switch is active (i.e. which dial setting is selected) + + /* Send CAN Message */ + can_msg_t can_msg = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO }; + memcpy(&can_msg.data, ¤t_switch, 1); + can_send_msg(can, &can_msg); + printf("CURRENT DIAL STATUS: %d\n", current_switch); +} \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 0f17818..8ae0712 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -24,7 +24,8 @@ #include "can.h" #include #include -#include "steering_io.c" +#include "buttons.c" +#include "dial.c" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -36,7 +37,6 @@ /* USER CODE BEGIN PD */ volatile uint8_t flag; volatile uint16_t gpio_pin; -volatile int switch_state = -1; /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -128,6 +128,8 @@ int main(void) can->hcan = &hcan1; can_init(can); + check_dial_status(can); // Check what setting the dial is set to on startup + /* USER CODE END 2 */ /* Infinite loop */ @@ -138,8 +140,10 @@ int main(void) flag = 0; determine_action(gpio_pin); } - switch_state = which_pin(); - printf("CURRENT SWITCH STATE: %d \n",switch_state); + + if(dial_timer()) { + check_dial_status(can); // Every DIAL_FREQUENCY ms (defined in dial.h), check the dial status and send it over CAN. + } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ @@ -331,7 +335,6 @@ static void MX_GPIO_Init(void) /* USER CODE BEGIN 4 */ void determine_action(uint16_t GPIO_Pin) { uint8_t button_id; - uint8_t dial_id; switch (GPIO_Pin) { case Button_1_Pin: button_id = BUTTON_LEFT; @@ -360,26 +363,6 @@ void determine_action(uint16_t GPIO_Pin) { case Button_Spare_Pin: button_id = SPARE_BUTTON; button_pressed(Button_Spare_GPIO_Port, GPIO_Pin, button_id, can); - break; - case Switch_1_Pin: - dial_id = DIAL_SWITCH_1; - dial_switched(Switch_1_GPIO_Port, GPIO_Pin, dial_id, can); - break; - case Switch_2_Pin: - dial_id = DIAL_SWITCH_2; - dial_switched(Switch_2_GPIO_Port, GPIO_Pin, dial_id, can); - break; - case Switch_3_Pin: - dial_id = DIAL_SWITCH_3; - dial_switched(Switch_3_GPIO_Port, GPIO_Pin, dial_id, can); - break; - case Switch_4_Pin: - dial_id = DIAL_SWITCH_4; - dial_switched(Switch_4_GPIO_Port, GPIO_Pin, dial_id, can); - break; - case Switch_5_Pin: - dial_id = DIAL_SWITCH_5; - dial_switched(Switch_5_GPIO_Port, GPIO_Pin, dial_id, can); break; default: break; diff --git a/Core/Src/steering_io.c b/Core/Src/steering_io.c deleted file mode 100644 index 43289ec..0000000 --- a/Core/Src/steering_io.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "steering_io.h" -#include - -extern volatile int switch_state; - -void button_pressed(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t button_id, - can_t *can) -{ - /* Debounce Logic */ - if (DEBOUNCE_BUTTON_ON) { - HAL_Delay(DEBOUNCE_BUTTON_TIME); - if (HAL_GPIO_ReadPin(port, GPIO_Pin) == GPIO_PIN_RESET) { - printf("Failed to read the pin for button %d when doing debounce check.\n", - button_id); - return; - } - } - - /* Send CAN Message */ - can_msg_t can_msg = { .len = sizeof(uint8_t), .id = BUTTON_CANID_IO }; - memcpy(&can_msg.data, &button_id, 1); - can_send_msg(can, &can_msg); - printf("Button %d pressed\n", button_id + 1); -} - -int which_pin() -{ - /* Check if the pin is pressed */ - if (HAL_GPIO_ReadPin(GPIOB, Switch_1_Pin)) return 1; // Switch 1 - if (HAL_GPIO_ReadPin(GPIOB, Switch_2_Pin)) return 2; // Switch 2 - if (HAL_GPIO_ReadPin(GPIOB, Switch_3_Pin)) return 3; // Switch 3 - if (HAL_GPIO_ReadPin(GPIOB, Switch_4_Pin)) return 4; // Switch 4 - if (HAL_GPIO_ReadPin(GPIOB, Switch_5_Pin)) return 5; // Switch 5 - return -1; -} - -void dial_switched(GPIO_TypeDef *port, uint16_t GPIO_Pin, uint8_t switch_id, - can_t *can) -{ - /* Send CAN Message */ - //can_msg_t can_msg = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO }; - //memcpy(&can_msg.data, &pin, 1); - //can_send_msg(can, &can_msg); - //printf("Dial switched to id %d\n", pin + 1); -} From 55c937f8db4a6083534dae809cf0bc00b0bb13cc Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Thu, 10 Apr 2025 11:02:58 -0400 Subject: [PATCH 5/5] c lang format --- Core/Src/dial.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Core/Src/dial.c b/Core/Src/dial.c index c721f33..f6b85e4 100644 --- a/Core/Src/dial.c +++ b/Core/Src/dial.c @@ -1,30 +1,37 @@ #include "dial.h" #include -bool dial_timer() { - static uint32_t last_time = 0; - /* Check if the timer has expired */ - if (HAL_GetTick() - last_time >= DIAL_FREQUENCY) { - last_time = HAL_GetTick(); // Update the last time - return true; // Timer expired - } - return false; // Timer not expired +bool dial_timer() +{ + static uint32_t last_time = 0; + /* Check if the timer has expired */ + if (HAL_GetTick() - last_time >= DIAL_FREQUENCY) { + last_time = HAL_GetTick(); // Update the last time + return true; // Timer expired + } + return false; // Timer not expired } int8_t which_switch() { /* Check which dial pin is currently active */ - if (HAL_GPIO_ReadPin(Switch_1_GPIO_Port, Switch_1_Pin)) return 1; // Switch 1 - if (HAL_GPIO_ReadPin(Switch_2_GPIO_Port, Switch_2_Pin)) return 2; // Switch 2 - if (HAL_GPIO_ReadPin(Switch_3_GPIO_Port, Switch_3_Pin)) return 3; // Switch 3 - if (HAL_GPIO_ReadPin(Switch_4_GPIO_Port, Switch_4_Pin)) return 4; // Switch 4 - if (HAL_GPIO_ReadPin(Switch_5_GPIO_Port, Switch_5_Pin)) return 5; // Switch 5 + if (HAL_GPIO_ReadPin(Switch_1_GPIO_Port, Switch_1_Pin)) + return 1; // Switch 1 + if (HAL_GPIO_ReadPin(Switch_2_GPIO_Port, Switch_2_Pin)) + return 2; // Switch 2 + if (HAL_GPIO_ReadPin(Switch_3_GPIO_Port, Switch_3_Pin)) + return 3; // Switch 3 + if (HAL_GPIO_ReadPin(Switch_4_GPIO_Port, Switch_4_Pin)) + return 4; // Switch 4 + if (HAL_GPIO_ReadPin(Switch_5_GPIO_Port, Switch_5_Pin)) + return 5; // Switch 5 return -1; // None active?? should not be possible unless hardware wrong } -void check_dial_status(can_t *can) { - - int8_t current_switch = which_switch(); // Check which switch is active (i.e. which dial setting is selected) +void check_dial_status(can_t *can) +{ + int8_t current_switch = + which_switch(); // Check which switch is active (i.e. which dial setting is selected) /* Send CAN Message */ can_msg_t can_msg = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO };