-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathx9c.js
More file actions
172 lines (149 loc) · 4.72 KB
/
Copy pathx9c.js
File metadata and controls
172 lines (149 loc) · 4.72 KB
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
/*
* Copyright (c) 2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
Renesas X9C Series - Digital Potentiometers - X9C102, X9C103, X9C104, X9C503
https://www.renesas.com/us/en/products/analog-products/data-converters/digital-controlled-potentiometers-dcp/x9c104-digitally-controlled-potentiometer-xdcp
Datasheet: https://www.renesas.com/us/en/document/dst/x9c102-x9c103-x9c104-x9c503-datasheet
Reference Driver: https://github.com/lucyamy/LapX9C10X/blob/main/src/LapX9C10X.cpp
*/
import Timer from "timer"
class X9C
{
#incrementPin;
#upDownPin;
#csNVRAMWritePin;
#currentWiper;
#minWiperValue = 0;
#maxWiperValue = 99;
constructor(options)
{
const { increment, upDown, csNVRAMWrite } = options;
try
{
this.#incrementPin = new increment.io({
mode: increment.io.Output,
pin: increment.pin
});
this.#upDownPin = new upDown.io({
mode: upDown.io.Output,
pin: upDown.pin
});
this.#csNVRAMWritePin = new csNVRAMWrite.io({
mode: csNVRAMWrite.io.Output,
pin: csNVRAMWrite.pin
});
}
catch (e)
{
this.close();
throw e;
}
this.#reset(1);
}
#reset(value)
{
if (value > (this.#maxWiperValue / 2) | 0)
{
this.#currentWiper = this.#maxWiperValue;
this.#setWiper(this.#minWiperValue);
}
else
{
this.#currentWiper = this.#minWiperValue;
this.#setWiper(this.#maxWiperValue);
}
this.#setWiper(value);
}
#setWiper(value)
{
let steps;
if (!this.#inRange(value))
throw new RangeError(`wiper range is 0-99`);
if (value > this.#currentWiper)
{
this.#upDownPin.write(1);
steps = value - this.#currentWiper;
} else if (value < this.#currentWiper)
{
this.#upDownPin.write(0);
steps = this.#currentWiper - value;
}
else
{
return;
}
this.#incrementPin.write(1);
this.#csNVRAMWritePin.write(0);
for (let i = 0; i < steps; i++)
this.#pulseInc();
this.#csNVRAMWritePin.write(0);
this.#currentWiper = value;
}
#pulseInc()
{
this.#incrementPin.write(1);
Timer.delay(1);
this.#incrementPin.write(0);
Timer.delay(1);
}
#inRange(value)
{
if (value < this.#minWiperValue || value > this.#maxWiperValue)
return false
return true
}
configure(options)
{
// configuration has been implemented this way for the following reasons:
// * if a user wants to reset, it only makes sense for it to happen first
// as changing the wiper value before a reset would be overriden anyway
// * an offset should only be performed last because either a reset or directly
// setting the wiper value after an offset would override the offset
if (undefined !== options.reset)
this.#reset(options.reset);
if (undefined !== options.wiper)
{
let value = options.wiper;
this.#setWiper(value);
}
if (undefined !== options.offset)
{
let value = this.#currentWiper + options.offset;
if (!this.#inRange(value))
throw new RangeError(`wiper range is 0-99`);
this.#setWiper(value);
}
}
get wiper()
{
return this.#currentWiper;
}
close()
{
this.#incrementPin?.close();
this.#incrementPin = undefined;
this.#upDownPin?.close();
this.#upDownPin = undefined;
this.#csNVRAMWritePin?.close();
this.#csNVRAMWritePin = undefined;
this.#currentWiper = undefined;
}
}
export default X9C;