-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFastX9CXXX.h
234 lines (198 loc) · 4.78 KB
/
FastX9CXXX.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
///
/// Created for personal use, use it at your own risk and benefit.
/// https://github.com/GitMoDu/FastX9CXXX
///
#ifndef _FASTX9CXXX_h
#define _FASTX9CXXX_h
#include <Arduino.h>
class X9CXXX
{
public:
static const uint8_t X9_STEPS = 100; //100 Wiper Tap Points
static const uint32_t X9C102_RESISTANCE = 1000; //X9C102 = 1kOhm
static const uint32_t X9C103_RESISTANCE = 10000; //X9C103 = 10kOhm
static const uint32_t X9C503_RESISTANCE = 50000; //X9C503 = 50kOhm
static const uint32_t X9C104_RESISTANCE = 100000; //X9C104 = 100kOhm
};
template<const uint32_t Resistance>
class FastX9CXXX
{
private:
//In microseconds.
const uint32_t NCS_TO_NINC_SETUP = 1;
const uint32_t NINC_HIGH_TO_UND_CHANGE = 1;
const uint32_t UND_TO_NINC_SETUP = 3;
const uint32_t NINC_LOW_PERIOD = 1;
const uint32_t NINC_HIGH_PERIOD = 1;
const uint32_t NINC_INACTIVE_TO_NCS_INACTIVE = 1;
const uint32_t NCS_DESELECT_TIME_STORE = 20000;
const uint32_t NCS_DESELECT_TIME_NO_STORE = 1;
const uint32_t NINC_TO_VWRW_CHANGE = 100;
const uint32_t NINC_CYCLE_TIME = 2;
const uint32_t POWER_UP_TO_WIPER_STABLE = 500;
private:
constexpr uint32_t GetResistanceStep()
{
return Resistance / X9CXXX::X9_STEPS;
}
private:
uint8_t PinCS = UINT8_MAX;
uint8_t PinUD = UINT8_MAX;
uint8_t PinINC = UINT8_MAX;
uint8_t CurrentStep = 0;
const uint32_t ResistanceStep = GetResistanceStep();
public:
uint32_t GetEstimatedResistance()
{
return CurrentStep * ResistanceStep;
}
FastX9CXXX()
{}
FastX9CXXX(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin)
{
Setup(csPin, udPin, incPin);
}
const bool Setup(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin)
{
PinCS = csPin;
PinUD = udPin;
PinINC = incPin;
if (PinCS == UINT8_MAX || PinUD == UINT8_MAX || PinINC == UINT8_MAX)
{
return false;
}
pinMode(PinCS, OUTPUT);
pinMode(PinUD, OUTPUT);
pinMode(PinINC, OUTPUT);
digitalWrite(PinCS, LOW);
digitalWrite(PinUD, LOW);
digitalWrite(PinINC, HIGH);
return true;
}
// Resets the pot back to a known state.
void Reset()
{
digitalWrite(PinCS, LOW);
digitalWrite(PinUD, LOW);
digitalWrite(PinINC, HIGH);
for (uint8_t i = 0; i < X9CXXX::X9_STEPS; i++)
{
digitalWrite(PinINC, LOW);
delayMicroseconds(NINC_HIGH_PERIOD);
digitalWrite(PinINC, HIGH);
delayMicroseconds(NINC_LOW_PERIOD);
}
digitalWrite(PinCS, HIGH);
digitalWrite(PinUD, HIGH);
CurrentStep = 0;
}
// Input step [0 ; X9_STEPS]
void JumpToStep(const uint8_t step, const bool store = false)
{
if (step > X9CXXX::X9_STEPS - 1)
{
return;//Invalid step.
}
while (CurrentStep != step)
{
if (CurrentStep > step)
{
Down(false);
}
else
{
Up(false);
}
}
if (store)
{
Store();
}
}
// Shifts the pot's wiper one step down.
void Down(const bool store = false)
{
digitalWrite(PinINC, HIGH);
digitalWrite(PinCS, LOW);
digitalWrite(PinUD, LOW);
delayMicroseconds(NINC_HIGH_TO_UND_CHANGE);
digitalWrite(PinINC, LOW);
if (store)
{
Store();
}
if (CurrentStep > 0)
{
CurrentStep--;
}
}
// Shifts the pot's wiper one step up.
void Up(const bool store = false)
{
digitalWrite(PinINC, HIGH);
digitalWrite(PinCS, LOW);
digitalWrite(PinUD, HIGH);
delayMicroseconds(NINC_HIGH_TO_UND_CHANGE);
digitalWrite(PinINC, LOW);
if (store)
{
Store();
}
if (CurrentStep < X9CXXX::X9_STEPS)
{
CurrentStep++;
}
}
const uint8_t GetStep()
{
return CurrentStep;
}
void Store()
{
digitalWrite(PinINC, HIGH);
digitalWrite(PinCS, HIGH);
delayMicroseconds(NCS_DESELECT_TIME_STORE);//This is way too long to wait for storage, better check elapsed outside if needed.
digitalWrite(PinCS, LOW);
}
};
class FastX9C102 : public FastX9CXXX<X9CXXX::X9C102_RESISTANCE>
{
public:
FastX9C102() :
FastX9CXXX<X9CXXX::X9C102_RESISTANCE>()
{}
FastX9C102(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin) :
FastX9CXXX<X9CXXX::X9C102_RESISTANCE>(csPin, udPin, incPin)
{}
};
class FastX9C103 : public FastX9CXXX<X9CXXX::X9C103_RESISTANCE>
{
public:
FastX9C103() :
FastX9CXXX<X9CXXX::X9C103_RESISTANCE>()
{}
FastX9C103(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin) :
FastX9CXXX<X9CXXX::X9C103_RESISTANCE>(csPin, udPin, incPin)
{}
};
class FastX9C104 : public FastX9CXXX<X9CXXX::X9C104_RESISTANCE>
{
public:
FastX9C104() :
FastX9CXXX<X9CXXX::X9C104_RESISTANCE>()
{}
FastX9C104(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin) :
FastX9CXXX<X9CXXX::X9C104_RESISTANCE>(csPin, udPin, incPin)
{}
};
class FastX9C503 : public FastX9CXXX<X9CXXX::X9C503_RESISTANCE>
{
public:
FastX9C503() :
FastX9CXXX<X9CXXX::X9C503_RESISTANCE>()
{}
FastX9C503(const uint8_t csPin, const uint8_t udPin, const uint8_t incPin) :
FastX9CXXX<X9CXXX::X9C503_RESISTANCE>(csPin, udPin, incPin)
{}
};
#endif