refactor Functions to group everything connected to the vehicle to a central location

This commit is contained in:
2024-05-28 03:52:03 +02:00
parent 9c489c1a25
commit 766d5e9ca8
14 changed files with 274 additions and 186 deletions

View File

@@ -11,6 +11,7 @@
#include "ulog.h"
#include "BSP_POWER.h"
#include "BSP_GPIO.h"
#include "Vehicle.h"
// Define thread flags
#define FLAG_FDCAN_RX_FIFO0 (1<<0)
#define FLAG_FDCAN_RX_FIFO1 (1<<1)
@@ -46,7 +47,7 @@ const osThreadAttr_t CarCanTask_attr = {
.priority = osPriorityNormal,
};
static uint64_t last_car_message_time = 0;
uint32_t dlcDecode(uint32_t dlcCode) {
switch(dlcCode) {
@@ -130,11 +131,6 @@ void CanDataTask_func(void *argument) {
}
}
static uint64_t last_unlock_message_time = UINT64_MAX;
static uint64_t last_car_message_time = 0;
static uint8_t car_can_brightness = 255;
static float car_can_speed = 0;
static int car_can_direction = 0;
// convert byte to 2 hex characters
void byteToHex(uint8_t byte, char * hex) {
@@ -152,20 +148,8 @@ void CarCanTask_func(void *argument) {
}
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = CLS_BSP_CAN_FILTER_LIST;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO1;
sFilterConfig.FilterID1 = 0x391;
sFilterConfig.FilterID2 = 0x395;
HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig);
sFilterConfig.FilterIndex = 1;
sFilterConfig.FilterID1 = 0x351;
sFilterConfig.FilterID2 = 0x635;
HAL_FDCAN_ConfigFilter(&hfdcan2, &sFilterConfig);
Vehicle_Setup_CAN();
/* Start the FDCAN module */
if (HAL_FDCAN_Start(&hfdcan2) != HAL_OK){
@@ -263,69 +247,7 @@ void CarCanTask_func(void *argument) {
} else {
char msg[17] = {0};
// do something with the can data
if(RxHeader.Identifier == 0x391) {
byteToHex(RxData[0], &msg[0]);
byteToHex(RxData[1], &msg[2]);
byteToHex(RxData[2], &msg[4]);
if (RxData[1] == 0x04)
{
// car was unlocked
last_unlock_message_time = osKernelGetTickCount();
}
else if (RxData[1] ==0x80)
{
// car was locked
if (!BSP_GPIO_K15isSet()) {
NVIC_SystemReset();
}
}
}
if (RxHeader.Identifier == 0x395) {
byteToHex(RxData[0], &msg[0]);
// send the unlock message to the car
if ((RxData[0] & 0x0F) == 0x01) {
// car was unlocked
last_unlock_message_time = osKernelGetTickCount();
ULOG_DEBUG("Unlock message received");
} else if ((RxData[0] & 0x0F) == 0x02) {
// car was locked
if (!BSP_GPIO_K15isSet()) {
NVIC_SystemReset();
}
}
}
// speed signal
// AA BB XX YY 00 00 00 00
// Speed (XX*(2^8)+(YY-1))/190
// direction AA = 0x00 forward, 0x02 backward
if (RxHeader.Identifier == 0x351) {
uint16_t speed = (RxData[2] << 8) + RxData[3];
float speed_kmh = (speed - 1) / 190.0;
car_can_speed = speed_kmh;
car_can_direction = RxData[0];
ULOG_DEBUG("Speed: %f, Direction: %d", car_can_speed, car_can_direction);
}
// brightness knob in 0 - 100
if (RxHeader.Identifier == 0x635) {
// scale the brightness to 0 - 255 only using integer math
car_can_brightness = ((uint32_t)RxData[0] * 255) / 100;
ULOG_DEBUG("Brightness: %d", car_can_brightness);
}
Vehicle_Receive_CAN(RxHeader, RxData);
//ULOG_DEBUG("Used Car MSG: %x, %d %s", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength), msg );
}
@@ -339,9 +261,6 @@ void CarCanTask_func(void *argument) {
* @return true if a car can message has been received
* @return false if no car can message has been received
*/
bool CanDataTask_gotCarCanMessage() {
return last_unlock_message_time != UINT64_MAX;
}
bool CanDataTask_CarCanActive() {
@@ -350,20 +269,9 @@ bool CanDataTask_CarCanActive() {
}
return osKernelGetTickCount() - last_car_message_time < 1000;
}
uint8_t CanDataTask_CarCanBrightness() {
return car_can_brightness;
}
float CanDataTask_CarCanSpeed() {
return car_can_speed;
}
int CanDataTask_CarCanDirectionIsForward() {
return car_can_direction == 0;
}
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
// Notify the thread