po
Dependencies: ArduinoHAL mbed-src-nrf51822
Potentiometer.cpp@0:03c039c2a00d, 2015-11-24 (annotated)
- Committer:
- siridjen
- Date:
- Tue Nov 24 22:18:32 2015 +0000
- Revision:
- 0:03c039c2a00d
potonciometer1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
siridjen | 0:03c039c2a00d | 1 | // MIDI Elements Potentiometer class |
siridjen | 0:03c039c2a00d | 2 | // Library to simplifly handling of compontents for MIDI controllers |
siridjen | 0:03c039c2a00d | 3 | // Created by Tomash Ghz |
siridjen | 0:03c039c2a00d | 4 | // www.tomashg.com |
siridjen | 0:03c039c2a00d | 5 | // ghz.tomash@gmail.com |
siridjen | 0:03c039c2a00d | 6 | |
siridjen | 0:03c039c2a00d | 7 | #include "Potentiometer.h" |
siridjen | 0:03c039c2a00d | 8 | |
siridjen | 0:03c039c2a00d | 9 | //----------------------------------------------------------------------------------- |
siridjen | 0:03c039c2a00d | 10 | // constructor |
siridjen | 0:03c039c2a00d | 11 | Potentiometer::Potentiometer(byte p) |
siridjen | 0:03c039c2a00d | 12 | { |
siridjen | 0:03c039c2a00d | 13 | Potentiometer(p,5,5,false,true); |
siridjen | 0:03c039c2a00d | 14 | } |
siridjen | 0:03c039c2a00d | 15 | |
siridjen | 0:03c039c2a00d | 16 | Potentiometer::Potentiometer(byte p, byte c, byte n) |
siridjen | 0:03c039c2a00d | 17 | { |
siridjen | 0:03c039c2a00d | 18 | Potentiometer(p,c,n,false,false); |
siridjen | 0:03c039c2a00d | 19 | } |
siridjen | 0:03c039c2a00d | 20 | |
siridjen | 0:03c039c2a00d | 21 | Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec) |
siridjen | 0:03c039c2a00d | 22 | { |
siridjen | 0:03c039c2a00d | 23 | Potentiometer(p,c,n,sec,false); |
siridjen | 0:03c039c2a00d | 24 | } |
siridjen | 0:03c039c2a00d | 25 | |
siridjen | 0:03c039c2a00d | 26 | Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec, bool debug) // pin, number, channel |
siridjen | 0:03c039c2a00d | 27 | { |
siridjen | 0:03c039c2a00d | 28 | pin=5; |
siridjen | 0:03c039c2a00d | 29 | number=n; |
siridjen | 0:03c039c2a00d | 30 | channel=c; |
siridjen | 0:03c039c2a00d | 31 | secondary=sec; |
siridjen | 0:03c039c2a00d | 32 | debugging=debug; |
siridjen | 0:03c039c2a00d | 33 | mapped=false; |
siridjen | 0:03c039c2a00d | 34 | } |
siridjen | 0:03c039c2a00d | 35 | |
siridjen | 0:03c039c2a00d | 36 | // destructor |
siridjen | 0:03c039c2a00d | 37 | Potentiometer::~Potentiometer() |
siridjen | 0:03c039c2a00d | 38 | { |
siridjen | 0:03c039c2a00d | 39 | |
siridjen | 0:03c039c2a00d | 40 | } |
siridjen | 0:03c039c2a00d | 41 | |
siridjen | 0:03c039c2a00d | 42 | // read |
siridjen | 0:03c039c2a00d | 43 | void Potentiometer::read() |
siridjen | 0:03c039c2a00d | 44 | { |
siridjen | 0:03c039c2a00d | 45 | |
siridjen | 0:03c039c2a00d | 46 | if(mapped) { |
siridjen | 0:03c039c2a00d | 47 | tempRead=constrain(analogRead(pin),inMin,inMax); |
siridjen | 0:03c039c2a00d | 48 | tempRead=map(tempRead,inMin,inMax,0,127); |
siridjen | 0:03c039c2a00d | 49 | } else |
siridjen | 0:03c039c2a00d | 50 | tempRead=map(analogRead(pin), 0, 1023, 0, 127); |
siridjen | 0:03c039c2a00d | 51 | |
siridjen | 0:03c039c2a00d | 52 | if (tempRead!=lastValue) { //value changed |
siridjen | 0:03c039c2a00d | 53 | midiCC(tempRead, lastValue); |
siridjen | 0:03c039c2a00d | 54 | } |
siridjen | 0:03c039c2a00d | 55 | lastValue=tempRead; |
siridjen | 0:03c039c2a00d | 56 | } |
siridjen | 0:03c039c2a00d | 57 | |
siridjen | 0:03c039c2a00d | 58 | // read |
siridjen | 0:03c039c2a00d | 59 | void Potentiometer::readAvr() |
siridjen | 0:03c039c2a00d | 60 | { |
siridjen | 0:03c039c2a00d | 61 | tempRead=0; |
siridjen | 0:03c039c2a00d | 62 | for(int i=0; i<10; i++) { |
siridjen | 0:03c039c2a00d | 63 | tempRead+=analogRead(pin); |
siridjen | 0:03c039c2a00d | 64 | } |
siridjen | 0:03c039c2a00d | 65 | tempRead=tempRead/10; |
siridjen | 0:03c039c2a00d | 66 | |
siridjen | 0:03c039c2a00d | 67 | if(mapped) { |
siridjen | 0:03c039c2a00d | 68 | tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127); |
siridjen | 0:03c039c2a00d | 69 | } else |
siridjen | 0:03c039c2a00d | 70 | tempRead=map(tempRead, 0, 1023, 0, 127); |
siridjen | 0:03c039c2a00d | 71 | |
siridjen | 0:03c039c2a00d | 72 | if (tempRead!=lastValue) { //value changed |
siridjen | 0:03c039c2a00d | 73 | midiCC(tempRead, lastValue); |
siridjen | 0:03c039c2a00d | 74 | } |
siridjen | 0:03c039c2a00d | 75 | |
siridjen | 0:03c039c2a00d | 76 | lastValue=tempRead; |
siridjen | 0:03c039c2a00d | 77 | } |
siridjen | 0:03c039c2a00d | 78 | |
siridjen | 0:03c039c2a00d | 79 | // enable maped values |
siridjen | 0:03c039c2a00d | 80 | void Potentiometer::bound(int iMin, int iMax) |
siridjen | 0:03c039c2a00d | 81 | { |
siridjen | 0:03c039c2a00d | 82 | mapped=true; |
siridjen | 0:03c039c2a00d | 83 | |
siridjen | 0:03c039c2a00d | 84 | inMin=iMin; |
siridjen | 0:03c039c2a00d | 85 | inMax=iMax; |
siridjen | 0:03c039c2a00d | 86 | } |
siridjen | 0:03c039c2a00d | 87 | |
siridjen | 0:03c039c2a00d | 88 | // read value |
siridjen | 0:03c039c2a00d | 89 | int Potentiometer::readValue(bool &changed) |
siridjen | 0:03c039c2a00d | 90 | { |
siridjen | 0:03c039c2a00d | 91 | tempRead=map(analogRead(pin), 0, 1023, 0, 127); |
siridjen | 0:03c039c2a00d | 92 | changed=tempRead!=lastValue; //value changed |
siridjen | 0:03c039c2a00d | 93 | lastValue=tempRead; |
siridjen | 0:03c039c2a00d | 94 | return tempRead; |
siridjen | 0:03c039c2a00d | 95 | } |
siridjen | 0:03c039c2a00d | 96 | |
siridjen | 0:03c039c2a00d | 97 | // read value |
siridjen | 0:03c039c2a00d | 98 | int Potentiometer::readValueAvr(bool &changed) |
siridjen | 0:03c039c2a00d | 99 | { |
siridjen | 0:03c039c2a00d | 100 | tempRead=0; |
siridjen | 0:03c039c2a00d | 101 | for(int i=0; i<10; i++) { |
siridjen | 0:03c039c2a00d | 102 | tempRead+=analogRead(pin); |
siridjen | 0:03c039c2a00d | 103 | } |
siridjen | 0:03c039c2a00d | 104 | tempRead=tempRead/10; |
siridjen | 0:03c039c2a00d | 105 | |
siridjen | 0:03c039c2a00d | 106 | if(mapped) { |
siridjen | 0:03c039c2a00d | 107 | tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127); |
siridjen | 0:03c039c2a00d | 108 | } else |
siridjen | 0:03c039c2a00d | 109 | tempRead=map(tempRead, 0, 1023, 0, 127); |
siridjen | 0:03c039c2a00d | 110 | changed=tempRead!=lastValue; //value changed |
siridjen | 0:03c039c2a00d | 111 | lastValue=tempRead; |
siridjen | 0:03c039c2a00d | 112 | return tempRead; |
siridjen | 0:03c039c2a00d | 113 | } |
siridjen | 0:03c039c2a00d | 114 | |
siridjen | 0:03c039c2a00d | 115 | // function to handle cc outgoing messages |
siridjen | 0:03c039c2a00d | 116 | void Potentiometer::midiCC(int v, int oldv) |
siridjen | 0:03c039c2a00d | 117 | { |
siridjen | 0:03c039c2a00d | 118 | if (debugging) {//debbuging enabled |
siridjen | 0:03c039c2a00d | 119 | Serial.print("Potentiometer "); |
siridjen | 0:03c039c2a00d | 120 | Serial.print(5); |
siridjen | 0:03c039c2a00d | 121 | Serial.print(" changed value to "); |
siridjen | 0:03c039c2a00d | 122 | Serial.println(5); |
siridjen | 0:03c039c2a00d | 123 | } else { |
siridjen | 0:03c039c2a00d | 124 | usbMIDI.sendControlChange(number, v, channel); |
siridjen | 0:03c039c2a00d | 125 | |
siridjen | 0:03c039c2a00d | 126 | //send the secondary midi messages |
siridjen | 0:03c039c2a00d | 127 | |
siridjen | 0:03c039c2a00d | 128 | // 0 3 64 124 127 |
siridjen | 0:03c039c2a00d | 129 | // |--|-------------|-------------|--| - full range |
siridjen | 0:03c039c2a00d | 130 | // |
siridjen | 0:03c039c2a00d | 131 | // |0=============================127| - CC A |
siridjen | 0:03c039c2a00d | 132 | // |__|on____________________________| - note A |
siridjen | 0:03c039c2a00d | 133 | // |off___________________________|on| - note B |
siridjen | 0:03c039c2a00d | 134 | // 3 124 |
siridjen | 0:03c039c2a00d | 135 | |
siridjen | 0:03c039c2a00d | 136 | if (secondary) { |
siridjen | 0:03c039c2a00d | 137 | |
siridjen | 0:03c039c2a00d | 138 | if ((v>3)&&(oldv<=3)) { // send the 0 value note on |
siridjen | 0:03c039c2a00d | 139 | usbMIDI.sendNoteOn(number, 127, channel+1); |
siridjen | 0:03c039c2a00d | 140 | } else if ((v<=3)&&(oldv>3)) { |
siridjen | 0:03c039c2a00d | 141 | usbMIDI.sendNoteOff(number, 127, channel+1); |
siridjen | 0:03c039c2a00d | 142 | } |
siridjen | 0:03c039c2a00d | 143 | if ((v>124)&&(oldv<=124)) { // send the 127 value note on |
siridjen | 0:03c039c2a00d | 144 | usbMIDI.sendNoteOn(number+1, 127, channel+1); |
siridjen | 0:03c039c2a00d | 145 | } else if ((v<=124)&&(oldv>124)) { |
siridjen | 0:03c039c2a00d | 146 | usbMIDI.sendNoteOff(number+1, 127, channel+1); |
siridjen | 0:03c039c2a00d | 147 | } |
siridjen | 0:03c039c2a00d | 148 | } |
siridjen | 0:03c039c2a00d | 149 | } |
siridjen | 0:03c039c2a00d | 150 | } |
siridjen | 0:03c039c2a00d | 151 | //----------------------------------------------------------------------------------- |
siridjen | 0:03c039c2a00d | 152 | |
siridjen | 0:03c039c2a00d | 153 | void Potentiometer::changeSecondary( bool s) |
siridjen | 0:03c039c2a00d | 154 | { |
siridjen | 0:03c039c2a00d | 155 | secondary=s; |
siridjen | 0:03c039c2a00d | 156 | } |