examples: Remove heap allocation from C++ examples

Cleanup of UPM C++ examples.  Switched from heap allocation to
stack allocation when possible.  This simplifies the samples since it
removes the need for explicit memory management.  A script was used to
identify and replace pointer use.  To simplify the replace script, I
re-formatted the C++ examples using the UPM .clang-format file.
Unfortuantely this changes the look of the UPM C++ examples to a large
degree.  However, examples will now have a standard look/feel and
uniform formatting.

    * Ran clang-format w/provided UPM .clang-format file
    * Removed new's/delete's whenever possible (left those in interface
      examples)
    * Added IIO sensor library implementation of callback void* arg
    * Converted all sleeps to upm defined delays (added header when
      necessary)
    * Scrubbed CXX example includes

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2017-08-30 15:00:29 -07:00 committed by Mihai Tudor Panu
parent bd6e4ec786
commit 5cefe7f5f3
290 changed files with 7976 additions and 8520 deletions

View File

@ -99,7 +99,7 @@ function (add_example example_src)
# Add each dependency to the library target # Add each dependency to the library target
foreach(_dep_target ${lib_target_names}) foreach(_dep_target ${lib_target_names})
target_link_libraries(${this_target_name} ${_dep_target} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(${this_target_name} ${_dep_target} ${CMAKE_THREAD_LIBS_INIT} utilities-c)
endforeach () endforeach ()
endfunction (add_example example_src) endfunction (add_example example_src)

View File

@ -22,55 +22,58 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <stddef.h>
#include "a110x.hpp" #include "a110x.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
// Our pulse counter // Our pulse counter
volatile unsigned int counter = 0; volatile unsigned int counter = 0;
// Our interrupt handler // Our interrupt handler
void hallISR(void *arg) void
hallISR(void* arg)
{ {
counter++; counter++;
} }
int main () int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an A110X sensor on digital pin D2 // Instantiate an A110X sensor on digital pin D2
upm::A110X* hall = new upm::A110X(2); upm::A110X hall(2);
// This example uses a user-supplied interrupt handler to count
// pulses that occur when a magnetic field of the correct polarity
// is detected. This could be used to measure the rotations per
// minute (RPM) of a rotor for example.
hall->installISR(hallISR, NULL); // This example uses a user-supplied interrupt handler to count
// pulses that occur when a magnetic field of the correct polarity
// is detected. This could be used to measure the rotations per
// minute (RPM) of a rotor for example.
while (shouldRun) hall.installISR(hallISR, NULL);
{
cout << "Pulses detected: " << counter << endl;
sleep(1); while (shouldRun) {
cout << "Pulses detected: " << counter << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete hall; return 0;
return 0;
} }

View File

@ -22,46 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "a110x.hpp" #include "a110x.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an A110X sensor on digital pin D2 // Instantiate an A110X sensor on digital pin D2
upm::A110X* hall = new upm::A110X(2); upm::A110X hall(2);
// check every second for the presence of a magnetic field (south
// polarity)
while (shouldRun)
{
bool val = hall->magnetDetected();
if (val)
cout << "Magnet (south polarity) detected." << endl;
else
cout << "No magnet detected." << endl;
sleep(1); // check every second for the presence of a magnetic field (south
// polarity)
while (shouldRun) {
bool val = hall.magnetDetected();
if (val)
cout << "Magnet (south polarity) detected." << endl;
else
cout << "No magnet detected." << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete hall; return 0;
return 0;
} }

View File

@ -21,41 +21,42 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "abp.hpp" #include "abp.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an ABP sensor on i2c bus 0 // Instantiate an ABP sensor on i2c bus 0
upm::ABP* abp = new upm::ABP(0, ABP_DEFAULT_ADDRESS); upm::ABP abp(0, ABP_DEFAULT_ADDRESS);
while (shouldRun) { while (shouldRun) {
abp->update(); abp.update();
cout << "Retrieved pressure: " << abp->getPressure() << endl; cout << "Retrieved pressure: " << abp.getPressure() << endl;
cout << "Retrieved Temperature: " << abp->getTemperature() << endl; cout << "Retrieved Temperature: " << abp.getTemperature() << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete abp;
return 0; return 0;
} }

View File

@ -22,44 +22,45 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "ad8232.hpp" #include "ad8232.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Ad8232 sensor on digital pins 10 (LO+), 11 (LO-) // Instantiate a Ad8232 sensor on digital pins 10 (LO+), 11 (LO-)
// and an analog pin, 0 (OUTPUT) // and an analog pin, 0 (OUTPUT)
upm::AD8232 *ad8232 = new upm::AD8232(10, 11, 0); upm::AD8232 ad8232(10, 11, 0);
// Output the raw numbers from the ADC, for plotting elsewhere. // Output the raw numbers from the ADC, for plotting elsewhere.
// A return of 0 indicates a Lead Off (LO) condition. // A return of 0 indicates a Lead Off (LO) condition.
// In theory, this data could be fed to software like Processing // In theory, this data could be fed to software like Processing
// (https://www.processing.org/) to plot the data just like an // (https://www.processing.org/) to plot the data just like an
// EKG you would see in a hospital. // EKG you would see in a hospital.
while (shouldRun) while (shouldRun) {
{ cout << ad8232.value() << endl;
cout << ad8232->value() << endl; upm_delay_us(1000);
usleep(1000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete ad8232; return 0;
return 0;
} }

View File

@ -22,61 +22,56 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include "adafruitms1438.hpp" #include "adafruitms1438.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate an Adafruit MS 1438 on I2C bus 0 // Instantiate an Adafruit MS 1438 on I2C bus 0
upm::AdafruitMS1438 *ms = upm::AdafruitMS1438 ms(ADAFRUITMS1438_I2C_BUS, ADAFRUITMS1438_DEFAULT_I2C_ADDR);
new upm::AdafruitMS1438(ADAFRUITMS1438_I2C_BUS,
ADAFRUITMS1438_DEFAULT_I2C_ADDR);
// Setup for use with a stepper motor connected to the M1 & M2 ports // Setup for use with a stepper motor connected to the M1 & M2 ports
// set a PWM period of 50Hz // set a PWM period of 50Hz
// disable first, to be safe // disable first, to be safe
ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12); ms.disableStepper(AdafruitMS1438::STEPMOTOR_M12);
// configure for a NEMA-17, 200 steps per revolution // configure for a NEMA-17, 200 steps per revolution
ms->stepConfig(AdafruitMS1438::STEPMOTOR_M12, 200); ms.stepConfig(AdafruitMS1438::STEPMOTOR_M12, 200);
// set speed at 10 RPM's // set speed at 10 RPM's
ms->setStepperSpeed(AdafruitMS1438::STEPMOTOR_M12, 10); ms.setStepperSpeed(AdafruitMS1438::STEPMOTOR_M12, 10);
ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, ms.setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, AdafruitMS1438::DIR_CW);
AdafruitMS1438::DIR_CW);
// enable // enable
cout << "Enabling..." << endl; cout << "Enabling..." << endl;
ms->enableStepper(AdafruitMS1438::STEPMOTOR_M12); ms.enableStepper(AdafruitMS1438::STEPMOTOR_M12);
cout << "Rotating 1 full revolution at 10 RPM speed." << endl; cout << "Rotating 1 full revolution at 10 RPM speed." << endl;
ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 200); ms.stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 200);
cout << "Sleeping for 2 seconds..." << endl; cout << "Sleeping for 2 seconds..." << endl;
sleep(2); upm_delay(2);
cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." << endl;
<< endl;
ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, ms.setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, AdafruitMS1438::DIR_CCW);
AdafruitMS1438::DIR_CCW); ms.stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 100);
ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 100);
cout << "Disabling..." << endl; cout << "Disabling..." << endl;
ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12); ms.disableStepper(AdafruitMS1438::STEPMOTOR_M12);
cout << "Exiting" << endl; cout << "Exiting" << endl;
//! [Interesting] //! [Interesting]
delete ms; return 0;
return 0;
} }

View File

@ -22,54 +22,51 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include "adafruitms1438.hpp" #include "adafruitms1438.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate an Adafruit MS 1438 on I2C bus 0 // Instantiate an Adafruit MS 1438 on I2C bus 0
upm::AdafruitMS1438 *ms = upm::AdafruitMS1438 ms(ADAFRUITMS1438_I2C_BUS, ADAFRUITMS1438_DEFAULT_I2C_ADDR);
new upm::AdafruitMS1438(ADAFRUITMS1438_I2C_BUS,
ADAFRUITMS1438_DEFAULT_I2C_ADDR);
// Setup for use with a DC motor connected to the M3 port // Setup for use with a DC motor connected to the M3 port
// set a PWM period of 50Hz // set a PWM period of 50Hz
ms->setPWMPeriod(50); ms.setPWMPeriod(50);
// disable first, to be safe // disable first, to be safe
ms->disableMotor(AdafruitMS1438::MOTOR_M3); ms.disableMotor(AdafruitMS1438::MOTOR_M3);
// set speed at 50% // set speed at 50%
ms->setMotorSpeed(AdafruitMS1438::MOTOR_M3, 50); ms.setMotorSpeed(AdafruitMS1438::MOTOR_M3, 50);
ms->setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CW); ms.setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CW);
cout << "Spin M3 at half speed for 3 seconds, then reverse for 3 seconds." cout << "Spin M3 at half speed for 3 seconds, then reverse for 3 seconds." << endl;
<< endl;
ms->enableMotor(AdafruitMS1438::MOTOR_M3); ms.enableMotor(AdafruitMS1438::MOTOR_M3);
sleep(3); upm_delay(3);
cout << "Reversing M3" << endl; cout << "Reversing M3" << endl;
ms->setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CCW); ms.setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CCW);
sleep(3); upm_delay(3);
cout << "Stopping M3" << endl; cout << "Stopping M3" << endl;
ms->disableMotor(AdafruitMS1438::MOTOR_M3); ms.disableMotor(AdafruitMS1438::MOTOR_M3);
cout << "Exiting" << endl; cout << "Exiting" << endl;
//! [Interesting] //! [Interesting]
delete ms; return 0;
return 0;
} }

View File

@ -22,56 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
/** /**
* Description * Description
* Demo program for Adafruit 16 channel servo shield/controller * Demo program for Adafruit 16 channel servo shield/controller
* Physical setup for tests is a single servo attached to one channel. * Physical setup for tests is a single servo attached to one channel.
* Note - when 3 or more GWS servos attached results unpredictable. * Note - when 3 or more GWS servos attached results unpredictable.
* Adafruit do recommend a Cap be installed on the board which should alleviate the issue. * Adafruit do recommend a Cap be installed on the board which should alleviate
* the issue.
* I (and Adafruit) are unable to give any Capacitor sizing data. * I (and Adafruit) are unable to give any Capacitor sizing data.
*/ */
#include <iostream> #include <iostream>
#include "adafruitss.hpp" #include "adafruitss.hpp"
#include <unistd.h> #include "upm_utilities.h"
using namespace std; using namespace std;
int main() { int
main()
{
int n;
int n; //! [Interesting]
upm::adafruitss servos(6, 0x40);
//! [Interesting] for (;;) {
upm::adafruitss* servos = new upm::adafruitss(6,0x40); cout << "Setting all to 0" << endl;
for (n = 0; n < 16; n++)
servos.servo(n, 1, 0); // GWS Mini Servo = Type 1.
upm_delay_us(1000000); // Wait 1 second
for (;;) cout << "Setting all to 45" << endl;
{ for (n = 0; n < 16; n++)
cout << "Setting all to 0" << endl; servos.servo(n, 1, 45);
for (n = 0; n < 16; n++) upm_delay_us(1000000); // Wait 1 second
servos->servo(n, 1, 0); // GWS Mini Servo = Type 1.
usleep(1000000); // Wait 1 second
cout << "Setting all to 45" << endl; cout << "Setting all to 90" << endl;
for (n = 0; n < 16; n++) for (n = 0; n < 16; n++)
servos->servo(n, 1, 45); servos.servo(n, 1, 90);
usleep(1000000); // Wait 1 second upm_delay_us(1000000); // Wait 1 second
cout << "Setting all to 90" << endl; cout << "Setting all to 135" << endl;
for (n = 0; n < 16; n++) for (n = 0; n < 16; n++)
servos->servo(n, 1, 90); servos.servo(n, 1, 135);
usleep(1000000); // Wait 1 second upm_delay_us(1000000); // Wait 1 second
cout << "Setting all to 135" << endl; cout << "Setting all to 180" << endl;
for (n = 0; n < 16; n++) for (n = 0; n < 16; n++)
servos->servo(n, 1, 135); servos.servo(n, 1, 160);
usleep(1000000); // Wait 1 second upm_delay_us(2000000); // Wait 1 second
}
cout << "Setting all to 180" << endl; //! [Interesting]
for (n = 0; n < 16; n++) return 0;
servos->servo(n, 1, 160);
usleep(2000000); // Wait 1 second
}
//! [Interesting]
return 0;
} }

View File

@ -22,45 +22,44 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "adc121c021.hpp" #include "adc121c021.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an ADC121C021 on I2C bus 0 // Instantiate an ADC121C021 on I2C bus 0
upm::ADC121C021 *adc = new upm::ADC121C021(ADC121C021_I2C_BUS, upm::ADC121C021 adc(ADC121C021_I2C_BUS, ADC121C021_DEFAULT_I2C_ADDR);
ADC121C021_DEFAULT_I2C_ADDR);
// An analog sensor, such as a Grove light sensor, // An analog sensor, such as a Grove light sensor,
// must be attached to the adc // must be attached to the adc
// Prints the value and corresponding voltage every 50 milliseconds // Prints the value and corresponding voltage every 50 milliseconds
while (shouldRun) while (shouldRun) {
{ uint16_t val = adc.value();
uint16_t val = adc->value(); cout << "ADC value: " << val << " Volts = " << adc.valueToVolts(val) << endl;
cout << "ADC value: " << val << " Volts = " upm_delay_us(50000);
<< adc->valueToVolts(val) << endl;
usleep(50000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete adc; return 0;
return 0;
} }

View File

@ -12,9 +12,12 @@
// This example code runs on an Intel Edison and uses mraa to acquire data // This example code runs on an Intel Edison and uses mraa to acquire data
// from an ADIS16448. This data is then scaled and printed onto the terminal. // from an ADIS16448. This data is then scaled and printed onto the terminal.
// //
// This software has been tested to connect to an ADIS16448 through a level shifter // This software has been tested to connect to an ADIS16448 through a level
// such as the TI TXB0104. The SPI lines (DIN, DOUT, SCLK, /CS) are all wired through // shifter
// the level shifter and the ADIS16448 is also being powered by the Intel Edison. // such as the TI TXB0104. The SPI lines (DIN, DOUT, SCLK, /CS) are all wired
// through
// the level shifter and the ADIS16448 is also being powered by the Intel
// Edison.
// //
// Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the // a copy of this software and associated documentation files (the
@ -36,31 +39,29 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "adis16448.hpp" #include "adis16448.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
while(true) while (true) {
{ //! [Interesting]
//! [Interesting] upm::ADIS16448 imu(0, 3); // upm::ADIS16448(SPI,RST)
upm::ADIS16448* imu = new upm::ADIS16448(0,3); //upm::ADIS16448(SPI,RST)
//Read the specified register, scale it, and display it on the screen // Read the specified register, scale it, and display it on the screen
std::cout << "XGYRO_OUT:" << imu->gyroScale(imu->regRead(XGYRO_OUT)) << std::endl; std::cout << "XGYRO_OUT:" << imu.gyroScale(imu.regRead(XGYRO_OUT)) << std::endl;
std::cout << "YGYRO_OUT:" << imu->gyroScale(imu->regRead(YGYRO_OUT)) << std::endl; std::cout << "YGYRO_OUT:" << imu.gyroScale(imu.regRead(YGYRO_OUT)) << std::endl;
std::cout << "ZGYRO_OUT:" << imu->gyroScale(imu->regRead(ZGYRO_OUT)) << std::endl; std::cout << "ZGYRO_OUT:" << imu.gyroScale(imu.regRead(ZGYRO_OUT)) << std::endl;
std::cout << " " << std::endl; std::cout << " " << std::endl;
std::cout << "XACCL_OUT:" << imu->accelScale(imu->regRead(XACCL_OUT)) << std::endl; std::cout << "XACCL_OUT:" << imu.accelScale(imu.regRead(XACCL_OUT)) << std::endl;
std::cout << "YACCL_OUT:" << imu->accelScale(imu->regRead(YACCL_OUT)) << std::endl; std::cout << "YACCL_OUT:" << imu.accelScale(imu.regRead(YACCL_OUT)) << std::endl;
std::cout << "ZACCL_OUT:" << imu->accelScale(imu->regRead(ZACCL_OUT)) << std::endl; std::cout << "ZACCL_OUT:" << imu.accelScale(imu.regRead(ZACCL_OUT)) << std::endl;
std::cout << " " << std::endl; std::cout << " " << std::endl;
//! [Interesting] //! [Interesting]
sleep(1); upm_delay(1);
} }
return (0); return (0);
} }

View File

