1 /* 2 * Author: Jon Trulson <jtrulson (at) ics.com> 3 * Copyright (c) 2014 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #pragma once 25 26 #include <string> 27 #include <iostream> 28 29 #include <stdint.h> 30 #include <stdlib.h> 31 #include <unistd.h> 32 #include <string.h> 33 #include <fcntl.h> 34 #include <errno.h> 35 #include <termios.h> 36 #include <sys/time.h> 37 #include <sys/select.h> 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 41 #include <mraa/uart.h> 42 43 const int WT5001_DEFAULT_UART = 0; 44 const int WT5001_MAX_VOLUME = 31; 45 46 // protocol start and end codes 47 const uint8_t WT5001_START = 0x7e; 48 const uint8_t WT5001_END = 0x7e; 49 50 namespace upm { 51 /** 52 * @brief WT5001 Serial MP3 module library 53 * @defgroup wt5001 libupm-wt5001 54 * @ingroup seeed uart sound 55 */ 56 /** 57 * @library wt5001 58 * @sensor wt5001 59 * @comname WT5001 Serial MP3 Module 60 * @altname Grove Serial MP3 Player 61 * @type sound 62 * @man seeed 63 * @web http://www.seeedstudio.com/wiki/Grove_%E2%80%93_Serial_MP3_Player 64 * @con uart 65 * 66 * @brief API for the WT5001 Serial MP3 Module 67 * 68 * UPM support for the WT5001 Serial MP3 module. This was tested 69 * specifically with the Grove Serial MP3 module. 70 * 71 * @image html wt5001.jpg 72 * @snippet wt5001.cxx Interesting 73 */ 74 class WT5001 { 75 public: 76 77 // WT5001 opcodes 78 typedef enum { NONE = 0x00, 79 PLAY_SD = 0xa0, 80 PLAY_SPI = 0xa1, 81 PLAY_UDISK = 0xa2, 82 PAUSE = 0xa3, 83 STOP = 0xa4, 84 NEXT = 0xa5, 85 PREVIOUS = 0xa6, 86 SET_VOLUME = 0xa7, 87 QUEUE = 0xa8, 88 PLAY_MODE = 0xa9, 89 COPY_SD2FLASH = 0xaa, // not implemented 90 COPY_UDISK2FLASH = 0xab, // not implemented 91 INSERT_SONG = 0xac, 92 SET_DATE = 0xb1, 93 SET_TIME = 0xb2, 94 SET_ALARM = 0xb3, 95 SET_ALARM_DUR = 0xb4, // not implemented 96 CLEAR_ALARM = 0xb5, 97 CLEAR_ALARM_DUR = 0xb6, // not implemented 98 READ_VOLUME = 0xc1, 99 READ_PLAY_STATE = 0xc2, 100 READ_SPI_NUMF = 0xc3, 101 READ_SD_NUMF = 0xc4, 102 READ_UDISK_NUMF = 0xc5, 103 READ_CUR_FNAME = 0xc6, 104 READ_CF_CHAR = 0xc7, // not implemented 105 READ_DATE = 0xd1, 106 READ_TIME = 0xd2 107 } WT5001_OPCODE_T; 108 109 // play modes 110 typedef enum { NORMAL = 0x00, 111 SINGLE_REPEAT = 0x01, 112 ALL_REPEAT = 0x02, 113 RANDOM = 0x03 114 } WT5001_PLAYMODE_T; 115 116 // music source 117 typedef enum { SD, 118 SPI, 119 UDISK 120 } WT5001_PLAYSOURCE_T; 121 122 /** 123 * WT5001 constructor 124 * 125 * @param uart Default UART to use (0 or 1) 126 */ 127 WT5001(int uart); 128 129 /** 130 * WT5001 destructor 131 */ 132 ~WT5001(); 133 134 /** 135 * Checks to see if there is data available for reading 136 * 137 * @param millis Number of milliseconds to wait; 0 means no waiting 138 * @return True if there is data available for reading 139 */ 140 bool dataAvailable(unsigned int millis); 141 142 /** 143 * Reads any available data in a user-supplied buffer. Note: the 144 * call blocks until data is available to be read. Use 145 * dataAvailable() to determine whether there is data available 146 * beforehand, to avoid blocking. 147 * 148 * @param buffer Buffer to hold the data read 149 * @param len Length of the buffer 150 * @return Number of bytes read 151 */ 152 int readData(char *buffer, int len); 153 154 /** 155 * Writes the data in the buffer to the device 156 * 157 * @param buffer Buffer to hold the data read 158 * @param len Length of the buffer 159 * @return Number of bytes written 160 */ 161 int writeData(char *buffer, int len); 162 163 /** 164 * Sets up proper tty I/O modes and the baud rate. The default 165 * baud rate is 9,600 (B9600). 166 * 167 * @param baud Desired baud rate. 168 * @return True if successful 169 */ 170 bool setupTty(speed_t baud=B9600); 171 172 /** 173 * Gets a command response and returns its validity 174 * 175 * @param index Opcode to verify 176 * @return True if successful 177 */ 178 bool checkResponse(WT5001_OPCODE_T opcode); 179 180 /** 181 * Plays a file from a source 182 * 183 * @param psrc Play source (SD, UDISK, SPI) 184 * @param index File number to play 185 * @return True if successful 186 */ 187 bool play(WT5001_PLAYSOURCE_T psrc, uint16_t index); 188 189 /** 190 * Stops playing 191 * 192 * @return True if successful 193 */ 194 bool stop(); 195 196 /** 197 * Pauses the playback or resumes it if already paused 198 * 199 * @return True if successful 200 */ 201 bool pause(); 202 203 /** 204 * Moves to the next track 205 * 206 * @return True if successful 207 */ 208 bool next(); 209 210 /** 211 * Moves to the previous track 212 * 213 * @return True if successful 214 */ 215 bool previous(); 216 217 /** 218 * Sets the volume. Valid range is 0-31. 0 means mute. 219 * 220 * @return True if successful 221 */ 222 bool setVolume(uint8_t vol); 223 224 /** 225 * Queues a track to play next, when the current song is finished 226 * 227 * @param index File number to queue 228 * @return True if successful 229 */ 230 bool queue(uint16_t index); 231 232 /** 233 * Sets the playback mode 234 * 235 * @param pm Play mode to enable 236 * @return True if successful 237 */ 238 bool setPlayMode(WT5001_PLAYMODE_T pm); 239 240 /** 241 * Inserts a track to play immediately, interrupting the current 242 * track. When the inserted track is finished, the 243 * interrupted track resumes where it was interrupted. 244 * 245 * @param index File number to insert 246 * @return True if successful 247 */ 248 bool insert(uint16_t index); 249 250 /** 251 * Sets the date of the internal clock 252 * 253 * @param year 4-digit year 254 * @param month Month 255 * @param day Day 256 * @return True if successful 257 */ 258 bool setDate(uint16_t year, uint8_t month, uint8_t day); 259 260 /** 261 * Sets the time of the internal clock 262 * 263 * @param hour Hour 264 * @param minute Minute 265 * @param second Second 266 * @return True if successful 267 */ 268 bool setTime(uint8_t hour, uint8_t minute, uint8_t second); 269 270 /** 271 * Sets the alarm 272 * 273 * @param hour Hour 274 * @param minute Minute 275 * @param second Second 276 * @return True if successful 277 */ 278 bool setAlarm(uint8_t hour, uint8_t minute, uint8_t second); 279 280 /** 281 * Clears any alarm that has been set 282 * 283 * @return True if successful 284 */ 285 bool clearAlarm(); 286 287 /** 288 * Gets the current volume 289 * 290 * @param vol Volume 291 * @return True if successful 292 */ 293 bool getVolume(uint8_t *vol); 294 295 /** 296 * Gets the current play state: 1 = playing, 2 = stopped, 3 = paused 297 * 298 * @param ps Play state 299 * @return True if successful 300 */ 301 bool getPlayState(uint8_t *ps); 302 303 /** 304 * Gets the number of files present on the source device 305 * 306 * @param psrc Storage source 307 * @param numf Number of files 308 * @return True if successful 309 */ 310 bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf); 311 312 /** 313 * Gets the index of the current file 314 * 315 * @param curf Index of the current file 316 * @return True if successful 317 */ 318 bool getCurrentFile(uint16_t *curf); 319 320 /** 321 * Gets the device date 322 * 323 * @param year 4-digit year 324 * @param month Month 325 * @param day Day 326 * @return True if successful 327 */ 328 bool getDate(uint16_t *year, uint8_t *month, uint8_t *day); 329 330 /** 331 * Gets the device time 332 * 333 * @param hour Hour 334 * @param minute Minute 335 * @param second Second 336 * @return True if successful 337 */ 338 bool getTime(uint8_t *hour, uint8_t *minute, uint8_t *second); 339 340 341 protected: 342 int ttyFd() { return m_ttyFd; }; 343 344 private: 345 mraa_uart_context m_uart; 346 int m_ttyFd; 347 }; 348 } 349 350 351