|
| 1 | +// |
| 2 | +// FILE: MiniMP3.cpp |
| 3 | + |
| 4 | +// VERSION: 0.1.2 |
| 5 | +// PURPOSE: Arduino library for DFRobotics MP3 player and compatibles. |
| 6 | + |
| 7 | + |
| 8 | +#include "MiniMP3.h" |
| 9 | + |
| 10 | + |
| 11 | +// CONSTANTS |
| 12 | +#define MP3_MAX_VOLUME 30 |
| 13 | + |
| 14 | + |
| 15 | +// COMMANDS |
| 16 | +#define MP3_NEXT 0x01 |
| 17 | +#define MP3_PREV 0x02 |
| 18 | +#define MP3_PLAY 0x03 |
| 19 | +#define MP3_VOL_INCR 0x04 |
| 20 | +#define MP3_VOL_DECR 0x05 |
| 21 | +#define MP3_VOL_SET 0x06 |
| 22 | +#define MP3_EQUAL 0x07 |
| 23 | +#define MP3_PLAYMODE 0x08 |
| 24 | +#define MP3_PLAYSOURCE 0x09 |
| 25 | +#define MP3_LOWPOWER 0x0A |
| 26 | +#define MP3_POWER 0x0B |
| 27 | +#define MP3_RESET 0x0C |
| 28 | +#define MP3_PLAYBACK 0x0D |
| 29 | +#define MP3_PAUSE 0x0E |
| 30 | +#define MP3_PLAY_FOLDER 0x0F |
| 31 | +#define MP3_VOL_ADJUST_SET 0x10 |
| 32 | +#define MP3_REPEAT 0x11 |
| 33 | + |
| 34 | + |
| 35 | +// QUERIES |
| 36 | +// not supported (yet) |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +///////////////////////////////////////////////////// |
| 41 | +// |
| 42 | +// PUBLIC |
| 43 | +// |
| 44 | +MINIMP3::MINIMP3(Stream * stream) |
| 45 | +{ |
| 46 | + _stream = stream; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +///////////////////////////////////////////////////// |
| 51 | +// |
| 52 | +// PLAY |
| 53 | +// |
| 54 | +void MINIMP3::play(uint16_t track) |
| 55 | +{ |
| 56 | + command(MP3_PLAY, track); |
| 57 | +} |
| 58 | + |
| 59 | +// void MINIMP3::playFolder(uint8_t folder, uint8_t track) |
| 60 | +// { |
| 61 | + // command(MP3_PLAY_FOLDER, folder, track); |
| 62 | +// } |
| 63 | + |
| 64 | +void MINIMP3::next() |
| 65 | +{ |
| 66 | + command(MP3_NEXT); |
| 67 | +} |
| 68 | + |
| 69 | +void MINIMP3::prev() |
| 70 | +{ |
| 71 | + command(MP3_PREV); |
| 72 | +} |
| 73 | + |
| 74 | +void MINIMP3::stop() |
| 75 | +{ |
| 76 | + command(0x15); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +///////////////////////////////////////////////////// |
| 81 | +// |
| 82 | +// CONTROL |
| 83 | +// |
| 84 | +void MINIMP3::pause() |
| 85 | +{ |
| 86 | + command(MP3_PAUSE); |
| 87 | +} |
| 88 | + |
| 89 | +void MINIMP3::reset() |
| 90 | +{ |
| 91 | + command(MP3_RESET); |
| 92 | +} |
| 93 | + |
| 94 | +void MINIMP3::lowPower() |
| 95 | +{ |
| 96 | + command(MP3_LOWPOWER); |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +///////////////////////////////////////////////////// |
| 101 | +// |
| 102 | +// VOLUME |
| 103 | +// |
| 104 | +void MINIMP3::volume(uint16_t vol) |
| 105 | +{ |
| 106 | + if (vol > MP3_MAX_VOLUME) vol = MP3_MAX_VOLUME; |
| 107 | + command(MP3_VOL_SET, 256 + vol); |
| 108 | +} |
| 109 | + |
| 110 | +void MINIMP3::volumeUp() |
| 111 | +{ |
| 112 | + command(MP3_VOL_INCR); |
| 113 | +} |
| 114 | + |
| 115 | +void MINIMP3::volumeDown() |
| 116 | +{ |
| 117 | + command(MP3_VOL_DECR); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +///////////////////////////////////////////////////// |
| 122 | +// |
| 123 | +// EQUALIZER |
| 124 | +// |
| 125 | +void MINIMP3::equalizer(uint8_t mode) |
| 126 | +{ |
| 127 | + if (mode > 5) mode = MP3_EQ_NORMAL; |
| 128 | + command(MP3_EQUAL, mode); |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +///////////////////////////////////////////////////// |
| 134 | +// |
| 135 | +// PRIVATE |
| 136 | +// |
| 137 | +void MINIMP3::command(uint8_t cmd, uint16_t arg) |
| 138 | +{ |
| 139 | + uint8_t arg2 = arg & 0xFF; |
| 140 | + uint8_t arg1 = arg >> 8; |
| 141 | + command(cmd, arg1, arg2); |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +void MINIMP3::command(uint8_t cmd, uint8_t arg1, uint8_t arg2) |
| 146 | +{ |
| 147 | + uint8_t buffer[10] = {0x7E, // Start == $ |
| 148 | + 0xFF, // version |
| 149 | + 0x06, // length |
| 150 | + cmd, |
| 151 | + 0x00, // 0 = no feedback |
| 152 | + arg1, // param 1 |
| 153 | + arg2, // param 2 |
| 154 | + 0x00, // CheckSumHigh |
| 155 | + 0x00, // CheckSumLow |
| 156 | + 0xEF // End Byte |
| 157 | + }; |
| 158 | + |
| 159 | + uint16_t chksum = 0; |
| 160 | + for (int i = 1; i < 7; i++ ) |
| 161 | + { |
| 162 | + chksum += buffer[i]; |
| 163 | + } |
| 164 | + chksum = -chksum; |
| 165 | + buffer[7] = (chksum >> 8); // CheckSumHigh |
| 166 | + buffer[8] = (chksum & 0xFF); // CheckSumLow |
| 167 | + |
| 168 | + _stream->write(buffer, 10); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +// -- END OF FILE -- |
| 173 | + |
0 commit comments