Speaker: Removed grove dependency

* Renamed all files from grovespeaker to speaker
    * Replaced all instances of grovespeaker with speaker
    * Updated all CMake files
This commit is contained in:
Sisinty Sasmita Patra
2016-09-13 16:09:09 -07:00
committed by Noel Eck
parent c759932ee6
commit 982065a7b7
16 changed files with 58 additions and 58 deletions

View File

@ -0,0 +1,5 @@
upm_mixed_module_init (NAME speaker
DESCRIPTION "UPM Speaker Module"
CPP_HDR speaker.hpp
CPP_SRC speaker.cxx
REQUIRES mraa)

View File

@ -0,0 +1,19 @@
%module javaupm_speaker
%include "../upm.i"
%{
#include "speaker.hpp"
%}
%include "speaker.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_speaker");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

View File

@ -0,0 +1,8 @@
%module jsupm_speaker
%include "../upm.i"
%{
#include "speaker.hpp"
%}
%include "speaker.hpp"

View File

@ -0,0 +1,11 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_speaker
%include "../upm.i"
%feature("autodoc", "3");
%include "speaker.hpp"
%{
#include "speaker.hpp"
%}

150
src/speaker/speaker.cxx Normal file
View File

@ -0,0 +1,150 @@
/*
* Author: Zion Orent <sorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include <stdexcept>
#include "speaker.hpp"
using namespace upm;
Speaker::Speaker(int pin)
{
if ( !(m_gpio = mraa_gpio_init(pin)) )
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
m_note_list['a'] = storeNote(1136, 1073, 568, 536, 284, 268);
m_note_list['b'] = storeNote(1012, 0, 506, 0, 253, 0);
m_note_list['c'] = storeNote(1911, 1804, 956, 902, 478, 451);
m_note_list['d'] = storeNote(1703, 1607, 851, 804, 426, 402);
m_note_list['e'] = storeNote(1517, 0, 758, 0, 379, 0);
m_note_list['f'] = storeNote(1432, 1351, 716, 676, 358, 338);
m_note_list['g'] = storeNote(1276, 1204, 638, 602, 319, 301);
}
Speaker::~Speaker()
{
mraa_gpio_close(m_gpio);
}
NoteData Speaker::storeNote(int noteDelayLow, int noteDelayLowSharp,
int noteDelayMed, int noteDelayMedSharp,
int noteDelayHigh, int noteDelayHighSharp)
{
NoteData note;
note.delayTimeLow = noteDelayLow;
note.delayTimeLowSharp = noteDelayLowSharp;
note.delayTimeMed = noteDelayMed;
note.delayTimeMedSharp = noteDelayMedSharp;
note.delayTimeHigh = noteDelayHigh;
note.delayTimeHighSharp = noteDelayHighSharp;
return note;
}
void Speaker::playAll()
{
playSound('c', false, "low");
usleep(200000);
playSound('d', false, "low");
usleep(200000);
playSound('e', false, "low");
usleep(200000);
playSound('f', false, "low");
usleep(200000);
playSound('g', false, "low");
usleep(500000);
playSound('a', false, "low");
usleep(500000);
playSound('b', false, "low");
usleep(500000);
}
void Speaker::playSound(char letter, bool sharp, std::string vocalWeight)
{
std::map<char, NoteData>::iterator it = m_note_list.find(letter);
if(it == m_note_list.end())
{
std::cout << "The key " << letter << " doesn't exist." << std::endl;
return;
}
NoteData nd = it->second;
int delayTime;
if (sharp)
{
if (vocalWeight.compare("low") == 0)
delayTime = nd.delayTimeLowSharp;
else if (vocalWeight.compare("med") == 0)
delayTime = nd.delayTimeMedSharp;
else if (vocalWeight.compare("high") == 0)
delayTime = nd.delayTimeHighSharp;
else
{
std::cout << "Correct voice weight values are low, med, or high"
<< std::endl;
return;
}
}
else
{
if (vocalWeight.compare("low") == 0)
delayTime = nd.delayTimeLow;
else if (vocalWeight.compare("med") == 0)
delayTime = nd.delayTimeMed;
else if (vocalWeight.compare("high") == 0)
delayTime = nd.delayTimeHigh;
else
{
std::cout << "Correct voice weight values are low, med, or high"
<< std::endl;
return;
}
}
// If delayTime is zero, that means you tried to choose a sharp note
// for a note that has no sharp
if (sharp && !delayTime)
{
std::cout << "The key " << letter << " doesn't have a sharp note."
<< std::endl;
return;
}
sound(delayTime);
}
void Speaker::sound(int note_delay)
{
mraa_result_t error = MRAA_SUCCESS;
for (int i = 0; i < 100; i++)
{
error = mraa_gpio_write (m_gpio, HIGH);
usleep(note_delay);
error = mraa_gpio_write (m_gpio, LOW);
usleep(note_delay);
}
if (error != MRAA_SUCCESS)
mraa_result_print(error);
}

103
src/speaker/speaker.hpp Normal file
View File

@ -0,0 +1,103 @@
/*
* Author: Zion Orent <sorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <string>
#include <map>
#include <unistd.h>
#include <mraa/gpio.h>
#define HIGH 1
#define LOW 0
namespace upm {
/**
* @brief Speaker library
* @defgroup speaker libupm-speaker
* @ingroup seeed gpio sound hak
*/
typedef struct
{
int delayTimeLow;
int delayTimeLowSharp;
int delayTimeMed;
int delayTimeMedSharp;
int delayTimeHigh;
int delayTimeHighSharp;
} NoteData;
/**
* @library speaker
* @sensor speaker libupm-speaker
* @comname Speaker
* @type sound
* @man seeed
* @con gpio
* @kit hak
*
* @brief API for the Speaker
*
* UPM module for the Speaker.
* This sensor can generate different tones and sounds depending on the
* frequency of the input signal.
*
* @image html speaker.jpg
* @snippet speaker.cxx Interesting
*/
class Speaker {
public:
/**
* Speaker constructor
*
* @param pin Digital pin to use
*/
Speaker(int pin);
/**
* Speaker destructor
*/
~Speaker();
/**
* Plays all alto notes (lowest notes)
*
*/
void playAll();
/**
* Plays a sound and a note whether it's sharp or not
*
* @param letter Character name of the note
* ('a', 'b', 'c', 'd', 'e', 'f', or 'g')
* @param sharp If true, plays a sharp version of the note; otherwise, does not play the note
* @param vocalWeight String to determine whether to play a low ("low"),
* a medium ("med"), or a high ("high") note
*/
void playSound(char letter, bool sharp, std::string vocalWeight);
private:
mraa_gpio_context m_gpio;
std::map <char, NoteData> m_note_list;
void sound(int note_delay);
NoteData storeNote(int noteDelayLow, int noteDelayLowSharp,
int noteDelayMed, int noteDelayMedSharp,
int noteDelayHigh, int noteDelayHighSharp);
};
}