-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDS1803.cpp
91 lines (76 loc) · 4.77 KB
/
DS1803.cpp
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
/* ### description ###
Library file for using a DS1803 dual digital potentiometer with an arduino.
please see 'README.txt' for more information
Copyright (C) <2020> <Chris Nichols. github/return5>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//-------------------------------------------- headers --------------------------------------------
#include "DS1803.h" //header file for DS1803 library
//-------------------------------------------- prototypes -----------------------------------------
//function checks to make sure only a valid wiper was selected
static int checkValidWiper(const int wiper);
//function checks to makes sure value is a valid value in the range [0,255]
static int checkValidValue(const int value);
//-------------------------------------------- code -----------------------------------------------
//function checks to make sure only a valid wiper was selected
static int checkValidWiper(const int wiper) {
char c[26]; //holds string to be printed
switch(wiper) {
case WIPER_0: //FALLTHROUGH
case WIPER_1: //FALLTHROUGH
case WIPER_BOTH:
return 1; //valid wiper so return 1;
default:
snprintf(c,26,"%#04x is incorrect wiper.",wiper); //format the string to print
Serial.println(c); //print the string to serial monitor
return 0; //invalid wiper so return 0;
}
}
//function checks to makes sure value is a valid value in the range [0,255]
static int checkValidValue(const int value) {
char c[22]; //holds string to be printed
switch(value) {
case 0 ... 255:
return 1; //valid value so return 1;
default:
snprintf(c,22,"%d is out of range.",value); //format the string to print
Serial.println(c); //print the string to serial monitor
return 0; //incorrect value so return 0;
}
}
//function to set value of wiper. takes in which address of device, wiper to write to and the value to write to it.
void setWiper(const ADDRESS address,const int wiper, const int value) {
if(checkValidWiper(wiper) && checkValidValue(value)) { //if both value and wiper are valid
char c[25]; //holds string to be printed
Wire.beginTransmission(address); //begin transmiting to potentiometer at ADRESS
Wire.write(wiper); //tell it which wiper to write to
Wire.write(value); //write the value to the wiper
Wire.endTransmission(); //end transmission to the DS1803
snprintf(c,25,"writing %d to wiper %#04x",value,wiper); //format the string to print
Serial.println(c); //print string to serial monitor
}
}
//function which reads the current value for both wipers at given address and prints values to the serial monitor
void readWipers(const ADDRESS address) {
int wiper = 0; //shows which wiper is being read.
char c[15]; //hold formatted string.
Wire.requestFrom(address,2); //write the value to the wiper
while(Wire.available()) { // slave may send less than requested
snprintf(c,15,"wiper %d is %d",wiper++,(int)Wire.read()); //format the string before printing
Serial.println(c); // print the formatted string
}
}
//function which starts serial communication and starts i2c communication with potentiometer
void initDS1803() {
Serial.begin(9600); //serial communication at 9600 baud
Wire.begin(); //init wire.h library and join the i2c bus
}