Skip to content

Commit

Permalink
Fix #21 redo constructors (#22)
Browse files Browse the repository at this point in the history
- refactor constructor interface - breaking changes.
  - minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples
  • Loading branch information
RobTillaart authored Nov 26, 2023
1 parent 4447578 commit 0a7e4a6
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 205 deletions.
146 changes: 70 additions & 76 deletions AD520X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: AD520X.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
// VERSION: 0.3.2
// VERSION: 0.4.0
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
// URL: https://github.com/RobTillaart/AD520X
Expand All @@ -11,15 +11,30 @@
#include "AD520X.h"


// HARDWARE SPI
AD520X::AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
{
_pmCount = 6;
_select = select;
_reset = reset;
_shutdown = shutdown;
_dataOut = 255;
_clock = 255;
_hwSPI = true;
_mySPI = mySPI;
}

// SOFTWARE SPI
AD520X::AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
{
_pmCount = 6;
_select = select;
_dataOut = dataOut;
_clock = clock;
_reset = reset;
_shutdown = shutdown;
_hwSPI = (dataOut == 255) && (clock == 255);
_dataOut = dataOut;
_clock = clock;
_hwSPI = false;
_mySPI = NULL;
}


Expand All @@ -37,24 +52,8 @@ void AD520X::begin(uint8_t value)

if(_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->end();
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
}
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->end();
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
}
#else // generic hardware SPI
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
_mySPI->end();
_mySPI->begin();
delay(1);
}
else
Expand All @@ -65,6 +64,14 @@ void AD520X::begin(uint8_t value)
digitalWrite(_clock, LOW);
}

reset(value);
}


void AD520X::reset(uint8_t value)
{
digitalWrite(_reset, LOW);
digitalWrite(_reset, HIGH);
setAll(value);
}

Expand Down Expand Up @@ -172,14 +179,6 @@ void AD520X::setGroupPercentage(uint8_t mask, float percentage)
//
// OTHER
//
void AD520X::reset(uint8_t value)
{
digitalWrite(_reset, LOW);
digitalWrite(_reset, HIGH);
setAll(value);
}


uint8_t AD520X::pmCount()
{
return _pmCount;
Expand Down Expand Up @@ -227,47 +226,6 @@ bool AD520X::usesHWSPI()
};


#if defined(ESP32)

void AD520X::selectHSPI()
{
_useHSPI = true;
}


void AD520X::selectVSPI()
{
_useHSPI = false;
}


bool AD520X::usesHSPI()
{
return _useHSPI;
}


bool AD520X::usesVSPI()
{
return !_useHSPI;
}


void AD520X::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataOut = mosi;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);

mySPI->end(); // disable SPI and restart
mySPI->begin(clk, miso, mosi, select);
}

#endif


/////////////////////////////////////////////////////////////////////////////
//
// PROTECTED
Expand All @@ -277,10 +235,10 @@ void AD520X::updateDevice(uint8_t pm, uint8_t value)
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(pm);
mySPI->transfer(value);
mySPI->endTransaction();
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(pm);
_mySPI->transfer(value);
_mySPI->endTransaction();
}
else // Software SPI
{
Expand Down Expand Up @@ -309,38 +267,74 @@ void AD520X::swSPI_transfer(uint8_t val)
//
// DERIVED CLASSES
//
AD5206::AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
: AD520X(select, reset, shutdown, mySPI)
{
_pmCount = 6;
}


AD5206::AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
: AD520X(select, reset, shutdown, dataOut, clock)
{
_pmCount = 6;
}


AD5204::AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
: AD520X(select, reset, shutdown, mySPI)
{
_pmCount = 4;
}


AD5204::AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
: AD520X(select, reset, shutdown, dataOut, clock)
{
_pmCount = 4;
}


AD8403::AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
: AD520X(select, reset, shutdown, mySPI)
{
_pmCount = 4;
}


AD8403::AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
: AD520X(select, reset, shutdown, dataOut, clock)
{
_pmCount = 4;
}


AD8402::AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
: AD520X(select, reset, shutdown, mySPI)
{
_pmCount = 2;
}


AD8402::AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
: AD520X(select, reset, shutdown, dataOut, clock)
{
_pmCount = 2;
}


AD8400::AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
: AD520X(select, reset, shutdown, mySPI)
{
_pmCount = 1;
}

AD8400::AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
: AD520X(select, reset, shutdown, dataOut, clock)
{
_pmCount = 1;
}

// -- END OF FILE --

// -- END OF FILE --

67 changes: 33 additions & 34 deletions AD520X.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: AD520X.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
// VERSION: 0.3.2
// VERSION: 0.4.0
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
// URL: https://github.com/RobTillaart/AD520X
Expand All @@ -13,21 +13,30 @@
#include "SPI.h"


#define AD520X_LIB_VERSION (F("0.3.2"))

#define AD520X_LIB_VERSION (F("0.4.0"))

#ifndef AD520X_MIDDLE_VALUE
#define AD520X_MIDDLE_VALUE 128
#endif


#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif


class AD520X
{
public:
// HARDWARE SPI
AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);

void begin(uint8_t value = AD520X_MIDDLE_VALUE);

void reset(uint8_t value = AD520X_MIDDLE_VALUE);

// MONO / SINGLE
bool setValue(uint8_t pm = 0, uint8_t value = AD520X_MIDDLE_VALUE);
Expand All @@ -48,32 +57,21 @@ class AD520X
void setGroupPercentage(uint8_t mask, float percentage);


void reset(uint8_t value = AD520X_MIDDLE_VALUE);
uint8_t pmCount();
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed();


// MISC
uint8_t pmCount();
void powerOn();
void powerOff();;
bool isPowerOn();


// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed();

// debugging
// Debugging
bool usesHWSPI();

// ESP32 specific
#if defined(ESP32)
void selectHSPI();
void selectVSPI();
bool usesHSPI();
bool usesVSPI();

// to overrule ESP32 default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif


protected:
uint8_t _dataOut;
Expand All @@ -90,12 +88,8 @@ class AD520X
void updateDevice(uint8_t pm, uint8_t value);
void swSPI_transfer(uint8_t value);

SPIClass * mySPI;
SPISettings _spi_settings;

#if defined(ESP32)
bool _useHSPI = true;
#endif
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};


Expand All @@ -106,35 +100,40 @@ class AD520X
class AD5206 : public AD520X
{
public:
AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};


class AD5204 : public AD520X
{
public:
AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};


class AD8400 : public AD520X
{
public:
AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};


class AD8402 : public AD520X
{
public:
AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};


class AD8403 : public AD520X
{
public:
AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};

// -- END OF FILE --
// -- END OF FILE --
Loading

0 comments on commit 0a7e4a6

Please sign in to comment.