3872 Play-live--test code - tempting to make this the main one

Dependencies:   mbed fastlib

Files at this revision

API Documentation at this revision

Comitter:
chenchen2020
Date:
Wed Nov 11 04:50:30 2020 +0000
Parent:
1:c704bea518d8
Commit message:
Final;

Changed in this revision

SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
fastlib.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
diff -r c704bea518d8 -r 9296823ea33d SongPlayer.h
--- a/SongPlayer.h	Fri Oct 30 01:43:49 2020 +0000
+++ b/SongPlayer.h	Wed Nov 11 04:50:30 2020 +0000
@@ -18,8 +18,8 @@
         durationptr = duration;
         //returns after first note starts to play
     }
-    void Play_Note(float frequency[]){
-        _pin.period(1.0/frequency[notecount]);
+    void Play_Note(float frequency){
+        _pin.period(1.0/frequency);
         _pin = 1.0/2.0;
     }
     void nextnote();
diff -r c704bea518d8 -r 9296823ea33d fastlib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fastlib.lib	Wed Nov 11 04:50:30 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Ivop/code/fastlib/#148b9af2b336
diff -r c704bea518d8 -r 9296823ea33d main.cpp
--- a/main.cpp	Fri Oct 30 01:43:49 2020 +0000
+++ b/main.cpp	Wed Nov 11 04:50:30 2020 +0000
@@ -1,49 +1,73 @@
-/*
-Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
-
 #include <mbed.h>
 #include <string>
 #include <list>
 
 #include <mpr121.h>
 #include "SongPlayer.h"
+#include "fastlib/clock.h"
 
+SongPlayer mySpeaker(p21);
+
+//fl_select_pll0_clock_source(1);
+//fl_pll0_control(1,1); //enable PLL clock
+//fl_pll0_config(1, 512); //Configure output clock freq.
+
+Timer       t;
+Timer       t_rest;
+Timer       t_base;
+Timer       reset_timer;
+Timer       song_timer;
+bool        timer_begin = 0;
+bool        played = 0;
+uint16_t    value = 0;
+float       temp_value;
+PwmOut      jawServo(p26);
+PwmOut      baseServo(p25);
 DigitalIn   record_mode(p14);
 DigitalIn   play_back_mode(p16);
 DigitalIn   play_live_mode(p15);
-SongPlayer mySpeaker(p21);
-Timer t;
-Timer t_rest;
+DigitalIn   stop_mode(p17);
+DigitalIn   reset_mode(p20);
+DigitalOut  greenLED(p23);
+DigitalOut  redLED(p18);
+DigitalOut  blueLED(p22);
+
 
-enum Statetype {IDLE = 0, REC = 1, PLAY_BACK = 2, PLAY_LIVE = 3, STOP = 4, RESET = 5};
+enum        Statetype {IDLE = 0, REC = 1, PLAY_BACK = 2, PLAY_LIVE = 3, STOP = 4, RESET = 5};
+
+Statetype   mode;
+
+float       mem_note[160];
+float       mem_duration[160];
+int         mem_ind;
+float       dt;
+float dur[1]= {0.5f};
 
-Statetype mode;
+class Watchdog
+{
+public:
+// Load timeout value in watchdog timer and enable
+    void kick(float s)
+    {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = s * (float)clk;
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        kick();
+    }
+// "kick" or "feed" the dog - reset the watchdog timer
+// by writing this required bit pattern
+    void kick()
+    {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
 
-float mem_note[160];
-float mem_duration[160];
-int mem_ind;
+Watchdog wdt;
 
 // Create the interrupt receiver object on pin 26
-InterruptIn interrupt(p26);
+InterruptIn interrupt(p30);
 
 // Setup the Serial to the PC for debugging
 Serial pc(USBTX, USBRX);
@@ -55,14 +79,145 @@
 // constructor(i2c object, i2c address of the mpr121)
 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
 
-void fallInterrupt() {
+float note = 0;
+
+float parse_key(uint16_t input)
+{
+    switch(input) {
+        case 0x0:
+            return 0.0;
+        case 0x1:
+            return 440.0;
+        case 0x2:
+            return 446.16;
+        case 0x4:
+            return 493.88;
+        case 0x8:
+            return 523.25;
+        case 0x10:
+            return 554.37;
+        case 0x20:
+            return 587.33;
+        case 0x40:
+            return 622.25;
+        case 0x80:
+            return 659.25;
+        case 0x100:
+            return 698.46;
+        case 0x200:
+            return 739.99;
+        case 0x400:
+            return 783.99;
+        case 0x800:
+            return 830.61;
+        default:
+            return 0.0;
+    }
+}
+
+void dance(uint16_t note_input)
+{
+
+    if(note_input == 0x1) {
+        redLED = 1;
+        greenLED = 0;
+        blueLED = 0;
+        jawServo.write(0.11f);
+        baseServo.write(0.11f);
+    } else if(note_input == 0x1) {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 0;
+        jawServo.write(0.18f);
+        baseServo.write(0.18f);
+    } else if(note_input == 0x2) {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.23f);
+        baseServo.write(0.23f);
+    } else if(note_input == 0x4) {
+        redLED = 0;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.28f);
+        baseServo.write(0.28f);
+    } else if(note_input == 0x8) {
+        redLED = 0;
+        greenLED = 0;
+        blueLED = 1;
+        jawServo.write(0.34f);
+        baseServo.write(0.34f);
+    } else if(note_input == 0x10) {
+        redLED = 0;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.39f);
+        baseServo.write(0.39f);
+    } else if(note_input == 0x20) {
+        redLED = 1;
+        greenLED = 0;
+        blueLED = 1;
+        jawServo.write(0.48f);
+        baseServo.write(0.48f);
+    } else if(note_input == 0x40) {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 0;
+        jawServo.write(0.55f);
+        baseServo.write(0.55f);
+    } else if(note_input == 0x80) {
+        redLED = 0;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.63f);
+        baseServo.write(0.63f);
+    } else if(note_input == 0x100) {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 0;
+        jawServo.write(0.73f);
+        baseServo.write(0.73f);
+    } else if(note_input == 0x200) {
+        redLED = 1;
+        greenLED = 0;
+        blueLED = 1;
+        jawServo.write(0.83f);
+        baseServo.write(0.83f);
+    } else if(note_input == 0x400) {
+        redLED = 1;
+        greenLED = 0;
+        blueLED = 1;
+        jawServo.write(0.88f);
+        baseServo.write(0.88f);
+    } else if(note_input == 0x800) {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.93f);
+        baseServo.write(0.93f);
+    } else {
+        redLED = 1;
+        greenLED = 1;
+        blueLED = 1;
+        jawServo.write(0.98f);
+        baseServo.write(0.98f);
+    }
+    //wait(0.45);
+}
+
+void fallInterrupt()
+{
     //int key_code=0;
     //int i=0;
+    //wdt.kick();
+    interrupt.fall(NULL);
+    t_rest.stop();
+    t.start();
     uint16_t value=mpr121.read(0x00);
     value +=mpr121.read(0x01)<<8;
-    float temp_value = float(value);
-    t_rest.stop();
-    if(mode == REC && mem_ind < 160){
+    float temp_value = parse_key(value);
+    if(record_mode == 0 && mem_ind < 160){
         //Write duration and silence in object
         mem_note[mem_ind] = 0;
         mem_duration[mem_ind] = t_rest.read();
@@ -71,27 +226,31 @@
     }
     pc.printf("The time of silence seconds %f \n", t_rest.read());
     t_rest.reset();
-    t.start();
-    float note[] = {value + 150};
-    if(mode == PLAY_LIVE || mode == REC){
+    wait(0.1);
+    float note = parse_key(value);
+    if(play_live_mode == 0 || record_mode == 0){
         mySpeaker.Play_Note(note);
+        dance(note);
     }
+    //wdt.kick();
     //sustaining note
     while(value > 0){
-        if(mode == PLAY_LIVE || mode == REC){
+        if(play_live_mode == 0 || record_mode == 0){
             mySpeaker.Play_Note(note);
         }
+        wait(0.05);
         pc.printf("MPR value: %x \r\n", value);
-        note[0] = value + 150;
-        if(mode == PLAY_LIVE || mode == REC){
+        note = parse_key(value);
+        if(play_live_mode == 0 || record_mode == 0){
             mySpeaker.Play_Note(note);
         }
         value=mpr121.read(0x00);
         value +=mpr121.read(0x01)<<8;
+        //wdt.kick();
     }
     mySpeaker.Play_Note(0);
     t.stop();
-    if(mode == REC && mem_ind < 160){
+    if(record_mode == 0 && mem_ind < 160){
         mem_note[mem_ind] = temp_value;
         mem_duration[mem_ind] = t.read();
         pc.printf("New Data in memory:  Note: %f  Duration: %f\n", mem_note[mem_ind], mem_duration[mem_ind]);
@@ -101,37 +260,109 @@
     float duration[] = {t.read()};
     pc.printf("The of note seconds %f \n", t.read());
     t.reset();
+    //wait(0.05);
+    interrupt.fall(&fallInterrupt);
+    interrupt.mode(PullUp);
+    //wdt.kick();
 }
-int main() {
-    
+
+int main()
+{
+    wait(1); //initialization
+    //pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock);
+    //wdt.kick(100);
+
+    //reset_mode.fall(&reset);
+    //reset_mode.rise(&reset_over);
+    //reset_mode.mode(PullUp);
+
+    jawServo.period_ms(20);
+    baseServo.period_ms(20);
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
 
     while (1) {
-        if(record_mode == 1){
-            mode = REC;
-        }
-        if(play_back_mode == 1){
-            mode = PLAY_BACK;
-        }
-        if(play_live_mode == 1){
-            mode = PLAY_LIVE;   
+        if(record_mode == 0 && reset_mode == 1) {
+            redLED = 1;
+            blueLED = 0;
+            greenLED = 0;
+            played = 0;
+            //wdt.kick();
+        } else if(play_back_mode == 0 && reset_mode == 1) {
+            note = 0;
+            redLED = 0;
+            blueLED = 1;
+            greenLED = 0;
+            interrupt.fall(NULL); //disable interrupt
+            //t.stop();
+            //t.reset();
+            //t_rest.stop();
+            //t_rest.reset();
+            for(int k = 0; k < mem_ind; k++) {
+                mySpeaker.Play_Note(mem_note[k]);
+                dance(mem_note[k]);
+                if(reset_mode == 0){
+                    break;   
+                }
+                wait(mem_duration[k]);
+            }
+            interrupt.fall(&fallInterrupt);
+            interrupt.mode(PullUp);
+            //wdt.kick();
+        } else if(play_live_mode == 0 && reset_mode == 1) {
+            note = 0;
+            redLED = 0;
+            blueLED = 0;
+            greenLED = 1;
+            played = 0;
+            //wdt.kick();
+        } else if(stop_mode == 0 && reset_mode == 1) {
+            note = 0;
+            redLED = 0;
+            blueLED = 0;
+            greenLED = 0;
+            //wdt.kick();
+        } else if(reset_mode == 0) {
+            //wdt.kick();
+            note = 0;
+            redLED = 0;
+            blueLED = 0;
+            greenLED = 0;
+            interrupt.fall(NULL);
+            mySpeaker.Play_Note(0);
+            if(!timer_begin) {
+                timer_begin = 1;
+                reset_timer.start();
+            }
+            while(reset_mode == 0) {
+
+                //t_base.stop();
+                //dt += t_base.read();
+                //t_base.reset();
+
+                //rotation orientation back to original position
+                //baseServo.pulsewidth_ms(1.9);
+                //wait(dt);
+                //dt = 0;
+                if(reset_timer.read() > 3) {
+                    for(int n = 0; n < 160; n++) {
+                        mem_note[n] = 0;
+                        mem_duration[n] = 0;
+                    }
+                }
+                //wdt.kick();
+            }
+            reset_timer.stop();
+            reset_timer.reset();
+            timer_begin = 1;
+            mem_ind = 0;
+            interrupt.fall(&fallInterrupt);
+            interrupt.mode(PullUp);
         }
-        switch (mode){
-            case REC:   
-            break;
-            case PLAY_BACK:
-                interrupt.fall(NULL);
-                mySpeaker.PlaySong(mem_note, mem_duration);
-                wait(1);
-                interrupt.fall(&fallInterrupt);
-            break;
-            case PLAY_LIVE:
-                
-            break;
-        }
+        //wdt.kick();
     }
 }
 
 
 
+