/* ### description ### Library file for using a DS1803 dual digital potentiometer with an arduino. please see 'README.txt' for more information Copyright (C) <2020> 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 . */ #ifndef DS1803_H //guard against including the file more than once #define DS1803_H //-------------------------------------------- headers -------------------------------------------- #include "Arduino.h" //give access to the arduino #include //allow library to use the arduino's I2C functions //-------------------------------------------- define --------------------------------------------- #define WIPER_0 0xA9 //write to wiper one #define WIPER_1 0xAA //write to wiper two #define WIPER_BOTH 0xAF //write to both wipers at once //---------------------------------------------- enums --------------------------------------------------- //address of DS1803 based on the address lines A2,A1,A0. //A_000 == address of DS1803 when A2,A1,A0 are all 0. //A_101 is address of DS1803 when A2 = 1, A1 = 0, and A0 = 1. enum ADDRESS_LIST {A_000 = 0x28, A_001, A_010, A_011, A_100, A_101,A_110,A_111}; //---------------------------------------------- typedefs ------------------------------------------------ typedef enum ADDRESS_LIST ADDRESS; //-------------------------------------------- prototypes ----------------------------------------- //function to set value of wiper. takes in address of device, which wiper to write to and the value to write to it. void setWiper(const ADDRESS address, const int wiper, const int value); //function which reads the current value for both wipers at given address and prints them to the serial monitor of the arduino void readWipers(const ADDRESS address, int *vals);//int vals[2] //function which starts serial communications and starts i2c communication with potentiometer void initDS1803(); class PotenciometroI2C { float corte; ADDRESS id; public: PotenciometroI2C() { corte=20; id=A_000; } void Configura(int _addres, float _corte=20) { corte=20; switch (_addres) { case 0: id=A_000; break; case 1: id=A_001; break; case 2: id=A_010; break; case 3: id=A_011; break; case 4: id=A_100; break; case 5: id=A_101; break; case 6: id=A_110; break; case 7: id=A_111; break; default: id=A_000; break; } } int getvi() { int vals[2]; readWipers(id, vals); int vmax = vals[0]; int valor = (int)((vmax)*100. / 255 + 0.5); valor = (int)getEscala(valor); return valor; } void setv(int vol) { vol = setEscala(vol); //vol=101-vol;//se invierte vol = vol; vol = (2.55 * (vol)); if (vol > 255) vol = 255; setWiper(id, WIPER_0, vol); setWiper(id, WIPER_1, vol); } private: float setEscala(float i) { float corte_max = 100 - corte; Serial.print("setEscala to "); Serial.print(i); float res = i; if (i < corte) { res = i * (corte_max + 1) / corte; } else { res = corte_max + 1 + ((i - corte) * corte / (corte_max)); } Serial.print(" set escala out: "); Serial.println(res); Serial.print(" get: "); Serial.println(getEscala(res)); return res; } float getEscala(float i) { float corte_max = 100 - corte; float res = i; if (i < corte_max) { res = i * (corte) / corte_max; } else { res = corte + ((i - corte_max) * corte_max / corte); } return res; } }; #endif