mirror of
https://github.com/eclipse/upm.git
synced 2025-07-26 13:41:16 +03:00
C++ Core: Add base class per sensor/actuator type
Adding base classes for UPM sensors and actuators. Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
@ -2,4 +2,4 @@ set (libname "ms5611")
|
||||
set (libdescription "Barometric Pressure and Temperature Sensor")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_hpp ${libname}.hpp)
|
||||
upm_module_init(mraa interfaces)
|
||||
upm_module_init(mraa core)
|
||||
|
@ -53,18 +53,21 @@ using namespace upm;
|
||||
|
||||
MS5611::MS5611(int i2cBus, int address)
|
||||
{
|
||||
AddSource("temperature", "C");
|
||||
AddSource("pressure", "Pa");
|
||||
|
||||
i2c = new mraa::I2c(i2cBus);
|
||||
this->address = address;
|
||||
i2c->address(address);
|
||||
prom = new uint16_t[MS5611_PROM_SIZE];
|
||||
if (i2c->writeByte(MS5611_CMD_RESET != mraa::SUCCESS))
|
||||
UPM_THROW("Reset failed.");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : Reset failed.");
|
||||
delayms(5);
|
||||
for (int i = 0; i < MS5611_PROM_SIZE; ++i) {
|
||||
uint8_t buf[2];
|
||||
int bytesRead = i2c->readBytesReg(MS5611_CMD_READ_PROM + 2*i, buf, 2);
|
||||
if (bytesRead != 2)
|
||||
UPM_THROW("PROM address failed.");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : PROM address failed.");
|
||||
prom[i] = buf[0] << 8;
|
||||
prom[i] |= buf[1];
|
||||
// printf("Read PROM entry %d = %04x\n", i, prom[i]);
|
||||
@ -72,7 +75,7 @@ MS5611::MS5611(int i2cBus, int address)
|
||||
|
||||
// printf("CRC = %X\n", promCrc4());
|
||||
if (promCrc4() != (prom[7] & 0x000F))
|
||||
UPM_THROW("PROM checksum error.");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : PROM checksum error.");
|
||||
setOverSampling(ULTRA_HIGH_RES);
|
||||
}
|
||||
|
||||
@ -153,11 +156,11 @@ uint32_t MS5611::readADC(int adcReg)
|
||||
uint32_t value;
|
||||
uint8_t buf[3];
|
||||
if (i2c->writeByte(adcReg + osr) != mraa::SUCCESS)
|
||||
UPM_THROW("Convert D2 failed");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : Convert D2 failed");
|
||||
delayms(100);
|
||||
int bytesRead = i2c->readBytesReg(MS5611_CMD_ADC_READ, buf, 3);
|
||||
if (bytesRead != 3)
|
||||
UPM_THROW("ADC read failed");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : ADC read failed");
|
||||
// printf("%02X%02X%02X\n", buf[0], buf[1], buf[2]);
|
||||
value = ((uint32_t)buf[0] << 16) | ((uint32_t)buf[1] << 8) | buf[2];
|
||||
return value;
|
||||
@ -208,3 +211,23 @@ int MS5611::getPressurePa()
|
||||
return pressure;
|
||||
}
|
||||
|
||||
std::map<std::string, float> MS5611::TemperatureForSources(std::vector<std::string> sources)
|
||||
{
|
||||
std::map<std::string, float> ret;
|
||||
|
||||
if (std::find(sources.begin(), sources.end(), "temperature") != sources.end())
|
||||
ret["temperature"] = getTemperatureCelsius();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::map<std::string, float> MS5611::PressureForSources(std::vector<std::string> sources)
|
||||
{
|
||||
std::map<std::string, float> ret;
|
||||
|
||||
if (std::find(sources.begin(), sources.end(), "pressure") != sources.end())
|
||||
ret["pressure"] = getPressurePa();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "interfaces/iPressureSensor.hpp"
|
||||
#include "interfaces/iTemperatureSensor.hpp"
|
||||
#include "iPressureSensor.hpp"
|
||||
#include "iTemperatureSensor.hpp"
|
||||
#include "mraa/i2c.hpp"
|
||||
|
||||
namespace upm
|
||||
@ -58,7 +58,7 @@ namespace upm
|
||||
* @snippet ms5611.cxx Interesting
|
||||
*/
|
||||
|
||||
class MS5611 : public IPressureSensor, public ITemperatureSensor
|
||||
class MS5611 : public iPressureSensor, public iTemperatureSensor
|
||||
{
|
||||
public:
|
||||
enum OsrMode
|
||||
@ -68,11 +68,22 @@ public:
|
||||
|
||||
MS5611(int i2cBus = 0, int address = MS5611_ADDRESS);
|
||||
~MS5611();
|
||||
virtual const char* getModuleName() { return "ms5611"; }
|
||||
void setOverSampling(OsrMode osrMode);
|
||||
int getTemperatureCelsius();
|
||||
int getPressurePa();
|
||||
|
||||
/** Return the name of this device */
|
||||
virtual std::string Name () {return "ms5611";}
|
||||
|
||||
/** Return the description of this device */
|
||||
virtual std::string Description () {return "Barometric pressure and temperature sensor";}
|
||||
|
||||
/* Provide an implementation of a method to get sensor values by source */
|
||||
virtual std::map<std::string, float> TemperatureForSources(std::vector<std::string> sources);
|
||||
|
||||
/* Provide an implementation of a method to get sensor values by source */
|
||||
virtual std::map<std::string, float> PressureForSources(std::vector<std::string> sources);
|
||||
|
||||
private:
|
||||
/* Disable implicit copy and assignment operators */
|
||||
MS5611(const MS5611&) = delete;
|
||||
|
@ -1,11 +1,9 @@
|
||||
%include "../common_top.i"
|
||||
%include "iPressureSensor.i"
|
||||
%include "iTemperatureSensor.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%import "../interfaces/javaupm_iTemperatureSensor.i"
|
||||
%import "../interfaces/javaupm_iPressureSensor.i"
|
||||
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_ms5611)
|
||||
#endif
|
||||
/* END Java syntax */
|
||||
|
Reference in New Issue
Block a user