@ -22,59 +22,50 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h> #include <exception>
#include <iostream> #include <iostream>
#include <stddef.h>
#include "ads1015.hpp" #include "ads1015.hpp"
#include "iADC.hpp"
#include "mraa/gpio.hpp" #include "mraa/gpio.hpp"
#include "upm_utilities.h"
#define EDISON_I2C_BUS 1 #define EDISON_I2C_BUS 1
#define FT4222_I2C_BUS 0 #define FT4222_I2C_BUS 0
#define EDISON_GPIO_SI7005_CS 20 #define EDISON_GPIO_SI7005_CS 20
int
//! [Interesting] main()
// Simple example of using IADC to determine
// which sensor is present and return its name.
// IADC is then used to get readings from sensor
upm::IADC* getADC()
{ {
upm::IADC* adc = NULL; /* Create an instance of the ADS1015 sensor */
try { upm::ADS1015 sensor(EDISON_I2C_BUS);
adc = new upm::ADS1015(EDISON_I2C_BUS); mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS); gpio.dir(mraa::DIR_OUT_HIGH);
gpio.dir(mraa::DIR_OUT_HIGH);
return adc;
} catch (std::exception& e) {
std::cerr << "ADS1015: " << e.what() << std::endl;
}
return adc;
}
int main () /* Show usage from the IADC interface */
{ upm::IADC* adc = static_cast<upm::IADC*>(&sensor);
upm::IADC* adc = getADC();
if (adc == NULL) { if (adc == NULL) {
std::cout << "ADC not detected" << std::endl; std::cout << "ADC not detected" << std::endl;
return 1; return 1;
} }
std::cout << "ADC " << adc->getModuleName() << " detected. " ; std::cout << "ADC " << adc->getModuleName() << " detected. ";
std::cout << adc->getNumInputs() << " inputs available" << std::endl; std::cout << adc->getNumInputs() << " inputs available" << std::endl;
while (true) { while (true) {
for (unsigned int i=0; i<adc->getNumInputs(); ++i) { for (unsigned int i = 0; i < adc->getNumInputs(); ++i) {
std::cout << "Input " << i; std::cout << "Input " << i;
try { try {
float voltage = adc->getVoltage(i); float voltage = adc->getVoltage(i);
std::cout << ": Voltage = " << voltage << "V" << std::endl; std::cout << ": Voltage = " << voltage << "V" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
} }
sleep(1); upm_delay(1);
} }
delete adc;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -29,11 +29,13 @@
*/ */
#include <fstream> #include <fstream>
#include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
#include <unistd.h>
#include "ads1015.hpp" #include "ads1015.hpp"
#include "ads1x15.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
@ -41,39 +43,41 @@ using namespace upm;
bool running = true; // Controls main read/write loop bool running = true; // Controls main read/write loop
// Thread function // Thread function
void stop() void
stop()
{ {
sleep(10); upm_delay(10);
running = false; running = false;
} }
int main() int
main()
{ {
//! [Interesting] //! [Interesting]
long id = 0; // Sample number long id = 0; // Sample number
string fileName = "./ads1015.data"; // Output filename string fileName = "./ads1015.data"; // Output filename
ofstream f; ofstream f;
// Initialize and configure the ADS1015 // Initialize and configure the ADS1015
ADS1015 *ads1015 = new upm::ADS1015(0, 0x48); ADS1015 ads1015(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1 // Put the ADC into differential mode for pins A0 and A1
ads1015->getSample(ADS1X15::DIFF_0_1); ads1015.getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V // Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API // Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details // documentation for details
ads1015->setGain(ADS1X15::GAIN_TWO); ads1015.setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 3300 samples per second (max) and turn on continuous // Set the sample rate to 3300 samples per second (max) and turn on continuous
// sampling // sampling
ads1015->setSPS(ADS1015::SPS_3300); ads1015.setSPS(ADS1015::SPS_3300);
ads1015->setContinuous(true); ads1015.setContinuous(true);
// Enable exceptions from the output stream // Enable exceptions from the output stream
f.exceptions(ofstream::failbit | ofstream::badbit); f.exceptions(ofstream::failbit | ofstream::badbit);
// Open the file // Open the file
try{ try {
f.open(fileName); f.open(fileName);
// Output formatting // Output formatting
@ -81,19 +85,18 @@ int main()
f.precision(7); f.precision(7);
// Start the thread that will stop logging after 10 seconds // Start the thread that will stop logging after 10 seconds
thread timer (stop); thread timer(stop);
// Read sensor data and write it to the output file every ms // Read sensor data and write it to the output file every ms
while(running){ while (running) {
f << id++ << " " << ads1015->getLastSample() << endl; f << id++ << " " << ads1015.getLastSample() << endl;
usleep(1000); upm_delay_us(1000);
} }
// Clean-up and exit // Clean-up and exit
timer.join(); timer.join();
f.close(); f.close();
delete ads1015; } catch (ios_base::failure& e) {
} catch (ios_base::failure &e) {
cout << "Failed to write to file: " << e.what() << endl; cout << "Failed to write to file: " << e.what() << endl;
return 1; return 1;
} }
@ -101,4 +104,3 @@ int main()
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -28,11 +28,13 @@
*/ */
#include <fstream> #include <fstream>
#include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
#include <unistd.h>
#include "ads1115.hpp" #include "ads1115.hpp"
#include "ads1x15.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
@ -40,16 +42,18 @@ using namespace upm;
bool running = true; // Controls main read/write loop bool running = true; // Controls main read/write loop
// Thread function // Thread function
void stop() void
stop()
{ {
sleep(10); upm_delay(10);
running = false; running = false;
} }
int main() int
main()
{ {
//! [Interesting] //! [Interesting]
long id = 0; // Sample number long id = 0; // Sample number
string fileName = "./ads1115.data"; // Output filename string fileName = "./ads1115.data"; // Output filename
ofstream f; ofstream f;
@ -57,26 +61,26 @@ int main()
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus // There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3 // - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7 // - 0x49 gives access to pins A4 - A7
ADS1115 *ads1115 = new upm::ADS1115(0, 0x48); ADS1115 ads1115(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1, // Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins // the SM-24 Geophone is connected to these pins
ads1115->getSample(ADS1X15::DIFF_0_1); ads1115.getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V // Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API // Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details // documentation for details
ads1115->setGain(ADS1X15::GAIN_TWO); ads1115.setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous // Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling // sampling
ads1115->setSPS(ADS1115::SPS_860); ads1115.setSPS(ADS1115::SPS_860);
ads1115->setContinuous(true); ads1115.setContinuous(true);
// Enable exceptions from the output stream // Enable exceptions from the output stream
f.exceptions(ofstream::failbit | ofstream::badbit); f.exceptions(ofstream::failbit | ofstream::badbit);
// Open the file // Open the file
try{ try {
f.open(fileName); f.open(fileName);
// Output formatting // Output formatting
@ -84,19 +88,18 @@ int main()
f.precision(7); f.precision(7);
// Start the thread that will stop logging after 10 seconds // Start the thread that will stop logging after 10 seconds
thread timer (stop); thread timer(stop);
// Read sensor data and write it to the output file every ms // Read sensor data and write it to the output file every ms
while(running){ while (running) {
f << id++ << " " << ads1115->getLastSample() << endl; f << id++ << " " << ads1115.getLastSample() << endl;
usleep(1000); upm_delay_us(1000);
} }
// Clean-up and exit // Clean-up and exit
timer.join(); timer.join();
f.close(); f.close();
delete ads1115; } catch (ios_base::failure& e) {
} catch (ios_base::failure &e) {
cout << "Failed to write to file: " << e.what() << endl; cout << "Failed to write to file: " << e.what() << endl;
return 1; return 1;
} }
@ -104,4 +107,3 @@ int main()
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,301 +22,306 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include "mraa.hpp"
#include <iostream> #include <iostream>
#include <unistd.h>
#include "ads1015.hpp" #include "ads1015.hpp"
#include "ads1115.hpp" #include "ads1115.hpp"
#include "ads1x15.hpp"
int
main()
int main()
{ {
using namespace std;
using namespace upm;
int command;
//! [Interesting]
// Select the device you are testing here and adjust case 6 for the correct
// sample rates.
upm::ADS1115 ads(1, 0x49);
float inputVoltage;
int ans;
using namespace std; do {
using namespace upm; cout << endl;
int command; cout << "1 - get Conversion \t";
//! [Interesting] cout << "2 - get last conversion" << endl;
//Select the device you are testing here and adjust case 6 for the correct sample rates. cout << "3 - get Gain \t\t";
//upm::ADS1015 *ads = new upm::ADS1015(1); cout << "4 - set Gain" << endl;
upm::ADS1115 *ads = new upm::ADS1115(1, 0x49); ;
float inputVoltage; cout << "5 - get Data Rate \t";
int ans; cout << "6 - set Data Rate" << endl;
cout << "7 - Set Upper Threshold \t";
cout << "8 - Set Lower Threshold \t";
cout << "9 - Display Thresholds \t";
cout << "10 - Set Default Thresholds \t";
cout << "11 - Set conversion ready" << endl;
cout << "12 - get Comp Que \t";
cout << "13 - set Comp Que" << endl;
cout << "14 - get Comp Pol \t";
cout << "15 - set Comp Pol" << endl;
cout << "16 - get Comp mode \t";
cout << "17 - set Comp mode " << endl;
cout << "18 - get Comp Latch\t";
cout << "19 - set Comp Latch " << endl;
cout << "20 - get Continuous \t";
cout << "21 - set Continuous \t" << endl;
cout << "-1 - exit" << endl;
cout << "Enter a command: ";
cin >> command;
do switch (command) {
{ case 2:
cout << ads.getLastSample() << endl;
cout << endl; break;
cout << "1 - get Conversion \t" ; case 3:
cout << "2 - get last conversion" << endl; cout << std::hex << ads.getGain() << endl;
cout << "3 - get Gain \t\t"; break;
cout << "4 - set Gain" << endl;; case 5:
cout << "5 - get Data Rate \t"; cout << std::hex << ads.getSPS() << endl;
cout << "6 - set Data Rate" << endl; break;
cout << "7 - Set Upper Threshold \t" ; case 4:
cout << "8 - Set Lower Threshold \t"; int gain;
cout << "9 - Display Thresholds \t"; ADS1015::ADSGAIN set_gain;
cout << "10 - Set Default Thresholds \t"; cout << "select one of the following:" << endl;
cout << "11 - Set conversion ready" << endl; cout << "1 -> gain 2/3 \t 2 -> gain1 \t 3 -> gain 2" << endl;
cout << "12 - get Comp Que \t" ; cout << "4 -> gain 4 \t 5 -> gain 8 \t 6 -> gain 15" << endl;
cout << "13 - set Comp Que" << endl; cin >> gain;
cout << "14 - get Comp Pol \t"; switch (gain) {
cout << "15 - set Comp Pol" << endl; case 1:
cout << "16 - get Comp mode \t"; set_gain = ADS1X15::GAIN_TWOTHIRDS;
cout << "17 - set Comp mode " << endl; break;
cout << "18 - get Comp Latch\t"; case 2:
cout << "19 - set Comp Latch " << endl; set_gain = ADS1X15::GAIN_ONE;
cout << "20 - get Continuous \t"; break;
cout << "21 - set Continuous \t" << endl; case 3:
cout << "-1 - exit" << endl; set_gain = ADS1X15::GAIN_TWO;
cout << "Enter a command: "; break;
cin >> command; case 4:
set_gain = ADS1X15::GAIN_FOUR;
break;
switch(command) case 5:
{ set_gain = ADS1X15::GAIN_EIGHT;
break;
case 6:
set_gain = ADS1X15::GAIN_SIXTEEN;
break;
default:
set_gain = ADS1X15::GAIN_ONE;
}
ads.setGain(set_gain);
break;
case 6:
int rate;
/*ADS1015::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_120 \t 2 -> SPS_250 \t 3 -> SPS_490 \t 4 -> SPS_920" <<
endl;
cout << "5 -> SPS_1600 \t 6 -> SPS_2400 \t 7 -> SPS_3300" << endl;
cin >> rate;
switch(rate){
case 1:
set_rate = ADS1015::SPS_128;
break;
case 2: case 2:
cout << ads->getLastSample() << endl; set_rate = ADS1015::SPS_250;
break; break;
case 3: case 3:
cout << std::hex << ads->getGain() << endl; set_rate = ADS1015::SPS_490;
break;
case 5:
cout << std::hex << ads->getSPS() << endl;
break; break;
case 4: case 4:
int gain; set_rate = ADS1015::SPS_920;
ADS1015::ADSGAIN set_gain; break;
cout << "select one of the following:" << endl; case 5:
cout << "1 -> gain 2/3 \t 2 -> gain1 \t 3 -> gain 2" << endl; set_rate = ADS1015::SPS_1600;
cout << "4 -> gain 4 \t 5 -> gain 8 \t 6 -> gain 15" << endl;
cin >> gain;
switch(gain){
case 1:
set_gain = ADS1X15::GAIN_TWOTHIRDS;
break;
case 2:
set_gain = ADS1X15::GAIN_ONE;
break;
case 3:
set_gain = ADS1X15::GAIN_TWO;
break;
case 4:
set_gain = ADS1X15::GAIN_FOUR;
break;
case 5:
set_gain = ADS1X15::GAIN_EIGHT;
break;
case 6:
set_gain = ADS1X15::GAIN_SIXTEEN;
break;
default:
set_gain = ADS1X15::GAIN_ONE;
}
ads->setGain(set_gain);
break; break;
case 6: case 6:
int rate; set_rate = ADS1015::SPS_2400;
/*ADS1015::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_120 \t 2 -> SPS_250 \t 3 -> SPS_490 \t 4 -> SPS_920" << endl;
cout << "5 -> SPS_1600 \t 6 -> SPS_2400 \t 7 -> SPS_3300" << endl;
cin >> rate;
switch(rate){
case 1:
set_rate = ADS1015::SPS_128;
break;
case 2:
set_rate = ADS1015::SPS_250;
break;
case 3:
set_rate = ADS1015::SPS_490;
break;
case 4:
set_rate = ADS1015::SPS_920;
break;
case 5:
set_rate = ADS1015::SPS_1600;
break;
case 6:
set_rate = ADS1015::SPS_2400;
break;
case 7:
set_rate = ADS1015::SPS_3300;
break;
default:
set_rate = ADS1015::SPS_1600;
} */
ADS1115::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_8 \t 2 -> SPS_16 \t 3 -> SPS_32 \t 4 -> SPS_64" << endl;
cout << "5 -> SPS_128 \t 6 -> SPS_250 \t 7 -> SPS_475 \t 8-> SPS_860" << endl;
cin >> rate;
switch(rate){
case 1:
set_rate = ADS1115::SPS_8;
break;
case 2:
set_rate = ADS1115::SPS_16;
break;
case 3:
set_rate = ADS1115::SPS_32;
break;
case 4:
set_rate = ADS1115::SPS_64;
break;
case 5:
set_rate = ADS1115::SPS_128;
break;
case 6:
set_rate = ADS1115::SPS_250;
break;
case 7:
set_rate = ADS1115::SPS_475;
break;
case 8:
set_rate = ADS1115::SPS_860;
break;
default:
set_rate = ADS1115::SPS_128;
}
ads->setSPS(set_rate);
break;
case 1:
int mode;
ADS1X15::ADSMUXMODE set_mode;
cout << "select one of the following:" << endl;
cout << "1 -> MUX_0_1 \t 2 -> MUX_0_3 \t 3 -> MUX_1_3 \t 4 -> MUX_2_3" << endl;
cout << "5 -> SINGLE_0 \t 6 -> SINGLE_1 \t 7 -> SINGLE_2 \t 8 -> SINGLE_3" << endl;
cin >> mode;
switch(mode){
case 1:
set_mode = ADS1X15::DIFF_0_1;
break;
case 2:
set_mode = ADS1X15::DIFF_0_3;
break;
case 3:
set_mode = ADS1X15::DIFF_1_3;
break;
case 4:
set_mode = ADS1X15::DIFF_2_3;
break;
case 5:
set_mode = ADS1X15::SINGLE_0;
break;
case 6:
set_mode = ADS1X15::SINGLE_1;
break;
case 7:
set_mode = ADS1X15::SINGLE_2;
break;
case 8:
set_mode = ADS1X15::SINGLE_3;
break;
default:
set_mode = ADS1X15::DIFF_0_1;
break;
}
cout << ads->getSample(set_mode) << endl;
break; break;
case 7: case 7:
cout << " enter a float value: " ; set_rate = ADS1015::SPS_3300;
cin >> inputVoltage;
ads->setThresh(ADS1115::THRESH_HIGH, inputVoltage);
break;
case 8:
cout << " enter a float value: " ;
cin >> inputVoltage;
ads->setThresh(ADS1115::THRESH_LOW, inputVoltage);
break;
case 9:
cout << "Upper " << ads->getThresh(ADS1X15::THRESH_HIGH) << endl;
cout << "Lower " << ads->getThresh(ADS1X15::THRESH_LOW) << endl;
break;
case 10:
ads->setThresh(ADS1115::THRESH_DEFAULT);
break;
case 11:
ads->setThresh(ADS1015::CONVERSION_RDY);
break;
case 12:
cout << ads->getCompQue() << endl;
break;
case 13:
int que;
cout << "select one of the following:" << endl;
cout << "1 -> CQUE_1CONV \t 2 -> CQUE_2CONV \t 3 -> CQUE_3CONV \t 4 -> CQUE_NONE" << endl;
cin >> que;
switch(que){
case 1:
ads->setCompQue(ADS1X15::CQUE_1CONV);
break;
case 2:
ads->setCompQue(ADS1X15::CQUE_2CONV);
break;
case 3:
ads->setCompQue(ADS1X15::CQUE_4CONV);
break;
case 4:
default:
ads->setCompQue(ADS1X15::CQUE_NONE);
break;
}
break;
case 14:
cout << ads->getCompPol() << endl;
break;
case 15:
cout << "select one of the following:" << endl;
cout << "1 -> active high \t 2 -> active low" << endl;
cin >> ans;
if(ans == 1) ads->setCompPol(true);
else ads->setCompPol(false);
break;
case 16:
cout << ads->getCompMode() << endl;
break;
case 17:
cout << "select one of the following:" << endl;
cout << "1 -> Window \t 2 -> Traditional (default)" << endl;
cin >> ans;
if(ans == 1) ads->setCompMode(true);
else ads->setCompMode();
break;
case 18:
cout << ads->getCompLatch() << endl;
break;
case 19:
cout << "select one of the following:" << endl;
cout << "1 -> Latching \t 2 -> Non-latching (default)" << endl;
cin >> ans;
if(ans == 1) ads->setCompLatch(true);
else ads->setCompLatch();
break;
case 20:
cout << ads->getContinuous() << endl;
break;
case 21:
cout << "select one of the following:" << endl;
cout << "1 -> Power Down (default) \t 2 -> Continuous" << endl;
cin >> ans;
if(ans == 1) ads->setContinuous(true);
else ads->setContinuous();
break;
case -1:
break; break;
default: default:
set_rate = ADS1015::SPS_1600;
} */
ADS1115::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_8 \t 2 -> SPS_16 \t 3 -> SPS_32 \t 4 -> SPS_64" << endl;
cout << "5 -> SPS_128 \t 6 -> SPS_250 \t 7 -> SPS_475 \t 8-> SPS_860" << endl;
cin >> rate;
switch (rate) {
case 1:
set_rate = ADS1115::SPS_8;
break;
case 2:
set_rate = ADS1115::SPS_16;
break;
case 3:
set_rate = ADS1115::SPS_32;
break;
case 4:
set_rate = ADS1115::SPS_64;
break;
case 5:
set_rate = ADS1115::SPS_128;
break;
case 6:
set_rate = ADS1115::SPS_250;
break;
case 7:
set_rate = ADS1115::SPS_475;
break;
case 8:
set_rate = ADS1115::SPS_860;
break;
default:
set_rate = ADS1115::SPS_128;
}
break; ads.setSPS(set_rate);
} break;
case 1:
int mode;
ADS1X15::ADSMUXMODE set_mode;
cout << "select one of the following:" << endl;
cout << "1 -> MUX_0_1 \t 2 -> MUX_0_3 \t 3 -> MUX_1_3 \t 4 -> MUX_2_3" << endl;
cout << "5 -> SINGLE_0 \t 6 -> SINGLE_1 \t 7 -> SINGLE_2 \t 8 -> "
"SINGLE_3"
<< endl;
cin >> mode;
switch (mode) {
case 1:
set_mode = ADS1X15::DIFF_0_1;
break;
case 2:
set_mode = ADS1X15::DIFF_0_3;
break;
case 3:
set_mode = ADS1X15::DIFF_1_3;
break;
case 4:
set_mode = ADS1X15::DIFF_2_3;
break;
case 5:
set_mode = ADS1X15::SINGLE_0;
break;
case 6:
set_mode = ADS1X15::SINGLE_1;
break;
case 7:
set_mode = ADS1X15::SINGLE_2;
break;
case 8:
set_mode = ADS1X15::SINGLE_3;
break;
default:
set_mode = ADS1X15::DIFF_0_1;
break;
}
cout << ads.getSample(set_mode) << endl;
break;
case 7:
cout << " enter a float value: ";
cin >> inputVoltage;
ads.setThresh(ADS1115::THRESH_HIGH, inputVoltage);
break;
case 8:
cout << " enter a float value: ";
cin >> inputVoltage;
ads.setThresh(ADS1115::THRESH_LOW, inputVoltage);
break;
case 9:
cout << "Upper " << ads.getThresh(ADS1X15::THRESH_HIGH) << endl;
cout << "Lower " << ads.getThresh(ADS1X15::THRESH_LOW) << endl;
break;
case 10:
ads.setThresh(ADS1115::THRESH_DEFAULT);
break;
case 11:
ads.setThresh(ADS1015::CONVERSION_RDY);
break;
case 12:
cout << ads.getCompQue() << endl;
break;
case 13:
int que;
cout << "select one of the following:" << endl;
cout << "1 -> CQUE_1CONV \t 2 -> CQUE_2CONV \t 3 -> CQUE_3CONV \t 4 -> "
"CQUE_NONE"
<< endl;
cin >> que;
switch (que) {
case 1:
ads.setCompQue(ADS1X15::CQUE_1CONV);
break;
case 2:
ads.setCompQue(ADS1X15::CQUE_2CONV);
break;
case 3:
ads.setCompQue(ADS1X15::CQUE_4CONV);
break;
case 4:
default:
ads.setCompQue(ADS1X15::CQUE_NONE);
break;
}
break;
case 14:
cout << ads.getCompPol() << endl;
break;
case 15:
cout << "select one of the following:" << endl;
cout << "1 -> active high \t 2 -> active low" << endl;
cin >> ans;
if (ans == 1)
ads.setCompPol(true);
else
ads.setCompPol(false);
break;
case 16:
cout << ads.getCompMode() << endl;
break;
case 17:
cout << "select one of the following:" << endl;
cout << "1 -> Window \t 2 -> Traditional (default)" << endl;
cin >> ans;
if (ans == 1)
ads.setCompMode(true);
else
ads.setCompMode();
break;
case 18:
cout << ads.getCompLatch() << endl;
break;
case 19:
cout << "select one of the following:" << endl;
cout << "1 -> Latching \t 2 -> Non-latching (default)" << endl;
cin >> ans;
if (ans == 1)
ads.setCompLatch(true);
else
ads.setCompLatch();
break;
case 20:
cout << ads.getContinuous() << endl;
break;
case 21:
cout << "select one of the following:" << endl;
cout << "1 -> Power Down (default) \t 2 -> Continuous" << endl;
cin >> ans;
if (ans == 1)
ads.setContinuous(true);
else
ads.setContinuous();
break;
case -1:
break;
default:
}while (command != -1 ); break;
}
} while (command != -1);
delete ads; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,57 +22,58 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <string>
#include "adxl335.hpp" #include "adxl335.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2 // Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2
upm::ADXL335* accel = new upm::ADXL335(0, 1, 2); upm::ADXL335 accel(0, 1, 2);
cout << "Please make sure the sensor is completely still. Sleeping for" cout << "Please make sure the sensor is completely still. Sleeping for"
<< " 2 seconds." << endl; << " 2 seconds." << endl;
sleep(2); upm_delay(2);
cout << "Calibrating..." << endl; cout << "Calibrating..." << endl;
accel->calibrate(); accel.calibrate();
while (shouldRun)
{
int x, y, z;
float aX, aY, aZ;
accel->values(&x, &y, &z); while (shouldRun) {
cout << "Raw Values: X: " << x << " Y: " << y << " Z: " << z << endl; int x, y, z;
float aX, aY, aZ;
accel->acceleration(&aX, &aY, &aZ); accel.values(&x, &y, &z);
cout << "Acceleration: X: " << aX << "g" << endl; cout << "Raw Values: X: " << x << " Y: " << y << " Z: " << z << endl;
cout << "Acceleration: Y: " << aY << "g" << endl;
cout << "Acceleration: Z: " << aZ << "g" << endl;
cout << endl;
usleep(200000); accel.acceleration(&aX, &aY, &aZ);
cout << "Acceleration: X: " << aX << "g" << endl;
cout << "Acceleration: Y: " << aY << "g" << endl;
cout << "Acceleration: Z: " << aZ << "g" << endl;
cout << endl;
upm_delay_us(200000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete accel; return 0;
return 0;
} }

View File

@ -22,30 +22,32 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h> #include <stdio.h>
#include "adxl345.hpp" #include "adxl345.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
int16_t *raw; int16_t* raw;
float *acc; float* acc;
// Note: Sensor only works at 3.3V on the Intel Edison with Arduino breakout // Note: Sensor only works at 3.3V on the Intel Edison with Arduino breakout
upm::Adxl345* accel = new upm::Adxl345(0); upm::Adxl345 accel(0);
while(true){ while (true) {
accel->update(); // Update the data accel.update(); // Update the data
raw = accel->getRawValues(); // Read raw sensor data raw = accel.getRawValues(); // Read raw sensor data
acc = accel->getAcceleration(); // Read acceleration (g) acc = accel.getAcceleration(); // Read acceleration (g)
fprintf(stdout, "Current scale: 0x%2xg\n", accel->getScale()); fprintf(stdout, "Current scale: 0x%2xg\n", accel.getScale());
fprintf(stdout, "Raw: %6d %6d %6d\n", raw[0], raw[1], raw[2]); fprintf(stdout, "Raw: %6d %6d %6d\n", raw[0], raw[1], raw[2]);
fprintf(stdout, "AccX: %5.2f g\n", acc[0]); fprintf(stdout, "AccX: %5.2f g\n", acc[0]);
fprintf(stdout, "AccY: %5.2f g\n", acc[1]); fprintf(stdout, "AccY: %5.2f g\n", acc[1]);
fprintf(stdout, "AccZ: %5.2f g\n", acc[2]); fprintf(stdout, "AccZ: %5.2f g\n", acc[2]);
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,50 +22,51 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "adxrs610.hpp" #include "adxrs610.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a ADXRS610 sensor on analog pin A0 (dataout), and // Instantiate a ADXRS610 sensor on analog pin A0 (dataout), and
// analog A1 (temp out) with an analog reference voltage of // analog A1 (temp out) with an analog reference voltage of
// 5.0 // 5.0
upm::ADXRS610 *sensor = new upm::ADXRS610(0, 1, 5.0); upm::ADXRS610 sensor(0, 1, 5.0);
// set a deadband region around the zero point to report 0.0 (optional)
sensor->setDeadband(0.015);
// Every tenth of a second, sample the ADXRS610 and output it's // set a deadband region around the zero point to report 0.0 (optional)
// corresponding temperature and angular velocity sensor.setDeadband(0.015);
while (shouldRun) // Every tenth of a second, sample the ADXRS610 and output it's
{ // corresponding temperature and angular velocity
cout << "Vel (deg/s): " << sensor->getAngularVelocity() << endl;
cout << "Temp (C): " << sensor->getTemperature() << endl; while (shouldRun) {
cout << "Vel (deg/s): " << sensor.getAngularVelocity() << endl;
usleep(100000); cout << "Temp (C): " << sensor.getTemperature() << endl;
upm_delay_us(100000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,16 +22,15 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include "am2315.hpp" #include "am2315.hpp"
#include "upm_utilities.h"
volatile int doWork = 0; volatile int doWork = 0;
upm::AM2315 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
@ -42,34 +41,30 @@ sig_handler(int signo)
} }
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// Register signal handler // Register signal handler
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
float humidity = 0.0; float humidity = 0.0;
float temperature = 0.0; float temperature = 0.0;
sensor = new upm::AM2315(0, AM2315_I2C_ADDRESS); upm::AM2315 sensor(0, AM2315_I2C_ADDRESS);
sensor->testSensor(); sensor.testSensor();
while (!doWork) { while (!doWork) {
humidity = sensor->getHumidity(); humidity = sensor.getHumidity();
temperature = sensor->getTemperature(); temperature = sensor.getTemperature();
std::cout << "humidity value = " << std::cout << "humidity value = " << humidity << ", temperature value = " << temperature
humidity << << std::endl;
", temperature value = " << upm_delay_us(500000);
temperature << std::endl;
usleep (500000);
} }
//! [Interesting] //! [Interesting]
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }

View File

@ -23,9 +23,6 @@
*/ */
#include "apa102.hpp" #include "apa102.hpp"
#include <iostream>
#include <signal.h>
#include <unistd.h>
using namespace std; using namespace std;
@ -34,18 +31,17 @@ main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a strip of 30 LEDs on SPI bus 0 // Instantiate a strip of 30 LEDs on SPI bus 0
upm::APA102* ledStrip = new upm::APA102(800, 0); upm::APA102 ledStrip(800, 0);
// Set all LEDs to Red // Set all LEDs to Red
ledStrip->setAllLeds(31, 255, 0, 0); ledStrip.setAllLeds(31, 255, 0, 0);
// Set a section (10 to 20) to blue // Set a section (10 to 20) to blue
ledStrip->setLeds(10, 20, 31, 0, 0, 255); ledStrip.setLeds(10, 20, 31, 0, 0, 255);
// Set a single LED to green // Set a single LED to green
ledStrip->setLed(15, 31, 0, 255, 0); ledStrip.setLed(15, 31, 0, 255, 0);
delete ledStrip;
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,41 +22,41 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "apds9002.hpp" #include "apds9002.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Luminance sensor on analog pin A0 // Instantiate a Grove Luminance sensor on analog pin A0
upm::APDS9002* luminance = new upm::APDS9002(0); upm::APDS9002 luminance(0);
while (shouldRun)
{
int val = luminance->value();
cout << "Luminance value is " << val << endl;
sleep(1); while (shouldRun) {
int val = luminance.value();
cout << "Luminance value is " << val << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete luminance; return 0;
return 0;
} }

View File

@ -22,10 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "apds9930.hpp" #include "apds9930.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
@ -44,31 +45,30 @@ main()
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Digital Proximity and Ambient Light sensor on iio device 4 // Instantiate a Digital Proximity and Ambient Light sensor on iio device 4
upm::APDS9930* light_proximity = new upm::APDS9930(4); upm::APDS9930 light_proximity(4);
// Kernel driver implement sleep 5000-5100us after enable illuminance sensor // Kernel driver implement upm_delay 5000-5100us after enable illuminance
light_proximity->enableIlluminance(true); // sensor
light_proximity.enableIlluminance(true);
// Kernel driver implement sleep 5000-5100us after enable proximity sensor // Kernel driver implement upm_delay 5000-5100us after enable proximity sensor
light_proximity->enableProximity(true); light_proximity.enableProximity(true);
// Tested this value works. Please change it on your platform // Tested this value works. Please change it on your platform
usleep(120000); upm_delay_us(120000);
while (shouldRun) { while (shouldRun) {
float lux = light_proximity->getAmbient(); float lux = light_proximity.getAmbient();
cout << "Luminance value is " << lux << endl; cout << "Luminance value is " << lux << endl;
float proximity = light_proximity->getProximity(); float proximity = light_proximity.getProximity();
cout << "Proximity value is " << proximity << endl; cout << "Proximity value is " << proximity << endl;
sleep(1); upm_delay(1);
} }
light_proximity->enableProximity(false); light_proximity.enableProximity(false);
light_proximity->enableIlluminance(false); light_proximity.enableIlluminance(false);
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete light_proximity;
return 0; return 0;
} }

View File

@ -22,69 +22,68 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "at42qt1070.hpp" #include "at42qt1070.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
void printButtons(upm::AT42QT1070 *touch) void
printButtons(upm::AT42QT1070& touch)
{ {
bool buttonPressed = false; bool buttonPressed = false;
uint8_t buttons = touch->getButtons(); uint8_t buttons = touch.getButtons();
cout << "Buttons Pressed: "; cout << "Buttons Pressed: ";
for (int i=0; i<7; i++) for (int i = 0; i < 7; i++) {
{ if (buttons & (1 << i)) {
if (buttons & (1 << i)) cout << i << " ";
{ buttonPressed = true;
cout << i << " ";
buttonPressed = true;
} }
} }
if (!buttonPressed) if (!buttonPressed)
cout << "None"; cout << "None";
cout << endl; cout << endl;
if (touch->isCalibrating()) if (touch.isCalibrating())
cout << "Calibration is occurring." << endl; cout << "Calibration is occurring." << endl;
if (touch->isOverflowed()) if (touch.isOverflowed())
cout << "Overflow was detected." << endl; cout << "Overflow was detected." << endl;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an AT42QT1070 on I2C bus 0 // Instantiate an AT42QT1070 on I2C bus 0
upm::AT42QT1070 *touch = new upm::AT42QT1070(AT42QT1070_I2C_BUS, upm::AT42QT1070 touch(AT42QT1070_I2C_BUS, AT42QT1070_DEFAULT_I2C_ADDR);
AT42QT1070_DEFAULT_I2C_ADDR);
while (shouldRun) while (shouldRun) {
{ touch.updateState();
touch->updateState(); printButtons(touch);
printButtons(touch); upm_delay_us(100000);
usleep(100000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete touch; return 0;
return 0;
} }

View File

@ -22,44 +22,45 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bh1750.hpp" #include "bh1750.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a BH1750 sensor using defaults (I2C bus (0), using // Instantiate a BH1750 sensor using defaults (I2C bus (0), using
// the default I2C address (0x23), and setting the mode to highest // the default I2C address (0x23), and setting the mode to highest
// resolution, lowest power mode). // resolution, lowest power mode).
upm::BH1750 *sensor = new upm::BH1750(); upm::BH1750 sensor;
// Every second, sample the BH1750 and output the measured lux value // Every second, sample the BH1750 and output the measured lux value
while (shouldRun) while (shouldRun) {
{ cout << "Detected Light Level (lux): " << sensor.getLux() << endl;
cout << "Detected Light Level (lux): " << sensor->getLux() << endl; upm_delay(1);
sleep(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,47 +22,47 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "biss0001.hpp" #include "biss0001.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Motion sensor on GPIO pin D2 // Instantiate a Grove Motion sensor on GPIO pin D2
upm::BISS0001* motion = new upm::BISS0001(2); upm::BISS0001 motion(2);
while (shouldRun)
{
bool val = motion->value();
if (val) while (shouldRun) {
cout << "Detecting moving object"; bool val = motion.value();
else
cout << "No moving objects detected";
cout << endl; if (val)
cout << "Detecting moving object";
else
cout << "No moving objects detected";
sleep(1); cout << endl;
}
//! [Interesting]
cout << "Exiting" << endl; upm_delay(1);
}
//! [Interesting]
delete motion; cout << "Exiting" << endl;
return 0;
return 0;
} }

View File

@ -22,49 +22,48 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bma220.hpp" #include "bma220.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMA220 using default parameters (bus 0, addr 0x0a) // Instantiate an BMA220 using default parameters (bus 0, addr 0x0a)
upm::BMA220 *sensor = new upm::BMA220(); upm::BMA220 sensor;
// Output data every half second until interrupted // Output data every half second until interrupted
while (shouldRun) while (shouldRun) {
{ sensor.update();
sensor->update();
float x, y, z;
sensor->getAccelerometer(&x, &y, &z);
cout << "Accelerometer: ";
cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl;
usleep(500000); float x, y, z;
sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer: ";
cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl;
upm_delay_us(500000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,60 +22,56 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bma250e.hpp" #include "bma250e.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMA250E using default I2C parameters // Instantiate an BMA250E using default I2C parameters
upm::BMA250E sensor; upm::BMA250E sensor;
// For SPI, bus 0, you would pass -1 as the address, and a valid pin // For SPI, bus 0, you would pass -1 as the address, and a valid pin
// for CS: BMA250E(0, -1, 10); // for CS: BMA250E(0, -1, 10);
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getAccelerometer(&x, &y, &z); sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer x: " << x cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl;
<< " y: " << y
<< " z: " << z
<< " g"
<< endl;
// we show both C and F for temperature // we show both C and F for temperature
cout << "Compensation Temperature: " << sensor.getTemperature() cout << "Compensation Temperature: " << sensor.getTemperature() << " C / "
<< " C / " << sensor.getTemperature(true) << " F" << sensor.getTemperature(true) << " F" << endl;
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,60 +22,56 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmg160.hpp" #include "bmg160.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMG160 using default I2C parameters // Instantiate an BMG160 using default I2C parameters
upm::BMG160 sensor; upm::BMG160 sensor;
// For SPI, bus 0, you would pass -1 as the address, and a valid pin // For SPI, bus 0, you would pass -1 as the address, and a valid pin
// for CS: BMG160(0, -1, 10); // for CS: BMG160(0, -1, 10);
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getGyroscope(&x, &y, &z); sensor.getGyroscope(&x, &y, &z);
cout << "Gyroscope x: " << x cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " degrees/s" << endl;
<< " y: " << y
<< " z: " << z
<< " degrees/s"
<< endl;
// we show both C and F for temperature // we show both C and F for temperature
cout << "Compensation Temperature: " << sensor.getTemperature() cout << "Compensation Temperature: " << sensor.getTemperature() << " C / "
<< " C / " << sensor.getTemperature(true) << " F" << sensor.getTemperature(true) << " F" << endl;
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,61 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmi160.hpp" #include "bmi160.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a BMI160 instance using default i2c bus and address // Instantiate a BMI160 instance using default i2c bus and address
upm::BMI160 *sensor = new upm::BMI160(); upm::BMI160 sensor;
while (shouldRun) while (shouldRun) {
{ // update our values from the sensor
// update our values from the sensor sensor.update();
sensor->update();
float dataX, dataY, dataZ; float dataX, dataY, dataZ;
sensor->getAccelerometer(&dataX, &dataY, &dataZ); sensor.getAccelerometer(&dataX, &dataY, &dataZ);
cout << "Accelerometer: "; cout << "Accelerometer: ";
cout << "AX: " << dataX << " AY: " << dataY << " AZ: " cout << "AX: " << dataX << " AY: " << dataY << " AZ: " << dataZ << endl;
<< dataZ << endl;
sensor->getGyroscope(&dataX, &dataY, &dataZ); sensor.getGyroscope(&dataX, &dataY, &dataZ);
cout << "Gryoscope: "; cout << "Gryoscope: ";
cout << "GX: " << dataX << " GY: " << dataY << " GZ: " cout << "GX: " << dataX << " GY: " << dataY << " GZ: " << dataZ << endl;
<< dataZ << endl;
sensor->getMagnetometer(&dataX, &dataY, &dataZ); sensor.getMagnetometer(&dataX, &dataY, &dataZ);
cout << "Magnetometer: "; cout << "Magnetometer: ";
cout << "MX: " << dataX << " MY: " << dataY << " MZ: " cout << "MX: " << dataX << " MY: " << dataY << " MZ: " << dataZ << endl;
<< dataZ << endl;
cout << endl; cout << endl;
usleep(500000); upm_delay_us(500000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,55 +22,52 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmm150.hpp" #include "bmm150.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMM150 using default I2C parameters // Instantiate an BMM150 using default I2C parameters
upm::BMM150 sensor; upm::BMM150 sensor;
// For SPI, bus 0, you would pass -1 as the address, and a valid pin // For SPI, bus 0, you would pass -1 as the address, and a valid pin
// for CS: BMM150(0, -1, 10); // for CS: BMM150(0, -1, 10);
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getMagnetometer(&x, &y, &z); sensor.getMagnetometer(&x, &y, &z);
cout << "Magnetometer x: " << x cout << "Magnetometer x: " << x << " y: " << y << " z: " << z << " uT" << endl;
<< " y: " << y
<< " z: " << z
<< " uT"
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,57 +22,54 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bme280.hpp" #include "bme280.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a BME280 instance using default i2c bus and address // Instantiate a BME280 instance using default i2c bus and address
upm::BME280 *sensor = new upm::BME280(); upm::BME280 sensor;
// For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS:
// BME280(0, -1, 10); // BME280(0, -1, 10);
while (shouldRun) while (shouldRun) {
{ // update our values from the sensor
// update our values from the sensor sensor.update();
sensor->update();
// we show both C and F for temperature // we show both C and F for temperature
cout << "Compensation Temperature: " << sensor->getTemperature() cout << "Compensation Temperature: " << sensor.getTemperature() << " C / "
<< " C / " << sensor->getTemperature(true) << " F" << sensor.getTemperature(true) << " F" << endl;
<< endl; cout << "Pressure: " << sensor.getPressure() << " Pa" << endl;
cout << "Pressure: " << sensor->getPressure() << " Pa" << endl; cout << "Computed Altitude: " << sensor.getAltitude() << " m" << endl;
cout << "Computed Altitude: " << sensor->getAltitude() << " m" << endl; cout << "Humidity: " << sensor.getHumidity() << " %RH" << endl;
cout << "Humidity: " << sensor->getHumidity() << " %RH" << endl;
cout << endl; cout << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -24,56 +24,53 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmp280.hpp" #include "bmp280.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a BMP280 instance using default i2c bus and address // Instantiate a BMP280 instance using default i2c bus and address
upm::BMP280 *sensor = new upm::BMP280(); upm::BMP280 sensor;
// For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS:
// BMP280(0, -1, 10); // BMP280(0, -1, 10);
while (shouldRun) while (shouldRun) {
{ // update our values from the sensor
// update our values from the sensor sensor.update();
sensor->update();
// we show both C and F for temperature // we show both C and F for temperature
cout << "Compensation Temperature: " << sensor->getTemperature() cout << "Compensation Temperature: " << sensor.getTemperature() << " C / "
<< " C / " << sensor->getTemperature(true) << " F" << sensor.getTemperature(true) << " F" << endl;
<< endl; cout << "Pressure: " << sensor.getPressure() << " Pa" << endl;
cout << "Pressure: " << sensor->getPressure() << " Pa" << endl; cout << "Computed Altitude: " << sensor.getAltitude() << " m" << endl;
cout << "Computed Altitude: " << sensor->getAltitude() << " m" << endl;
cout << endl; cout << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -27,52 +27,47 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmpx8x.hpp" #include "bmpx8x.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a BMPX8X sensor on I2C using defaults. // Instantiate a BMPX8X sensor on I2C using defaults.
upm::BMPX8X sensor; upm::BMPX8X sensor;
// Print the pressure, altitude, sea level, and // Print the pressure, altitude, sea level, and
// temperature values every 0.5 seconds // temperature values every 0.5 seconds
while (shouldRun) while (shouldRun) {
{
sensor.update(); sensor.update();
cout << "Pressure: " cout << "Pressure: " << sensor.getPressure()
<< sensor.getPressure() << " Pa, Temperature: " << sensor.getTemperature()
<< " Pa, Temperature: " << " C, Altitude: " << sensor.getAltitude()
<< sensor.getTemperature() << " m, Sea level: " << sensor.getSealevelPressure() << " Pa" << endl;
<< " C, Altitude: "
<< sensor.getAltitude()
<< " m, Sea level: "
<< sensor.getSealevelPressure()
<< " Pa"
<< endl;
usleep(500000); upm_delay_us(500000);
} }
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,59 +22,52 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmc150.hpp" #include "bmc150.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMC150 using default I2C parameters // Instantiate an BMC150 using default I2C parameters
upm::BMC150 sensor; upm::BMC150 sensor;
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getAccelerometer(&x, &y, &z); sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer x: " << x cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl;
<< " y: " << y
<< " z: " << z
<< " g"
<< endl;
sensor.getMagnetometer(&x, &y, &z); sensor.getMagnetometer(&x, &y, &z);
cout << "Magnetometer x: " << x cout << "Magnetometer x: " << x << " y: " << y << " z: " << z << " uT" << endl;
<< " y: " << y
<< " z: " << z
<< " uT"
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,59 +22,52 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmi055.hpp" #include "bmi055.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMI055 using default I2C parameters // Instantiate an BMI055 using default I2C parameters
upm::BMI055 sensor; upm::BMI055 sensor;
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getAccelerometer(&x, &y, &z); sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer x: " << x cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl;
<< " y: " << y
<< " z: " << z
<< " g"
<< endl;
sensor.getGyroscope(&x, &y, &z); sensor.getGyroscope(&x, &y, &z);
cout << "Gyroscope x: " << x cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " degrees/s" << endl;
<< " y: " << y
<< " z: " << z
<< " degrees/s"
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,66 +22,55 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bmx055.hpp" #include "bmx055.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BMX055 using default I2C parameters // Instantiate an BMX055 using default I2C parameters
upm::BMX055 sensor; upm::BMX055 sensor;
// now output data every 250 milliseconds // now output data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float x, y, z;
float x, y, z;
sensor.update(); sensor.update();
sensor.getAccelerometer(&x, &y, &z); sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer x: " << x cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl;
<< " y: " << y
<< " z: " << z
<< " g"
<< endl;
sensor.getGyroscope(&x, &y, &z); sensor.getGyroscope(&x, &y, &z);
cout << "Gyroscope x: " << x cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " degrees/s" << endl;
<< " y: " << y
<< " z: " << z
<< " degrees/s"
<< endl;
sensor.getMagnetometer(&x, &y, &z); sensor.getMagnetometer(&x, &y, &z);
cout << "Magnetometer x: " << x cout << "Magnetometer x: " << x << " y: " << y << " z: " << z << " uT" << endl;
<< " y: " << y
<< " z: " << z
<< " uT"
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
return 0; return 0;
} }

View File

@ -22,108 +22,82 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "bno055.hpp" #include "bno055.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an BNO055 using default parameters (bus 0, addr // Instantiate an BNO055 using default parameters (bus 0, addr
// 0x28). The default running mode is NDOF absolute orientation // 0x28). The default running mode is NDOF absolute orientation
// mode. // mode.
upm::BNO055 *sensor = new upm::BNO055(); upm::BNO055 sensor;
// First we need to calibrate.... // First we need to calibrate....
cout << "First we need to calibrate. 4 numbers will be output every" cout << "First we need to calibrate. 4 numbers will be output every" << endl;
<< endl; cout << "second for each sensor. 0 means uncalibrated, and 3 means" << endl;
cout << "second for each sensor. 0 means uncalibrated, and 3 means" cout << "fully calibrated." << endl;
<< endl; cout << "See the UPM documentation on this sensor for instructions on" << endl;
cout << "fully calibrated." cout << "what actions are required to calibrate." << endl;
<< endl; cout << endl;
cout << "See the UPM documentation on this sensor for instructions on"
<< endl;
cout << "what actions are required to calibrate."
<< endl;
cout << endl;
// do the calibration... // do the calibration...
while (shouldRun && !sensor->isFullyCalibrated()) while (shouldRun && !sensor.isFullyCalibrated()) {
{ int mag, acc, gyr, sys;
int mag, acc, gyr, sys; sensor.getCalibrationStatus(&mag, &acc, &gyr, &sys);
sensor->getCalibrationStatus(&mag, &acc, &gyr, &sys);
cout << "Magnetometer: " << mag cout << "Magnetometer: " << mag << " Accelerometer: " << acc << " Gyroscope: " << gyr
<< " Accelerometer: " << acc << " System: " << sys << endl;
<< " Gyroscope: " << gyr
<< " System: " << sys
<< endl;
sleep(1); upm_delay(1);
} }
cout << endl; cout << endl;
cout << "Calibration complete." << endl; cout << "Calibration complete." << endl;
cout << endl; cout << endl;
// now output various fusion data every 250 milliseconds // now output various fusion data every 250 milliseconds
while (shouldRun) while (shouldRun) {
{ float w, x, y, z;
float w, x, y, z;
sensor->update(); sensor.update();
sensor->getEulerAngles(&x, &y, &z); sensor.getEulerAngles(&x, &y, &z);
cout << "Euler: Heading: " << x cout << "Euler: Heading: " << x << " Roll: " << y << " Pitch: " << z << " degrees" << endl;
<< " Roll: " << y
<< " Pitch: " << z
<< " degrees"
<< endl;
sensor->getQuaternions(&w, &x, &y, &z); sensor.getQuaternions(&w, &x, &y, &z);
cout << "Quaternion: W: " << w cout << "Quaternion: W: " << w << " X: " << x << " Y: " << y << " Z: " << z << endl;
<< " X: " << x
<< " Y: " << y
<< " Z: " << z
<< endl;
sensor->getLinearAcceleration(&x, &y, &z); sensor.getLinearAcceleration(&x, &y, &z);
cout << "Linear Acceleration: X: " << x cout << "Linear Acceleration: X: " << x << " Y: " << y << " Z: " << z << " m/s^2" << endl;
<< " Y: " << y
<< " Z: " << z
<< " m/s^2"
<< endl;
sensor->getGravityVectors(&x, &y, &z); sensor.getGravityVectors(&x, &y, &z);
cout << "Gravity Vector: X: " << x cout << "Gravity Vector: X: " << x << " Y: " << y << " Z: " << z << " m/s^2" << endl;
<< " Y: " << y
<< " Z: " << z
<< " m/s^2"
<< endl;
cout << endl; cout << endl;
usleep(250000); upm_delay_us(250000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,28 +22,28 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "button.hpp" #include "button.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// This example uses GPIO 0 // This example uses GPIO 0
//! [Interesting] //! [Interesting]
// Create the button object using GPIO pin 0 // Create the button object using GPIO pin 0
upm::Button* button = new upm::Button(0); upm::Button button(0);
// Read the input and print, waiting one second between readings // Read the input and print, waiting one second between readings
while( 1 ) { while (1) {
std::cout << button->name() << " value is " << button->value() << std::endl; std::cout << button.name() << " value is " << button.value() << std::endl;
sleep(1); upm_delay(1);
} }
// Delete the button object // Delete the button object
delete button; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,37 +22,32 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "buzzer.hpp" #include "buzzer.hpp"
#include "buzzer_tones.h"
#include "upm_utilities.h" #include "upm_utilities.h"
int int
main(int argc, char **argv) { main(int argc, char** argv)
{
//! [Interesting] //! [Interesting]
int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, BUZZER_FA, BUZZER_SOL, BUZZER_LA, BUZZER_SI };
BUZZER_FA, BUZZER_SOL, BUZZER_LA,
BUZZER_SI };
// create Buzzer instance // create Buzzer instance
upm::Buzzer* sound = new upm::Buzzer(5); upm::Buzzer sound(5);
// print sensor name // print sensor name
std::cout << sound->name() << std::endl; std::cout << sound.name() << std::endl;
// play each sound (DO, RE, MI, etc...) for .5 seconds, pausing // play each sound (DO, RE, MI, etc...) for .5 seconds, pausing
// for 0.1 seconds between notes // for 0.1 seconds between notes
for (int chord_ind = 0; chord_ind < 7; chord_ind++) for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
{ std::cout << sound.playSound(chord[chord_ind], 500000) << std::endl;
std::cout << sound->playSound(chord[chord_ind], 500000) << std::endl;
upm_delay_ms(100); upm_delay_ms(100);
} }
//! [Interesting] //! [Interesting]
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sound;
return 0; return 0;
} }

View File

@ -22,56 +22,54 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "cjq4435.hpp" #include "cjq4435.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a CJQ4435 MOSFET on a PWM capable digital pin D3 // Instantiate a CJQ4435 MOSFET on a PWM capable digital pin D3
upm::CJQ4435* mosfet = new upm::CJQ4435(3); upm::CJQ4435 mosfet(3);
mosfet->setPeriodMS(10); mosfet.setPeriodMS(10);
mosfet->enable(true); mosfet.enable(true);
while (shouldRun) while (shouldRun) {
{
// start with a duty cycle of 0.0 (off) and increment to 1.0 (on) // start with a duty cycle of 0.0 (off) and increment to 1.0 (on)
for (float i=0.0; i <= 1.0; i+=0.1) for (float i = 0.0; i <= 1.0; i += 0.1) {
{ mosfet.setDutyCycle(i);
mosfet->setDutyCycle(i); upm_delay_us(100000);
usleep(100000);
} }
sleep(1); upm_delay(1);
// Now take it back down // Now take it back down
// start with a duty cycle of 1.0 (on) and decrement to 0.0 (off) // start with a duty cycle of 1.0 (on) and decrement to 0.0 (off)
for (float i=1.0; i >= 0.0; i-=0.1) for (float i = 1.0; i >= 0.0; i -= 0.1) {
{ mosfet.setDutyCycle(i);
mosfet->setDutyCycle(i); upm_delay_us(100000);
usleep(100000);
} }
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete mosfet;
return 0; return 0;
} }

View File

@ -22,48 +22,45 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <unistd.h>
#include <signal.h> #include <signal.h>
#include "collision.hpp" #include "collision.hpp"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the Collision Sensor // The was tested with the Collision Sensor
// Instantiate a Collision on digital pin D2 // Instantiate a Collision on digital pin D2
upm::Collision* collision = new upm::Collision(2); upm::Collision collision(2);
bool collisionState = false; bool collisionState = false;
cout << "No collision" << endl; cout << "No collision" << endl;
while (shouldRun) while (shouldRun) {
{ if (collision.isColliding() && !collisionState) {
if (collision->isColliding() && !collisionState) cout << "Collision!" << endl;
{ collisionState = true;
cout << "Collision!" << endl; } else if (collisionState) {
collisionState = true; cout << "No collision" << endl;
} collisionState = false;
else if (collisionState) }
{ }
cout << "No collision" << endl;
collisionState = false;
}
}
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete collision; return 0;
return 0;
} }

View File

@ -24,58 +24,49 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream>
#include "curieimu.hpp" #include "curieimu.hpp"
#include "mraa.h" #include "mraa.h"
#include "mraa/firmata.h" #include "mraa/firmata.h"
#include <iostream>
#include <math.h> #include <math.h>
#include <unistd.h>
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
mraa_init(); mraa_init();
mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0"); mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0");
upm::CurieImu* sensor = new upm::CurieImu(); upm::CurieImu sensor;
std::cout << "temperature is: " << (sensor->getTemperature() * pow(0.5, 9) + 23) << std::endl; std::cout << "temperature is: " << (sensor.getTemperature() * pow(0.5, 9) + 23) << std::endl;
sensor->updateAccel(); sensor.updateAccel();
int x = sensor->getAccelX(), int x = sensor.getAccelX(), y = sensor.getAccelY(), z = sensor.getAccelZ();
y = sensor->getAccelY(),
z = sensor->getAccelZ();
printf("accelerometer is: %d, %d, %d\n", x, y, z); printf("accelerometer is: %d, %d, %d\n", x, y, z);
sensor->updateGyro(); sensor.updateGyro();
int a = sensor->getGyroX(), int a = sensor.getGyroX(), b = sensor.getGyroY(), c = sensor.getGyroZ();
b = sensor->getGyroY(),
c = sensor->getGyroZ();
printf("gyroscope is: %d, %d, %d\n", a, b, c); printf("gyroscope is: %d, %d, %d\n", a, b, c);
int axis, direction; int axis, direction;
sensor->enableShockDetection(true); sensor.enableShockDetection(true);
for(int i=0; i<300; i++) { for (int i = 0; i < 300; i++) {
if (sensor->isShockDetected()) { if (sensor.isShockDetected()) {
sensor->getNextShock(); sensor.getNextShock();
axis = sensor->getAxis(); axis = sensor.getAxis();
direction = sensor->getDirection(); direction = sensor.getDirection();
printf("shock data is: %d, %d\n", axis, direction); printf("shock data is: %d, %d\n", axis, direction);
} }
usleep(10000); upm_delay_us(10000);
} }
sensor->updateMotion(); sensor.updateMotion();
int m = sensor->getAccelX(), int m = sensor.getAccelX(), n = sensor.getAccelY(), o = sensor.getAccelZ(),
n = sensor->getAccelY(), p = sensor.getGyroX(), q = sensor.getGyroY(), r = sensor.getGyroZ();
o = sensor->getAccelZ(),
p = sensor->getGyroX(),
q = sensor->getGyroY(),
r = sensor->getGyroZ();
printf("motion is: %d, %d, %d, %d, %d, %d\n", m, n, o, p, q, r); printf("motion is: %d, %d, %d, %d, %d, %d\n", m, n, o, p, q, r);
delete sensor;
//! [Interesting] //! [Interesting]
return 0; return 0;

View File

@ -22,61 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "cwlsxxa.hpp" #include "cwlsxxa.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
cout << "Initializing..." << endl; cout << "Initializing..." << endl;
// Instantiate an CWLSXXA instance, using A0 for CO2, A1 for // Instantiate an CWLSXXA instance, using A0 for CO2, A1 for
// humidity and A2 for temperature // humidity and A2 for temperature
upm::CWLSXXA *sensor = new upm::CWLSXXA(0, 1, 2); upm::CWLSXXA sensor(0, 1, 2);
// update and print available values every second // update and print available values every second
while (shouldRun) while (shouldRun) {
{ // update our values from the sensor
// update our values from the sensor sensor.update();
sensor->update();
// we show both C and F for temperature // we show both C and F for temperature
cout << "Temperature: " << sensor->getTemperature() cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true)
<< " C / " << sensor->getTemperature(true) << " F" << " F" << endl;
<< endl;
cout << "Humidity: " << sensor->getHumidity() cout << "Humidity: " << sensor.getHumidity() << " %" << endl;
<< " %" << endl;
cout << "CO2: " << sensor->getCO2() cout << "CO2: " << sensor.getCO2() << " ppm" << endl;
<< " ppm" << endl;
cout << endl; cout << endl;
sleep(1); upm_delay(1);
} }
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; //! [Interesting]
//! [Interesting] return 0;
return 0;
} }

View File

@ -22,60 +22,53 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "dfrec.hpp" #include "dfrec.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a DFRobot EC sensor on analog pin A0, with a ds18b20 // Instantiate a DFRobot EC sensor on analog pin A0, with a ds18b20
// temperature sensor connected to UART 0, and a device index (for // temperature sensor connected to UART 0, and a device index (for
// the ds1820b uart bus) of 0, and an analog reference voltage of // the ds1820b uart bus) of 0, and an analog reference voltage of
// 5.0. // 5.0.
upm::DFREC *sensor = new upm::DFREC(0, 0, 0, 5.0); upm::DFREC sensor(0, 0, 0, 5.0);
// Every 2 seconds, update and print values // Every 2 seconds, update and print values
while (shouldRun) while (shouldRun) {
{ sensor.update();
sensor->update();
cout << "EC = " cout << "EC = " << sensor.getEC() << " ms/cm" << endl;
<< sensor->getEC()
<< " ms/cm"
<< endl;
cout << "Volts = " cout << "Volts = " << sensor.getVolts() << ", Temperature = " << sensor.getTemperature()
<< sensor->getVolts() << " C" << endl;
<< ", Temperature = "
<< sensor->getTemperature()
<< " C"
<< endl;
cout << endl; cout << endl;
sleep(2); upm_delay(2);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,66 +22,63 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "dfrorp.hpp" #include "dfrorp.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a DFRobot ORP sensor on analog pin A0 with an analog // Instantiate a DFRobot ORP sensor on analog pin A0 with an analog
// reference voltage of 5.0. // reference voltage of 5.0.
upm::DFRORP *sensor = new upm::DFRORP(0, 5.0); upm::DFRORP sensor(0, 5.0);
// To calibrate: // To calibrate:
// //
// Disconnect the sensor probe (but leave the sensor interface board // Disconnect the sensor probe (but leave the sensor interface board
// connected). Then run one of the examples while holding down the // connected). Then run one of the examples while holding down the
// 'calibrate' button on the device. Read the ORP value reported // 'calibrate' button on the device. Read the ORP value reported
// (it should be fairly small). // (it should be fairly small).
// //
// This value is what you should supply to setCalibrationOffset(). // This value is what you should supply to setCalibrationOffset().
// Then reconnect the probe to the interface board and you should be // Then reconnect the probe to the interface board and you should be
// ready to go. // ready to go.
// //
// DO NOT press the calibrate button on the interface board while // DO NOT press the calibrate button on the interface board while
// the probe is attached or you can permanently damage the probe. // the probe is attached or you can permanently damage the probe.
sensor->setCalibrationOffset(0.97); sensor.setCalibrationOffset(0.97);
// Every second, update and print values // Every second, update and print values
while (shouldRun) while (shouldRun) {
{ sensor.update();
sensor->update();
cout << "ORP: " cout << "ORP: " << sensor.getORP() << " mV" << endl;
<< sensor->getORP()
<< " mV"
<< endl;
cout << endl; cout << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,55 +22,55 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "dfrph.hpp" #include "dfrph.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
#define DFRPH_AREF 5.0 #define DFRPH_AREF 5.0
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a DFRPH sensor on analog pin A0, with an analog // Instantiate a DFRPH sensor on analog pin A0, with an analog
// reference voltage of DFRPH_AREF // reference voltage of DFRPH_AREF
upm::DFRPH *sensor = new upm::DFRPH(0, DFRPH_AREF); upm::DFRPH sensor(0, DFRPH_AREF);
// After calibration, set the offset (based on calibration with a pH // After calibration, set the offset (based on calibration with a pH
// 7.0 buffer solution). See the UPM sensor documentation for // 7.0 buffer solution). See the UPM sensor documentation for
// calibrations instructions. // calibrations instructions.
sensor->setOffset(0.065); sensor.setOffset(0.065);
// Every second, sample the pH and output it's corresponding // Every second, sample the pH and output it's corresponding
// analog voltage. // analog voltage.
while (shouldRun) while (shouldRun) {
{ cout << "Detected volts: " << sensor.volts() << endl;
cout << "Detected volts: " << sensor->volts() << endl; cout << "pH value: " << sensor.pH() << endl;
cout << "pH value: " << sensor->pH() << endl; cout << endl;
cout << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }

View File

@ -22,56 +22,54 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <string>
#include "ds1307.hpp" #include "ds1307.hpp"
using namespace std; using namespace std;
void printTime(upm::DS1307 *rtc) void
printTime(upm::DS1307& rtc)
{ {
cout << "The time is: " << cout << "The time is: " << rtc.month << "/" << rtc.dayOfMonth << "/" << rtc.year << " "
rtc->month << "/" << rtc->dayOfMonth << "/" << rtc->year << " " << rtc.hours << ":" << rtc.minutes << ":" << rtc.seconds;
<< rtc->hours << ":" << rtc->minutes << ":" << rtc->seconds;
if (rtc->amPmMode) if (rtc.amPmMode)
cout << ((rtc->pm) ? " PM " : " AM "); cout << ((rtc.pm) ? " PM " : " AM ");
cout << endl; cout << endl;
cout << "Clock is in " << ((rtc->amPmMode) ? "AM/PM mode" : "24hr mode") cout << "Clock is in " << ((rtc.amPmMode) ? "AM/PM mode" : "24hr mode") << endl;
<< endl;
} }
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a DS1037 on I2C bus 0 // Instantiate a DS1037 on I2C bus 0
upm::DS1307 *rtc = new upm::DS1307(0); upm::DS1307 rtc(0);
// always do this first // always do this first
cout << "Loading the current time... " << endl; cout << "Loading the current time... " << endl;
if (!rtc->loadTime()) if (!rtc.loadTime()) {
{ cerr << "rtc.loadTime() failed." << endl;
cerr << "rtc->loadTime() failed." << endl; return 0;
return 0;
} }
printTime(rtc);
// set the year as an example printTime(rtc);
cout << "setting the year to 50" << endl;
rtc->year = 50;
rtc->setTime(); // set the year as an example
cout << "setting the year to 50" << endl;
rtc.year = 50;
// reload the time and print it rtc.setTime();
rtc->loadTime();
printTime(rtc);
//! [Interesting] // reload the time and print it
rtc.loadTime();
delete rtc; printTime(rtc);
return 0;
//! [Interesting]
return 0;
} }

View File

@ -1,43 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream> #include <iostream>
#include <stdlib.h>
#include <string> #include <string>
#include "ds1808lc.hpp" #include "ds1808lc.hpp"
#define EDISON_I2C_BUS 1 // Edison I2C-1 #define EDISON_I2C_BUS 1 // Edison I2C-1
#define DS1808_GPIO_PWR 15 // Edison GP165 #define DS1808_GPIO_PWR 15 // Edison GP165
void printState(upm::ILightController &lightController) void
printState(upm::ILightController& lightController)
{ {
if (lightController.isPowered()) if (lightController.isPowered()) {
{ std::cout << "Light is powered, brightness = " << lightController.getBrightness()
std::cout << "Light is powered, brightness = " << lightController.getBrightness() << std::endl; << std::endl;
} } else {
else
{
std::cout << "Light is not powered." << std::endl; std::cout << "Light is not powered." << std::endl;
} }
} }
int main( int argc, char **argv ) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
upm::DS1808LC lightController(DS1808_GPIO_PWR, EDISON_I2C_BUS); upm::DS1808LC lightController(DS1808_GPIO_PWR, EDISON_I2C_BUS);
std::cout << "Existing state: "; printState(lightController); std::cout << "Existing state: ";
if (argc == 2) printState(lightController);
{ if (argc == 2) {
std::string arg = argv[1]; std::string arg = argv[1];
int brightness = ::atoi(argv[1]); int brightness = ::atoi(argv[1]);
if (brightness > 0) if (brightness > 0) {
{
lightController.setPowerOn(); lightController.setPowerOn();
lightController.setBrightness(brightness); lightController.setBrightness(brightness);
} } else
else
lightController.setPowerOff(); lightController.setPowerOff();
} }
std::cout << "Now: ";printState(lightController); std::cout << "Now: ";
printState(lightController);
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,65 +22,61 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "ds18b20.hpp" #include "ds18b20.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
cout << "Initializing..." << endl; cout << "Initializing..." << endl;
// Instantiate an DS18B20 instance using the default values (uart 0) // Instantiate an DS18B20 instance using the default values (uart 0)
upm::DS18B20 sensor; upm::DS18B20 sensor;
cout << "Found " << sensor.devicesFound() << " device(s)" << endl; cout << "Found " << sensor.devicesFound() << " device(s)" << endl;
cout << endl; cout << endl;
// bail if we didn't find anything // bail if we didn't find anything
if (!sensor.devicesFound()) if (!sensor.devicesFound())
return 1; return 1;
// update and print available values every 2 seconds // update and print available values every 2 seconds
while (shouldRun) while (shouldRun) {
{ // update our values for all of the detected sensors
// update our values for all of the detected sensors sensor.update(-1);
sensor.update(-1);
// we show both C and F for temperature for the sensors // we show both C and F for temperature for the sensors
int i; int i;
for (i=0; i<sensor.devicesFound(); i++) for (i = 0; i < sensor.devicesFound(); i++) {
{ cout << "Device " << i << ": Temperature: " << sensor.getTemperature(i) << " C / "
cout << "Device " << sensor.getTemperature(i, true) << " F" << endl;
<< i }
<< ": Temperature: " cout << endl;
<< sensor.getTemperature(i)
<< " C / " << sensor.getTemperature(i, true) << " F"
<< endl;
}
cout << endl;
sleep(2); upm_delay(2);
} }
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
//! [Interesting] //! [Interesting]
return 0; return 0;
} }

View File

@ -22,38 +22,37 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <stdio.h>
#include "ds2413.hpp" #include "ds2413.hpp"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a DS2413 Module on a Dallas 1-wire bus connected to UART 0 // Instantiate a DS2413 Module on a Dallas 1-wire bus connected to UART 0
upm::DS2413* sensor = new upm::DS2413(0); upm::DS2413 sensor(0);
// find all of the DS2413 devices present on the bus // find all of the DS2413 devices present on the bus
sensor->init(); sensor.init();
// how many devices were found? // how many devices were found?
cout << "Found "<< sensor->devicesFound() << " device(s)" << endl; cout << "Found " << sensor.devicesFound() << " device(s)" << endl;
// read the gpio and latch values from the first device // read the gpio and latch values from the first device
// the lower 4 bits are of the form: // the lower 4 bits are of the form:
// <gpioB latch> <gpioB value> <gpioA latch> <gpioA value> // <gpioB latch> <gpioB value> <gpioA latch> <gpioA value>
cout << "GPIO device 0 values: " << sensor->readGpios(0) << endl; cout << "GPIO device 0 values: " << sensor.readGpios(0) << endl;
// set the gpio latch values of the first device // set the gpio latch values of the first device
cout << "Setting GPIO latches to on" << endl; cout << "Setting GPIO latches to on" << endl;
sensor->writeGpios(0, 0x03); sensor.writeGpios(0, 0x03);
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; //! [Interesting]
//! [Interesting] return 0;
return 0;
} }

View File

@ -22,91 +22,84 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <string>
#include "e50hx.hpp" #include "e50hx.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// You will need to edit this example to conform to your site and your // You will need to edit this example to conform to your site and your
// devices, specifically the Device Object Instance ID passed to the // devices, specifically the Device Object Instance ID passed to the
// constructor, and the arguments to initMaster() that are // constructor, and the arguments to initMaster() that are
// appropriate for your BACnet network. // appropriate for your BACnet network.
string defaultDev = "/dev/ttyUSB0"; string defaultDev = "/dev/ttyUSB0";
// if an argument was specified, use it as the device instead // if an argument was specified, use it as the device instead
if (argc > 1) if (argc > 1)
defaultDev = string(argv[1]); defaultDev = string(argv[1]);
cout << "Using device " << defaultDev << endl; cout << "Using device " << defaultDev << endl;
cout << "Initializing..." << endl; cout << "Initializing..." << endl;
// Instantiate an E50HX object for an E50HX device that has 1075425 // Instantiate an E50HX object for an E50HX device that has 1075425
// as it's unique Device Object Instance ID. NOTE: You will // as it's unique Device Object Instance ID. NOTE: You will
// certainly want to change this to the correct value for your // certainly want to change this to the correct value for your
// device(s). // device(s).
E50HX *sensor = new E50HX(1075425); E50HX sensor(1075425);
// Initialize our BACnet master, if it has not already been // Initialize our BACnet master, if it has not already been
// initialized, with the device and baudrate, choosing 1000001 as // initialized, with the device and baudrate, choosing 1000001 as
// our unique Device Object Instance ID, 2 as our MAC address and // our unique Device Object Instance ID, 2 as our MAC address and
// using default values for maxMaster and maxInfoFrames // using default values for maxMaster and maxInfoFrames
sensor->initMaster(defaultDev, 38400, 1000001, 2); sensor.initMaster(defaultDev, 38400, 1000001, 2);
// Uncomment to enable debugging output // Uncomment to enable debugging output
// sensor->setDebug(true); // sensor.setDebug(true);
cout << endl; cout << endl;
cout << "Device Description: " << sensor->getDeviceDescription() << endl; cout << "Device Description: " << sensor.getDeviceDescription() << endl;
cout << "Device Location: " << sensor->getDeviceLocation() << endl; cout << "Device Location: " << sensor.getDeviceLocation() << endl;
cout << endl; cout << endl;
// update and print a few values every 5 seconds // update and print a few values every 5 seconds
while (shouldRun) while (shouldRun) {
{ cout << "System Voltage: " << sensor.getAnalogValue(E50HX::AV_System_Voltage) << " "
cout << "System Voltage: " << sensor.getAnalogValueUnits(E50HX::AV_System_Voltage) << endl;
<< sensor->getAnalogValue(E50HX::AV_System_Voltage)
<< " " << sensor->getAnalogValueUnits(E50HX::AV_System_Voltage)
<< endl;
cout << "System Type: " cout << "System Type: " << sensor.getAnalogValue(E50HX::AV_System_Type) << endl;
<< sensor->getAnalogValue(E50HX::AV_System_Type)
<< endl;
cout << "Energy Consumption: " << sensor->getAnalogInput(E50HX::AI_Energy) cout << "Energy Consumption: " << sensor.getAnalogInput(E50HX::AI_Energy) << " "
<< " " << sensor->getAnalogInputUnits(E50HX::AI_Energy) << sensor.getAnalogInputUnits(E50HX::AI_Energy) << endl;
<< endl;
cout << "Power Up Counter: " cout << "Power Up Counter: " << sensor.getAnalogInput(E50HX::AI_Power_Up_Count) << endl;
<< sensor->getAnalogInput(E50HX::AI_Power_Up_Count)
<< endl;
cout << endl; cout << endl;
sleep(5); upm_delay(5);
} }
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; //! [Interesting]
//! [Interesting] return 0;
return 0;
} }

View File

@ -22,51 +22,44 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <upm_utilities.h>
#include <ecezo.hpp> #include <ecezo.hpp>
#include <iostream>
#include <signal.h>
#include <upm_utilities.h>
using namespace std; using namespace std;
using namespace upm; using namespace upm;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a ECEZO sensor on uart 0 at 9600 baud. // Instantiate a ECEZO sensor on uart 0 at 9600 baud.
upm::ECEZO *sensor = new upm::ECEZO(0, 9600, false); upm::ECEZO sensor(0, 9600, false);
// For I2C, assuming the device is configured for address 0x64 on // For I2C, assuming the device is configured for address 0x64 on
// I2C bus 0, you could use something like: // I2C bus 0, you could use something like:
// //
// upm::ECEZO *sensor = new upm::ECEZO(0, 0x64, true); // upm::ECEZO sensor(0, 0x64, true);
while (shouldRun) while (shouldRun) {
{
// this will take about 1 second to complete // this will take about 1 second to complete
sensor->update(); sensor.update();
cout << "EC " cout << "EC " << sensor.getEC() << " uS/cm, TDS " << sensor.getTDS() << " mg/L, Salinity "
<< sensor->getEC() << sensor.getSalinity() << " PSS-78, SG " << sensor.getSG() << endl;
<< " uS/cm, TDS "
<< sensor->getTDS()
<< " mg/L, Salinity "
<< sensor->getSalinity()
<< " PSS-78, SG "
<< sensor->getSG()
<< endl;
upm_delay(5); upm_delay(5);
} }
@ -75,7 +68,5 @@ int main()
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor;
return 0; return 0;
} }

View File

@ -22,14 +22,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdio.h>
#include "ecs1030.hpp" #include "ecs1030.hpp"
int is_running = 0; int is_running = 0;
upm::ECS1030 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
@ -42,20 +41,20 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
sensor = new upm::ECS1030(0); upm::ECS1030 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
while (!is_running) { while (!is_running) {
std::cout << "I = " << sensor->getCurrency_A () << ", Power = " << sensor->getPower_A () << std::endl; std::cout << "I = " << sensor.getCurrency_A() << ", Power = " << sensor.getPower_A()
std::cout << "I = " << sensor->getCurrency_B () << ", Power = " << sensor->getPower_B () << std::endl; << std::endl;
std::cout << "I = " << sensor.getCurrency_B() << ", Power = " << sensor.getPower_B()
<< std::endl;
} }
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,57 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "ehr.hpp" #include "ehr.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main() main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Ear-clip Heart Rate sensor on digital pin D2 // Instantiate a Ear-clip Heart Rate sensor on digital pin D2
upm::EHR* heart = new upm::EHR(2); upm::EHR heart(2);
// set the beat counter to 0, init the clock and start counting beats
heart->clearBeatCounter();
heart->initClock();
heart->startBeatCounter();
while (shouldRun) // set the beat counter to 0, init the clock and start counting beats
{ heart.clearBeatCounter();
// we grab these just for display purposes in this example heart.initClock();
uint32_t millis = heart->getMillis(); heart.startBeatCounter();
uint32_t beats = heart->beatCounter();
// heartRate() requires that at least 5 seconds pass before while (shouldRun) {
// returning anything other than 0 // we grab these just for display purposes in this example
int hr = heart->heartRate(); uint32_t millis = heart.getMillis();
uint32_t beats = heart.beatCounter();
// output milliseconds passed, beat count, and computed heart rate // heartRate() requires that at least 5 seconds pass before
cout << "Millis: " << millis << " Beats: " << beats; // returning anything other than 0
cout << " Heart Rate: " << hr << endl; int hr = heart.heartRate();
sleep(1); // output milliseconds passed, beat count, and computed heart rate
cout << "Millis: " << millis << " Beats: " << beats;
cout << " Heart Rate: " << hr << endl;
upm_delay(1);
} }
heart->stopBeatCounter(); heart.stopBeatCounter();
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete heart; return 0;
return 0; }
}

View File

@ -22,45 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <unistd.h>
#include <signal.h> #include <signal.h>
#include "eldriver.hpp" #include "eldriver.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the El Driver Module // This was tested with the El Driver Module
// Instantiate a El Driver on digital pin D2 // Instantiate an El Driver on digital pin D2
upm::ElDriver* eldriver = new upm::ElDriver(2); upm::ElDriver eldriver(2);
bool lightState = true; bool lightState = true;
while (shouldRun) while (shouldRun) {
{ if (lightState)
if (lightState) eldriver.on();
eldriver->on(); else
else eldriver.off();
eldriver->off(); lightState = !lightState;
lightState = !lightState; upm_delay(1);
sleep(1); }
}
//! [Interesting] //! [Interesting]
eldriver->off(); eldriver.off();
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete eldriver; return 0;
return 0;
} }

View File

@ -22,58 +22,60 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <time.h>
#include <signal.h> #include <signal.h>
#include <sys/time.h>
#include <time.h>
#include "electromagnet.hpp" #include "electromagnet.hpp"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
float get_time() float
get_time()
{ {
return ((float)(clock()))/CLOCKS_PER_SEC; return ((float) (clock())) / CLOCKS_PER_SEC;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the Electromagnetic Module // The was tested with the Electromagnetic Module
// Instantiate a Electromagnet on digital pin D2 // Instantiate a Electromagnet on digital pin D2
upm::Electromagnet* magnet = new upm::Electromagnet(2); upm::Electromagnet magnet(2);
cout << "Starting up magnet...." << endl; cout << "Starting up magnet...." << endl;
magnet->off(); magnet.off();
bool magnetState = false; bool magnetState = false;
float time_passed = get_time(); float time_passed = get_time();
// Turn magnet on and off every 5 seconds // Turn magnet on and off every 5 seconds
while (shouldRun) while (shouldRun) {
{ if ((get_time() - time_passed) > 5.0) {
if ((get_time() - time_passed) > 5.0) magnetState = !magnetState;
{ if (magnetState)
magnetState = !magnetState; magnet.on();
if (magnetState) else
magnet->on(); magnet.off();
else cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl;
magnet->off(); time_passed = get_time();
cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; }
time_passed = get_time(); }
}
}
//! [Interesting] //! [Interesting]
magnet->off(); magnet.off();
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete magnet; return 0;
return 0;
} }

View File

@ -21,40 +21,41 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "emg.hpp" #include "emg.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the EMG Muscle Signal Reader Sensor Module // The was tested with the EMG Muscle Signal Reader Sensor Module
// Instantiate a EMG on analog pin A0 // Instantiate a EMG on analog pin A0
upm::EMG *emg = new upm::EMG(0); upm::EMG emg(0);
cout << "Calibrating...." << endl; cout << "Calibrating...." << endl;
emg->calibrate(); emg.calibrate();
while (shouldRun)
{
cout << emg->value() << endl;
usleep(100000);
}
//! [Interesting] while (shouldRun) {
cout << "Exiting" << endl; cout << emg.value() << endl;
delete emg; upm_delay_us(100000);
return 0; }
//! [Interesting]
cout << "Exiting" << endl;
return 0;
} }

View File

@ -22,10 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "enc03r.hpp" #include "enc03r.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
@ -34,47 +35,43 @@ bool shouldRun = true;
// analog voltage, usually 3.3 or 5.0 // analog voltage, usually 3.3 or 5.0
#define CALIBRATION_SAMPLES 1000 #define CALIBRATION_SAMPLES 1000
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a ENC03R on analog pin A0 // Instantiate a ENC03R on analog pin A0
upm::ENC03R *gyro = new upm::ENC03R(0); upm::ENC03R gyro(0);
// The first thing we need to do is calibrate the sensor. // The first thing we need to do is calibrate the sensor.
cout << "Please place the sensor in a stable location, and do not" << endl; cout << "Please place the sensor in a stable location, and do not" << endl;
cout << "move it while calibration takes place." << endl; cout << "move it while calibration takes place." << endl;
cout << "This may take a couple of minutes." << endl; cout << "This may take a couple of minutes." << endl;
gyro->calibrate(CALIBRATION_SAMPLES); gyro.calibrate(CALIBRATION_SAMPLES);
cout << "Calibration complete. Reference value: " cout << "Calibration complete. Reference value: " << gyro.calibrationValue() << endl;
<< gyro->calibrationValue() << endl;
// Read the input and print both the raw value and the angular velocity, // Read the input and print both the raw value and the angular velocity,
// waiting 0.1 seconds between readings // waiting 0.1 seconds between readings
while (shouldRun) while (shouldRun) {
{ gyro.update();
gyro->update();
cout << "Angular velocity: " cout << "Angular velocity: " << gyro.angularVelocity() << " deg/s" << endl;
<< gyro->angularVelocity()
<< " deg/s"
<< endl;
usleep(100000); upm_delay_us(100000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete gyro;
return 0; return 0;
} }

View File

@ -22,45 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "flex.hpp" #include "flex.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with a Spectra Symbol flex sensor. // The was tested with a Spectra Symbol flex sensor.
// We attached a 22K resistor to a breadboard, // We attached a 22K resistor to a breadboard,
// with 1 end attached to GND and the other connected to // with 1 end attached to GND and the other connected to
// both the flex sensor and A0. // both the flex sensor and A0.
// The flex sensor was connected on 1 pin to the 22K resistor and A0 // The flex sensor was connected on 1 pin to the 22K resistor and A0
// and on the other pin to 5V. // and on the other pin to 5V.
// Instantiate a Flex sensor on analog pin A0 // Instantiate a Flex sensor on analog pin A0
upm::Flex *flex = new upm::Flex(0); upm::Flex flex(0);
while (shouldRun) while (shouldRun) {
{ cout << "Flex value: " << flex.value() << endl;
cout << "Flex value: " << flex->value() << endl; upm_delay(1);
sleep(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete flex; return 0;
return 0;
} }

View File

@ -22,16 +22,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq2.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h> #include <stdio.h>
#include "gas.hpp"
#include "mq2.hpp"
int is_running = 0; int is_running = 0;
uint16_t buffer [128]; uint16_t buffer[128];
upm::MQ2 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
@ -44,26 +44,26 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// Attach gas sensor to A0 // Attach gas sensor to A0
sensor = new upm::MQ2(0); upm::MQ2 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
thresholdContext ctx; thresholdContext ctx;
ctx.averageReading = 0; ctx.averageReading = 0;
ctx.runningAverage = 0; ctx.runningAverage = 0;
ctx.averagedOver = 2; ctx.averagedOver = 2;
// Infinite loop, ends when script is cancelled // Infinite loop, ends when script is cancelled
// Repeatedly, take a sample every 2 milliseconds; // Repeatedly, take a sample every 2 milliseconds;
// find the average of 128 samples; and // find the average of 128 samples; and
// print a running graph of the averages using a resolution of 5 // print a running graph of the averages using a resolution of 5
while (!is_running) { while (!is_running) {
int len = sensor->getSampledWindow (2, 128, buffer); int len = sensor.getSampledWindow(2, 128, buffer);
if (len) { if (len) {
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
sensor->printGraph(&ctx, 5); sensor.printGraph(&ctx, 5);
if (thresh) { if (thresh) {
// do something .... // do something ....
} }
@ -72,8 +72,6 @@ main(int argc, char **argv)
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,16 +22,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq3.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h> #include <stdio.h>
#include "gas.hpp"
#include "mq3.hpp"
int is_running = 0; int is_running = 0;
uint16_t buffer [128]; uint16_t buffer[128];
upm::MQ3 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
@ -44,26 +44,26 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// Attach gas sensor to A0 // Attach gas sensor to A0
sensor = new upm::MQ3(0); upm::MQ3 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
thresholdContext ctx; thresholdContext ctx;
ctx.averageReading = 0; ctx.averageReading = 0;
ctx.runningAverage = 0; ctx.runningAverage = 0;
ctx.averagedOver = 2; ctx.averagedOver = 2;
// Infinite loop, ends when script is cancelled // Infinite loop, ends when script is cancelled
// Repeatedly, take a sample every 2 milliseconds; // Repeatedly, take a sample every 2 milliseconds;
// find the average of 128 samples; and // find the average of 128 samples; and
// print a running graph of the averages using a resolution of 5 // print a running graph of the averages using a resolution of 5
while (!is_running) { while (!is_running) {
int len = sensor->getSampledWindow (2, 128, buffer); int len = sensor.getSampledWindow(2, 128, buffer);
if (len) { if (len) {
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
sensor->printGraph(&ctx, 5); sensor.printGraph(&ctx, 5);
if (thresh) { if (thresh) {
// do something .... // do something ....
} }
@ -72,8 +72,6 @@ main(int argc, char **argv)
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,12 +22,12 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq4.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h>
#include "gas.hpp"
#include "mq4.hpp"
bool shouldRun = true; bool shouldRun = true;
@ -36,46 +36,43 @@ using namespace std;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT) {
{ shouldRun = false;
shouldRun = false;
} }
} }
//! [Interesting] //! [Interesting]
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
// Attach gas sensor to Analog A0 // Attach gas sensor to Analog A0
upm::MQ4 *sensor = new upm::MQ4(0); upm::MQ4 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
uint16_t buffer [128];
thresholdContext ctx; uint16_t buffer[128];
ctx.averageReading = 0;
ctx.runningAverage = 0; thresholdContext ctx;
ctx.averagedOver = 2; ctx.averageReading = 0;
ctx.runningAverage = 0;
// Infinite loop, ends when script is cancelled ctx.averagedOver = 2;
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and // Infinite loop, ends when script is cancelled
// print a running graph of asteriskss as averages // Repeatedly, take a sample every 2 microseconds;
while (shouldRun) // find the average of 128 samples; and
{ // print a running graph of asteriskss as averages
int len = sensor->getSampledWindow (2, 128, buffer); while (shouldRun) {
if (len) { int len = sensor.getSampledWindow(2, 128, buffer);
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); if (len) {
sensor->printGraph(&ctx, 5); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
if (thresh) { sensor.printGraph(&ctx, 5);
// do something .... if (thresh) {
// do something ....
}
} }
}
} }
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,16 +22,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq5.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h> #include <stdio.h>
#include "gas.hpp"
#include "mq5.hpp"
int is_running = 0; int is_running = 0;
uint16_t buffer [128]; uint16_t buffer[128];
upm::MQ5 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
@ -44,21 +44,21 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
sensor = new upm::MQ5(0); upm::MQ5 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
thresholdContext ctx; thresholdContext ctx;
ctx.averageReading = 0; ctx.averageReading = 0;
ctx.runningAverage = 0; ctx.runningAverage = 0;
ctx.averagedOver = 2; ctx.averagedOver = 2;
while (!is_running) { while (!is_running) {
int len = sensor->getSampledWindow (2, 128, buffer); int len = sensor.getSampledWindow(2, 128, buffer);
if (len) { if (len) {
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
sensor->printGraph(&ctx, 7); sensor.printGraph(&ctx, 7);
if (thresh) { if (thresh) {
// do something .... // do something ....
} }
@ -67,8 +67,6 @@ main(int argc, char **argv)
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,12 +22,12 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq6.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h>
#include "gas.hpp"
#include "mq6.hpp"
bool shouldRun = true; bool shouldRun = true;
@ -36,46 +36,43 @@ using namespace std;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT) {
{ shouldRun = false;
shouldRun = false;
} }
} }
//! [Interesting] //! [Interesting]
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
// Attach gas sensor to Analog A0 // Attach gas sensor to Analog A0
upm::MQ6 *sensor = new upm::MQ6(0); upm::MQ6 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
uint16_t buffer [128];
thresholdContext ctx; uint16_t buffer[128];
ctx.averageReading = 0;
ctx.runningAverage = 0; thresholdContext ctx;
ctx.averagedOver = 2; ctx.averageReading = 0;
ctx.runningAverage = 0;
// Infinite loop, ends when script is cancelled ctx.averagedOver = 2;
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and // Infinite loop, ends when script is cancelled
// print a running graph of asteriskss as averages // Repeatedly, take a sample every 2 microseconds;
while (shouldRun) // find the average of 128 samples; and
{ // print a running graph of asteriskss as averages
int len = sensor->getSampledWindow (2, 128, buffer); while (shouldRun) {
if (len) { int len = sensor.getSampledWindow(2, 128, buffer);
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); if (len) {
sensor->printGraph(&ctx, 5); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
if (thresh) { sensor.printGraph(&ctx, 5);
// do something .... if (thresh) {
// do something ....
}
} }
}
} }
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,12 +22,12 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq7.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h>
#include "gas.hpp"
#include "mq7.hpp"
bool shouldRun = true; bool shouldRun = true;
@ -36,46 +36,43 @@ using namespace std;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT) {
{ shouldRun = false;
shouldRun = false;
} }
} }
//! [Interesting] //! [Interesting]
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
// Attach gas sensor to Analog A0 // Attach gas sensor to Analog A0
upm::MQ7 *sensor = new upm::MQ7(0); upm::MQ7 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
uint16_t buffer [128];
thresholdContext ctx; uint16_t buffer[128];
ctx.averageReading = 0;
ctx.runningAverage = 0; thresholdContext ctx;
ctx.averagedOver = 2; ctx.averageReading = 0;
ctx.runningAverage = 0;
// Infinite loop, ends when script is cancelled ctx.averagedOver = 2;
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and // Infinite loop, ends when script is cancelled
// print a running graph of asteriskss as averages // Repeatedly, take a sample every 2 microseconds;
while (shouldRun) // find the average of 128 samples; and
{ // print a running graph of asteriskss as averages
int len = sensor->getSampledWindow (2, 128, buffer); while (shouldRun) {
if (len) { int len = sensor.getSampledWindow(2, 128, buffer);
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); if (len) {
sensor->printGraph(&ctx, 5); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
if (thresh) { sensor.printGraph(&ctx, 5);
// do something .... if (thresh) {
// do something ....
}
} }
}
} }
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,12 +22,12 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq8.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h>
#include "gas.hpp"
#include "mq8.hpp"
bool shouldRun = true; bool shouldRun = true;
@ -36,46 +36,43 @@ using namespace std;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT) {
{ shouldRun = false;
shouldRun = false;
} }
} }
//! [Interesting] //! [Interesting]
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
// Attach gas sensor to Analog A0 // Attach gas sensor to Analog A0
upm::MQ8 *sensor = new upm::MQ8(0); upm::MQ8 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
uint16_t buffer [128];
thresholdContext ctx; uint16_t buffer[128];
ctx.averageReading = 0;
ctx.runningAverage = 0; thresholdContext ctx;
ctx.averagedOver = 2; ctx.averageReading = 0;
ctx.runningAverage = 0;
// Infinite loop, ends when script is cancelled ctx.averagedOver = 2;
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and // Infinite loop, ends when script is cancelled
// print a running graph of asteriskss as averages // Repeatedly, take a sample every 2 microseconds;
while (shouldRun) // find the average of 128 samples; and
{ // print a running graph of asteriskss as averages
int len = sensor->getSampledWindow (2, 128, buffer); while (shouldRun) {
if (len) { int len = sensor.getSampledWindow(2, 128, buffer);
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); if (len) {
sensor->printGraph(&ctx, 5); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
if (thresh) { sensor.printGraph(&ctx, 5);
// do something .... if (thresh) {
// do something ....
}
} }
}
} }
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete sensor; return 0;
return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,16 +22,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "mq9.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdint.h>
#include <sys/time.h> #include <stdio.h>
#include "gas.hpp"
#include "mq9.hpp"
int is_running = 0; int is_running = 0;
uint16_t buffer [128]; uint16_t buffer[128];
upm::MQ9 *sensor = NULL;
void void
sig_handler(int signo) sig_handler(int signo)
@ -44,21 +44,21 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
sensor = new upm::MQ9(0); upm::MQ9 sensor(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
thresholdContext ctx; thresholdContext ctx;
ctx.averageReading = 0; ctx.averageReading = 0;
ctx.runningAverage = 0; ctx.runningAverage = 0;
ctx.averagedOver = 2; ctx.averagedOver = 2;
while (!is_running) { while (!is_running) {
int len = sensor->getSampledWindow (2, 128, buffer); int len = sensor.getSampledWindow(2, 128, buffer);
if (len) { if (len) {
int thresh = sensor->findThreshold (&ctx, 30, buffer, len); int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
sensor->printGraph(&ctx, 5); sensor.printGraph(&ctx, 5);
if (thresh) { if (thresh) {
// do something .... // do something ....
} }
@ -67,8 +67,6 @@ main(int argc, char **argv)
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;
delete sensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,10 +22,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include "tp401.hpp" #include "tp401.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
@ -34,37 +37,41 @@ using namespace std;
std::string std::string
airQuality(uint16_t value) airQuality(uint16_t value)
{ {
if(value < 50) return "Fresh Air"; if (value < 50)
if(value < 200) return "Normal Indoor Air"; return "Fresh Air";
if(value < 400) return "Low Pollution"; if (value < 200)
if(value < 600) return "High Pollution - Action Recommended"; return "Normal Indoor Air";
if (value < 400)
return "Low Pollution";
if (value < 600)
return "High Pollution - Action Recommended";
return "Very High Pollution - Take Action Immediately"; return "Very High Pollution - Take Action Immediately";
} }
int main () int
main()
{ {
upm::TP401* airSensor = new upm::TP401(0); // Instantiate new grove air quality sensor on analog pin A0 upm::TP401 airSensor(0);
cout << airSensor->name() << endl; cout << airSensor.name() << endl;
fprintf(stdout, "Heating sensor for 3 minutes...\n"); fprintf(stdout, "Heating sensor for 3 minutes...\n");
// wait 3 minutes for sensor to warm up // wait 3 minutes for sensor to warm up
for(int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if(i) { if (i) {
fprintf(stdout, "Please wait, %d minute(s) passed..\n", i); fprintf(stdout, "Please wait, %d minute(s) passed..\n", i);
} }
sleep(60); upm_delay(60);
} }
fprintf(stdout, "Sensor ready!\n"); fprintf(stdout, "Sensor ready!\n");
while(true) { while (true) {
uint16_t value = airSensor->getSample(); // Read raw value uint16_t value = airSensor.getSample(); // Read raw value
float ppm = airSensor->getPPM(); // Read CO ppm (can vary slightly from previous read) float ppm = airSensor.getPPM(); // Read CO ppm (can vary slightly from previous read)
fprintf(stdout, "raw: %4d ppm: %5.2f %s\n", value, ppm, airQuality(value).c_str()); fprintf(stdout, "raw: %4d ppm: %5.2f %s\n", value, ppm, airQuality(value).c_str());
usleep(2500000); // Sleep for 2.5s upm_delay_us(2500000); // Sleep for 2.5s
} }
delete airSensor;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

View File

@ -22,52 +22,52 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "gp2y0a.hpp" #include "gp2y0a.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
// analog voltage, usually 3.3 or 5.0 // analog voltage, usually 3.3 or 5.0
#define GP2Y0A_AREF 5.0 #define GP2Y0A_AREF 5.0
#define SAMPLES_PER_QUERY 20 #define SAMPLES_PER_QUERY 20
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Note, for the Grove 80cm version of this sensor, due to the way // Note, for the Grove 80cm version of this sensor, due to the way
// it is wired, you need to plug this into the A0 port, where it // it is wired, you need to plug this into the A0 port, where it
// will use the available A1 pin for data. // will use the available A1 pin for data.
// Instantiate a GP2Y0A on analog pin A1 // Instantiate a GP2Y0A on analog pin A1
upm::GP2Y0A *volts = new upm::GP2Y0A(1); upm::GP2Y0A volts(1);
// The higher the voltage (closer to AREF) the closer the object is. NOTE: // The higher the voltage (closer to AREF) the closer the object is. NOTE:
// the measured voltage will probably not exceed 3.3 volts. // the measured voltage will probably not exceed 3.3 volts.
// Every second, print the averaged voltage value (averaged over 20 samples). // Every second, print the averaged voltage value (averaged over 20 samples).
while (shouldRun) while (shouldRun) {
{ cout << "AREF: " << GP2Y0A_AREF << ", Voltage value (higher means closer): "
cout << "AREF: " << GP2Y0A_AREF << volts.value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl;
<< ", Voltage value (higher means closer): "
<< volts->value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl; upm_delay(1);
sleep(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete volts; return 0;
return 0;
} }

View File

@ -22,90 +22,81 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include <stdio.h>
#include "gprs.hpp" #include "gprs.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
void printUsage(char *progname) void
printUsage(char* progname)
{ {
cout << "Usage: " << progname << " [AT command]" << endl; cout << "Usage: " << progname << " [AT command]" << endl;
cout << endl; cout << endl;
cout << "If an argument is supplied on the command line, that argument is" cout << "If an argument is supplied on the command line, that argument is" << endl;
<< endl; cout << "sent to the module and the response is printed out." << endl;
cout << "sent to the module and the response is printed out." << endl; cout << endl;
cout << endl; cout << "If no argument is used, then the manufacturer and the current" << endl;
cout << "If no argument is used, then the manufacturer and the current" cout << "saved profiles are queried and the results printed out." << endl;
<< endl; cout << endl;
cout << "saved profiles are queried and the results printed out." << endl; cout << endl;
cout << endl;
cout << endl;
} }
// simple helper function to send a command and wait for a response // simple helper function to send a command and wait for a response
void sendCommand(upm::GPRS* sensor, string cmd) void
sendCommand(upm::GPRS& sensor, string cmd)
{ {
// commands need to be terminated with a carriage return // commands need to be terminated with a carriage return
cmd += "\r"; cmd += "\r";
sensor->writeDataStr(cmd); sensor.writeDataStr(cmd);
// wait up to 1 second // wait up to 1 second
if (sensor->dataAvailable(1000)) if (sensor.dataAvailable(1000)) {
{ cout << "Returned: " << sensor.readDataStr(1024) << endl;
cout << "Returned: " << sensor->readDataStr(1024) << endl; } else {
} cerr << "Timed out waiting for response" << endl;
else
{
cerr << "Timed out waiting for response" << endl;
} }
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a GPRS Module on UART 0 // Instantiate a GPRS Module on UART 0
upm::GPRS* sensor = new upm::GPRS(0); upm::GPRS sensor(0);
// Set the baud rate, 19200 baud is the default. // Set the baud rate, 19200 baud is the default.
if (sensor->setBaudRate(19200) != mraa::SUCCESS) if (sensor.setBaudRate(19200) != mraa::SUCCESS) {
{ cerr << "Failed to set tty baud rate" << endl;
cerr << "Failed to set tty baud rate" << endl; return 1;
return 1;
} }
printUsage(argv[0]); printUsage(argv[0]);
if (argc > 1) if (argc > 1) {
{ cout << "Sending command line argument (" << argv[1] << ")..." << endl;
cout << "Sending command line argument (" << argv[1] << ")..." << endl; sendCommand(sensor, argv[1]);
sendCommand(sensor, argv[1]); } else {
} // query the module manufacturer
else cout << "Querying module manufacturer (AT+CGMI)..." << endl;
{ sendCommand(sensor, "AT+CGMI");
// query the module manufacturer
cout << "Querying module manufacturer (AT+CGMI)..." << endl;
sendCommand(sensor, "AT+CGMI");
sleep(1); upm_delay(1);
// query the saved profiles // query the saved profiles
cout << "Querying the saved profiles (AT&V)..." << endl; cout << "Querying the saved profiles (AT&V)..." << endl;
sendCommand(sensor, "AT&V"); sendCommand(sensor, "AT&V");
// A comprehensive list is available from the datasheet at: // A comprehensive list is available from the datasheet at:
// http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
} }
//! [Interesting] //! [Interesting]
delete sensor; return 0;
return 0; }
}

View File

@ -22,28 +22,29 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "grove.hpp" #include <string>
#include "grovebutton.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// This example uses GPIO 0 // This example uses GPIO 0
//! [Interesting] //! [Interesting]
// Create the button object using GPIO pin 0 // Create the button object using GPIO pin 0
upm::GroveButton* button = new upm::GroveButton(0); upm::GroveButton button(0);
// Read the input and print, waiting one second between readings // Read the input and print, waiting one second between readings
while( 1 ) { while (1) {
std::cout << button->name() << " value is " << button->value() << std::endl; std::cout << button.name() << " value is " << button.value() << std::endl;
sleep(1); upm_delay(1);
} }
// Delete the button object // Delete the button object
delete button; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,47 +22,47 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grove.hpp"
#include "groveled.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main() main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a grove LED on D2. Here we are controlling a Grove // Instantiate a grove LED on D2. Here we are controlling a Grove
// Multi-color flash LED. We just just need to turn it on - it will // Multi-color flash LED. We just just need to turn it on - it will
// then cycle through various colors (red, green, blue, purple) on it's // then cycle through various colors (red, green, blue, purple) on it's
// own until turned off. // own until turned off.
upm::GroveLed* led = new upm::GroveLed(2); upm::GroveLed led(2);
// start the light show // start the light show
led->on(); led.on();
// just sleep until interrupted // just upm_delay until interrupted
while (shouldRun) while (shouldRun)
sleep(1); upm_delay(1);
//! [Interesting]
led->off(); //! [Interesting]
cout << "Exiting..." << endl;
delete led; led.off();
cout << "Exiting..." << endl;
return 0;
return 0;
} }

View File

@ -23,33 +23,34 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "grove.hpp" #include <string>
#include "groveled.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Create the Grove LED object using GPIO pin 2 // Create the Grove LED object using GPIO pin 2
upm::GroveLed* led = new upm::GroveLed(2); upm::GroveLed led(2);
// Print the name // Print the name
std::cout << led->name() << std::endl; std::cout << led.name() << std::endl;
// Turn the LED on and off 10 times, pausing one second // Turn the LED on and off 10 times, pausing one second
// between transitions // between transitions
for (int i=0; i < 10; i++) { for (int i = 0; i < 10; i++) {
led->on(); led.on();
sleep(1); upm_delay(1);
led->off(); led.off();
sleep(1); upm_delay(1);
} }
// Delete the Grove LED object // Delete the Grove LED object
delete led; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -23,27 +23,28 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "grove.hpp" #include <string>
#include "grovelight.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Create the light sensor object using AIO pin 0 // Create the light sensor object using AIO pin 0
upm::GroveLight* light = new upm::GroveLight(0); upm::GroveLight light(0);
// Read the input and print both the raw value and a rough lux value, // Read the input and print both the raw value and a rough lux value,
// waiting one second between readings // waiting one second between readings
while( 1 ) { while (1) {
std::cout << light->name() << " raw value is " << light->raw_value() << std::cout << light.name() << " raw value is " << light.raw_value() << ", which is roughly "
", which is roughly " << light->value() << " lux" << std::endl; << light.value() << " lux" << std::endl;
sleep(1); upm_delay(1);
} }
// Delete the light sensor object // Delete the light sensor object
delete light; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,37 +22,38 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "grove.hpp" #include <string>
#include "groverelay.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
// This example uses GPIO 0 // This example uses GPIO 0
//! [Interesting] //! [Interesting]
// Create the relay switch object using GPIO pin 0 // Create the relay switch object using GPIO pin 0
upm::GroveRelay* relay = new upm::GroveRelay(0); upm::GroveRelay relay(0);
// Close and then open the relay switch 3 times, // Close and then open the relay switch 3 times,
// waiting one second each time. The LED on the relay switch // waiting one second each time. The LED on the relay switch
// will light up when the switch is on (closed). // will light up when the switch is on (closed).
// The switch will also make a noise between transitions. // The switch will also make a noise between transitions.
for ( int i = 0; i < 3; i++ ) { for (int i = 0; i < 3; i++) {
relay->on(); relay.on();
if ( relay->isOn() ) if (relay.isOn())
std::cout << relay->name() << " is on" << std::endl; std::cout << relay.name() << " is on" << std::endl;
sleep(1); upm_delay(1);
relay->off(); relay.off();
if ( relay->isOff() ) if (relay.isOff())
std::cout << relay->name() << " is off" << std::endl; std::cout << relay.name() << " is off" << std::endl;
sleep(1); upm_delay(1);
} }
// Delete the relay switch object // Delete the relay switch object
delete relay; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,36 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <iomanip> #include <stdint.h>
#include "grove.hpp" #include <stdio.h>
#include <string>
#include "groverotary.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int main () int
main()
{ {
//! [Interesting] //! [Interesting]
// Instantiate a rotary sensor on analog pin A0 // Instantiate a rotary sensor on analog pin A0
upm::GroveRotary* knob = new upm::GroveRotary(0); upm::GroveRotary knob(0);
// Print sensor name to confirm it initialized properly // Print sensor name to confirm it initialized properly
cout << knob->name() << endl; cout << knob.name() << endl;
while(true) { while (true) {
float abs_value = knob->abs_value(); // Absolute raw value float abs_value = knob.abs_value(); // Absolute raw value
float abs_deg = knob->abs_deg(); // Absolute degrees float abs_deg = knob.abs_deg(); // Absolute degrees
float abs_rad = knob->abs_rad(); // Absolute radians float abs_rad = knob.abs_rad(); // Absolute radians
float rel_value = knob->rel_value(); // Relative raw value float rel_value = knob.rel_value(); // Relative raw value
float rel_deg = knob->rel_deg(); // Relative degrees float rel_deg = knob.rel_deg(); // Relative degrees
float rel_rad = knob->rel_rad(); // Relative radians float rel_rad = knob.rel_rad(); // Relative radians
fprintf(stdout, "Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f deg %3.2f rad\n", fprintf(stdout,
(int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad); "Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f "
"deg %3.2f rad\n",
(int16_t) abs_value,
abs_deg,
abs_rad,
(int16_t) rel_value,
rel_deg,
rel_rad);
usleep(2500000); // Sleep for 2.5s upm_delay_us(2500000); // Sleep for 2.5s
} }
//! [Interesting] //! [Interesting]
delete knob;
return 0; return 0;
} }

View File

@ -22,29 +22,31 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <iomanip> #include <stdint.h>
#include "grove.hpp" #include <stdio.h>
#include <string>
#include "groveslide.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int main () int
main()
{ {
//! [Interesting] //! [Interesting]
upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0 upm::GroveSlide slide(0);
cout << slide->name() << endl; cout << slide.name() << endl;
while(true) { while (true) {
float adc_value = slide->raw_value(); // Read raw value float adc_value = slide.raw_value(); // Read raw value
float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V float volts = slide.voltage_value(); // Read voltage, board reference set at 5.0V
fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts); fprintf(stdout, "%4d = %.2f V\n", (uint16_t) adc_value, volts);
usleep(2500000); // Sleep for 2.5s upm_delay_us(2500000); // Sleep for 2.5s
} }
//! [Interesting] //! [Interesting]
delete slide;
return 0; return 0;
} }

View File

@ -23,33 +23,33 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <iomanip> #include <stdio.h>
#include "grove.hpp" #include <string>
#include "grovetemp.hpp"
#include "upm_utilities.h"
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Create the temperature sensor object using AIO pin 0 // Create the temperature sensor object using AIO pin 0
upm::GroveTemp* temp = new upm::GroveTemp(0); upm::GroveTemp temp(0);
std::cout << temp->name() << std::endl; std::cout << temp.name() << std::endl;
// Read the temperature ten times, printing both the Celsius and // Read the temperature ten times, printing both the Celsius and
// equivalent Fahrenheit temperature, waiting one second between readings // equivalent Fahrenheit temperature, waiting one second between readings
for (int i=0; i < 10; i++) { for (int i = 0; i < 10; i++) {
int celsius = temp->value(); int celsius = temp.value();
int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0); int fahrenheit = (int) (celsius * 9.0 / 5.0 + 32.0);
printf("%d degrees Celsius, or %d degrees Fahrenheit\n", printf("%d degrees Celsius, or %d degrees Fahrenheit\n", celsius, fahrenheit);
celsius, fahrenheit); upm_delay(1);
sleep(1);
} }
// Delete the temperature sensor object // Delete the temperature sensor object
delete temp; //! [Interesting]
//! [Interesting]
return 0; return 0;
} }

View File

@ -22,48 +22,45 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <unistd.h>
#include <signal.h> #include <signal.h>
#include "grovecollision.hpp" #include "grovecollision.hpp"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the Grove Collision Sensor // The was tested with the Grove Collision Sensor
// Instantiate a Grove Collision on digital pin D2 // Instantiate a Grove Collision on digital pin D2
upm::GroveCollision* collision = new upm::GroveCollision(2); upm::GroveCollision collision(2);
bool collisionState = false; bool collisionState = false;
cout << "No collision" << endl; cout << "No collision" << endl;
while (shouldRun) while (shouldRun) {
{ if (collision.isColliding() && !collisionState) {
if (collision->isColliding() && !collisionState) cout << "Collision!" << endl;
{ collisionState = true;
cout << "Collision!" << endl; } else if (collisionState) {
collisionState = true; cout << "No collision" << endl;
} collisionState = false;
else if (collisionState) }
{ }
cout << "No collision" << endl;
collisionState = false;
}
}
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete collision; return 0;
return 0;
} }

