upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
wt5001.h
1 /*
2  * Author: Jon Trulson <jtrulson@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 {
76  class WT5001 {
77  public:
78 
79  // WT5001 opcodes
80  typedef enum { NONE = 0x00,
81  PLAY_SD = 0xa0,
82  PLAY_SPI = 0xa1,
83  PLAY_UDISK = 0xa2,
84  PAUSE = 0xa3,
85  STOP = 0xa4,
86  NEXT = 0xa5,
87  PREVIOUS = 0xa6,
88  SET_VOLUME = 0xa7,
89  QUEUE = 0xa8,
90  PLAY_MODE = 0xa9,
91  COPY_SD2FLASH = 0xaa, // not implemented
92  COPY_UDISK2FLASH = 0xab, // not implemented
93  INSERT_SONG = 0xac,
94  SET_DATE = 0xb1,
95  SET_TIME = 0xb2,
96  SET_ALARM = 0xb3,
97  SET_ALARM_DUR = 0xb4, // not implemented
98  CLEAR_ALARM = 0xb5,
99  CLEAR_ALARM_DUR = 0xb6, // not implemented
100  READ_VOLUME = 0xc1,
101  READ_PLAY_STATE = 0xc2,
102  READ_SPI_NUMF = 0xc3,
103  READ_SD_NUMF = 0xc4,
104  READ_UDISK_NUMF = 0xc5,
105  READ_CUR_FNAME = 0xc6,
106  READ_CF_CHAR = 0xc7, // not implemented
107  READ_DATE = 0xd1,
108  READ_TIME = 0xd2
109  } WT5001_OPCODE_T;
110 
111  // play modes
112  typedef enum { NORMAL = 0x00,
113  SINGLE_REPEAT = 0x01,
114  ALL_REPEAT = 0x02,
115  RANDOM = 0x03
116  } WT5001_PLAYMODE_T;
117 
118  // music source
119  typedef enum { SD,
120  SPI,
121  UDISK
122  } WT5001_PLAYSOURCE_T;
123 
129  WT5001(int uart);
130 
134  ~WT5001();
135 
142  bool dataAvailable(unsigned int millis);
143 
154  int readData(char *buffer, size_t len);
155 
163  int writeData(char *buffer, size_t len);
164 
172  bool setupTty(speed_t baud=B9600);
173 
180  bool checkResponse(WT5001_OPCODE_T opcode);
181 
189  bool play(WT5001_PLAYSOURCE_T psrc, uint16_t index);
190 
196  bool stop();
197 
203  bool pause();
204 
210  bool next();
211 
217  bool previous();
218 
224  bool setVolume(uint8_t vol);
225 
232  bool queue(uint16_t index);
233 
240  bool setPlayMode(WT5001_PLAYMODE_T pm);
241 
250  bool insert(uint16_t index);
251 
260  bool setDate(uint16_t year, uint8_t month, uint8_t day);
261 
270  bool setTime(uint8_t hour, uint8_t minute, uint8_t second);
271 
280  bool setAlarm(uint8_t hour, uint8_t minute, uint8_t second);
281 
287  bool clearAlarm();
288 
295  bool getVolume(uint8_t *vol);
296 
303  bool getPlayState(uint8_t *ps);
304 
312  bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf);
313 
320  bool getCurrentFile(uint16_t *curf);
321 
330  bool getDate(uint16_t *year, uint8_t *month, uint8_t *day);
331 
340  bool getTime(uint8_t *hour, uint8_t *minute, uint8_t *second);
341 
342 
343  protected:
344  int ttyFd() { return m_ttyFd; };
345  int setTtyFd(int fd) { m_ttyFd = fd; };
346 
347  private:
348  mraa_uart_context m_uart;
349  int m_ttyFd;
350  };
351 }
352 
353 
bool setDate(uint16_t year, uint8_t month, uint8_t day)
Definition: wt5001.cxx:339
bool stop()
Definition: wt5001.cxx:206
bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf)
Definition: wt5001.cxx:455
int writeData(char *buffer, size_t len)
Definition: wt5001.cxx:110
~WT5001()
Definition: wt5001.cxx:62
WT5001(int uart)
Definition: wt5001.cxx:34
bool setupTty(speed_t baud=B9600)
Definition: wt5001.cxx:131
Definition: a110x.h:29
bool play(WT5001_PLAYSOURCE_T psrc, uint16_t index)
Definition: wt5001.cxx:173
bool setAlarm(uint8_t hour, uint8_t minute, uint8_t second)
Definition: wt5001.cxx:376
bool getVolume(uint8_t *vol)
Definition: wt5001.cxx:409
bool next()
Definition: wt5001.cxx:221
bool dataAvailable(unsigned int millis)
Definition: wt5001.cxx:70
bool queue(uint16_t index)
Definition: wt5001.cxx:289
bool getTime(uint8_t *hour, uint8_t *minute, uint8_t *second)
Definition: wt5001.cxx:550
int readData(char *buffer, size_t len)
Definition: wt5001.cxx:94
bool checkResponse(WT5001_OPCODE_T opcode)
Definition: wt5001.cxx:159
C++ API for the WT5001 Serial MP3 module.
Definition: wt5001.h:76
bool setVolume(uint8_t vol)
Definition: wt5001.cxx:266
bool insert(uint16_t index)
Definition: wt5001.cxx:322
bool getPlayState(uint8_t *ps)
Definition: wt5001.cxx:432
bool setPlayMode(WT5001_PLAYMODE_T pm)
Definition: wt5001.cxx:306
bool clearAlarm()
Definition: wt5001.cxx:394
bool pause()
Definition: wt5001.cxx:251
bool getDate(uint16_t *year, uint8_t *month, uint8_t *day)
Definition: wt5001.cxx:523
bool getCurrentFile(uint16_t *curf)
Definition: wt5001.cxx:497
bool previous()
Definition: wt5001.cxx:236
bool setTime(uint8_t hour, uint8_t minute, uint8_t second)
Definition: wt5001.cxx:358