ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Revision:
11:7f3e9bc7366d
Parent:
3:63cdd5cab431
Child:
12:ff8d26124c38
--- a/WDplayer/WDplayer.cpp	Fri Apr 26 02:31:11 2019 +0000
+++ b/WDplayer/WDplayer.cpp	Sun May 05 03:45:29 2019 +0000
@@ -1,16 +1,20 @@
 #include "WDplayer.h"
+Ticker ISR;
 
 //Constructor
-WDplayer::WDplayer() {
+WDplayer::WDplayer(PinName buzzer) 
+    : _dac(new PwmOut(buzzer)) {
     buffer = (unsigned char *)malloc(1);
 }
 
 //Deconstructor
 WDplayer::~WDplayer() {
     free(buffer);
+    free(cache);
+    delete _dac;
 }
 
-void WDplayer::intWD(const char *path, PwmOut &dac) {
+void WDplayer::intWD(const char *path, bool allocate) {
     //Open File and Check
     _fptr = fopen(path,"r");
     if (_fptr == NULL) {
@@ -28,24 +32,32 @@
     _length = len - 44;
     _fptr = NULL;
     _tck = 0;
-    dac.period( _pwmfreq / 4.0f);
+    lock = false;
+    vtck = 8000;
+    cache = NULL;    
+    _dac->period( _pwmfreq / 4.0f);
+    if (allocate) {
+        cache = (unsigned char*)std::calloc(8000,sizeof(unsigned char));
+    }
 };
 
-void WDplayer::WDplay(PwmOut &dac) {
+//Inline Functions (Operates using main MCU)
+
+void WDplayer::WDplay() {
     _fptr = fopen(_path,"r");
     fseek(_fptr,44,SEEK_SET);
     //Sampling Loop- Sample Byte by Byte
     //CORE ENGINE
     for (_tck = _length; _tck > 1; _tck--) {
         fread(buffer,1,1,_fptr);
-        dac.write((float)buffer[0] / 255.00f);
+        _dac->write((float)buffer[0] / 255.00f);
         wait(_pwmfreq / 2.0f); //I dont like this & Playback present here        
     }
     _fptr = NULL;
     _tck = 0;
 };
 
-void WDplayer::WDtck(PwmOut &dac) {
+void WDplayer::WDtck() {
     //To Loop the Music File When it Ends
     if (_fptr == NULL) {
     _fptr = fopen(_path,"r");
@@ -58,11 +70,69 @@
     } else {
         //Plays One Sample
         fread(buffer,1,1,_fptr);
-        dac.write( (int)buffer[0] / 255.00 );
+        _dac->write( (int)buffer[0] / 255.00 );
         _tck++;
     }
 };
 
+//Outline Functions (Uses ISR to operate)
+
+void WDplayer::ISRpreload() {
+    //File Reset
+    if (_fptr == NULL) {
+        _fptr = fopen(_path,"r");
+        fseek(_fptr,44,SEEK_SET);
+        //Reset State and Locational Variables
+        lock = false;
+        vtck = 8000;
+        _tck = 0;
+    }
+    //CORE ENGINE WRITING
+    if (_tck + 4000 >= _length) {
+        _fptr = NULL;
+        _tck = 0;
+        vtck = 0;
+    } else if (vtck > 4001 && !lock) {
+        //Block 1 Update
+        for (int i = 0; i <= 4000; i++) {
+                fread(buffer,1,1,_fptr);
+                cache[i] = ((unsigned char)buffer[0]);
+        }
+        lock = true;
+        _tck = _tck + 4000;
+    } else if (vtck < 3999 && lock) {
+        //Block 2 Update
+        for (int i = 4001; i <= 8000; i++) {
+            fread(buffer,1,1,_fptr);
+            cache[i] = ((int)buffer[0]);
+        }
+        lock = false;
+        _tck = _tck + 4000;
+    } 
+}
+
+void WDplayer::ISRset() {
+    this->ISRpreload();
+    ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq);
+}
+
+void WDplayer::ISRreset() {
+    free(cache);
+    lock = false;
+    vtck = 8000;
+    _tck = 0;
+    ISR.detach();
+}
+
+void WDplayer::ISRtck() {
+    _dac->write( cache[vtck] / 255.00 );
+    if (vtck == 8000) {
+        vtck = -1;
+    }
+    vtck++;
+};
+
+
 float WDplayer::get_pwmfreq() {
     return _pwmfreq;
 };
\ No newline at end of file