/* Wtv020sd16p - Library to control a WTV020-SD-16P module Created by Diego J. Arevalo, August 6th, 2012 for Arduino plaform Modified by Kamil Kanas, 17/5/2017 for mbed platforms Tested with FRDM KL25Z board Released into the public domain 9/6/2017. */

Dependents:   WTV020SD16_Player_KL25

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WTV020SD16.cpp Source File

WTV020SD16.cpp

00001 /*
00002  Wtv020sd16p.cpp - Library to control a WTV020-SD-16P module 
00003  Created by Diego J. Arevalo, August 6th, 2012 for  Arduino plaform
00004  Modified by Kamil Kanas, 17/5/2017 for mbed platforms
00005  Tested with FRDM KL25Z board
00006  Released into the public domain 9/6/2017.
00007  */
00008 
00009 #include "mbed.h"
00010 #include "WTV020SD16.h"
00011 
00012 const unsigned int PLAY_PAUSE = 0xFFFE;
00013 const unsigned int STOP = 0xFFFF;
00014 const unsigned int VOLUME_MIN = 0xFFF0;
00015 const unsigned int VOLUME_MAX = 0xFFF7;
00016 
00017 /*Pin definition*/
00018 DigitalOut _resetPin(PTB0);                             //reset pin   Output
00019 DigitalOut _clockPin(PTB1);                             //clock pin   Output
00020 DigitalOut _dataPin(PTB2);                              //data pin    Output
00021 DigitalIn  _busyPin(PTB3);                              //busy pin    Input 
00022 
00023 
00024 
00025 void Wtv020sd16p::reset(){
00026   
00027                                                            //Reset pulse.
00028   _clockPin=0;
00029   _resetPin=1;
00030   _resetPin=0;
00031   wait_ms(5);
00032   _resetPin=1;                                               //Reset idle to start bit. 
00033   _clockPin=1;
00034   wait_ms(300);
00035 }
00036 
00037 void Wtv020sd16p::playVoice(int voiceNumber){  
00038   sendCommand(voiceNumber);
00039 
00040   _busyPinState= _busyPin;
00041 
00042   while(_busyPinState==1){
00043     _busyPinState=_busyPin;
00044   }
00045 }
00046 
00047 void Wtv020sd16p::asyncPlayVoice(int voiceNumber){
00048   sendCommand(voiceNumber);
00049 }
00050 
00051 void Wtv020sd16p::stopVoice(){
00052   sendCommand(STOP);
00053 }
00054 
00055 void Wtv020sd16p::pauseVoice(){
00056   sendCommand(PLAY_PAUSE);
00057 }
00058 
00059 void Wtv020sd16p::mute(){
00060   sendCommand(VOLUME_MIN);
00061 }
00062 
00063 void Wtv020sd16p::unmute(){
00064   sendCommand(VOLUME_MAX);
00065 }
00066 
00067 void Wtv020sd16p::sendCommand(unsigned int command) {
00068   //Start bit Low level pulse.
00069  _clockPin=0;
00070   wait_ms(2);
00071   for (unsigned int mask = 0x8000; mask > 0; mask >>= 1) {
00072     //Clock low level pulse.
00073     _clockPin=0;
00074     wait_us(50);
00075     //Write data setup.
00076     if (command & mask) { 
00077       _dataPin=1;
00078     } else
00079      {
00080       
00081        _dataPin=0;
00082      }
00083     //Write data hold.
00084     wait_us(50);
00085     //Clock high level pulse.
00086     
00087     _clockPin=1;
00088     wait_us(100);
00089     if (mask>0x0001){
00090       //Stop bit high level pulse.
00091       wait_ms(2);      
00092     }
00093   }
00094   //Busy active high from last data bit latch.
00095   wait_ms(20);
00096 }