Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/VCF_Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ namespace VCFInterfaceConstants {
// constexpr int BTN_PRESET_READ = 31;
// constexpr int BTN_MODE_READ = 27; // USED TO BE 26.
*/

/* Dashboard Buttons */
constexpr int BTN_MC_CYCLE_READ = 31; // DB/MC_RESET on schematic
constexpr int BTN_START_READ = 29; // RTD on schematic
constexpr int BTN_DATA_READ = 30; // DATA_MARK on schematic
constexpr int BUZZER_CONTROL_PIN = 32;
constexpr int BRIGHTNESS_CONTROL_PIN = 26; //BUTTON_1 on schematic
constexpr int BUTTON_2 = 27; // BUTTON_2 on schematic
constexpr int BTN_PRESET_READ = 28; // Pedals recal button (brightness control on schematic)

/* Dashboard IOExpander Setup */
constexpr uint8_t IO_EXPANDER_ADDR = 0x20;

constexpr int NEOPIXEL_CONTROL_PIN = 33;
constexpr int NEOPIXEL_COUNT = 16; // 16 neopixeles on dashboard
Expand Down
33 changes: 0 additions & 33 deletions include/VCF_Globals.h

This file was deleted.

3 changes: 0 additions & 3 deletions include/VCF_Tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ HT_TASK::TaskResponse handle_CAN_receive(const unsigned long& sysMicros, const H
HT_TASK::TaskResponse run_dash_GPIOs_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info); // NOLINT (capitalization of GPIOs)
HT_TASK::TaskResponse send_dash_data(const unsigned long &sysMicros, const HT_TASK::TaskInfo &taskInfo);

HT_TASK::TaskResponse create_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
HT_TASK::TaskResponse read_ioexpander(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);

HT_TASK::TaskResponse init_neopixels_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);
HT_TASK::TaskResponse run_update_neopixels_task(const unsigned long& sys_micros, const HT_TASK::TaskInfo& task_info);

Expand Down
32 changes: 26 additions & 6 deletions lib/interfaces/include/DashboardInterface.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#ifndef DASHBOARD_INTERFACE_H
#define DASHBOARD_INTERFACE_H

#include "hytech_msgs.pb.h"
#include "SharedFirmwareTypes.h"
#include "Arduino.h"
#include "etl/singleton.h"
#include "hytech.h"
#include "MCP23017.h"
#include <Wire.h>
#include "SystemTimeInterface.h"
#include "FlexCAN_T4.h"

#include "hytech_msgs.pb.h"
#include "SharedFirmwareTypes.h"
#include "hytech.h"

#include "SystemTimeInterface.h"


// Struct representing dashboard gpios
struct DashboardGPIOs_s {
Expand All @@ -28,7 +31,9 @@ class DashboardInterface
public:
DashboardInterface() = delete;

DashboardInterface(DashboardGPIOs_s gpios) : _dashboard_gpios(gpios)
DashboardInterface(DashboardGPIOs_s gpios,
uint8_t io_expander_addr, TwoWire &i2c_bus)
: _dashboard_gpios(gpios), _io_expander(MCP23017(io_expander_addr, i2c_bus))
{
pinMode(_dashboard_gpios.START_BUTTON, INPUT_PULLUP);
pinMode(_dashboard_gpios.PRESET_BUTTON, INPUT_PULLUP);
Expand All @@ -38,6 +43,9 @@ class DashboardInterface
pinMode(_dashboard_gpios.BUTTON_2, INPUT_PULLUP);

_dash_created_millis = sys_time::hal_millis();

i2c_bus.begin();
_initIOExpander();
}

// Reading gpios
Expand All @@ -60,17 +68,29 @@ class DashboardInterface
bool imd_ok = true;

void set_dial_state(ControllerMode_e mode);

void read_ioexpander();

private:

DashboardGPIOs_s _dashboard_gpios;
DashInputState_s _dashboard_outputs;
DashInputState_s _dashboard_stored_state;

MCP23017 _io_expander;

unsigned long _dash_created_millis;
};

inline void _initIOExpander() {
_io_expander.init();

_io_expander.portMode(MCP23017Port::A, 0b00000000); // 0b0000 0000 = 0
_io_expander.portMode(MCP23017Port::B, 0b01111111); // 0b0111 1111 = 127

_io_expander.writeRegister(MCP23017Register::GPPU_B, 0xFF); // Internal pull-ups
_io_expander.writeRegister(MCP23017Register::IPOL_B, 0xFF); // Polarity (inverted)
}
};

using DashboardInterfaceInstance = etl::singleton<DashboardInterface>;

Expand Down
66 changes: 65 additions & 1 deletion lib/interfaces/src/DashboardInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "DashboardInterface.h"
#include "SharedFirmwareTypes.h"