View File

@ -22,57 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "groveehr.hpp" #include "groveehr.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main() main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 // Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2
upm::GroveEHR* heart = new upm::GroveEHR(2); upm::GroveEHR heart(2);
// set the beat counter to 0, init the clock and start counting beats
heart->clearBeatCounter();
heart->initClock();
heart->startBeatCounter();
while (shouldRun) // set the beat counter to 0, init the clock and start counting beats
{ heart.clearBeatCounter();
// we grab these just for display purposes in this example heart.initClock();
uint32_t millis = heart->getMillis(); heart.startBeatCounter();
uint32_t beats = heart->beatCounter();
// heartRate() requires that at least 5 seconds pass before while (shouldRun) {
// returning anything other than 0 // we grab these just for display purposes in this example
int hr = heart->heartRate(); uint32_t millis = heart.getMillis();
uint32_t beats = heart.beatCounter();
// output milliseconds passed, beat count, and computed heart rate // heartRate() requires that at least 5 seconds pass before
cout << "Millis: " << millis << " Beats: " << beats; // returning anything other than 0
cout << " Heart Rate: " << hr << endl; int hr = heart.heartRate();
sleep(1); // output milliseconds passed, beat count, and computed heart rate
cout << "Millis: " << millis << " Beats: " << beats;
cout << " Heart Rate: " << hr << endl;
upm_delay(1);
} }
heart->stopBeatCounter(); heart.stopBeatCounter();
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete heart; return 0;
return 0;
} }

