Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBMIDI_HelloWorld by
Diff: main.cpp
- Revision:
- 1:1e34feaa7774
- Parent:
- 0:4b55d56b6b61
- Child:
- 2:012e56772666
--- 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); - } + }*/ } }