mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 07:04:14 +03:00 
			
		
		
		
	AM2315: Add string based constructor for Temperature & Humidity Sensor
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
						
							a79f85cb17
						
					
				
				
					commit
					7265373a47
				
			| @@ -30,6 +30,7 @@ | ||||
| #include <time.h> | ||||
|  | ||||
| #include "am2315.hpp" | ||||
| #include "upm_string_parser.hpp" | ||||
|  | ||||
| using namespace upm; | ||||
|  | ||||
| @@ -47,7 +48,7 @@ AM2315::AM2315(int bus, int devAddr) { | ||||
|  | ||||
|     m_base_priority = sched_getscheduler(0); | ||||
|  | ||||
|     if ( !(m_i2ControlCtx = mraa_i2c_init(m_bus)) )  | ||||
|     if ( !(m_i2ControlCtx = mraa_i2c_init(m_bus)) ) | ||||
|       { | ||||
|         throw std::invalid_argument(std::string(__FUNCTION__) + | ||||
|                                     ": mraa_i2c_init() failed"); | ||||
| @@ -68,6 +69,52 @@ AM2315::AM2315(int bus, int devAddr) { | ||||
|             m_name, m_model, m_version, m_id ); | ||||
| } | ||||
|  | ||||
| AM2315::AM2315(std::string initStr) : mraaIo(initStr) | ||||
| { | ||||
|     m_temperature = 0; | ||||
|     m_humidity    = 0; | ||||
|     m_last_time = 0; | ||||
|  | ||||
|     m_name = g_name; | ||||
|  | ||||
|     m_base_priority = sched_getscheduler(0); | ||||
|  | ||||
|     mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); | ||||
|  | ||||
|     if(!descs->i2cs) | ||||
|     { | ||||
|         throw std::invalid_argument(std::string(__FUNCTION__) + | ||||
|                                     ": mraa_i2c_init() failed"); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         if( !(m_i2ControlCtx = descs->i2cs[0]) ) | ||||
|         { | ||||
|             throw std::invalid_argument(std::string(__FUNCTION__) + | ||||
|                                 ": mraa_i2c_init() failed"); | ||||
|         } | ||||
|     } | ||||
|     m_model = i2cReadReg_16(AM2315_MODEL); | ||||
|     m_version = i2cReadReg_8(AM2315_VERSION); | ||||
|     m_id = i2cReadReg_32(AM2315_ID); | ||||
|  | ||||
|     fprintf(stdout,"%s: Model: 0x%04x Version: 0x%02x ID: 0x%08x\n", | ||||
|             m_name, m_model, m_version, m_id ); | ||||
|  | ||||
|     std::vector<std::string> upmTokens; | ||||
|  | ||||
|     if(!mraaIo.getLeftoverStr().empty()) | ||||
|     { | ||||
|         upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); | ||||
|     } | ||||
|  | ||||
|     for (std::string tok : upmTokens) { | ||||
|       if(tok.substr(0,13) == "updateValues:") { | ||||
|           update_values(); | ||||
|       } | ||||
|     } | ||||
| } | ||||
|  | ||||
| AM2315::~AM2315() { | ||||
|     mraa_i2c_stop(m_i2ControlCtx); | ||||
| } | ||||
|   | ||||
| @@ -25,8 +25,10 @@ | ||||
|  | ||||
| #include <string> | ||||
| #include <mraa/i2c.h> | ||||
| #include <mraa/initio.hpp> | ||||
| #include <math.h> | ||||
|  | ||||
|  | ||||
| #define AM2315_NAME "am2315" | ||||
| #define AM2315_I2C_ADDRESS 0x5c | ||||
|  | ||||
| @@ -87,6 +89,13 @@ class AM2315 { | ||||
|          */ | ||||
|         AM2315 (int bus, int devAddr=AM2315_I2C_ADDRESS); | ||||
|  | ||||
|         /** | ||||
|          * Instantiates AM2315 Temperature & Humidity Sensor based on a given string. | ||||
|          * | ||||
|          * @param initStr string containing specific information for AM2315 initialization. | ||||
|          */ | ||||
|         AM2315 (std::string initStr); | ||||
|  | ||||
|         /** | ||||
|          * AM2315 object destructor; basically, it closes the I2C connection. | ||||
|          */ | ||||
| @@ -180,6 +189,7 @@ class AM2315 { | ||||
|         int m_controlAddr; | ||||
|         int m_bus; | ||||
|         mraa_i2c_context m_i2ControlCtx; | ||||
|         mraa::MraaIo mraaIo; | ||||
|  | ||||
|         void update_values(void); | ||||
|         uint8_t i2cReadReg(int reg, uint8_t* data, int ilen); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user