-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWtv020sd16p.cpp
More file actions
95 lines (84 loc) · 2.13 KB
/
Wtv020sd16p.cpp
File metadata and controls
95 lines (84 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Wtv020sd16p.cpp - Library to control a WTV020-SD-16P module to play voices from an Arduino board.
Created by Diego J. Arevalo, August 6th, 2012.
Released into the public domain.
*/
#include "Arduino.h"
#include "Wtv020sd16p.h"
const unsigned int PLAY_PAUSE = 0xFFFE;
const unsigned int STOP = 0xFFFF;
const unsigned int VOLUME_MIN = 0xFFF0;
const unsigned int VOLUME_MAX = 0xFFF7;
Wtv020sd16p::Wtv020sd16p(int resetPin,int clockPin,int dataPin,int busyPin)
{
_resetPin=resetPin;
_clockPin=clockPin;
_dataPin=dataPin;
_busyPin=busyPin;
_busyPinState=HIGH;
pinMode(_resetPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
pinMode(_busyPin, INPUT);
}
void Wtv020sd16p::reset(){
digitalWrite(_clockPin, LOW);
digitalWrite(_resetPin, HIGH);
//Reset pulse.
digitalWrite(_resetPin, LOW);
delay(5);
digitalWrite(_resetPin, HIGH);
//Reset idle to start bit.
digitalWrite(_clockPin, HIGH);
delay(300);
}
void Wtv020sd16p::playVoice(int voiceNumber){
sendCommand(voiceNumber);
_busyPinState=digitalRead(_busyPin);
while(_busyPinState==HIGH){
_busyPinState=digitalRead(_busyPin);
}
}
void Wtv020sd16p::asyncPlayVoice(int voiceNumber){
sendCommand(voiceNumber);
}
void Wtv020sd16p::stopVoice(){
sendCommand(STOP);
}
void Wtv020sd16p::pauseVoice(){
sendCommand(PLAY_PAUSE);
}
void Wtv020sd16p::mute(){
sendCommand(VOLUME_MIN);
}
void Wtv020sd16p::unmute(){
sendCommand(VOLUME_MAX);
}
void Wtv020sd16p::sendCommand(unsigned int command) {
//Start bit Low level pulse.
digitalWrite(_clockPin, LOW);
delay(2);
for (unsigned int mask = 0x8000; mask > 0; mask >>= 1) {
//Clock low level pulse.
digitalWrite(_clockPin, LOW);
delayMicroseconds(50);
//Write data setup.
if (command & mask) {
digitalWrite(_dataPin, HIGH);
}
else {
digitalWrite(_dataPin, LOW);
}
//Write data hold.
delayMicroseconds(50);
//Clock high level pulse.
digitalWrite(_clockPin, HIGH);
delayMicroseconds(100);
if (mask>0x0001){
//Stop bit high level pulse.
delay(2);
}
}
//Busy active high from last data bit latch.
delay(20);
}