View File

@ -22,45 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <unistd.h>
#include <signal.h> #include <signal.h>
#include "groveeldriver.hpp" #include "groveeldriver.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the Grove El Driver Module // The was tested with the Grove El Driver Module
// Instantiate a Grove El Driver on digital pin D2 // Instantiate a Grove El Driver on digital pin D2
upm::GroveElDriver* eldriver = new upm::GroveElDriver(2); upm::GroveElDriver eldriver(2);
bool lightState = true; bool lightState = true;
while (shouldRun) while (shouldRun) {
{ if (lightState)
if (lightState) eldriver.on();
eldriver->on(); else
else eldriver.off();
eldriver->off(); lightState = !lightState;
lightState = !lightState; upm_delay(1);
sleep(1); }
}
//! [Interesting] //! [Interesting]
eldriver->off(); eldriver.off();
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete eldriver; return 0;
return 0;
} }

View File

@ -22,58 +22,60 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream> #include <iostream>
#include <time.h>
#include <signal.h> #include <signal.h>
#include <sys/time.h>
#include <time.h>
#include "groveelectromagnet.hpp" #include "groveelectromagnet.hpp"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
float get_time() float
get_time()
{ {
return ((float)(clock()))/CLOCKS_PER_SEC; return ((float) (clock())) / CLOCKS_PER_SEC;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the Grove Electromagnetic Module // The was tested with the Grove Electromagnetic Module
// Instantiate a Grove Electromagnet on digital pin D2 // Instantiate a Grove Electromagnet on digital pin D2
upm::GroveElectromagnet* magnet = new upm::GroveElectromagnet(2); upm::GroveElectromagnet magnet(2);
cout << "Starting up magnet...." << endl; cout << "Starting up magnet...." << endl;
magnet->off(); magnet.off();
bool magnetState = false; bool magnetState = false;
float time_passed = get_time(); float time_passed = get_time();
// Turn magnet on and off every 5 seconds // Turn magnet on and off every 5 seconds
while (shouldRun) while (shouldRun) {
{ if ((get_time() - time_passed) > 5.0) {
if ((get_time() - time_passed) > 5.0) magnetState = !magnetState;
{ if (magnetState)
magnetState = !magnetState; magnet.on();
if (magnetState) else
magnet->on(); magnet.off();
else cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl;
magnet->off(); time_passed = get_time();
cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; }
time_passed = get_time(); }
}
}
//! [Interesting] //! [Interesting]
magnet->off(); magnet.off();
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete magnet; return 0;
return 0;
} }

