Skip to content

Commit 17a52f4

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/rotor_temp_msgs
2 parents 19cc772 + b2d0db1 commit 17a52f4

11 files changed

Lines changed: 114 additions & 145 deletions

File tree

include/VCF_Constants.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ namespace VCFInterfaceConstants {
1212
// constexpr int BTN_PRESET_READ = 31;
1313
// constexpr int BTN_MODE_READ = 27; // USED TO BE 26.
1414
*/
15-
15+
/* Dashboard Buttons */
1616
constexpr int BTN_MC_CYCLE_READ = 31; // DB/MC_RESET on schematic
1717
constexpr int BTN_START_READ = 29; // RTD on schematic
1818
constexpr int BTN_DATA_READ = 30; // DATA_MARK on schematic
1919
constexpr int BUZZER_CONTROL_PIN = 32;
2020
constexpr int BRIGHTNESS_CONTROL_PIN = 26; //BUTTON_1 on schematic
2121
constexpr int BUTTON_2 = 27; // BUTTON_2 on schematic
2222
constexpr int BTN_PRESET_READ = 28; // Pedals recal button (brightness control on schematic)
23+
24+
/* Dashboard IOExpander Setup */
25+
constexpr uint8_t IO_EXPANDER_ADDR = 0x20;
2326

2427
constexpr int NEOPIXEL_CONTROL_PIN = 33;
2528
constexpr int NEOPIXEL_COUNT = 16; // 16 neopixeles on dashboard

include/VCF_Globals.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

include/VCF_Tasks.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ HT_TASK::TaskResponse handle_CAN_receive(const unsigned long& sysMicros, const H
9595
HT_TASK::TaskResponse run_dash_GPIOs_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info); // NOLINT (capitalization of GPIOs)
9696
HT_TASK::TaskResponse send_dash_data(const unsigned long &sysMicros, const HT_TASK::TaskInfo &taskInfo);
9797

98-
HT_TASK::TaskResponse create_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
99-
HT_TASK::TaskResponse read_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
100-
10198
HT_TASK::TaskResponse init_neopixels_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
10299
HT_TASK::TaskResponse run_update_neopixels_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
103100

lib/interfaces/include/DashboardInterface.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#ifndef DASHBOARD_INTERFACE_H
22
#define DASHBOARD_INTERFACE_H
33

4-
#include "hytech_msgs.pb.h"
5-
#include "SharedFirmwareTypes.h"
64
#include "Arduino.h"
75
#include "etl/singleton.h"
8-
#include "hytech.h"
6+
#include "MCP23017.h"
97
#include <Wire.h>
10-
#include "SystemTimeInterface.h"
118
#include "FlexCAN_T4.h"
129

10+
#include "hytech_msgs.pb.h"
11+
#include "SharedFirmwareTypes.h"
12+
#include "hytech.h"
13+
14+
#include "SystemTimeInterface.h"
15+
1316

1417
// Struct representing dashboard gpios
1518
struct DashboardGPIOs_s {
@@ -28,7 +31,9 @@ class DashboardInterface
2831
public:
2932
DashboardInterface() = delete;
3033

31-
DashboardInterface(DashboardGPIOs_s gpios) : _dashboard_gpios(gpios)
34+
DashboardInterface(DashboardGPIOs_s gpios,
35+
uint8_t io_expander_addr, TwoWire &i2c_bus)
36+
: _dashboard_gpios(gpios), _io_expander(MCP23017(io_expander_addr, i2c_bus))
3237
{
3338
pinMode(_dashboard_gpios.START_BUTTON, INPUT_PULLUP);
3439
pinMode(_dashboard_gpios.PRESET_BUTTON, INPUT_PULLUP);
@@ -38,6 +43,9 @@ class DashboardInterface
3843
pinMode(_dashboard_gpios.BUTTON_2, INPUT_PULLUP);
3944

4045
_dash_created_millis = sys_time::hal_millis();
46+
47+
i2c_bus.begin();
48+
_initIOExpander();
4149
}
4250

4351
// Reading gpios
@@ -60,17 +68,29 @@ class DashboardInterface
6068
bool imd_ok = true;
6169

6270
void set_dial_state(ControllerMode_e mode);
71+
72+
void read_ioexpander();
6373

6474
private:
6575

6676
DashboardGPIOs_s _dashboard_gpios;
6777
DashInputState_s _dashboard_outputs;
6878
DashInputState_s _dashboard_stored_state;
6979

80+
MCP23017 _io_expander;
81+
7082
unsigned long _dash_created_millis;
71-
};
7283

84+
inline void _initIOExpander() {
85+
_io_expander.init();
86+
87+
_io_expander.portMode(MCP23017Port::A, 0b00000000); // 0b0000 0000 = 0
88+
_io_expander.portMode(MCP23017Port::B, 0b01111111); // 0b0111 1111 = 127
7389

90+
_io_expander.writeRegister(MCP23017Register::GPPU_B, 0xFF); // Internal pull-ups
91+
_io_expander.writeRegister(MCP23017Register::IPOL_B, 0xFF); // Polarity (inverted)
92+
}
93+
};
7494