#include "DashboardInterface.h"
#include "CANInterface.h"
#include "VCFCANInterfaceImpl.h"

#include "IOExpanderUtils.h"

/* Button reads */
DashInputState_s DashboardInterface::get_dashboard_outputs()
{
Expand Down Expand Up @@ -39,4 +42,65 @@ DashInputState_s DashboardInterface::get_dashboard_stored_state()
void DashboardInterface::sync_dashboard_stored_state()
{
_dashboard_stored_state = _dashboard_outputs;
}

void DashboardInterface::read_ioexpander() {
uint16_t data = _io_expander.read(); // read data from IOExpander
ControllerMode_e new_mode = ControllerMode_e::MODE_0; // default to mode 0

// check for value of dial
if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 0)) { // NOLINT 0 is pos of bit
new_mode = ControllerMode_e::MODE_0;
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 1)) { // NOLINT 1 is pos of bit
new_mode = ControllerMode_e::MODE_1;
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 2)) { // NOLINT 2 is pos of bit
new_mode = ControllerMode_e::MODE_2;
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 3)) { // NOLINT 3 is pos of bit
new_mode = ControllerMode_e::MODE_3;
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 4)) { // NOLINT 4 is pos of bit
new_mode = ControllerMode_e::MODE_4;
} else if (IOExpanderUtils::getBit(data, (bool) MCP23017Port::B, 5)) { // NOLINT 5 is pos of bit
new_mode = ControllerMode_e::MODE_5;
}

_dashboard_outputs.dial_state = new_mode; // set new mode

// write to 8-seg display based on current mode
switch (_dashboard_outputs.dial_state) {
case ControllerMode_e::MODE_0:
{
_io_expander.writePort(MCP23017Port::A, 0b00000010); // NOLINT 0b0000 0010 = 2
break;
}
case ControllerMode_e::MODE_1:
{
_io_expander.writePort(MCP23017Port::A, 0b01010111); // NOLINT 0b0101 0111 = 87
break;
}
case ControllerMode_e::MODE_2:
{
_io_expander.writePort(MCP23017Port::A, 0b00011000); // NOLINT 0b0001 1000 = 24
break;
}
case ControllerMode_e::MODE_3:
{
_io_expander.writePort(MCP23017Port::A, 0b00010100); // NOLINT 0b0001 0100 = 20
break;
}
case ControllerMode_e::MODE_4:
{
_io_expander.writePort(MCP23017Port::A, 0b01000101); // NOLINT 0b0100 0101 = 69
break;
}
case ControllerMode_e::MODE_5:
{
_io_expander.writePort(MCP23017Port::A, 0b00100100); // NOLINT 0b0010 0100 = 36
break;
}
default:
{
_io_expander.writePort(MCP23017Port::A, 0b11110000); // NOLINT 0b1111 0000 = 240
break;
}
}
}
11 changes: 0 additions & 11 deletions lib/interfaces/src/VCFCANInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ namespace VCFCANInterfaceImpl {
uint8_t buf[sizeof(CAN_message_t)];
memmove(buf, &msg, sizeof(msg)); // NOLINT (memory operations are fine)
VCFCANInterfaceInstance::instance().front_aux_can_rx_buffer.push_back(buf, sizeof(CAN_message_t));

// Serial.println("msg recvd");
// Serial.print("MB: "); Serial.print(msg.mb);
// Serial.print(" ID: 0x"); Serial.print(msg.id, HEX);
// Serial.print(" EXT: "); Serial.print(msg.flags.extended);
// Serial.print(" LEN: "); Serial.print(msg.len);
// Serial.print(" DATA: ");
// for ( uint8_t i = 0; i < 8; i++ ) {
// Serial.print(msg.buf[i]); Serial.print(" ");
// }
// Serial.print(" TS: "); Serial.println(msg.timestamp);
}

void vcf_recv_switch(CANInterfaces &interfaces, const CAN_message_t &msg, unsigned long millis, CANInterfaceType_e interface_type)
Expand Down
8 changes: 5 additions & 3 deletions lib/systems/include/IOExpanderUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

namespace IOExpanderUtils
{
/*
/**
IOExpander's read() only reads.
getBit() only get specified bit from previously read dataframe and does not read()
@param port 0=A; 1=B
@param data data from which to get the specified bit
@param port port from which to get the bit from (A=0, B=1)
@param bit bit number of port to get bit from
*/
bool getBit(uint16_t data, bool port, int bit);
bool getBit(uint16_t data, bool port, uint8_t bit);
}

#endif
11 changes: 6 additions & 5 deletions lib/systems/src/IOExpanderUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "IOExpanderUtils.h"

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