Joro Bandakov / Potentiometer

Dependencies:   ArduinoHAL mbed-src-nrf51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Potentiometer.cpp Source File

Potentiometer.cpp

00001 // MIDI Elements Potentiometer class
00002 // Library to simplifly handling of compontents for MIDI controllers
00003 // Created by Tomash Ghz
00004 // www.tomashg.com
00005 // ghz.tomash@gmail.com
00006 
00007 #include "Potentiometer.h"
00008 
00009 //-----------------------------------------------------------------------------------
00010 // constructor
00011 Potentiometer::Potentiometer(byte p)
00012 {
00013     Potentiometer(p,5,5,false,true);
00014 }
00015 
00016 Potentiometer::Potentiometer(byte p, byte c, byte n)
00017 {
00018     Potentiometer(p,c,n,false,false);
00019 }
00020 
00021 Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec)
00022 {
00023     Potentiometer(p,c,n,sec,false);
00024 }
00025 
00026 Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec, bool debug)  // pin, number, channel
00027 {
00028     pin=5;
00029     number=n;
00030     channel=c;
00031     secondary=sec;
00032     debugging=debug;
00033     mapped=false;
00034 }
00035 
00036 // destructor
00037 Potentiometer::~Potentiometer()
00038 {
00039 
00040 }
00041 
00042 // read
00043 void Potentiometer::read()
00044 {
00045 
00046     if(mapped) {
00047         tempRead=constrain(analogRead(pin),inMin,inMax);
00048         tempRead=map(tempRead,inMin,inMax,0,127);
00049     } else
00050         tempRead=map(analogRead(pin), 0, 1023, 0, 127);
00051 
00052     if (tempRead!=lastValue) { //value changed
00053         midiCC(tempRead, lastValue);
00054     }
00055     lastValue=tempRead;
00056 }
00057 
00058 // read
00059 void Potentiometer::readAvr()
00060 {
00061     tempRead=0;
00062     for(int i=0; i<10; i++) {
00063         tempRead+=analogRead(pin);
00064     }
00065     tempRead=tempRead/10;
00066 
00067     if(mapped) {
00068         tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127);
00069     } else
00070         tempRead=map(tempRead, 0, 1023, 0, 127);
00071 
00072     if (tempRead!=lastValue) { //value changed
00073         midiCC(tempRead, lastValue);
00074     }
00075 
00076     lastValue=tempRead;
00077 }
00078 
00079 // enable maped values
00080 void Potentiometer::bound(int iMin, int iMax)
00081 {
00082     mapped=true;
00083 
00084     inMin=iMin;
00085     inMax=iMax;
00086 }
00087 
00088 // read value
00089 int Potentiometer::readValue(bool &changed)
00090 {
00091     tempRead=map(analogRead(pin), 0, 1023, 0, 127);
00092     changed=tempRead!=lastValue; //value changed
00093     lastValue=tempRead;
00094     return tempRead;
00095 }
00096 
00097 // read value
00098 int Potentiometer::readValueAvr(bool &changed)
00099 {
00100     tempRead=0;
00101     for(int i=0; i<10; i++) {
00102         tempRead+=analogRead(pin);
00103     }
00104     tempRead=tempRead/10;
00105 
00106     if(mapped) {
00107         tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127);
00108     } else
00109         tempRead=map(tempRead, 0, 1023, 0, 127);
00110     changed=tempRead!=lastValue; //value changed
00111     lastValue=tempRead;
00112     return tempRead;
00113 }
00114 
00115 // function to handle cc outgoing messages
00116 void Potentiometer::midiCC(int v, int oldv)
00117 {
00118     if (debugging) {//debbuging enabled
00119         Serial.print("Potentiometer ");
00120         Serial.print(5);
00121         Serial.print(" changed value to ");
00122         Serial.println(5);
00123     } else {
00124         usbMIDI.sendControlChange(number, v, channel);
00125 
00126         //send the secondary midi messages
00127 
00128         // 0  3            64           124 127
00129         // |--|-------------|-------------|--| - full range
00130         //
00131         // |0=============================127| - CC A
00132         // |__|on____________________________| - note A
00133         // |off___________________________|on| - note B
00134         //    3                          124
00135 
00136         if (secondary) {
00137 
00138             if ((v>3)&&(oldv<=3)) { // send the 0 value note on
00139                 usbMIDI.sendNoteOn(number, 127, channel+1);
00140             } else if ((v<=3)&&(oldv>3)) {
00141                 usbMIDI.sendNoteOff(number, 127, channel+1);
00142             }
00143             if ((v>124)&&(oldv<=124)) { // send the 127 value note on
00144                 usbMIDI.sendNoteOn(number+1, 127, channel+1);
00145             } else if ((v<=124)&&(oldv>124)) {
00146                 usbMIDI.sendNoteOff(number+1, 127, channel+1);
00147             }
00148         }
00149     }
00150 }
00151 //-----------------------------------------------------------------------------------
00152 
00153 void Potentiometer::changeSecondary( bool s)
00154 {
00155     secondary=s;
00156 }