Skip to content

Commit b2d0db1

Browse files
fix/io_expander_into_dash_interface (#60)
* init commit + fixes * adjust faux can baudrate * merge with main * removed commented blocks and moved around some CAN things * removed ethernet socket defs from globals * updated getBit from IOExpander Utils to use MCP lib * removed VCF Globals * reworked IOExpander interface into Dashboard interface * moved io expander reading to dash GPIOs task * updated to reflect the cpp file * fixed CI checks * added back digital steering sensor --------- Co-authored-by: mansi-b29 <133283027+mansi-b29@users.noreply.github.com>
1 parent b841a8e commit b2d0db1

12 files changed

Lines changed: 117 additions & 156 deletions

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/interfaces/src/VCFCANInterfaceImpl.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ namespace VCFCANInterfaceImpl {
1515
uint8_t buf[sizeof(CAN_message_t)];
1616
memmove(buf, &msg, sizeof(msg)); // NOLINT (memory operations are fine)
1717
VCFCANInterfaceInstance::instance().front_aux_can_rx_buffer.push_back(buf, sizeof(CAN_message_t));
18-
19-
// Serial.println("msg recvd");
20-
// Serial.print("MB: "); Serial.print(msg.mb);
21-
// Serial.print(" ID: 0x"); Serial.print(msg.id, HEX);
22-
// Serial.print(" EXT: "); Serial.print(msg.flags.extended);
23-
// Serial.print(" LEN: "); Serial.print(msg.len);
24-
// Serial.print(" DATA: ");
25-
// for ( uint8_t i = 0; i < 8; i++ ) {
26-
// Serial.print(msg.buf[i]); Serial.print(" ");
27-
// }
28-
// Serial.print(" TS: "); Serial.println(msg.timestamp);
2918
}
3019

3120
void vcf_recv_switch(CANInterfaces &interfaces, const CAN_message_t &msg, unsigned long millis, CANInterfaceType_e interface_type)

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
}

0 commit comments

Comments
 (0)