mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 23:24:20 +03:00 
			
		
		
		
	BH1749: 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
						
							93449a99f2
						
					
				
				
					commit
					a5fa8407a9
				
			| @@ -26,6 +26,14 @@ | |||||||
| #include <string> | #include <string> | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
| #include "bh1749.hpp" | #include "bh1749.hpp" | ||||||
|  | #include "upm_string_parser.hpp" | ||||||
|  |  | ||||||
|  | #define DEFAULT_OP_MODE INT_JUDGE_1 | ||||||
|  | #define DEFAULT_MEAS_TIME MEAS_240MS | ||||||
|  | #define DEFAULT_RGB_GAIN RGB_GAIN_1X | ||||||
|  | #define DEFAULT_IR_GAIN IR_GAIN_1X | ||||||
|  | #define DEFAULT_INT_SOURCE RED | ||||||
|  | #define DEFAULT_THRESHOLD_HIGH 511 | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| @@ -43,6 +51,51 @@ BH1749::BH1749(int bus, int addr) : m_bh1749(bh1749_init(bus, addr)) | |||||||
|                                 "bh1749_init() failed"); |                                 "bh1749_init() failed"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | BH1749::BH1749(std::string initStr) : mraaIo(initStr) | ||||||
|  | { | ||||||
|  |   mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); | ||||||
|  |   std::vector<std::strings> upmTokens; | ||||||
|  |  | ||||||
|  |   m_bh1749 = (bh1749_context)malloc(sizeof(struct _bh1749_context)); | ||||||
|  |   if(!m_bh1749) | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__) + | ||||||
|  |                               "bh1749_init() failed"); | ||||||
|  |  | ||||||
|  |   m_bh1749->i2c = NULL; | ||||||
|  |   m_bh1749->interrupt = NULL; | ||||||
|  |  | ||||||
|  |   if(mraa_init() != MRAA_SUCCESS) { | ||||||
|  |       bh1749_close(m_bh1749); | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__) + | ||||||
|  |                               "bh1749_init() failed"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if(!descs->i2cs) { | ||||||
|  |       bh1749_close(m_bh1749); | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__) | ||||||
|  |                                 + ": mraa_i2c_init() failed"); | ||||||
|  |   } else { | ||||||
|  |       if( !(m_bh1749->i2c = descs->i2cs[0])) { | ||||||
|  |           bh1749_close(m_bh1749); | ||||||
|  |           throw std::runtime_error(std::string(__FUNCTION__) | ||||||
|  |                                     + ": mraa_i2c_init() failed"); | ||||||
|  |       } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if(bh1749_check_who_am_i(dev) != UPM_SUCCESS) | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__) | ||||||
|  |                                 + ": bh1749_init() failed"); | ||||||
|  |  | ||||||
|  |   m_bh1749->enabled = false; | ||||||
|  |   m_bh1749->isrEnabled = false; | ||||||
|  |  | ||||||
|  |   if(bh1749_sensor_init(m_bh1749, DEFAULT_OP_MODE, DEFAULT_MEAS_TIME, | ||||||
|  |       DEFAULT_RGB_GAIN, DEFAULT_IR_GAIN, DEFAULT_INT_SOURCE) != UPM_SUCCESS) | ||||||
|  |       throw std::runtime_error(std::string(__FUNCTION__) + | ||||||
|  |                                 "bh1749_init() failed"); | ||||||
|  |   bh1749_set_threshold_high(m_bh1749, DEFAULT_THRESHOLD_HIGH); | ||||||
|  | } | ||||||
|  |  | ||||||
| BH1749::~BH1749() | BH1749::~BH1749() | ||||||
| { | { | ||||||
|     bh1749_close(m_bh1749); |     bh1749_close(m_bh1749); | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ | |||||||
|  |  | ||||||
| #include <vector> | #include <vector> | ||||||
| #include "bh1749.h" | #include "bh1749.h" | ||||||
|  | #include <mraa/initio.hpp> | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @brief BH1749 Color Sensor |  * @brief BH1749 Color Sensor | ||||||
| @@ -62,6 +63,13 @@ namespace upm { | |||||||
|          */ |          */ | ||||||
|         BH1749(int bus = 0, int addr = 0x39); |         BH1749(int bus = 0, int addr = 0x39); | ||||||
|  |  | ||||||
|  |         /** | ||||||
|  |          * Instantiates BH1749 Color Sensor based on a given string. | ||||||
|  |          * | ||||||
|  |          * @param initStr string containing specific information for BH1749 Sensor initialization. | ||||||
|  |          */ | ||||||
|  |         BH1749(std::string initStr); | ||||||
|  |  | ||||||
|         /** |         /** | ||||||
|          * @brief Close and free sensor |          * @brief Close and free sensor | ||||||
|          */ |          */ | ||||||
| @@ -337,6 +345,7 @@ namespace upm { | |||||||
|  |  | ||||||
|     private: |     private: | ||||||
|         bh1749_context m_bh1749; |         bh1749_context m_bh1749; | ||||||
|  |         mraa::MraaIo mraaIo; | ||||||
|  |  | ||||||
|         /* Disable implicit copy and assignment operators */ |         /* Disable implicit copy and assignment operators */ | ||||||
|         BH1749(const BH1749 &) = delete; |         BH1749(const BH1749 &) = delete; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user