mirror of
https://github.com/eclipse/upm.git
synced 2025-03-14 20:47:30 +03:00
Add string based constructor for Buzzer and an example to initialize it
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
parent
58ee51c767
commit
32b73a1d25
@ -7,6 +7,6 @@
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
upm::Buzzer buzzer("p:33,vol:1.0");
|
||||
upm::Buzzer buzzer("p:32,vol:0.01,play:3800:500000");
|
||||
return 0;
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ main(int argc, char** argv)
|
||||
int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, BUZZER_FA, BUZZER_SOL, BUZZER_LA, BUZZER_SI };
|
||||
|
||||
// create Buzzer instance
|
||||
upm::Buzzer sound(5);
|
||||
upm::Buzzer sound(32);
|
||||
// print sensor name
|
||||
std::cout << sound.name() << std::endl;
|
||||
|
||||
|
@ -63,7 +63,7 @@ buzzer_context buzzer_init(int pin)
|
||||
|
||||
mraa_pwm_enable(dev->pwm, 1);
|
||||
|
||||
buzzer_set_volume(dev, 1.0);
|
||||
buzzer_set_volume(dev, 0.01);
|
||||
|
||||
dev->initialized = true;
|
||||
return dev;
|
||||
|
@ -30,7 +30,6 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "mraa/initio.hpp"
|
||||
#include "upm_string_parser.hpp"
|
||||
#include <upm_utilities.h>
|
||||
#include "buzzer.hpp"
|
||||
@ -46,73 +45,95 @@ Buzzer::Buzzer(int pinNumber) : m_buzzer(buzzer_init(pinNumber))
|
||||
": buzzer_init() failed");
|
||||
}
|
||||
|
||||
Buzzer::Buzzer(std::string initStr)
|
||||
Buzzer::Buzzer(std::string initStr) : mraaIo(initStr)
|
||||
{
|
||||
mraa::MraaIo mraaIo(initStr);
|
||||
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
|
||||
std::vector<std::string> upmTokens;
|
||||
|
||||
if (mraaIo.getLeftoverStr() != "") {
|
||||
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
|
||||
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
|
||||
}
|
||||
|
||||
m_buzzer = nullptr;
|
||||
if (!mraaIo.pwms.empty()) {
|
||||
m_buzzer = mraaIo.pwms[0];
|
||||
m_buzzer = (buzzer_context)malloc(sizeof(struct _buzzer_context));
|
||||
|
||||
if (!m_buzzer)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": buzzer_init() failed");
|
||||
|
||||
m_buzzer->pwm = NULL;
|
||||
m_buzzer->volume = 0.0;
|
||||
m_buzzer->initialized = false;
|
||||
|
||||
// make sure MRAA is initialized
|
||||
int mraa_rv;
|
||||
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
|
||||
{
|
||||
buzzer_close(m_buzzer);
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": mraa_init() failed");
|
||||
}
|
||||
|
||||
if (m_buzzer == nullptr) {
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": null buzzer context");
|
||||
if(!descs->pwms)
|
||||
{
|
||||
buzzer_close(m_buzzer);
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": mraa_pwm_init() failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
if( !(m_buzzer->pwm = descs->pwms[0]) )
|
||||
{
|
||||
buzzer_close(m_buzzer);
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": mraa_pwm_init() failed");
|
||||
}
|
||||
}
|
||||
mraa_pwm_enable(m_buzzer->pwm, 1);
|
||||
|
||||
volume = 1.0;
|
||||
m_buzzer.enable(true);
|
||||
|
||||
/*
|
||||
for (std::string tok : upmTokens) {
|
||||
if (tok.substr(0, 4) == "vol:") {
|
||||
// setVolume(::atof(tok.substr(4));
|
||||
} else {}
|
||||
}*/
|
||||
if(tok.substr(0, 4) == "vol:") {
|
||||
float vol = std::stof(tok.substr(4));
|
||||
setVolume(vol);
|
||||
}
|
||||
if(tok.substr(0, 5) == "play:") {
|
||||
std::string::size_type sz;
|
||||
int note = std::stoi(tok.substr(5), &sz);
|
||||
tok = tok.substr(5);
|
||||
int delay = std::stoi(tok.substr(sz+1));
|
||||
playSound(note, delay);
|
||||
}
|
||||
}
|
||||
|
||||
m_buzzer->initialized = true;
|
||||
}
|
||||
|
||||
Buzzer::~Buzzer()
|
||||
{
|
||||
stopSound();
|
||||
m_buzzer.enable(false);
|
||||
buzzer_close(m_buzzer);
|
||||
}
|
||||
|
||||
void Buzzer::setVolume(float vol)
|
||||
{
|
||||
volume = vol;
|
||||
buzzer_set_volume(m_buzzer, vol);
|
||||
}
|
||||
|
||||
float Buzzer::getVolume()
|
||||
{
|
||||
return volume;
|
||||
return buzzer_get_volume(m_buzzer);
|
||||
}
|
||||
|
||||
int Buzzer::playSound(int note, int delay)
|
||||
{
|
||||
if (m_buzzer.period_us(note) != MRAA_SUCCESS) {
|
||||
cout << "period() error\n";
|
||||
}
|
||||
|
||||
if (m_buzzer.write(volume * 0.5)) {
|
||||
cout << "write() error\n";
|
||||
}
|
||||
|
||||
if (delay >= 0) {
|
||||
upm_delay_us(delay);
|
||||
stopSound();
|
||||
}
|
||||
|
||||
if (buzzer_play_sound(m_buzzer, note, delay))
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": buzzer_play_sound() failed");
|
||||
return note;
|
||||
}
|
||||
|
||||
void Buzzer::stopSound()
|
||||
{
|
||||
m_buzzer.period_us(1);
|
||||
m_buzzer.write(0);
|
||||
if (buzzer_stop_sound(m_buzzer))
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": buzzer_stop_sound() failed");
|
||||
}
|
||||
|
||||
|
@ -111,38 +111,6 @@ extern "C" {
|
||||
*/
|
||||
float buzzer_get_volume(const buzzer_context dev);
|
||||
|
||||
char** upm_parse_init_str(const char* str, const char* delims, int *num_tokens)
|
||||
{
|
||||
char *saveptr, *tok, *s, *p_str;
|
||||
char **output = NULL;
|
||||
size_t output_size = 0;
|
||||
|
||||
p_str = strdup(str);
|
||||
|
||||
for (s = p_str; ; s = NULL) {
|
||||
tok = strtok_r(s, delims, &saveptr);
|
||||
if (tok == NULL)
|
||||
break;
|
||||
output = (char**)realloc(output, (++output_size) * sizeof(char*));
|
||||
output[output_size - 1] = (char*)calloc(strlen(tok) + 1, sizeof(char));
|
||||
strncpy(output[output_size - 1], tok, strlen(tok));
|
||||
}
|
||||
*num_tokens = output_size;
|
||||
|
||||
free(p_str);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void upm_delete_parsed_str(char **str, int num_tokens)
|
||||
{
|
||||
for (int i = 0; i < num_tokens; ++i) {
|
||||
free(str[i]);
|
||||
}
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -30,7 +30,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mraa/pwm.hpp>
|
||||
//#include <buzzer.h>
|
||||
#include <mraa/initio.hpp>
|
||||
#include <buzzer.h>
|
||||
|
||||
namespace upm {
|
||||
|
||||
@ -74,7 +75,6 @@ namespace upm {
|
||||
* Instantiates a Buzzer object based on a given string.
|
||||
*
|
||||
* @param initStr string containing specific information for Buzzer initialization.
|
||||
* Usage: TODO
|
||||
*/
|
||||
Buzzer(std::string initStr);
|
||||
|
||||
@ -128,10 +128,8 @@ namespace upm {
|
||||
|
||||
protected:
|
||||
std::string m_name;
|
||||
mraa::Pwm m_buzzer;
|
||||
float volume;
|
||||
bool initialized;
|
||||
//buzzer_context m_buzzer;
|
||||
mraa::MraaIo mraaIo;
|
||||
buzzer_context m_buzzer;
|
||||
private:
|
||||
/* Disable implicit copy and assignment operators */
|
||||
Buzzer(const Buzzer&) = delete;
|
||||
|
Loading…
x
Reference in New Issue
Block a user