Skip to content

Commit

Permalink
ongoing refactore
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdyscout committed Feb 28, 2024
1 parent f99511a commit c49e9cd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 82 deletions.
86 changes: 55 additions & 31 deletions src/MCP3x6x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,41 @@
MCP3x6x::MCP3x6x(const uint16_t MCP3x6x_DEVICE_TYPE, const uint8_t pinCS, SPIClass *theSPI,
const uint8_t pinMOSI, const uint8_t pinMISO, const uint8_t pinCLK)
: settings(MCP3x6x_DEVICE_TYPE) {
_spi = theSPI;
_pinMISO = pinMISO;
_pinMOSI = pinMOSI;
_pinCLK = pinCLK;
_pinCS = pinCS;

_resolution = getMaxResolution();
_channel_mask |= 0xff << getChannelCount(); // todo use this one
_spi = theSPI;
_pinMISO = pinMISO;
_pinMOSI = pinMOSI;
_pinCLK = pinCLK;
_pinCS = pinCS;

switch (MCP3x6x_DEVICE_TYPE) {
case MCP3461_DEVICE_TYPE:
case MCP3462_DEVICE_TYPE:
case MCP3464_DEVICE_TYPE:
_resolution = 16;
break;
case MCP3561_DEVICE_TYPE:
case MCP3562_DEVICE_TYPE:
case MCP3564_DEVICE_TYPE:
_resolution = 24;
break;
}

switch (MCP3x6x_DEVICE_TYPE) {
case MCP3461_DEVICE_TYPE:
case MCP3561_DEVICE_TYPE:
_channel_count = 1;
break;
case MCP3462_DEVICE_TYPE:
case MCP3562_DEVICE_TYPE:
_channel_count = 2;
break;
case MCP3564_DEVICE_TYPE:
case MCP3464_DEVICE_TYPE:
_channel_count = 4;
break;
}

_channel_mask |= 0xff << _channel_count; // todo use this one
}