7595
using DashboardInterfaceInstance = etl::singleton<DashboardInterface>;
7696

lib/interfaces/src/DashboardInterface.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#include "DashboardInterface.h"
21
#include "SharedFirmwareTypes.h"
2+
3+
#include "DashboardInterface.h"
34
#include "CANInterface.h"
45
#include "VCFCANInterfaceImpl.h"
56

7+
#include "IOExpanderUtils.h"
8+
69
/* Button reads */
710
DashInputState_s DashboardInterface::get_dashboard_outputs()
811
{
@@ -39,4 +42,65 @@ DashInputState_s DashboardInterface::get_dashboard_stored_state()
3942
void DashboardInterface::sync_dashboard_stored_state()
4043
{
4144
_dashboard_stored_state = _dashboard_outputs;
45+
}
46+
47+
void DashboardInterface::read_ioexpander() {
48+
uint16_t data = _io_expander.read(); // read data from IOExpander
49+
ControllerMode_e new_mode = ControllerMode_e::MODE_0; // default to mode 0
50+
51+
// check for value of dial
52+
if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 0)) { // NOLINT 0 is pos of bit
53+
new_mode = ControllerMode_e::MODE_0;
54+
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 1)) { // NOLINT 1 is pos of bit
55+
new_mode = ControllerMode_e::MODE_1;
56+
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 2)) { // NOLINT 2 is pos of bit
57+
new_mode = ControllerMode_e::MODE_2;
58+
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 3)) { // NOLINT 3 is pos of bit
59+
new_mode = ControllerMode_e::MODE_3;
60+
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 4)) { // NOLINT 4 is pos of bit
61+
new_mode = ControllerMode_e::MODE_4;
62+
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 5)) { // NOLINT 5 is pos of bit
63+
new_mode = ControllerMode_e::MODE_5;
64+
}
65+
66+
_dashboard_outputs.dial_state = new_mode; // set new mode
67+
68+
// write to 8-seg display based on current mode
69+
switch (_dashboard_outputs.dial_state) {
70+
case ControllerMode_e::MODE_0:
71+
{
72+
_io_expander.writePort(MCP23017Port::A, 0b00000010); // NOLINT 0b0000 0010 = 2
73+
break;
74+
}
75+
case ControllerMode_e::MODE_1:
76+
{
77+
_io_expander.writePort(MCP23017Port::A, 0b01010111); // NOLINT 0b0101 0111 = 87
78+
break;
79+
}
80+
case ControllerMode_e::MODE_2:
81+
{
82+
_io_expander.writePort(MCP23017Port::A, 0b00011000); // NOLINT 0b0001 1000 = 24
83+
break;
84+
}
85+
case ControllerMode_e::MODE_3:
86+
{
87+
_io_expander.writePort(MCP23017Port::A, 0b00010100); // NOLINT 0b0001 0100 = 20
88+
break;
89+
}
90+
case ControllerMode_e::MODE_4:
91+
{
92+
_io_expander.writePort(MCP23017Port::A, 0b01000101); // NOLINT 0b0100 0101 = 69
93+
break;
94+
}
95+
case ControllerMode_e::MODE_5:
96+
{
97+
_io_expander.writePort(MCP23017Port::A, 0b00100100); // NOLINT 0b0010 0100 = 36
98+
break;
99+
}
100+
default:
101+
{
102+
_io_expander.writePort(MCP23017Port::A, 0b11110000); // NOLINT 0b1111 0000 = 240
103+
break;
104+
}
105+
}
42106
}

lib/systems/include/IOExpanderUtils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
namespace IOExpanderUtils
99
{
10-
/*
10+
/**
1111
IOExpander's read() only reads.
1212
getBit() only get specified bit from previously read dataframe and does not read()
13-
@param port 0=A; 1=B
13+
@param data data from which to get the specified bit
14+
@param port port from which to get the bit from (A=0, B=1)
15+
@param bit bit number of port to get bit from
1416
*/
15-
bool getBit(uint16_t data, bool port, int bit);
17+
bool getBit(uint16_t data, bool port, uint8_t bit);
1618
}
1719

1820
#endif
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "IOExpanderUtils.h"
22

3-
bool IOExpanderUtils::getBit(uint16_t data, bool port, int bit)
3+
/*
4+
* Retrieves the bit from the data frame.
5+
* Port A = 0, and is the lower byte of data. Port B = 1, and is the higher byte of data.
6+
*/
7+
bool IOExpanderUtils::getBit(uint16_t data, bool port, uint8_t bit)
48
{
5-
if(!port){ //0=A
6-
return (data>>bit)&1;
7-
}
8-
return (data>>(8+bit))&1; // NOLINT (B port is in upper 8 bits, while A port is in lower 8 bits)
9+
return (data >> ((uint16_t) port * 8 + bit)) & 1; // NOLINT 8 is num of bits in byte
910
}

