Skip to content

Commit

Permalink
Add CAN logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremycote committed Sep 24, 2023
1 parent b10b10d commit e0d4e05
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 31 deletions.
36 changes: 21 additions & 15 deletions UOSM-Dashboard/Core/Modules/CANCallbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,47 @@ void MotorRPMDataCallback(iCommsMessage_t* msg) {
velocity_t rpm = readMsg(msg);
DebugPrint("CAN rpm received: %d", rpm);
SetMotorRPM(aggregatorWrapper, rpm * -1);
}

void VoltageDataCallback(iCommsMessage_t* msg) {
if (!CAN_Enabled()) {
return;
}

uint32_t voltage = readMsg(msg);
DebugPrint("CAN voltage received: %d", voltage);
SetBatteryVoltage(aggregatorWrapper, voltage);
LogCanMessage(aggregatorWrapper, MOTOR_RPM_DATA_ID, rpm, CAN_DECIMAL);
}

void ThrottleDataCallback(iCommsMessage_t* msg) {
// DebugPrint("ThrottleDataCallback not implemented! %d", msg->standardMessageID);
if (!CAN_Enabled()) {
return;
}

percentage_t throttle = readMsg(msg);
DebugPrint("CAN throttle received: %d", throttle);
SetThrottlePosition(aggregatorWrapper, throttle);
LogCanMessage(aggregatorWrapper, THROTTLE_DATA_ID, throttle, CAN_DECIMAL);
}

void ErrorDataCallback(iCommsMessage_t* msg) {
DebugPrint("ErrorDataCallback not implemented! %d", msg->standardMessageID);
if (msg->dataLength == CANMessageLookUpTable[ERROR_DATA_ID].numberOfBytes) {
ErrorCode code = msg->data[1];
flag_status_t status = msg->data[0];
LogCanMessagePairValue(aggregatorWrapper, ERROR_DATA_ID, code, status, CAN_DECIMAL);
} else {
DebugPrint("Received corrupted error CAN message.");
}
}

void SpeedDataCallback(iCommsMessage_t* msg) {
DebugPrint("SpeedDataCallback not implemented! %d", msg->standardMessageID);
speed_t speed = readMsg(msg);
LogCanMessage(aggregatorWrapper, SPEED_DATA_ID, speed, CAN_DECIMAL);
}

void EventDataCallback(iCommsMessage_t* msg) {
DebugPrint("EventDataCallback not implemented! %d", msg->standardMessageID);
if (msg->dataLength == CANMessageLookUpTable[EVENT_DATA_ID].numberOfBytes) {
EventCode code = (EventCode) msg->data[1];
flag_status_t status = (flag_status_t) msg->data[0];
LogCanMessagePairValue(aggregatorWrapper, EVENT_DATA_ID, code, status, CAN_DECIMAL);
} else {
DebugPrint("Received corrupted event CAN message.");
}
}

