-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AD520X.h
144 lines (109 loc) · 3.5 KB
/
AD520X.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
//
// FILE: AD520X.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-24
// VERSION: 0.5.1
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers
// (+ AD8400, AD8402, AD8403)
// URL: https://github.com/RobTillaart/AD520X
#include "Arduino.h"
#include "SPI.h"
#define AD520X_LIB_VERSION (F("0.5.1"))
#ifndef AD520X_MIDDLE_VALUE
#define AD520X_MIDDLE_VALUE 128
#endif
#ifndef __SPI_CLASS__
// MBED must be tested before RP2040
#if defined(ARDUINO_ARCH_MBED)
#define __SPI_CLASS__ SPIClass
#elif defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#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);
uint8_t getValue(uint8_t pm = 0);
// STEREO / DOUBLE
bool setValue(uint8_t pmA, uint8_t pmB, uint8_t value);
// GROUP
void setAll(uint8_t value = AD520X_MIDDLE_VALUE);
void setGroupValue(uint8_t mask, uint8_t value = AD520X_MIDDLE_VALUE);
// MONO / SINGLE
bool setPercentage(uint8_t pm = 0, float percentage = 50);
float getPercentage(uint8_t pm = 0);
// STEREO / DOUBLE
bool setPercentage(uint8_t pmA, uint8_t pmB, float percentage);
// GROUP
void setGroupPercentage(uint8_t mask, float percentage);
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed();
// MISC
uint8_t pmCount();
void powerOn();
void powerOff();;
bool isPowerOn();
// Debugging
bool usesHWSPI();
protected:
uint8_t _dataOut;
uint8_t _clock;
uint8_t _select;
uint8_t _reset;
uint8_t _shutdown;
bool _hwSPI;
uint32_t _SPIspeed = 16000000;
uint8_t _value[6];
uint8_t _pmCount = 6;
void updateDevice(uint8_t pm, uint8_t value);
void swSPI_transfer(uint8_t value);
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};
/////////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES
//
class AD5206 : public AD520X
{
public:
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, __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, __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, __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, __SPI_CLASS__ * mySPI = &SPI);
AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
};
// -- END OF FILE --