View File

@ -21,40 +21,41 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "groveemg.hpp" #include "groveemg.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the GroveEMG Muscle Signal Reader Sensor Module // The was tested with the GroveEMG Muscle Signal Reader Sensor Module
// Instantiate a GroveEMG on analog pin A0 // Instantiate a GroveEMG on analog pin A0
upm::GroveEMG *emg = new upm::GroveEMG(0); upm::GroveEMG emg(0);
cout << "Calibrating...." << endl; cout << "Calibrating...." << endl;
emg->calibrate(); emg.calibrate();
while (shouldRun)
{
cout << emg->value() << endl;
usleep(100000);
}
//! [Interesting] while (shouldRun) {
cout << "Exiting" << endl; cout << emg.value() << endl;
delete emg; upm_delay_us(100000);
return 0; }
//! [Interesting]
cout << "Exiting" << endl;
return 0;
} }

View File

@ -22,90 +22,81 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include <stdio.h>
#include "grovegprs.hpp" #include "grovegprs.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
void printUsage(char *progname) void
printUsage(char* progname)
{ {
cout << "Usage: " << progname << " [AT command]" << endl; cout << "Usage: " << progname << " [AT command]" << endl;
cout << endl; cout << endl;
cout << "If an argument is supplied on the command line, that argument is" cout << "If an argument is supplied on the command line, that argument is" << endl;
<< endl; cout << "sent to the module and the response is printed out." << endl;
cout << "sent to the module and the response is printed out." << endl; cout << endl;
cout << endl; cout << "If no argument is used, then the manufacturer and the current" << endl;
cout << "If no argument is used, then the manufacturer and the current" cout << "saved profiles are queried and the results printed out." << endl;
<< endl; cout << endl;
cout << "saved profiles are queried and the results printed out." << endl; cout << endl;
cout << endl;
cout << endl;
} }
// simple helper function to send a command and wait for a response // simple helper function to send a command and wait for a response
void sendCommand(upm::GroveGPRS* sensor, string cmd) void
sendCommand(upm::GroveGPRS& sensor, string cmd)
{ {
// commands need to be terminated with a carriage return // commands need to be terminated with a carriage return
cmd += "\r"; cmd += "\r";
sensor->writeDataStr(cmd); sensor.writeDataStr(cmd);
// wait up to 1 second // wait up to 1 second
if (sensor->dataAvailable(1000)) if (sensor.dataAvailable(1000)) {
{ cout << "Returned: " << sensor.readDataStr(1024) << endl;
cout << "Returned: " << sensor->readDataStr(1024) << endl; } else {
} cerr << "Timed out waiting for response" << endl;
else
{
cerr << "Timed out waiting for response" << endl;
} }
} }
int
int main(int argc, char **argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a GroveGPRS Module on UART 0 // Instantiate a GroveGPRS Module on UART 0
upm::GroveGPRS* sensor = new upm::GroveGPRS(0); upm::GroveGPRS sensor(0);
// Set the baud rate, 19200 baud is the default. // Set the baud rate, 19200 baud is the default.
if (sensor->setBaudRate(19200) != mraa::SUCCESS) if (sensor.setBaudRate(19200) != mraa::SUCCESS) {
{ cerr << "Failed to set tty baud rate" << endl;
cerr << "Failed to set tty baud rate" << endl; return 1;
return 1;
} }
printUsage(argv[0]); printUsage(argv[0]);
if (argc > 1) if (argc > 1) {
{ cout << "Sending command line argument (" << argv[1] << ")..." << endl;
cout << "Sending command line argument (" << argv[1] << ")..." << endl; sendCommand(sensor, argv[1]);
sendCommand(sensor, argv[1]); } else {
} // query the module manufacturer
else cout << "Querying module manufacturer (AT+CGMI)..." << endl;
{ sendCommand(sensor, "AT+CGMI");
// query the module manufacturer
cout << "Querying module manufacturer (AT+CGMI)..." << endl;
sendCommand(sensor, "AT+CGMI");
sleep(1); upm_delay(1);
// query the saved profiles // query the saved profiles
cout << "Querying the saved profiles (AT&V)..." << endl; cout << "Querying the saved profiles (AT&V)..." << endl;
sendCommand(sensor, "AT&V"); sendCommand(sensor, "AT&V");
// A comprehensive list is available from the datasheet at: // A comprehensive list is available from the datasheet at:
// http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
} }
//! [Interesting] //! [Interesting]
delete sensor; return 0;
return 0;
} }

