mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
Add string based constructor for Buzzer and an example to initialize it
This commit is contained in:
parent
a1accb65df
commit
145db5997b
@ -7,6 +7,6 @@
|
|||||||
int
|
int
|
||||||
main(int argc, char** argv)
|
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;
|
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 };
|
int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, BUZZER_FA, BUZZER_SOL, BUZZER_LA, BUZZER_SI };
|
||||||
|
|
||||||
// create Buzzer instance
|
// create Buzzer instance
|
||||||
upm::Buzzer sound(5);
|
upm::Buzzer sound(32);
|
||||||
// print sensor name
|
// print sensor name
|
||||||
std::cout << sound.name() << std::endl;
|
std::cout << sound.name() << std::endl;
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ buzzer_context buzzer_init(int pin)
|
|||||||
|
|
||||||
mraa_pwm_enable(dev->pwm, 1);
|
mraa_pwm_enable(dev->pwm, 1);
|
||||||
|
|
||||||
buzzer_set_volume(dev, 1.0);
|
buzzer_set_volume(dev, 0.01);
|
||||||
|
|
||||||
dev->initialized = true;
|
dev->initialized = true;
|
||||||
return dev;
|
return dev;
|
||||||
|
@ -30,7 +30,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "mraa/initio.hpp"
|
|
||||||
#include "upm_string_parser.hpp"
|
#include "upm_string_parser.hpp"
|
||||||
#include <upm_utilities.h>
|
#include <upm_utilities.h>
|
||||||
#include "buzzer.hpp"
|
#include "buzzer.hpp"
|
||||||
@ -46,73 +45,95 @@ Buzzer::Buzzer(int pinNumber) : m_buzzer(buzzer_init(pinNumber))
|
|||||||
": buzzer_init() failed");
|
": 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;
|
std::vector<std::string> upmTokens;
|
||||||
|
|
||||||
if (mraaIo.getLeftoverStr() != "") {
|
if (mraaIo.getLeftoverStr() != "") {
|
||||||
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
|
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_buzzer = nullptr;
|
m_buzzer = (buzzer_context)malloc(sizeof(struct _buzzer_context));
|
||||||
if (!mraaIo.pwms.empty()) {
|
|
||||||
m_buzzer = mraaIo.pwms[0];
|
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) {
|
if(!descs->pwms)
|
||||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
{
|
||||||
": null buzzer context");
|
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) {
|
for (std::string tok : upmTokens) {
|
||||||
if (tok.substr(0, 4) == "vol:") {
|
if(tok.substr(0, 4) == "vol:") {
|
||||||
// setVolume(::atof(tok.substr(4));
|
float vol = std::stof(tok.substr(4));
|
||||||
} else {}
|
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()
|
Buzzer::~Buzzer()
|
||||||
{
|
{
|
||||||
stopSound();
|
buzzer_close(m_buzzer);
|
||||||
m_buzzer.enable(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buzzer::setVolume(float vol)
|
void Buzzer::setVolume(float vol)
|
||||||
{
|
{
|
||||||
volume = vol;
|
buzzer_set_volume(m_buzzer, vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Buzzer::getVolume()
|
float Buzzer::getVolume()
|
||||||
{
|
{
|
||||||
return volume;
|
return buzzer_get_volume(m_buzzer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Buzzer::playSound(int note, int delay)
|
int Buzzer::playSound(int note, int delay)
|
||||||
{
|
{
|
||||||
if (m_buzzer.period_us(note) != MRAA_SUCCESS) {
|
if (buzzer_play_sound(m_buzzer, note, delay))
|
||||||
cout << "period() error\n";
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
}
|
": buzzer_play_sound() failed");
|
||||||
|
|
||||||
if (m_buzzer.write(volume * 0.5)) {
|
|
||||||
cout << "write() error\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delay >= 0) {
|
|
||||||
upm_delay_us(delay);
|
|
||||||
stopSound();
|
|
||||||
}
|
|
||||||
|
|
||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buzzer::stopSound()
|
void Buzzer::stopSound()
|
||||||
{
|
{
|
||||||
m_buzzer.period_us(1);
|
if (buzzer_stop_sound(m_buzzer))
|
||||||
m_buzzer.write(0);
|
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);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -30,7 +30,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <mraa/pwm.hpp>
|
#include <mraa/pwm.hpp>
|
||||||
//#include <buzzer.h>
|
#include <mraa/initio.hpp>
|
||||||
|
#include <buzzer.h>
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
|
|
||||||
@ -74,7 +75,6 @@ namespace upm {
|
|||||||
* Instantiates a Buzzer object based on a given string.
|
* Instantiates a Buzzer object based on a given string.
|
||||||
*
|
*
|
||||||
* @param initStr string containing specific information for Buzzer initialization.
|
* @param initStr string containing specific information for Buzzer initialization.
|
||||||
* Usage: TODO
|
|
||||||
*/
|
*/
|
||||||
Buzzer(std::string initStr);
|
Buzzer(std::string initStr);
|
||||||
|
|
||||||
@ -128,10 +128,8 @@ namespace upm {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
mraa::Pwm m_buzzer;
|
mraa::MraaIo mraaIo;
|
||||||
float volume;
|
buzzer_context m_buzzer;
|
||||||
bool initialized;
|
|
||||||
//buzzer_context m_buzzer;
|
|
||||||
private:
|
private:
|
||||||
/* Disable implicit copy and assignment operators */
|
/* Disable implicit copy and assignment operators */
|
||||||
Buzzer(const Buzzer&) = delete;
|
Buzzer(const Buzzer&) = delete;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user