1 /*
2  * Author: Zion Orent <sorent@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 <map>
28 #include <unistd.h>
29 #include <mraa/gpio.h>
30 
31 #define HIGH      1
32 #define LOW       0
33 
34 namespace upm {
35   /**
36    * @brief Grove Speaker library
37    * @defgroup grovespeaker libupm-grovespeaker
38    * @ingroup seeed gpio sound hak
39    */
40 typedef struct
41 {
42     int delayTimeLow;
43     int delayTimeLowSharp;
44     int delayTimeMed;
45     int delayTimeMedSharp;
46     int delayTimeHigh;
47     int delayTimeHighSharp;
48 } NoteData;
49   /**
50    * @library grovespeaker
51    * @sensor grovespeaker
52    * @comname Grove Speaker
53    * @type sound
54    * @man seeed
55    * @con gpio
56    * @kit hak
57    *
58    * @brief API for the Grove Speaker
59    *
60    * UPM module for the Grove Speaker.
61    * This sensor can generate different tones and sounds depending on the
62    * frequency of the input signal.
63    *
64    * @image html grovespeaker.jpg
65    * @snippet grovespeaker.cxx Interesting
66    */
67   class GroveSpeaker {
68   public:
69     /**
70      * Grove Speaker constructor
71      *
72      * @param pin Digital pin to use
73      */
74     GroveSpeaker(int pin);
75     /**
76      * GroveSpeaker destructor
77      */
78     ~GroveSpeaker();
79     /**
80      * Plays all alto notes (lowest notes)
81      *
82      */
83     void playAll();
84     /**
85      * Plays a sound and a note whether it's sharp or not
86      *
87      * @param letter Character name of the note
88      * ('a', 'b', 'c', 'd', 'e', 'f', or 'g')
89      * @param sharp If true, plays a sharp version of the note; otherwise, does not play the note
90      * @param vocalWeight String to determine whether to play a low ("low"),
91      * a medium ("med"), or a high ("high") note
92      */
93     void playSound(char letter, bool sharp, std::string vocalWeight);
94 
95   private:
96         mraa_gpio_context m_gpio;
97         std::map <char, NoteData> m_note_list;
98         void sound(int note_delay);
99         NoteData storeNote(int noteDelayLow, int noteDelayLowSharp,
100                            int noteDelayMed, int noteDelayMedSharp,
101                            int noteDelayHigh, int noteDelayHighSharp);
102   };
103 }
104