Skip to content

Commit 4538bd0

Browse files
committed
refactor(settings): make settings its own class
refactored to make Settings() a class within which will hold all configuration register values
1 parent 6173f11 commit 4538bd0

File tree

2 files changed

+93
-85
lines changed

2 files changed

+93
-85
lines changed

src/MCP3x6x.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,16 @@
2020
// #endif
2121

2222
MCP3x6x::MCP3x6x(const uint16_t MCP3x6x_DEVICE_TYPE, const uint8_t pinCS, SPIClass *theSPI,
23-
const uint8_t pinMOSI, const uint8_t pinMISO, const uint8_t pinCLK) {
24-
switch (MCP3x6x_DEVICE_TYPE) {
25-
case MCP3461_DEVICE_TYPE:
26-
_resolution_max = 16;
27-
_channels_max = 2;
28-
break;
29-
case MCP3462_DEVICE_TYPE:
30-
_resolution_max = 16;
31-
_channels_max = 4;
32-
break;
33-
case MCP3464_DEVICE_TYPE:
34-
_resolution_max = 16;
35-
_channels_max = 8;
36-
break;
37-
case MCP3561_DEVICE_TYPE:
38-
_resolution_max = 24;
39-
_channels_max = 2;
40-
break;
41-
case MCP3562_DEVICE_TYPE:
42-
_resolution_max = 24;
43-
_channels_max = 4;
44-
break;
45-
case MCP3564_DEVICE_TYPE:
46-
_resolution_max = 24;
47-
_channels_max = 8;
48-
break;
49-
default:
50-
break;
51-
}
52-
53-
// settings.id = MCP3x6x_DEVICE_TYPE;
54-
23+
const uint8_t pinMOSI, const uint8_t pinMISO, const uint8_t pinCLK)
24+
: settings(MCP3x6x_DEVICE_TYPE) {
5525
_spi = theSPI;
5626
_pinMISO = pinMISO;
5727
_pinMOSI = pinMOSI;
5828
_pinCLK = pinCLK;
5929
_pinCS = pinCS;
6030

61-
_resolution = _resolution_max;
62-
_channel_mask |= 0xff << _channels_max; // todo use this one
31+
_resolution = settings.getMaxResolution();
32+
_channel_mask |= 0xff << settings.getChannelCount(); // todo use this one
6333
}
6434