View File

@ -22,42 +22,43 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovegsr.hpp" #include "grovegsr.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the GroveGSR Galvanic Skin Response Sensor module. // The was tested with the GroveGSR Galvanic Skin Response Sensor module.
// Instantiate a GroveGSR on analog pin A0 // Instantiate a GroveGSR on analog pin A0
upm::GroveGSR *gsr = new upm::GroveGSR(0); upm::GroveGSR gsr(0);
cout << "Calibrating...." << endl; cout << "Calibrating...." << endl;
gsr->calibrate(); gsr.calibrate();
while (shouldRun) while (shouldRun) {
{ cout << gsr.value() << endl;
cout << gsr->value() << endl; upm_delay_us(500000);
usleep(500000); }
} //! [Interesting]
//! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete gsr; return 0;
return 0;
} }

View File

@ -22,45 +22,45 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovelinefinder.hpp" #include "grovelinefinder.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Line Finder sensor on digital pin D2 // Instantiate a Grove Line Finder sensor on digital pin D2
upm::GroveLineFinder* finder = new upm::GroveLineFinder(2); upm::GroveLineFinder finder(2);
// check every second for the presence of white detection
while (shouldRun)
{
bool val = finder->whiteDetected();
if (val)
cout << "White detected." << endl;
else
cout << "Black detected." << endl;
sleep(1); // check every second for the presence of white detection
while (shouldRun) {
bool val = finder.whiteDetected();
if (val)
cout << "White detected." << endl;
else
cout << "Black detected." << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete finder; return 0;
return 0;
} }

