upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
wt5001.hpp
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 {
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 
127  WT5001(int uart);
128 
132  ~WT5001();
133 
140  bool dataAvailable(unsigned int millis);
141 
152  int readData(char *buffer, int len);
153 
161  int writeData(char *buffer, int len);
162 
170  bool setupTty(speed_t baud=B9600);
171 
178  bool checkResponse(WT5001_OPCODE_T opcode);
179 
187  bool play(WT5001_PLAYSOURCE_T psrc, uint16_t index);
188 
194  bool stop();
195 
201  bool pause();
202 
208  bool next();
209 
215  bool previous();
216 
222  bool setVolume(uint8_t vol);
223 
230  bool queue(uint16_t index);
231 
238  bool setPlayMode(WT5001_PLAYMODE_T pm);
239 
248  bool insert(uint16_t index);
249 
258  bool setDate(uint16_t year, uint8_t month, uint8_t day);
259 
268  bool setTime(uint8_t hour, uint8_t minute, uint8_t second);
269 
278  bool setAlarm(uint8_t hour, uint8_t minute, uint8_t second);
279 
285  bool clearAlarm();
286 
293  bool getVolume(uint8_t *vol);
294 
301  uint8_t getVolume();
302 
309  bool getPlayState(uint8_t *ps);
310 
317  uint8_t getPlayState();
318 
326  bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf);
327 
335  uint16_t getNumFiles(WT5001_PLAYSOURCE_T psrc);
336 
343  bool getCurrentFile(uint16_t *curf);
344 
351  uint16_t getCurrentFile();
352 
361  bool getDate(uint16_t *year, uint8_t *month, uint8_t *day);
362 
371  bool getTime(uint8_t *hour, uint8_t *minute, uint8_t *second);
372 
373 
374  protected:
375  int ttyFd() { return m_ttyFd; };
376 
377  private:
378  mraa_uart_context m_uart;
379  int m_ttyFd;
380  };
381 }
382 
383 
bool setDate(uint16_t year, uint8_t month, uint8_t day)
Definition: wt5001.cxx:359
int writeData(char *buffer, int len)
Definition: wt5001.cxx:121
bool stop()
Definition: wt5001.cxx:221
bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf)
Definition: wt5001.cxx:493
~WT5001()
Definition: wt5001.cxx:69
uint8_t getVolume()
Definition: wt5001.cxx:452
WT5001(int uart)
Definition: wt5001.cxx:37
int readData(char *buffer, int len)
Definition: wt5001.cxx:100
uint8_t getPlayState()
Definition: wt5001.cxx:484
bool setupTty(speed_t baud=B9600)
Definition: wt5001.cxx:144
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
bool play(WT5001_PLAYSOURCE_T psrc, uint16_t index)
Definition: wt5001.cxx:188
bool setAlarm(uint8_t hour, uint8_t minute, uint8_t second)
Definition: wt5001.cxx:396
bool next()
Definition: wt5001.cxx:236
bool dataAvailable(unsigned int millis)
Definition: wt5001.cxx:77
uint16_t getCurrentFile()
Definition: wt5001.cxx:570
bool queue(uint16_t index)
Definition: wt5001.cxx:309
bool getTime(uint8_t *hour, uint8_t *minute, uint8_t *second)
Definition: wt5001.cxx:606
bool checkResponse(WT5001_OPCODE_T opcode)
Definition: wt5001.cxx:174
API for the WT5001 Serial MP3 Module.
Definition: wt5001.hpp:74
bool setVolume(uint8_t vol)
Definition: wt5001.cxx:281
bool insert(uint16_t index)
Definition: wt5001.cxx:342
bool setPlayMode(WT5001_PLAYMODE_T pm)
Definition: wt5001.cxx:326
bool clearAlarm()
Definition: wt5001.cxx:414
bool pause()
Definition: wt5001.cxx:266
bool getDate(uint16_t *year, uint8_t *month, uint8_t *day)
Definition: wt5001.cxx:579
bool previous()
Definition: wt5001.cxx:251
bool setTime(uint8_t hour, uint8_t minute, uint8_t second)
Definition: wt5001.cxx:378