forked from andykarpov/VS1003
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVS1003.h
156 lines (133 loc) · 3.36 KB
/
VS1003.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
/*
Copyright (C) 2012 Andy Karpov <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#ifndef __VS1003_H__
#define __VS1003_H__
// STL headers
// C headers
// Framework headers
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
/**
* Driver for VS1003 - MP3 / WMA / MIDI Audio Codec Chip
*
* See http://www.vlsi.fi/en/products/vs1003.html
*/
class VS1003
{
private:
uint8_t cs_pin; /**< Pin where CS line is connected */
uint8_t dcs_pin; /**< Pin where DCS line is connected */
uint8_t dreq_pin; /**< Pin where DREQ line is connected */
uint8_t reset_pin; /**< Pin where RESET line is connected */
uint8_t my_SPCR; /**< Value of the SPCR register how we like it. */
uint8_t my_SPSR; /**< Value of the SPSR register how we like it. */
protected:
inline void await_data_request(void) const
{
while ( !digitalRead(dreq_pin) );
}
inline void control_mode_on(void) const
{
digitalWrite(dcs_pin,HIGH);
digitalWrite(cs_pin,LOW);
}
inline void control_mode_off(void) const
{
digitalWrite(cs_pin,HIGH);
}
inline void data_mode_on(void) const
{
digitalWrite(cs_pin,HIGH);
digitalWrite(dcs_pin,LOW);
}
inline void data_mode_off(void) const
{
digitalWrite(dcs_pin,HIGH);
}
inline void save_our_spi(void)
{
my_SPSR = SPSR;
my_SPCR = SPCR;
}
inline void set_our_spi(void)
{
SPSR = my_SPSR;
SPCR = my_SPCR;
}
uint16_t read_register(uint8_t _reg) const;
void write_register(uint8_t _reg,uint16_t _value) const;
void sdi_send_buffer(const uint8_t* data,size_t len);
void sdi_send_zeroes(size_t length);
void print_byte_register(uint8_t reg) const;
/**
* Load a user code plugin
*
* @param buf Location of memory (in PROGMEM) where the code is
* @param len Number of words to load
*/
void loadUserCode(const uint16_t* buf, size_t len) const;
void loadMidiCode() const;
public:
/**
* Constructor
*
* Only sets pin values. Doesn't do touch the chip. Be sure to call begin()!
*/
VS1003( uint8_t _cs_pin, uint8_t _dcs_pin, uint8_t _dreq_pin, uint8_t _reset_pin);
/**
* Begin operation
*
* Sets pins correctly, and prepares SPI bus.
*/
void begin(void);
/**
* Initialize real time MIDI
*
*/
void beginMidi(void);
/**
* Send a MIDI message
*/
void sendMidiMessage(uint8_t cmd, uint8_t data1, uint8_t data2);
/**
* Prepare to start playing
*
* Call this each time a new song starts.
*/
void startSong(void);
/**
* Play a chunk of data. Copies the data to the chip. Blocks until complete.
*
* @param data Pointer to where the data lives
* @param len How many bytes of data to play
*/
void playChunk(const uint8_t* data, size_t len);
/**
* Finish playing a song.
*
* Call this after the last playChunk call.
*/
void stopSong(void);
/**
* Print configuration details
*
* Dumps all registers to stdout. Be sure to have stdout configured first
* (see fdevopen() in avr/io.h).
*/
void printDetails(void) const;
/**
* Set the player volume
*
* @param vol Volume level from 0-255, lower is louder.
*/
void setVolume(uint8_t vol) const;
};
#endif // __VS1003_H__
// vim:cin:ai:sts=2 sw=2 ft=cpp