Skip to content

Commit

Permalink
Add speed dial
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremycote committed Mar 31, 2024
2 parents 0b0bea9 + 1c01310 commit 6353e67
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Core/Inc/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

/*Number of stops allowed per gradient. Increase this to allow more stops.
*This adds (sizeof(lv_color_t) + 1) bytes per additional stop*/
#define LV_GRADIENT_MAX_STOPS 2
#define LV_GRADIENT_MAX_STOPS 3

/*Default gradient buffer size.
*When LVGL calculates the gradient "maps" it can save them into a cache to avoid calculating them again.
Expand Down Expand Up @@ -347,7 +347,7 @@
#define LV_FONT_MONTSERRAT_24 0
#define LV_FONT_MONTSERRAT_26 0
#define LV_FONT_MONTSERRAT_28 0
#define LV_FONT_MONTSERRAT_30 0
#define LV_FONT_MONTSERRAT_30 1
#define LV_FONT_MONTSERRAT_32 0
#define LV_FONT_MONTSERRAT_34 0
#define LV_FONT_MONTSERRAT_36 0
Expand Down
17 changes: 16 additions & 1 deletion Core/Modules/CANCallbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "InternalCommsModule.h"
#include "DataAggregatorWrapper.h"
#include "CANDriver.h"

static DataAggregatorWrapper* aggregatorWrapper;

Expand All @@ -16,4 +17,18 @@ void EfficiencyDataCallback(iCommsMessage_t* msg) {
DebugPrint("Lap Efficiency: %d %d %d %d", e.lap_0, e.lap_1, e.lap_2, e.lap_3);

SetEfficiency(aggregatorWrapper, &e);
}
}

void ThrottleDataCallback(iCommsMessage_t *msg){}
void ErrorDataCallback(iCommsMessage_t *msg){}
void SpeedDataCallback(iCommsMessage_t *msg){
DebugPrint("Received speed");
speed_t speed = readMsg(msg);
DebugPrint("Speed %d", speed);
SetSpeed(aggregatorWrapper, speed);
}
void EventDataCallback(iCommsMessage_t *msg){}
void MotorRPMDataCallback(iCommsMessage_t *msg){}
void CurrentVoltageDataCallback(iCommsMessage_t *msg){}
void PressureTemperatureDataCallback(iCommsMessage_t *msg){}
void LightsDataCallback(iCommsMessage_t *msg){}
1 change: 1 addition & 0 deletions Core/Tasks/InternalCommsTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PRIVATE void InternalCommsTask(void* argument) {
cycleTick += TIMER_INTERNAL_COMMS_TASK;
osDelayUntil(cycleTick);

DebugPrint(".");
IComms_PeriodicReceive();
}
}
1 change: 1 addition & 0 deletions Core/UI/Data/DataAggregator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DataAggregator {
public:
Observable<voltage_t> batteryVoltage;
Observable<lap_efficiencies_t> efficiency;
Observable<speed_t> speed;
};

/** Returns a reference to the data aggregator object from a given wrapper.
Expand Down
4 changes: 4 additions & 0 deletions Core/UI/Data/DataAggregatorWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void SetEfficiency(DataAggregatorWrapper* wrapper, lap_efficiencies_t* efficienc
wrapper->aggregator.efficiency.notify(*efficiencies);
}

void SetSpeed(DataAggregatorWrapper* wrapper, speed_t speed) {
wrapper->aggregator.speed.notify(speed);
}

DataAggregator& DataAggregator_GetReference(DataAggregatorWrapper* wrapper) {
return wrapper->aggregator;
}
1 change: 1 addition & 0 deletions Core/UI/Data/DataAggregatorWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
DataAggregatorWrapper* DataAggregator_Create();

void SetEfficiency(DataAggregatorWrapper* wrapper, lap_efficiencies_t* efficiencies);
void SetSpeed(DataAggregatorWrapper* wrapper, speed_t speed);

#ifdef __cplusplus
}
Expand Down
106 changes: 96 additions & 10 deletions Core/UI/HomeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,51 @@

#include "HomeView.hpp"

#define MAX_SPEED 45

static void set_value(void * bar, int32_t v)
{
lv_bar_set_value((lv_obj_t*) bar, v, LV_ANIM_OFF);
}

static void event_cb(lv_event_t * e)
{
lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
if(dsc->part != LV_PART_INDICATOR) return;

lv_obj_t * obj = lv_event_get_target(e);

lv_draw_label_dsc_t label_dsc;
lv_draw_label_dsc_init(&label_dsc);
label_dsc.font = &lv_font_montserrat_30;

char buf[8];
lv_snprintf(buf, sizeof(buf), "%d", (int)lv_bar_get_value(obj));

lv_point_t txt_size;
lv_txt_get_size(&txt_size, buf, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
label_dsc.flag);

lv_area_t txt_area;
/*If the indicator is long enough put the text inside on the right*/
if(lv_area_get_width(dsc->draw_area) > txt_size.x + 20) {
txt_area.x2 = dsc->draw_area->x2 - 5;
txt_area.x1 = txt_area.x2 - txt_size.x + 1;
label_dsc.color = lv_color_white();
}
/*If the indicator is still short put the text out of it on the right*/
else {
txt_area.x1 = dsc->draw_area->x2 + 5;
txt_area.x2 = txt_area.x1 + txt_size.x - 1;
label_dsc.color = lv_color_black();
}

