From 027f38d2be05f3aadd66fc90ebef71a8d3c4e321 Mon Sep 17 00:00:00 2001 From: Hank Mino Date: Wed, 2 Oct 2019 19:54:40 +0000 Subject: [PATCH 1/6] 2smpb02e: initial implementation of Omron Digital Barometric Pressure Sensor Signed-off-by: Hank Mino --- examples/c++/2smpb02e.cxx | 1193 +++++++++++++++++++++++++++++++++++ src/2smpb02e/2smpb02e.cxx | 598 ++++++++++++++++++ src/2smpb02e/2smpb02e.hpp | 428 +++++++++++++ src/2smpb02e/2smpb02e.i | 19 + src/2smpb02e/2smpb02e.json | 28 + src/2smpb02e/CMakeLists.txt | 5 + 6 files changed, 2271 insertions(+) create mode 100644 examples/c++/2smpb02e.cxx create mode 100644 src/2smpb02e/2smpb02e.cxx create mode 100644 src/2smpb02e/2smpb02e.hpp create mode 100644 src/2smpb02e/2smpb02e.i create mode 100644 src/2smpb02e/2smpb02e.json create mode 100644 src/2smpb02e/CMakeLists.txt diff --git a/examples/c++/2smpb02e.cxx b/examples/c++/2smpb02e.cxx new file mode 100644 index 000000000..46278938f --- /dev/null +++ b/examples/c++/2smpb02e.cxx @@ -0,0 +1,1193 @@ +/* +* Author: Hiroyuki Mino +* Copyright (c) 2019 Omron Electronic Components - Americas +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of +* this software and associated documentation files (the "Software"), to deal in +* the Software without restriction, including without limitation the rights to +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +* the Software, and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +/* headers */ +#include "2smpb02e.hpp" + +#include +#include +#include +#include +#include + + +/* Define */ +#define CLEAR_SCREEN "\033[2J\033[1;1H" /* Clear screen */ + +using namespace upm; +using namespace std; + +volatile sig_atomic_t flag = true; +UINT8 isCinWaitState = 0; //State for cin is waiting for input or not. + +void +sig_handler(int signum) +{ + // Check for Interrupt signal + if (signum == SIGINT || signum == SIGTERM){ + cout << "exit" << endl; + flag = false; + if(isCinWaitState ==1){ + std::cin.setstate(std::ios_base::badbit); + } + } +} + +UINT8 +takeInput(STRING str, INT32 &value, INT32 startIndex, INT32 endIndex){// Take input + + isCinWaitState = 1; + + try{ + cin.exceptions(std::ios_base::badbit); + + do{ + cout << str; + cin >> value; + + // Clear and ignor bad input + if(isdigit(value) != true){ + cin.clear(); // reset badbit + cin.ignore(std::numeric_limits::max(), '\n'); //skip bad input + } + + // Check start index and end index with input value + if(value < startIndex || value > endIndex){ + cout << "*************************************************************" << endl; + cout << "Invalid choice. Please select from " << std::dec << startIndex << " to " << endIndex << "." << endl; + cout << "*************************************************************" << endl; + }else{ + isCinWaitState = 0; + return true; + } + + }while((value < startIndex || value > endIndex) && flag); + + }catch (std::ios_base::failure& fail){ + + isCinWaitState = 0; + return false; + } + + isCinWaitState = 0; + return false; +} + +int +main(int argc, char** argv) +{ + INT32 bus = OM2SMPB02E_DEFAULT_I2C_BUS_NUM; + UINT32 display_time = 1000; + UINT8 addr = OM2SMPB02E_DEFAULT_I2C_ADDR; + TemperatureUnit iTempscale = TemperatureUnit::CELSIUS; + PressureUnit iPresscale = PressureUnit::PA; + INT32 choice,sub_choice,input,sub_input; + UINT8 chipId; + OM2SMPB02E_STATUS_T returnStatus; + UINT8 ret; + + OM2SMPB02E_VAL_POWERMODE_VALUES_T powerMode; + OM2SMPB02E_VAL_MEASMODE_VALUES_T measMode; + OM2SMPB02E_VAL_IIR_VALUES_T iirVal; + OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T standbyTimeValue; + + FLOAT temperatureData; + FLOAT pressureData; + + // Data and Time + INT8 dateAndTime[30]; + struct timeval value; + time_t curtime; + + signal(SIGINT, sig_handler); + + // Instantiate a 2SMPB02E sensor on I2C using bus and address. + OM2SMPB02E sensor(bus,addr); + + while(flag){ + + // Clear screen + cout << CLEAR_SCREEN; + + // Get Device Chip Id + returnStatus = sensor.getDeviceId(chipId); + + // Check return value + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get Device Id with error code : " << returnStatus << endl; + return 0; + } + + cout << "*************************************************************" << endl; + cout << " Barometric Pressure Sensor Chip ID (Device ID) : " << hex + << uppercase << (unsigned) chipId << endl; + + // Print main menu for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " MAIN MENU " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. Display Temperature " << endl; + cout << "2. Display Pressure " << endl; + cout << "3. Display Temperature and Pressure " << endl; + cout << "4. Settings " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from main menu : ",choice,1,4); + + if(ret != true){ + break; + } + + switch(choice){ + + case 1:// Display Temperature + + // Print Temperature scale for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Temperature Scale " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. CELSIUS [deg C] " << endl; + cout << "2. FAHRENHEIT [deg F] " << endl; + cout << "3. KELVIN [deg K] " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of temperature scale : ",sub_choice,1,3); + + if(ret != true){ + break; + } + + // Update Temperature scale variable + switch(sub_choice){ + + case 1:// Celsius + iTempscale = TemperatureUnit::CELSIUS; + break; + + case 2:// Fahrenheit + iTempscale = TemperatureUnit::FAHRENHEIT; + break; + + case 3:// Kelvin + iTempscale = TemperatureUnit::KELVIN; + break; + + default: + break; + + }// end switch select temperature scale + + // Set temperature scale + returnStatus = sensor.setTemperatureScale(iTempscale); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set Temperature scale with error code : " << returnStatus << endl; + break; + } + + // Print display time + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Display time " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Please enter display time [ms] : ",input,1,1000000); + + if(ret != true){ + break; + } + + display_time = input; + + // Set precision + cout << fixed << setprecision(2); + + cout << "*************************************************************" << endl; + cout << "Timestamp Temperature " << endl; + cout << "*************************************************************" << endl; + + while(flag){ + + // Get date and time + gettimeofday(&value, NULL); + curtime=value.tv_sec; + strftime(dateAndTime,30,"[%F %T.",localtime(&curtime)); + cout << dateAndTime << std::setfill('0') << std::setw(3) << std::dec << value.tv_usec/1000 << "] " << setfill(' ') << setw(1) << " "; + + // Get temperature value + returnStatus = sensor.getTemperature(temperatureData); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get temperatue value with error code : " << returnStatus << endl; + }else{ + // Print temperature data + cout << temperatureData << endl; + } + + //Delay + upm_delay_ms(display_time); + } + + break; + + case 2:// Display Pressure + + // Print Pressure scale for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Pressure Scale " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. Pascal [pa] " << endl; + cout << "2. Bar [bar] " << endl; + cout << "3. Standard atmosphere [atm] " << endl; + cout << "4. Torr [torr] " << endl; + cout << "5. Pounds per square inch [psi] " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of pressure scale : ",sub_choice,1,5); + + if(ret != true){ + break; + } + + // Update Pressure scale variable + switch(sub_choice){ + + case 1:// Pascal + iPresscale = PressureUnit::PA; + break; + + case 2:// Bar + iPresscale = PressureUnit::BAR; + break; + + case 3:// Standard Atmosphere + iPresscale = PressureUnit::ATM; + break; + + case 4:// Torr + iPresscale = PressureUnit::TORR; + break; + + case 5:// Pounds per square inch + iPresscale = PressureUnit::PSI; + break; + + default: + break; + + }// end switch select pressure scale + + // Set Preesure scale + returnStatus = sensor.setPressureScale(iPresscale); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set pressure scale with error code : " << returnStatus << endl; + break; + } + + // Print display time + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Display time " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Please enter display time [ms] : ",input,1,1000000); + + if(ret != true){ + break; + } + + display_time = input; + + cout << "*************************************************************" << endl; + cout << "Timestamp Pressure " << endl; + cout << "*************************************************************" << endl; + + while(flag){ + + // Get date and time + gettimeofday(&value, NULL); + curtime=value.tv_sec; + strftime(dateAndTime,30,"[%F %T.",localtime(&curtime)); + cout << dateAndTime << std::setfill('0') << std::setw(3) << std::dec << value.tv_usec/1000 << "] " << setfill(' ') << setw(1) << " "; + + // Get pressure data + returnStatus = sensor.getPressure(pressureData); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get pressure value with error code : " << returnStatus << endl; + }else{ + // Print pressure data + cout << fixed << (iPresscale==PressureUnit::PA?setprecision(2):setprecision(7)) << pressureData << endl; + } + + //Delay + upm_delay_ms(display_time); + } + + break; + + case 3:// Display Temperature and Pressure + + // Print Temperature scale for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Temperature Scale " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. CELSIUS [deg C] " << endl; + cout << "2. FAHRENHEIT [deg F] " << endl; + cout << "3. KELVIN [deg K] " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of temperature scale : ",sub_choice,1,3); + + if(ret != true){ + break; + } + + // Update Temperature scale variable + switch(sub_choice){ + + case 1:// Celsius + iTempscale = TemperatureUnit::CELSIUS; + break; + + case 2:// Fahrenheit + iTempscale = TemperatureUnit::FAHRENHEIT; + break; + + case 3:// Kelvin + iTempscale = TemperatureUnit::KELVIN; + break; + + default: + break; + + }// end switch select temperature scale + + // Set Temeperature scale + returnStatus = sensor.setTemperatureScale(iTempscale); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set Temperature scale with error code : " << returnStatus << endl; + break; + } + + // Print Pressure scale for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Pressure Scale " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. Pascal [pa] " << endl; + cout << "2. Bar [bar] " << endl; + cout << "3. Standard atmosphere [atm] " << endl; + cout << "4. Torr [torr] " << endl; + cout << "5. Pounds per square inch [psi] " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of pressure scale : ",input,1,5); + + if(ret != true){ + break; + } + + // Update Pressure scale variable + switch(input){ + + case 1:// Pascal + iPresscale = PressureUnit::PA; + break; + + case 2:// Bar + iPresscale = PressureUnit::BAR; + break; + + case 3:// Standard Atmosphere + iPresscale = PressureUnit::ATM; + break; + + case 4:// Torr + iPresscale = PressureUnit::TORR; + break; + + case 5:// Pounds per square inch + iPresscale = PressureUnit::PSI; + break; + + default: + break; + + }// end switch select pressure scale + + // Set pressure scale + returnStatus = sensor.setPressureScale(iPresscale); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set pressure scale with error code : " << returnStatus << endl; + break; + } + + // Print display time + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Display time " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Please enter display time [ms] : ",sub_input,1,1000000); + + if(ret != true){ + break; + } + + display_time = sub_input; + + cout << "*************************************************************" << endl; + cout << "Timestamp Temperature Pressure " << endl; + cout << "*************************************************************" << endl; + + while(flag){ + + // Get date and time + gettimeofday(&value, NULL); + curtime=value.tv_sec; + strftime(dateAndTime,30,"[%F %T.",localtime(&curtime)); + cout << dateAndTime << std::setfill('0') << std::setw(3) << std::dec << value.tv_usec/1000 << "]" << setfill(' ') << setw(3) << " "; + + // Get pressura and temperature data + returnStatus = sensor.getPresAndTempValues(pressureData,temperatureData); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get pressure and temperature value with error code : " << returnStatus << endl; + }else{ + // Print pressure and temperature data + cout << fixed << setprecision(2) << temperatureData << " " << setfill(' ') << setw(15) << " " << fixed << (iPresscale==PressureUnit::PA?setprecision(2):setprecision(7)) << pressureData << endl; + } + + //Delay + upm_delay_ms(display_time); + } + + break; + case 4:// Settings + + // Print main menu for selection + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " SETTINGS " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. Get Power Mode " << endl; + cout << "2. Set Power Mode " << endl; + cout << "3. Get IIR filter value " << endl; + cout << "4. Set IIR filter value " << endl; + cout << "5. Get Measurement Mode " << endl; + cout << "6. Set Measurement Mode " << endl; + cout << "7. Set I2C Frequency " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of settings : ",sub_choice,1,7); + + if(ret != true){ + break; + } + switch(sub_choice){ + + case 1:// Get Power Mode + + // Get Power mode + returnStatus = sensor.getPowerMode(powerMode); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get power mode with error code : " << returnStatus << endl; + break; + } + + switch(powerMode){ + + case OM2SMPB02E_VAL_POWERMODE_SLEEP: + cout << "Power Mode : SLEEP" << endl; + break; + + case OM2SMPB02E_VAL_POWERMODE_FORCED: + cout << "Power Mode : FORCED" << endl; + break; + + case OM2SMPB02E_VAL_POWERMODE_NORMAL: + + returnStatus = sensor.getStandbyTimeValue(standbyTimeValue); + + + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get standby time value with error code : " << returnStatus << endl; + break; + } + + cout << "Power Mode : NORMAL, and Standby Time Value : "; + + switch(standbyTimeValue){ + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_0001MS: + cout << "1 ms." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_0005MS: + cout << "5 ms." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_0050MS: + cout << "50 ms." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_0250MS: + cout << "250 ms." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_0500MS: + cout << "500 ms." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_1000MS: + cout << "1 s." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_2000MS: + cout << "2 s." << endl; + break; + + case OM2SMPB02E_VAL_IOSETUP_STANDBY_4000MS: + cout << "4 s." << endl; + break; + + default: + cout << "Invalid value received for standby time." << endl; + + }// end switch get standby time value + + break; + + default: + cout << "Invalid power mode value receive." << endl; + + }// end switch getPowerMode + + break; + + case 2:// Set Power Mode + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " POWER MODE " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. Sleep " << endl; + cout << "2. Forced " << endl; + cout << "3. Normal " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of power mode : ",input,1,3); + + if(ret != true){ + break; + } + + switch(input){ + + case 1:// Sleep + powerMode = OM2SMPB02E_VAL_POWERMODE_SLEEP; + break; + + case 2:// Forced + powerMode = OM2SMPB02E_VAL_POWERMODE_FORCED; + break; + + case 3:// Normal + powerMode = OM2SMPB02E_VAL_POWERMODE_NORMAL; + + // Set Standby time Value + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " STANDBY TIME VALUES " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. 1 ms " << endl; + cout << "2. 5 ms " << endl; + cout << "3. 50 ms " << endl; + cout << "4. 250 ms " << endl; + cout << "5. 500 ms " << endl; + cout << "6. 1 s " << endl; + cout << "7. 2 s " << endl; + cout << "8. 4 s " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of standby time value : ",sub_input,1,8); + + if(ret != true){ + break; + } + + switch(sub_input){ + + case 1:// 1 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_0001MS; + break; + + case 2:// 5 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_0005MS; + break; + + case 3:// 50 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_0050MS; + break; + + case 4:// 250 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_0250MS; + break; + + case 5:// 500 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_0500MS; + break; + + case 6:// 1000 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_1000MS; + break; + + case 7:// 2000 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_2000MS; + break; + + case 8:// 4000 MS + standbyTimeValue = OM2SMPB02E_VAL_IOSETUP_STANDBY_4000MS; + break; + + default: + break; + }//end switch set standby time value + + break; + + default: + break; + + }//end switch set power mode + + // Set power mode + returnStatus = sensor.setPowerMode(powerMode); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set power mode with error code : " << returnStatus << endl; + break; + } + + if(input == 3){// Check Power mode input is Normal + returnStatus = sensor.setStandbyTimeValue(standbyTimeValue); + + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set standby time value with error code : " << returnStatus << endl; + break; + } + + cout << "Power Mode set successfully with standby time value." << endl; + + }else{ + cout << "Power Mode successfully changed." << endl; + } + + break; + + case 3:// Get IIR filter value + + // Get IIR Filter value + returnStatus = sensor.getIIRFilterValue(iirVal); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get IIR filter value with error code : " << returnStatus << endl; + break; + } + + switch(iirVal){ + + case OM2SMPB02E_VAL_IIR_OFF: + cout << "IIR Value : 0 Times (OFF)" << endl; + break; + + case OM2SMPB02E_VAL_IIR_02TIMES: + cout << "IIR Value : 2 Times" << endl; + break; + + case OM2SMPB02E_VAL_IIR_04TIMES: + cout << "IIR Value : 4 Times" << endl; + break; + + case OM2SMPB02E_VAL_IIR_08TIMES: + cout << "IIR Value : 8 Times" << endl; + break; + + case OM2SMPB02E_VAL_IIR_16TIMES: + cout << "IIR Value : 16 Times" << endl; + break; + + case OM2SMPB02E_VAL_IIR_32TIMES: + cout << "IIR Value : 32 Times" << endl; + break; + + default: + cout << "Invalid iir value receive." << endl; + + }// end switch getIIRFilterValue + + break; + + case 4:// Set IIR filter value + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " IIR Filter Value " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. 0 Times (OFF) " << endl; + cout << "2. 2 Times " << endl; + cout << "3. 4 Times " << endl; + cout << "4. 8 Times " << endl; + cout << "5. 16 Times " << endl; + cout << "6. 32 Times " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of iir filter value : ",input,1,6); + + if(ret != true){ + break; + } + + switch(input){ + + case 1:// 0 Times (OFF) + iirVal = OM2SMPB02E_VAL_IIR_OFF; + break; + + case 2:// 2 Times + iirVal = OM2SMPB02E_VAL_IIR_02TIMES; + break; + + case 3:// 4 Times + iirVal = OM2SMPB02E_VAL_IIR_04TIMES; + break; + + case 4:// 8 Times + iirVal = OM2SMPB02E_VAL_IIR_08TIMES; + break; + + case 5:// 16 Times + iirVal = OM2SMPB02E_VAL_IIR_16TIMES; + break; + + case 6:// 32 Times + iirVal = OM2SMPB02E_VAL_IIR_32TIMES; + break; + + default: + break; + + }//end switch set iir filter value + + // Set IIR Filter value + returnStatus = sensor.setIIRFilterValue(iirVal); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set iir filter value with error code : " << returnStatus << endl; + break; + } + + cout << "IIR filter value set successfully." << endl; + + break; + + case 5:// Get Measurement Mode + + // Get measurement mode + returnStatus = sensor.getMeasurementMode(measMode); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to get measurement mode value with error code : " << returnStatus << endl; + break; + } + + cout << "*************************************************************" << endl; + + switch(measMode){ + + case OM2SMPB02E_VAL_MEASMODE_HIGHSPEED:// High Speed + cout << "Measurement Mode : High Speed "; + break; + + case OM2SMPB02E_VAL_MEASMODE_LOWPOWER:// Low Power + cout << "Measurement Mode : Low Power "; + break; + + case OM2SMPB02E_VAL_MEASMODE_STANDARD:// Standard + cout << "Measurement Mode : Standard "; + break; + + case OM2SMPB02E_VAL_MEASMODE_HIGHACCURACY:// High Accuracy + cout << "Measurement Mode : High Accuracy "; + break; + + case OM2SMPB02E_VAL_MEASMODE_ULTRAHIGH:// Ultra High + cout << "Measurement Mode : Ultra High "; + break; + + default: + cout << "Measurement Mode : Custom "; + + }// end switch getMeasurementMode + + cout << "[Pressure Measurement Avg time : "; + + switch(measMode & OM2SMPB02E_MASK_PRESAVERAGE){ + + case OM2SMPB02E_VAL_PRESAVERAGE_00: + cout << "Skip"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_01: + cout << "1"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_02: + cout << "2"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_04: + cout << "4"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_08: + cout << "8"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_16: + cout << "16"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_32: + cout << "32"; + break; + + case OM2SMPB02E_VAL_PRESAVERAGE_64: + cout << "64"; + break; + + default: + cout << "Invalud pressure average value"; + + }//end switch print pressure average value + + cout << ", Temperature Measurement Avg time : "; + + switch(measMode & OM2SMPB02E_MASK_TEMPAVERAGE){ + + case OM2SMPB02E_VAL_TEMPAVERAGE_00: + cout << "Skip]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_01: + cout << "1]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_02: + cout << "2]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_04: + cout << "4]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_08: + cout << "8]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_16: + cout << "16]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_32: + cout << "32]" << endl; + break; + + case OM2SMPB02E_VAL_TEMPAVERAGE_64: + cout << "64]" << endl; + break; + + default: + cout << "Invalid temperature average value" << endl; + + }//end switch print temperature average value + + break; + + case 6:// Set Measurement Mode + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Measurement Mode " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. High Speed " << endl; + cout << "2. Low Power " << endl; + cout << "3. Standard " << endl; + cout << "4. High Accuracy " << endl; + cout << "5. Ultra High " << endl; + cout << "6. Custom " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of measurement mode : ",input,1,6); + + if(ret != true){ + break; + } + + switch(input){ + + case 1:// High Speed + measMode = OM2SMPB02E_VAL_MEASMODE_HIGHSPEED; + break; + + case 2:// Low Power + measMode = OM2SMPB02E_VAL_MEASMODE_LOWPOWER; + break; + + case 3:// Standard + measMode = OM2SMPB02E_VAL_MEASMODE_STANDARD; + break; + + case 4:// High Accuracy + measMode = OM2SMPB02E_VAL_MEASMODE_HIGHACCURACY; + break; + + case 5:// Ultra High + measMode = OM2SMPB02E_VAL_MEASMODE_ULTRAHIGH; + break; + + case 6:// Custom + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Temperature Average Value " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. 1 " << endl; + cout << "2. 2 " << endl; + cout << "3. 4 " << endl; + cout << "4. 8 " << endl; + cout << "5. 16 " << endl; + cout << "6. 32 " << endl; + cout << "7. 64 " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of temperature average value : ",sub_input,1,7); + + if(ret != true){ + break; + } + + switch(sub_input){ + + case 1:// 1 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_01; + break; + + case 2:// 2 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_02; + break; + + case 3:// 4 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_04; + break; + + case 4:// 8 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_08; + break; + + case 5:// 16 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_16; + break; + + case 6:// 32 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_32; + break; + + case 7:// 64 + measMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T) OM2SMPB02E_VAL_TEMPAVERAGE_64; + break; + + default: + break; + + }// end sub input temperature average value + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " Pressure Average Value " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. 1 " << endl; + cout << "2. 2 " << endl; + cout << "3. 4 " << endl; + cout << "4. 8 " << endl; + cout << "5. 16 " << endl; + cout << "6. 32 " << endl; + cout << "7. 64 " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of pressure average value : ",sub_input,1,7); + + if(ret != true){ + break; + } + + switch(sub_input){ + + case 1:// 1 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_01); + break; + + case 2:// 2 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_02); + break; + + case 3:// 4 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_04); + break; + + case 4:// 8 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_08); + break; + + case 5:// 16 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_16); + break; + + case 6:// 32 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_32); + break; + + case 7:// 64 + measMode = OM2SMPB02E_VAL_MEASMODE_VALUES_T (measMode | OM2SMPB02E_VAL_PRESAVERAGE_64); + break; + + default: + break; + + }// end sub input pressure average value + + break; + + default: + break; + + }//end switch set Measurement Mode + + // Set measurement mode + returnStatus = sensor.setMeasurementMode(measMode); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set measurement mode value with error code : " << returnStatus << endl; + break; + } + + cout << "Measurement mode value set successfully." << endl; + + break; + + case 7:// Set I2C Frequency + + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << " I2C Frequency " << endl; + cout << "*************************************************************" << endl; + cout << "*************************************************************" << endl; + cout << "1. 100 KHz " << endl; + cout << "2. 400 KHz " << endl; + cout << "3. 3.4 MHz " << endl; + cout << "*************************************************************" << endl; + + ret = takeInput("Select from the list of i2c frequency : ",input,1,3); + + if(ret != true){ + break; + } + + // Set I2C Frequency + returnStatus = sensor.setI2cFrequency((mraa::I2cMode)(input-1)); + + // Check return status + if(returnStatus != OM2SMPB02E_STATUS_SUCCESS){ + cout << "Failed to set I2C frequency with error code : " << returnStatus << endl; + break; + } + + cout << "I2C frequency set successfully." << endl; + break; + + default: + break; + + }// end sub_choice + + break; + + default: + break; + + }//end switch + + if(flag){ + cout << "Press enter to continue..."; + //cin.ignore(); + cin.get(); + } + }//end while + return 0; +} diff --git a/src/2smpb02e/2smpb02e.cxx b/src/2smpb02e/2smpb02e.cxx new file mode 100644 index 000000000..4dc2ec604 --- /dev/null +++ b/src/2smpb02e/2smpb02e.cxx @@ -0,0 +1,598 @@ +/* +* Author: Hiroyuki Mino +* Copyright (c) 2019 Omron Electronic Components - Americas +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of +* this software and associated documentation files (the "Software"), to deal in +* the Software without restriction, including without limitation the rights to +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +* the Software, and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +* IN AN ACTION 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 "2smpb02e.hpp" + +using namespace upm; + +OM2SMPB02E::OM2SMPB02E(INT32 bus,UINT8 address): mBus(bus), mAddress(address){ + + OM2SMPB02E_VAL_MEASMODE_VALUES_T tempByte; + + // Create I2c object + mI2c = new mraa::I2c(mBus); + + // Check for I2c object created well or not + if(mI2c->address(mAddress) != mraa::SUCCESS){ + throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed"); + } + + // Set default Temperature scale to celsius + mTempScale = upm::TemperatureUnit::CELSIUS; + + // Set default Pressure scale to pascal + mPressureScale = upm::PressureUnit::PA; + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_COEFS)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed to get co-effi values" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(mBuf, OM2SMPB02E_COEFFI_MAX_VALUE)){ + + #ifdef DEBUG + std::cout << "read failed to get co-effi values" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + // Get co-effi values + getCOEFFValues(mCompFactor); + + try{// Get Measurement Mode + getMeasurementMode(tempByte); + }catch(const std::runtime_error& e){ + throw std::runtime_error(std::string(__FUNCTION__) + ": getMeasurementMode() failed"); + } + + try{// Get power mode + getPowerMode(mPowerMode); + }catch(const std::runtime_error& e){ + throw std::runtime_error(std::string(__FUNCTION__) + ": getPowerMode() failed"); + } + +} + +OM2SMPB02E::~OM2SMPB02E(){ + + // Delete I2c instance + delete mI2c; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getPresAndTempValues(FLOAT &outPres, FLOAT &outTemp){ + + UINT8 buf[6] = {0}; + UINT32 rawpres,rawtemp; + DOUBLE Pr, Po; + DOUBLE Tr, Dt; + + outPres = 0; + outTemp = 0; + + // Check power mode and return if sleep mode + if(mPowerMode == OM2SMPB02E_VAL_POWERMODE_SLEEP){ + std::cerr << "Error: The sensor is in sleep mode. Please change the power mode to either forced or normal." << std::endl; + return OM2SMPB02E_STATUS_SLEEP_MODE; + } + + // Check measurement mode average value + if((mPresMeasValue == OM2SMPB02E_VAL_PRESAVERAGE_00) && (mTempMeasValue == OM2SMPB02E_VAL_TEMPAVERAGE_00)){ + std::cerr << "Error: Get pressure and temperature value requires both measurement mode enabled." << std::endl; + return OM2SMPB02E_STATUS_TEMP_PRES_MEASUREMENT_SKIP; + } + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_PRES_TXD2)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed to get pres register" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(buf, sizeof(buf))){ + + #ifdef DEBUG + std::cout << "read failed to get pres value" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + rawpres = OM2SMPB02E_CONV8S_S24_BE(buf[0], buf[1], buf[2]); + rawtemp = OM2SMPB02E_CONV8S_S24_BE(buf[3], buf[4], buf[5]); + + // Check power mode + if(mPowerMode == OM2SMPB02E_VAL_POWERMODE_FORCED){ + try{// Set power mode + setPowerMode(mPowerMode); + }catch(const std::runtime_error& e){ + throw std::runtime_error(std::string(__FUNCTION__) + ": setPowerMode() failed"); + } + } + + #ifdef DEBUG + std::cout << "Raw pres data : " << rawpres << std::endl; + std::cout << "Raw temp data : " << rawtemp << std::endl; + #endif + + // temperature compensation + if(rawtemp){ + Dt = (INT32)rawtemp - 0x800000; + Tr = mCompFactor._A0 + (mCompFactor._A1 * Dt) + (mCompFactor._A2 * (Dt * Dt)); + + // Check Temperature average value + if(mTempMeasValue != OM2SMPB02E_VAL_TEMPAVERAGE_00){ + outTemp = Tr/256.0; + + // Check temperature unit scale and convert + if(mTempScale != upm::TemperatureUnit::CELSIUS){ + outTemp = convertCelsiusTo(outTemp,mTempScale); + } + } + } + + // barometer compensation + if(rawtemp && rawpres){ + Pr = (INT32)rawpres - 0x800000; + Po = mCompFactor._B00 + (mCompFactor._BT1 * Tr) + (mCompFactor._BP1 * Pr) + + (mCompFactor._B11 * Tr * Pr) + mCompFactor._BT2 * (Tr * Tr) + + (mCompFactor._BP2 * (Pr * Pr)) + (mCompFactor._B12 * Pr * (Tr * Tr)) + + (mCompFactor._B21 * (Pr * Pr) * Tr) + (mCompFactor._BP3 * (Pr * Pr * Pr)); + + // Check Pressure average value + if((mPresMeasValue != OM2SMPB02E_VAL_PRESAVERAGE_00) && (mTempMeasValue != OM2SMPB02E_VAL_TEMPAVERAGE_00)){ + outPres = Po; + + // Check pressure unit scale and convert + if(mPressureScale != upm::PressureUnit::PA){ + outPres = convertPaTo(outPres,mPressureScale); + } + } + } + + #ifdef DEBUG + std::cout << "outTemp data : " << outTemp << std::endl; + std::cout << "outPres data : " << outPres << std::endl; + #endif + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getPressure(FLOAT &outPres){ + + FLOAT temTemp; + + // Check power mode and return if sleep mode + if(mPowerMode == OM2SMPB02E_VAL_POWERMODE_SLEEP){ + std::cerr << "Error: The sensor is in sleep mode. Please change the power mode to either forced or normal." << std::endl; + return OM2SMPB02E_STATUS_SLEEP_MODE; + } + + // Check Pressure average value + if(mPresMeasValue == OM2SMPB02E_VAL_PRESAVERAGE_00 || mTempMeasValue == OM2SMPB02E_VAL_TEMPAVERAGE_00){ + std::cerr << "Error: Get pressure value requires temperature and pressure both measurement mode enabled." << std::endl; + return OM2SMPB02E_STATUS_TEMP_PRES_MEASUREMENT_SKIP; + } + + // Get Preesure value + return getPresAndTempValues(outPres,temTemp); +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getTemperature(FLOAT &outTemp){ + + FLOAT temPres; + + // Check power mode and return if sleep mode + if(mPowerMode == OM2SMPB02E_VAL_POWERMODE_SLEEP){ + std::cerr << "Error: The sensor is in sleep mode. Please change the power mode to either forced or normal." << std::endl; + return OM2SMPB02E_STATUS_SLEEP_MODE; + } + + // Check Temperature average value + if(mTempMeasValue == OM2SMPB02E_VAL_TEMPAVERAGE_00){ + std::cerr << "Error: Get temperature value requires temperature measurement mode enabled." << std::endl; + return OM2SMPB02E_STATUS_TEMP_MEASUREMENT_SKIP; + } + + // Get Temperature value + return getPresAndTempValues(temPres,outTemp); +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getPowerMode(OM2SMPB02E_VAL_POWERMODE_VALUES_T &powerMode){ + + UINT8 tempByte; + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_CTRL_MEAS)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed getPowerMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(&tempByte, 1)){ + + #ifdef DEBUG + std::cout << "read failed getPowerMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + #ifdef DEBUG + std::cout << "Power Mode Register value : " << (unsigned)tempByte << std::endl; + #endif + + // Parse power mode bits + powerMode = (OM2SMPB02E_VAL_POWERMODE_VALUES_T)(tempByte & OM2SMPB02E_MASK_POWERMODE_VALUE); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setPowerMode(OM2SMPB02E_VAL_POWERMODE_VALUES_T powerMode){ + + OM2SMPB02E_VAL_MEASMODE_VALUES_T tempByte; + UINT8 tempBuf[2]; + + try{// Get measurement mode + getMeasurementMode(tempByte); + }catch(const std::runtime_error& e){ + std::cerr << "Error while handling: " << e.what() << std::endl; + return OM2SMPB02E_STATUS_FAILURE; + } + + tempBuf[0] = OM2SMPB02E_REGI2C_CTRL_MEAS; + tempBuf[1] = (powerMode | tempByte); + + // Write register + if((mI2c->write(tempBuf,sizeof(tempBuf))) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed setPowerMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.write failed"); + } + + mPowerMode = powerMode; + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getMeasurementMode(OM2SMPB02E_VAL_MEASMODE_VALUES_T &measurementMode){ + + UINT8 tempByte; + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_CTRL_MEAS)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed getMeasurementMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(&tempByte, 1)){ + + #ifdef DEBUG + std::cout << "Read failed getMeasurementMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + // Parse measurement mode bits + measurementMode = (OM2SMPB02E_VAL_MEASMODE_VALUES_T)(tempByte & (OM2SMPB02E_MASK_TEMPAVERAGE|OM2SMPB02E_MASK_PRESAVERAGE)); + + // Mask the pressure and temperature value + mPresMeasValue = (measurementMode & OM2SMPB02E_MASK_PRESAVERAGE); + mTempMeasValue = (measurementMode & OM2SMPB02E_MASK_TEMPAVERAGE); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setMeasurementMode(OM2SMPB02E_VAL_MEASMODE_VALUES_T measurementMode){ + + UINT8 tempBuf[2]; + + // Check measurement mode average value + if(((measurementMode & OM2SMPB02E_MASK_PRESAVERAGE) == OM2SMPB02E_VAL_PRESAVERAGE_00) || + ((measurementMode & OM2SMPB02E_MASK_TEMPAVERAGE) == OM2SMPB02E_VAL_TEMPAVERAGE_00)){ + return OM2SMPB02E_STATUS_TEMP_PRES_MEASUREMENT_SKIP; + } + + try{// Get power mode + getPowerMode(mPowerMode); + }catch(const std::runtime_error& e){ + std::cerr << "Error while handling: " << e.what() << std::endl; + return OM2SMPB02E_STATUS_FAILURE; + } + + tempBuf[0] = OM2SMPB02E_REGI2C_CTRL_MEAS; + tempBuf[1] = (measurementMode | mPowerMode); + + // Write register + if((mI2c->write(tempBuf,sizeof(tempBuf))) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed setMeasurementMode" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.write failed"); + } + + // Mask the pressure and temperature value + mPresMeasValue = (measurementMode & OM2SMPB02E_MASK_PRESAVERAGE); + mTempMeasValue = (measurementMode & OM2SMPB02E_MASK_TEMPAVERAGE); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getStandbyTimeValue(OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T &standbyTimeValue){ + + UINT8 tempByte; + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_IO_SETUP)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "Write failed getStandbyTimeValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(&tempByte, 1)){ + + #ifdef DEBUG + std::cout << "Read failed getStandbyTimeValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + // Parse standby value bits + standbyTimeValue = (OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T)(tempByte & OM2SMPB02E_MASK_TEMPAVERAGE); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setStandbyTimeValue(OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T standbyTimeValue){ + + UINT8 tempBuf[2]; + + tempBuf[0] = OM2SMPB02E_REGI2C_IO_SETUP; + tempBuf[1] = standbyTimeValue; + + // Write register + if((mI2c->write(tempBuf,sizeof(tempBuf))) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed setStandbyTimeValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.write failed"); + } + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getIIRFilterValue(OM2SMPB02E_VAL_IIR_VALUES_T &iirValue){ + + UINT8 tempByte; + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_IIR)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "Write failed getIIRFilterValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(&tempByte, 1)){ + + #ifdef DEBUG + std::cout << "Read failed getIIRFilterValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + // Assign IIT Filter value + iirValue = (OM2SMPB02E_VAL_IIR_VALUES_T)(tempByte); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setIIRFilterValue(OM2SMPB02E_VAL_IIR_VALUES_T iirValue){ + + UINT8 tempBuf[2]; + + tempBuf[0] = OM2SMPB02E_REGI2C_IIR; + tempBuf[1] = iirValue; + + // Write register + if((mI2c->write(tempBuf,sizeof(tempBuf))) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed setIIRFilterValue" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.write failed"); + } + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getTemperatureScale(TemperatureUnit &unit){ + + // Get temperature scale + unit = mTempScale; + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setTemperatureScale(TemperatureUnit unit){ + + // Set temperature scale + mTempScale = unit; + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getPressureScale(PressureUnit &unit){ + + // Get pressure scale + unit = mPressureScale; + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setPressureScale(PressureUnit unit){ + + // Set pressure scale + mPressureScale = unit; + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getDeviceId(UINT8 &deviceId){ + + // Write register + if((mI2c->writeByte(OM2SMPB02E_REGI2C_CHIP_ID)) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "write failed getDeviceId" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.writeByte failed"); + } + + // Read values + if(!mI2c->read(&deviceId, 1)){ + + #ifdef DEBUG + std::cout << "read failed getDeviceId" << std::endl; + #endif + + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.read() failed"); + } + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::setI2cFrequency(mraa::I2cMode frequency){ + + // Set I2C frequency + if(mI2c->frequency(frequency) != mraa::SUCCESS){ + + #ifdef DEBUG + std::cout << "set I2C frequency failed" << std::endl; + #endif + + throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.frequency() failed"); + } + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::getCOEFFValues(OM2SMPB02E_SETTING_T &setting){ + + // pressure parameters + setting.ex = (mBuf[OM2SMPB02E_COEFFI_MAX_VALUE - 1] & 0xF0) >> 4; + + conv20Q4ToDouble(mBuf,setting._B00,setting.ex,0); + conv16ToDouble(OM2SMPB02E_COEFF_A_BT1,OM2SMPB02E_COEFF_S_BT1,mBuf,setting._BT1,2); + conv16ToDouble(OM2SMPB02E_COEFF_A_BT2,OM2SMPB02E_COEFF_S_BT2,mBuf,setting._BT2,4); + conv16ToDouble(OM2SMPB02E_COEFF_A_BP1,OM2SMPB02E_COEFF_S_BP1,mBuf,setting._BP1,6); + conv16ToDouble(OM2SMPB02E_COEFF_A_B11,OM2SMPB02E_COEFF_S_B11,mBuf,setting._B11,8); + conv16ToDouble(OM2SMPB02E_COEFF_A_BP2,OM2SMPB02E_COEFF_S_BP2,mBuf,setting._BP2,10); + conv16ToDouble(OM2SMPB02E_COEFF_A_B12,OM2SMPB02E_COEFF_S_B12,mBuf,setting._B12,12); + conv16ToDouble(OM2SMPB02E_COEFF_A_B21,OM2SMPB02E_COEFF_S_B21,mBuf,setting._B21,14); + conv16ToDouble(OM2SMPB02E_COEFF_A_BP3,OM2SMPB02E_COEFF_S_BP3,mBuf,setting._BP3,16); + + // temperature parameters + setting.ex = (mBuf[OM2SMPB02E_COEFFI_MAX_VALUE - 1] & 0x0F); + + conv20Q4ToDouble(mBuf,setting._A0,setting.ex,18); + conv16ToDouble(OM2SMPB02E_COEFF_A_A1,OM2SMPB02E_COEFF_S_A1,mBuf,setting._A1,20); + conv16ToDouble(OM2SMPB02E_COEFF_A_A2,OM2SMPB02E_COEFF_S_A2,mBuf,setting._A2,22); + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::conv16ToDouble(DOUBLE a, DOUBLE s, UINT8* inValue, DOUBLE &outValue, INT32 offSet){ + + UINT16 val; + INT16 ret; + + // Integer values shift + val = (UINT16)((UINT16)(inValue[offSet] << 8) | (UINT16)inValue[offSet + 1]); + + // Check value + if((val & 0x8000) != 0){ + ret = (INT16)((INT32)val - 0x10000); + }else{ + ret = val; + } + + // Convert value + outValue = a + (DOUBLE)ret * s /32767.0; + + return OM2SMPB02E_STATUS_SUCCESS; +} + +OM2SMPB02E_STATUS_T OM2SMPB02E::conv20Q4ToDouble(UINT8* inValue, DOUBLE &outValue, UINT8 ex, INT32 offSet){ + + INT32 ret; + UINT32 val; + + // Integet values shift + val = (UINT32)((inValue[offSet] << 12) | (inValue[offSet + 1] << 4) | ex); + + // Check value + if((val & 0x80000) != 0){ + ret = (INT32)val - 0x100000; + }else{ + ret = val; + } + + // Convert value + outValue = (DOUBLE)ret / 16.0; + + return OM2SMPB02E_STATUS_SUCCESS; +} + diff --git a/src/2smpb02e/2smpb02e.hpp b/src/2smpb02e/2smpb02e.hpp new file mode 100644 index 000000000..344b59f51 --- /dev/null +++ b/src/2smpb02e/2smpb02e.hpp @@ -0,0 +1,428 @@ +/* +* Author: Hiroyuki Mino +* Copyright (c) 2019 Omron Electronic Components - Americas +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of +* this software and associated documentation files (the "Software"), to deal in +* the Software without restriction, including without limitation the rights to +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +* the Software, and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + + +#pragma once + +#include //Required for c++ standard IO +#include "mraa/i2c.hpp" //Required for MRAA I2c operations +#include //Required to Temperature Unit functionality +#include //Required to Pressure Unit functionality + +/** + * MACROS and enum + */ +#define OM2SMPB02E_DEFAULT_I2C_BUS_NUM 6 +#define OM2SMPB02E_DEFAULT_I2C_ADDR 0x56 +#define OM2SMPB02E_COEFFI_MAX_VALUE (UINT8)25 +//#define DEBUG /* Debug print */ + +typedef unsigned char UINT8; /* 8 bit Unsigned Integer */ +typedef int INT32; /* 32 bit Signed Integer */ +typedef unsigned int UINT32; /* 32 bit Signed Integer */ +typedef char INT8; /* 8 bit Signed Integer */ +typedef double DOUBLE; /* 64 bit Floating-Point */ +typedef unsigned short int UINT16; /* 16 bit Unsigned Integer */ +typedef short int INT16; /* 16 bit Signed Integer */ +typedef float FLOAT; /* 32 bit Floating-Point */ +typedef std::string STRING; /* string */ + +/* Registers */ +#define OM2SMPB02E_REGI2C_PRES_TXD2 0xF7 +#define OM2SMPB02E_REGI2C_IO_SETUP 0xF5 +#define OM2SMPB02E_REGI2C_CTRL_MEAS 0xF4 +#define OM2SMPB02E_REGI2C_IIR 0xF1 +#define OM2SMPB02E_REGI2C_CHIP_ID 0xD1 +#define OM2SMPB02E_REGI2C_COEFS 0xA0 + +/** + * OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES enum. + * An Enum contains io setup stand by values. + */ +typedef enum OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES{ + OM2SMPB02E_VAL_IOSETUP_STANDBY_0001MS = 0x00, + OM2SMPB02E_VAL_IOSETUP_STANDBY_0005MS = 0x20, + OM2SMPB02E_VAL_IOSETUP_STANDBY_0050MS = 0x40, + OM2SMPB02E_VAL_IOSETUP_STANDBY_0250MS = 0x60, + OM2SMPB02E_VAL_IOSETUP_STANDBY_0500MS = 0x80, + OM2SMPB02E_VAL_IOSETUP_STANDBY_1000MS = 0xA0, + OM2SMPB02E_VAL_IOSETUP_STANDBY_2000MS = 0xC0, + OM2SMPB02E_VAL_IOSETUP_STANDBY_4000MS = 0xE0, +}OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T; + +#define OM2SMPB02E_MASK_TEMPAVERAGE ((UINT8)0xE0) +#define OM2SMPB02E_VAL_TEMPAVERAGE_00 ((UINT8)0x00) +#define OM2SMPB02E_VAL_TEMPAVERAGE_01 ((UINT8)0x20) +#define OM2SMPB02E_VAL_TEMPAVERAGE_02 ((UINT8)0x40) +#define OM2SMPB02E_VAL_TEMPAVERAGE_04 ((UINT8)0x60) +#define OM2SMPB02E_VAL_TEMPAVERAGE_08 ((UINT8)0x80) +#define OM2SMPB02E_VAL_TEMPAVERAGE_16 ((UINT8)0xA0) +#define OM2SMPB02E_VAL_TEMPAVERAGE_32 ((UINT8)0xC0) +#define OM2SMPB02E_VAL_TEMPAVERAGE_64 ((UINT8)0xE0) + +#define OM2SMPB02E_MASK_PRESAVERAGE ((UINT8)0x1C) +#define OM2SMPB02E_VAL_PRESAVERAGE_00 ((UINT8)0x00) +#define OM2SMPB02E_VAL_PRESAVERAGE_01 ((UINT8)0x04) +#define OM2SMPB02E_VAL_PRESAVERAGE_02 ((UINT8)0x08) +#define OM2SMPB02E_VAL_PRESAVERAGE_04 ((UINT8)0x0C) +#define OM2SMPB02E_VAL_PRESAVERAGE_08 ((UINT8)0x10) +#define OM2SMPB02E_VAL_PRESAVERAGE_16 ((UINT8)0x14) +#define OM2SMPB02E_VAL_PRESAVERAGE_32 ((UINT8)0x18) +#define OM2SMPB02E_VAL_PRESAVERAGE_64 ((UINT8)0x1C) + +/** + * OM2SMPB02E_VAL_MEASMODE_VALUES enum. + * An Enum contains measurement mode values. + */ +typedef enum OM2SMPB02E_VAL_MEASMODE_VALUES{ + OM2SMPB02E_VAL_MEASMODE_HIGHSPEED = (OM2SMPB02E_VAL_PRESAVERAGE_02 | OM2SMPB02E_VAL_TEMPAVERAGE_01), + OM2SMPB02E_VAL_MEASMODE_LOWPOWER = (OM2SMPB02E_VAL_PRESAVERAGE_04 | OM2SMPB02E_VAL_TEMPAVERAGE_01), + OM2SMPB02E_VAL_MEASMODE_STANDARD = (OM2SMPB02E_VAL_PRESAVERAGE_08 | OM2SMPB02E_VAL_TEMPAVERAGE_01), + OM2SMPB02E_VAL_MEASMODE_HIGHACCURACY = (OM2SMPB02E_VAL_PRESAVERAGE_16 | OM2SMPB02E_VAL_TEMPAVERAGE_02), + OM2SMPB02E_VAL_MEASMODE_ULTRAHIGH = (OM2SMPB02E_VAL_PRESAVERAGE_32 | OM2SMPB02E_VAL_TEMPAVERAGE_04), +}OM2SMPB02E_VAL_MEASMODE_VALUES_T; + + +#define OM2SMPB02E_MASK_POWERMODE_VALUE ((UINT8)0x03) + +/** + * OM2SMPB02E_VAL_POWERMODE_VALUES enum. + * An Enum contains power mode values. + */ +typedef enum OM2SMPB02E_VAL_POWERMODE_VALUES{ + OM2SMPB02E_VAL_POWERMODE_SLEEP = 0x00, + OM2SMPB02E_VAL_POWERMODE_FORCED = 0x01, + OM2SMPB02E_VAL_POWERMODE_NORMAL = 0x03, +}OM2SMPB02E_VAL_POWERMODE_VALUES_T; + +/** + * OM2SMPB02E_VAL_IIR_VALUES enum. + * An Enum contains IIR values. + */ +typedef enum OM2SMPB02E_VAL_IIR_VALUES{ + OM2SMPB02E_VAL_IIR_OFF = 0x00, + OM2SMPB02E_VAL_IIR_02TIMES = 0x01, + OM2SMPB02E_VAL_IIR_04TIMES = 0x02, + OM2SMPB02E_VAL_IIR_08TIMES = 0x03, + OM2SMPB02E_VAL_IIR_16TIMES = 0x04, + OM2SMPB02E_VAL_IIR_32TIMES = 0x05, +}OM2SMPB02E_VAL_IIR_VALUES_T; + +/* Coeff */ +#define OM2SMPB02E_COEFF_S_A1 ((DOUBLE)( 4.3E-04)) +#define OM2SMPB02E_COEFF_A_A1 ((DOUBLE)(-6.3E-03)) +#define OM2SMPB02E_COEFF_S_A2 ((DOUBLE)( 1.2E-10)) +#define OM2SMPB02E_COEFF_A_A2 ((DOUBLE)(-1.9E-11)) +#define OM2SMPB02E_COEFF_S_BT1 ((DOUBLE)( 9.1E-02)) +#define OM2SMPB02E_COEFF_A_BT1 ((DOUBLE)( 1.0E-01)) +#define OM2SMPB02E_COEFF_S_BT2 ((DOUBLE)( 1.2E-06)) +#define OM2SMPB02E_COEFF_A_BT2 ((DOUBLE)( 1.2E-08)) +#define OM2SMPB02E_COEFF_S_BP1 ((DOUBLE)( 1.9E-02)) +#define OM2SMPB02E_COEFF_A_BP1 ((DOUBLE)( 3.3E-02)) +#define OM2SMPB02E_COEFF_S_B11 ((DOUBLE)( 1.4E-07)) +#define OM2SMPB02E_COEFF_A_B11 ((DOUBLE)( 2.1E-07)) +#define OM2SMPB02E_COEFF_S_BP2 ((DOUBLE)( 3.5E-10)) +#define OM2SMPB02E_COEFF_A_BP2 ((DOUBLE)(-6.3E-10)) +#define OM2SMPB02E_COEFF_S_B12 ((DOUBLE)( 7.6E-13)) +#define OM2SMPB02E_COEFF_A_B12 ((DOUBLE)( 2.9E-13)) +#define OM2SMPB02E_COEFF_S_B21 ((DOUBLE)( 1.2E-14)) +#define OM2SMPB02E_COEFF_A_B21 ((DOUBLE)( 2.1E-15)) +#define OM2SMPB02E_COEFF_S_BP3 ((DOUBLE)( 7.9E-17)) +#define OM2SMPB02E_COEFF_A_BP3 ((DOUBLE)( 1.3E-16)) + +/* CALIBRATION DATA */ +typedef struct OM2SMPB02E_SETTING{ + /* Compensation Factor */ + UINT8 ex; + DOUBLE _A0, _A1, _A2; + DOUBLE _B00, _BT1, _BP1; + DOUBLE _B11, _BT2, _BP2; + DOUBLE _B12, _B21, _BP3; +}OM2SMPB02E_SETTING_T; + +#define OM2SMPB02E_CONV8S_S24_BE(a, b, c) \ + (INT32)((((UINT32)a << 16) & 0x00FF0000) | \ + (((UINT32)b << 8) & 0x0000FF00) | \ + ((UINT32)c & 0x000000FF)) + + +/** + * OM2SMPB02E_STATUS enum + * An Enum contains status code of operations + */ +typedef enum OM2SMPB02E_STATUS{ +OM2SMPB02E_STATUS_FAILURE = 0, +OM2SMPB02E_STATUS_SUCCESS = 1, +OM2SMPB02E_STATUS_I2C_NOT_INITED, +OM2SMPB02E_STATUS_I2C_WRITE_FAILED, +OM2SMPB02E_STATUS_I2C_READ_FAILED, +OM2SMPB02E_STATUS_INVALID_MEASURMENT_INTERVAL, +OM2SMPB02E_STATUS_INVALID_INPUT, +OM2SMPB02E_STATUS_PRES_MEASUREMENT_SKIP, +OM2SMPB02E_STATUS_TEMP_MEASUREMENT_SKIP, +OM2SMPB02E_STATUS_TEMP_PRES_MEASUREMENT_SKIP, +OM2SMPB02E_STATUS_SLEEP_MODE, +}OM2SMPB02E_STATUS_T; + +/*=========================================================================*/ + +namespace upm { + /** + * @brief Barometric Perssure Sensors + * @defgroup 2smpb02e libupm-2smpb02e + * @ingroup --add group + */ + /** + * @library 2smpb02e + * @sensor 2smpb-02e + * @comname Omron Barometric Pressure sensors + * @type --add type + * @man omron + * @con I2C + * @web --add weblink + * + * @brief API for the Omron Mems thermal sensors interface + * + * It is connected via a I2C Interface. + * + * @snippet 2smpb02e.cxx Interesting + */ + + class OM2SMPB02E : virtual private iTemperature, virtual private iPressure{ + public : + /** + * OM2SMPB02E constructor + * @param bus I2c bus the sensor is attached to. Default is 6. + * @param address I2c address Optional device address. Default is 0x56. + */ + OM2SMPB02E(INT32 bus = OM2SMPB02E_DEFAULT_I2C_BUS_NUM,UINT8 address = OM2SMPB02E_DEFAULT_I2C_ADDR); + + /** + * OM2SMPB02E destructor + */ + ~OM2SMPB02E(); + + /** + * Get Barometric Pressure sensor get pressure and temperature data. + * + * @param outPres Pressure value. Value is based on current pressure scale unit setting. + * @param outTemp Temperature data. Value is based on current temperature scale unit setting. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getPresAndTempValues(FLOAT &outPres, FLOAT &outTemp); + + /** + * Get Barometric Pressure sensor get pressure data. + * + * @param outPres Pressure value. Value is based on current pressure scale unit setting. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getPressure(FLOAT &outPres); + + /** + * Get Barometric Pressure sensor get temperature data. + * + * @param outTemp Temperature data. Value is based on current temperature scale unit setting. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getTemperature(FLOAT &outTemp); + + /** + * Get power mode. + * + * @param powerMode power mode. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getPowerMode(OM2SMPB02E_VAL_POWERMODE_VALUES_T &powerMode); + + /** + * Set power mode. + * + * @param powerMode power mode. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T setPowerMode(OM2SMPB02E_VAL_POWERMODE_VALUES_T powerMode); + + /** + * Get measurement mode. + * + * @param measurementMode measurement mode. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getMeasurementMode(OM2SMPB02E_VAL_MEASMODE_VALUES_T &measurementMode); + + /** + * Set measurement mode. + * + * @param measurementMode measurement mode. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T setMeasurementMode(OM2SMPB02E_VAL_MEASMODE_VALUES_T measurementMode); + + /** + * Get Standby time value + * + * @param standbyTimeValue standby time. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getStandbyTimeValue(OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T &standbyTimeValue); + + /** + * Set Standby time value + * + * @param standbyTimeValue standby time. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T setStandbyTimeValue(OM2SMPB02E_VAL_IOSETUP_STANDBY_VALUES_T standbyTimeValue); + + /** + * Get IIR filter value + * + * @param iirValue iif filter value. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T getIIRFilterValue(OM2SMPB02E_VAL_IIR_VALUES_T &iirValue); + + /** + * Set IIR filter value + * + * @param iirValue iif filter value. + * @return One of the OM2SMPB02E_STATUS_T value. + */ + OM2SMPB02E_STATUS_T setIIRFilterValue(OM2SMPB02E_VAL_IIR_VALUES_T iirValue); + + /** + * Get the temperature unit. + * + * @param unit Temperature scale unit + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T getTemperatureScale(TemperatureUnit &unit); + + /** + * Set the temperature unit. + * + * @param unit Temperature scale unit + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T setTemperatureScale(TemperatureUnit unit); + + /** + * Get the Pressure unit. + * + * @param unit Pressure scale unit + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T getPressureScale(PressureUnit &unit); + + /** + * Set the Pressure unit. + * + * @param unit Pressure scale unit + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T setPressureScale(PressureUnit unit); + + /** + * Set the I2C Frequency. + * + * @param I2cMode frequency + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T setI2cFrequency(mraa::I2cMode frequency); + + /** + * Get the device Id. + * + * @param deviceId Device ID + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T getDeviceId(UINT8 &deviceId); + + private: + INT32 mBus; + INT32 mAddress; + TemperatureUnit mTempScale; + PressureUnit mPressureScale; + mraa::I2c* mI2c; + UINT8 mBuf[OM2SMPB02E_COEFFI_MAX_VALUE] = {0}; + OM2SMPB02E_SETTING_T mCompFactor; + UINT8 mTempMeasValue = OM2SMPB02E_VAL_TEMPAVERAGE_00; + UINT8 mPresMeasValue = OM2SMPB02E_VAL_PRESAVERAGE_00; + OM2SMPB02E_VAL_POWERMODE_VALUES_T mPowerMode = OM2SMPB02E_VAL_POWERMODE_SLEEP; + + /** + * Returns the temperature. This method is not publicly accessible. + * + * @return Dummy value + */ + virtual FLOAT getTemperature(){return 0.0;} + + /** + * Returns the Pressure. This method is not publicly accessible. + * + * @return Dummy value + */ + virtual FLOAT getPressure(){return 0.0;} + + /** + * Get the co-effi. values for Temperature and Pressure. + * + * @param settings Settings. + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T getCOEFFValues(OM2SMPB02E_SETTING_T &Setting); + + /** + * Convert byte buffer to double. Byte buffer format is a signed-16bit Big-Endian. + * + * @param a + * @param s + * @param inBuf + * @param outBuf + * @param offSet + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T conv16ToDouble(DOUBLE a, DOUBLE s, UINT8* inValue, DOUBLE &outValue, INT32 offSet); + + /** + * Convert byte buffer to double. Byte buffer format is a signed-20Q4,from -32768.0 to 32767.9375. + * + * @param inValue + * @param outValue + * @param ex + * @param offSet + * @return One of the OM2SMPB02E_STATUS_T values. + */ + OM2SMPB02E_STATUS_T conv20Q4ToDouble(UINT8* inValue, DOUBLE &outValue, UINT8 ex, INT32 offSet); + + /* + * Copy Constructor + */ + OM2SMPB02E(const OM2SMPB02E&); + + /** + * Operator Overloading + */ + OM2SMPB02E& operator=(const OM2SMPB02E&); + }; +} diff --git a/src/2smpb02e/2smpb02e.i b/src/2smpb02e/2smpb02e.i new file mode 100644 index 000000000..bf4ee25fc --- /dev/null +++ b/src/2smpb02e/2smpb02e.i @@ -0,0 +1,19 @@ +#ifdef SWIGPYTHON +%module (package="upm") 2smpb02e +#endif + +%include "../common_top.i" + +/* BEGIN Java syntax ------------------------------------------------------- */ +#ifdef SWIGJAVA +JAVA_JNI_LOADLIBRARY(javaupm_d6t) +#endif +/* END Java syntax */ + +/* BEGIN Common SWIG syntax ------------------------------------------------- */ +%{ +#include "2smpb02e.hpp" +%} + +%include "2smpb02e.hpp" +/* END Common SWIG syntax */ diff --git a/src/2smpb02e/2smpb02e.json b/src/2smpb02e/2smpb02e.json new file mode 100644 index 000000000..e78b87a56 --- /dev/null +++ b/src/2smpb02e/2smpb02e.json @@ -0,0 +1,28 @@ +{ + "Library": "2smpb02e", + "Description": "Omron Digital Barometric Pressure Sensor", + "Sensor Class": { + "d6t": { + "Name": "API for Barometric Pressure Sensor", + "Description": "This is the UPM Module for the Omron Barometric Pressure Sensor using I2C interface.", + "Aliases": ["Absolute Pressure Sensor"], + "Connections": ["I2C"], + "Project Type": ["sensor"], + "Manufacturers": ["Omron"], + "Examples": { + "C++": ["2smpb02e.cxx"] + }, + "Platforms": { + "Intel Edison": { + "Notes": ["Might need omron barometric pressure sensor"] + } + }, + "Urls": { + "Product Pages": ["https://www.components.omron.com/product-detail?partId=35065"], + "Datasheets": ["https://omronfs.omron.com/en_US/ecb/products/pdf/en_2smpb_02e.pdf"] + } + } + } +} + + diff --git a/src/2smpb02e/CMakeLists.txt b/src/2smpb02e/CMakeLists.txt new file mode 100644 index 000000000..242514c4f --- /dev/null +++ b/src/2smpb02e/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "2smpb02e") +set (libdescription "Omron Digital Barometric Pressure Sensor") +set (module_src "${libname}.cxx" "2smpb02e.cxx") +set (module_hpp "${libname}.hpp" "2smpb02e.hpp") +upm_module_init(mraa) From ebd4d07ff3b3f610e710246b427134a44e6a756e Mon Sep 17 00:00:00 2001 From: omron-ankit <47993414+omron-ankit@users.noreply.github.com> Date: Wed, 2 Oct 2019 15:30:53 -0500 Subject: [PATCH 2/6] Update 2smpb02e.cxx --- src/2smpb02e/2smpb02e.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/2smpb02e/2smpb02e.cxx b/src/2smpb02e/2smpb02e.cxx index 4dc2ec604..eba87cbea 100644 --- a/src/2smpb02e/2smpb02e.cxx +++ b/src/2smpb02e/2smpb02e.cxx @@ -462,28 +462,28 @@ OM2SMPB02E_STATUS_T OM2SMPB02E::setIIRFilterValue(OM2SMPB02E_VAL_IIR_VALUES_T ii return OM2SMPB02E_STATUS_SUCCESS; } -OM2SMPB02E_STATUS_T OM2SMPB02E::getTemperatureScale(TemperatureUnit &unit){ +OM2SMPB02E_STATUS_T OM2SMPB02E::getTemperatureScale(upm::TemperatureUnit &unit){ // Get temperature scale unit = mTempScale; return OM2SMPB02E_STATUS_SUCCESS; } -OM2SMPB02E_STATUS_T OM2SMPB02E::setTemperatureScale(TemperatureUnit unit){ +OM2SMPB02E_STATUS_T OM2SMPB02E::setTemperatureScale(upm::TemperatureUnit unit){ // Set temperature scale mTempScale = unit; return OM2SMPB02E_STATUS_SUCCESS; } -OM2SMPB02E_STATUS_T OM2SMPB02E::getPressureScale(PressureUnit &unit){ +OM2SMPB02E_STATUS_T OM2SMPB02E::getPressureScale(upm::PressureUnit &unit){ // Get pressure scale unit = mPressureScale; return OM2SMPB02E_STATUS_SUCCESS; } -OM2SMPB02E_STATUS_T OM2SMPB02E::setPressureScale(PressureUnit unit){ +OM2SMPB02E_STATUS_T OM2SMPB02E::setPressureScale(upm::PressureUnit unit){ // Set pressure scale mPressureScale = unit; From 48b40b7ad97d7f21b8938c87d77b88f4d0451d74 Mon Sep 17 00:00:00 2001 From: omron-ankit <47993414+omron-ankit@users.noreply.github.com> Date: Wed, 2 Oct 2019 15:54:23 -0500 Subject: [PATCH 3/6] Update 2smpb02e.hpp --- src/2smpb02e/2smpb02e.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/2smpb02e/2smpb02e.hpp b/src/2smpb02e/2smpb02e.hpp index 344b59f51..489c60db5 100644 --- a/src/2smpb02e/2smpb02e.hpp +++ b/src/2smpb02e/2smpb02e.hpp @@ -187,20 +187,20 @@ OM2SMPB02E_STATUS_SLEEP_MODE, namespace upm { /** - * @brief Barometric Perssure Sensors + * @brief Digital Barometric Perssure Sensor * @defgroup 2smpb02e libupm-2smpb02e - * @ingroup --add group + * @ingroup omron i2c barometric pressure sensor */ /** * @library 2smpb02e * @sensor 2smpb-02e * @comname Omron Barometric Pressure sensors - * @type --add type + * @type barometric pressure * @man omron * @con I2C - * @web --add weblink + * @web https://www.components.omron.com/product-detail?partId=35065 * - * @brief API for the Omron Mems thermal sensors interface + * @brief API for the Omron Mems barometric pressure sensor interface * * It is connected via a I2C Interface. * @@ -316,7 +316,7 @@ namespace upm { * @param unit Temperature scale unit * @return One of the OM2SMPB02E_STATUS_T values. */ - OM2SMPB02E_STATUS_T getTemperatureScale(TemperatureUnit &unit); + OM2SMPB02E_STATUS_T getTemperatureScale(upm::TemperatureUnit &unit); /** * Set the temperature unit. @@ -324,7 +324,7 @@ namespace upm { * @param unit Temperature scale unit * @return One of the OM2SMPB02E_STATUS_T values. */ - OM2SMPB02E_STATUS_T setTemperatureScale(TemperatureUnit unit); + OM2SMPB02E_STATUS_T setTemperatureScale(upm::TemperatureUnit unit); /** * Get the Pressure unit. @@ -332,7 +332,7 @@ namespace upm { * @param unit Pressure scale unit * @return One of the OM2SMPB02E_STATUS_T values. */ - OM2SMPB02E_STATUS_T getPressureScale(PressureUnit &unit); + OM2SMPB02E_STATUS_T getPressureScale(upm::PressureUnit &unit); /** * Set the Pressure unit. @@ -340,7 +340,7 @@ namespace upm { * @param unit Pressure scale unit * @return One of the OM2SMPB02E_STATUS_T values. */ - OM2SMPB02E_STATUS_T setPressureScale(PressureUnit unit); + OM2SMPB02E_STATUS_T setPressureScale(upm::PressureUnit unit); /** * Set the I2C Frequency. @@ -361,8 +361,8 @@ namespace upm { private: INT32 mBus; INT32 mAddress; - TemperatureUnit mTempScale; - PressureUnit mPressureScale; + upm::TemperatureUnit mTempScale; + upm::PressureUnit mPressureScale; mraa::I2c* mI2c; UINT8 mBuf[OM2SMPB02E_COEFFI_MAX_VALUE] = {0}; OM2SMPB02E_SETTING_T mCompFactor; From e23a80a190077aa25540aef7c40aac52026a01d1 Mon Sep 17 00:00:00 2001 From: omron-ankit <47993414+omron-ankit@users.noreply.github.com> Date: Wed, 2 Oct 2019 16:31:54 -0500 Subject: [PATCH 4/6] Update 2smpb02e.i --- src/2smpb02e/2smpb02e.i | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/2smpb02e/2smpb02e.i b/src/2smpb02e/2smpb02e.i index bf4ee25fc..93a5488e3 100644 --- a/src/2smpb02e/2smpb02e.i +++ b/src/2smpb02e/2smpb02e.i @@ -2,11 +2,18 @@ %module (package="upm") 2smpb02e #endif +%import "interfaces/interfaces.i" + %include "../common_top.i" /* BEGIN Java syntax ------------------------------------------------------- */ #ifdef SWIGJAVA -JAVA_JNI_LOADLIBRARY(javaupm_d6t) +%typemap(javaimports) SWIGTYPE %{ +import upm_interfaces.*; +%} + +JAVA_JNI_LOADLIBRARY(javaupm_2smpb02e) + #endif /* END Java syntax */ From 26752cafba9bf483e46d3328733a40348647571a Mon Sep 17 00:00:00 2001 From: omron-ankit <47993414+omron-ankit@users.noreply.github.com> Date: Wed, 2 Oct 2019 17:24:18 -0500 Subject: [PATCH 5/6] Update 2smpb02e.i --- src/2smpb02e/2smpb02e.i | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/2smpb02e/2smpb02e.i b/src/2smpb02e/2smpb02e.i index 93a5488e3..0370243b6 100644 --- a/src/2smpb02e/2smpb02e.i +++ b/src/2smpb02e/2smpb02e.i @@ -2,18 +2,11 @@ %module (package="upm") 2smpb02e #endif -%import "interfaces/interfaces.i" - %include "../common_top.i" /* BEGIN Java syntax ------------------------------------------------------- */ #ifdef SWIGJAVA -%typemap(javaimports) SWIGTYPE %{ -import upm_interfaces.*; -%} - JAVA_JNI_LOADLIBRARY(javaupm_2smpb02e) - #endif /* END Java syntax */ From 600088b24d22a444f8c45c5054f290360d1667d6 Mon Sep 17 00:00:00 2001 From: omron-ankit <47993414+omron-ankit@users.noreply.github.com> Date: Wed, 2 Oct 2019 17:28:10 -0500 Subject: [PATCH 6/6] Update 2smpb02e.i --- src/2smpb02e/2smpb02e.i | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/2smpb02e/2smpb02e.i b/src/2smpb02e/2smpb02e.i index 0370243b6..50fa33412 100644 --- a/src/2smpb02e/2smpb02e.i +++ b/src/2smpb02e/2smpb02e.i @@ -1,5 +1,5 @@ #ifdef SWIGPYTHON -%module (package="upm") 2smpb02e +%module (package="upm") om2smpb02e #endif %include "../common_top.i"