ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Wed May 08 00:30:09 2019 +0000
Revision:
17:7d4d8905b608
Parent:
12:ff8d26124c38
Child:
18:14e5391beccf
All Libraries: Huge Bug due to previous changes that causes unexpected crash.; WDplayer: Tuned to meet 20 lines per function criteria;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 2:964e19f2f3f1 1 #include "WDplayer.h"
rottenegg 11:7f3e9bc7366d 2 Ticker ISR;
rottenegg 2:964e19f2f3f1 3
rottenegg 3:63cdd5cab431 4 //Constructor
rottenegg 11:7f3e9bc7366d 5 WDplayer::WDplayer(PinName buzzer)
rottenegg 11:7f3e9bc7366d 6 : _dac(new PwmOut(buzzer)) {
rottenegg 3:63cdd5cab431 7 buffer = (unsigned char *)malloc(1);
rottenegg 3:63cdd5cab431 8 }
rottenegg 2:964e19f2f3f1 9
rottenegg 3:63cdd5cab431 10 //Deconstructor
rottenegg 3:63cdd5cab431 11 WDplayer::~WDplayer() {
rottenegg 3:63cdd5cab431 12 free(buffer);
rottenegg 11:7f3e9bc7366d 13 free(cache);
rottenegg 11:7f3e9bc7366d 14 delete _dac;
rottenegg 3:63cdd5cab431 15 }
rottenegg 3:63cdd5cab431 16
rottenegg 11:7f3e9bc7366d 17 void WDplayer::intWD(const char *path, bool allocate) {
rottenegg 3:63cdd5cab431 18 //Open File and Check
rottenegg 2:964e19f2f3f1 19 _fptr = fopen(path,"r");
rottenegg 3:63cdd5cab431 20 //Get Riff Information
rottenegg 2:964e19f2f3f1 21 unsigned int samplerate;
rottenegg 2:964e19f2f3f1 22 fseek(_fptr,24,SEEK_SET);
rottenegg 2:964e19f2f3f1 23 fread(&samplerate,4,1,_fptr);
rottenegg 2:964e19f2f3f1 24 fseek(_fptr,0,SEEK_END);
rottenegg 2:964e19f2f3f1 25 unsigned int len = ftell(_fptr);
rottenegg 3:63cdd5cab431 26 //Set Private Variables
rottenegg 2:964e19f2f3f1 27 _pwmfreq = (1.0f / samplerate);
rottenegg 2:964e19f2f3f1 28 _path = path;
rottenegg 2:964e19f2f3f1 29 _length = len - 44;
rottenegg 2:964e19f2f3f1 30 _fptr = NULL;
rottenegg 2:964e19f2f3f1 31 _tck = 0;
rottenegg 11:7f3e9bc7366d 32 lock = false;
rottenegg 12:ff8d26124c38 33 vtck = 4000;
rottenegg 11:7f3e9bc7366d 34 _dac->period( _pwmfreq / 4.0f);
rottenegg 11:7f3e9bc7366d 35 if (allocate) {
rottenegg 12:ff8d26124c38 36 cache = (unsigned char*)std::calloc(4000,sizeof(unsigned char));
rottenegg 11:7f3e9bc7366d 37 }
rottenegg 2:964e19f2f3f1 38 };
rottenegg 2:964e19f2f3f1 39
rottenegg 11:7f3e9bc7366d 40 //Inline Functions (Operates using main MCU)
rottenegg 11:7f3e9bc7366d 41
rottenegg 11:7f3e9bc7366d 42 void WDplayer::WDplay() {
rottenegg 2:964e19f2f3f1 43 _fptr = fopen(_path,"r");
rottenegg 2:964e19f2f3f1 44 fseek(_fptr,44,SEEK_SET);
rottenegg 3:63cdd5cab431 45 //Sampling Loop- Sample Byte by Byte
rottenegg 3:63cdd5cab431 46 //CORE ENGINE
rottenegg 2:964e19f2f3f1 47 for (_tck = _length; _tck > 1; _tck--) {
rottenegg 2:964e19f2f3f1 48 fread(buffer,1,1,_fptr);
rottenegg 11:7f3e9bc7366d 49 _dac->write((float)buffer[0] / 255.00f);
rottenegg 2:964e19f2f3f1 50 wait(_pwmfreq / 2.0f); //I dont like this & Playback present here
rottenegg 2:964e19f2f3f1 51 }
rottenegg 2:964e19f2f3f1 52 _fptr = NULL;
rottenegg 2:964e19f2f3f1 53 _tck = 0;
rottenegg 2:964e19f2f3f1 54 };
rottenegg 2:964e19f2f3f1 55
rottenegg 11:7f3e9bc7366d 56 void WDplayer::WDtck() {
rottenegg 3:63cdd5cab431 57 //To Loop the Music File When it Ends
rottenegg 2:964e19f2f3f1 58 if (_fptr == NULL) {
rottenegg 2:964e19f2f3f1 59 _fptr = fopen(_path,"r");
rottenegg 2:964e19f2f3f1 60 fseek(_fptr,44,SEEK_SET);
rottenegg 2:964e19f2f3f1 61 }
rottenegg 3:63cdd5cab431 62 //Sampling If Statements
rottenegg 2:964e19f2f3f1 63 if (_tck == _length) {
rottenegg 2:964e19f2f3f1 64 _fptr = NULL;
rottenegg 2:964e19f2f3f1 65 _tck = 0;
rottenegg 2:964e19f2f3f1 66 } else {
rottenegg 3:63cdd5cab431 67 //Plays One Sample
rottenegg 2:964e19f2f3f1 68 fread(buffer,1,1,_fptr);
rottenegg 11:7f3e9bc7366d 69 _dac->write( (int)buffer[0] / 255.00 );
rottenegg 2:964e19f2f3f1 70 _tck++;
rottenegg 2:964e19f2f3f1 71 }
rottenegg 2:964e19f2f3f1 72 };
rottenegg 2:964e19f2f3f1 73
rottenegg 11:7f3e9bc7366d 74 //Outline Functions (Uses ISR to operate)
rottenegg 11:7f3e9bc7366d 75
rottenegg 11:7f3e9bc7366d 76 void WDplayer::ISRpreload() {
rottenegg 11:7f3e9bc7366d 77 //File Reset
rottenegg 11:7f3e9bc7366d 78 if (_fptr == NULL) {
rottenegg 11:7f3e9bc7366d 79 _fptr = fopen(_path,"r");
rottenegg 11:7f3e9bc7366d 80 fseek(_fptr,44,SEEK_SET);
rottenegg 11:7f3e9bc7366d 81 //Reset State and Locational Variables
rottenegg 11:7f3e9bc7366d 82 lock = false;
rottenegg 12:ff8d26124c38 83 vtck = 4000;
rottenegg 11:7f3e9bc7366d 84 _tck = 0;
rottenegg 11:7f3e9bc7366d 85 }
rottenegg 11:7f3e9bc7366d 86 //CORE ENGINE WRITING
rottenegg 12:ff8d26124c38 87 if (_tck + 2000 >= _length) {
rottenegg 11:7f3e9bc7366d 88 _fptr = NULL;
rottenegg 11:7f3e9bc7366d 89 _tck = 0;
rottenegg 11:7f3e9bc7366d 90 vtck = 0;
rottenegg 12:ff8d26124c38 91 } else if (vtck > 2001 && !lock) {
rottenegg 17:7d4d8905b608 92 this->UpdateBlock1();
rottenegg 12:ff8d26124c38 93 } else if (vtck < 1999 && lock) {
rottenegg 17:7d4d8905b608 94 this->UpdateBlock2();
rottenegg 11:7f3e9bc7366d 95 }
rottenegg 11:7f3e9bc7366d 96 }
rottenegg 11:7f3e9bc7366d 97
rottenegg 17:7d4d8905b608 98 void WDplayer::UpdateBlock2() {
rottenegg 17:7d4d8905b608 99 //Block 2 Update
rottenegg 17:7d4d8905b608 100 for (int i = 2001; i <= 4000; i++) {
rottenegg 17:7d4d8905b608 101 fread(buffer,1,1,_fptr);
rottenegg 17:7d4d8905b608 102 cache[i] = ((int)buffer[0]);
rottenegg 17:7d4d8905b608 103 }
rottenegg 17:7d4d8905b608 104 lock = false;
rottenegg 17:7d4d8905b608 105 _tck = _tck + 2000;
rottenegg 17:7d4d8905b608 106 }
rottenegg 17:7d4d8905b608 107
rottenegg 17:7d4d8905b608 108 void WDplayer::UpdateBlock1() {
rottenegg 17:7d4d8905b608 109 //Block 1 Update
rottenegg 17:7d4d8905b608 110 for (int i = 0; i <= 2000; i++) {
rottenegg 17:7d4d8905b608 111 fread(buffer,1,1,_fptr);
rottenegg 17:7d4d8905b608 112 cache[i] = ((unsigned char)buffer[0]);
rottenegg 17:7d4d8905b608 113 }
rottenegg 17:7d4d8905b608 114 lock = true;
rottenegg 17:7d4d8905b608 115 _tck = _tck + 2000;
rottenegg 17:7d4d8905b608 116 }
rottenegg 17:7d4d8905b608 117
rottenegg 11:7f3e9bc7366d 118 void WDplayer::ISRset() {
rottenegg 11:7f3e9bc7366d 119 this->ISRpreload();
rottenegg 11:7f3e9bc7366d 120 ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq);
rottenegg 11:7f3e9bc7366d 121 }
rottenegg 11:7f3e9bc7366d 122
rottenegg 12:ff8d26124c38 123 void WDplayer::ISRpause() {
rottenegg 12:ff8d26124c38 124 ISR.detach();
rottenegg 12:ff8d26124c38 125 }
rottenegg 12:ff8d26124c38 126
rottenegg 12:ff8d26124c38 127 void WDplayer::ISRresume() {
rottenegg 12:ff8d26124c38 128 ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq);
rottenegg 12:ff8d26124c38 129 }
rottenegg 12:ff8d26124c38 130
rottenegg 11:7f3e9bc7366d 131 void WDplayer::ISRreset() {
rottenegg 11:7f3e9bc7366d 132 free(cache);
rottenegg 11:7f3e9bc7366d 133 lock = false;
rottenegg 12:ff8d26124c38 134 vtck = 4000;
rottenegg 11:7f3e9bc7366d 135 _tck = 0;
rottenegg 11:7f3e9bc7366d 136 ISR.detach();
rottenegg 11:7f3e9bc7366d 137 }
rottenegg 11:7f3e9bc7366d 138
rottenegg 11:7f3e9bc7366d 139 void WDplayer::ISRtck() {
rottenegg 11:7f3e9bc7366d 140 _dac->write( cache[vtck] / 255.00 );
rottenegg 12:ff8d26124c38 141 if (vtck == 4000) {
rottenegg 11:7f3e9bc7366d 142 vtck = -1;
rottenegg 11:7f3e9bc7366d 143 }
rottenegg 11:7f3e9bc7366d 144 vtck++;
rottenegg 11:7f3e9bc7366d 145 };
rottenegg 11:7f3e9bc7366d 146
rottenegg 11:7f3e9bc7366d 147
rottenegg 2:964e19f2f3f1 148 float WDplayer::get_pwmfreq() {
rottenegg 2:964e19f2f3f1 149 return _pwmfreq;
rottenegg 2:964e19f2f3f1 150 };