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
25 #include <iostream>
26 #include <string>
27 #include <stdexcept>
28
29 #include "grovespeaker.h"
30
31 using namespace upm;
32
GroveSpeaker(int pin)33 GroveSpeaker::GroveSpeaker(int pin)
34 {
35 if ( !(m_gpio = mraa_gpio_init(pin)) )
36 throw std::invalid_argument(std::string(__FUNCTION__) +
37 ": mraa_gpio_init() failed, invalid pin?");
38
39 mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
40 m_note_list['a'] = storeNote(1136, 1073, 568, 536, 284, 268);
41 m_note_list['b'] = storeNote(1012, 0, 506, 0, 253, 0);
42 m_note_list['c'] = storeNote(1911, 1804, 956, 902, 478, 451);
43 m_note_list['d'] = storeNote(1703, 1607, 851, 804, 426, 402);
44 m_note_list['e'] = storeNote(1517, 0, 758, 0, 379, 0);
45 m_note_list['f'] = storeNote(1432, 1351, 716, 676, 358, 338);
46 m_note_list['g'] = storeNote(1276, 1204, 638, 602, 319, 301);
47 }
48
~GroveSpeaker()49 GroveSpeaker::~GroveSpeaker()
50 {
51 mraa_gpio_close(m_gpio);
52 }
53
storeNote(int noteDelayLow,int noteDelayLowSharp,int noteDelayMed,int noteDelayMedSharp,int noteDelayHigh,int noteDelayHighSharp)54 NoteData GroveSpeaker::storeNote(int noteDelayLow, int noteDelayLowSharp,
55 int noteDelayMed, int noteDelayMedSharp,
56 int noteDelayHigh, int noteDelayHighSharp)
57 {
58 NoteData note;
59 note.delayTimeLow = noteDelayLow;
60 note.delayTimeLowSharp = noteDelayLowSharp;
61 note.delayTimeMed = noteDelayMed;
62 note.delayTimeMedSharp = noteDelayMedSharp;
63 note.delayTimeHigh = noteDelayHigh;
64 note.delayTimeHighSharp = noteDelayHighSharp;
65 return note;
66 }
67
playAll()68 void GroveSpeaker::playAll()
69 {
70 playSound('c', false, "low");
71 usleep(200000);
72 playSound('d', false, "low");
73 usleep(200000);
74 playSound('e', false, "low");
75 usleep(200000);
76 playSound('f', false, "low");
77 usleep(200000);
78 playSound('g', false, "low");
79 usleep(500000);
80 playSound('a', false, "low");
81 usleep(500000);
82 playSound('b', false, "low");
83 usleep(500000);
84 }
85
playSound(char letter,bool sharp,std::string vocalWeight)86 void GroveSpeaker::playSound(char letter, bool sharp, std::string vocalWeight)
87 {
88 std::map<char, NoteData>::iterator it = m_note_list.find(letter);
89 if(it == m_note_list.end())
90 {
91 std::cout << "The key " << letter << " doesn't exist." << std::endl;
92 return;
93 }
94 NoteData nd = it->second;
95 int delayTime;
96 if (sharp)
97 {
98 if (vocalWeight.compare("low") == 0)
99 delayTime = nd.delayTimeLowSharp;
100 else if (vocalWeight.compare("med") == 0)
101 delayTime = nd.delayTimeMedSharp;
102 else if (vocalWeight.compare("high") == 0)
103 delayTime = nd.delayTimeHighSharp;
104 else
105 {
106 std::cout << "Correct voice weight values are low, med, or high"
107 << std::endl;
108 return;
109 }
110 }
111 else
112 {
113 if (vocalWeight.compare("low") == 0)
114 delayTime = nd.delayTimeLow;
115 else if (vocalWeight.compare("med") == 0)
116 delayTime = nd.delayTimeMed;
117 else if (vocalWeight.compare("high") == 0)
118 delayTime = nd.delayTimeHigh;
119 else
120 {
121 std::cout << "Correct voice weight values are low, med, or high"
122 << std::endl;
123 return;
124 }
125 }
126 // If delayTime is zero, that means you tried to choose a sharp note
127 // for a note that has no sharp
128 if (sharp && !delayTime)
129 {
130 std::cout << "The key " << letter << " doesn't have a sharp note."
131 << std::endl;
132 return;
133 }
134 sound(delayTime);
135 }
136
sound(int note_delay)137 void GroveSpeaker::sound(int note_delay)
138 {
139 mraa_result_t error = MRAA_SUCCESS;
140 for (int i = 0; i < 100; i++)
141 {
142 error = mraa_gpio_write (m_gpio, HIGH);
143 usleep(note_delay);
144 error = mraa_gpio_write (m_gpio, LOW);
145 usleep(note_delay);
146 }
147 if (error != MRAA_SUCCESS)
148 mraa_result_print(error);
149 }
150
151