Skip to content

Commit 5d1841b

Browse files
committed
moved button functionality out of main.cpp and cleaned up code
1 parent 79478f9 commit 5d1841b

5 files changed

Lines changed: 44 additions & 108 deletions

File tree

lib/interfaces/include/ACUInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
static constexpr uint8_t MAX_IC_INDEX = 12;
1313
static constexpr uint8_t VOLT_GROUPS = 4;
1414
static constexpr uint8_t VOLTS_PER_IC = 12;
15+
16+
static constexpr uint8_t MAX_ICS_Voltage_Screen1 = 6;
17+
static constexpr uint8_t MAX_ICS_Voltage_Screen2 = 12;
18+
static constexpr uint8_t TEMPS_PER_IC = 3;
1519
struct ACUInterfaceData_s
1620
{
1721
/* ACU Status Message */

lib/interfaces/include/DisplayInterface.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include "SharedFirmwareTypes.h"
1111
#include "ACUInterface.h"
1212

13-
extern unsigned long button_state;
14-
extern unsigned long last_button_state;
15-
extern unsigned long clicks;
1613

1714
using pin = size_t;
1815

@@ -42,14 +39,12 @@ class DisplayInterface
4239
void display_data_v();
4340
void refresh_display_data(unsigned long curr_millis);
4441
void toggle_display_data();
45-
46-
static constexpr uint8_t MAX_ICS_set1 = 7;
47-
static constexpr uint8_t MAX_ICS_set2 = 13;
48-
static constexpr uint8_t MAX_ICS = 12;
49-
static constexpr uint8_t VOLTS_PER_IC = 12;
50-
static constexpr uint8_t TEMPS_PER_IC = 3;
42+
void setup_button_pin(uint8_t pin);
43+
static void button_func();
5144

5245
Adafruit_ILI9341 Display;
46+
47+
volatile unsigned long clicks = 0;
5348

5449

5550
private:

lib/interfaces/src/DisplayInterface.cpp

Lines changed: 33 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
#include "EMInterface.h"
44

55

6-
extern unsigned long button_state = 0;
7-
extern unsigned long last_button_state = 0;
8-
extern unsigned long clicks = 0;
9-
106
void DisplayInterface::init() {
117
Display.begin();
128
Display.setRotation(3);
@@ -15,11 +11,12 @@ void DisplayInterface::init() {
1511
}
1612

1713
void DisplayInterface::display_data() {
14+
15+
//print critical charging data
1816
Display.fillScreen(ILI9341_BLACK);
1917
Display.setCursor(0,0);
2018
Display.setRotation(3);
2119
Display.setTextSize(2);
22-
2320
Display.println("Charging Status: ");
2421

2522
if (_ccu_data.charging_state == ChargingState_e::CHARGING) {
@@ -30,28 +27,8 @@ void DisplayInterface::display_data() {
3027
} else {
3128
Display.println("Not Charging");
3229
}
33-
34-
35-
Display.print("Cell Voltage max: ");
36-
Display.println(ACUInterfaceInstance::instance().get_latest_data().high_voltage, 3);
37-
38-
39-
Display.print("Cell Voltage min: ");
40-
Display.println(ACUInterfaceInstance::instance().get_latest_data().low_voltage, 3);
41-
42-
43-
Display.print("Cell Voltage avg: ");
44-
Display.println(ACUInterfaceInstance::instance().get_latest_data().average_voltage, 3);
45-
46-
47-
Display.print("Cell Voltage delta: ");
48-
Display.println((ACUInterfaceInstance::instance().get_latest_data().high_voltage - ACUInterfaceInstance::instance().get_latest_data().low_voltage), 3);
49-
50-
51-
Display.print("Total pack Volts: ");
52-
Display.println(ACUInterfaceInstance::instance().get_latest_data().total_voltage, 3);
53-
54-
30+
Display.print("\n");
31+
Display.print("\n");
5532
Display.print("Max Board Temp (C): ");
5633
Display.println(ACUInterfaceInstance::instance().get_latest_data().max_board_temp, 3);
5734

@@ -69,7 +46,6 @@ void DisplayInterface::display_data() {
6946

7047
Display.print("EM current (A): ");
7148
Display.println(EnergyMeterInterfaceInstance::instance().get_latest_em_data().current_amps, 3);
72-
7349

7450
}
7551
// function that displays cell temps from BMS
@@ -80,19 +56,19 @@ void DisplayInterface::display_data_temps() {
8056
Display.setTextSize(2);
8157
Display.println("Cell Temps");
8258

83-
for (int ic = 1; ic < MAX_ICS_set2; ic++){
84-
uint8_t ic_idx = static_cast<uint8_t>(ic - 1);
59+
for (int ic = 0; ic < MAX_IC_INDEX; ic++){
60+
uint8_t ic_idx = static_cast<uint8_t>(ic);
8561

8662
// Bounds check for linter
87-
if (ic_idx < MAX_ICS){
63+
if (ic_idx < MAX_IC_INDEX){
8864
Display.print("Cell ");
89-
Display.print(ic);
65+
Display.print(ic+1);
9066
Display.print(": ");
91-
Display.print(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic-1][0], 3);
67+
Display.print(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic][0], 3);
9268
Display.print(" ");
93-
Display.print(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic-1][1], 3);
69+
Display.print(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic][1], 3);
9470
Display.print(" ");
95-
Display.println(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic-1][2], 3);
71+
Display.println(ACUInterfaceInstance::instance().get_latest_data().cell_temps[ic][2], 3);
9672
}
9773
}
9874

@@ -109,12 +85,12 @@ void DisplayInterface::display_data_v(){
10985
Display.println("Detailed Voltages");
11086
Display.print("\n");
11187

112-
for(int ic = 1; ic < MAX_ICS_set1; ic++){ //can change conditiosn of this look to show more or less ICs
88+
for(int ic = 0; ic < MAX_ICS_Voltage_Screen1; ic++){ //can change conditiosn of this look to show more or less ICs
11389
Display.print("Cell ");
114-
Display.print(ic);
90+
Display.print(ic+1);
11591
Display.print(": ");
116-
for (int i = 0; i < MAX_ICS; i++){
117-
Display.print(ACUInterfaceInstance::instance().get_latest_data().voltage_array[ic-1][i], 3);
92+
for (int i = 0; i < MAX_IC_INDEX; i++){
93+
Display.print(ACUInterfaceInstance::instance().get_latest_data().voltage_array[ic][i], 3);
11894
Display.print(" ");
11995
}
12096
Display.print("\n");
@@ -143,40 +119,7 @@ void DisplayInterface::toggle_display_data()
143119
switch (clicks)
144120
{
145121
default:
146-
//print critical charging data
147-
Display.fillScreen(ILI9341_BLACK);
148-
Display.setCursor(0,0);
149-
Display.setRotation(3);
150-
Display.setTextSize(2);
151-
Display.println("Charging Status: ");
152-
153-
if (_ccu_data.charging_state == ChargingState_e::CHARGING) {
154-
Display.print("Charging at ");
155-
Display.println(_ccu_data.calculated_charge_current);
156-
} else if (_ccu_data.charging_state == ChargingState_e::DONE_CHARGING) {
157-
Display.println("Done charging!");
158-
} else {
159-
Display.println("Not Charging");
160-
}
161-
Display.print("\n");
162-
Display.print("\n");
163-
Display.print("Max Board Temp (C): ");
164-
Display.println(ACUInterfaceInstance::instance().get_latest_data().max_board_temp, 3);
165-
166-
167-
Display.print("Max Cell Temp (C): ");
168-
//Display.println(_ccu_data.max_cell_temp, 3);
169-
Display.println(ACUInterfaceInstance::instance().get_latest_data().max_cell_temp, 3);
170-
171-
172-
Display.print("Min Cell Temp (C): ");
173-
Display.println(ACUInterfaceInstance::instance().get_latest_data().min_cell_temp, 3);
174-
175-
Display.print("Avg Cell Temp (C): ");
176-
Display.println((ACUInterfaceInstance::instance().get_latest_data().max_cell_temp + ACUInterfaceInstance::instance().get_latest_data().min_cell_temp)/2, 3);
177-
178-
Display.print("EM current (A): ");
179-
Display.println(EnergyMeterInterfaceInstance::instance().get_latest_em_data().current_amps, 3);
122+
display_data();
180123
break;
181124
case 1:
182125
Display.fillScreen(ILI9341_BLACK);
@@ -216,12 +159,12 @@ void DisplayInterface::toggle_display_data()
216159
Display.setTextSize(1);
217160
Display.println("Detailed Voltages");
218161
Display.print("\n");
219-
for(int ic = MAX_ICS_set1; ic < MAX_ICS_set2; ic++){
162+
for(int ic = MAX_ICS_Voltage_Screen1; ic < MAX_ICS_Voltage_Screen2; ic++){
220163
Display.print("Cell ");
221-
Display.print(ic);
164+
Display.print(ic+1);
222165
Display.print(": ");
223-
for (int i = 0; i < MAX_ICS; i++){
224-
Display.print(ACUInterfaceInstance::instance().get_latest_data().voltage_array[ic-1][i], 3);
166+
for (int i = 0; i < MAX_IC_INDEX; i++){
167+
Display.print(ACUInterfaceInstance::instance().get_latest_data().voltage_array[ic][i], 3);
225168
Display.print(" ");
226169
}
227170
Display.print("\n");
@@ -232,3 +175,16 @@ void DisplayInterface::toggle_display_data()
232175
}
233176
}
234177

178+
void DisplayInterface::setup_button_pin(uint8_t pin) {
179+
pinMode(pin, INPUT_PULLUP);
180+
attachInterrupt(digitalPinToInterrupt(pin), DisplayInterface::button_func, FALLING);
181+
}
182+
183+
void DisplayInterface::button_func() {
184+
static unsigned long last_interrupt_time = 0;
185+
unsigned long interrupt_time = millis();
186+
if (interrupt_time - last_interrupt_time > 50) {
187+
DisplayInterfaceInstance::instance().clicks++;
188+
last_interrupt_time = interrupt_time;
189+
}
190+
}

src/CCUTasks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ HT_TASK::TaskResponse init_update_display_task(const unsigned long& sysMicros, c
116116

117117
HT_TASK::TaskResponse run_update_display_task(const unsigned long& sysMicros, const HT_TASK::TaskInfo& taskInfo) {
118118
//DisplayInterfaceInstance::instance().display_data();
119-
//DisplayInterfaceInstance::instance().refresh_display_data(sys_time::hal_millis());
119+
DisplayInterfaceInstance::instance().refresh_display_data(sys_time::hal_millis());
120120
return HT_TASK::TaskResponse::YIELD;
121121
}
122122

src/main.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
#include "SystemTimeInterface.h"
1717
#include "DisplayInterface.h"
1818

19-
const unsigned long debounce_time = 50; // milliseconds
20-
unsigned long switch_press_time;
21-
2219
FlexCAN_Type<CAN2> CHARGER_CAN; //placed here after debugging
2320
FlexCAN_Type<CAN1> ACU_CAN;
2421

@@ -53,20 +50,7 @@ HT_TASK::Task debug_print_task(HT_TASK::DUMMY_FUNCTION, print_data, CCUConstants
5350
HT_TASK::Task tick_state_machine_task(HT_TASK::DUMMY_FUNCTION, tick_state_machine, CCUConstants::TICK_STATE_MACHINE_PRIORITY, CCUConstants::TICK_STATE_MACHINE_PERIOD);
5451
HT_TASK::Task calculate_charge_current_task(HT_TASK::DUMMY_FUNCTION, calculate_charge_current, CCUConstants::TICK_STATE_MACHINE_PRIORITY, CCUConstants::TICK_STATE_MACHINE_PERIOD);
5552

56-
void button_press()
57-
{
58-
clicks += 1;
59-
}
6053

61-
void debounce()
62-
{
63-
64-
if (millis() - switch_press_time >= debounce_time)
65-
{
66-
button_press();
67-
switch_press_time = millis();
68-
}
69-
}
7054

7155

7256

@@ -76,10 +60,9 @@ void setup() {
7660

7761

7862
qn::Ethernet.begin(); //begins QNEthernet
79-
pinMode(A15, INPUT_PULLUP); // edit
8063

8164
intitialize_all_interfaces();
82-
attachInterrupt(digitalPinToInterrupt(A15), debounce, FALLING);
65+
DisplayInterfaceInstance::instance().setup_button_pin(A15);
8366

8467
scheduler.setTimingFunction(micros);
8568
scheduler.schedule(read_dial_task);
@@ -93,7 +76,7 @@ void setup() {
9376
scheduler.schedule(kick_watchdog_task);
9477
//scheduler.schedule(tick_state_machine_task); //this task times out watchdog for some reason (state machine would be nice to have but isn't a priority for CCU to work)
9578
scheduler.schedule(calculate_charge_current_task);
96-
//scheduler.schedule(update_display_task);
79+
scheduler.schedule(update_display_task);
9780
scheduler.schedule(toggle_display_task);
9881

9982
handle_CAN_setup(ACU_CAN, CCUConstants::CAN_BAUDRATE, &CCUCANInterfaceImpl::on_acu_can_receive);
@@ -104,8 +87,6 @@ void setup() {
10487

10588
void loop()
10689
{
107-
button_state = digitalRead(A15);
10890
scheduler.run();
109-
11091
}
11192

0 commit comments

Comments
 (0)