void CurrentVoltageDataCallback(iCommsMessage_t* msg) {
DebugPrint("CurrentVoltageDataCallback not implemented! %d", msg->standardMessageID);
uint16_pair_t pair = readMsgPairUInt16Bit(msg);
SetBatteryVoltage(aggregatorWrapper, pair.b);
LogCanMessagePairValue(aggregatorWrapper, CURRENT_VOLTAGE_DATA_ID, pair.a, pair.b, CAN_DECIMAL);
}
2 changes: 1 addition & 1 deletion UOSM-Dashboard/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(void) {
tft_init();
touchpad_init();

DataAggregatorWrapper* wrapper = DataAggregator_Create(10, 10, 10, 10);
DataAggregatorWrapper* wrapper = DataAggregator_Create(10, 10, 10, 10, 10, 10);
CAN_SetAggregator(wrapper);
Application_Create(wrapper);

Expand Down
4 changes: 4 additions & 0 deletions UOSM-Dashboard/Core/UI/Data/DataAggregatorWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void LogCanMessage(DataAggregatorWrapper* wrapper, ICommsMessageLookUpIndex type
wrapper->aggregator.canLogEntries.add(new CANLogEntry(type, value, style));
}

void LogCanMessagePairValue(DataAggregatorWrapper* wrapper, ICommsMessageLookUpIndex type, uint32_t a, uint32_t b, CANLogEntryFormat style) {
wrapper->aggregator.canLogEntries.add(new CANLogEntry(type, a, b, style));
}

DataAggregator& DataAggregator_GetReference(DataAggregatorWrapper* wrapper) {
return wrapper->aggregator;
}
13 changes: 13 additions & 0 deletions UOSM-Dashboard/Core/UI/Data/DataAggregatorWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ApplicationTypes.h"
#include "DataAggregatorWrapperType.h"
#include "CANMessageLookUpModule.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -46,6 +47,18 @@ void SetLapTime(DataAggregatorWrapper* wrapper, ms_t time);
*/
void SetThrottlePosition(DataAggregatorWrapper* wrapper, percentage_t throttle);

/** @ingroup core-modules
* Log a new can message in the data aggregator object from a given wrapper.
* @param wrapper The pointer to the wrapper that contains the data aggregator object.
* @param type The type of CAN message
* @param value The value to log
* @param style How to display the value in the logs
*/
void LogCanMessage(DataAggregatorWrapper* wrapper, ICommsMessageLookUpIndex type, uint32_t value, CANLogEntryFormat style);

void LogCanMessagePairValue(DataAggregatorWrapper* wrapper, ICommsMessageLookUpIndex type, uint32_t a, uint32_t b, CANLogEntryFormat style);


#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions UOSM-Dashboard/Core/UI/HomeView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/** @ingroup core-ui
* A class that represents a view that displays the home screen of the dashboard.
* It inherits from the View class and uses the \ref HomeViewModel class to get the data for the view elements.
* It inherits from the View class.
*/
class HomeView : public View {
private:
Expand Down Expand Up @@ -44,7 +44,7 @@ class HomeView : public View {
public:
/** Constructs a home view with a given parent and a home view model.
* @param parent The parent object of the container, or NULL if the container is a screen.
* @param viewModel The reference to the home view model that provides the data for the view elements.
* @param aggregator The reference to the data aggregator to use as source of truth for this view.
*/
HomeView(lv_obj_t* parent, DataAggregator& aggregator);
};
Expand Down
4 changes: 2 additions & 2 deletions UOSM-Dashboard/Core/UI/StatsView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/** @ingroup core-ui
* A class that represents a view that displays the statistics of the dashboard, such as the motor RPM over time.
* It inherits from the View class and uses the StatsViewModel class to get the data for the view elements.
* It inherits from the View class.
*/
class StatsView : public View {
private:
Expand All @@ -31,7 +31,7 @@ class StatsView : public View {
public:
/** Constructs a stats view with a given parent and a stats view model.
* @param parent The parent object of the container, or NULL if the container is a screen.
* @param viewModel The reference to the stats view model that provides the data for the view elements.
* @param aggregator The reference to the data aggregator to use as source of truth for this view.
*/
StatsView(lv_obj_t* parent, DataAggregator& aggregator);
};
Expand Down
20 changes: 14 additions & 6 deletions UOSM-Dashboard/Core/UI/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@

#include "HomeView.hpp"
#include "StatsView.hpp"

static void Application_Fetch_Data(lv_timer_t*);
#include "LogView.hpp"

static HomeView* homeView;

static StatsView* statsView;

static lv_timer_t* dataTimer;
static LogView* logView;

static uint8_t screenIndex = 0;
static uint8_t nScreens = 2;
static uint8_t nScreens = 3;

void swipe_event_callback(lv_event_t* event) {
if (event->code == LV_EVENT_GESTURE) {
Expand Down Expand Up @@ -53,6 +50,9 @@ void swipe_event_callback(lv_event_t* event) {
case 1:
lv_scr_load_anim(statsView->getContainer(), animDirection, 300, 0, false);
break;
case 2:
lv_scr_load_anim(logView->getContainer(), animDirection, 300, 0, false);
break;
default:
break;
}
Expand All @@ -69,15 +69,23 @@ void Application_Create(DataAggregatorWrapper* aggregatorWrapper) {

DataAggregator& aggregator = DataAggregator_GetReference(aggregatorWrapper);

aggregator.canLogEntries.add(new CANLogEntry(THROTTLE_DATA_ID, 200, CAN_DECIMAL));
aggregator.canLogEntries.add(new CANLogEntry(THROTTLE_DATA_ID, 200, CAN_DECIMAL));
aggregator.canLogEntries.add(new CANLogEntry(MOTOR_RPM_DATA_ID, 200, CAN_DECIMAL));

// Create an object with no parent. (This will act as the screen).
homeView = new HomeView(nullptr, aggregator);
lv_obj_set_size(homeView->getContainer(), SCREEN_WIDTH, SCREEN_HEIGHT);

statsView = new StatsView(nullptr, aggregator);
lv_obj_set_size(statsView->getContainer(), SCREEN_WIDTH, SCREEN_HEIGHT);

logView = new LogView(nullptr, aggregator);
lv_obj_set_size(logView->getContainer(), SCREEN_WIDTH, SCREEN_HEIGHT);

lv_scr_load(homeView->getContainer());

lv_obj_add_event_cb(homeView->getContainer(), swipe_event_callback, LV_EVENT_GESTURE, nullptr);
lv_obj_add_event_cb(statsView->getContainer(), swipe_event_callback, LV_EVENT_GESTURE, nullptr);
lv_obj_add_event_cb(logView->getContainer(), swipe_event_callback, LV_EVENT_GESTURE, nullptr);
}
5 changes: 5 additions & 0 deletions UOSM-Dashboard/UOSM-Core/Inc/ApplicationTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ typedef enum {
RESULT_OK
} result_t;

typedef enum {
CAN_DECIMAL,
CAN_HEX
} CANLogEntryFormat;

typedef enum {
MOTOR_LOW_SPEED,
MOTOR_HIGH_SPEED
Expand Down
6 changes: 1 addition & 5 deletions UOSM-Dashboard/UOSM-Core/Utils/CANLogEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

#include "Identifiable.hpp"
#include "CANMessageLookUpModule.h"

enum CANLogEntryFormat {
CAN_DECIMAL,
CAN_HEX
};
#include "ApplicationTypes.h"

/**
* @class CANLogEntry
Expand Down
53 changes: 53 additions & 0 deletions UOSM-Dashboard/UOSM-Core/Utils/DataQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@

#include <stdexcept>

template <typename T>
class DataQueue;

template <typename T>
class DataQueueIterator {
private:
const DataQueue<T>& dataQueue;
uint8_t currentIndex;

public:
DataQueueIterator(const DataQueue<T>& dataQueue, uint8_t index): dataQueue(dataQueue), currentIndex(index) {}

bool operator!=(const DataQueueIterator& other) const {
return currentIndex != other.currentIndex;
}

DataQueueIterator& operator++() {
currentIndex++;
return *this;
}

const T& operator*() {
if (currentIndex >= dataQueue.getNumberOfElements()) {
throw std::out_of_range("Iterator is out of range.");
}
return dataQueue.getValues()[currentIndex];
}

uint8_t getCurrentIndex() const {
return currentIndex;
}
};

/** @ingroup core-ui-utils
* A class that aggregates the data to display on a bar chart.
* It can store any type T, but the values will be cast to lv_coord_t when displayed in a bar chart.
Expand Down Expand Up @@ -93,6 +126,16 @@ class DataQueue {
uint8_t i = head - 1;
values[i] = value;
}

using iterator = DataQueueIterator<T>;

iterator begin() const {
return iterator(*this, 0);
}

iterator end() const {
return iterator(*this, getNumberOfElements());
}
};

template<typename T>
Expand Down Expand Up @@ -180,6 +223,16 @@ class DataQueue<T*> {
delete values[i];
values[i] = value;
}

using iterator = DataQueueIterator<T*>;

iterator begin() const {
return iterator(*this, 0);
}

iterator end() const {
return iterator(*this, getNumberOfElements());
}
};

#endif //UOSM_CORE_DATAQUEUE_HPP
Expand Down

0 comments on commit e0d4e05

Please sign in to comment.