Add string based constructors for UPM sensors

Signed-off-by: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
Signed-off-by: Stefan Andritoiu <stefan.andritoiu@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
mihais
2018-03-29 18:03:59 +03:00
committed by Mihai Tudor Panu
parent e4f106a0c7
commit 58ee51c767
11 changed files with 273 additions and 15 deletions

View File

@ -25,14 +25,17 @@
* 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 <unistd.h>
#include <string>
#include <iostream>
#include "mraa/initio.hpp"
#include "upm_string_parser.hpp"
#include <upm_utilities.h>
#include "buzzer.hpp"
using namespace upm;
using namespace std;
@ -43,33 +46,73 @@ Buzzer::Buzzer(int pinNumber) : m_buzzer(buzzer_init(pinNumber))
": buzzer_init() failed");
}
Buzzer::Buzzer(std::string initStr)
{
mraa::MraaIo mraaIo(initStr);
std::vector<std::string> upmTokens;
if (mraaIo.getLeftoverStr() != "") {
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
}
m_buzzer = nullptr;
if (!mraaIo.pwms.empty()) {
m_buzzer = mraaIo.pwms[0];
}
if (m_buzzer == nullptr) {
throw std::runtime_error(std::string(__FUNCTION__) +
": null buzzer context");
}
volume = 1.0;
m_buzzer.enable(true);
/*
for (std::string tok : upmTokens) {
if (tok.substr(0, 4) == "vol:") {
// setVolume(::atof(tok.substr(4));
} else {}
}*/
}
Buzzer::~Buzzer()
{
buzzer_close(m_buzzer);
stopSound();
m_buzzer.enable(false);
}
void Buzzer::setVolume(float vol)
{
buzzer_set_volume(m_buzzer, vol);
volume = vol;
}
float Buzzer::getVolume()
{
return buzzer_get_volume(m_buzzer);
return volume;
}
int Buzzer::playSound(int note, int delay)
{
if (buzzer_play_sound(m_buzzer, note, delay))
throw std::runtime_error(std::string(__FUNCTION__) +
": buzzer_play_sound() failed");
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();
}
return note;
}
void Buzzer::stopSound()
{
if (buzzer_stop_sound(m_buzzer))
throw std::runtime_error(std::string(__FUNCTION__) +
": buzzer_stop_sound() failed");
m_buzzer.period_us(1);
m_buzzer.write(0);
}

View File

@ -29,6 +29,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <upm.h>
#include <mraa/pwm.h>
@ -110,6 +111,38 @@ 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

View File

@ -28,7 +28,9 @@
#pragma once
#include <string>
#include <buzzer.h>
#include <vector>
#include <mraa/pwm.hpp>
//#include <buzzer.h>
namespace upm {
@ -68,6 +70,14 @@ namespace upm {
*/
Buzzer(int pinNumber);
/**
* Instantiates a Buzzer object based on a given string.
*
* @param initStr string containing specific information for Buzzer initialization.
* Usage: TODO
*/
Buzzer(std::string initStr);
/**
* Buzzer object destructor.
*/
@ -115,10 +125,13 @@ namespace upm {
{
return m_name;
}
protected:
std::string m_name;
buzzer_context m_buzzer;
mraa::Pwm m_buzzer;
float volume;
bool initialized;
//buzzer_context m_buzzer;
private:
/* Disable implicit copy and assignment operators */
Buzzer(const Buzzer&) = delete;