Source code for a MIDI controller built from parts and designed to be extensible

Dependencies:   USBMIDI mbed

Fork of USBMIDI_HelloWorld by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
earlz
Date:
Mon Jan 04 05:00:13 2016 +0000
Parent:
0:4b55d56b6b61
Child:
2:012e56772666
Commit message:
Making it work with a simple potentiometer

Changed in this revision

MovingAverage.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MovingAverage.lib	Mon Jan 04 05:00:13 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/fblanc/code/MovingAverage/#ad69440a9bef
--- a/main.cpp	Sun Feb 20 13:15:46 2011 +0000
+++ b/main.cpp	Mon Jan 04 05:00:13 2016 +0000
@@ -2,6 +2,7 @@
 
 #include "mbed.h"
 #include "USBMIDI.h"
+#include "MovingAverage.h"
 
 void show_message(MIDIMessage msg) {
     switch (msg.type()) {
@@ -22,16 +23,64 @@
     }    
 }
 
+static const int MIDI_MAX_VALUE = 16384;
+static const float MIDI_MAX_VALUE_F = 16384.0f;
+
+
+static const float TOLERANCE = 0.01f;
+static const int NOISE_FLOOR = 600;
+
+static const float LOWER_TOLERANCE = 0.016f;
+static const float LOWER_TOLERANCE_BEGIN = 0.34f;
+
 USBMIDI midi;
+AnalogIn pot1(p20);
+Serial pc(USBTX, USBRX); // tx, rx
+
+void write_full_cc(int controlmsb, int controllsb, int channel, int value){
+    int lsb = value / 128; //value & 0x7F;
+    int msb = value % 128; //value & 0x7F80 >> 7;
+    midi.write(MIDIMessage::ControlChange(controlmsb, msb, channel));
+    midi.write(MIDIMessage::ControlChange(controllsb, lsb, channel));
+}
+
+#define SMOOTHING_AMOUNT 500
 
-int main() {          
+int main() {
+    //MovingAverage <float>vavg(100,0.0);          
     midi.attach(show_message);         // call back for messages received    
-    while (1) {    
-        for(int i=48; i<83; i++) {     // send some messages!
+    
+    float last_value  = 0.0f;
+    while (1) {   
+        //float values[SMOOTHING_AMOUNT] = {0.0f}; 
+        float counter=0.0f;
+        for(int i=0;i<SMOOTHING_AMOUNT;i++){
+            wait_us(10);
+            counter+=pot1;
+        }
+        float value = counter / SMOOTHING_AMOUNT;
+        int midi_value = (int)(MIDI_MAX_VALUE_F * value);
+        
+        //at low voltage noise takes over..
+        if(midi_value < NOISE_FLOOR){
+            value = 0.0f;
+            midi_value = 0;
+        }
+        if(value - last_value > TOLERANCE || value - last_value < -TOLERANCE){
+            //as we approach noise floor, things get noisey.. 
+            if(value < LOWER_TOLERANCE_BEGIN && !(value - last_value > LOWER_TOLERANCE || value - last_value < -LOWER_TOLERANCE)){
+                continue;
+            }
+            pc.printf("sent: %f, %i\r\n", value, midi_value);
+            last_value = value;
+            write_full_cc(20, 52, 0, midi_value);
+       }
+        
+        /*for(int i=48; i<83; i++) {     // send some messages!
             midi.write(MIDIMessage::NoteOn(i));
             wait(0.25);
             midi.write(MIDIMessage::NoteOff(i));
             wait(0.5);
-        }
+        }*/
     }
 }