mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 07:04:14 +03:00 
			
		
		
		
	DFRPH: Add string based constructor
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
		 Adelin Dobre
					Adelin Dobre
				
			
				
					committed by
					
						 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
			
				
	
			
			
			 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
						parent
						
							7ee242b668
						
					
				
				
					commit
					9ba72d8717
				
			| @@ -24,6 +24,7 @@ | |||||||
|  |  | ||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <assert.h> | ||||||
|  |  | ||||||
| #include "dfrph.h" | #include "dfrph.h" | ||||||
|  |  | ||||||
| @@ -60,7 +61,11 @@ dfrph_context dfrph_init(int16_t pin) | |||||||
|  |  | ||||||
| void dfrph_close(dfrph_context dev) | void dfrph_close(dfrph_context dev) | ||||||
| { | { | ||||||
|     mraa_aio_close(dev->aio); |     assert(dev != NULL); | ||||||
|  |    | ||||||
|  |     if(dev->aio) | ||||||
|  |         mraa_aio_close(dev->aio); | ||||||
|  |      | ||||||
|     free(dev); |     free(dev); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ | |||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
|  |  | ||||||
| #include "dfrph.hpp" | #include "dfrph.hpp" | ||||||
|  | #include "upm_string_parser.hpp" | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| @@ -36,6 +37,63 @@ DFRPH::DFRPH(int pin, float vref) : _dev(dfrph_init(pin)) | |||||||
|                 ": dfrph_init() failed, invalid pin?"); |                 ": dfrph_init() failed, invalid pin?"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | DFRPH::DFRPH(std::string initStr) : mraaIo(initStr) | ||||||
|  | { | ||||||
|  |     mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); | ||||||
|  |     std::vector<std::string> upmTokens; | ||||||
|  |  | ||||||
|  |     if(!mraaIo.getLeftoverStr().empty()) { | ||||||
|  |         upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     _dev = (dfrph_context) malloc(sizeof(struct _dfrph_context)); | ||||||
|  |  | ||||||
|  |     if(!_dev) | ||||||
|  |         throw std::runtime_error(std::string(__FUNCTION__) | ||||||
|  |                                 + ": dfrph_init() failed"); | ||||||
|  |      | ||||||
|  |     // make sure MRAA is initialized | ||||||
|  |     int mraa_rv; | ||||||
|  |     if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) | ||||||
|  |     { | ||||||
|  |       dfrph_close(_dev); | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__)  | ||||||
|  |                               + ": mraa_init() failed"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if(!descs->aios) | ||||||
|  |     { | ||||||
|  |       dfrph_close(_dev); | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__)  | ||||||
|  |                               + ": mraa_aio_init() failed");     | ||||||
|  |     } | ||||||
|  |     else  | ||||||
|  |     { | ||||||
|  |       if( !(_dev->aio = descs->aios[0]) ) | ||||||
|  |       { | ||||||
|  |         dfrph_close(_dev); | ||||||
|  |         throw std::runtime_error(std::string(__FUNCTION__)  | ||||||
|  |                                 + ": mraa_aio_init() failed");           | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     /* Set the ref, offset, and scale */ | ||||||
|  |     _dev->m_aref = 5.0; | ||||||
|  |     _dev->m_offset = 0.0; | ||||||
|  |     _dev->m_scale = 1.0;  | ||||||
|  |      | ||||||
|  |     for (std::string tok : upmTokens) { | ||||||
|  |         if(tok.substr(0,10) == "setOffset:") { | ||||||
|  |           float offset = std::stof(tok.substr(10)); | ||||||
|  |           setOffset(offset); | ||||||
|  |         } | ||||||
|  |         if(tok.substr(0,9) == "setScale:") { | ||||||
|  |           float scale = std::stof(tok.substr(9)); | ||||||
|  |           setScale(scale); | ||||||
|  |         } | ||||||
|  |     }     | ||||||
|  | } | ||||||
|  |  | ||||||
| DFRPH::~DFRPH() | DFRPH::~DFRPH() | ||||||
| { | { | ||||||
|     dfrph_close(_dev); |     dfrph_close(_dev); | ||||||
|   | |||||||
| @@ -25,8 +25,10 @@ | |||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> | #include <string> | ||||||
|  | #include <vector> | ||||||
|  |  | ||||||
| #include "dfrph.h" | #include "dfrph.h" | ||||||
|  | #include <mraa/initio.hpp> | ||||||
|  |  | ||||||
| namespace upm { | namespace upm { | ||||||
|   /** |   /** | ||||||
| @@ -99,6 +101,13 @@ namespace upm { | |||||||
|      */ |      */ | ||||||
|     DFRPH(int pin, float vref = 5.0); |     DFRPH(int pin, float vref = 5.0); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Instantiates DFRPH object based on a given string. | ||||||
|  |      * | ||||||
|  |      * @param initStr string containing specific information for DFRPH initialization. | ||||||
|  |      */ | ||||||
|  |     DFRPH(std::string initStr); | ||||||
|  |      | ||||||
|     /** |     /** | ||||||
|      * DFRPH destructor |      * DFRPH destructor | ||||||
|      */ |      */ | ||||||
| @@ -139,6 +148,7 @@ namespace upm { | |||||||
|     DFRPH &operator=(const DFRPH &) {return *this;} |     DFRPH &operator=(const DFRPH &) {return *this;} | ||||||
|  |  | ||||||
|     dfrph_context _dev; |     dfrph_context _dev; | ||||||
|  |     mraa::MraaIo mraaIo; | ||||||
|   }; |   }; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user