2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: AD520X.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-07-24
|
2022-09-19 04:24:53 -04:00
|
|
|
// VERSION: 0.2.2
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers (+ older AD8400, AD8402, AD8403)
|
|
|
|
// URL: https://github.com/RobTillaart/AD520X
|
|
|
|
//
|
|
|
|
// HISTORY:
|
|
|
|
// see AD520X.cpp file
|
|
|
|
//
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "SPI.h"
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
#define AD520X_LIB_VERSION (F("0.2.2"))
|
2021-12-10 08:12:54 -05:00
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
|
|
|
|
#ifndef AD520X_MIDDLE_VALUE
|
2021-12-10 08:12:54 -05:00
|
|
|
#define AD520X_MIDDLE_VALUE 128
|
2022-09-19 04:24:53 -04:00
|
|
|
#endif
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
class AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
|
|
|
|
2021-12-10 08:12:54 -05:00
|
|
|
void begin(uint8_t value = AD520X_MIDDLE_VALUE);
|
2022-09-19 04:24:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
// MONO / SINGLE
|
2021-12-10 08:12:54 -05:00
|
|
|
bool setValue(uint8_t pm = 0, uint8_t value = AD520X_MIDDLE_VALUE);
|
2022-09-19 04:24:53 -04:00
|
|
|
// STEREO / DOUBLE
|
|
|
|
bool setValue(uint8_t pmA, uint8_t pmB, uint8_t value);
|
2021-12-10 08:12:54 -05:00
|
|
|
void setAll(uint8_t value = AD520X_MIDDLE_VALUE);
|
2021-10-16 14:58:54 -04:00
|
|
|
uint8_t getValue(uint8_t pm = 0);
|
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
// MONO / SINGLE
|
2021-10-16 14:58:54 -04:00
|
|
|
bool setPercentage(uint8_t pm = 0, float percentage = 50);
|
2022-09-19 04:24:53 -04:00
|
|
|
// STEREO / DOUBLE
|
|
|
|
bool setPercentage(uint8_t pmA, uint8_t pmB, float percentage);
|
2021-10-16 14:58:54 -04:00
|
|
|
float getPercentage(uint8_t pm = 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
|
2021-12-10 08:12:54 -05:00
|
|
|
void reset(uint8_t value = AD520X_MIDDLE_VALUE);
|
2021-10-16 14:58:54 -04:00
|
|
|
uint8_t pmCount() { return _pmCount; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void powerOn() { digitalWrite(_shutdown, LOW); };
|
|
|
|
void powerOff() { digitalWrite(_shutdown, HIGH); };
|
2021-12-10 08:12:54 -05:00
|
|
|
void powerDown() { powerOff(); }; // obsolete, remove in 0.3.0
|
2021-01-29 06:31:58 -05:00
|
|
|
bool isPowerOn() { return digitalRead(_shutdown) == LOW; };
|
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
// speed in Hz
|
|
|
|
void setSPIspeed(uint32_t speed);
|
|
|
|
uint32_t getSPIspeed() { return _SPIspeed; };
|
|
|
|
|
|
|
|
// debugging
|
|
|
|
bool usesHWSPI() { return _hwSPI; };
|
|
|
|
|
|
|
|
// ESP32 specific
|
|
|
|
#if defined(ESP32)
|
|
|
|
void selectHSPI() { _useHSPI = true; };
|
|
|
|
void selectVSPI() { _useHSPI = false; };
|
|
|
|
bool usesHSPI() { return _useHSPI; };
|
|
|
|
bool usesVSPI() { return !_useHSPI; };
|
|
|
|
|
|
|
|
// to overrule ESP32 default hardware pins
|
|
|
|
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
|
|
|
|
#endif
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
protected:
|
2021-08-22 04:47:15 -04:00
|
|
|
uint8_t _dataOut;
|
|
|
|
uint8_t _clock;
|
|
|
|
uint8_t _select;
|
|
|
|
uint8_t _reset;
|
|
|
|
uint8_t _shutdown;
|
|
|
|
bool _hwSPI = 3;
|
|
|
|
uint32_t _SPIspeed = 16000000;
|
|
|
|
|
|
|
|
uint8_t _value[6];
|
|
|
|
uint8_t _pmCount = 6;
|
|
|
|
|
2022-09-19 04:24:53 -04:00
|
|
|
void updateDevice(uint8_t pm, uint8_t value);
|
2021-08-22 04:47:15 -04:00
|
|
|
void swSPI_transfer(uint8_t value);
|
|
|
|
|
|
|
|
SPIClass * mySPI;
|
|
|
|
SPISettings _spi_settings;
|
|
|
|
|
|
|
|
#if defined(ESP32)
|
|
|
|
bool _useHSPI = true;
|
|
|
|
#endif
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2021-08-22 04:47:15 -04:00
|
|
|
//
|
|
|
|
// DERIVED CLASSES
|
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
class AD5206 : public AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
};
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class AD5204 : public AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
};
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class AD8400 : public AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
};
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class AD8402 : public AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
};
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class AD8403 : public AD520X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
};
|
|
|
|
|
|
|
|
// -- END OF FILE --
|