mirror of
https://github.com/eclipse/upm.git
synced 2025-03-14 20:47:30 +03:00
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:
parent
e4f106a0c7
commit
58ee51c767
12
examples/c++/buzzer-initio.cxx
Normal file
12
examples/c++/buzzer-initio.cxx
Normal file
@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "buzzer.hpp"
|
||||
#include "buzzer_tones.h"
|
||||
#include "upm_utilities.h"
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
upm::Buzzer buzzer("p:33,vol:1.0");
|
||||
return 0;
|
||||
}
|
36
include/upm_string_parser.hpp
Normal file
36
include/upm_string_parser.hpp
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// NOTE: A Logging mechanism for the whole library should be implemenented.
|
||||
#include <iostream>
|
||||
|
||||
namespace upm {
|
||||
class UpmStringParser {
|
||||
public:
|
||||
static std::vector<std::string> parse(std::string initStr, std::string delim = ",") {
|
||||
if (initStr.empty()) {
|
||||
std::cout << "parse(): NULL or empty string given as argument." << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::string> strTokens;
|
||||
size_t start = 0;
|
||||
size_t end = initStr.find(delim);
|
||||
|
||||
while (end != std::string::npos) {
|
||||
strTokens.push_back(initStr.substr(start, end - start));
|
||||
start = end + delim.length();
|
||||
end = initStr.find(delim, start);
|
||||
}
|
||||
strTokens.push_back(initStr.substr(start, end));
|
||||
|
||||
for (auto i : strTokens)
|
||||
std::cout << i << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
return strTokens;
|
||||
}
|
||||
};
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
5
src/initio/CMakeLists.txt
Normal file
5
src/initio/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
upm_mixed_module_init (NAME initioSampleModule
|
||||
DESCRIPTION "initio sample module"
|
||||
CPP_HDR initioSampleModule.hpp
|
||||
CPP_SRC initioSampleModule.cpp
|
||||
REQUIRES mraa)
|
18
src/initio/initioSampleModule.cpp
Normal file
18
src/initio/initioSampleModule.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "initioSampleModule.hpp"
|
||||
|
||||
using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
initioSampleModule::initioSampleModule(std::string initStr) : mraa::MraaIo(initStr)
|
||||
{
|
||||
// Do some processing here
|
||||
//std::cout << "111 " << leftoverStr << std::endl;
|
||||
}
|
||||
|
||||
initioSampleModule::~initioSampleModule()
|
||||
{
|
||||
}
|
17
src/initio/initioSampleModule.hpp
Normal file
17
src/initio/initioSampleModule.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mraa/initio.hpp>
|
||||
|
||||
namespace upm {
|
||||
class initioSampleModule : public mraa::MraaIo {
|
||||
public:
|
||||
initioSampleModule(std::string initStr);
|
||||
~initioSampleModule();
|
||||
|
||||
private:
|
||||
//mraa_gpio_context m_gpio;
|
||||
};
|
||||
}
|
||||
|
||||
|
8
src/initio/jsupm_initioSampleModule.i
Normal file
8
src/initio/jsupm_initioSampleModule.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_initioSampleModule
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "initioSampleModule.hpp"
|
||||
%}
|
||||
|
||||
%include "initioSampleModule.hpp"
|
9
src/initio/pyupm_initioSampleModule.i
Normal file
9
src/initio/pyupm_initioSampleModule.i
Normal file
@ -0,0 +1,9 @@
|
||||
// Include doxygen-generated documentation
|
||||
%include "pyupm_doxy2swig.i"
|
||||
%module pyupm_initioSampleModule
|
||||
%include "../upm.i"
|
||||
|
||||
%include "initioSampleModule.hpp"
|
||||
%{
|
||||
#include "initioSampleModule.hpp"
|
||||
%}
|
64
src/interfaces/UpmObject.hpp
Normal file
64
src/interfaces/UpmObject.hpp
Normal file
@ -0,0 +1,64 @@
|
||||
/* NOTE: WIP */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "upm/upm_types.h"
|
||||
#include <mraa/initio.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
static const std::string DELIM = ":";
|
||||
|
||||
namespace upm
|
||||
{
|
||||
class UpmObject
|
||||
{
|
||||
public:
|
||||
UpmObject();
|
||||
UpmObject(std::string initStr) : mraaIo(initStr)
|
||||
{
|
||||
parseUpmInitString(mraaIo.getLeftoverString());
|
||||
|
||||
for (auto i : strTokens) {
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
~UpmObject() {};
|
||||
|
||||
protected:
|
||||
std::vector<std::string> getParsedTokens()
|
||||
{
|
||||
return strTokens;
|
||||
}
|
||||
|
||||
mraa::MraaIo mraaIo;
|
||||
|
||||
private:
|
||||
upm_result_t parseUpmInitString(std::string initStr)
|
||||
{
|
||||
upm_result_t result = UPM_SUCCESS;
|
||||
|
||||
if (initStr == "") {
|
||||
// No specific UPM init string provided.
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t start = 0;
|
||||
size_t end = initStr.find(DELIM);
|
||||
|
||||
while (end != std::string::npos) {
|
||||
strTokens.push_back(initStr.substr(start, end - start));
|
||||
start = end + DELIM.length();
|
||||
end = initStr.find(DELIM, start);
|
||||
}
|
||||
strTokens.push_back(initStr.substr(start, end));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> strTokens;
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user