6535
MCP3x6x::MCP3x6x(const uint8_t pinIRQ, const uint8_t pinMCLK, const uint16_t MCP3x6x_DEVICE_TYPE,
@@ -123,7 +93,7 @@ bool MCP3x6x::begin(uint16_t channelmask, float vref) {
12393
MCP3x6x::status_t MCP3x6x::read(Adcdata *data) {
12494
size_t s = 0;
12595

126-
switch (_resolution_max) {
96+
switch (settings.getMaxResolution()) {
12797
case 16:
12898
s = settings.config3.data_format == data_format::SGN_DATA ? 2 : 4;
12999
break;
@@ -170,7 +140,7 @@ void MCP3x6x::lock(uint8_t key) {
170140
}
171141

172142
void MCP3x6x::unlock() {
173-
settings.lock.raw = _DEFAULT_LOCK;
143+
settings.lock.raw = settings.DEFAULT.LOCK;
174144
_status = write(settings.lock);
175145
}
176146

@@ -249,7 +219,7 @@ float MCP3x6x::getReference() { return _reference; }
249219

250220
// returns signed ADC value from raw data
251221
int32_t MCP3x6x::_getValue(uint32_t raw) {
252-
switch (_resolution_max) {
222+
switch (settings.getMaxResolution()) {
253223
case 16:
254224
switch (settings.config3.data_format) {
255225
case (data_format::SGN_DATA_ZERO):
@@ -338,7 +308,7 @@ int32_t MCP3x6x::analogReadDifferential(mux pinP, mux pinN) {
338308
}
339309

340310
void MCP3x6x::analogReadResolution(size_t bits) {
341-
if (bits <= _resolution_max) {
311+
if (bits <= settings.getMaxResolution()) {
342312
_resolution = bits;
343313
}
344314
}

src/MCP3x6x.hpp

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,6 @@
8787
*
8888
*/
8989
class MCP3x6x {
90-
const uint8_t _DEFAULT_CONFIG0 = 0xC0; //!< default value
91-
const uint8_t _DEFAULT_CONFIG1 = 0x0C; //!< default value
92-
const uint8_t _DEFAULT_CONFIG2 = 0x8B; //!< default value
93-
const uint8_t _DEFAULT_CONFIG3 = 0x00; //!< default value
94-
const uint8_t _DEFAULT_IRQ = 0x73; //!< default value
95-
const uint8_t _DEFAULT_MUX = 0x01; //!< default value
96-
const uint8_t _DEFAULT_SCAN[3] = {0x00, 0x00, 0x00}; //!< default value
97-
const uint8_t _DEFAULT_TIMER[3] = {0x00, 0x00, 0x00}; //!< default value
98-
const uint8_t _DEFAULT_OFFSET[3] = {0x00, 0x00, 0x00}; //!< default value
99-
const uint8_t _DEFAULT_GAIN[3] = {0x80, 0x00, 0x00}; //!< default value
100-
const uint8_t _DEFAULT_RESERVED1[3] = {0x90, 0x00, 0x00}; //!< default value
101-
const uint8_t _DEFAULT_RESERVED2 = 0x50; //!< default value
102-
const uint8_t _DEFAULT_LOCK = 0xA5; //!< default value
103-
const uint8_t _DEFAULT_CRCCFG[2] = {0x00, 0x00}; //!< default value
104-
const uint8_t _DEFAULTS[27] = {_DEFAULT_CONFIG0, _DEFAULT_CONFIG1, _DEFAULT_CONFIG2,
105-
_DEFAULT_CONFIG3, _DEFAULT_IRQ, _DEFAULT_MUX,
106-
*_DEFAULT_SCAN, *_DEFAULT_TIMER, *_DEFAULT_OFFSET,
107-
*_DEFAULT_GAIN, *_DEFAULT_RESERVED1, _DEFAULT_RESERVED2,
108-
_DEFAULT_LOCK, (uint16_t)0x0000, *_DEFAULT_CRCCFG};
109-
11090
typedef union __attribute__((__packed__)) {
11191
struct {
11292
struct {
@@ -134,8 +114,7 @@ class MCP3x6x {
134114

135115
bool _differential = false;
136116
float _reference = 3.3;
137-
size_t _resolution, _resolution_max;
138-
size_t _channels_max;
117+
size_t _resolution;
139118
uint16_t _channel_mask;
140119
const uint8_t _channelID[16] = {MCP_CH0, MCP_CH1, MCP_CH2, MCP_CH3, MCP_CH4, MCP_CH5,
141120
MCP_CH6, MCP_CH7, MCP_DIFFA, MCP_DIFFB, MCP_DIFFC, MCP_DIFFD,
@@ -326,6 +305,7 @@ class MCP3x6x {
326305
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1576710>MCP346x.pdf</a>
327306
*/
328307
typedef union Config0 {
308+
Config0(uint8_t data) : raw(data) {}
329309
struct {
330310
enum adc_mode adc : 2; //!< ADC Operating Mode Selection
331311
enum cs_sel bias : 2; //!< Current Source/Sink Selection Bits for Sensor Bias
@@ -343,6 +323,7 @@ class MCP3x6x {
343323
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269089>MCP346x.pdf</a>
344324
*/
345325
typedef union Config1 {
326+
Config1(uint8_t data) : raw(data) {}
346327
struct {
347328
uint8_t : 2; //!< reserved
348329
enum osr osr : 4; //!< Oversampling Ratio for Delta-Sigma A/D Conversion
@@ -358,6 +339,7 @@ class MCP3x6x {
358339
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269283>MCP346x.pdf</a>
359340
*/
360341
typedef union Config2 {
342+
Config2(uint8_t data) : raw(data) {}
361343
struct {
362344
uint8_t : 2; //!< reserved // Should always be equal to ‘11’
363345
bool az_mu : 1; //!< Auto-Zeroing MUX Setting
@@ -374,6 +356,7 @@ class MCP3x6x {
374356
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269504>MCP346x.pdf</a>
375357
*/
376358
typedef union Config3 {
359+
Config3(uint8_t data) : raw(data) {}
377360
struct {
378361
bool en_gaincal : 1; //!< Enable Digital Gain Calibration
379362
bool en_offcal : 1; //!< Enable Digital Offset Calibration
@@ -392,6 +375,7 @@ class MCP3x6x {
392375
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269747>MCP346x.pdf</a>
393376
*/
394377
typedef union Irq {
378+
Irq(uint8_t data) : raw(data) {}
395379
struct {
396380
bool en_stp : 1; //!< Enable Conversion Start Interrupt Output
397381
bool en_fastcmd : 1; //!< Enable Fast Commands in the COMMAND Byte
@@ -411,11 +395,6 @@ class MCP3x6x {
411395
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1273028>MCP346x.pdf</a>
412396
*/
413397
typedef union Mux {
414-
/**
415-
* @brief Construct a new Mux object
416-
*
417-
* @param data
418-
*/
419398
Mux(uint8_t data) : raw(data) {}
420399
struct {
421400
enum mux vin_minus : 4; //!< MUX_VIN- Input Selection
@@ -431,6 +410,7 @@ class MCP3x6x {
431410
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270252>MCP346x.pdf</a>
432411
*/
433412
typedef union Scan {
413+
Scan(const uint8_t data[3]) : raw{data[0], data[1], data[2]} {}
434414
struct {
435415
union {
436416
struct {
@@ -457,6 +437,7 @@ class MCP3x6x {
457437
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270583>MCP346x.pdf</a>
458438
*/
459439
typedef union Timer {
440+
Timer(const uint8_t data[3]) : raw{data[0], data[1], data[2]} {}
460441
uint8_t raw[3]; //!< Selection Bits for the Time Interval Between Two Consecutive Scan Cycles
461442
} timer_t;
462443

@@ -467,6 +448,7 @@ class MCP3x6x {
467448
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270742>MCP346x.pdf</a>
468449
*/
469450
typedef union Offset {
451+
Offset(const uint8_t data[3]) : raw{data[0], data[1], data[2]} {}
470452
uint8_t raw[3]; //!< Offset Error Digital Calibration Code (two’s complement, MSb first coding)
471453
} offset_t;
472454

@@ -477,6 +459,7 @@ class MCP3x6x {
477459
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270900>MCP346x.pdf</a>
478460
*/
479461
typedef union Gain {
462+
Gain(const uint8_t data[3]) : raw{data[0], data[1], data[2]} {}
480463
uint8_t raw[3]; //!< Gain Error Digital Calibration Code (unsigned, MSb first coding)
481464
} gain_t;
482465

@@ -487,6 +470,7 @@ class MCP3x6x {
487470
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1271641>MCP346x.pdf</a>
488471
*/
489472
typedef union Lock {
473+
Lock(uint8_t data) : raw(data) {}
490474
uint8_t raw; //!< Write Access Password Entry Code
491475
} lock_t;
492476

@@ -497,32 +481,86 @@ class MCP3x6x {
497481
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1272118>MCP346x.pdf</a>
498482
*/
499483
typedef union Crccfg {
500-
uint8_t raw[2]; //!< CRC-16 Checksum Value
484+
Crccfg(const uint8_t data[2]) : raw{data[0], data[1]} {}
485+
union {
486+
// uint16_t val;
487+
uint8_t raw[2]; //!< CRC-16 Checksum Value
488+
};
501489
} crccfg_t;
502490

503491
/**
504492
* @brief settings
505493
*
506494
*/
507-
struct Settings {
495+
class Settings {
496+
size_t getMaxResolution() {
497+
switch (id) {
498+
case MCP3461_DEVICE_TYPE:
499+
case MCP3462_DEVICE_TYPE:
500+
case MCP3464_DEVICE_TYPE:
501+
return 16;
502+
case MCP3561_DEVICE_TYPE:
503+
case MCP3562_DEVICE_TYPE:
504+
case MCP3564_DEVICE_TYPE:
505+
return 24;
506+
default:
507+
return 0;
508+
}
509+
}
510+
511+
size_t getChannelCount() {
512+
switch (id) {
513+
case MCP3461_DEVICE_TYPE:
514+
case MCP3561_DEVICE_TYPE:
515+
return 2;
516+
case MCP3462_DEVICE_TYPE:
517+
case MCP3562_DEVICE_TYPE:
518+
return 4;
519+
case MCP3464_DEVICE_TYPE:
520+
case MCP3564_DEVICE_TYPE:
521+
return 8;
522+
default:
523+
return 0;
524+
}
525+
}
526+
508527
public:
509-
config0_t config0; //!< register setting
510-
config1_t config1; //!< register setting
511-
config2_t config2; //!< register setting
512-
config3_t config3; //!< register setting
513-
irq_t irq; //!< register setting
514-
mux_t mux; //!< register setting
515-
scan_t scan; //!< register setting
516-
timer_t timer; //!< register setting
517-
offset_t offsetcal; //!< register setting
518-
gain_t gaincal; //!< register setting
519-
uint8_t reserverd1[3] = {0, 0, 0}; //!< register setting
520-
uint8_t reserverd2 = 0; //!< register setting
521-
lock_t lock; //!< register setting
522-
uint16_t id = 0; //!< register setting
523-
crccfg_t crccfg; //!< register setting
524-
525-
Settings() : mux(0x01) {}
528+
struct {
529+
const uint8_t CONFIG0 = 0xC0; //!< default value
530+
const uint8_t CONFIG1 = 0x0C; //!< default value
531+
const uint8_t CONFIG2 = 0x8B; //!< default value
532+
const uint8_t CONFIG3 = 0x00; //!< default value
533+
const uint8_t IRQ = 0x73; //!< default value
534+
const uint8_t MUX = 0x01; //!< default value
535+
const uint8_t SCAN[3] = {0x00, 0x00, 0x00}; //!< default value
536+
const uint8_t TIMER[3] = {0x00, 0x00, 0x00}; //!< default value
537+
const uint8_t OFFSET[3] = {0x00, 0x00, 0x00}; //!< default value
538+
const uint8_t GAIN[3] = {0x80, 0x00, 0x00}; //!< default value
539+
// const uint8_t RESERVED1[3] = {0x90, 0x00, 0x00}; //!< default value
540+
// const uint8_t RESERVED2 = 0x50; //!< default value
541+
const uint8_t LOCK = 0xA5; //!< default value
542+
const uint8_t CRCCFG[2] = {0x00, 0x00}; //!< default value
543+
} DEFAULT;
544+
545+
config0_t config0 = DEFAULT.CONFIG0; //!< register setting
546+
config1_t config1 = DEFAULT.CONFIG1; //!< register setting
547+
config2_t config2 = DEFAULT.CONFIG2; //!< register setting
548+
config3_t config3 = DEFAULT.CONFIG3; //!< register setting
549+
irq_t irq = DEFAULT.IRQ; //!< register setting
550+
mux_t mux = DEFAULT.MUX; //!< register setting
551+
scan_t scan = DEFAULT.SCAN; //!< register setting
552+
timer_t timer = DEFAULT.TIMER; //!< register setting
553+
offset_t offsetcal = DEFAULT.OFFSET; //!< register setting
554+
gain_t gaincal = DEFAULT.GAIN; //!< register setting
555+
// const uint8_t reserverd1[3] = _DEFAULT.RESERVED1; //!< register setting
556+
// const uint8_t reserverd2 = _DEFAULT.RESERVED2; //!< register setting
557+
uint16_t id; //!< register setting
558+
lock_t lock = DEFAULT.LOCK; //!< register setting
559+
crccfg_t crccfg = DEFAULT.CRCCFG; //!< register setting
560+
561+
Settings(const uint16_t MCP3x6x_DEVICE_TYPE) : id(MCP3x6x_DEVICE_TYPE) {}
562+
563+
friend MCP3x6x;
526564
} settings;
527565

528566
/**
@@ -651,7 +689,7 @@ class MCP3x6x {
651689
* @return status_t
652690
*/
653691
inline status_t reset() {
654-
// memcpy(settings, _DEFAULTS, 27);
692+
// todo: reset settings values;
655693
return _fastcmd(MCP3x6x_CMD_RESET);
656694
}
657695

0 commit comments

Comments
 (0)