View File

@ -22,45 +22,44 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include "grovemd.hpp" #include "grovemd.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate an I2C Grove Motor Driver on I2C bus 0 // Instantiate an I2C Grove Motor Driver on I2C bus 0
upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS, upm::GroveMD motors(GROVEMD_I2C_BUS, GROVEMD_DEFAULT_I2C_ADDR);
GROVEMD_DEFAULT_I2C_ADDR);
// This example demonstrates using the GroveMD to drive a stepper motor // This example demonstrates using the GroveMD to drive a stepper motor
// configure it, for this example, we'll assume 200 steps per rev
motors->configStepper(200);
// set for half a rotation // configure it, for this example, we'll assume 200 steps per rev
motors->setStepperSteps(100); motors.configStepper(200);
// let it go - clockwise rotation, 10 RPM speed // set for half a rotation
motors->enableStepper(upm::GroveMD::STEP_DIR_CW, 10); motors.setStepperSteps(100);
sleep(3); // let it go - clockwise rotation, 10 RPM speed
motors.enableStepper(upm::GroveMD::STEP_DIR_CW, 10);
// Now do it backwards... upm_delay(3);
motors->setStepperSteps(100);
motors->enableStepper(upm::GroveMD::STEP_DIR_CCW, 10);
// now disable // Now do it backwards...
motors->disableStepper(); motors.setStepperSteps(100);
motors.enableStepper(upm::GroveMD::STEP_DIR_CCW, 10);
//! [Interesting] // now disable
motors.disableStepper();
cout << "Exiting..." << endl; //! [Interesting]
delete motors; cout << "Exiting..." << endl;
return 0;
return 0;
} }

View File

@ -22,39 +22,38 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include "grovemd.hpp" #include "grovemd.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate an I2C Grove Motor Driver on I2C bus 0 // Instantiate an I2C Grove Motor Driver on I2C bus 0
upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS, upm::GroveMD motors(GROVEMD_I2C_BUS, GROVEMD_DEFAULT_I2C_ADDR);
GROVEMD_DEFAULT_I2C_ADDR);
// set direction to CW and set speed to 50% // set direction to CW and set speed to 50%
cout << "Spin M1 and M2 at half speed for 3 seconds" << endl; cout << "Spin M1 and M2 at half speed for 3 seconds" << endl;
motors->setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW); motors.setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW);
motors->setMotorSpeeds(127, 127); motors.setMotorSpeeds(127, 127);
sleep(3);
// counter clockwise
cout << "Reversing M1 and M2 for 3 seconds" << endl;
motors->setMotorDirections(upm::GroveMD::DIR_CCW, upm::GroveMD::DIR_CCW);
sleep(3);
//! [Interesting] upm_delay(3);
// counter clockwise
cout << "Reversing M1 and M2 for 3 seconds" << endl;
motors.setMotorDirections(upm::GroveMD::DIR_CCW, upm::GroveMD::DIR_CCW);
upm_delay(3);
cout << "Stopping motors" << endl; //! [Interesting]
motors->setMotorSpeeds(0, 0);
cout << "Exiting..." << endl; cout << "Stopping motors" << endl;
motors.setMotorSpeeds(0, 0);
delete motors; cout << "Exiting..." << endl;
return 0;
return 0;
} }

View File

