1 #include "MutilaDebug.h" 8 double dmod(
double a,
double b)
10 return (a - b * floor(a / b));
13 const uint8_t DFPReader::SMALL_NUMBERS[] = {
14 Mp3TrackZero, Mp3TrackZero+1, Mp3TrackZero+2, Mp3TrackZero+3,
15 Mp3TrackZero+4, Mp3TrackZero+5, Mp3TrackZero+6, Mp3TrackZero+7,
16 Mp3TrackZero+8, Mp3TrackZero+9, Mp3TrackZero+10, Mp3TrackZero+11,
17 Mp3TrackZero+12, Mp3TrackZero+13, Mp3TrackZero+14, Mp3TrackZero+15,
18 Mp3TrackZero+16, Mp3TrackZero+17, Mp3TrackZero+18, Mp3TrackZero+19
21 const uint8_t DFPReader::TENS[] = {
22 0, 0, Mp3TrackTwenty, Mp3TrackThirty, Mp3TrackFourty,
23 Mp3TrackFifty, Mp3TrackSixty, Mp3TrackSeventy,
24 Mp3TrackEighty, Mp3TrackNinety
30 readerBufSize(readerBufferSize)
32 readerBuf =
new uint8_t[readerBufSize];
51 if (MillisSince(lastPlayStart) >= PlaybackStartMs) {
52 _DBLN(F(
"playbackState Pending -> Playing"));
55 }
else if (playbackState ==
Playing) {
57 _DBLN(F(
"playbackState Playing -> Idle"));
62 if (unplayedElements > 0) {
63 if (playbackState ==
Idle) {
64 startPlayback(popElement());
69 void DFPReader::startPlayback(uint16_t track)
71 _DB(F(
"DFPReader::startPlayback "));
73 lastPlayStart = Millis();
80 _DB(F(
"DFPReader::readNumber n="));
84 double integerPart, fractionalPart;
85 double exp = pow(10, dp);
86 number = round(number*exp) / exp;
89 fractionalPart = modf(number, &integerPart);
93 _DBLN(fractionalPart, 10);
95 if (integerPart < 0.0) {
97 integerPart = fabs(integerPart);
100 if (integerPart < 0.1) {
103 appendMagnitude(&integerPart, 1000000000000.0, Mp3TrackTrillion);
104 appendMagnitude(&integerPart, 1000000000.0, Mp3TrackBillion);
105 appendMagnitude(&integerPart, 1000000, Mp3TrackMillion);
106 appendMagnitude(&integerPart, 1000, Mp3TrackThousand);
107 appendSubThousand((
long)integerPart);
112 for (uint8_t i=1; i<dp+1; i++) {
113 uint8_t digit = (uint32_t)(fractionalPart*pow(10, i))%10;
123 }
else if (unplayedElements > 0) {
133 unplayedElements = 0;
139 uint8_t DFPReader::popElement()
141 if (unplayedElements==0) {
144 uint8_t e = readerBuf[tailPtr];
145 tailPtr = (tailPtr+1) % readerBufSize;
153 DB(F(
"DFPReader::appendElement "));
155 if (unplayedElements >= readerBufSize) {
156 DBLN(F(
"DFPReader::appendElement ERROR: buffer full"));
159 readerBuf[(tailPtr+unplayedElements)%readerBufSize] = value;
165 void DFPReader::appendSubThousand(int16_t num)
181 void DFPReader::appendMagnitude(
double* number,
double magnitude, uint8_t magnitudeElement)
183 if (*number < magnitude) {
187 appendSubThousand((
int)(*number / magnitude));
189 *number = dmod(*number, magnitude);
Reader is not playing, but is waiting for an audio file to play.
Reader is playing an audio file.
void readNumber(double number, uint8_t dp=0)
DFPReader(Stream &serial, DFPlayerMini::Cmd playCmd=DFPlayerMini::PlayTf, uint8_t busyPin=0, uint8_t readerBufferSize=30)
void sendCmd(Cmd cmd, uint16_t arg=0)
bool appendElement(uint8_t value)
void begin(bool bootWait=true)
void begin(bool bootWait=true)