Conner Awald / Mbed 2 deprecated StepperMotorSongMidi

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "notes.h"
00004 #include "math.h"
00005 #include "wii.h"
00006 
00007 Serial pc(USBTX, USBRX); // tx, rx
00008 class Flipper {
00009 public:
00010     Flipper(PinName pin) : _pin(pin) {
00011         _pin = 0;
00012     }
00013     void flip() {
00014         _pin = !_pin;
00015     }
00016 private:
00017     DigitalOut _pin;
00018 };
00019  
00020 DigitalOut led1(LED1);
00021 Flipper f1(p21);
00022 Flipper f2(p22);
00023 Flipper f3(p23);
00024 Ticker t1;
00025 Ticker t2;
00026 Ticker t3;
00027 
00028 Mutex motor1lock;
00029 Mutex motor2lock;
00030 Mutex motor3lock;
00031 Timer motor1timer;
00032 
00033 /*
00034 Takes the midi number in from the array and then converts it to a frequency in hz;
00035 */
00036 float midi2freq(float midi){
00037     return 440.0*powf(2.0, (midi-69)/12);
00038 }
00039 
00040 /*
00041 Stops playback for motor1 and unlocks it to allow a new note to be set
00042 */
00043 void stopMotor1(){
00044     motor1lock.unlock();
00045     }
00046 int main() {
00047     //parse length here
00048     int length = (int)( sizeof(noteswii) / sizeof(noteswii[0]));
00049     // copy above for more lines of notes
00050     
00051     //parsing of notes, needs to partse 
00052     
00053     //parsing of durs
00054     
00055     
00056 
00057     pc.printf("Hello World!\n");
00058     motor1timer.start();
00059     // spin in a main loop. flipper will interrupt it to call flip
00060     while(1) {
00061         for(int i = 0; i < length;) {
00062         //This sees if we ever have an unlocked motor lock,
00063         //in which case the motor will be done playing and
00064         //we will need to set it to silent
00065         if(motor1lock.trylock()){
00066             motor1lock.unlock(); //Make the motor available for note commands
00067             t1.detach();  //Stop the ticker on the motor
00068             }
00069         if(motor2lock.trylock()){
00070             motor2lock.unlock();
00071             t2.detach(); 
00072             }
00073         if(motor3lock.trylock()){
00074             motor3lock.unlock();
00075             t3.detach(); 
00076             }   
00077             
00078             /*
00079             IF THIS IS TRUE it means we have a note that just came into 
00080             being played and we should move through the indexed list
00081             at the very end
00082             */         
00083         if(notesstart[i]<motor1timer.read()){
00084             if(motor1lock.trylock_for((notesstop[i]-notesstart[i]*1000)) { //This means we were succesfully able to lock the motor
00085                 t1.attach(&f1, &Flipper::flip, 1.0/midi2freq(noteswii[i]));
00086                 }
00087                 
00088          
00089          
00090          i++;//THIS NEEDS TO BE THE LAST THING   
00091         }
00092         else {
00093         t1.attach(&f1, &Flipper::flip, 1.0/notes1[i]); }
00094         pc.printf("%f\n",notes1[i]);
00095         pc.printf("%f\n",durs1[i]);
00096         wait(durs1[i]);
00097         pc.printf("The timer is at %f\n\n", motor1timer.read());
00098         }
00099         
00100     }
00101 }