txt_area.y1 = dsc->draw_area->y1 + (lv_area_get_height(dsc->draw_area) - txt_size.y) / 2;
txt_area.y2 = txt_area.y1 + txt_size.y - 1;

lv_draw_label(dsc->draw_ctx, &label_dsc, &txt_area, buf, NULL);
}

HomeView::HomeView(lv_obj_t* parent, DataAggregator& aggregator) : View(parent, aggregator) {
lv_obj_t* container = getContainer();

Expand All @@ -16,39 +61,80 @@ HomeView::HomeView(lv_obj_t* parent, DataAggregator& aggregator) : View(parent,

lv_obj_add_style(container, &fullscreenViewStyle, LV_PART_MAIN);

DebugPrint("Flex column");

lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);

lv_style_t fullscreenRowStyle;
lv_style_init(&fullscreenRowStyle);
lv_style_set_bg_opa(&fullscreenRowStyle, 0);
lv_style_set_border_width(&fullscreenRowStyle, 0);

DebugPrint("Creating bottomRow");

topRow = lv_obj_create(container);
lv_obj_set_width(topRow, lv_obj_get_width(container));
lv_obj_set_height(topRow, 100);
lv_obj_add_style(topRow, &fullscreenRowStyle, LV_PART_MAIN);

lv_obj_t *bar = lv_bar_create(topRow);
lv_obj_add_event_cb(bar, event_cb, LV_EVENT_DRAW_PART_END, NULL);
lv_obj_set_size(bar, SCREEN_WIDTH - 40, 50);
lv_obj_center(bar);

lv_bar_set_range(bar, 0, MAX_SPEED);

static lv_style_t style_indic;

lv_style_init(&style_indic);
lv_style_set_bg_opa(&style_indic, LV_OPA_COVER);
lv_style_set_bg_color(&style_indic, lv_palette_main(LV_PALETTE_RED));
lv_style_set_bg_grad_color(&style_indic, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_bg_grad_dir(&style_indic, LV_GRAD_DIR_HOR);

static lv_grad_dsc_t grad_dsc;

grad_dsc.dir = LV_GRAD_DIR_HOR;
grad_dsc.stops_count = 3;

grad_dsc.stops[0].color = lv_palette_main(LV_PALETTE_DEEP_ORANGE);
grad_dsc.stops[0].frac = 0;

grad_dsc.stops[1].color = lv_palette_main(LV_PALETTE_GREEN);
grad_dsc.stops[1].frac = 255 * 25 / MAX_SPEED;

grad_dsc.stops[2].color = lv_palette_main(LV_PALETTE_RED);
grad_dsc.stops[2].frac = 255 * 35 / MAX_SPEED;

lv_style_set_bg_grad(&style_indic, &grad_dsc);

lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR);

bottomRow = lv_obj_create(container);
lv_obj_set_width(bottomRow, lv_obj_get_width(container));
lv_obj_set_height(bottomRow, SCREEN_HEIGHT - 100);
lv_obj_add_style(bottomRow, &fullscreenRowStyle, LV_PART_MAIN);

lv_obj_set_flex_grow(bottomRow, 1);
DebugPrint("Creating efficiency chart");

lv_obj_t* efficiency_chart;
efficiency_chart = lv_chart_create(bottomRow);
lv_obj_set_size(efficiency_chart, 800, 460);
lv_obj_set_size(efficiency_chart, 800, SCREEN_HEIGHT - 100);
lv_obj_center(efficiency_chart);

lv_chart_set_type(efficiency_chart, LV_CHART_TYPE_BAR);
lv_chart_set_update_mode(efficiency_chart, LV_CHART_UPDATE_MODE_SHIFT);
lv_chart_set_point_count(efficiency_chart, 4);
lv_chart_set_range(efficiency_chart, LV_CHART_AXIS_PRIMARY_Y, 0, 200);

lv_style_t style;
lv_style_set_line_width(&style, 20);

lv_obj_add_style(efficiency_chart, &style, LV_PART_MAIN);

DebugPrint("Adding efficiency series");
lv_chart_series_t* efficiencies = lv_chart_add_series(efficiency_chart, lv_palette_main(LV_PALETTE_INDIGO),
LV_CHART_AXIS_PRIMARY_Y);
getDataAggregator().speed.addListener([bar](const speed_t& speed) {
set_value(bar, speed / 1000);
});

getDataAggregator().efficiency.addListener([efficiency_chart, efficiencies](const lap_efficiencies_t& e) {
DebugPrint("Displaying efficiencies: %d %d %d %d", e.lap_0, e.lap_1, e.lap_2, e.lap_3);

getDataAggregator().efficiency.addListener([efficiency_chart, efficiencies, bar](const lap_efficiencies_t& e) {
lv_chart_set_next_value(efficiency_chart, efficiencies, e.lap_0);
lv_chart_set_next_value(efficiency_chart, efficiencies, e.lap_1);
lv_chart_set_next_value(efficiency_chart, efficiencies, e.lap_2);
Expand Down
1 change: 1 addition & 0 deletions Core/UI/HomeView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class HomeView : public View {
private:
/** Bottom row of the 2 row flex layout */
lv_obj_t* topRow;
lv_obj_t* bottomRow;

protected:
Expand Down
7 changes: 7 additions & 0 deletions Core/UI/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ void Application_Create(DataAggregatorWrapper* aggregatorWrapper) {

DataAggregator& aggregator = DataAggregator_GetReference(aggregatorWrapper);

DebugPrint("Creating HomeView");

DebugPrint("Aggregator: %04x", aggregatorWrapper);

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

DebugPrint("Setting Size");
lv_obj_set_size(homeView->getContainer(), SCREEN_WIDTH, SCREEN_HEIGHT);

DebugPrint("Loading View");
lv_scr_load(homeView->getContainer());
}
26 changes: 12 additions & 14 deletions UOSM-Core/Modules/CANMessageLookUpModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
#include "CANMessageLookUpModule.h"
#include "CANDriver.h"

#ifndef MBED
#define WEAK __weak
#else
#define WEAK
#endif
/**
* Do not mark these as weak, if it's not implemented somewhere else, the code will crash
*/

extern WEAK void ThrottleDataCallback(iCommsMessage_t* msg);
extern WEAK void ErrorDataCallback(iCommsMessage_t* msg);
extern WEAK void SpeedDataCallback(iCommsMessage_t* msg);
extern WEAK void EventDataCallback(iCommsMessage_t* msg);
extern WEAK void MotorRPMDataCallback(iCommsMessage_t* msg);
extern WEAK void CurrentVoltageDataCallback(iCommsMessage_t* msg);
extern WEAK void PressureTemperatureDataCallback(iCommsMessage_t* msg);
extern WEAK void LightsDataCallback(iCommsMessage_t* msg);
extern WEAK void EfficiencyDataCallback(iCommsMessage_t* msg);
extern void ThrottleDataCallback(iCommsMessage_t *msg);
extern void ErrorDataCallback(iCommsMessage_t *msg);
extern void SpeedDataCallback(iCommsMessage_t *msg);
extern void EventDataCallback(iCommsMessage_t *msg);
extern void MotorRPMDataCallback(iCommsMessage_t *msg);
extern void CurrentVoltageDataCallback(iCommsMessage_t *msg);
extern void PressureTemperatureDataCallback(iCommsMessage_t *msg);
extern void LightsDataCallback(iCommsMessage_t *msg);
extern void EfficiencyDataCallback(iCommsMessage_t *msg);

/*********************************************************************************
*
Expand Down

0 comments on commit 6353e67

Please sign in to comment.