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 f053ff8..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 8 // 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..f6b85e4 --- /dev/null +++ b/Core/Src/dial.c @@ -0,0 +1,41 @@ +#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 6bca88d..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 -----------------------------------------------------------*/ @@ -127,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 */ @@ -137,6 +140,10 @@ int main(void) flag = 0; determine_action(gpio_pin); } + + 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 */ @@ -328,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; @@ -357,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 a3245b5..0000000 --- a/Core/Src/steering_io.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "steering_io.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); -} - -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 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