-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdafruit_VS1053.h
195 lines (163 loc) · 5.32 KB
/
Adafruit_VS1053.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/***************************************************
This is a library for the Adafruit VS1053 Codec Breakout
Designed specifically to work with the Adafruit VS1053 Codec Breakout
----> https://www.adafruit.com/products/1381
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#ifndef ADAFRUIT_VS1053_H
#define ADAFRUIT_VS1053_H
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif
#if !defined(ARDUINO_STM32_FEATHER)
#include "pins_arduino.h"
#include "wiring_private.h"
#endif
#include <SPI.h>
#include <SD.h>
// define here the size of a register!
#if defined(ARDUINO_STM32_FEATHER)
typedef volatile uint32 RwReg;
typedef uint32_t PortMask;
#elif defined (__AVR__)
typedef volatile uint8_t RwReg;
typedef uint8_t PortMask;
#elif defined (__arm__)
#if defined(TEENSYDUINO)
typedef volatile uint8_t RwReg;
typedef uint8_t PortMask;
#else
typedef volatile uint32_t RwReg;
typedef uint32_t PortMask;
#endif
#elif defined (ESP8266) || defined (ESP32)
typedef volatile uint32_t RwReg;
typedef uint32_t PortMask;
#elif defined (__ARDUINO_ARC__)
typedef volatile uint32_t RwReg;
typedef uint32_t PortMask;
#else
typedef volatile uint8_t RwReg;
typedef uint8_t PortMask;
#endif
typedef volatile RwReg PortReg;
#define VS1053_FILEPLAYER_TIMER0_INT 255 // allows useInterrupt to accept pins 0 to 254
#define VS1053_FILEPLAYER_PIN_INT 5
#define VS1053_SCI_READ 0x03
#define VS1053_SCI_WRITE 0x02
#define VS1053_REG_MODE 0x00
#define VS1053_REG_STATUS 0x01
#define VS1053_REG_BASS 0x02
#define VS1053_REG_CLOCKF 0x03
#define VS1053_REG_DECODETIME 0x04
#define VS1053_REG_AUDATA 0x05
#define VS1053_REG_WRAM 0x06
#define VS1053_REG_WRAMADDR 0x07
#define VS1053_REG_HDAT0 0x08
#define VS1053_REG_HDAT1 0x09
#define VS1053_REG_VOLUME 0x0B
#define VS1053_GPIO_DDR 0xC017
#define VS1053_GPIO_IDATA 0xC018
#define VS1053_GPIO_ODATA 0xC019
#define VS1053_INT_ENABLE 0xC01A
#define VS1053_MODE_SM_DIFF 0x0001
#define VS1053_MODE_SM_LAYER12 0x0002
#define VS1053_MODE_SM_RESET 0x0004
#define VS1053_MODE_SM_CANCEL 0x0008
#define VS1053_MODE_SM_EARSPKLO 0x0010
#define VS1053_MODE_SM_TESTS 0x0020
#define VS1053_MODE_SM_STREAM 0x0040
#define VS1053_MODE_SM_SDINEW 0x0800
#define VS1053_MODE_SM_ADPCM 0x1000
#define VS1053_MODE_SM_LINE1 0x4000
#define VS1053_MODE_SM_CLKRANGE 0x8000
#define VS1053_SCI_AIADDR 0x0A
#define VS1053_SCI_AICTRL0 0x0C
#define VS1053_SCI_AICTRL1 0x0D
#define VS1053_SCI_AICTRL2 0x0E
#define VS1053_SCI_AICTRL3 0x0F
#define VS1053_DATABUFFERLEN 32
#define VS1053_BASE 0x1800
class Adafruit_VS1053 {
public:
Adafruit_VS1053(int8_t mosi, int8_t miso, int8_t clk,
int8_t rst, int8_t cs, int8_t dcs, int8_t dreq);
Adafruit_VS1053(int8_t rst, int8_t cs, int8_t dcs, int8_t dreq);
uint8_t begin(void);
void reset(void);
void softReset(void);
uint16_t sciRead(uint8_t addr);
void sciWrite(uint8_t addr, uint16_t data);
void sineTest(uint8_t n, uint16_t ms);
void spiwrite(uint8_t d);
void spiwrite(uint8_t *c, uint16_t num);
uint8_t spiread(void);
uint16_t decodeTime(void);
void setVolume(uint8_t left, uint8_t right);
void dumpRegs(void);
void playData(uint8_t *buffer, uint8_t buffsiz);
boolean readyForData(void);
void applyPatch(const uint16_t *patch, uint16_t patchsize);
uint16_t loadPlugin(char *fn);
void GPIO_digitalWrite(uint8_t i, uint8_t val);
void GPIO_digitalWrite(uint8_t i);
uint16_t GPIO_digitalRead(void);
boolean GPIO_digitalRead(uint8_t i);
void GPIO_pinMode(uint8_t i, uint8_t dir);
boolean prepareRecordOgg(char *plugin);
void startRecordOgg(boolean mic);
void stopRecordOgg(void);
uint16_t recordedWordsWaiting(void);
uint16_t recordedReadWord(void);
uint8_t mp3buffer[VS1053_DATABUFFERLEN];
void readFrequencies(uint16_t *currentVal, uint16_t *peak);
void checkRateAndBands(void);
boolean setFrequencies(int numOfBands, short *frequency);
uint16_t readBands(void);
uint16_t readSamplingRate(void);
#ifdef ARDUINO_ARCH_SAMD
protected:
uint32_t _dreq;
private:
int32_t _mosi, _miso, _clk, _reset, _cs, _dcs;
boolean useHardwareSPI;
#else
protected:
uint8_t _dreq;
private:
int8_t _mosi, _miso, _clk, _reset, _cs, _dcs;
boolean useHardwareSPI;
#endif
};
class Adafruit_VS1053_FilePlayer : public Adafruit_VS1053 {
public:
Adafruit_VS1053_FilePlayer (int8_t mosi, int8_t miso, int8_t clk,
int8_t rst, int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardCS);
Adafruit_VS1053_FilePlayer (int8_t rst, int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardCS);
Adafruit_VS1053_FilePlayer (int8_t cs, int8_t dcs, int8_t dreq,
int8_t cardCS);
boolean begin(void);
boolean useInterrupt(uint8_t type);
File currentTrack;
volatile boolean playingMusic;
void feedBuffer(void);
boolean startPlayingFile(const char *trackname);
boolean playFullFile(const char *trackname);
void stopPlaying(void);
boolean paused(void);
boolean stopped(void);
void pausePlaying(boolean pause);
private:
void feedBuffer_noLock(void);
uint8_t _cardCS;
};
#endif // ADAFRUIT_VS1053_H