Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"files.associations": {
"steering_io.h": "c"
"steering_io.h": "c",
"buttons.h": "c",
"stdlib.h": "c",
"string.h": "c"
}
}
22 changes: 2 additions & 20 deletions Core/Inc/steering_io.h → Core/Inc/buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
#define DEBOUNCE_BUTTON_ON 0 // 0 for off, 1 for on
30 changes: 30 additions & 0 deletions Core/Inc/dial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "main.h"
#include "can.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

/* 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);
22 changes: 22 additions & 0 deletions Core/Src/buttons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "buttons.h"
#include <stdlib.h>

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);
}
41 changes: 41 additions & 0 deletions Core/Src/dial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "dial.h"
#include <stdlib.h>

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, &current_switch, 1);
can_send_msg(can, &can_msg);
printf("CURRENT DIAL STATUS: %d\n", current_switch);
}
30 changes: 8 additions & 22 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include "can.h"
#include <stdlib.h>
#include <string.h>
#include "steering_io.c"
#include "buttons.c"
#include "dial.c"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 0 additions & 42 deletions Core/Src/steering_io.c

This file was deleted.