GY-63_MS5611/libraries/AD520X/AD520X.h

140 lines
3.4 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: AD520X.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
2024-01-19 07:42:51 -05:00
// VERSION: 0.5.0
2022-10-15 13:46:24 -04:00
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/AD520X
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
2024-01-19 07:42:51 -05:00
#define AD520X_LIB_VERSION (F("0.5.0"))
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
2024-01-19 07:42:51 -05:00
#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
2023-11-26 11:07:23 -05:00
#endif
2021-01-29 06:31:58 -05:00
class AD520X
{
public:
2023-11-26 11:07:23 -05:00
// HARDWARE SPI
AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
2022-10-15 13:46:24 -04:00
AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
2021-01-29 06:31:58 -05:00
2021-12-10 08:12:54 -05:00
void begin(uint8_t value = AD520X_MIDDLE_VALUE);
2023-11-26 11:07:23 -05:00
void reset(uint8_t value = AD520X_MIDDLE_VALUE);
2023-01-10 14:37:57 -05:00
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);
2023-01-10 14:37:57 -05:00
uint8_t getValue(uint8_t pm = 0);
2022-09-19 04:24:53 -04:00
// STEREO / DOUBLE
bool setValue(uint8_t pmA, uint8_t pmB, uint8_t value);
2023-01-10 14:37:57 -05:00
// GROUP
2021-12-10 08:12:54 -05:00
void setAll(uint8_t value = AD520X_MIDDLE_VALUE);
2023-01-10 14:37:57 -05:00
void setGroupValue(uint8_t mask, uint8_t value = AD520X_MIDDLE_VALUE);
2021-10-16 14:58:54 -04:00
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);
2023-01-10 14:37:57 -05:00
float getPercentage(uint8_t pm = 0);
2022-09-19 04:24:53 -04:00
// STEREO / DOUBLE
bool setPercentage(uint8_t pmA, uint8_t pmB, float percentage);
2023-01-10 14:37:57 -05:00
// GROUP
void setGroupPercentage(uint8_t mask, float percentage);
2021-01-29 06:31:58 -05:00
2023-11-26 11:07:23 -05:00
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed();
2023-01-10 14:37:57 -05:00
2023-11-26 11:07:23 -05:00
// MISC
uint8_t pmCount();
2023-01-10 14:37:57 -05:00
void powerOn();
void powerOff();;
bool isPowerOn();
2021-01-29 06:31:58 -05:00
2022-09-19 04:24:53 -04:00
2023-11-26 11:07:23 -05:00
// Debugging
2023-01-10 14:37:57 -05:00
bool usesHWSPI();
2021-08-22 04:47:15 -04:00
2023-01-10 14:37:57 -05:00
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;
2023-01-10 14:37:57 -05:00
bool _hwSPI;
2021-08-22 04:47:15 -04:00
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);
2023-11-26 11:07:23 -05:00
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
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:
2023-11-26 11:07:23 -05:00
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);
2021-01-29 06:31:58 -05:00
};
2021-08-22 04:47:15 -04:00
2021-01-29 06:31:58 -05:00
class AD5204 : public AD520X
{
public:
2023-11-26 11:07:23 -05:00
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);
2021-01-29 06:31:58 -05:00
};
2021-08-22 04:47:15 -04:00
2021-01-29 06:31:58 -05:00
class AD8400 : public AD520X
{
public:
2023-11-26 11:07:23 -05:00
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);
2021-01-29 06:31:58 -05:00
};
2021-08-22 04:47:15 -04:00
2021-01-29 06:31:58 -05:00
class AD8402 : public AD520X
{
public:
2023-11-26 11:07:23 -05:00
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);
2021-01-29 06:31:58 -05:00
};
2021-08-22 04:47:15 -04:00
2021-01-29 06:31:58 -05:00
class AD8403 : public AD520X
{
public:
2023-11-26 11:07:23 -05:00
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);
2021-01-29 06:31:58 -05:00
};
2023-11-26 11:07:23 -05:00
// -- END OF FILE --