MCP3x6x::MCP3x6x(const uint8_t pinIRQ, const uint8_t pinMCLK, const uint16_t MCP3x6x_DEVICE_TYPE,
Expand All @@ -55,20 +82,16 @@ MCP3x6x::status_t MCP3x6x::_transfer(uint8_t *data, uint8_t addr, size_t size) {
noInterrupts();
digitalWrite(_pinCS, LOW);
_status.raw = _spi->transfer(addr);
#ifdef ARDUINO_ARCH_ESP32
_spi->writeBytes(data, size);
#else
_spi->transfer(data, size);
#endif
digitalWrite(_pinCS, HIGH);
interrupts();
_spi->endTransaction();

return _status;
}

bool MCP3x6x::begin(MCP3x6x::MCPSettings settings) {
// memccpy(&settings, &settings, 0, sizeof(MCP3x6x::MCPSettings::DEFAULTS));
bool MCP3x6x::begin(MCP3x6x::MCPSettings set) {
memccpy(&settings, &set, 0, sizeof(MCP3x6x::MCPSettings));

pinMode(_pinCS, OUTPUT);
digitalWrite(_pinCS, HIGH);
Expand All @@ -84,26 +107,27 @@ bool MCP3x6x::begin(MCP3x6x::MCPSettings settings) {
#endif

reset();
// write(settings);
return status_por();
}

MCP3x6x::status_t MCP3x6x::read(Adcdata *data) {
size_t s = 0;

switch (getMaxResolution()) {
case 16:
s = settings.registers.config3.data_format == data_format::SGN_DATA ? 2 : 4;
break;
case 24:
s = settings.registers.config3.data_format == data_format::SGN_DATA ? 3 : 4;
break;
}

size_t s = 4;
/*
switch (_resolution) {
case 16:
s = settings.registers.config3.data_format == data_format::SGN_DATA ? 2 : 4;
break;
case 24:
s = settings.registers.config3.data_format == data_format::SGN_DATA ? 3 : 4;
break;
}
*/
uint8_t buffer[s];

while (status_dr()) {
_transfer(buffer, MCP3x6x_CMD_SREAD | MCP3x6x_ADR_ADCDATA, s);
}
// while (status_dr()) {
_transfer(buffer, MCP3x6x_CMD_SREAD | MCP3x6x_ADR_ADCDATA, s);
// }

_reverse_array(buffer, s);

Expand Down Expand Up @@ -141,7 +165,7 @@ void MCP3x6x::setDataFormat(data_format format) {
case data_format::ID_SGNEXT_DATA:
break;
default:
_resolution = -1;
// _resolution = -1;
break;
}
}
Expand Down Expand Up @@ -194,7 +218,7 @@ float MCP3x6x::getReference() { return _reference; }

// returns signed ADC value from raw data
int32_t MCP3x6x::_getValue(uint32_t raw) {
switch (getMaxResolution()) {
switch (_resolution) {
case 16:
switch (settings.registers.config3.data_format) {
case (data_format::SGN_DATA_ZERO):
Expand Down Expand Up @@ -277,7 +301,7 @@ int32_t MCP3x6x::analogReadDifferential(mux pinP, mux pinN) {
}

void MCP3x6x::analogReadResolution(size_t bits) {
if (bits <= getMaxResolution()) {
if (bits <= _resolution) {
_resolution = bits;
}
}
Expand Down
71 changes: 24 additions & 47 deletions src/MCP3x6x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MCP3x6x {
uint8_t _pinMCLK, _pinIRQ;

float _reference = 3.3;
size_t _resolution;
size_t _resolution, _channel_count;
uint16_t _channel_mask;
const uint8_t _channelID[16] = {MCP_CH0, MCP_CH1, MCP_CH2, MCP_CH3, MCP_CH4, MCP_CH5,
MCP_CH6, MCP_CH7, MCP_DIFFA, MCP_DIFFB, MCP_DIFFC, MCP_DIFFD,
Expand Down Expand Up @@ -518,44 +518,13 @@ class MCP3x6x {
offset_t offset;
gain_t gain;
uint8_t reserved1;
uint8_t id;
lock_t lock;
uint16_t id;
uint8_t crccfg;
} registers;
uint8_t raw[27];
} settings;

size_t getMaxResolution() {
switch (settings.registers.id) {
case MCP3461_DEVICE_TYPE:
case MCP3462_DEVICE_TYPE:
case MCP3464_DEVICE_TYPE:
return 16;
case MCP3561_DEVICE_TYPE:
case MCP3562_DEVICE_TYPE:
case MCP3564_DEVICE_TYPE:
return 24;
default:
return 0;
}
}

size_t getChannelCount() {
switch (settings.registers.id) {
case MCP3461_DEVICE_TYPE:
case MCP3561_DEVICE_TYPE:
return 2;
case MCP3462_DEVICE_TYPE:
case MCP3562_DEVICE_TYPE:
return 4;
case MCP3464_DEVICE_TYPE:
case MCP3564_DEVICE_TYPE:
return 8;
default:
return 0;
}
}

/**
* @brief structure with latest value per channel
*
Expand Down Expand Up @@ -800,18 +769,22 @@ class MCP3x6x {
return _transfer(data.raw, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CRCCFG, 2);
}

/*
inline status_t write(Settings data) {
return _transfer((uint8_t)data, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0, 27);
/**
* @brief write all settings register to ADC
*
* @param data
* @return status_t
*/
inline status_t write(MCPSettings data) {
return _transfer(data.raw, MCP3x6x_CMD_IWRITE | MCP3x6x_ADR_CONFIG0, 27);
}
* /

/**
* @brief read register ADCDATA from ADC
*
* @param data
* @return status_t
*/
* @brief read register ADCDATA from ADC
*
* @param data
* @return status_t
*/
status_t read(Adcdata *data);

/**
Expand Down Expand Up @@ -934,11 +907,15 @@ class MCP3x6x {
return _transfer(data.raw, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CRCCFG, 2);
}

/*
inline status_t read(Settings data) {
return _transfer((uint8_t)data, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0, 27);
}
*/
/**
* @brief read all settings register from ADC
*
* @param data
* @return status_t
*/
inline status_t read(MCPSettings data) {
return _transfer(data.raw, MCP3x6x_CMD_IREAD | MCP3x6x_ADR_CONFIG0, 27);
}

/**
* @brief handler
Expand Down
4 changes: 0 additions & 4 deletions test/test_MCP3462/test_MCP3462.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ void loop(void) {}
int runUnityTests(void) {
UNITY_BEGIN();

RUN_TEST(test_Resolution);

return UNITY_END();
}

Expand Down Expand Up @@ -68,5 +66,3 @@ void setUp(void) { mcp.reset(); }
void tearDown(void) {}

// actual test cases

void test_Resolution(void) { TEST_ASSERT_EQUAL_INT(16, mcp.settings.getMaxResolution()); }

0 comments on commit c49e9cd

Please sign in to comment.