Skip to content

Commit

Permalink
Fix #3 redo constructor (#4)
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.
- reordered parameters software SPI constructor.
- update readme.md
- update examples
  • Loading branch information
RobTillaart authored Nov 26, 2023
1 parent 67768aa commit 80fdf08
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 153 deletions.
122 changes: 30 additions & 92 deletions AD568X.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: AD568X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// DATE: 2023-09-18
// PURPOSE: Arduino library for AD568X series Digital Analog Convertor.

Expand All @@ -17,20 +17,24 @@
#define AD568X_REG_CONTROL 0x40


AD568X::AD568X(uint8_t slaveSelect)

// HARDWARE SPI
AD568X::AD568X(uint8_t slaveSelect, __SPI_CLASS__ * mySPI)
{
_hwSPI = true;
_select = slaveSelect;
_hwSPI = true;
_mySPI = mySPI;
_value = 0;
}


AD568X::AD568X(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
// SOFTWARE SPI
AD568X::AD568X(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
{
_select = slaveSelect;
_hwSPI = false;
_mySPI = NULL;
_dataOut = spiData;
_clock = spiClock;
_select = slaveSelect;
_value = 0;
}

Expand All @@ -46,27 +50,11 @@ void AD568X::begin()

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 // software SPI
else // SOFTWARE SPI
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
Expand Down Expand Up @@ -239,48 +227,6 @@ bool AD568X::usesHWSPI()
}


// ESP32 specific
#if defined(ESP32)

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


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


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


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


void AD568X::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
mySPI->begin(clk, miso, mosi, select); // enable SPI
}

#endif


//////////////////////////////////////////////////////////////////
//
// PRIVATE
Expand All @@ -302,11 +248,11 @@ void AD568X::updateDevice(uint8_t a, uint8_t b, uint8_t c)
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(a);
mySPI->transfer(b);
mySPI->transfer(c);
mySPI->endTransaction();
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(a);
_mySPI->transfer(b);
_mySPI->transfer(c);
_mySPI->endTransaction();
}
else // Software SPI
{
Expand Down Expand Up @@ -354,71 +300,63 @@ void AD568X::swSPI_transfer(uint8_t value)
//
// DERIVED AD5681
//
AD5681R::AD5681R(uint8_t slaveSelect) : AD568X(slaveSelect)
AD5681R::AD5681R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI)
{
_type = 12;
_value = 0;
}

AD5681R::AD5681R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: AD568X(spiData, spiClock, slaveSelect)
AD5681R::AD5681R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
: AD568X(slaveSelect, spiData, spiClock)
{
_type = 12;
_value = 0;
}


/////////////////////////////////////////////////////////////////////////////
//
// DERIVED AD5682
//
AD5682R::AD5682R(uint8_t slaveSelect) : AD568X(slaveSelect)
AD5682R::AD5682R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI)
{
_type = 14;
_value = 0;
}

AD5682R::AD5682R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: AD568X(spiData, spiClock, slaveSelect)
AD5682R::AD5682R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
: AD568X(slaveSelect, spiData, spiClock)
{
_type = 14;
_value = 0;
}


/////////////////////////////////////////////////////////////////////////////
//
// DERIVED AD5683
//
AD5683R::AD5683R(uint8_t slaveSelect) : AD568X(slaveSelect)
AD5683R::AD5683R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI)
{
_type = 16;
_value = 0;
}

AD5683R::AD5683R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: AD568X(spiData, spiClock, slaveSelect)
AD5683R::AD5683R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
: AD568X(slaveSelect, spiData, spiClock)
{
_type = 16;
_value = 0;
}


/////////////////////////////////////////////////////////////////////////////
//
// DERIVED AD5683
//
AD5683::AD5683(uint8_t slaveSelect) : AD568X(slaveSelect)
AD5683::AD5683(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI)
{
_type = 16;
_value = 0;
}

AD5683::AD5683(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: AD568X(spiData, spiClock, slaveSelect)
AD5683::AD5683(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
: AD568X(slaveSelect, spiData, spiClock)
{
_type = 16;
_value = 0;
}


Expand Down
63 changes: 35 additions & 28 deletions AD568X.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
//
// FILE: AD568X.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// DATE: 2023-09-18
// PURPOSE: Arduino library for AD568X series Digital Analog Convertor.


#include "Arduino.h"
#include "SPI.h"

#define AD568X_LIB_VERSION (F("0.1.0"))
#define AD568X_LIB_VERSION (F("0.2.0"))


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


#define AD568X_PWR_NORMAL 0x00
Expand All @@ -22,8 +29,11 @@
class AD568X
{
public:
AD568X(uint8_t slaveSelect);
AD568X(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);
// HARDWARE SPI
AD568X(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD568X(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);


void begin();
uint8_t getType();
Expand Down Expand Up @@ -71,17 +81,6 @@ class AD568X
uint32_t getSPIspeed();
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 _type = 0; // # bits
Expand All @@ -100,8 +99,8 @@ class AD568X
void updateDevice(uint8_t a, uint8_t b, uint8_t c);
void swSPI_transfer(uint8_t value);

SPIClass * mySPI;
SPISettings _spi_settings;
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;

#if defined(ESP32)
bool _useHSPI = true;
Expand All @@ -124,36 +123,44 @@ class AD568X
class AD5681R : public AD568X
{
public:
AD5681R(uint8_t slaveSelect);
AD5681R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);

// SHORT WRITE ?
// HARDWARE SPI
AD5681R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD5681R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);
};


class AD5682R : public AD568X
{
public:
AD5682R(uint8_t slaveSelect);
AD5682R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);
// HARDWARE SPI
AD5682R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD5682R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);
};


class AD5683R : public AD568X
{
public:
AD5683R(uint8_t slaveSelect);
AD5683R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);
// HARDWARE SPI
AD5683R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD5683R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);

};


class AD5683 : public AD568X
{
public:
AD5683(uint8_t slaveSelect);
AD5683(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);
// HARDWARE SPI
AD5683(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
AD5683(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock);

};


// -- END OF FILE --

// -- END OF FILE --
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.0] - 2023-11-26
- refactor constructor interface - breaking changes.
- minimize conditional code. -- create SPI_CLASS macro to solve it.
- reordered parameters software SPI constructor.
- update readme.md
- update examples

----

## [0.1.0] - 2023-09-18
- initial version

Expand Down
Loading

0 comments on commit 80fdf08

Please sign in to comment.