src/VCF_Tasks.cpp

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "VCF_Tasks.h"
22
#include "SharedFirmwareTypes.h"
3-
#include "VCF_Globals.h"
3+
#include "EthernetAddressDefs.h"
44
#include "VCF_Constants.h"
55
#include <QNEthernet.h>
66
#include "ProtobufMsgInterface.h"
@@ -37,7 +37,7 @@ HT_TASK::TaskResponse run_read_adc0_task(const unsigned long& sysMicros, const H
3737
});
3838

3939
// sample digital steering too TODO: move this to its own task maybe?
40-
// OrbisBRInstance::instance().sample();
40+
OrbisBRInstance::instance().sample();
4141
return HT_TASK::TaskResponse::YIELD;
4242
}
4343

@@ -267,92 +267,9 @@ HT_TASK::TaskResponse run_dash_GPIOs_task(const unsigned long& sys_micros, const
267267
NeopixelControllerInstance::instance().dim_neopixels();
268268
}
269269

270-
DashboardInterfaceInstance::instance().sync_dashboard_stored_state();
271-
272-
return HT_TASK::TaskResponse::YIELD;
273-
}
274-
275-
HT_TASK::TaskResponse create_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info)
276-
{
277-
Wire2.begin();
278-
IOExpanderInstance::create(0x20, Wire2);
279-
IOExpanderInstance::instance().init();
280-
281-
IOExpanderInstance::instance().portMode(MCP23017Port::A, 0b00000000);
282-
IOExpanderInstance::instance().portMode(MCP23017Port::B, 0b01111111);
283-
284-
// IOExpanderInstance::instance().writeRegister(MCP23017Register::GPIO_A, 0x00); //Reset port A
285-
// IOExpanderInstance::instance().writeRegister(MCP23017Register::GPIO_B, 0x00); //Reset port B
286-
287-
IOExpanderInstance::instance().writeRegister(MCP23017Register::GPPU_B, 0xFF); //Internal pull-ups
270+
DashboardInterfaceInstance::instance().read_ioexpander();
288271

289-
IOExpanderInstance::instance().writeRegister(MCP23017Register::IPOL_B, 0xFF); //Polarity (inverted)
290-
291-
return HT_TASK::TaskResponse::YIELD;
292-
}
293-
294-
HT_TASK::TaskResponse read_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info)
295-
{
296-
uint16_t in = IOExpanderInstance::instance().read(); // NOLINT (linter thinks variable uninitialized)
297-
// for (int i = 0; i < 8; ++i) {
298-
// Serial.printf("%d ", in & 0x01);
299-
// in = in >> 1;
300-
// }
301-
// Serial.println("");
302-
303-
//TODO: Double check the harnessing is not wrong this time
304-
if (IOExpanderUtils::getBit(in, 1, 0)) {
305-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_0);
306-
} else if (IOExpanderUtils::getBit(in, 1, 1)) {
307-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_1);
308-
} else if (IOExpanderUtils::getBit(in, 1, 2)) {
309-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_2);
310-
} else if (IOExpanderUtils::getBit(in, 1, 3)) { // NOLINT (pin is magic number)
311-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_3);
312-
} else if (IOExpanderUtils::getBit(in, 1, 4)) { // NOLINT (pin is magic number)
313-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_4);
314-
} else if (IOExpanderUtils::getBit(in, 1, 5)) { // NOLINT (pin is magic number)
315-
DashboardInterfaceInstance::instance().set_dial_state(ControllerMode_e::MODE_5);
316-
}
317-
318-
ControllerMode_e state = DashboardInterfaceInstance::instance().get_dashboard_outputs().dial_state; // NOLINT (linter thinks state uninitialized)
319-
switch (state) {
320-
case ControllerMode_e::MODE_0:
321-
{
322-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b00000010);
323-
break;
324-
}
325-
case ControllerMode_e::MODE_1:
326-
{
327-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b01010111);
328-
break;
329-
}
330-
case ControllerMode_e::MODE_2:
331-
{
332-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b00011000);
333-
break;
334-
}
335-
case ControllerMode_e::MODE_3:
336-
{
337-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b00010100);
338-
break;
339-
}
340-
case ControllerMode_e::MODE_4:
341-
{
342-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b01000101);
343-
break;
344-
}
345-
case ControllerMode_e::MODE_5:
346-
{
347-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b00100100);
348-
break;
349-
}
350-
default:
351-
{
352-
IOExpanderInstance::instance().writePort(MCP23017Port::A, 0b11110000);
353-
break;
354-
}
355-
}
272+
DashboardInterfaceInstance::instance().sync_dashboard_stored_state();
356273

357274
return HT_TASK::TaskResponse::YIELD;
358275
}
@@ -616,7 +533,7 @@ void setup_all_interfaces() {
616533
.DATA_BUTTON = VCFInterfaceConstants::BTN_DATA_READ,
617534
.BUTTON_2 = VCFInterfaceConstants::BUTTON_2
618535
};
619-
DashboardInterfaceInstance::create(dashboard_gpios); //NOLINT
536+
DashboardInterfaceInstance::create(dashboard_gpios, VCFInterfaceConstants::IO_EXPANDER_ADDR, Wire2); //NOLINT
620537
ACUInterfaceInstance::create();
621538
VCRInterfaceInstance::create();
622539

0 commit comments

Comments
 (0)