@ -22,54 +22,54 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovemoisture.hpp" #include "grovemoisture.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Moisture sensor on analog pin A0 // Instantiate a Grove Moisture sensor on analog pin A0
upm::GroveMoisture* moisture = new upm::GroveMoisture(0); upm::GroveMoisture moisture(0);
// Values (approximate):
// 0-300, sensor in air or dry soil
// 300-600, sensor in humid soil
// 600+, sensor in wet soil or submerged in water.
// Read the value every second and print the corresponding moisture level
while (shouldRun)
{
int val = moisture->value();
cout << "Moisture value: " << val << ", ";
if (val >= 0 && val < 300)
cout << "dry";
else if (val >= 300 && val < 600)
cout << "moist";
else
cout << "wet";
cout << endl; // Values (approximate):
// 0-300, sensor in air or dry soil
// 300-600, sensor in humid soil
// 600+, sensor in wet soil or submerged in water.
// Read the value every second and print the corresponding moisture level
while (shouldRun) {
int val = moisture.value();
cout << "Moisture value: " << val << ", ";
if (val >= 0 && val < 300)
cout << "dry";
else if (val >= 300 && val < 600)
cout << "moist";
else
cout << "wet";
sleep(1); cout << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete moisture; return 0;
return 0;
} }

View File

@ -21,37 +21,38 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "groveo2.hpp" #include "groveo2.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the O2 Oxygen Concentration Sensor Module // The was tested with the O2 Oxygen Concentration Sensor Module
// Instantiate a GroveO2 on analog pin A0 // Instantiate a GroveO2 on analog pin A0
upm::GroveO2 *O2 = new upm::GroveO2(0); upm::GroveO2 O2(0);
while (shouldRun) while (shouldRun) {
{ cout << "The output voltage is: " << O2.voltageValue() << "mV" << endl;
cout << "The output voltage is: " << O2->voltageValue() << "mV" << endl; upm_delay_us(100000);
usleep(100000); }
}
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete O2; return 0;
return 0;
} }

View File

@ -22,56 +22,52 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include <stdio.h>
#include "grovescam.hpp" #include "grovescam.hpp"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
int main (int argc, char **argv) int
main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
// Instantiate a Grove Serial Camera on UART 0 // Instantiate a Grove Serial Camera on UART 0
upm::GROVESCAM* camera = new upm::GROVESCAM(0); upm::GROVESCAM camera(0);
// make sure port is initialized properly. 115200 baud is the default. // make sure port is initialized properly. 115200 baud is the default.
if (!camera->setupTty()) if (!camera.setupTty()) {
{ cerr << "Failed to setup tty port parameters" << endl;
cerr << "Failed to setup tty port parameters" << endl; return 1;
return 1;
} }
if (camera->init()) if (camera.init())
cout << "Initialized..." << endl; cout << "Initialized..." << endl;
else else
cout << "init() failed" << endl; cout << "init() failed" << endl;
if (camera->preCapture()) if (camera.preCapture())
cout << "preCapture succeeded..." << endl; cout << "preCapture succeeded..." << endl;
else else
cout << "preCapture failed." << endl; cout << "preCapture failed." << endl;
if (camera->doCapture()) if (camera.doCapture())
cout << "doCapture succeeded..." << endl; cout << "doCapture succeeded..." << endl;
else else
cout << "doCapture failed." << endl; cout << "doCapture failed." << endl;
cout << "Image size is " << camera->getImageSize() << " bytes" << endl; cout << "Image size is " << camera.getImageSize() << " bytes" << endl;
if (camera->getImageSize() > 0) if (camera.getImageSize() > 0) {
{ cout << "Storing image.jpg..." << endl;
cout << "Storing image.jpg..." << endl; if (camera.storeImage("image.jpg"))
if (camera->storeImage("image.jpg")) cout << "storeImage succeeded..." << endl;
cout << "storeImage succeeded..." << endl; else
else cout << "storeImage failed." << endl;
cout << "storeImage failed." << endl;
} }
//! [Interesting] //! [Interesting]
delete camera; return 0;
return 0;
} }

View File

@ -22,28 +22,27 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "grovespeaker.hpp" #include "grovespeaker.hpp"
using namespace std; using namespace std;
int main () int
main()
{ {
//! [Interesting] //! [Interesting]
// Instantiate a Grove Speaker on digital pin D2 // Instantiate a Grove Speaker on digital pin D2
upm::GroveSpeaker* speaker = new upm::GroveSpeaker(2); upm::GroveSpeaker speaker(2);
// Play all 7 of the lowest notes // Play all 7 of the lowest notes
speaker->playAll(); speaker.playAll();
// Play a medium C-sharp // Play a medium C-sharp
speaker->playSound('c', true, "med"); speaker.playSound('c', true, "med");
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete speaker; return 0;
return 0;
} }

View File

@ -24,39 +24,36 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream>
#include "groveultrasonic.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdio.h>
#include <sys/time.h>
#include "groveultrasonic.hpp"
#include "upm_utilities.h"
upm::GroveUltraSonic *sonar = NULL;
bool running = true; bool running = true;
void void
sig_handler(int signo) sig_handler(int signo)
{ {
if (signo == SIGINT) { if (signo == SIGINT) {
running = false; running = false;
} }
} }
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// upm::GroveUltraSonic *sonar = NULL; // upm::GroveUltraSonic *sonar = NULL;
sonar = new upm::GroveUltraSonic(2); upm::GroveUltraSonic sonar(2);
while(running) { while (running) {
int width = sonar->getDistance(); int width = sonar.getDistance();
printf("Echo width = %d\n", width); printf("Echo width = %d\n", width);
printf("Distance inches = %f.2\n\n", width/148.0); printf("Distance inches = %f.2\n\n", width / 148.0);
sleep(3); upm_delay(3);
} }
//! [Interesting] //! [Interesting]
printf("exiting application\n"); printf("exiting application\n");
delete sonar; return 0;
return 0;
} }

View File

@ -22,46 +22,46 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovevdiv.hpp" #include "grovevdiv.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Voltage Divider sensor on analog pin A0 // Instantiate a Grove Voltage Divider sensor on analog pin A0
upm::GroveVDiv* vDiv = new upm::GroveVDiv(0); upm::GroveVDiv vDiv(0);
// collect data and output measured voltage according to the setting // collect data and output measured voltage according to the setting
// of the scaling switch (3 or 10) // of the scaling switch (3 or 10)
while (shouldRun) while (shouldRun) {
{ unsigned int val = vDiv.value(100);
unsigned int val = vDiv->value(100); float gain3val = vDiv.computedValue(3, val);
float gain3val = vDiv->computedValue(3, val); float gain10val = vDiv.computedValue(10, val);
float gain10val = vDiv->computedValue(10, val); cout << "ADC value: " << val << " Gain 3: " << gain3val << "v Gain 10: " << gain10val << "v"
cout << "ADC value: " << val << " Gain 3: " << gain3val << endl;
<< "v Gain 10: " << gain10val << "v" << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete vDiv; return 0;
return 0;
} }

View File

@ -22,44 +22,44 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovewater.hpp" #include "grovewater.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main () main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Water sensor on digital pin D2 // Instantiate a Grove Water sensor on digital pin D2
upm::GroveWater* water = new upm::GroveWater(2); upm::GroveWater water(2);
while (shouldRun)
{
bool val = water->isWet();
if (val)
cout << "Sensor is wet." << endl;
else
cout << "Sensor is dry." << endl;
sleep(1); while (shouldRun) {
bool val = water.isWet();
if (val)
cout << "Sensor is wet." << endl;
else
cout << "Sensor is dry." << endl;
upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete water; return 0;
return 0;
} }

View File

@ -22,57 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovewfs.hpp" #include "grovewfs.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int
int main() main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Water Flow Sensor on digital pin D2 // Instantiate a Grove Water Flow Sensor on digital pin D2
upm::GroveWFS* flow = new upm::GroveWFS(2); upm::GroveWFS flow(2);
// set the flow counter to 0 and start counting
flow->clearFlowCounter();
flow->startFlowCounter();
while (shouldRun) // set the flow counter to 0 and start counting
{ flow.clearFlowCounter();
// we grab these (,illis and flowCount) just for display flow.startFlowCounter();
// purposes in this example
uint32_t millis = flow->getMillis();
uint32_t flowCount = flow->flowCounter();
float fr = flow->flowRate(); while (shouldRun) {
// we grab these (,illis and flowCount) just for display
// purposes in this example
uint32_t millis = flow.getMillis();
uint32_t flowCount = flow.flowCounter();
// output milliseconds passed, flow count, and computed flow rate float fr = flow.flowRate();
cout << "Millis: " << millis << " Flow Count: " << flowCount;
cout << " Flow Rate: " << fr << " LPM" << endl;
// best to gather data for at least one second for reasonable // output milliseconds passed, flow count, and computed flow rate
// results. cout << "Millis: " << millis << " Flow Count: " << flowCount;
sleep(2); cout << " Flow Rate: " << fr << " LPM" << endl;
// best to gather data for at least one second for reasonable
// results.
upm_delay(2);
} }
flow->stopFlowCounter(); flow.stopFlowCounter();
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete flow; return 0;
return 0;
} }

View File

@ -22,42 +22,43 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "gsr.hpp" #include "gsr.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// The was tested with the GSR Galvanic Skin Response Sensor module. // The was tested with the GSR Galvanic Skin Response Sensor module.
// Instantiate a GSR on analog pin A0 // Instantiate a GSR on analog pin A0
upm::GSR *gsr = new upm::GSR(0); upm::GSR gsr(0);
cout << "Calibrating...." << endl; cout << "Calibrating...." << endl;
gsr->calibrate(); gsr.calibrate();
while (shouldRun) while (shouldRun) {
{ cout << gsr.value() << endl;
cout << gsr->value() << endl; upm_delay_us(500000);
usleep(500000); }
} //! [Interesting]
//! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete gsr; return 0;
return 0;
} }

View File

@ -22,51 +22,50 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "guvas12d.hpp" #include "guvas12d.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
// analog voltage, usually 3.3 or 5.0 // analog voltage, usually 3.3 or 5.0
#define GUVAS12D_AREF 5.0 #define GUVAS12D_AREF 5.0
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main() int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// This was tested with the Grove UV Sensor module. // This was tested with the Grove UV Sensor module.
// It has a sensing range from between 240-370nm. It's strongest // It has a sensing range from between 240-370nm. It's strongest
// response is around 320-360nm. // response is around 320-360nm.
// Instantiate a GUVAS12D on analog pin A0 // Instantiate a GUVAS12D on analog pin A0
upm::GUVAS12D *volts = new upm::GUVAS12D(0); upm::GUVAS12D volts(0);
// The higher the voltage the more intense the UV radiation. // The higher the voltage the more intense the UV radiation.
while (shouldRun) while (shouldRun) {
{ cout << "Volts: " << volts.volts() << ", Intensity: " << volts.intensity() << " mW/m^2"
cout << "Volts: " << volts->volts()
<< ", Intensity: " << volts->intensity()
<< " mW/m^2"
<< endl; << endl;
sleep(1); upm_delay(1);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting" << endl; cout << "Exiting" << endl;
delete volts;
return 0; return 0;
} }

View File

@ -22,59 +22,58 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <signal.h>
#include <iostream> #include <iostream>
#include <signal.h>
#include "h3lis331dl.hpp" #include "h3lis331dl.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
using namespace upm; using namespace upm;
int shouldRun = true; int shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate an H3LIS331DL on I2C bus 0 // Instantiate an H3LIS331DL on I2C bus 0
upm::H3LIS331DL *accel = new upm::H3LIS331DL(H3LIS331DL_I2C_BUS, upm::H3LIS331DL accel(H3LIS331DL_I2C_BUS, H3LIS331DL_DEFAULT_I2C_ADDR);
H3LIS331DL_DEFAULT_I2C_ADDR);
// Initialize the device with default values // Initialize the device with default values
accel->init(); accel.init();
while (shouldRun) while (shouldRun) {
{ int x, y, z;
int x, y, z; float ax, ay, az;
float ax, ay, az;
accel->update(); accel.update();
accel->getRawXYZ(&x, &y, &z); accel.getRawXYZ(&x, &y, &z);
accel->getAcceleration(&ax, &ay, &az); accel.getAcceleration(&ax, &ay, &az);
cout << "Raw: X = " << x << " Y = " << y << " Z = " << z << endl; cout << "Raw: X = " << x << " Y = " << y << " Z = " << z << endl;
cout << "Acceleration: AX = " << ax << " AY = " << ay << " AZ = " << az
<< endl;
cout << endl; cout << "Acceleration: AX = " << ax << " AY = " << ay << " AZ = " << az << endl;
usleep(500000); cout << endl;
upm_delay_us(500000);
} }
//! [Interesting] //! [Interesting]
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete accel; return 0;
return 0;
} }

View File

@ -22,128 +22,100 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include <string>
#include "h803x.hpp" #include "h803x.hpp"
#include "upm_utilities.h"
using namespace std; using namespace std;
bool shouldRun = true; bool shouldRun = true;
void sig_handler(int signo) void
sig_handler(int signo)
{ {
if (signo == SIGINT) if (signo == SIGINT)
shouldRun = false; shouldRun = false;
} }
int main(int argc, char **argv) int
main(int argc, char** argv)
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
string defaultDev = "/dev/ttyUSB0"; string defaultDev = "/dev/ttyUSB0";
// if an argument was specified, use it as the device instead // if an argument was specified, use it as the device instead
if (argc > 1) if (argc > 1)
defaultDev = string(argv[1]); defaultDev = string(argv[1]);
cout << "Using device " << defaultDev << endl; cout << "Using device " << defaultDev << endl;
cout << "Initializing..." << endl; cout << "Initializing..." << endl;
// Instantiate an H803X instance, using MODBUS slave address 1, and // Instantiate an H803X instance, using MODBUS slave address 1, and
// default comm parameters (9600, 8, N, 2) // default comm parameters (9600, 8, N, 2)
upm::H803X *sensor = new upm::H803X(defaultDev, 1); upm::H803X sensor(defaultDev, 1);
// output the Slave ID string // output the Slave ID string
cout << "Slave ID: " << sensor->getSlaveID() << endl; cout << "Slave ID: " << sensor.getSlaveID() << endl;
cout << endl; cout << endl;
// update and print available values every second // update and print available values every second
while (shouldRun) while (shouldRun) {
{ // update our values from the sensor
// update our values from the sensor sensor.update();
sensor->update();
// H8035 / H8036 // H8035 / H8036
cout << "Consumption (kWh): " << sensor->getConsumption() << endl; cout << "Consumption (kWh): " << sensor.getConsumption() << endl;
cout << "Real Power (kW): " << sensor->getRealPower() << endl; cout << "Real Power (kW): " << sensor.getRealPower() << endl;
if (sensor->isH8036()) if (sensor.isH8036()) {
{ // The H8036 has much more data available...
// The H8036 has much more data available...
cout << "Reactive Power (kVAR): " << sensor->getReactivePower() cout << "Reactive Power (kVAR): " << sensor.getReactivePower() << endl;
<< endl; cout << "Apparent Power (kVA): " << sensor.getApparentPower() << endl;
cout << "Apparent Power (kVA): " << sensor->getApparentPower() cout << "Power Factor: " << sensor.getPowerFactor() << endl;
<< endl; cout << "Volts Line to Line: " << sensor.getVoltsLineToLine() << endl;
cout << "Power Factor: " << sensor->getPowerFactor() cout << "Volts Line to Neutral: " << sensor.getVoltsLineToNeutral() << endl;
<< endl;
cout << "Volts Line to Line: " << sensor->getVoltsLineToLine()
<< endl;
cout << "Volts Line to Neutral: " << sensor->getVoltsLineToNeutral()
<< endl;
cout << "Current: " << sensor->getCurrent() cout << "Current: " << sensor.getCurrent() << endl;
<< endl;
cout << "Real Power Phase A (kW): " << sensor->getRealPowerPhaseA() cout << "Real Power Phase A (kW): " << sensor.getRealPowerPhaseA() << endl;
<< endl; cout << "Real Power Phase B (kW): " << sensor.getRealPowerPhaseB() << endl;
cout << "Real Power Phase B (kW): " << sensor->getRealPowerPhaseB() cout << "Real Power Phase C (kW): " << sensor.getRealPowerPhaseC() << endl;
<< endl;
cout << "Real Power Phase C (kW): " << sensor->getRealPowerPhaseC()
<< endl;
cout << "Power Factor Phase A: " << sensor->getPowerFactorPhaseA() cout << "Power Factor Phase A: " << sensor.getPowerFactorPhaseA() << endl;
<< endl; cout << "Power Factor Phase B: " << sensor.getPowerFactorPhaseB() << endl;
cout << "Power Factor Phase B: " << sensor->getPowerFactorPhaseB() cout << "Power Factor Phase C: " << sensor.getPowerFactorPhaseC() << endl;
<< endl;
cout << "Power Factor Phase C: " << sensor->getPowerFactorPhaseC()
<< endl;
cout << "Volts Phase A to B: " << sensor->getVoltsPhaseAToB() cout << "Volts Phase A to B: " << sensor.getVoltsPhaseAToB() << endl;
<< endl; cout << "Volts Phase B to C: " << sensor.getVoltsPhaseBToC() << endl;
cout << "Volts Phase B to C: " << sensor->getVoltsPhaseBToC() cout << "Volts Phase A to C: " << sensor.getVoltsPhaseAToC() << endl;
<< endl; cout << "Volts Phase A to Neutral: " << sensor.getVoltsPhaseAToNeutral() << endl;
cout << "Volts Phase A to C: " << sensor->getVoltsPhaseAToC() cout << "Volts Phase B to Neutral: " << sensor.getVoltsPhaseBToNeutral() << endl;
<< endl; cout << "Volts Phase C to Neutral: " << sensor.getVoltsPhaseCToNeutral() << endl;
cout << "Volts Phase A to Neutral: "
<< sensor->getVoltsPhaseAToNeutral()
<< endl;
cout << "Volts Phase B to Neutral: "
<< sensor->getVoltsPhaseBToNeutral()
<< endl;
cout << "Volts Phase C to Neutral: "
<< sensor->getVoltsPhaseCToNeutral()
<< endl;
cout << "Current Phase A: " << sensor->getCurrentPhaseA() cout << "Current Phase A: " << sensor.getCurrentPhaseA() << endl;
<< endl; cout << "Current Phase B: " << sensor.getCurrentPhaseB() << endl;
cout << "Current Phase B: " << sensor->getCurrentPhaseB() cout << "Current Phase C: " << sensor.getCurrentPhaseC() << endl;
<< endl;
cout << "Current Phase C: " << sensor->getCurrentPhaseC()
<< endl;
cout << "Avg Real Power (kW): " << sensor->getAvgRealPower() cout << "Avg Real Power (kW): " << sensor.getAvgRealPower() << endl;
<< endl; cout << "Min Real Power (kW): " << sensor.getMinRealPower() << endl;
cout << "Min Real Power (kW): " << sensor->getMinRealPower() cout << "Max Real Power (kW): " << sensor.getMaxRealPower() << endl;
<< endl;
cout << "Max Real Power (kW): " << sensor->getMaxRealPower()
<< endl;
} }
cout << endl; cout << endl;
sleep(2); upm_delay(2);
} }
cout << "Exiting..." << endl; cout << "Exiting..." << endl;
delete sensor; //! [Interesting]
//! [Interesting] return 0;
return 0;
} }

View File

@ -22,12 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <unistd.h>
#include <iostream> #include <iostream>
#include "hcsr04.hpp"
#include <signal.h> #include <signal.h>
#include <stdlib.h>
#include <sys/time.h> #include "hcsr04.hpp"
#include "upm_utilities.h"
int shouldRun = true; int shouldRun = true;
@ -41,22 +40,21 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
upm::HCSR04 *sonar = new upm::HCSR04(2, 4); upm::HCSR04 sonar(2, 4);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
sleep(1); upm_delay(1);
while(shouldRun){ while (shouldRun) {
std::cout << "get distance" << std::endl; std::cout << "get distance" << std::endl;
double distance = sonar->getDistance(HCSR04_CM); double distance = sonar.getDistance(HCSR04_CM);
std::cout << "distance " << distance << std::endl; std::cout << "distance " << distance << std::endl;
sleep(2); upm_delay(2);
} }
std::cout << "Exiting... " << std::endl; std::cout << "Exiting... " << std::endl;
delete sonar;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]

Some files were not shown because too many files have changed in this diff Show More