Conner Awald / Mbed 2 deprecated StepperMotorSongMidi

Dependencies:   mbed mbed-rtos

Revision:
2:7c59ebc9dc23
Parent:
1:58fce6cd1c51
--- a/main.cpp	Sun Apr 25 22:45:01 2021 +0000
+++ b/main.cpp	Mon Apr 26 00:17:01 2021 +0000
@@ -1,15 +1,10 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "notes.h"
-Thread motor;
+#include "math.h"
+#include "wii.h"
 
 Serial pc(USBTX, USBRX); // tx, rx
-/*void playNote(PwmOut motor, float note){
-    motor.period(1/note);
-    motor.write(.5f);
-    }*/
-
-
 class Flipper {
 public:
     Flipper(PinName pin) : _pin(pin) {
@@ -23,38 +18,83 @@
 };
  
 DigitalOut led1(LED1);
-Flipper f(p21);
-Flipper f1(p22);
-Ticker t;
+Flipper f1(p21);
+Flipper f2(p22);
+Flipper f3(p23);
 Ticker t1;
- 
+Ticker t2;
+Ticker t3;
+
+Mutex motor1lock;
+Mutex motor2lock;
+Mutex motor3lock;
+Timer motor1timer;
+
+/*
+Takes the midi number in from the array and then converts it to a frequency in hz;
+*/
+float midi2freq(float midi){
+    return 440.0*powf(2.0, (midi-69)/12);
+}
+
+/*
+Stops playback for motor1 and unlocks it to allow a new note to be set
+*/
+void stopMotor1(){
+    motor1lock.unlock();
+    }
 int main() {
-    //float note1 = 1.0/(NOTE_A4*2);
-    //float note2 = 1.0/(NOTE_CS5*2);
-    //float note2m = 1.0/(NOTE_C5*2);
-    //float note3 = 1.0/(NOTE_E5*2);
-    
     //parse length here
-    int   length = 100;
-    
-    float notes1 [length];
-    float durs1  [length];
-    
+    int length = (int)( sizeof(noteswii) / sizeof(noteswii[0]));
     // copy above for more lines of notes
     
     //parsing of notes, needs to partse 
     
     //parsing of durs
     
-    //t.attach(&f, &Flipper::flip, note1); // the address of the object, member function, and interval
-    //t1.attach(&f1, &Flipper::flip, note2);
+    
+
     pc.printf("Hello World!\n");
-    //pc.printf("%f",note1);
+    motor1timer.start();
     // spin in a main loop. flipper will interrupt it to call flip
     while(1) {
-        for(int i = 0; i < length; i++) {
-        t1.attach(&f1, &Flipper::flip, notes[i]);
-        wait(durs[i]);
+        for(int i = 0; i < length;) {
+        //This sees if we ever have an unlocked motor lock,
+        //in which case the motor will be done playing and
+        //we will need to set it to silent
+        if(motor1lock.trylock()){
+            motor1lock.unlock(); //Make the motor available for note commands
+            t1.detach();  //Stop the ticker on the motor
+            }
+        if(motor2lock.trylock()){
+            motor2lock.unlock();
+            t2.detach(); 
+            }
+        if(motor3lock.trylock()){
+            motor3lock.unlock();
+            t3.detach(); 
+            }   
+            
+            /*
+            IF THIS IS TRUE it means we have a note that just came into 
+            being played and we should move through the indexed list
+            at the very end
+            */         
+        if(notesstart[i]<motor1timer.read()){
+            if(motor1lock.trylock_for((notesstop[i]-notesstart[i]*1000)) { //This means we were succesfully able to lock the motor
+                t1.attach(&f1, &Flipper::flip, 1.0/midi2freq(noteswii[i]));
+                }
+                
+         
+         
+         i++;//THIS NEEDS TO BE THE LAST THING   
+        }
+        else {
+        t1.attach(&f1, &Flipper::flip, 1.0/notes1[i]); }
+        pc.printf("%f\n",notes1[i]);
+        pc.printf("%f\n",durs1[i]);
+        wait(durs1[i]);
+        pc.printf("The timer is at %f\n\n", motor1timer.read());
         }
         
     }