diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f4b1c498..fcf12440 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -99,7 +99,7 @@ function (add_example example_src) # Add each dependency to the library target 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 () endfunction (add_example example_src) diff --git a/examples/c++/a110x-intr.cxx b/examples/c++/a110x-intr.cxx index 93b2a492..a19280e4 100644 --- a/examples/c++/a110x-intr.cxx +++ b/examples/c++/a110x-intr.cxx @@ -22,55 +22,58 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "a110x.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } // Our pulse counter volatile unsigned int counter = 0; // 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] - // Instantiate an A110X sensor on digital pin D2 - upm::A110X* hall = new upm::A110X(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. + //! [Interesting] + // Instantiate an A110X sensor on digital pin D2 + upm::A110X hall(2); - 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) - { - cout << "Pulses detected: " << counter << endl; + hall.installISR(hallISR, NULL); - 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; } diff --git a/examples/c++/a110x.cxx b/examples/c++/a110x.cxx index 0344ac36..3acb0861 100644 --- a/examples/c++/a110x.cxx +++ b/examples/c++/a110x.cxx @@ -22,46 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "a110x.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an A110X sensor on digital pin D2 - upm::A110X* hall = new upm::A110X(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; + //! [Interesting] + // Instantiate an A110X sensor on digital pin D2 + upm::A110X hall(2); - 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; } diff --git a/examples/c++/abp.cxx b/examples/c++/abp.cxx index 564a9749..e13b0a00 100644 --- a/examples/c++/abp.cxx +++ b/examples/c++/abp.cxx @@ -21,41 +21,42 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include + #include #include + #include "abp.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main () +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // 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) { - abp->update(); - cout << "Retrieved pressure: " << abp->getPressure() << endl; - cout << "Retrieved Temperature: " << abp->getTemperature() << endl; + abp.update(); + cout << "Retrieved pressure: " << abp.getPressure() << endl; + cout << "Retrieved Temperature: " << abp.getTemperature() << endl; - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; - delete abp; return 0; } diff --git a/examples/c++/ad8232.cxx b/examples/c++/ad8232.cxx index c07094bd..fe8de298 100644 --- a/examples/c++/ad8232.cxx +++ b/examples/c++/ad8232.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "ad8232.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Ad8232 sensor on digital pins 10 (LO+), 11 (LO-) - // and an analog pin, 0 (OUTPUT) - upm::AD8232 *ad8232 = new upm::AD8232(10, 11, 0); - - // Output the raw numbers from the ADC, for plotting elsewhere. - // A return of 0 indicates a Lead Off (LO) condition. - // In theory, this data could be fed to software like Processing - // (https://www.processing.org/) to plot the data just like an - // EKG you would see in a hospital. - while (shouldRun) - { - cout << ad8232->value() << endl; - usleep(1000); + //! [Interesting] + // Instantiate a Ad8232 sensor on digital pins 10 (LO+), 11 (LO-) + // and an analog pin, 0 (OUTPUT) + upm::AD8232 ad8232(10, 11, 0); + + // Output the raw numbers from the ADC, for plotting elsewhere. + // A return of 0 indicates a Lead Off (LO) condition. + // In theory, this data could be fed to software like Processing + // (https://www.processing.org/) to plot the data just like an + // EKG you would see in a hospital. + while (shouldRun) { + cout << ad8232.value() << endl; + upm_delay_us(1000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete ad8232; - return 0; + return 0; } diff --git a/examples/c++/adafruitms1438-stepper.cxx b/examples/c++/adafruitms1438-stepper.cxx index 41a5f88a..64aef41e 100644 --- a/examples/c++/adafruitms1438-stepper.cxx +++ b/examples/c++/adafruitms1438-stepper.cxx @@ -22,61 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "adafruitms1438.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an Adafruit MS 1438 on I2C bus 0 + //! [Interesting] + // Instantiate an Adafruit MS 1438 on I2C bus 0 - upm::AdafruitMS1438 *ms = - new upm::AdafruitMS1438(ADAFRUITMS1438_I2C_BUS, - ADAFRUITMS1438_DEFAULT_I2C_ADDR); + upm::AdafruitMS1438 ms(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 - ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12); + // disable first, to be safe + ms.disableStepper(AdafruitMS1438::STEPMOTOR_M12); - // configure for a NEMA-17, 200 steps per revolution - ms->stepConfig(AdafruitMS1438::STEPMOTOR_M12, 200); + // configure for a NEMA-17, 200 steps per revolution + ms.stepConfig(AdafruitMS1438::STEPMOTOR_M12, 200); - // set speed at 10 RPM's - ms->setStepperSpeed(AdafruitMS1438::STEPMOTOR_M12, 10); - ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, - AdafruitMS1438::DIR_CW); + // set speed at 10 RPM's + ms.setStepperSpeed(AdafruitMS1438::STEPMOTOR_M12, 10); + ms.setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, AdafruitMS1438::DIR_CW); - // enable - cout << "Enabling..." << endl; - ms->enableStepper(AdafruitMS1438::STEPMOTOR_M12); + // enable + cout << "Enabling..." << endl; + ms.enableStepper(AdafruitMS1438::STEPMOTOR_M12); - cout << "Rotating 1 full revolution at 10 RPM speed." << endl; - ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 200); + cout << "Rotating 1 full revolution at 10 RPM speed." << endl; + ms.stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 200); - cout << "Sleeping for 2 seconds..." << endl; - sleep(2); - cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." - << endl; + cout << "Sleeping for 2 seconds..." << endl; + upm_delay(2); + cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." << endl; - ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, - AdafruitMS1438::DIR_CCW); - ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 100); + ms.setStepperDirection(AdafruitMS1438::STEPMOTOR_M12, AdafruitMS1438::DIR_CCW); + ms.stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 100); - cout << "Disabling..." << endl; - ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12); + cout << "Disabling..." << endl; + ms.disableStepper(AdafruitMS1438::STEPMOTOR_M12); - cout << "Exiting" << endl; + cout << "Exiting" << endl; -//! [Interesting] + //! [Interesting] - delete ms; - return 0; + return 0; } diff --git a/examples/c++/adafruitms1438.cxx b/examples/c++/adafruitms1438.cxx index 1a94e17f..a151ddbf 100644 --- a/examples/c++/adafruitms1438.cxx +++ b/examples/c++/adafruitms1438.cxx @@ -22,54 +22,51 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "adafruitms1438.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an Adafruit MS 1438 on I2C bus 0 + //! [Interesting] + // Instantiate an Adafruit MS 1438 on I2C bus 0 - upm::AdafruitMS1438 *ms = - new upm::AdafruitMS1438(ADAFRUITMS1438_I2C_BUS, - ADAFRUITMS1438_DEFAULT_I2C_ADDR); + upm::AdafruitMS1438 ms(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 - ms->setPWMPeriod(50); + // set a PWM period of 50Hz + ms.setPWMPeriod(50); - // disable first, to be safe - ms->disableMotor(AdafruitMS1438::MOTOR_M3); + // disable first, to be safe + ms.disableMotor(AdafruitMS1438::MOTOR_M3); - // set speed at 50% - ms->setMotorSpeed(AdafruitMS1438::MOTOR_M3, 50); - ms->setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CW); + // set speed at 50% + ms.setMotorSpeed(AdafruitMS1438::MOTOR_M3, 50); + ms.setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CW); - cout << "Spin M3 at half speed for 3 seconds, then reverse for 3 seconds." - << endl; + cout << "Spin M3 at half speed for 3 seconds, then reverse for 3 seconds." << endl; - ms->enableMotor(AdafruitMS1438::MOTOR_M3); + ms.enableMotor(AdafruitMS1438::MOTOR_M3); - sleep(3); + upm_delay(3); - cout << "Reversing M3" << endl; - ms->setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CCW); + cout << "Reversing M3" << endl; + ms.setMotorDirection(AdafruitMS1438::MOTOR_M3, AdafruitMS1438::DIR_CCW); - sleep(3); + upm_delay(3); - cout << "Stopping M3" << endl; - ms->disableMotor(AdafruitMS1438::MOTOR_M3); + cout << "Stopping M3" << endl; + ms.disableMotor(AdafruitMS1438::MOTOR_M3); - cout << "Exiting" << endl; + cout << "Exiting" << endl; -//! [Interesting] + //! [Interesting] - delete ms; - return 0; + return 0; } diff --git a/examples/c++/adafruitss.cxx b/examples/c++/adafruitss.cxx index 7e9f68d4..54115ea9 100644 --- a/examples/c++/adafruitss.cxx +++ b/examples/c++/adafruitss.cxx @@ -22,56 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - /** * Description * Demo program for Adafruit 16 channel servo shield/controller * Physical setup for tests is a single servo attached to one channel. * 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. */ #include + #include "adafruitss.hpp" -#include +#include "upm_utilities.h" using namespace std; -int main() { +int +main() +{ + int n; -int n; + //! [Interesting] + upm::adafruitss servos(6, 0x40); -//! [Interesting] - upm::adafruitss* servos = new upm::adafruitss(6,0x40); + for (;;) { + 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 0" << endl; - for (n = 0; n < 16; n++) - servos->servo(n, 1, 0); // GWS Mini Servo = Type 1. - usleep(1000000); // Wait 1 second + cout << "Setting all to 45" << endl; + for (n = 0; n < 16; n++) + servos.servo(n, 1, 45); + upm_delay_us(1000000); // Wait 1 second - cout << "Setting all to 45" << endl; - for (n = 0; n < 16; n++) - servos->servo(n, 1, 45); - usleep(1000000); // Wait 1 second + cout << "Setting all to 90" << endl; + for (n = 0; n < 16; n++) + servos.servo(n, 1, 90); + upm_delay_us(1000000); // Wait 1 second - cout << "Setting all to 90" << endl; - for (n = 0; n < 16; n++) - servos->servo(n, 1, 90); - usleep(1000000); // Wait 1 second + cout << "Setting all to 135" << endl; + for (n = 0; n < 16; n++) + servos.servo(n, 1, 135); + upm_delay_us(1000000); // Wait 1 second - cout << "Setting all to 135" << endl; - for (n = 0; n < 16; n++) - servos->servo(n, 1, 135); - usleep(1000000); // Wait 1 second - - cout << "Setting all to 180" << endl; - for (n = 0; n < 16; n++) - servos->servo(n, 1, 160); - usleep(2000000); // Wait 1 second - } -//! [Interesting] - return 0; + cout << "Setting all to 180" << endl; + for (n = 0; n < 16; n++) + servos.servo(n, 1, 160); + upm_delay_us(2000000); // Wait 1 second + } + //! [Interesting] + return 0; } diff --git a/examples/c++/adc121c021.cxx b/examples/c++/adc121c021.cxx index 7a86c18d..5fbb4522 100644 --- a/examples/c++/adc121c021.cxx +++ b/examples/c++/adc121c021.cxx @@ -22,45 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include + #include "adc121c021.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an ADC121C021 on I2C bus 0 + //! [Interesting] + // Instantiate an ADC121C021 on I2C bus 0 - upm::ADC121C021 *adc = new upm::ADC121C021(ADC121C021_I2C_BUS, - ADC121C021_DEFAULT_I2C_ADDR); + upm::ADC121C021 adc(ADC121C021_I2C_BUS, ADC121C021_DEFAULT_I2C_ADDR); - // An analog sensor, such as a Grove light sensor, - // must be attached to the adc - // Prints the value and corresponding voltage every 50 milliseconds - while (shouldRun) - { - uint16_t val = adc->value(); - cout << "ADC value: " << val << " Volts = " - << adc->valueToVolts(val) << endl; - usleep(50000); + // An analog sensor, such as a Grove light sensor, + // must be attached to the adc + // Prints the value and corresponding voltage every 50 milliseconds + while (shouldRun) { + uint16_t val = adc.value(); + cout << "ADC value: " << val << " Volts = " << adc.valueToVolts(val) << endl; + upm_delay_us(50000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete adc; - return 0; + return 0; } diff --git a/examples/c++/adis16448.cxx b/examples/c++/adis16448.cxx index de7bbff5..8fe576b0 100644 --- a/examples/c++/adis16448.cxx +++ b/examples/c++/adis16448.cxx @@ -12,9 +12,12 @@ // 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. // -// This software has been tested to connect to an ADIS16448 through a level shifter -// 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. +// This software has been tested to connect to an ADIS16448 through a level +// shifter +// 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 // 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. // ////////////////////////////////////////////////////////////////////////////////////// -#include #include -#include #include "adis16448.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { - while(true) - { - //! [Interesting] - upm::ADIS16448* imu = new upm::ADIS16448(0,3); //upm::ADIS16448(SPI,RST) + while (true) { + //! [Interesting] + upm::ADIS16448 imu(0, 3); // upm::ADIS16448(SPI,RST) - //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 << "YGYRO_OUT:" << imu->gyroScale(imu->regRead(YGYRO_OUT)) << std::endl; - std::cout << "ZGYRO_OUT:" << imu->gyroScale(imu->regRead(ZGYRO_OUT)) << std::endl; + // 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 << "YGYRO_OUT:" << imu.gyroScale(imu.regRead(YGYRO_OUT)) << std::endl; + std::cout << "ZGYRO_OUT:" << imu.gyroScale(imu.regRead(ZGYRO_OUT)) << std::endl; std::cout << " " << 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 << "ZACCL_OUT:" << imu->accelScale(imu->regRead(ZACCL_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 << "ZACCL_OUT:" << imu.accelScale(imu.regRead(ZACCL_OUT)) << std::endl; std::cout << " " << std::endl; - //! [Interesting] - sleep(1); + //! [Interesting] + upm_delay(1); } return (0); } diff --git a/examples/c++/ads1x15-adc-sensor.cxx b/examples/c++/ads1x15-adc-sensor.cxx index b367086c..ee6bf4bb 100644 --- a/examples/c++/ads1x15-adc-sensor.cxx +++ b/examples/c++/ads1x15-adc-sensor.cxx @@ -22,59 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include +#include + #include "ads1015.hpp" +#include "iADC.hpp" #include "mraa/gpio.hpp" +#include "upm_utilities.h" #define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 #define EDISON_GPIO_SI7005_CS 20 - -//! [Interesting] -// 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() +int +main() { - upm::IADC* adc = NULL; - try { - adc = new upm::ADS1015(EDISON_I2C_BUS); - mraa::Gpio gpio(EDISON_GPIO_SI7005_CS); - gpio.dir(mraa::DIR_OUT_HIGH); - return adc; - } catch (std::exception& e) { - std::cerr << "ADS1015: " << e.what() << std::endl; - } - return adc; -} + /* Create an instance of the ADS1015 sensor */ + upm::ADS1015 sensor(EDISON_I2C_BUS); + mraa::Gpio gpio(EDISON_GPIO_SI7005_CS); + gpio.dir(mraa::DIR_OUT_HIGH); -int main () -{ - upm::IADC* adc = getADC(); - if (adc == NULL) { - std::cout << "ADC not detected" << std::endl; - return 1; - } - std::cout << "ADC " << adc->getModuleName() << " detected. " ; - std::cout << adc->getNumInputs() << " inputs available" << std::endl; - while (true) { - for (unsigned int i=0; igetNumInputs(); ++i) { - std::cout << "Input " << i; - try { - float voltage = adc->getVoltage(i); - std::cout << ": Voltage = " << voltage << "V" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - } - sleep(1); - } - delete adc; - return 0; + /* Show usage from the IADC interface */ + upm::IADC* adc = static_cast(&sensor); + + if (adc == NULL) { + std::cout << "ADC not detected" << std::endl; + return 1; + } + std::cout << "ADC " << adc->getModuleName() << " detected. "; + std::cout << adc->getNumInputs() << " inputs available" << std::endl; + while (true) { + for (unsigned int i = 0; i < adc->getNumInputs(); ++i) { + std::cout << "Input " << i; + try { + float voltage = adc->getVoltage(i); + std::cout << ": Voltage = " << voltage << "V" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + } + upm_delay(1); + } + + return 0; } //! [Interesting] diff --git a/examples/c++/ads1x15-ads1015.cxx b/examples/c++/ads1x15-ads1015.cxx index d151fab6..71968178 100644 --- a/examples/c++/ads1x15-ads1015.cxx +++ b/examples/c++/ads1x15-ads1015.cxx @@ -29,11 +29,13 @@ */ #include +#include #include #include -#include #include "ads1015.hpp" +#include "ads1x15.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; @@ -41,39 +43,41 @@ using namespace upm; bool running = true; // Controls main read/write loop // Thread function -void stop() +void +stop() { - sleep(10); + upm_delay(10); running = false; } -int main() +int +main() { //! [Interesting] - long id = 0; // Sample number + long id = 0; // Sample number string fileName = "./ads1015.data"; // Output filename ofstream f; // 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 - ads1015->getSample(ADS1X15::DIFF_0_1); + ads1015.getSample(ADS1X15::DIFF_0_1); // 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 // 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 // sampling - ads1015->setSPS(ADS1015::SPS_3300); - ads1015->setContinuous(true); + ads1015.setSPS(ADS1015::SPS_3300); + ads1015.setContinuous(true); // Enable exceptions from the output stream f.exceptions(ofstream::failbit | ofstream::badbit); // Open the file - try{ + try { f.open(fileName); // Output formatting @@ -81,19 +85,18 @@ int main() f.precision(7); // 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 - while(running){ - f << id++ << " " << ads1015->getLastSample() << endl; - usleep(1000); + while (running) { + f << id++ << " " << ads1015.getLastSample() << endl; + upm_delay_us(1000); } // Clean-up and exit timer.join(); f.close(); - delete ads1015; - } catch (ios_base::failure &e) { + } catch (ios_base::failure& e) { cout << "Failed to write to file: " << e.what() << endl; return 1; } @@ -101,4 +104,3 @@ int main() //! [Interesting] return 0; } - diff --git a/examples/c++/ads1x15-ads1115.cxx b/examples/c++/ads1x15-ads1115.cxx index 835137a8..2c96474f 100644 --- a/examples/c++/ads1x15-ads1115.cxx +++ b/examples/c++/ads1x15-ads1115.cxx @@ -28,11 +28,13 @@ */ #include +#include #include #include -#include #include "ads1115.hpp" +#include "ads1x15.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; @@ -40,16 +42,18 @@ using namespace upm; bool running = true; // Controls main read/write loop // Thread function -void stop() +void +stop() { - sleep(10); + upm_delay(10); running = false; } -int main() +int +main() { //! [Interesting] - long id = 0; // Sample number + long id = 0; // Sample number string fileName = "./ads1115.data"; // Output filename ofstream f; @@ -57,26 +61,26 @@ int main() // There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus // - 0x48 gives access to pins A0 - A3 // - 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, // 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 // Can be adjusted based on application to as low as -/+ 0.256 V, see API // 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 // sampling - ads1115->setSPS(ADS1115::SPS_860); - ads1115->setContinuous(true); + ads1115.setSPS(ADS1115::SPS_860); + ads1115.setContinuous(true); // Enable exceptions from the output stream f.exceptions(ofstream::failbit | ofstream::badbit); // Open the file - try{ + try { f.open(fileName); // Output formatting @@ -84,19 +88,18 @@ int main() f.precision(7); // 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 - while(running){ - f << id++ << " " << ads1115->getLastSample() << endl; - usleep(1000); + while (running) { + f << id++ << " " << ads1115.getLastSample() << endl; + upm_delay_us(1000); } // Clean-up and exit timer.join(); f.close(); - delete ads1115; - } catch (ios_base::failure &e) { + } catch (ios_base::failure& e) { cout << "Failed to write to file: " << e.what() << endl; return 1; } @@ -104,4 +107,3 @@ int main() //! [Interesting] return 0; } - diff --git a/examples/c++/ads1x15.cxx b/examples/c++/ads1x15.cxx index 7ff2d0cb..b41f303e 100644 --- a/examples/c++/ads1x15.cxx +++ b/examples/c++/ads1x15.cxx @@ -22,301 +22,306 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "mraa.hpp" - #include -#include + #include "ads1015.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; - using namespace upm; - int command; - //! [Interesting] - //Select the device you are testing here and adjust case 6 for the correct sample rates. - //upm::ADS1015 *ads = new upm::ADS1015(1); - upm::ADS1115 *ads = new upm::ADS1115(1, 0x49); - float inputVoltage; - int ans; + do { + cout << endl; + cout << "1 - get Conversion \t"; + cout << "2 - get last conversion" << endl; + cout << "3 - get Gain \t\t"; + cout << "4 - set Gain" << endl; + ; + cout << "5 - get Data Rate \t"; + 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 - { - - cout << endl; - cout << "1 - get Conversion \t" ; - cout << "2 - get last conversion" << endl; - cout << "3 - get Gain \t\t"; - cout << "4 - set Gain" << endl;; - cout << "5 - get Data Rate \t"; - 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; - - - switch(command) - { + switch (command) { + case 2: + cout << ads.getLastSample() << endl; + break; + case 3: + cout << std::hex << ads.getGain() << endl; + break; + case 5: + cout << std::hex << ads.getSPS() << endl; + break; + case 4: + int gain; + ADS1015::ADSGAIN set_gain; + cout << "select one of the following:" << endl; + cout << "1 -> gain 2/3 \t 2 -> gain1 \t 3 -> gain 2" << endl; + 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; + 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: - cout << ads->getLastSample() << endl; + set_rate = ADS1015::SPS_250; break; case 3: - cout << std::hex << ads->getGain() << endl; - break; - case 5: - cout << std::hex << ads->getSPS() << endl; + set_rate = ADS1015::SPS_490; break; case 4: - int gain; - ADS1015::ADSGAIN set_gain; - cout << "select one of the following:" << endl; - cout << "1 -> gain 2/3 \t 2 -> gain1 \t 3 -> gain 2" << endl; - 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); + set_rate = ADS1015::SPS_920; + break; + case 5: + set_rate = ADS1015::SPS_1600; 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: - 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; + set_rate = ADS1015::SPS_2400; 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: + 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; + } - 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; } diff --git a/examples/c++/adxl335.cxx b/examples/c++/adxl335.cxx index 8152012a..74b56a67 100644 --- a/examples/c++/adxl335.cxx +++ b/examples/c++/adxl335.cxx @@ -22,57 +22,58 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "adxl335.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2 - upm::ADXL335* accel = new upm::ADXL335(0, 1, 2); + //! [Interesting] + // Instantiate an ADXL335 accelerometer on analog pins A0, A1, and A2 + upm::ADXL335 accel(0, 1, 2); - cout << "Please make sure the sensor is completely still. Sleeping for" - << " 2 seconds." << endl; - sleep(2); - cout << "Calibrating..." << endl; + cout << "Please make sure the sensor is completely still. Sleeping for" + << " 2 seconds." << endl; + upm_delay(2); + cout << "Calibrating..." << endl; - accel->calibrate(); - - while (shouldRun) - { - int x, y, z; - float aX, aY, aZ; + accel.calibrate(); - accel->values(&x, &y, &z); - cout << "Raw Values: X: " << x << " Y: " << y << " Z: " << z << endl; + while (shouldRun) { + int x, y, z; + float aX, aY, aZ; - 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; + accel.values(&x, &y, &z); + cout << "Raw Values: X: " << x << " Y: " << y << " Z: " << z << 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; } diff --git a/examples/c++/adxl345.cxx b/examples/c++/adxl345.cxx index 851bc770..29c515a9 100644 --- a/examples/c++/adxl345.cxx +++ b/examples/c++/adxl345.cxx @@ -22,30 +22,32 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include + #include "adxl345.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - int16_t *raw; - float *acc; + //! [Interesting] + int16_t* raw; + float* acc; // 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){ - accel->update(); // Update the data - raw = accel->getRawValues(); // Read raw sensor data - acc = accel->getAcceleration(); // Read acceleration (g) - fprintf(stdout, "Current scale: 0x%2xg\n", accel->getScale()); + while (true) { + accel.update(); // Update the data + raw = accel.getRawValues(); // Read raw sensor data + acc = accel.getAcceleration(); // Read acceleration (g) + fprintf(stdout, "Current scale: 0x%2xg\n", accel.getScale()); fprintf(stdout, "Raw: %6d %6d %6d\n", raw[0], raw[1], raw[2]); fprintf(stdout, "AccX: %5.2f g\n", acc[0]); fprintf(stdout, "AccY: %5.2f g\n", acc[1]); fprintf(stdout, "AccZ: %5.2f g\n", acc[2]); - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/adxrs610.cxx b/examples/c++/adxrs610.cxx index f24ccd5d..0651d2cf 100644 --- a/examples/c++/adxrs610.cxx +++ b/examples/c++/adxrs610.cxx @@ -22,50 +22,51 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "adxrs610.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 - // analog A1 (temp out) with an analog reference voltage of - // 5.0 - upm::ADXRS610 *sensor = new upm::ADXRS610(0, 1, 5.0); - - // set a deadband region around the zero point to report 0.0 (optional) - sensor->setDeadband(0.015); + // Instantiate a ADXRS610 sensor on analog pin A0 (dataout), and + // analog A1 (temp out) with an analog reference voltage of + // 5.0 + upm::ADXRS610 sensor(0, 1, 5.0); - // Every tenth of a second, sample the ADXRS610 and output it's - // corresponding temperature and angular velocity + // set a deadband region around the zero point to report 0.0 (optional) + sensor.setDeadband(0.015); - while (shouldRun) - { - cout << "Vel (deg/s): " << sensor->getAngularVelocity() << endl; - cout << "Temp (C): " << sensor->getTemperature() << endl; - - usleep(100000); + // Every tenth of a second, sample the ADXRS610 and output it's + // corresponding temperature and angular velocity + + while (shouldRun) { + cout << "Vel (deg/s): " << sensor.getAngularVelocity() << endl; + cout << "Temp (C): " << sensor.getTemperature() << endl; + + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/am2315.cxx b/examples/c++/am2315.cxx index 56f27ccc..30c3854f 100644 --- a/examples/c++/am2315.cxx +++ b/examples/c++/am2315.cxx @@ -22,16 +22,15 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "am2315.hpp" +#include "upm_utilities.h" volatile int doWork = 0; -upm::AM2315 *sensor = NULL; - void sig_handler(int signo) { @@ -42,34 +41,30 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { // Register signal handler signal(SIGINT, sig_handler); //! [Interesting] - float humidity = 0.0; + float humidity = 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) { - humidity = sensor->getHumidity(); - temperature = sensor->getTemperature(); + humidity = sensor.getHumidity(); + temperature = sensor.getTemperature(); - std::cout << "humidity value = " << - humidity << - ", temperature value = " << - temperature << std::endl; - usleep (500000); + std::cout << "humidity value = " << humidity << ", temperature value = " << temperature + << std::endl; + upm_delay_us(500000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/apa102.cxx b/examples/c++/apa102.cxx index 01aa963d..c2f26b56 100644 --- a/examples/c++/apa102.cxx +++ b/examples/c++/apa102.cxx @@ -23,9 +23,6 @@ */ #include "apa102.hpp" -#include -#include -#include using namespace std; @@ -34,18 +31,17 @@ main(int argc, char** argv) { //! [Interesting] // 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 - ledStrip->setAllLeds(31, 255, 0, 0); + ledStrip.setAllLeds(31, 255, 0, 0); // 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 - ledStrip->setLed(15, 31, 0, 255, 0); + ledStrip.setLed(15, 31, 0, 255, 0); - delete ledStrip; //! [Interesting] return 0; } diff --git a/examples/c++/apds9002.cxx b/examples/c++/apds9002.cxx index 345f53ab..b4dea649 100644 --- a/examples/c++/apds9002.cxx +++ b/examples/c++/apds9002.cxx @@ -22,41 +22,41 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "apds9002.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Luminance sensor on analog pin A0 - upm::APDS9002* luminance = new upm::APDS9002(0); - - while (shouldRun) - { - int val = luminance->value(); - cout << "Luminance value is " << val << endl; + //! [Interesting] + // Instantiate a Grove Luminance sensor on analog pin A0 + upm::APDS9002 luminance(0); - 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; } diff --git a/examples/c++/apds9930.cxx b/examples/c++/apds9930.cxx index 37520693..fd4cdf37 100644 --- a/examples/c++/apds9930.cxx +++ b/examples/c++/apds9930.cxx @@ -22,10 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "apds9930.hpp" +#include "upm_utilities.h" using namespace std; @@ -44,31 +45,30 @@ main() signal(SIGINT, sig_handler); //! [Interesting] // 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 - light_proximity->enableIlluminance(true); + // Kernel driver implement upm_delay 5000-5100us after enable illuminance + // sensor + light_proximity.enableIlluminance(true); - // Kernel driver implement sleep 5000-5100us after enable proximity sensor - light_proximity->enableProximity(true); + // Kernel driver implement upm_delay 5000-5100us after enable proximity sensor + light_proximity.enableProximity(true); // Tested this value works. Please change it on your platform - usleep(120000); + upm_delay_us(120000); while (shouldRun) { - float lux = light_proximity->getAmbient(); + float lux = light_proximity.getAmbient(); cout << "Luminance value is " << lux << endl; - float proximity = light_proximity->getProximity(); + float proximity = light_proximity.getProximity(); cout << "Proximity value is " << proximity << endl; - sleep(1); + upm_delay(1); } - light_proximity->enableProximity(false); - light_proximity->enableIlluminance(false); + light_proximity.enableProximity(false); + light_proximity.enableIlluminance(false); //! [Interesting] cout << "Exiting" << endl; - delete light_proximity; - return 0; } diff --git a/examples/c++/at42qt1070.cxx b/examples/c++/at42qt1070.cxx index b1986491..6acfb483 100644 --- a/examples/c++/at42qt1070.cxx +++ b/examples/c++/at42qt1070.cxx @@ -22,69 +22,68 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include + #include "at42qt1070.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -void printButtons(upm::AT42QT1070 *touch) +void +printButtons(upm::AT42QT1070& touch) { - bool buttonPressed = false; - uint8_t buttons = touch->getButtons(); + bool buttonPressed = false; + uint8_t buttons = touch.getButtons(); - cout << "Buttons Pressed: "; - for (int i=0; i<7; i++) - { - if (buttons & (1 << i)) - { - cout << i << " "; - buttonPressed = true; + cout << "Buttons Pressed: "; + for (int i = 0; i < 7; i++) { + if (buttons & (1 << i)) { + cout << i << " "; + buttonPressed = true; } } - if (!buttonPressed) - cout << "None"; + if (!buttonPressed) + cout << "None"; - cout << endl; + cout << endl; - if (touch->isCalibrating()) - cout << "Calibration is occurring." << endl; + if (touch.isCalibrating()) + cout << "Calibration is occurring." << endl; - if (touch->isOverflowed()) - cout << "Overflow was detected." << endl; + if (touch.isOverflowed()) + 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] - // Instantiate an AT42QT1070 on I2C bus 0 + //! [Interesting] + // Instantiate an AT42QT1070 on I2C bus 0 - upm::AT42QT1070 *touch = new upm::AT42QT1070(AT42QT1070_I2C_BUS, - AT42QT1070_DEFAULT_I2C_ADDR); + upm::AT42QT1070 touch(AT42QT1070_I2C_BUS, AT42QT1070_DEFAULT_I2C_ADDR); - while (shouldRun) - { - touch->updateState(); - printButtons(touch); - usleep(100000); + while (shouldRun) { + touch.updateState(); + printButtons(touch); + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete touch; - return 0; + return 0; } diff --git a/examples/c++/bh1750.cxx b/examples/c++/bh1750.cxx index fb804605..c5658739 100644 --- a/examples/c++/bh1750.cxx +++ b/examples/c++/bh1750.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bh1750.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 - // the default I2C address (0x23), and setting the mode to highest - // resolution, lowest power mode). - upm::BH1750 *sensor = new upm::BH1750(); + // Instantiate a BH1750 sensor using defaults (I2C bus (0), using + // the default I2C address (0x23), and setting the mode to highest + // resolution, lowest power mode). + 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) - { - cout << "Detected Light Level (lux): " << sensor->getLux() << endl; - sleep(1); + while (shouldRun) { + cout << "Detected Light Level (lux): " << sensor.getLux() << endl; + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/biss0001.cxx b/examples/c++/biss0001.cxx index 2468804b..91d55cd7 100644 --- a/examples/c++/biss0001.cxx +++ b/examples/c++/biss0001.cxx @@ -22,47 +22,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "biss0001.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Motion sensor on GPIO pin D2 - upm::BISS0001* motion = new upm::BISS0001(2); - - while (shouldRun) - { - bool val = motion->value(); + //! [Interesting] + // Instantiate a Grove Motion sensor on GPIO pin D2 + upm::BISS0001 motion(2); - if (val) - cout << "Detecting moving object"; - else - cout << "No moving objects detected"; + while (shouldRun) { + bool val = motion.value(); - cout << endl; + if (val) + cout << "Detecting moving object"; + else + cout << "No moving objects detected"; - sleep(1); - } -//! [Interesting] + cout << endl; - cout << "Exiting" << endl; + upm_delay(1); + } + //! [Interesting] - delete motion; - return 0; + cout << "Exiting" << endl; + + return 0; } diff --git a/examples/c++/bma220.cxx b/examples/c++/bma220.cxx index 9701510b..34ca7ec1 100644 --- a/examples/c++/bma220.cxx +++ b/examples/c++/bma220.cxx @@ -22,49 +22,48 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bma220.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMA220 using default parameters (bus 0, addr 0x0a) - upm::BMA220 *sensor = new upm::BMA220(); + // Instantiate an BMA220 using default parameters (bus 0, addr 0x0a) + upm::BMA220 sensor; - // Output data every half second until interrupted - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getAccelerometer(&x, &y, &z); - cout << "Accelerometer: "; - cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + // Output data every half second until interrupted + while (shouldRun) { + sensor.update(); - 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; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/bma250e.cxx b/examples/c++/bma250e.cxx index e3ba0f4b..11981fab 100644 --- a/examples/c++/bma250e.cxx +++ b/examples/c++/bma250e.cxx @@ -22,60 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bma250e.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMA250E using default I2C parameters - upm::BMA250E sensor; + // Instantiate an BMA250E using default I2C parameters + upm::BMA250E sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin - // for CS: BMA250E(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin + // for CS: BMA250E(0, -1, 10); - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/bmg160.cxx b/examples/c++/bmg160.cxx index ff8ed637..d1f93f00 100644 --- a/examples/c++/bmg160.cxx +++ b/examples/c++/bmg160.cxx @@ -22,60 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmg160.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMG160 using default I2C parameters - upm::BMG160 sensor; + // Instantiate an BMG160 using default I2C parameters + upm::BMG160 sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin - // for CS: BMG160(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin + // for CS: BMG160(0, -1, 10); - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getGyroscope(&x, &y, &z); - cout << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " degrees/s" - << endl; + sensor.getGyroscope(&x, &y, &z); + cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " degrees/s" << endl; - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/bmi160.cxx b/examples/c++/bmi160.cxx index 6f98556c..90baf8ed 100644 --- a/examples/c++/bmi160.cxx +++ b/examples/c++/bmi160.cxx @@ -22,61 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmi160.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate a BMI160 instance using default i2c bus and address - upm::BMI160 *sensor = new upm::BMI160(); + // Instantiate a BMI160 instance using default i2c bus and address + upm::BMI160 sensor; - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + while (shouldRun) { + // update our values from the sensor + sensor.update(); - float dataX, dataY, dataZ; + float dataX, dataY, dataZ; - sensor->getAccelerometer(&dataX, &dataY, &dataZ); - cout << "Accelerometer: "; - cout << "AX: " << dataX << " AY: " << dataY << " AZ: " - << dataZ << endl; + sensor.getAccelerometer(&dataX, &dataY, &dataZ); + cout << "Accelerometer: "; + cout << "AX: " << dataX << " AY: " << dataY << " AZ: " << dataZ << endl; - sensor->getGyroscope(&dataX, &dataY, &dataZ); - cout << "Gryoscope: "; - cout << "GX: " << dataX << " GY: " << dataY << " GZ: " - << dataZ << endl; + sensor.getGyroscope(&dataX, &dataY, &dataZ); + cout << "Gryoscope: "; + cout << "GX: " << dataX << " GY: " << dataY << " GZ: " << dataZ << endl; - sensor->getMagnetometer(&dataX, &dataY, &dataZ); - cout << "Magnetometer: "; - cout << "MX: " << dataX << " MY: " << dataY << " MZ: " - << dataZ << endl; + sensor.getMagnetometer(&dataX, &dataY, &dataZ); + cout << "Magnetometer: "; + cout << "MX: " << dataX << " MY: " << dataY << " MZ: " << 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; } diff --git a/examples/c++/bmm150.cxx b/examples/c++/bmm150.cxx index 7bca4558..1af97c6b 100644 --- a/examples/c++/bmm150.cxx +++ b/examples/c++/bmm150.cxx @@ -22,55 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmm150.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMM150 using default I2C parameters - upm::BMM150 sensor; + // Instantiate an BMM150 using default I2C parameters + upm::BMM150 sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin - // for CS: BMM150(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin + // for CS: BMM150(0, -1, 10); - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getMagnetometer(&x, &y, &z); - cout << "Magnetometer x: " << x - << " y: " << y - << " z: " << z - << " uT" - << endl; + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer x: " << x << " 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; } diff --git a/examples/c++/bmp280-bme280.cxx b/examples/c++/bmp280-bme280.cxx index 23fdfb04..1fc3f18b 100644 --- a/examples/c++/bmp280-bme280.cxx +++ b/examples/c++/bmp280-bme280.cxx @@ -22,57 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "bme280.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate a BME280 instance using default i2c bus and address - upm::BME280 *sensor = new upm::BME280(); + // Instantiate a BME280 instance using default i2c bus and address + upm::BME280 sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: - // BME280(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: + // BME280(0, -1, 10); - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; - cout << "Pressure: " << sensor->getPressure() << " Pa" << endl; - cout << "Computed Altitude: " << sensor->getAltitude() << " m" << endl; - cout << "Humidity: " << sensor->getHumidity() << " %RH" << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; + cout << "Pressure: " << sensor.getPressure() << " Pa" << endl; + cout << "Computed Altitude: " << sensor.getAltitude() << " m" << 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; } diff --git a/examples/c++/bmp280.cxx b/examples/c++/bmp280.cxx index 334edb95..f98c05a4 100644 --- a/examples/c++/bmp280.cxx +++ b/examples/c++/bmp280.cxx @@ -24,56 +24,53 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "bmp280.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate a BMP280 instance using default i2c bus and address - upm::BMP280 *sensor = new upm::BMP280(); + // Instantiate a BMP280 instance using default i2c bus and address + upm::BMP280 sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: - // BMP280(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: + // BMP280(0, -1, 10); - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; - cout << "Pressure: " << sensor->getPressure() << " Pa" << endl; - cout << "Computed Altitude: " << sensor->getAltitude() << " m" << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; + cout << "Pressure: " << sensor.getPressure() << " Pa" << 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; } diff --git a/examples/c++/bmpx8x.cxx b/examples/c++/bmpx8x.cxx index 294a52a2..f4759d02 100644 --- a/examples/c++/bmpx8x.cxx +++ b/examples/c++/bmpx8x.cxx @@ -27,52 +27,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "bmpx8x.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a BMPX8X sensor on I2C using defaults. upm::BMPX8X sensor; // Print the pressure, altitude, sea level, and // temperature values every 0.5 seconds - while (shouldRun) - { + while (shouldRun) { sensor.update(); - cout << "Pressure: " - << sensor.getPressure() - << " Pa, Temperature: " - << sensor.getTemperature() - << " C, Altitude: " - << sensor.getAltitude() - << " m, Sea level: " - << sensor.getSealevelPressure() - << " Pa" - << endl; + cout << "Pressure: " << sensor.getPressure() + << " Pa, Temperature: " << sensor.getTemperature() + << " C, Altitude: " << sensor.getAltitude() + << " m, Sea level: " << sensor.getSealevelPressure() << " Pa" << endl; - usleep(500000); + upm_delay_us(500000); } cout << "Exiting..." << endl; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/bmx055-bmc150.cxx b/examples/c++/bmx055-bmc150.cxx index 83dfdefc..3c8e8668 100644 --- a/examples/c++/bmx055-bmc150.cxx +++ b/examples/c++/bmx055-bmc150.cxx @@ -22,59 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmc150.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMC150 using default I2C parameters - upm::BMC150 sensor; + // Instantiate an BMC150 using default I2C parameters + upm::BMC150 sensor; - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getMagnetometer(&x, &y, &z); - cout << "Magnetometer x: " << x - << " y: " << y - << " z: " << z - << " uT" - << endl; + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer x: " << x << " 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; } diff --git a/examples/c++/bmx055-bmi055.cxx b/examples/c++/bmx055-bmi055.cxx index 12ac1a55..761d4653 100644 --- a/examples/c++/bmx055-bmi055.cxx +++ b/examples/c++/bmx055-bmi055.cxx @@ -22,59 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmi055.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMI055 using default I2C parameters - upm::BMI055 sensor; + // Instantiate an BMI055 using default I2C parameters + upm::BMI055 sensor; - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getGyroscope(&x, &y, &z); - cout << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " degrees/s" - << endl; + sensor.getGyroscope(&x, &y, &z); + cout << "Gyroscope x: " << x << " 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; } diff --git a/examples/c++/bmx055.cxx b/examples/c++/bmx055.cxx index cb90a9f2..4218a7e2 100644 --- a/examples/c++/bmx055.cxx +++ b/examples/c++/bmx055.cxx @@ -22,66 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bmx055.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BMX055 using default I2C parameters - upm::BMX055 sensor; + // Instantiate an BMX055 using default I2C parameters + upm::BMX055 sensor; - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getGyroscope(&x, &y, &z); - cout << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " degrees/s" - << endl; + sensor.getGyroscope(&x, &y, &z); + cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " degrees/s" << endl; - sensor.getMagnetometer(&x, &y, &z); - cout << "Magnetometer x: " << x - << " y: " << y - << " z: " << z - << " uT" - << endl; + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer x: " << x << " 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; } diff --git a/examples/c++/bno055.cxx b/examples/c++/bno055.cxx index b46999ec..d346af9e 100644 --- a/examples/c++/bno055.cxx +++ b/examples/c++/bno055.cxx @@ -22,108 +22,82 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "bno055.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an BNO055 using default parameters (bus 0, addr - // 0x28). The default running mode is NDOF absolute orientation - // mode. - upm::BNO055 *sensor = new upm::BNO055(); + // Instantiate an BNO055 using default parameters (bus 0, addr + // 0x28). The default running mode is NDOF absolute orientation + // mode. + upm::BNO055 sensor; - // First we need to calibrate.... - cout << "First we need to calibrate. 4 numbers will be output every" - << endl; - cout << "second for each sensor. 0 means uncalibrated, and 3 means" - << endl; - cout << "fully calibrated." - << endl; - cout << "See the UPM documentation on this sensor for instructions on" - << endl; - cout << "what actions are required to calibrate." - << endl; - cout << endl; + // First we need to calibrate.... + cout << "First we need to calibrate. 4 numbers will be output every" << endl; + cout << "second for each sensor. 0 means uncalibrated, and 3 means" << endl; + cout << "fully calibrated." << 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... - while (shouldRun && !sensor->isFullyCalibrated()) - { - int mag, acc, gyr, sys; - sensor->getCalibrationStatus(&mag, &acc, &gyr, &sys); + // do the calibration... + while (shouldRun && !sensor.isFullyCalibrated()) { + int mag, acc, gyr, sys; + sensor.getCalibrationStatus(&mag, &acc, &gyr, &sys); - cout << "Magnetometer: " << mag - << " Accelerometer: " << acc - << " Gyroscope: " << gyr - << " System: " << sys - << endl; + cout << "Magnetometer: " << mag << " Accelerometer: " << acc << " Gyroscope: " << gyr + << " System: " << sys << endl; - sleep(1); + upm_delay(1); } - cout << endl; - cout << "Calibration complete." << endl; - cout << endl; + cout << endl; + cout << "Calibration complete." << endl; + cout << endl; - // now output various fusion data every 250 milliseconds - while (shouldRun) - { - float w, x, y, z; + // now output various fusion data every 250 milliseconds + while (shouldRun) { + float w, x, y, z; - sensor->update(); + sensor.update(); - sensor->getEulerAngles(&x, &y, &z); - cout << "Euler: Heading: " << x - << " Roll: " << y - << " Pitch: " << z - << " degrees" - << endl; + sensor.getEulerAngles(&x, &y, &z); + cout << "Euler: Heading: " << x << " Roll: " << y << " Pitch: " << z << " degrees" << endl; - sensor->getQuaternions(&w, &x, &y, &z); - cout << "Quaternion: W: " << w - << " X: " << x - << " Y: " << y - << " Z: " << z - << endl; + sensor.getQuaternions(&w, &x, &y, &z); + cout << "Quaternion: W: " << w << " X: " << x << " Y: " << y << " Z: " << z << endl; - sensor->getLinearAcceleration(&x, &y, &z); - cout << "Linear Acceleration: X: " << x - << " Y: " << y - << " Z: " << z - << " m/s^2" - << endl; + sensor.getLinearAcceleration(&x, &y, &z); + cout << "Linear Acceleration: X: " << x << " Y: " << y << " Z: " << z << " m/s^2" << endl; - sensor->getGravityVectors(&x, &y, &z); - cout << "Gravity Vector: X: " << x - << " Y: " << y - << " Z: " << z - << " m/s^2" - << endl; + sensor.getGravityVectors(&x, &y, &z); + cout << "Gravity Vector: X: " << x << " Y: " << y << " Z: " << z << " m/s^2" << endl; - cout << endl; - usleep(250000); + cout << endl; + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/button.cxx b/examples/c++/button.cxx index 8e12d463..d5903b2f 100644 --- a/examples/c++/button.cxx +++ b/examples/c++/button.cxx @@ -22,28 +22,28 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "button.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { // This example uses GPIO 0 -//! [Interesting] + //! [Interesting] // 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 - while( 1 ) { - std::cout << button->name() << " value is " << button->value() << std::endl; - sleep(1); + while (1) { + std::cout << button.name() << " value is " << button.value() << std::endl; + upm_delay(1); } // Delete the button object - delete button; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/buzzer.cxx b/examples/c++/buzzer.cxx index 411af0f7..dc9465be 100644 --- a/examples/c++/buzzer.cxx +++ b/examples/c++/buzzer.cxx @@ -22,37 +22,32 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include + #include "buzzer.hpp" +#include "buzzer_tones.h" #include "upm_utilities.h" - int -main(int argc, char **argv) { +main(int argc, char** argv) +{ //! [Interesting] - int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, - BUZZER_FA, BUZZER_SOL, BUZZER_LA, - BUZZER_SI }; + int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, BUZZER_FA, BUZZER_SOL, BUZZER_LA, BUZZER_SI }; // create Buzzer instance - upm::Buzzer* sound = new upm::Buzzer(5); + upm::Buzzer sound(5); // 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 // for 0.1 seconds between notes - for (int chord_ind = 0; chord_ind < 7; chord_ind++) - { - std::cout << sound->playSound(chord[chord_ind], 500000) << std::endl; + for (int chord_ind = 0; chord_ind < 7; chord_ind++) { + std::cout << sound.playSound(chord[chord_ind], 500000) << std::endl; upm_delay_ms(100); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sound; - return 0; } diff --git a/examples/c++/cjq4435.cxx b/examples/c++/cjq4435.cxx index fe9fd882..c0d30baf 100644 --- a/examples/c++/cjq4435.cxx +++ b/examples/c++/cjq4435.cxx @@ -22,56 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "cjq4435.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main () +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // 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->enable(true); + mosfet.setPeriodMS(10); + mosfet.enable(true); - while (shouldRun) - { + while (shouldRun) { // 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) - { - mosfet->setDutyCycle(i); - usleep(100000); + for (float i = 0.0; i <= 1.0; i += 0.1) { + mosfet.setDutyCycle(i); + upm_delay_us(100000); } - sleep(1); + upm_delay(1); // Now take it back down // 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) - { - mosfet->setDutyCycle(i); - usleep(100000); + for (float i = 1.0; i >= 0.0; i -= 0.1) { + mosfet.setDutyCycle(i); + upm_delay_us(100000); } - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; - delete mosfet; return 0; } diff --git a/examples/c++/collision.cxx b/examples/c++/collision.cxx index 0d9f19fc..81122518 100644 --- a/examples/c++/collision.cxx +++ b/examples/c++/collision.cxx @@ -22,48 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include + #include "collision.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the Collision Sensor - // Instantiate a Collision on digital pin D2 - upm::Collision* collision = new upm::Collision(2); + //! [Interesting] + // The was tested with the Collision Sensor + // Instantiate a Collision on digital pin D2 + upm::Collision collision(2); - bool collisionState = false; - cout << "No collision" << endl; - while (shouldRun) - { - if (collision->isColliding() && !collisionState) - { - cout << "Collision!" << endl; - collisionState = true; - } - else if (collisionState) - { - cout << "No collision" << endl; - collisionState = false; - } - } + bool collisionState = false; + cout << "No collision" << endl; + while (shouldRun) { + if (collision.isColliding() && !collisionState) { + cout << "Collision!" << endl; + collisionState = true; + } else if (collisionState) { + cout << "No collision" << endl; + collisionState = false; + } + } -//! [Interesting] - cout << "Exiting" << endl; + //! [Interesting] + cout << "Exiting" << endl; - delete collision; - return 0; + return 0; } diff --git a/examples/c++/curieimu.cxx b/examples/c++/curieimu.cxx index a9222d48..dda0d285 100644 --- a/examples/c++/curieimu.cxx +++ b/examples/c++/curieimu.cxx @@ -24,58 +24,49 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include "curieimu.hpp" #include "mraa.h" #include "mraa/firmata.h" +#include #include +#include int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] mraa_init(); 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(); - int x = sensor->getAccelX(), - y = sensor->getAccelY(), - z = sensor->getAccelZ(); + sensor.updateAccel(); + int x = sensor.getAccelX(), y = sensor.getAccelY(), z = sensor.getAccelZ(); printf("accelerometer is: %d, %d, %d\n", x, y, z); - sensor->updateGyro(); - int a = sensor->getGyroX(), - b = sensor->getGyroY(), - c = sensor->getGyroZ(); + sensor.updateGyro(); + int a = sensor.getGyroX(), b = sensor.getGyroY(), c = sensor.getGyroZ(); printf("gyroscope is: %d, %d, %d\n", a, b, c); int axis, direction; - sensor->enableShockDetection(true); - for(int i=0; i<300; i++) { - if (sensor->isShockDetected()) { - sensor->getNextShock(); - axis = sensor->getAxis(); - direction = sensor->getDirection(); - printf("shock data is: %d, %d\n", axis, direction); - } - usleep(10000); + sensor.enableShockDetection(true); + for (int i = 0; i < 300; i++) { + if (sensor.isShockDetected()) { + sensor.getNextShock(); + axis = sensor.getAxis(); + direction = sensor.getDirection(); + printf("shock data is: %d, %d\n", axis, direction); + } + upm_delay_us(10000); } - sensor->updateMotion(); - int m = sensor->getAccelX(), - n = sensor->getAccelY(), - o = sensor->getAccelZ(), - p = sensor->getGyroX(), - q = sensor->getGyroY(), - r = sensor->getGyroZ(); + sensor.updateMotion(); + int m = sensor.getAccelX(), n = sensor.getAccelY(), 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); - delete sensor; //! [Interesting] return 0; diff --git a/examples/c++/cwlsxxa.cxx b/examples/c++/cwlsxxa.cxx index b96c85e9..d4a2ebfb 100644 --- a/examples/c++/cwlsxxa.cxx +++ b/examples/c++/cwlsxxa.cxx @@ -22,61 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "cwlsxxa.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 - // humidity and A2 for temperature - upm::CWLSXXA *sensor = new upm::CWLSXXA(0, 1, 2); + // Instantiate an CWLSXXA instance, using A0 for CO2, A1 for + // humidity and A2 for temperature + upm::CWLSXXA sensor(0, 1, 2); - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor->getHumidity() - << " %" << endl; + cout << "Humidity: " << sensor.getHumidity() << " %" << endl; - cout << "CO2: " << sensor->getCO2() - << " ppm" << endl; + cout << "CO2: " << sensor.getCO2() << " 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; } diff --git a/examples/c++/dfrec.cxx b/examples/c++/dfrec.cxx index 90b51c3f..00dd84cd 100644 --- a/examples/c++/dfrec.cxx +++ b/examples/c++/dfrec.cxx @@ -22,60 +22,53 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "dfrec.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 - // temperature sensor connected to UART 0, and a device index (for - // the ds1820b uart bus) of 0, and an analog reference voltage of - // 5.0. - upm::DFREC *sensor = new upm::DFREC(0, 0, 0, 5.0); + // Instantiate a DFRobot EC sensor on analog pin A0, with a ds18b20 + // temperature sensor connected to UART 0, and a device index (for + // the ds1820b uart bus) of 0, and an analog reference voltage of + // 5.0. + upm::DFREC sensor(0, 0, 0, 5.0); - // Every 2 seconds, update and print values - while (shouldRun) - { - sensor->update(); + // Every 2 seconds, update and print values + while (shouldRun) { + sensor.update(); - cout << "EC = " - << sensor->getEC() - << " ms/cm" - << endl; + cout << "EC = " << sensor.getEC() << " ms/cm" << endl; - cout << "Volts = " - << sensor->getVolts() - << ", Temperature = " - << sensor->getTemperature() - << " C" - << endl; + cout << "Volts = " << sensor.getVolts() << ", 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; } diff --git a/examples/c++/dfrorp.cxx b/examples/c++/dfrorp.cxx index f24dc296..3b4f2d6b 100644 --- a/examples/c++/dfrorp.cxx +++ b/examples/c++/dfrorp.cxx @@ -22,66 +22,63 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "dfrorp.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 - // reference voltage of 5.0. - upm::DFRORP *sensor = new upm::DFRORP(0, 5.0); + // Instantiate a DFRobot ORP sensor on analog pin A0 with an analog + // reference voltage of 5.0. + upm::DFRORP sensor(0, 5.0); - // To calibrate: - // - // Disconnect the sensor probe (but leave the sensor interface board - // connected). Then run one of the examples while holding down the - // 'calibrate' button on the device. Read the ORP value reported - // (it should be fairly small). - // - // This value is what you should supply to setCalibrationOffset(). - // Then reconnect the probe to the interface board and you should be - // ready to go. - // - // DO NOT press the calibrate button on the interface board while - // the probe is attached or you can permanently damage the probe. - sensor->setCalibrationOffset(0.97); + // To calibrate: + // + // Disconnect the sensor probe (but leave the sensor interface board + // connected). Then run one of the examples while holding down the + // 'calibrate' button on the device. Read the ORP value reported + // (it should be fairly small). + // + // This value is what you should supply to setCalibrationOffset(). + // Then reconnect the probe to the interface board and you should be + // ready to go. + // + // DO NOT press the calibrate button on the interface board while + // the probe is attached or you can permanently damage the probe. + sensor.setCalibrationOffset(0.97); - // Every second, update and print values - while (shouldRun) - { - sensor->update(); + // Every second, update and print values + while (shouldRun) { + sensor.update(); - cout << "ORP: " - << sensor->getORP() - << " mV" - << endl; + cout << "ORP: " << 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; } diff --git a/examples/c++/dfrph.cxx b/examples/c++/dfrph.cxx index 22d3db51..30dfc4f1 100644 --- a/examples/c++/dfrph.cxx +++ b/examples/c++/dfrph.cxx @@ -22,55 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "dfrph.hpp" +#include "upm_utilities.h" using namespace std; 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) - shouldRun = false; + if (signo == SIGINT) + 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 - // reference voltage of DFRPH_AREF - upm::DFRPH *sensor = new upm::DFRPH(0, DFRPH_AREF); - + // Instantiate a DFRPH sensor on analog pin A0, with an analog + // reference voltage of DFRPH_AREF + upm::DFRPH sensor(0, DFRPH_AREF); - // After calibration, set the offset (based on calibration with a pH - // 7.0 buffer solution). See the UPM sensor documentation for - // calibrations instructions. - sensor->setOffset(0.065); + // After calibration, set the offset (based on calibration with a pH + // 7.0 buffer solution). See the UPM sensor documentation for + // calibrations instructions. + sensor.setOffset(0.065); - // Every second, sample the pH and output it's corresponding - // analog voltage. + // Every second, sample the pH and output it's corresponding + // analog voltage. - while (shouldRun) - { - cout << "Detected volts: " << sensor->volts() << endl; - cout << "pH value: " << sensor->pH() << endl; - cout << endl; + while (shouldRun) { + cout << "Detected volts: " << sensor.volts() << endl; + cout << "pH value: " << sensor.pH() << endl; + cout << endl; - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/ds1307.cxx b/examples/c++/ds1307.cxx index 0dcc3aa5..c5a8deb0 100644 --- a/examples/c++/ds1307.cxx +++ b/examples/c++/ds1307.cxx @@ -22,56 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include + #include "ds1307.hpp" using namespace std; -void printTime(upm::DS1307 *rtc) +void +printTime(upm::DS1307& rtc) { - cout << "The time is: " << - rtc->month << "/" << rtc->dayOfMonth << "/" << rtc->year << " " - << rtc->hours << ":" << rtc->minutes << ":" << rtc->seconds; + cout << "The time is: " << rtc.month << "/" << rtc.dayOfMonth << "/" << rtc.year << " " + << rtc.hours << ":" << rtc.minutes << ":" << rtc.seconds; - if (rtc->amPmMode) - cout << ((rtc->pm) ? " PM " : " AM "); + if (rtc.amPmMode) + cout << ((rtc.pm) ? " PM " : " AM "); - cout << endl; + cout << endl; - cout << "Clock is in " << ((rtc->amPmMode) ? "AM/PM mode" : "24hr mode") - << endl; + cout << "Clock is in " << ((rtc.amPmMode) ? "AM/PM mode" : "24hr mode") << endl; } int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a DS1037 on I2C bus 0 - upm::DS1307 *rtc = new upm::DS1307(0); - - // always do this first - cout << "Loading the current time... " << endl; - if (!rtc->loadTime()) - { - cerr << "rtc->loadTime() failed." << endl; - return 0; + //! [Interesting] + // Instantiate a DS1037 on I2C bus 0 + upm::DS1307 rtc(0); + + // always do this first + cout << "Loading the current time... " << endl; + if (!rtc.loadTime()) { + cerr << "rtc.loadTime() failed." << endl; + return 0; } - - printTime(rtc); - // set the year as an example - cout << "setting the year to 50" << endl; - rtc->year = 50; + printTime(rtc); - 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->loadTime(); - printTime(rtc); + rtc.setTime(); - //! [Interesting] - - delete rtc; - return 0; + // reload the time and print it + rtc.loadTime(); + printTime(rtc); + + //! [Interesting] + + return 0; } diff --git a/examples/c++/ds1808lc.cxx b/examples/c++/ds1808lc.cxx index 944415d6..da245dec 100644 --- a/examples/c++/ds1808lc.cxx +++ b/examples/c++/ds1808lc.cxx @@ -1,43 +1,41 @@ -#include -#include -#include #include +#include #include + #include "ds1808lc.hpp" -#define EDISON_I2C_BUS 1 // Edison I2C-1 -#define DS1808_GPIO_PWR 15 // Edison GP165 +#define EDISON_I2C_BUS 1 // Edison I2C-1 +#define DS1808_GPIO_PWR 15 // Edison GP165 -void printState(upm::ILightController &lightController) +void +printState(upm::ILightController& lightController) { - if (lightController.isPowered()) - { - std::cout << "Light is powered, brightness = " << lightController.getBrightness() << std::endl; - } - else - { + if (lightController.isPowered()) { + std::cout << "Light is powered, brightness = " << lightController.getBrightness() + << std::endl; + } else { std::cout << "Light is not powered." << std::endl; } } -int main( int argc, char **argv ) +int +main(int argc, char** argv) { //! [Interesting] upm::DS1808LC lightController(DS1808_GPIO_PWR, EDISON_I2C_BUS); - std::cout << "Existing state: "; printState(lightController); - if (argc == 2) - { + std::cout << "Existing state: "; + printState(lightController); + if (argc == 2) { std::string arg = argv[1]; int brightness = ::atoi(argv[1]); - if (brightness > 0) - { + if (brightness > 0) { lightController.setPowerOn(); lightController.setBrightness(brightness); - } - else + } else lightController.setPowerOff(); } - std::cout << "Now: ";printState(lightController); + std::cout << "Now: "; + printState(lightController); //! [Interesting] return 0; } diff --git a/examples/c++/ds18b20.cxx b/examples/c++/ds18b20.cxx index 7855e238..6c1d439f 100644 --- a/examples/c++/ds18b20.cxx +++ b/examples/c++/ds18b20.cxx @@ -22,65 +22,61 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "ds18b20.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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) - upm::DS18B20 sensor; + // Instantiate an DS18B20 instance using the default values (uart 0) + upm::DS18B20 sensor; - cout << "Found " << sensor.devicesFound() << " device(s)" << endl; - cout << endl; + cout << "Found " << sensor.devicesFound() << " device(s)" << endl; + cout << endl; - // bail if we didn't find anything - if (!sensor.devicesFound()) - return 1; + // bail if we didn't find anything + if (!sensor.devicesFound()) + return 1; - // update and print available values every 2 seconds - while (shouldRun) - { - // update our values for all of the detected sensors - sensor.update(-1); + // update and print available values every 2 seconds + while (shouldRun) { + // update our values for all of the detected sensors + sensor.update(-1); - // we show both C and F for temperature for the sensors - int i; - for (i=0; i #include -#include + #include "ds2413.hpp" using namespace std; using namespace upm; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a DS2413 Module on a Dallas 1-wire bus connected to UART 0 - upm::DS2413* sensor = new upm::DS2413(0); + //! [Interesting] + // Instantiate a DS2413 Module on a Dallas 1-wire bus connected to UART 0 + upm::DS2413 sensor(0); - // find all of the DS2413 devices present on the bus - sensor->init(); + // find all of the DS2413 devices present on the bus + sensor.init(); - // how many devices were found? - cout << "Found "<< sensor->devicesFound() << " device(s)" << endl; + // how many devices were found? + cout << "Found " << sensor.devicesFound() << " device(s)" << endl; - // read the gpio and latch values from the first device - // the lower 4 bits are of the form: - // - cout << "GPIO device 0 values: " << sensor->readGpios(0) << endl; + // read the gpio and latch values from the first device + // the lower 4 bits are of the form: + // + cout << "GPIO device 0 values: " << sensor.readGpios(0) << endl; - // set the gpio latch values of the first device - cout << "Setting GPIO latches to on" << endl; - sensor->writeGpios(0, 0x03); + // set the gpio latch values of the first device + cout << "Setting GPIO latches to on" << endl; + sensor.writeGpios(0, 0x03); - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] - return 0; + //! [Interesting] + return 0; } diff --git a/examples/c++/e50hx.cxx b/examples/c++/e50hx.cxx index 253e089a..2d5695c1 100644 --- a/examples/c++/e50hx.cxx +++ b/examples/c++/e50hx.cxx @@ -22,91 +22,84 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "e50hx.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // You will need to edit this example to conform to your site and your - // devices, specifically the Device Object Instance ID passed to the - // constructor, and the arguments to initMaster() that are - // appropriate for your BACnet network. + //! [Interesting] + // You will need to edit this example to conform to your site and your + // devices, specifically the Device Object Instance ID passed to the + // constructor, and the arguments to initMaster() that are + // 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an E50HX object for an E50HX device that has 1075425 - // as it's unique Device Object Instance ID. NOTE: You will - // certainly want to change this to the correct value for your - // device(s). - E50HX *sensor = new E50HX(1075425); + // Instantiate an E50HX object for an E50HX device that has 1075425 + // as it's unique Device Object Instance ID. NOTE: You will + // certainly want to change this to the correct value for your + // device(s). + E50HX sensor(1075425); - // Initialize our BACnet master, if it has not already been - // initialized, with the device and baudrate, choosing 1000001 as - // our unique Device Object Instance ID, 2 as our MAC address and - // using default values for maxMaster and maxInfoFrames - sensor->initMaster(defaultDev, 38400, 1000001, 2); + // Initialize our BACnet master, if it has not already been + // initialized, with the device and baudrate, choosing 1000001 as + // our unique Device Object Instance ID, 2 as our MAC address and + // using default values for maxMaster and maxInfoFrames + sensor.initMaster(defaultDev, 38400, 1000001, 2); - // Uncomment to enable debugging output - // sensor->setDebug(true); + // Uncomment to enable debugging output + // sensor.setDebug(true); - cout << endl; - cout << "Device Description: " << sensor->getDeviceDescription() << endl; - cout << "Device Location: " << sensor->getDeviceLocation() << endl; - cout << endl; + cout << endl; + cout << "Device Description: " << sensor.getDeviceDescription() << endl; + cout << "Device Location: " << sensor.getDeviceLocation() << endl; + cout << endl; - // update and print a few values every 5 seconds - while (shouldRun) - { - cout << "System Voltage: " - << sensor->getAnalogValue(E50HX::AV_System_Voltage) - << " " << sensor->getAnalogValueUnits(E50HX::AV_System_Voltage) - << endl; + // update and print a few values every 5 seconds + while (shouldRun) { + cout << "System Voltage: " << sensor.getAnalogValue(E50HX::AV_System_Voltage) << " " + << sensor.getAnalogValueUnits(E50HX::AV_System_Voltage) << endl; - cout << "System Type: " - << sensor->getAnalogValue(E50HX::AV_System_Type) - << endl; + cout << "System Type: " << sensor.getAnalogValue(E50HX::AV_System_Type) << endl; - cout << "Energy Consumption: " << sensor->getAnalogInput(E50HX::AI_Energy) - << " " << sensor->getAnalogInputUnits(E50HX::AI_Energy) - << endl; + cout << "Energy Consumption: " << sensor.getAnalogInput(E50HX::AI_Energy) << " " + << sensor.getAnalogInputUnits(E50HX::AI_Energy) << endl; - cout << "Power Up Counter: " - << sensor->getAnalogInput(E50HX::AI_Power_Up_Count) - << endl; + cout << "Power Up Counter: " << sensor.getAnalogInput(E50HX::AI_Power_Up_Count) << endl; - cout << endl; - sleep(5); + cout << endl; + upm_delay(5); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/ecezo.cxx b/examples/c++/ecezo.cxx index 2e17f777..dad9b6b8 100644 --- a/examples/c++/ecezo.cxx +++ b/examples/c++/ecezo.cxx @@ -22,51 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include - -#include #include +#include +#include +#include using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { signal(SIGINT, sig_handler); //! [Interesting] // 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 // 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 - sensor->update(); + sensor.update(); - cout << "EC " - << sensor->getEC() - << " uS/cm, TDS " - << sensor->getTDS() - << " mg/L, Salinity " - << sensor->getSalinity() - << " PSS-78, SG " - << sensor->getSG() - << endl; + cout << "EC " << sensor.getEC() << " uS/cm, TDS " << sensor.getTDS() << " mg/L, Salinity " + << sensor.getSalinity() << " PSS-78, SG " << sensor.getSG() << endl; upm_delay(5); } @@ -75,7 +68,5 @@ int main() cout << "Exiting..." << endl; - delete sensor; - return 0; } diff --git a/examples/c++/ecs1030.cxx b/examples/c++/ecs1030.cxx index ff87279a..e2b2918c 100644 --- a/examples/c++/ecs1030.cxx +++ b/examples/c++/ecs1030.cxx @@ -22,14 +22,13 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include +#include + #include "ecs1030.hpp" int is_running = 0; -upm::ECS1030 *sensor = NULL; void sig_handler(int signo) @@ -42,20 +41,20 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { - sensor = new upm::ECS1030(0); + upm::ECS1030 sensor(0); signal(SIGINT, sig_handler); while (!is_running) { - std::cout << "I = " << sensor->getCurrency_A () << ", Power = " << sensor->getPower_A () << std::endl; - std::cout << "I = " << sensor->getCurrency_B () << ", Power = " << sensor->getPower_B () << std::endl; + std::cout << "I = " << sensor.getCurrency_A() << ", Power = " << sensor.getPower_A() + << std::endl; + std::cout << "I = " << sensor.getCurrency_B() << ", Power = " << sensor.getPower_B() + << std::endl; } std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/ehr.cxx b/examples/c++/ehr.cxx index a411f08d..03002596 100644 --- a/examples/c++/ehr.cxx +++ b/examples/c++/ehr.cxx @@ -22,57 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "ehr.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Ear-clip Heart Rate sensor on digital pin D2 - upm::EHR* heart = new upm::EHR(2); - - // set the beat counter to 0, init the clock and start counting beats - heart->clearBeatCounter(); - heart->initClock(); - heart->startBeatCounter(); + //! [Interesting] + // Instantiate a Ear-clip Heart Rate sensor on digital pin D2 + upm::EHR heart(2); - while (shouldRun) - { - // we grab these just for display purposes in this example - uint32_t millis = heart->getMillis(); - uint32_t beats = heart->beatCounter(); + // set the beat counter to 0, init the clock and start counting beats + heart.clearBeatCounter(); + heart.initClock(); + heart.startBeatCounter(); - // heartRate() requires that at least 5 seconds pass before - // returning anything other than 0 - int hr = heart->heartRate(); + while (shouldRun) { + // we grab these just for display purposes in this example + uint32_t millis = heart.getMillis(); + uint32_t beats = heart.beatCounter(); - // output milliseconds passed, beat count, and computed heart rate - cout << "Millis: " << millis << " Beats: " << beats; - cout << " Heart Rate: " << hr << endl; + // heartRate() requires that at least 5 seconds pass before + // returning anything other than 0 + 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(); -//! [Interesting] + heart.stopBeatCounter(); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete heart; - return 0; -} \ No newline at end of file + return 0; +} diff --git a/examples/c++/eldriver.cxx b/examples/c++/eldriver.cxx index 7a139e3d..bc0ea87c 100644 --- a/examples/c++/eldriver.cxx +++ b/examples/c++/eldriver.cxx @@ -22,45 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include + #include "eldriver.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the El Driver Module - // Instantiate a El Driver on digital pin D2 - upm::ElDriver* eldriver = new upm::ElDriver(2); + //! [Interesting] + // This was tested with the El Driver Module + // Instantiate an El Driver on digital pin D2 + upm::ElDriver eldriver(2); - bool lightState = true; + bool lightState = true; - while (shouldRun) - { - if (lightState) - eldriver->on(); - else - eldriver->off(); - lightState = !lightState; - sleep(1); - } + while (shouldRun) { + if (lightState) + eldriver.on(); + else + eldriver.off(); + lightState = !lightState; + upm_delay(1); + } -//! [Interesting] - eldriver->off(); - cout << "Exiting" << endl; + //! [Interesting] + eldriver.off(); + cout << "Exiting" << endl; - delete eldriver; - return 0; + return 0; } diff --git a/examples/c++/electromagnet.cxx b/examples/c++/electromagnet.cxx index ca2dbb38..2781aea1 100644 --- a/examples/c++/electromagnet.cxx +++ b/examples/c++/electromagnet.cxx @@ -22,58 +22,60 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include +#include +#include + #include "electromagnet.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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] - // The was tested with the Electromagnetic Module - // Instantiate a Electromagnet on digital pin D2 - upm::Electromagnet* magnet = new upm::Electromagnet(2); - cout << "Starting up magnet...." << endl; - magnet->off(); + //! [Interesting] + // The was tested with the Electromagnetic Module + // Instantiate a Electromagnet on digital pin D2 + upm::Electromagnet magnet(2); + cout << "Starting up magnet...." << endl; + magnet.off(); - bool magnetState = false; - float time_passed = get_time(); + bool magnetState = false; + float time_passed = get_time(); - // Turn magnet on and off every 5 seconds - while (shouldRun) - { - if ((get_time() - time_passed) > 5.0) - { - magnetState = !magnetState; - if (magnetState) - magnet->on(); - else - magnet->off(); - cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; - time_passed = get_time(); - } - } + // Turn magnet on and off every 5 seconds + while (shouldRun) { + if ((get_time() - time_passed) > 5.0) { + magnetState = !magnetState; + if (magnetState) + magnet.on(); + else + magnet.off(); + cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; + time_passed = get_time(); + } + } -//! [Interesting] - magnet->off(); - cout << "Exiting" << endl; + //! [Interesting] + magnet.off(); + cout << "Exiting" << endl; - delete magnet; - return 0; + return 0; } diff --git a/examples/c++/emg.cxx b/examples/c++/emg.cxx index 7cf2df8d..06afe4a7 100644 --- a/examples/c++/emg.cxx +++ b/examples/c++/emg.cxx @@ -21,40 +21,41 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "emg.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the EMG Muscle Signal Reader Sensor Module - // Instantiate a EMG on analog pin A0 - upm::EMG *emg = new upm::EMG(0); - cout << "Calibrating...." << endl; - emg->calibrate(); - - while (shouldRun) - { - cout << emg->value() << endl; - usleep(100000); - } + //! [Interesting] + // The was tested with the EMG Muscle Signal Reader Sensor Module + // Instantiate a EMG on analog pin A0 + upm::EMG emg(0); + cout << "Calibrating...." << endl; + emg.calibrate(); -//! [Interesting] - cout << "Exiting" << endl; - delete emg; - return 0; + while (shouldRun) { + cout << emg.value() << endl; + upm_delay_us(100000); + } + + //! [Interesting] + cout << "Exiting" << endl; + return 0; } diff --git a/examples/c++/enc03r.cxx b/examples/c++/enc03r.cxx index 1d4f0110..8b43794b 100644 --- a/examples/c++/enc03r.cxx +++ b/examples/c++/enc03r.cxx @@ -22,10 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "enc03r.hpp" +#include "upm_utilities.h" using namespace std; @@ -34,47 +35,43 @@ bool shouldRun = true; // analog voltage, usually 3.3 or 5.0 #define CALIBRATION_SAMPLES 1000 -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } -int main() +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // 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. cout << "Please place the sensor in a stable location, and do not" << endl; cout << "move it while calibration takes place." << endl; cout << "This may take a couple of minutes." << endl; - gyro->calibrate(CALIBRATION_SAMPLES); - cout << "Calibration complete. Reference value: " - << gyro->calibrationValue() << endl; + gyro.calibrate(CALIBRATION_SAMPLES); + cout << "Calibration complete. Reference value: " << gyro.calibrationValue() << endl; // Read the input and print both the raw value and the angular velocity, // waiting 0.1 seconds between readings - while (shouldRun) - { - gyro->update(); + while (shouldRun) { + gyro.update(); - cout << "Angular velocity: " - << gyro->angularVelocity() - << " deg/s" - << endl; + cout << "Angular velocity: " << gyro.angularVelocity() << " deg/s" << endl; - usleep(100000); + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] cout << "Exiting" << endl; - delete gyro; return 0; } diff --git a/examples/c++/flex.cxx b/examples/c++/flex.cxx index 26cdcdd2..b7872e82 100644 --- a/examples/c++/flex.cxx +++ b/examples/c++/flex.cxx @@ -22,45 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "flex.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with a Spectra Symbol flex sensor. - // We attached a 22K resistor to a breadboard, - // with 1 end attached to GND and the other connected to - // both the flex sensor and A0. - // The flex sensor was connected on 1 pin to the 22K resistor and A0 - // and on the other pin to 5V. + //! [Interesting] + // The was tested with a Spectra Symbol flex sensor. + // We attached a 22K resistor to a breadboard, + // with 1 end attached to GND and the other connected to + // both the flex sensor and A0. + // The flex sensor was connected on 1 pin to the 22K resistor and A0 + // and on the other pin to 5V. - // Instantiate a Flex sensor on analog pin A0 - upm::Flex *flex = new upm::Flex(0); - - while (shouldRun) - { - cout << "Flex value: " << flex->value() << endl; - sleep(1); + // Instantiate a Flex sensor on analog pin A0 + upm::Flex flex(0); + + while (shouldRun) { + cout << "Flex value: " << flex.value() << endl; + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete flex; - return 0; + return 0; } diff --git a/examples/c++/gas-mq2.cxx b/examples/c++/gas-mq2.cxx index 040706e6..622b22b2 100644 --- a/examples/c++/gas-mq2.cxx +++ b/examples/c++/gas-mq2.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq2.hpp" #include -#include -#include +#include +#include + +#include "gas.hpp" +#include "mq2.hpp" int is_running = 0; -uint16_t buffer [128]; -upm::MQ2 *sensor = NULL; +uint16_t buffer[128]; void sig_handler(int signo) @@ -44,26 +44,26 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { // Attach gas sensor to A0 - sensor = new upm::MQ2(0); + upm::MQ2 sensor(0); signal(SIGINT, sig_handler); thresholdContext ctx; ctx.averageReading = 0; ctx.runningAverage = 0; - ctx.averagedOver = 2; + ctx.averagedOver = 2; // Infinite loop, ends when script is cancelled // Repeatedly, take a sample every 2 milliseconds; // find the average of 128 samples; and // print a running graph of the averages using a resolution of 5 while (!is_running) { - int len = sensor->getSampledWindow (2, 128, buffer); + int len = sensor.getSampledWindow(2, 128, buffer); if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); if (thresh) { // do something .... } @@ -72,8 +72,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq3.cxx b/examples/c++/gas-mq3.cxx index 13e7586c..db5c2613 100644 --- a/examples/c++/gas-mq3.cxx +++ b/examples/c++/gas-mq3.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq3.hpp" #include -#include -#include +#include +#include + +#include "gas.hpp" +#include "mq3.hpp" int is_running = 0; -uint16_t buffer [128]; -upm::MQ3 *sensor = NULL; +uint16_t buffer[128]; void sig_handler(int signo) @@ -44,26 +44,26 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { // Attach gas sensor to A0 - sensor = new upm::MQ3(0); + upm::MQ3 sensor(0); signal(SIGINT, sig_handler); thresholdContext ctx; ctx.averageReading = 0; ctx.runningAverage = 0; - ctx.averagedOver = 2; + ctx.averagedOver = 2; // Infinite loop, ends when script is cancelled // Repeatedly, take a sample every 2 milliseconds; // find the average of 128 samples; and // print a running graph of the averages using a resolution of 5 while (!is_running) { - int len = sensor->getSampledWindow (2, 128, buffer); + int len = sensor.getSampledWindow(2, 128, buffer); if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); if (thresh) { // do something .... } @@ -72,8 +72,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq4.cxx b/examples/c++/gas-mq4.cxx index 87d5dafa..ca8e8729 100644 --- a/examples/c++/gas-mq4.cxx +++ b/examples/c++/gas-mq4.cxx @@ -22,12 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq4.hpp" #include -#include -#include +#include + +#include "gas.hpp" +#include "mq4.hpp" bool shouldRun = true; @@ -36,46 +36,43 @@ using namespace std; void sig_handler(int signo) { - if (signo == SIGINT) - { - shouldRun = false; + if (signo == SIGINT) { + shouldRun = false; } } //! [Interesting] -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Attach gas sensor to Analog A0 - upm::MQ4 *sensor = new upm::MQ4(0); - signal(SIGINT, sig_handler); - - uint16_t buffer [128]; + // Attach gas sensor to Analog A0 + upm::MQ4 sensor(0); + signal(SIGINT, sig_handler); - thresholdContext ctx; - ctx.averageReading = 0; - ctx.runningAverage = 0; - ctx.averagedOver = 2; - - // Infinite loop, ends when script is cancelled - // Repeatedly, take a sample every 2 microseconds; - // find the average of 128 samples; and - // print a running graph of asteriskss as averages - while (shouldRun) - { - int len = sensor->getSampledWindow (2, 128, buffer); - if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); - if (thresh) { - // do something .... + uint16_t buffer[128]; + + thresholdContext ctx; + ctx.averageReading = 0; + ctx.runningAverage = 0; + ctx.averagedOver = 2; + + // Infinite loop, ends when script is cancelled + // Repeatedly, take a sample every 2 microseconds; + // find the average of 128 samples; and + // print a running graph of asteriskss as averages + while (shouldRun) { + int len = sensor.getSampledWindow(2, 128, buffer); + if (len) { + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); + if (thresh) { + // do something .... + } } - } } - - cout << "Exiting" << endl; - - delete sensor; - - return 0; + + cout << "Exiting" << endl; + + return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq5.cxx b/examples/c++/gas-mq5.cxx index 89c0a529..55503dcc 100644 --- a/examples/c++/gas-mq5.cxx +++ b/examples/c++/gas-mq5.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq5.hpp" #include -#include -#include +#include +#include + +#include "gas.hpp" +#include "mq5.hpp" int is_running = 0; -uint16_t buffer [128]; -upm::MQ5 *sensor = NULL; +uint16_t buffer[128]; void sig_handler(int signo) @@ -44,21 +44,21 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { - sensor = new upm::MQ5(0); + upm::MQ5 sensor(0); signal(SIGINT, sig_handler); thresholdContext ctx; ctx.averageReading = 0; ctx.runningAverage = 0; - ctx.averagedOver = 2; + ctx.averagedOver = 2; while (!is_running) { - int len = sensor->getSampledWindow (2, 128, buffer); + int len = sensor.getSampledWindow(2, 128, buffer); if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 7); + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 7); if (thresh) { // do something .... } @@ -67,8 +67,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq6.cxx b/examples/c++/gas-mq6.cxx index be25f14c..57fa057d 100644 --- a/examples/c++/gas-mq6.cxx +++ b/examples/c++/gas-mq6.cxx @@ -22,12 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq6.hpp" #include -#include -#include +#include + +#include "gas.hpp" +#include "mq6.hpp" bool shouldRun = true; @@ -36,46 +36,43 @@ using namespace std; void sig_handler(int signo) { - if (signo == SIGINT) - { - shouldRun = false; + if (signo == SIGINT) { + shouldRun = false; } } //! [Interesting] -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Attach gas sensor to Analog A0 - upm::MQ6 *sensor = new upm::MQ6(0); - signal(SIGINT, sig_handler); - - uint16_t buffer [128]; + // Attach gas sensor to Analog A0 + upm::MQ6 sensor(0); + signal(SIGINT, sig_handler); - thresholdContext ctx; - ctx.averageReading = 0; - ctx.runningAverage = 0; - ctx.averagedOver = 2; - - // Infinite loop, ends when script is cancelled - // Repeatedly, take a sample every 2 microseconds; - // find the average of 128 samples; and - // print a running graph of asteriskss as averages - while (shouldRun) - { - int len = sensor->getSampledWindow (2, 128, buffer); - if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); - if (thresh) { - // do something .... + uint16_t buffer[128]; + + thresholdContext ctx; + ctx.averageReading = 0; + ctx.runningAverage = 0; + ctx.averagedOver = 2; + + // Infinite loop, ends when script is cancelled + // Repeatedly, take a sample every 2 microseconds; + // find the average of 128 samples; and + // print a running graph of asteriskss as averages + while (shouldRun) { + int len = sensor.getSampledWindow(2, 128, buffer); + if (len) { + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); + if (thresh) { + // do something .... + } } - } } - - cout << "Exiting" << endl; - - delete sensor; - - return 0; + + cout << "Exiting" << endl; + + return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq7.cxx b/examples/c++/gas-mq7.cxx index 561fde0a..ffa8f342 100644 --- a/examples/c++/gas-mq7.cxx +++ b/examples/c++/gas-mq7.cxx @@ -22,12 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq7.hpp" #include -#include -#include +#include + +#include "gas.hpp" +#include "mq7.hpp" bool shouldRun = true; @@ -36,46 +36,43 @@ using namespace std; void sig_handler(int signo) { - if (signo == SIGINT) - { - shouldRun = false; + if (signo == SIGINT) { + shouldRun = false; } } //! [Interesting] -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Attach gas sensor to Analog A0 - upm::MQ7 *sensor = new upm::MQ7(0); - signal(SIGINT, sig_handler); - - uint16_t buffer [128]; + // Attach gas sensor to Analog A0 + upm::MQ7 sensor(0); + signal(SIGINT, sig_handler); - thresholdContext ctx; - ctx.averageReading = 0; - ctx.runningAverage = 0; - ctx.averagedOver = 2; - - // Infinite loop, ends when script is cancelled - // Repeatedly, take a sample every 2 microseconds; - // find the average of 128 samples; and - // print a running graph of asteriskss as averages - while (shouldRun) - { - int len = sensor->getSampledWindow (2, 128, buffer); - if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); - if (thresh) { - // do something .... + uint16_t buffer[128]; + + thresholdContext ctx; + ctx.averageReading = 0; + ctx.runningAverage = 0; + ctx.averagedOver = 2; + + // Infinite loop, ends when script is cancelled + // Repeatedly, take a sample every 2 microseconds; + // find the average of 128 samples; and + // print a running graph of asteriskss as averages + while (shouldRun) { + int len = sensor.getSampledWindow(2, 128, buffer); + if (len) { + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); + if (thresh) { + // do something .... + } } - } } - - cout << "Exiting" << endl; - - delete sensor; - - return 0; + + cout << "Exiting" << endl; + + return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq8.cxx b/examples/c++/gas-mq8.cxx index 5f9fb28d..6145ab6e 100644 --- a/examples/c++/gas-mq8.cxx +++ b/examples/c++/gas-mq8.cxx @@ -22,12 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq8.hpp" #include -#include -#include +#include + +#include "gas.hpp" +#include "mq8.hpp" bool shouldRun = true; @@ -36,46 +36,43 @@ using namespace std; void sig_handler(int signo) { - if (signo == SIGINT) - { - shouldRun = false; + if (signo == SIGINT) { + shouldRun = false; } } //! [Interesting] -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Attach gas sensor to Analog A0 - upm::MQ8 *sensor = new upm::MQ8(0); - signal(SIGINT, sig_handler); - - uint16_t buffer [128]; + // Attach gas sensor to Analog A0 + upm::MQ8 sensor(0); + signal(SIGINT, sig_handler); - thresholdContext ctx; - ctx.averageReading = 0; - ctx.runningAverage = 0; - ctx.averagedOver = 2; - - // Infinite loop, ends when script is cancelled - // Repeatedly, take a sample every 2 microseconds; - // find the average of 128 samples; and - // print a running graph of asteriskss as averages - while (shouldRun) - { - int len = sensor->getSampledWindow (2, 128, buffer); - if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); - if (thresh) { - // do something .... + uint16_t buffer[128]; + + thresholdContext ctx; + ctx.averageReading = 0; + ctx.runningAverage = 0; + ctx.averagedOver = 2; + + // Infinite loop, ends when script is cancelled + // Repeatedly, take a sample every 2 microseconds; + // find the average of 128 samples; and + // print a running graph of asteriskss as averages + while (shouldRun) { + int len = sensor.getSampledWindow(2, 128, buffer); + if (len) { + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); + if (thresh) { + // do something .... + } } - } } - - cout << "Exiting" << endl; - - delete sensor; - - return 0; + + cout << "Exiting" << endl; + + return 0; } //! [Interesting] diff --git a/examples/c++/gas-mq9.cxx b/examples/c++/gas-mq9.cxx index f1a8cd30..0bbadebf 100644 --- a/examples/c++/gas-mq9.cxx +++ b/examples/c++/gas-mq9.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mq9.hpp" #include -#include -#include +#include +#include + +#include "gas.hpp" +#include "mq9.hpp" int is_running = 0; -uint16_t buffer [128]; -upm::MQ9 *sensor = NULL; +uint16_t buffer[128]; void sig_handler(int signo) @@ -44,21 +44,21 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { - sensor = new upm::MQ9(0); + upm::MQ9 sensor(0); signal(SIGINT, sig_handler); thresholdContext ctx; ctx.averageReading = 0; ctx.runningAverage = 0; - ctx.averagedOver = 2; + ctx.averagedOver = 2; while (!is_running) { - int len = sensor->getSampledWindow (2, 128, buffer); + int len = sensor.getSampledWindow(2, 128, buffer); if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx, 5); + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx, 5); if (thresh) { // do something .... } @@ -67,8 +67,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/gas-tp401.cxx b/examples/c++/gas-tp401.cxx index 4749ea52..a782967f 100644 --- a/examples/c++/gas-tp401.cxx +++ b/examples/c++/gas-tp401.cxx @@ -22,10 +22,13 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include #include +#include +#include +#include + #include "tp401.hpp" +#include "upm_utilities.h" using namespace std; @@ -34,37 +37,41 @@ using namespace std; std::string airQuality(uint16_t value) { - if(value < 50) return "Fresh Air"; - if(value < 200) return "Normal Indoor Air"; - if(value < 400) return "Low Pollution"; - if(value < 600) return "High Pollution - Action Recommended"; + if (value < 50) + return "Fresh Air"; + if (value < 200) + 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"; } -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"); // wait 3 minutes for sensor to warm up - for(int i = 0; i < 3; i++) { - if(i) { + for (int i = 0; i < 3; i++) { + if (i) { fprintf(stdout, "Please wait, %d minute(s) passed..\n", i); } - sleep(60); + upm_delay(60); } fprintf(stdout, "Sensor ready!\n"); - while(true) { - uint16_t value = airSensor->getSample(); // Read raw value - float ppm = airSensor->getPPM(); // Read CO ppm (can vary slightly from previous read) + while (true) { + uint16_t value = airSensor.getSample(); // Read raw value + 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()); - usleep(2500000); // Sleep for 2.5s + upm_delay_us(2500000); // Sleep for 2.5s } - delete airSensor; return 0; } //! [Interesting] diff --git a/examples/c++/gp2y0a.cxx b/examples/c++/gp2y0a.cxx index 1a6114b9..4c04656b 100644 --- a/examples/c++/gp2y0a.cxx +++ b/examples/c++/gp2y0a.cxx @@ -22,52 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "gp2y0a.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; // analog voltage, usually 3.3 or 5.0 -#define GP2Y0A_AREF 5.0 +#define GP2Y0A_AREF 5.0 #define SAMPLES_PER_QUERY 20 -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // 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 - // will use the available A1 pin for data. + //! [Interesting] + // 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 + // will use the available A1 pin for data. - // Instantiate a GP2Y0A on analog pin A1 - upm::GP2Y0A *volts = new upm::GP2Y0A(1); - - // The higher the voltage (closer to AREF) the closer the object is. NOTE: - // the measured voltage will probably not exceed 3.3 volts. - // Every second, print the averaged voltage value (averaged over 20 samples). - while (shouldRun) - { - cout << "AREF: " << GP2Y0A_AREF - << ", Voltage value (higher means closer): " - << volts->value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl; - - sleep(1); + // Instantiate a GP2Y0A on analog pin A1 + upm::GP2Y0A volts(1); + + // The higher the voltage (closer to AREF) the closer the object is. NOTE: + // the measured voltage will probably not exceed 3.3 volts. + // Every second, print the averaged voltage value (averaged over 20 samples). + while (shouldRun) { + cout << "AREF: " << GP2Y0A_AREF << ", Voltage value (higher means closer): " + << volts.value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete volts; - return 0; + return 0; } diff --git a/examples/c++/gprs.cxx b/examples/c++/gprs.cxx index 48fe922e..9b546799 100644 --- a/examples/c++/gprs.cxx +++ b/examples/c++/gprs.cxx @@ -22,90 +22,81 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "gprs.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; -void printUsage(char *progname) +void +printUsage(char* progname) { - cout << "Usage: " << progname << " [AT command]" << endl; - cout << endl; + cout << "Usage: " << progname << " [AT command]" << endl; + cout << endl; - cout << "If an argument is supplied on the command line, that argument is" - << endl; - cout << "sent to the module and the response is printed out." << endl; - cout << endl; - cout << "If no argument is used, then the manufacturer and the current" - << endl; - cout << "saved profiles are queried and the results printed out." << endl; - cout << endl; - cout << endl; + cout << "If an argument is supplied on the command line, that argument is" << endl; + cout << "sent to the module and the response is printed out." << endl; + cout << endl; + cout << "If no argument is used, then the manufacturer and the current" << endl; + cout << "saved profiles are queried and the results printed out." << endl; + cout << endl; + cout << endl; } // 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 - cmd += "\r"; + // commands need to be terminated with a carriage return + cmd += "\r"; - sensor->writeDataStr(cmd); - - // wait up to 1 second - if (sensor->dataAvailable(1000)) - { - cout << "Returned: " << sensor->readDataStr(1024) << endl; - } - else - { - cerr << "Timed out waiting for response" << endl; + sensor.writeDataStr(cmd); + + // wait up to 1 second + if (sensor.dataAvailable(1000)) { + cout << "Returned: " << sensor.readDataStr(1024) << endl; + } else { + cerr << "Timed out waiting for response" << endl; } } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - // Instantiate a GPRS Module on UART 0 - upm::GPRS* sensor = new upm::GPRS(0); + // Instantiate a GPRS Module on UART 0 + upm::GPRS sensor(0); - // Set the baud rate, 19200 baud is the default. - if (sensor->setBaudRate(19200) != mraa::SUCCESS) - { - cerr << "Failed to set tty baud rate" << endl; - return 1; + // Set the baud rate, 19200 baud is the default. + if (sensor.setBaudRate(19200) != mraa::SUCCESS) { + cerr << "Failed to set tty baud rate" << endl; + return 1; } - printUsage(argv[0]); + printUsage(argv[0]); - if (argc > 1) - { - cout << "Sending command line argument (" << argv[1] << ")..." << endl; - sendCommand(sensor, argv[1]); - } - else - { - // query the module manufacturer - cout << "Querying module manufacturer (AT+CGMI)..." << endl; - sendCommand(sensor, "AT+CGMI"); + if (argc > 1) { + cout << "Sending command line argument (" << argv[1] << ")..." << endl; + sendCommand(sensor, argv[1]); + } else { + // query the module manufacturer + cout << "Querying module manufacturer (AT+CGMI)..." << endl; + sendCommand(sensor, "AT+CGMI"); - sleep(1); + upm_delay(1); - // query the saved profiles - cout << "Querying the saved profiles (AT&V)..." << endl; - sendCommand(sensor, "AT&V"); + // query the saved profiles + cout << "Querying the saved profiles (AT&V)..." << endl; + sendCommand(sensor, "AT&V"); - // A comprehensive list is available from the datasheet at: - // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf + // A comprehensive list is available from the datasheet at: + // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf } -//! [Interesting] + //! [Interesting] - delete sensor; - return 0; -} \ No newline at end of file + return 0; +} diff --git a/examples/c++/grove-grovebutton.cxx b/examples/c++/grove-grovebutton.cxx index 3534d16f..eae504c8 100644 --- a/examples/c++/grove-grovebutton.cxx +++ b/examples/c++/grove-grovebutton.cxx @@ -22,28 +22,29 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "grove.hpp" +#include + +#include "grovebutton.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { // This example uses GPIO 0 -//! [Interesting] + //! [Interesting] // 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 - while( 1 ) { - std::cout << button->name() << " value is " << button->value() << std::endl; - sleep(1); + while (1) { + std::cout << button.name() << " value is " << button.value() << std::endl; + upm_delay(1); } // Delete the button object - delete button; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/grove-groveled-multi.cxx b/examples/c++/grove-groveled-multi.cxx index 1a6f8eb7..b82512be 100644 --- a/examples/c++/grove-groveled-multi.cxx +++ b/examples/c++/grove-groveled-multi.cxx @@ -22,47 +22,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include "grove.hpp" + +#include "groveled.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // 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 - // then cycle through various colors (red, green, blue, purple) on it's - // own until turned off. - upm::GroveLed* led = new upm::GroveLed(2); + // 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 + // then cycle through various colors (red, green, blue, purple) on it's + // own until turned off. + upm::GroveLed led(2); - // start the light show - led->on(); + // start the light show + led.on(); - // just sleep until interrupted - while (shouldRun) - sleep(1); - -//! [Interesting] + // just upm_delay until interrupted + while (shouldRun) + upm_delay(1); - led->off(); - cout << "Exiting..." << endl; + //! [Interesting] - delete led; - - return 0; + led.off(); + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/grove-groveled.cxx b/examples/c++/grove-groveled.cxx index 8f7e752d..02b8e0e3 100644 --- a/examples/c++/grove-groveled.cxx +++ b/examples/c++/grove-groveled.cxx @@ -23,33 +23,34 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "grove.hpp" +#include + +#include "groveled.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Create the Grove LED object using GPIO pin 2 - upm::GroveLed* led = new upm::GroveLed(2); + upm::GroveLed led(2); // 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 // between transitions - for (int i=0; i < 10; i++) { - led->on(); - sleep(1); - led->off(); - sleep(1); + for (int i = 0; i < 10; i++) { + led.on(); + upm_delay(1); + led.off(); + upm_delay(1); } // Delete the Grove LED object - delete led; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/grove-grovelight.cxx b/examples/c++/grove-grovelight.cxx index 18f8f6c0..b0255c65 100644 --- a/examples/c++/grove-grovelight.cxx +++ b/examples/c++/grove-grovelight.cxx @@ -23,27 +23,28 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "grove.hpp" +#include + +#include "grovelight.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // 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, // waiting one second between readings - while( 1 ) { - std::cout << light->name() << " raw value is " << light->raw_value() << - ", which is roughly " << light->value() << " lux" << std::endl; - sleep(1); + while (1) { + std::cout << light.name() << " raw value is " << light.raw_value() << ", which is roughly " + << light.value() << " lux" << std::endl; + upm_delay(1); } // Delete the light sensor object - delete light; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/grove-groverelay.cxx b/examples/c++/grove-groverelay.cxx index 92971399..653a748d 100644 --- a/examples/c++/grove-groverelay.cxx +++ b/examples/c++/grove-groverelay.cxx @@ -22,37 +22,38 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "grove.hpp" +#include + +#include "groverelay.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { // This example uses GPIO 0 -//! [Interesting] + //! [Interesting] // 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, // waiting one second each time. The LED on the relay switch // will light up when the switch is on (closed). // The switch will also make a noise between transitions. - for ( int i = 0; i < 3; i++ ) { - relay->on(); - if ( relay->isOn() ) - std::cout << relay->name() << " is on" << std::endl; - sleep(1); - relay->off(); - if ( relay->isOff() ) - std::cout << relay->name() << " is off" << std::endl; - sleep(1); + for (int i = 0; i < 3; i++) { + relay.on(); + if (relay.isOn()) + std::cout << relay.name() << " is on" << std::endl; + upm_delay(1); + relay.off(); + if (relay.isOff()) + std::cout << relay.name() << " is off" << std::endl; + upm_delay(1); } // Delete the relay switch object - delete relay; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/grove-groverotary.cxx b/examples/c++/grove-groverotary.cxx index aa2ee57f..a101a0b1 100644 --- a/examples/c++/grove-groverotary.cxx +++ b/examples/c++/grove-groverotary.cxx @@ -22,36 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include "grove.hpp" +#include +#include +#include + +#include "groverotary.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { -//! [Interesting] + //! [Interesting] // 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 - cout << knob->name() << endl; + cout << knob.name() << endl; - while(true) { - float abs_value = knob->abs_value(); // Absolute raw value - float abs_deg = knob->abs_deg(); // Absolute degrees - float abs_rad = knob->abs_rad(); // Absolute radians - float rel_value = knob->rel_value(); // Relative raw value - float rel_deg = knob->rel_deg(); // Relative degrees - float rel_rad = knob->rel_rad(); // Relative radians + while (true) { + float abs_value = knob.abs_value(); // Absolute raw value + float abs_deg = knob.abs_deg(); // Absolute degrees + float abs_rad = knob.abs_rad(); // Absolute radians + float rel_value = knob.rel_value(); // Relative raw value + float rel_deg = knob.rel_deg(); // Relative degrees + 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", - (int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad); + fprintf(stdout, + "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] - delete knob; + //! [Interesting] return 0; } diff --git a/examples/c++/grove-groveslide.cxx b/examples/c++/grove-groveslide.cxx index 04b8bb4e..5b734cea 100644 --- a/examples/c++/grove-groveslide.cxx +++ b/examples/c++/grove-groveslide.cxx @@ -22,29 +22,31 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include #include -#include -#include "grove.hpp" +#include +#include +#include + +#include "groveslide.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { -//! [Interesting] - upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0 + //! [Interesting] + upm::GroveSlide slide(0); - cout << slide->name() << endl; + cout << slide.name() << endl; - while(true) { - float adc_value = slide->raw_value(); // Read raw value - float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V - fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts); + while (true) { + float adc_value = slide.raw_value(); // Read raw value + float volts = slide.voltage_value(); // Read voltage, board reference set at 5.0V + 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] - delete slide; + //! [Interesting] return 0; } diff --git a/examples/c++/grove-grovetemp.cxx b/examples/c++/grove-grovetemp.cxx index 2a21bc15..600fdb43 100644 --- a/examples/c++/grove-grovetemp.cxx +++ b/examples/c++/grove-grovetemp.cxx @@ -23,33 +23,33 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include "grove.hpp" +#include +#include + +#include "grovetemp.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Create the temperature sensor object using AIO pin 0 - upm::GroveTemp* temp = new upm::GroveTemp(0); - std::cout << temp->name() << std::endl; + upm::GroveTemp temp(0); + std::cout << temp.name() << std::endl; // Read the temperature ten times, printing both the Celsius and // equivalent Fahrenheit temperature, waiting one second between readings - for (int i=0; i < 10; i++) { - int celsius = temp->value(); - int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0); - printf("%d degrees Celsius, or %d degrees Fahrenheit\n", - celsius, fahrenheit); - sleep(1); + for (int i = 0; i < 10; i++) { + int celsius = temp.value(); + int fahrenheit = (int) (celsius * 9.0 / 5.0 + 32.0); + printf("%d degrees Celsius, or %d degrees Fahrenheit\n", celsius, fahrenheit); + upm_delay(1); } // Delete the temperature sensor object - delete temp; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/grovecollision.cxx b/examples/c++/grovecollision.cxx index e7ad0f8d..5471f815 100644 --- a/examples/c++/grovecollision.cxx +++ b/examples/c++/grovecollision.cxx @@ -22,48 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include + #include "grovecollision.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the Grove Collision Sensor - // Instantiate a Grove Collision on digital pin D2 - upm::GroveCollision* collision = new upm::GroveCollision(2); + //! [Interesting] + // The was tested with the Grove Collision Sensor + // Instantiate a Grove Collision on digital pin D2 + upm::GroveCollision collision(2); - bool collisionState = false; - cout << "No collision" << endl; - while (shouldRun) - { - if (collision->isColliding() && !collisionState) - { - cout << "Collision!" << endl; - collisionState = true; - } - else if (collisionState) - { - cout << "No collision" << endl; - collisionState = false; - } - } + bool collisionState = false; + cout << "No collision" << endl; + while (shouldRun) { + if (collision.isColliding() && !collisionState) { + cout << "Collision!" << endl; + collisionState = true; + } else if (collisionState) { + cout << "No collision" << endl; + collisionState = false; + } + } -//! [Interesting] - cout << "Exiting" << endl; + //! [Interesting] + cout << "Exiting" << endl; - delete collision; - return 0; + return 0; } diff --git a/examples/c++/groveehr.cxx b/examples/c++/groveehr.cxx index 73775606..261edc4c 100644 --- a/examples/c++/groveehr.cxx +++ b/examples/c++/groveehr.cxx @@ -22,57 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "groveehr.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 - upm::GroveEHR* heart = new upm::GroveEHR(2); - - // set the beat counter to 0, init the clock and start counting beats - heart->clearBeatCounter(); - heart->initClock(); - heart->startBeatCounter(); + //! [Interesting] + // Instantiate a Grove Ear-clip Heart Rate sensor on digital pin D2 + upm::GroveEHR heart(2); - while (shouldRun) - { - // we grab these just for display purposes in this example - uint32_t millis = heart->getMillis(); - uint32_t beats = heart->beatCounter(); + // set the beat counter to 0, init the clock and start counting beats + heart.clearBeatCounter(); + heart.initClock(); + heart.startBeatCounter(); - // heartRate() requires that at least 5 seconds pass before - // returning anything other than 0 - int hr = heart->heartRate(); + while (shouldRun) { + // we grab these just for display purposes in this example + uint32_t millis = heart.getMillis(); + uint32_t beats = heart.beatCounter(); - // output milliseconds passed, beat count, and computed heart rate - cout << "Millis: " << millis << " Beats: " << beats; - cout << " Heart Rate: " << hr << endl; + // heartRate() requires that at least 5 seconds pass before + // returning anything other than 0 + 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(); -//! [Interesting] + heart.stopBeatCounter(); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete heart; - return 0; + return 0; } diff --git a/examples/c++/groveeldriver.cxx b/examples/c++/groveeldriver.cxx index 9c1d561d..a390b6aa 100644 --- a/examples/c++/groveeldriver.cxx +++ b/examples/c++/groveeldriver.cxx @@ -22,45 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include + #include "groveeldriver.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the Grove El Driver Module - // Instantiate a Grove El Driver on digital pin D2 - upm::GroveElDriver* eldriver = new upm::GroveElDriver(2); + //! [Interesting] + // The was tested with the Grove El Driver Module + // Instantiate a Grove El Driver on digital pin D2 + upm::GroveElDriver eldriver(2); - bool lightState = true; + bool lightState = true; - while (shouldRun) - { - if (lightState) - eldriver->on(); - else - eldriver->off(); - lightState = !lightState; - sleep(1); - } + while (shouldRun) { + if (lightState) + eldriver.on(); + else + eldriver.off(); + lightState = !lightState; + upm_delay(1); + } -//! [Interesting] - eldriver->off(); - cout << "Exiting" << endl; + //! [Interesting] + eldriver.off(); + cout << "Exiting" << endl; - delete eldriver; - return 0; + return 0; } diff --git a/examples/c++/groveelectromagnet.cxx b/examples/c++/groveelectromagnet.cxx index bbd4d33e..01df036a 100644 --- a/examples/c++/groveelectromagnet.cxx +++ b/examples/c++/groveelectromagnet.cxx @@ -22,58 +22,60 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include +#include +#include + #include "groveelectromagnet.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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] - // The was tested with the Grove Electromagnetic Module - // Instantiate a Grove Electromagnet on digital pin D2 - upm::GroveElectromagnet* magnet = new upm::GroveElectromagnet(2); - cout << "Starting up magnet...." << endl; - magnet->off(); + //! [Interesting] + // The was tested with the Grove Electromagnetic Module + // Instantiate a Grove Electromagnet on digital pin D2 + upm::GroveElectromagnet magnet(2); + cout << "Starting up magnet...." << endl; + magnet.off(); - bool magnetState = false; - float time_passed = get_time(); + bool magnetState = false; + float time_passed = get_time(); - // Turn magnet on and off every 5 seconds - while (shouldRun) - { - if ((get_time() - time_passed) > 5.0) - { - magnetState = !magnetState; - if (magnetState) - magnet->on(); - else - magnet->off(); - cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; - time_passed = get_time(); - } - } + // Turn magnet on and off every 5 seconds + while (shouldRun) { + if ((get_time() - time_passed) > 5.0) { + magnetState = !magnetState; + if (magnetState) + magnet.on(); + else + magnet.off(); + cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; + time_passed = get_time(); + } + } -//! [Interesting] - magnet->off(); - cout << "Exiting" << endl; + //! [Interesting] + magnet.off(); + cout << "Exiting" << endl; - delete magnet; - return 0; + return 0; } diff --git a/examples/c++/groveemg.cxx b/examples/c++/groveemg.cxx index 3a049dd3..6e1ae056 100644 --- a/examples/c++/groveemg.cxx +++ b/examples/c++/groveemg.cxx @@ -21,40 +21,41 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "groveemg.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the GroveEMG Muscle Signal Reader Sensor Module - // Instantiate a GroveEMG on analog pin A0 - upm::GroveEMG *emg = new upm::GroveEMG(0); - cout << "Calibrating...." << endl; - emg->calibrate(); - - while (shouldRun) - { - cout << emg->value() << endl; - usleep(100000); - } + //! [Interesting] + // The was tested with the GroveEMG Muscle Signal Reader Sensor Module + // Instantiate a GroveEMG on analog pin A0 + upm::GroveEMG emg(0); + cout << "Calibrating...." << endl; + emg.calibrate(); -//! [Interesting] - cout << "Exiting" << endl; - delete emg; - return 0; + while (shouldRun) { + cout << emg.value() << endl; + upm_delay_us(100000); + } + + //! [Interesting] + cout << "Exiting" << endl; + return 0; } diff --git a/examples/c++/grovegprs.cxx b/examples/c++/grovegprs.cxx index f882cba7..8d0e34d9 100644 --- a/examples/c++/grovegprs.cxx +++ b/examples/c++/grovegprs.cxx @@ -22,90 +22,81 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "grovegprs.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; -void printUsage(char *progname) +void +printUsage(char* progname) { - cout << "Usage: " << progname << " [AT command]" << endl; - cout << endl; + cout << "Usage: " << progname << " [AT command]" << endl; + cout << endl; - cout << "If an argument is supplied on the command line, that argument is" - << endl; - cout << "sent to the module and the response is printed out." << endl; - cout << endl; - cout << "If no argument is used, then the manufacturer and the current" - << endl; - cout << "saved profiles are queried and the results printed out." << endl; - cout << endl; - cout << endl; + cout << "If an argument is supplied on the command line, that argument is" << endl; + cout << "sent to the module and the response is printed out." << endl; + cout << endl; + cout << "If no argument is used, then the manufacturer and the current" << endl; + cout << "saved profiles are queried and the results printed out." << endl; + cout << endl; + cout << endl; } // 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 - cmd += "\r"; + // commands need to be terminated with a carriage return + cmd += "\r"; - sensor->writeDataStr(cmd); - - // wait up to 1 second - if (sensor->dataAvailable(1000)) - { - cout << "Returned: " << sensor->readDataStr(1024) << endl; - } - else - { - cerr << "Timed out waiting for response" << endl; + sensor.writeDataStr(cmd); + + // wait up to 1 second + if (sensor.dataAvailable(1000)) { + cout << "Returned: " << sensor.readDataStr(1024) << endl; + } else { + cerr << "Timed out waiting for response" << endl; } } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - // Instantiate a GroveGPRS Module on UART 0 - upm::GroveGPRS* sensor = new upm::GroveGPRS(0); + // Instantiate a GroveGPRS Module on UART 0 + upm::GroveGPRS sensor(0); - // Set the baud rate, 19200 baud is the default. - if (sensor->setBaudRate(19200) != mraa::SUCCESS) - { - cerr << "Failed to set tty baud rate" << endl; - return 1; + // Set the baud rate, 19200 baud is the default. + if (sensor.setBaudRate(19200) != mraa::SUCCESS) { + cerr << "Failed to set tty baud rate" << endl; + return 1; } - printUsage(argv[0]); + printUsage(argv[0]); - if (argc > 1) - { - cout << "Sending command line argument (" << argv[1] << ")..." << endl; - sendCommand(sensor, argv[1]); - } - else - { - // query the module manufacturer - cout << "Querying module manufacturer (AT+CGMI)..." << endl; - sendCommand(sensor, "AT+CGMI"); + if (argc > 1) { + cout << "Sending command line argument (" << argv[1] << ")..." << endl; + sendCommand(sensor, argv[1]); + } else { + // query the module manufacturer + cout << "Querying module manufacturer (AT+CGMI)..." << endl; + sendCommand(sensor, "AT+CGMI"); - sleep(1); + upm_delay(1); - // query the saved profiles - cout << "Querying the saved profiles (AT&V)..." << endl; - sendCommand(sensor, "AT&V"); + // query the saved profiles + cout << "Querying the saved profiles (AT&V)..." << endl; + sendCommand(sensor, "AT&V"); - // A comprehensive list is available from the datasheet at: - // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf + // A comprehensive list is available from the datasheet at: + // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf } -//! [Interesting] + //! [Interesting] - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/grovegsr.cxx b/examples/c++/grovegsr.cxx index 6068e791..89a7f7ba 100644 --- a/examples/c++/grovegsr.cxx +++ b/examples/c++/grovegsr.cxx @@ -22,42 +22,43 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovegsr.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the GroveGSR Galvanic Skin Response Sensor module. + //! [Interesting] + // The was tested with the GroveGSR Galvanic Skin Response Sensor module. - // Instantiate a GroveGSR on analog pin A0 - upm::GroveGSR *gsr = new upm::GroveGSR(0); - cout << "Calibrating...." << endl; - gsr->calibrate(); + // Instantiate a GroveGSR on analog pin A0 + upm::GroveGSR gsr(0); + cout << "Calibrating...." << endl; + gsr.calibrate(); - while (shouldRun) - { - cout << gsr->value() << endl; - usleep(500000); - } -//! [Interesting] + while (shouldRun) { + cout << gsr.value() << endl; + upm_delay_us(500000); + } + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete gsr; - return 0; + return 0; } diff --git a/examples/c++/grovelinefinder.cxx b/examples/c++/grovelinefinder.cxx index 53b1e6e2..7c5245fd 100644 --- a/examples/c++/grovelinefinder.cxx +++ b/examples/c++/grovelinefinder.cxx @@ -22,45 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovelinefinder.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Line Finder sensor on digital pin D2 - upm::GroveLineFinder* finder = new upm::GroveLineFinder(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; + //! [Interesting] + // Instantiate a Grove Line Finder sensor on digital pin D2 + upm::GroveLineFinder finder(2); - 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; } diff --git a/examples/c++/grovemd-stepper.cxx b/examples/c++/grovemd-stepper.cxx index ce189d2a..3be01f19 100644 --- a/examples/c++/grovemd-stepper.cxx +++ b/examples/c++/grovemd-stepper.cxx @@ -22,45 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "grovemd.hpp" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { - //! [Interesting] - // Instantiate an I2C Grove Motor Driver on I2C bus 0 + //! [Interesting] + // Instantiate an I2C Grove Motor Driver on I2C bus 0 - upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS, - GROVEMD_DEFAULT_I2C_ADDR); + upm::GroveMD motors(GROVEMD_I2C_BUS, GROVEMD_DEFAULT_I2C_ADDR); - // 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); + // This example demonstrates using the GroveMD to drive a stepper motor - // set for half a rotation - motors->setStepperSteps(100); + // configure it, for this example, we'll assume 200 steps per rev + motors.configStepper(200); - // let it go - clockwise rotation, 10 RPM speed - motors->enableStepper(upm::GroveMD::STEP_DIR_CW, 10); + // set for half a rotation + 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... - motors->setStepperSteps(100); - motors->enableStepper(upm::GroveMD::STEP_DIR_CCW, 10); + upm_delay(3); - // now disable - motors->disableStepper(); + // Now do it backwards... + motors.setStepperSteps(100); + motors.enableStepper(upm::GroveMD::STEP_DIR_CCW, 10); - //! [Interesting] + // now disable + motors.disableStepper(); - cout << "Exiting..." << endl; + //! [Interesting] - delete motors; - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/grovemd.cxx b/examples/c++/grovemd.cxx index 87c3616c..adf13e0a 100644 --- a/examples/c++/grovemd.cxx +++ b/examples/c++/grovemd.cxx @@ -22,39 +22,38 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "grovemd.hpp" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { - //! [Interesting] - // Instantiate an I2C Grove Motor Driver on I2C bus 0 + //! [Interesting] + // Instantiate an I2C Grove Motor Driver on I2C bus 0 - upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS, - GROVEMD_DEFAULT_I2C_ADDR); + upm::GroveMD motors(GROVEMD_I2C_BUS, GROVEMD_DEFAULT_I2C_ADDR); - // set direction to CW and set speed to 50% - cout << "Spin M1 and M2 at half speed for 3 seconds" << endl; - motors->setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW); - 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); + // set direction to CW and set speed to 50% + cout << "Spin M1 and M2 at half speed for 3 seconds" << endl; + motors.setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW); + motors.setMotorSpeeds(127, 127); - //! [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; - motors->setMotorSpeeds(0, 0); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Stopping motors" << endl; + motors.setMotorSpeeds(0, 0); - delete motors; - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/grovemoisture.cxx b/examples/c++/grovemoisture.cxx index f63a088d..6fe4f09c 100644 --- a/examples/c++/grovemoisture.cxx +++ b/examples/c++/grovemoisture.cxx @@ -22,54 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovemoisture.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Moisture sensor on analog pin A0 - upm::GroveMoisture* moisture = new upm::GroveMoisture(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"; + //! [Interesting] + // Instantiate a Grove Moisture sensor on analog pin A0 + upm::GroveMoisture moisture(0); - 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; } diff --git a/examples/c++/groveo2.cxx b/examples/c++/groveo2.cxx index 79741b52..5dbc7cb8 100644 --- a/examples/c++/groveo2.cxx +++ b/examples/c++/groveo2.cxx @@ -21,37 +21,38 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "groveo2.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the O2 Oxygen Concentration Sensor Module - // Instantiate a GroveO2 on analog pin A0 - upm::GroveO2 *O2 = new upm::GroveO2(0); - while (shouldRun) - { - cout << "The output voltage is: " << O2->voltageValue() << "mV" << endl; - usleep(100000); - } + //! [Interesting] + // The was tested with the O2 Oxygen Concentration Sensor Module + // Instantiate a GroveO2 on analog pin A0 + upm::GroveO2 O2(0); + while (shouldRun) { + cout << "The output voltage is: " << O2.voltageValue() << "mV" << endl; + upm_delay_us(100000); + } -//! [Interesting] - cout << "Exiting" << endl; - delete O2; - return 0; + //! [Interesting] + cout << "Exiting" << endl; + return 0; } diff --git a/examples/c++/grovescam.cxx b/examples/c++/grovescam.cxx index 6592b33e..81abc6f0 100644 --- a/examples/c++/grovescam.cxx +++ b/examples/c++/grovescam.cxx @@ -22,56 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "grovescam.hpp" using namespace std; 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 - upm::GROVESCAM* camera = new upm::GROVESCAM(0); + // Instantiate a Grove Serial Camera on UART 0 + upm::GROVESCAM camera(0); - // make sure port is initialized properly. 115200 baud is the default. - if (!camera->setupTty()) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 115200 baud is the default. + if (!camera.setupTty()) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - if (camera->init()) - cout << "Initialized..." << endl; - else - cout << "init() failed" << endl; + if (camera.init()) + cout << "Initialized..." << endl; + else + cout << "init() failed" << endl; - if (camera->preCapture()) - cout << "preCapture succeeded..." << endl; - else - cout << "preCapture failed." << endl; + if (camera.preCapture()) + cout << "preCapture succeeded..." << endl; + else + cout << "preCapture failed." << endl; - if (camera->doCapture()) - cout << "doCapture succeeded..." << endl; - else - cout << "doCapture failed." << endl; + if (camera.doCapture()) + cout << "doCapture succeeded..." << endl; + else + cout << "doCapture failed." << endl; - cout << "Image size is " << camera->getImageSize() << " bytes" << endl; + cout << "Image size is " << camera.getImageSize() << " bytes" << endl; - if (camera->getImageSize() > 0) - { - cout << "Storing image.jpg..." << endl; - if (camera->storeImage("image.jpg")) - cout << "storeImage succeeded..." << endl; - else - cout << "storeImage failed." << endl; + if (camera.getImageSize() > 0) { + cout << "Storing image.jpg..." << endl; + if (camera.storeImage("image.jpg")) + cout << "storeImage succeeded..." << endl; + else + cout << "storeImage failed." << endl; } -//! [Interesting] + //! [Interesting] - delete camera; - return 0; + return 0; } diff --git a/examples/c++/grovespeaker.cxx b/examples/c++/grovespeaker.cxx index 4e139e5b..8a4427fd 100644 --- a/examples/c++/grovespeaker.cxx +++ b/examples/c++/grovespeaker.cxx @@ -22,28 +22,27 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include + #include "grovespeaker.hpp" using namespace std; -int main () +int +main() { -//! [Interesting] - // Instantiate a Grove Speaker on digital pin D2 - upm::GroveSpeaker* speaker = new upm::GroveSpeaker(2); + //! [Interesting] + // Instantiate a Grove Speaker on digital pin D2 + upm::GroveSpeaker speaker(2); - // Play all 7 of the lowest notes - speaker->playAll(); + // Play all 7 of the lowest notes + speaker.playAll(); - // Play a medium C-sharp - speaker->playSound('c', true, "med"); -//! [Interesting] + // Play a medium C-sharp + speaker.playSound('c', true, "med"); + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete speaker; - return 0; + return 0; } diff --git a/examples/c++/groveultrasonic.cxx b/examples/c++/groveultrasonic.cxx index f23e8f4f..cc36ecca 100644 --- a/examples/c++/groveultrasonic.cxx +++ b/examples/c++/groveultrasonic.cxx @@ -24,39 +24,36 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include "groveultrasonic.hpp" #include -#include -#include +#include + +#include "groveultrasonic.hpp" +#include "upm_utilities.h" -upm::GroveUltraSonic *sonar = NULL; bool running = true; void sig_handler(int signo) { if (signo == SIGINT) { - running = false; + running = false; } } int -main(int argc, char **argv) +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] - // upm::GroveUltraSonic *sonar = NULL; - sonar = new upm::GroveUltraSonic(2); - while(running) { - int width = sonar->getDistance(); - printf("Echo width = %d\n", width); - printf("Distance inches = %f.2\n\n", width/148.0); - sleep(3); - } -//! [Interesting] - printf("exiting application\n"); - delete sonar; - return 0; + signal(SIGINT, sig_handler); + //! [Interesting] + // upm::GroveUltraSonic *sonar = NULL; + upm::GroveUltraSonic sonar(2); + while (running) { + int width = sonar.getDistance(); + printf("Echo width = %d\n", width); + printf("Distance inches = %f.2\n\n", width / 148.0); + upm_delay(3); + } + //! [Interesting] + printf("exiting application\n"); + return 0; } diff --git a/examples/c++/grovevdiv.cxx b/examples/c++/grovevdiv.cxx index 0f903338..f090c694 100644 --- a/examples/c++/grovevdiv.cxx +++ b/examples/c++/grovevdiv.cxx @@ -22,46 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovevdiv.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Voltage Divider sensor on analog pin A0 - upm::GroveVDiv* vDiv = new upm::GroveVDiv(0); + //! [Interesting] + // Instantiate a Grove Voltage Divider sensor on analog pin A0 + upm::GroveVDiv vDiv(0); - // collect data and output measured voltage according to the setting - // of the scaling switch (3 or 10) - while (shouldRun) - { - unsigned int val = vDiv->value(100); - float gain3val = vDiv->computedValue(3, val); - float gain10val = vDiv->computedValue(10, val); - cout << "ADC value: " << val << " Gain 3: " << gain3val - << "v Gain 10: " << gain10val << "v" << endl; + // collect data and output measured voltage according to the setting + // of the scaling switch (3 or 10) + while (shouldRun) { + unsigned int val = vDiv.value(100); + float gain3val = vDiv.computedValue(3, val); + float gain10val = vDiv.computedValue(10, val); + cout << "ADC value: " << val << " Gain 3: " << gain3val << "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; } diff --git a/examples/c++/grovewater.cxx b/examples/c++/grovewater.cxx index e13f8e7d..88ed496b 100644 --- a/examples/c++/grovewater.cxx +++ b/examples/c++/grovewater.cxx @@ -22,44 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovewater.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Water sensor on digital pin D2 - upm::GroveWater* water = new upm::GroveWater(2); - - while (shouldRun) - { - bool val = water->isWet(); - if (val) - cout << "Sensor is wet." << endl; - else - cout << "Sensor is dry." << endl; + //! [Interesting] + // Instantiate a Grove Water sensor on digital pin D2 + upm::GroveWater water(2); - 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; } diff --git a/examples/c++/grovewfs.cxx b/examples/c++/grovewfs.cxx index b92ff17f..45e5342f 100644 --- a/examples/c++/grovewfs.cxx +++ b/examples/c++/grovewfs.cxx @@ -22,57 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovewfs.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Water Flow Sensor on digital pin D2 - upm::GroveWFS* flow = new upm::GroveWFS(2); - - // set the flow counter to 0 and start counting - flow->clearFlowCounter(); - flow->startFlowCounter(); + //! [Interesting] + // Instantiate a Grove Water Flow Sensor on digital pin D2 + upm::GroveWFS flow(2); - 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(); + // set the flow counter to 0 and start counting + flow.clearFlowCounter(); + flow.startFlowCounter(); - 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 - cout << "Millis: " << millis << " Flow Count: " << flowCount; - cout << " Flow Rate: " << fr << " LPM" << endl; + float fr = flow.flowRate(); - // best to gather data for at least one second for reasonable - // results. - sleep(2); + // output milliseconds passed, flow count, and computed flow rate + cout << "Millis: " << millis << " Flow Count: " << flowCount; + cout << " Flow Rate: " << fr << " LPM" << endl; + + // best to gather data for at least one second for reasonable + // results. + upm_delay(2); } - flow->stopFlowCounter(); -//! [Interesting] + flow.stopFlowCounter(); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete flow; - return 0; + return 0; } diff --git a/examples/c++/gsr.cxx b/examples/c++/gsr.cxx index 08034060..9e559434 100644 --- a/examples/c++/gsr.cxx +++ b/examples/c++/gsr.cxx @@ -22,42 +22,43 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "gsr.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the GSR Galvanic Skin Response Sensor module. + //! [Interesting] + // The was tested with the GSR Galvanic Skin Response Sensor module. - // Instantiate a GSR on analog pin A0 - upm::GSR *gsr = new upm::GSR(0); - cout << "Calibrating...." << endl; - gsr->calibrate(); + // Instantiate a GSR on analog pin A0 + upm::GSR gsr(0); + cout << "Calibrating...." << endl; + gsr.calibrate(); - while (shouldRun) - { - cout << gsr->value() << endl; - usleep(500000); - } -//! [Interesting] + while (shouldRun) { + cout << gsr.value() << endl; + upm_delay_us(500000); + } + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete gsr; - return 0; + return 0; } diff --git a/examples/c++/guvas12d.cxx b/examples/c++/guvas12d.cxx index 8e88e00f..7496055a 100644 --- a/examples/c++/guvas12d.cxx +++ b/examples/c++/guvas12d.cxx @@ -22,51 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "guvas12d.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; // 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) shouldRun = false; } -int main() +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // This was tested with the Grove UV Sensor module. // It has a sensing range from between 240-370nm. It's strongest // response is around 320-360nm. // 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. - while (shouldRun) - { - cout << "Volts: " << volts->volts() - << ", Intensity: " << volts->intensity() - << " mW/m^2" + while (shouldRun) { + cout << "Volts: " << volts.volts() << ", Intensity: " << volts.intensity() << " mW/m^2" << endl; - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting" << endl; - delete volts; return 0; } diff --git a/examples/c++/h3lis331dl.cxx b/examples/c++/h3lis331dl.cxx index 714d29b4..fe269b3a 100644 --- a/examples/c++/h3lis331dl.cxx +++ b/examples/c++/h3lis331dl.cxx @@ -22,59 +22,58 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include + #include "h3lis331dl.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an H3LIS331DL on I2C bus 0 + //! [Interesting] + // Instantiate an H3LIS331DL on I2C bus 0 - upm::H3LIS331DL *accel = new upm::H3LIS331DL(H3LIS331DL_I2C_BUS, - H3LIS331DL_DEFAULT_I2C_ADDR); + upm::H3LIS331DL accel(H3LIS331DL_I2C_BUS, H3LIS331DL_DEFAULT_I2C_ADDR); - // Initialize the device with default values - accel->init(); + // Initialize the device with default values + accel.init(); - while (shouldRun) - { - int x, y, z; - float ax, ay, az; + while (shouldRun) { + int x, y, z; + float ax, ay, az; - accel->update(); + accel.update(); - accel->getRawXYZ(&x, &y, &z); - accel->getAcceleration(&ax, &ay, &az); + accel.getRawXYZ(&x, &y, &z); + accel.getAcceleration(&ax, &ay, &az); - cout << "Raw: X = " << x << " Y = " << y << " Z = " << z << endl; - - cout << "Acceleration: AX = " << ax << " AY = " << ay << " AZ = " << az - << endl; + cout << "Raw: X = " << x << " Y = " << y << " Z = " << z << 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; } diff --git a/examples/c++/h803x.cxx b/examples/c++/h803x.cxx index 4416439e..8738cb44 100644 --- a/examples/c++/h803x.cxx +++ b/examples/c++/h803x.cxx @@ -22,128 +22,100 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "h803x.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an H803X instance, using MODBUS slave address 1, and - // default comm parameters (9600, 8, N, 2) - upm::H803X *sensor = new upm::H803X(defaultDev, 1); + // Instantiate an H803X instance, using MODBUS slave address 1, and + // default comm parameters (9600, 8, N, 2) + upm::H803X sensor(defaultDev, 1); - // output the Slave ID string - cout << "Slave ID: " << sensor->getSlaveID() << endl; - cout << endl; + // output the Slave ID string + cout << "Slave ID: " << sensor.getSlaveID() << endl; + cout << endl; - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // H8035 / H8036 - cout << "Consumption (kWh): " << sensor->getConsumption() << endl; - cout << "Real Power (kW): " << sensor->getRealPower() << endl; + // H8035 / H8036 + cout << "Consumption (kWh): " << sensor.getConsumption() << endl; + cout << "Real Power (kW): " << sensor.getRealPower() << endl; - if (sensor->isH8036()) - { - // The H8036 has much more data available... + if (sensor.isH8036()) { + // The H8036 has much more data available... - cout << "Reactive Power (kVAR): " << sensor->getReactivePower() - << endl; - cout << "Apparent Power (kVA): " << sensor->getApparentPower() - << endl; - cout << "Power Factor: " << sensor->getPowerFactor() - << endl; - cout << "Volts Line to Line: " << sensor->getVoltsLineToLine() - << endl; - cout << "Volts Line to Neutral: " << sensor->getVoltsLineToNeutral() - << endl; + cout << "Reactive Power (kVAR): " << sensor.getReactivePower() << endl; + cout << "Apparent Power (kVA): " << sensor.getApparentPower() << endl; + cout << "Power Factor: " << sensor.getPowerFactor() << endl; + cout << "Volts Line to Line: " << sensor.getVoltsLineToLine() << endl; + cout << "Volts Line to Neutral: " << sensor.getVoltsLineToNeutral() << endl; - cout << "Current: " << sensor->getCurrent() - << endl; + cout << "Current: " << sensor.getCurrent() << endl; - cout << "Real Power Phase A (kW): " << sensor->getRealPowerPhaseA() - << endl; - cout << "Real Power Phase B (kW): " << sensor->getRealPowerPhaseB() - << endl; - cout << "Real Power Phase C (kW): " << sensor->getRealPowerPhaseC() - << endl; + cout << "Real Power Phase A (kW): " << sensor.getRealPowerPhaseA() << endl; + cout << "Real Power Phase B (kW): " << sensor.getRealPowerPhaseB() << endl; + cout << "Real Power Phase C (kW): " << sensor.getRealPowerPhaseC() << endl; - cout << "Power Factor Phase A: " << sensor->getPowerFactorPhaseA() - << endl; - cout << "Power Factor Phase B: " << sensor->getPowerFactorPhaseB() - << endl; - cout << "Power Factor Phase C: " << sensor->getPowerFactorPhaseC() - << endl; + cout << "Power Factor Phase A: " << sensor.getPowerFactorPhaseA() << endl; + cout << "Power Factor Phase B: " << sensor.getPowerFactorPhaseB() << endl; + cout << "Power Factor Phase C: " << sensor.getPowerFactorPhaseC() << endl; - cout << "Volts Phase A to B: " << sensor->getVoltsPhaseAToB() - << endl; - cout << "Volts Phase B to C: " << sensor->getVoltsPhaseBToC() - << endl; - cout << "Volts Phase A to C: " << sensor->getVoltsPhaseAToC() - << 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 << "Volts Phase A to B: " << sensor.getVoltsPhaseAToB() << endl; + cout << "Volts Phase B to C: " << sensor.getVoltsPhaseBToC() << endl; + cout << "Volts Phase A to C: " << sensor.getVoltsPhaseAToC() << 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() - << endl; - cout << "Current Phase B: " << sensor->getCurrentPhaseB() - << endl; - cout << "Current Phase C: " << sensor->getCurrentPhaseC() - << endl; + cout << "Current Phase A: " << sensor.getCurrentPhaseA() << endl; + cout << "Current Phase B: " << sensor.getCurrentPhaseB() << endl; + cout << "Current Phase C: " << sensor.getCurrentPhaseC() << endl; - cout << "Avg Real Power (kW): " << sensor->getAvgRealPower() - << endl; - cout << "Min Real Power (kW): " << sensor->getMinRealPower() - << endl; - cout << "Max Real Power (kW): " << sensor->getMaxRealPower() - << endl; + cout << "Avg Real Power (kW): " << sensor.getAvgRealPower() << endl; + cout << "Min Real Power (kW): " << sensor.getMinRealPower() << 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; } diff --git a/examples/c++/hcsr04.cxx b/examples/c++/hcsr04.cxx index 4c1dbed9..b5a6d85b 100644 --- a/examples/c++/hcsr04.cxx +++ b/examples/c++/hcsr04.cxx @@ -22,12 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "hcsr04.hpp" #include -#include -#include + +#include "hcsr04.hpp" +#include "upm_utilities.h" int shouldRun = true; @@ -41,22 +40,21 @@ sig_handler(int signo) //! [Interesting] 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); - sleep(1); + upm_delay(1); - while(shouldRun){ + while (shouldRun) { std::cout << "get distance" << std::endl; - double distance = sonar->getDistance(HCSR04_CM); + double distance = sonar.getDistance(HCSR04_CM); std::cout << "distance " << distance << std::endl; - sleep(2); + upm_delay(2); } std::cout << "Exiting... " << std::endl; - delete sonar; return 0; } //! [Interesting] diff --git a/examples/c++/hdc1000.cxx b/examples/c++/hdc1000.cxx index a82fad10..dd12b3d1 100644 --- a/examples/c++/hdc1000.cxx +++ b/examples/c++/hdc1000.cxx @@ -21,47 +21,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "hdc1000.hpp" +#include "upm_utilities.h" using namespace upm; bool run = true; -void sig_handler(int sig) +void +sig_handler(int sig) { - if (sig == SIGINT) - run = false; + if (sig == SIGINT) + run = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - std::cout << "Initializing test-application..." << std::endl; + //! [Interesting] + std::cout << "Initializing test-application..." << std::endl; - // Instantiate an HDC1000 instance on bus 1 - upm::HDC1000 *mySensor = new upm::HDC1000(1); + // Instantiate an HDC1000 instance on bus 1 + upm::HDC1000 mySensor(1); - // update and print available values every second - while (run) - { - std::cout << "Humidity: " << mySensor->getHumidity(true) << std::endl - << "Temperature: " << mySensor->getTemperature(true) << std::endl; + // update and print available values every second + while (run) { + std::cout << "Humidity: " << mySensor.getHumidity(true) << std::endl + << "Temperature: " << mySensor.getTemperature(true) << std::endl; std::cout << std::endl; - sleep(1); + upm_delay(1); } - std::cout << "Exiting test-application..." << std::endl; + std::cout << "Exiting test-application..." << std::endl; - delete mySensor; -//! [Interesting] + //! [Interesting] - return 0; -} \ No newline at end of file + return 0; +} diff --git a/examples/c++/hdxxvxta.cxx b/examples/c++/hdxxvxta.cxx index c2583556..ccd644c9 100644 --- a/examples/c++/hdxxvxta.cxx +++ b/examples/c++/hdxxvxta.cxx @@ -22,58 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "hdxxvxta.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 HDXXVXTA instance, using A1 for humidity and A0 - // for temperature - upm::HDXXVXTA *sensor = new upm::HDXXVXTA(1, 0); + // Instantiate an HDXXVXTA instance, using A1 for humidity and A0 + // for temperature + upm::HDXXVXTA sensor(1, 0); - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor->getHumidity() - << " %" << endl; + cout << "Humidity: " << sensor.getHumidity() << " %" << endl; - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/hka5.cxx b/examples/c++/hka5.cxx index f01fe738..8d7102ef 100644 --- a/examples/c++/hka5.cxx +++ b/examples/c++/hka5.cxx @@ -22,65 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "hka5.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a HKA5 sensor on uart 0. We don't use the set or - // reset pins, so we pass -1 for them. - upm::HKA5 *sensor = new upm::HKA5(0, -1, -1); + // Instantiate a HKA5 sensor on uart 0. We don't use the set or + // reset pins, so we pass -1 for them. + upm::HKA5 sensor(0, -1, -1); - // update once every 2 seconds and output data - while (shouldRun) - { - sensor->update(); + // update once every 2 seconds and output data + while (shouldRun) { + sensor.update(); - cout << "PM 1 : " - << sensor->getPM1() - << " ug/m3" - << endl; + cout << "PM 1 : " << sensor.getPM1() << " ug/m3" << endl; + cout << "PM 2.5: " << sensor.getPM2_5() << " ug/m3" << endl; + cout << "PM 10 : " << sensor.getPM10() << " ug/m3" << endl; + cout << endl; - cout << "PM 2.5: " - << sensor->getPM2_5() - << " ug/m3" - << endl; - - cout << "PM 10 : " - << sensor->getPM10() - << " ug/m3" - << endl; - - cout << endl; - - sleep(2); + upm_delay(2); } - if (shouldRun) - cerr << "Timed out" << endl; + if (shouldRun) + cerr << "Timed out" << endl; -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/hlg150h.cxx b/examples/c++/hlg150h.cxx index d9b3bca4..660058ce 100644 --- a/examples/c++/hlg150h.cxx +++ b/examples/c++/hlg150h.cxx @@ -1,44 +1,43 @@ -#include -#include -#include -#include -#include -#include "hlg150h.hpp" - -#define HLG150H_GPIO_RELAY 21 -#define HLG150H_GPIO_PWM 22 - -void printState(upm::ILightController &lightController) -{ - if (lightController.isPowered()) - { - std::cout << "Light is powered, brightness = " << lightController.getBrightness() << std::endl; - } - else - { - std::cout << "Light is not powered." << std::endl; - } -} - -int main( int argc, char **argv ) -{ - //! [Interesting] - upm::HLG150H lightController(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM); - std::cout << "Existing state: "; printState(lightController); - if (argc == 2) - { - std::string arg = argv[1]; - int brightness = ::atoi(argv[1]); - if (brightness > 0) - { - lightController.setPowerOn(); - lightController.setBrightness(brightness); - } - else - lightController.setPowerOff(); - } - std::cout << "Now: ";printState(lightController); - //! [Interesting] - - return 0; -} +#include +#include +#include + +#include "hlg150h.hpp" +#include "iLightController.hpp" + +#define HLG150H_GPIO_RELAY 21 +#define HLG150H_GPIO_PWM 22 + +void +printState(upm::ILightController& lightController) +{ + if (lightController.isPowered()) { + std::cout << "Light is powered, brightness = " << lightController.getBrightness() + << std::endl; + } else { + std::cout << "Light is not powered." << std::endl; + } +} + +int +main(int argc, char** argv) +{ + //! [Interesting] + upm::HLG150H lightController(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM); + std::cout << "Existing state: "; + printState(lightController); + if (argc == 2) { + std::string arg = argv[1]; + int brightness = ::atoi(argv[1]); + if (brightness > 0) { + lightController.setPowerOn(); + lightController.setBrightness(brightness); + } else + lightController.setPowerOff(); + } + std::cout << "Now: "; + printState(lightController); + //! [Interesting] + + return 0; +} diff --git a/examples/c++/hm11.cxx b/examples/c++/hm11.cxx index faf80ee6..d65a1b03 100644 --- a/examples/c++/hm11.cxx +++ b/examples/c++/hm11.cxx @@ -22,101 +22,95 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include #include +#include +#include + #include "hm11.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; -void printUsage(char *progname) +void +printUsage(char* progname) { - cout << "Usage: " << progname << " [AT command]" << endl; - cout << endl; + cout << "Usage: " << progname << " [AT command]" << endl; + cout << endl; - cout << "If an argument is supplied on the command line, that argument is" - << endl; - cout << "sent to the module and the response is printed out." << endl; - cout << endl; - cout << "If no argument is used, then the address and PIN of the module" - << endl; - cout << "are queried and the results printed out." << endl; - cout << endl; - cout << endl; + cout << "If an argument is supplied on the command line, that argument is" << endl; + cout << "sent to the module and the response is printed out." << endl; + cout << endl; + cout << "If no argument is used, then the address and PIN of the module" << endl; + cout << "are queried and the results printed out." << endl; + cout << endl; + cout << endl; } // simple helper function to send a command and wait for a response -void sendCommand(upm::HM11* ble, char *cmd) +void +sendCommand(upm::HM11& ble, char* cmd) { - char buffer[BUFSIZ]; - ble->writeData(cmd, strlen(cmd)); + char buffer[BUFSIZ]; + ble.writeData(cmd, strlen(cmd)); - // wait up to 1 second - if (ble->dataAvailable(1000)) - { - memset(buffer, 0, BUFSIZ); + // wait up to 1 second + if (ble.dataAvailable(1000)) { + memset(buffer, 0, BUFSIZ); - ble->readData(buffer, BUFSIZ - 1); - cout << "Returned: " << buffer << endl; - } - else - { - cerr << "Timed out waiting for response" << endl; + ble.readData(buffer, BUFSIZ - 1); + cout << "Returned: " << buffer << endl; + } else { + cerr << "Timed out waiting for response" << endl; } } - -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a HM11 BLE Module on UART 0 + //! [Interesting] + // Instantiate a HM11 BLE Module on UART 0 - upm::HM11* ble = new upm::HM11(0); + upm::HM11 ble(0); - // make sure port is initialized properly. 9600 baud is the default. - if (!ble->setupTty(B9600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 9600 baud is the default. + if (!ble.setupTty(B9600)) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - printUsage(argv[0]); + printUsage(argv[0]); - if (argc > 1) - { - cout << "Sending command line argument (" << argv[1] << ")..." << endl; - sendCommand(ble, argv[1]); - } - else - { - // query the module address - char addr[] = "AT+ADDR?"; - cout << "Querying module address (" << addr << ")..." << endl; - sendCommand(ble, addr); + if (argc > 1) { + cout << "Sending command line argument (" << argv[1] << ")..." << endl; + sendCommand(ble, argv[1]); + } else { + // query the module address + char addr[] = "AT+ADDR?"; + cout << "Querying module address (" << addr << ")..." << endl; + sendCommand(ble, addr); - sleep(1); + upm_delay(1); - // query the module address - char pin[] = "AT+PASS?"; - cout << "Querying module PIN (" << pin << ")..." << endl; - sendCommand(ble, pin); + // query the module address + char pin[] = "AT+PASS?"; + cout << "Querying module PIN (" << pin << ")..." << endl; + sendCommand(ble, pin); - // Other potentially useful commands are: - // - // AT+VERS? - query module version - // AT+ROLE0 - set as slave - // AT+ROLE1 - set as master - // AT+CLEAR - clear all previous settings - // AT+RESET - restart the device - // - // A comprehensive list is available from the datasheet at: - // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf + // Other potentially useful commands are: + // + // AT+VERS? - query module version + // AT+ROLE0 - set as slave + // AT+ROLE1 - set as master + // AT+CLEAR - clear all previous settings + // AT+RESET - restart the device + // + // A comprehensive list is available from the datasheet at: + // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf } -//! [Interesting] + //! [Interesting] - delete ble; - return 0; + return 0; } diff --git a/examples/c++/hmc5883l.cxx b/examples/c++/hmc5883l.cxx index 7c686a95..04cee8cc 100644 --- a/examples/c++/hmc5883l.cxx +++ b/examples/c++/hmc5883l.cxx @@ -23,29 +23,30 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include + #include "hmc5883l.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Instantiate on I2C - upm::Hmc5883l* compass = new upm::Hmc5883l(0); + upm::Hmc5883l compass(0); - compass->set_declination(0.2749); // Set your declination from - // true north in radians + compass.set_declination(0.2749); // Set your declination from + // true north in radians // Print out the coordinates, heading, and direction every second - while(true){ - compass->update(); // Update the coordinates - const int16_t *pos = compass->coordinates(); + while (true) { + compass.update(); // Update the coordinates + const int16_t* pos = compass.coordinates(); fprintf(stdout, "coor: %5d %5d %5d ", pos[0], pos[1], pos[2]); - fprintf(stdout, "heading: %5.2f direction: %3.2f\n", - compass->heading(), compass->direction()); - sleep(1); + fprintf(stdout, "heading: %5.2f direction: %3.2f\n", compass.heading(), compass.direction()); + upm_delay(1); } -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/hmtrp.cxx b/examples/c++/hmtrp.cxx index f711dd53..16816d18 100644 --- a/examples/c++/hmtrp.cxx +++ b/examples/c++/hmtrp.cxx @@ -22,145 +22,132 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include #include +#include +#include + #include "hmtrp.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -void printUsage() +void +printUsage() { - cout << "Usage:" << endl; - cout << "Pass a commandline argument (any argument) to this program" - << endl; - cout << "to query the radio configuration and output it. NOTE: the" - << endl; - cout << "radio must be in CONFIG mode for this to work." - << endl; - cout << endl; - cout << "Running this program without arguments will simply transmit" - << endl; - cout << "'Hello World!' every second, and output any data received from" - << endl; - cout << "another radio." - << endl; - cout << endl; + cout << "Usage:" << endl; + cout << "Pass a commandline argument (any argument) to this program" << endl; + cout << "to query the radio configuration and output it. NOTE: the" << endl; + cout << "radio must be in CONFIG mode for this to work." << endl; + cout << endl; + cout << "Running this program without arguments will simply transmit" << endl; + cout << "'Hello World!' every second, and output any data received from" << endl; + cout << "another radio." << endl; + cout << endl; } const size_t bufferLength = 256; -int main (int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a HMTRP radio device on uart 0 + //! [Interesting] + // Instantiate a HMTRP radio device on uart 0 - upm::HMTRP* radio = new upm::HMTRP(0); + upm::HMTRP radio(0); - // make sure port is initialized properly. 9600 baud is the default. - if (!radio->setupTty(B9600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 9600 baud is the default. + if (!radio.setupTty(B9600)) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - printUsage(); + printUsage(); - // By default, this radio simply transmits data sent via writeData() - // and reads any available data via readData(). + // By default, this radio simply transmits data sent via writeData() + // and reads any available data via readData(). - // It can be placed into a configuration mode by grounding the - // CONFIG pin on the module. When this is done, the various - // configuration query and config methods can be used. In this - // example, by default, we just read any data available fom the - // device, and periodically transmit "Hello World". + // It can be placed into a configuration mode by grounding the + // CONFIG pin on the module. When this is done, the various + // configuration query and config methods can be used. In this + // example, by default, we just read any data available fom the + // device, and periodically transmit "Hello World". - // If any argument was specified on the command line, do a simple - // configuration query and output the results. The radio must be in - // CONFIG mode for this to work. + // If any argument was specified on the command line, do a simple + // configuration query and output the results. The radio must be in + // CONFIG mode for this to work. - if (argc > 1) - { - // config mode - uint32_t freq; - uint32_t dataRate; - uint16_t rxBandwidth; - uint8_t modulation; - uint8_t txPower; - uint32_t uartBaud; - - if (radio->getConfig(&freq, &dataRate, &rxBandwidth, &modulation, - &txPower, &uartBaud)) - { - cout << "Radio configuration:" << endl; - cout << "freq: " << freq << " dataRate: " << dataRate - << " rxBandwidth: " << rxBandwidth << "Khz" << endl; - - cout << "modulation: " << int(modulation) << "Khz txPower: " - << int(txPower) << " uartBaud: " << uartBaud << endl; + if (argc > 1) { + // config mode + uint32_t freq; + uint32_t dataRate; + uint16_t rxBandwidth; + uint8_t modulation; + uint8_t txPower; + uint32_t uartBaud; + + if (radio.getConfig(&freq, &dataRate, &rxBandwidth, &modulation, &txPower, &uartBaud)) { + cout << "Radio configuration:" << endl; + cout << "freq: " << freq << " dataRate: " << dataRate << " rxBandwidth: " << rxBandwidth + << "Khz" << endl; + + cout << "modulation: " << int(modulation) << "Khz txPower: " << int(txPower) + << " uartBaud: " << uartBaud << endl; + } else { + cerr << "getConfig() failed. Make sure the radio is in " + << "CONFIG mode." << endl; } - else - { - cerr << "getConfig() failed. Make sure the radio is in " - << "CONFIG mode." << endl; - } - } - else - { - // normal read/write mode - char radioBuffer[bufferLength]; - int counter = 0; - cout << "Running in normal read/write mode." << endl; + } else { + // normal read/write mode + char radioBuffer[bufferLength]; + int counter = 0; + cout << "Running in normal read/write mode." << endl; - while (shouldRun) - { - // we don't want the read to block in this example, so always - // check to see if data is available first. - if (radio->dataAvailable()) - { - memset(radioBuffer, 0, bufferLength); - int rv = radio->readData(radioBuffer, bufferLength - 1); - - if (rv > 0) - cout << "Received: " << radioBuffer << endl; - - if (rv < 0) // some sort of read error occurred + while (shouldRun) { + // we don't want the read to block in this example, so always + // check to see if data is available first. + if (radio.dataAvailable()) { + memset(radioBuffer, 0, bufferLength); + int rv = radio.readData(radioBuffer, bufferLength - 1); + + if (rv > 0) + cout << "Received: " << radioBuffer << endl; + + if (rv < 0) // some sort of read error occurred { - cerr << "Port read error." << endl; - break; + cerr << "Port read error." << endl; + break; } - - continue; + + continue; } - - usleep(100000); // 100ms - counter++; - // every second, transmit "Hello World" - if (counter > 10) - { - static const char *hello = "Hello World!"; - cout << "Transmitting hello world..." << endl; - radio->writeData((char *)hello, strlen(hello) + 1); - counter = 0; + + upm_delay_us(100000); // 100ms + counter++; + // every second, transmit "Hello World" + if (counter > 10) { + static const char* hello = "Hello World!"; + cout << "Transmitting hello world..." << endl; + radio.writeData((char*) hello, strlen(hello) + 1); + counter = 0; } } } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete radio; - return 0; + return 0; } diff --git a/examples/c++/hp20x.cxx b/examples/c++/hp20x.cxx index 0aa82bba..fdcf0159 100644 --- a/examples/c++/hp20x.cxx +++ b/examples/c++/hp20x.cxx @@ -22,49 +22,51 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include +#include + #include "hp20x.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an HP20X on default I2C bus and address + //! [Interesting] + // Instantiate an HP20X on default I2C bus and address - upm::HP20X *bar = new upm::HP20X(); + upm::HP20X bar; - // Initialize the device with default values - bar->init(); + // Initialize the device with default values + bar.init(); - // Output data every second until interrupted - while (shouldRun) - { - printf("Temperature: %f Celsius\n", bar->getTemperature()); - printf("Pressure: %f Millibars\n", bar->getPressure()); - printf("Altitude: %f Meters\n", bar->getAltitude()); + // Output data every second until interrupted + while (shouldRun) { + printf("Temperature: %f Celsius\n", bar.getTemperature()); + printf("Pressure: %f Millibars\n", bar.getPressure()); + printf("Altitude: %f Meters\n", bar.getAltitude()); - printf("\n"); + printf("\n"); - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete bar; - return 0; + return 0; } diff --git a/examples/c++/ht9170.cxx b/examples/c++/ht9170.cxx index 299c82ce..53352c26 100644 --- a/examples/c++/ht9170.cxx +++ b/examples/c++/ht9170.cxx @@ -22,51 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "ht9170.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main () +int +main() { - signal(SIGINT, sig_handler); - //! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate a DTMF decoder - upm::HT9170* dtmf = new upm::HT9170(12, 11, 10, 9, 8); - - // Now we just spin in a loop, sleeping every 100ms, checking to see - // if a digit is available. If so, we decode and print the digit, - // and continue looping. + // Instantiate a DTMF decoder + upm::HT9170 dtmf(12, 11, 10, 9, 8); - while(shouldRun) - { - if (dtmf->digitReady()) - { - cout << "Got DTMF code: " << dtmf->decodeDigit() << endl; - // now spin until digitReady() goes false again - while (dtmf->digitReady()) - ; + // Now we just spin in a loop, upm_delaying every 100ms, checking to see + // if a digit is available. If so, we decode and print the digit, + // and continue looping. + + while (shouldRun) { + if (dtmf.digitReady()) { + cout << "Got DTMF code: " << dtmf.decodeDigit() << endl; + // now spin until digitReady() goes false again + while (dtmf.digitReady()) + ; } - usleep(100000); + upm_delay_us(100000); } - - //! [Interesting] - cout << "Exiting..." << endl; + //! [Interesting] - delete dtmf; - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/htu21d.cxx b/examples/c++/htu21d.cxx index 31cec0f9..5f8d71b0 100644 --- a/examples/c++/htu21d.cxx +++ b/examples/c++/htu21d.cxx @@ -22,16 +22,15 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "htu21d.hpp" +#include "upm_utilities.h" volatile int doWork = 0; -upm::HTU21D *sensor = NULL; - void sig_handler(int signo) { @@ -42,38 +41,32 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { // Register signal handler signal(SIGINT, sig_handler); //! [Interesting] - float humidity = 0.0; + float humidity = 0.0; float temperature = 0.0; - float compRH = 0.0; + float compRH = 0.0; - sensor = new upm::HTU21D(0, HTU21D_I2C_ADDRESS); + upm::HTU21D sensor(0, HTU21D_I2C_ADDRESS); - sensor->testSensor(); + sensor.testSensor(); while (!doWork) { - compRH = sensor->getCompRH(true); - humidity = sensor->getHumidity(false); - temperature = sensor->getTemperature(false); + compRH = sensor.getCompRH(true); + humidity = sensor.getHumidity(false); + temperature = sensor.getTemperature(false); - std::cout << "humidity value = " << - humidity << - ", temperature value = " << - temperature << - ", compensated RH value = " << - compRH << std::endl; - usleep (500000); + std::cout << "humidity value = " << humidity << ", temperature value = " << temperature + << ", compensated RH value = " << compRH << std::endl; + upm_delay_us(500000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/hwxpxx.cxx b/examples/c++/hwxpxx.cxx index c9d92bf2..50d04a49 100644 --- a/examples/c++/hwxpxx.cxx +++ b/examples/c++/hwxpxx.cxx @@ -22,81 +22,76 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "hwxpxx.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an HWXPXX instance, using MODBUS slave address 3, and - // default comm parameters (19200, 8, N, 2) - upm::HWXPXX *sensor = new upm::HWXPXX(defaultDev, 3); + // Instantiate an HWXPXX instance, using MODBUS slave address 3, and + // default comm parameters (19200, 8, N, 2) + upm::HWXPXX sensor(defaultDev, 3); - // output the Slave ID (manufacturer, model, serno) - cout << "Slave ID: " << sensor->getSlaveID() << endl; + // output the Slave ID (manufacturer, model, serno) + cout << "Slave ID: " << sensor.getSlaveID() << endl; - // stored temperature and humidity offsets - cout << "Temperature Offset: " << sensor->getTemperatureOffset() - << endl; - cout << "Humidity Offset: " << sensor->getHumidityOffset() - << endl; + // stored temperature and humidity offsets + cout << "Temperature Offset: " << sensor.getTemperatureOffset() << endl; + cout << "Humidity Offset: " << sensor.getHumidityOffset() << endl; - cout << endl; + cout << endl; - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor->getHumidity() - << " %" << endl; + cout << "Humidity: " << sensor.getHumidity() << " %" << endl; - cout << "Slider: " << sensor->getSlider() << " %" << endl; + cout << "Slider: " << sensor.getSlider() << " %" << endl; - cout << "Override Switch Status: " << sensor->getOverrideSwitchStatus() - << endl; + cout << "Override Switch Status: " << sensor.getOverrideSwitchStatus() << endl; - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/hx711.cxx b/examples/c++/hx711.cxx index e894ad83..4dedd4ec 100644 --- a/examples/c++/hx711.cxx +++ b/examples/c++/hx711.cxx @@ -4,39 +4,41 @@ * Copyright (c) 2015 Intel Corporation. * * -* Permission is hereby granted, free of charge, to any person obtaining a copy of +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to -* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -* the Software, and to permit persons to whom the Software is furnished to do so, +* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +* of +* the Software, and to permit persons to whom the Software is furnished to do +* so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include //! [Interesting] #include "hx711.hpp" int -main(int argc, char **argv) +main(int argc, char** argv) { - upm::HX711 *scale = new upm::HX711(3, 2); + upm::HX711 scale(3, 2); // 2837: value obtained via calibration - scale->setScale(2837); - scale->tare(); - std::cout << scale->getUnits() << std::endl; + scale.setScale(2837); + scale.tare(); + std::cout << scale.getUnits() << std::endl; return 0; } diff --git a/examples/c++/i2clcd-eboled.cxx b/examples/c++/i2clcd-eboled.cxx index 08325373..220fc26c 100644 --- a/examples/c++/i2clcd-eboled.cxx +++ b/examples/c++/i2clcd-eboled.cxx @@ -23,27 +23,29 @@ */ #include + #include "eboled.hpp" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an Edison Block OLED using default values - upm::EBOLED *lcd = new upm::EBOLED(); + //! [Interesting] + // Instantiate an Edison Block OLED using default values + upm::EBOLED lcd; - lcd->clear(); - lcd->setCursor(10, 15); - lcd->write("Hello"); - lcd->setCursor(30, 15); - lcd->write("World!"); - lcd->refresh(); - - cout << "Sleeping for 5 seconds..." << endl; - sleep(5); - - delete lcd; -//! [Interesting] - return 0; + lcd.clear(); + lcd.setCursor(10, 15); + lcd.write("Hello"); + lcd.setCursor(30, 15); + lcd.write("World!"); + lcd.refresh(); + + cout << "Sleeping for 5 seconds..." << endl; + upm_delay(5); + + //! [Interesting] + return 0; } diff --git a/examples/c++/i2clcd-ssd1306-oled.cxx b/examples/c++/i2clcd-ssd1306-oled.cxx index c0eef153..da1327f0 100644 --- a/examples/c++/i2clcd-ssd1306-oled.cxx +++ b/examples/c++/i2clcd-ssd1306-oled.cxx @@ -26,124 +26,124 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "ssd1306.hpp" +#include "upm_utilities.h" -#define DEVICE_ADDRESS 0x3C -#define BUS_NUMBER 0x0 +#define DEVICE_ADDRESS 0x3C +#define BUS_NUMBER 0x0 -static uint8_t intel_logo[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 192, 192, 192, 224, - 224, 224, 224, 240, 240, 248, 248, 120, 120, 120, 120, 60, 60, 60, 60, 60, - 62, 30, 30, 30, 30, 30, 30, 30, 31, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 31, - 30, 62, 62, 62, 62, 126, 126, 124, 124, 252, 252, 248, 248, 240, 240, 240, - 224, 224, 224, 192, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, - 128, 0, 56, 56, 28, 30, 14, 15, 15, 7, 7, 7, 7, 3, 3, 1, 1, 1, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 192, 193, 195, 195, - 195, 7, 15, 15, 63, 127, 255, 255, 255, 254, 252, 252, 240, 192, 0, 0, 0, - 0, 0, 0, 0, 0, 128, 192, 192, 240, 248, 124, 124, 60, 0, 0, 0, 0, 159, 159, - 159, 159, 159, 159, 159, 159, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, - 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, - 0, 0, 0, 0, 254, 254, 254, 254, 254, 254, 254, 254, 128, 128, 128, 128, - 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 192, 192, 192, 192, 192, 192, 128, - 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, - 0, 0, 0, 0, 3, 7, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 255, 255, 255, 255, 255, - 255, 255, 0, 0, 224, 248, 252, 252, 255, 127, 15, 15, 3, 1, 0, 0, 0, 0, 0, - 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, - 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, - 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 15, 15, - 15, 15, 15, 224, 224, 252, 254, 255, 255, 255, 255, 159, 159, 143, 143, - 135, 135, 143, 159, 255, 255, 255, 255, 255, 255, 252, 248, 0, 0, 0, 255, - 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, - 224, 248, 248, 255, 255, 255, 255, 255, 127, 15, 255, 255, 255, 255, 255, - 255, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, - 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, - 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, - 255, 255, 255, 255, 255, 192, 192, 192, 192, 192, 31, 31, 255, 255, 255, - 255, 255, 255, 231, 231, 199, 199, 199, 199, 199, 199, 199, 199, 231, 231, - 231, 231, 199, 135, 0, 0, 0, 63, 255, 255, 255, 255, 255, 255, 255, 0, 0, - 0, 0, 224, 240, 248, 248, 252, 254, 255, 255, 255, 127, 63, 63, 31, 15, 7, - 7, 1, 0, 0, 63, 63, 255, 255, 255, 255, 255, 240, 192, 192, 128, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 3, 3, 7, 7, 7, 7, 7, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, - 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 0, 0, 0, 1, 3, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 7, 7, 7, - 7, 7, 3, 3, 3, 1, 0, 0, 0, 0, 1, 3, 3, 7, 135, 135, 135, 192, 192, 0, 0, 7, - 7, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 15, 15, - 31, 127, 127, 127, 255, 255, 252, 252, 252, 248, 240, 240, 240, 224, 224, - 224, 192, 192, 192, 192, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 192, 192, 192, 192, 192, - 224, 224, 224, 224, 240, 240, 240, 240, 248, 248, 248, 248, 252, 252, 252, - 254, 254, 255, 255, 255, 255, 255, 255, 127, 127, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 3, 3, 3, 7, 7, 7, 15, 15, 31, 31, 31, 63, 63, 63, 63, 63, 127, 127, 127, - 127, 127, 255, 255, 255, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 255, 255, 255, 255, 255, 255, 255, 127, 127, 127, 127, 127, 127, 127, 127, - 63, 63, 63, 63, 63, 31, 31, 31, 31, 31, 15, 15, 15, 15, 7, 7, 7, 7, 3, 3, - 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0 - }; +static uint8_t intel_logo[] = +{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, + 128, 128, 192, 192, 192, 224, 224, 224, 224, 240, 240, 248, 248, 120, 120, 120, 120, 60, 60, + 60, 60, 60, 62, 30, 30, 30, 30, 30, 30, 30, 31, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, + 31, 31, 31, 30, 62, 62, 62, 62, 126, 126, 124, 124, 252, 252, 248, 248, 240, 240, 240, + 224, 224, 224, 192, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 56, 56, 28, 30, 14, 15, 15, 7, 7, 7, 7, 3, 3, 1, 1, + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 192, 192, + 192, 192, 192, 192, 192, 0, 0, 0, 0, 192, 193, 195, 195, 195, 7, 15, 15, 63, 127, + 255, 255, 255, 254, 252, 252, 240, 192, 0, 0, 0, 0, 0, 0, 0, 0, 128, 192, 192, + 240, 248, 124, 124, 60, 0, 0, 0, 0, 159, 159, 159, 159, 159, 159, 159, 159, 0, 0, + 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 254, 254, 254, 254, 254, 254, 254, 254, 128, 128, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 192, 192, 192, 192, 192, 192, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 0, 0, 0, 3, 7, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 255, 255, 255, + 255, 255, 255, 255, 0, 0, 224, 248, 252, 252, 255, 127, 15, 15, 3, 1, 0, 0, 0, + 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, + 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, 255, 255, 252, + 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 224, 224, + 252, 254, 255, 255, 255, 255, 159, 159, 143, 143, 135, 135, 143, 159, 255, 255, 255, 255, 255, + 255, 252, 248, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 128, 224, 248, 248, 255, 255, 255, 255, 255, 127, 15, 255, + 255, 255, 255, 255, 255, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, + 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, + 255, 255, 255, 255, 255, 255, 255, 192, 192, 192, 192, 192, 31, 31, 255, 255, 255, 255, 255, + 255, 231, 231, 199, 199, 199, 199, 199, 199, 199, 199, 231, 231, 231, 231, 199, 135, 0, 0, + 0, 63, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 224, 240, 248, 248, 252, 254, + 255, 255, 255, 127, 63, 63, 31, 15, 7, 7, 1, 0, 0, 63, 63, 255, 255, 255, 255, + 255, 240, 192, 192, 128, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 7, 7, 7, + 7, 7, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, + 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 1, 3, 3, 3, 7, 7, 7, 7, 15, + 15, 15, 15, 7, 7, 7, 7, 7, 3, 3, 3, 1, 0, 0, 0, 0, 1, 3, 3, + 7, 135, 135, 135, 192, 192, 0, 0, 7, 7, 3, 3, 3, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 15, 15, 31, 127, 127, 127, 255, + 255, 252, 252, 252, 248, 240, 240, 240, 224, 224, 224, 192, 192, 192, 192, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 128, 128, 128, 128, 128, 128, 128, 192, 192, 192, 192, 192, 224, 224, 224, 224, 240, + 240, 240, 240, 248, 248, 248, 248, 252, 252, 252, 254, 254, 255, 255, 255, 255, 255, 255, 127, + 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 3, 7, + 7, 7, 15, 15, 31, 31, 31, 63, 63, 63, 63, 63, 127, 127, 127, 127, 127, 255, 255, + 255, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 127, 127, 127, 127, 127, + 127, 127, 127, 63, 63, 63, 63, 63, 31, 31, 31, 31, 31, 15, 15, 15, 15, 7, 7, + 7, 7, 3, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - upm::SSD1306 *lcd = new upm::SSD1306(BUS_NUMBER, DEVICE_ADDRESS); + //! [Interesting] + upm::SSD1306 lcd(BUS_NUMBER, DEVICE_ADDRESS); - lcd->clear(); - lcd->stopscroll(); - lcd->draw(intel_logo, 1024); + lcd.clear(); + lcd.stopscroll(); + lcd.draw(intel_logo, 1024); - sleep(3); + upm_delay(3); - lcd->clear(); - lcd->setCursor(2, 0); - lcd->write("Hello"); - lcd->setCursor(3,0); - lcd->write("World"); + lcd.clear(); + lcd.setCursor(2, 0); + lcd.write("Hello"); + lcd.setCursor(3, 0); + lcd.write("World"); - sleep(3); + upm_delay(3); - lcd->invert(true); + lcd.invert(true); - sleep(3); + upm_delay(3); - lcd->dim(true); + lcd.dim(true); - sleep(3); + upm_delay(3); - lcd->dim(false); + lcd.dim(false); - sleep(3); + upm_delay(3); - lcd->invert(false); - lcd->startscrollright(0x00, 0x0F); + lcd.invert(false); + lcd.startscrollright(0x00, 0x0F); - sleep(5); + upm_delay(5); - lcd->stopscroll(); - lcd->startscrollleft(0x00, 0x0F); + lcd.stopscroll(); + lcd.startscrollleft(0x00, 0x0F); - sleep(5); + upm_delay(5); - lcd->stopscroll(); - lcd->startscrolldiagleft(0x00,0x0F); + lcd.stopscroll(); + lcd.startscrolldiagleft(0x00, 0x0F); - sleep(5); + upm_delay(5); - lcd->stopscroll(); - lcd->startscrolldiagright(0x00,0x0F); + lcd.stopscroll(); + lcd.startscrolldiagright(0x00, 0x0F); - sleep(5); + upm_delay(5); - lcd->stopscroll(); + lcd.stopscroll(); - delete lcd; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/i2clcd-ssd1308-oled.cxx b/examples/c++/i2clcd-ssd1308-oled.cxx index c78e8b85..999483d5 100644 --- a/examples/c++/i2clcd-ssd1308-oled.cxx +++ b/examples/c++/i2clcd-ssd1308-oled.cxx @@ -22,78 +22,78 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "ssd1308.hpp" -#define DEVICE_ADDRESS 0x3C -#define BUS_NUMBER 0x0 +#define DEVICE_ADDRESS 0x3C +#define BUS_NUMBER 0x0 -static uint8_t intel_logo[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 192, 192, 192, 224, - 224, 224, 224, 240, 240, 248, 248, 120, 120, 120, 120, 60, 60, 60, 60, 60, - 62, 30, 30, 30, 30, 30, 30, 30, 31, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 31, - 30, 62, 62, 62, 62, 126, 126, 124, 124, 252, 252, 248, 248, 240, 240, 240, - 224, 224, 224, 192, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, - 128, 0, 56, 56, 28, 30, 14, 15, 15, 7, 7, 7, 7, 3, 3, 1, 1, 1, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 192, 193, 195, 195, - 195, 7, 15, 15, 63, 127, 255, 255, 255, 254, 252, 252, 240, 192, 0, 0, 0, - 0, 0, 0, 0, 0, 128, 192, 192, 240, 248, 124, 124, 60, 0, 0, 0, 0, 159, 159, - 159, 159, 159, 159, 159, 159, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, - 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, - 0, 0, 0, 0, 254, 254, 254, 254, 254, 254, 254, 254, 128, 128, 128, 128, - 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 192, 192, 192, 192, 192, 192, 128, - 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, - 0, 0, 0, 0, 3, 7, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 255, 255, 255, 255, 255, - 255, 255, 0, 0, 224, 248, 252, 252, 255, 127, 15, 15, 3, 1, 0, 0, 0, 0, 0, - 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, - 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, - 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 15, 15, - 15, 15, 15, 224, 224, 252, 254, 255, 255, 255, 255, 159, 159, 143, 143, - 135, 135, 143, 159, 255, 255, 255, 255, 255, 255, 252, 248, 0, 0, 0, 255, - 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, - 224, 248, 248, 255, 255, 255, 255, 255, 127, 15, 255, 255, 255, 255, 255, - 255, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, - 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, - 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, - 255, 255, 255, 255, 255, 192, 192, 192, 192, 192, 31, 31, 255, 255, 255, - 255, 255, 255, 231, 231, 199, 199, 199, 199, 199, 199, 199, 199, 231, 231, - 231, 231, 199, 135, 0, 0, 0, 63, 255, 255, 255, 255, 255, 255, 255, 0, 0, - 0, 0, 224, 240, 248, 248, 252, 254, 255, 255, 255, 127, 63, 63, 31, 15, 7, - 7, 1, 0, 0, 63, 63, 255, 255, 255, 255, 255, 240, 192, 192, 128, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 3, 3, 7, 7, 7, 7, 7, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, - 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, 7, - 7, 7, 7, 7, 7, 7, 0, 0, 0, 1, 3, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 7, 7, 7, - 7, 7, 3, 3, 3, 1, 0, 0, 0, 0, 1, 3, 3, 7, 135, 135, 135, 192, 192, 0, 0, 7, - 7, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 15, 15, - 31, 127, 127, 127, 255, 255, 252, 252, 252, 248, 240, 240, 240, 224, 224, - 224, 192, 192, 192, 192, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 192, 192, 192, 192, 192, - 224, 224, 224, 224, 240, 240, 240, 240, 248, 248, 248, 248, 252, 252, 252, - 254, 254, 255, 255, 255, 255, 255, 255, 127, 127, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 3, 3, 3, 7, 7, 7, 15, 15, 31, 31, 31, 63, 63, 63, 63, 63, 127, 127, 127, - 127, 127, 255, 255, 255, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, - 255, 255, 255, 255, 255, 255, 255, 127, 127, 127, 127, 127, 127, 127, 127, - 63, 63, 63, 63, 63, 31, 31, 31, 31, 31, 15, 15, 15, 15, 7, 7, 7, 7, 3, 3, - 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0 }; +static uint8_t intel_logo[] = +{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, + 128, 128, 192, 192, 192, 224, 224, 224, 224, 240, 240, 248, 248, 120, 120, 120, 120, 60, 60, + 60, 60, 60, 62, 30, 30, 30, 30, 30, 30, 30, 31, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, + 31, 31, 31, 30, 62, 62, 62, 62, 126, 126, 124, 124, 252, 252, 248, 248, 240, 240, 240, + 224, 224, 224, 192, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 56, 56, 28, 30, 14, 15, 15, 7, 7, 7, 7, 3, 3, 1, 1, + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 192, 192, + 192, 192, 192, 192, 192, 0, 0, 0, 0, 192, 193, 195, 195, 195, 7, 15, 15, 63, 127, + 255, 255, 255, 254, 252, 252, 240, 192, 0, 0, 0, 0, 0, 0, 0, 0, 128, 192, 192, + 240, 248, 124, 124, 60, 0, 0, 0, 0, 159, 159, 159, 159, 159, 159, 159, 159, 0, 0, + 0, 0, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 254, 254, 254, 254, 254, 254, 254, 254, 128, 128, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 192, 192, 192, 192, 192, 192, + 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 0, 0, 0, 3, 7, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 255, 255, 255, + 255, 255, 255, 255, 0, 0, 224, 248, 252, 252, 255, 127, 15, 15, 3, 1, 0, 0, 0, + 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, + 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, 255, 255, 252, + 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 224, 224, + 252, 254, 255, 255, 255, 255, 159, 159, 143, 143, 135, 135, 143, 159, 255, 255, 255, 255, 255, + 255, 252, 248, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 128, 224, 248, 248, 255, 255, 255, 255, 255, 127, 15, 255, + 255, 255, 255, 255, 255, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, + 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, + 255, 255, 255, 255, 255, 255, 255, 192, 192, 192, 192, 192, 31, 31, 255, 255, 255, 255, 255, + 255, 231, 231, 199, 199, 199, 199, 199, 199, 199, 199, 231, 231, 231, 231, 199, 135, 0, 0, + 0, 63, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 224, 240, 248, 248, 252, 254, + 255, 255, 255, 127, 63, 63, 31, 15, 7, 7, 1, 0, 0, 63, 63, 255, 255, 255, 255, + 255, 240, 192, 192, 128, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 7, 7, 7, + 7, 7, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, + 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 1, 3, 3, 3, 7, 7, 7, 7, 15, + 15, 15, 15, 7, 7, 7, 7, 7, 3, 3, 3, 1, 0, 0, 0, 0, 1, 3, 3, + 7, 135, 135, 135, 192, 192, 0, 0, 7, 7, 3, 3, 3, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 15, 15, 31, 127, 127, 127, 255, + 255, 252, 252, 252, 248, 240, 240, 240, 224, 224, 224, 192, 192, 192, 192, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 128, 128, 128, 128, 128, 128, 128, 192, 192, 192, 192, 192, 224, 224, 224, 224, 240, + 240, 240, 240, 248, 248, 248, 248, 252, 252, 252, 254, 254, 255, 255, 255, 255, 255, 255, 127, + 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3, 3, 7, + 7, 7, 15, 15, 31, 31, 31, 63, 63, 63, 63, 63, 127, 127, 127, 127, 127, 255, 255, + 255, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 127, 127, 127, 127, 127, + 127, 127, 127, 63, 63, 63, 63, 63, 31, 31, 31, 31, 31, 15, 15, 15, 15, 7, 7, + 7, 7, 3, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - upm::SSD1308 *lcd = new upm::SSD1308(BUS_NUMBER, DEVICE_ADDRESS); + //! [Interesting] + upm::SSD1308 lcd(BUS_NUMBER, DEVICE_ADDRESS); - lcd->clear(); - lcd->draw(intel_logo, 1024); + lcd.clear(); + lcd.draw(intel_logo, 1024); - delete lcd; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/i2clcd-ssd1327-oled.cxx b/examples/c++/i2clcd-ssd1327-oled.cxx index d087f700..aa3d6cc1 100644 --- a/examples/c++/i2clcd-ssd1327-oled.cxx +++ b/examples/c++/i2clcd-ssd1327-oled.cxx @@ -22,106 +22,104 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "ssd1327.hpp" -#define DEVICE_ADDRESS 0x3C -#define BUS_NUMBER 0x0 - -static uint8_t SeeedLogo[] ={ -0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x60, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x06, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x03, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x01, 0xC0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, -0x07, 0x80, 0x01, 0xE0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0F, 0x80, 0x01, 0xE0, -0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, 0x08, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, -0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, -0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x38, 0x0F, 0x00, 0x01, 0xE0, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, -0x0F, 0x80, 0x01, 0xE0, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x0F, 0x80, 0x01, 0xE0, -0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x0F, 0x80, 0x03, 0xE0, 0x78, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x1E, 0x07, 0x80, 0x03, 0xE0, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, -0x07, 0x80, 0x03, 0xE0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x07, 0x80, 0x03, 0xC1, -0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x87, 0xC0, 0x07, 0xC1, 0xF0, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x0F, 0x83, 0xC0, 0x07, 0x83, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, -0xC3, 0xC0, 0x07, 0x87, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE1, 0xE0, 0x07, 0x0F, -0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0xE0, 0x0F, 0x0F, 0x80, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x01, 0xF8, 0xF0, 0x0E, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, -0xF8, 0x70, 0x1C, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x30, 0x18, 0x7E, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x18, 0x30, 0xFC, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x1F, 0x88, 0x21, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x0F, 0xC4, 0x47, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x0F, 0x80, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x06, -0x00, 0x00, 0x60, 0x00, 0x7E, 0x3F, 0x0F, 0xC3, 0xF0, 0xFA, 0x0F, 0xDF, 0xE1, 0x9F, 0xEC, 0x7E, -0xE6, 0x73, 0x9C, 0xE7, 0x39, 0xCE, 0x1C, 0xDF, 0xE1, 0xB9, 0xEC, 0xE7, 0xE0, 0x61, 0xD8, 0x66, -0x1B, 0x86, 0x1C, 0x06, 0x61, 0xB0, 0x6D, 0xC3, 0x7C, 0x7F, 0xFF, 0xFF, 0xFF, 0x06, 0x0F, 0x86, -0x61, 0xB0, 0x6D, 0x83, 0x3E, 0x7F, 0xFF, 0xFF, 0xFF, 0x06, 0x07, 0xC6, 0x61, 0xB0, 0x6D, 0x83, -0xC3, 0x61, 0x18, 0x46, 0x03, 0x86, 0x18, 0x66, 0x61, 0xB0, 0x6D, 0xC3, 0xFE, 0x7F, 0x9F, 0xE7, -0xF9, 0xFE, 0x1F, 0xE6, 0x3F, 0x9F, 0xEC, 0xFE, 0x7E, 0x3F, 0x0F, 0xC3, 0xF0, 0xFA, 0x0F, 0xC6, -0x3F, 0x9F, 0xEC, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, -0x00, 0x20, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x20, 0x82, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0xF3, 0xCF, 0x70, 0x9E, 0x79, 0xE7, 0x80, 0x00, 0x00, -0x00, 0x00, 0x7D, 0x9E, 0x68, 0x20, 0xB2, 0xC8, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x9E, -0x6F, 0x20, 0xB2, 0xF9, 0xE7, 0x80, 0x00, 0x00, 0x00, 0x00, 0x46, 0x9A, 0x61, 0x20, 0xB2, 0xCB, -0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xF3, 0xCF, 0x30, 0x9E, 0x79, 0xE7, 0x90, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x02, 0x00, 0x00, 0x82, 0x60, 0x00, 0x00, -0xF8, 0x00, 0x00, 0x40, 0x40, 0x02, 0x00, 0x00, 0x83, 0x60, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x40, -0x60, 0xB7, 0x79, 0xE7, 0x81, 0xC7, 0x92, 0x70, 0x89, 0xE7, 0x9E, 0x78, 0x7C, 0xE2, 0xC9, 0x2C, -0x81, 0xCC, 0xD2, 0x40, 0xFB, 0x21, 0xB2, 0x48, 0x40, 0x62, 0xF9, 0x2C, 0x80, 0x8C, 0xD2, 0x40, -0x8B, 0xE7, 0xB0, 0x48, 0x40, 0xE2, 0xC9, 0x2C, 0x80, 0x84, 0xD2, 0x40, 0x8B, 0x2D, 0x92, 0x48, -0x7D, 0xB3, 0x79, 0x27, 0x80, 0x87, 0x9E, 0x40, 0x8D, 0xE7, 0x9E, 0x48, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; +#define DEVICE_ADDRESS 0x3C +#define BUS_NUMBER 0x0 +static uint8_t SeeedLogo[] = +{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x60, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x06, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x01, 0xC0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, + 0x07, 0x80, 0x01, 0xE0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0F, 0x80, 0x01, 0xE0, + 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, 0x08, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, + 0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0F, 0x00, 0x01, 0xE0, + 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0F, 0x00, 0x01, 0xE0, 0x18, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x38, 0x0F, 0x00, 0x01, 0xE0, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, + 0x0F, 0x80, 0x01, 0xE0, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x0F, 0x80, 0x01, 0xE0, + 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x0F, 0x80, 0x03, 0xE0, 0x78, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x1E, 0x07, 0x80, 0x03, 0xE0, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, + 0x07, 0x80, 0x03, 0xE0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x07, 0x80, 0x03, 0xC1, + 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x87, 0xC0, 0x07, 0xC1, 0xF0, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x0F, 0x83, 0xC0, 0x07, 0x83, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, + 0xC3, 0xC0, 0x07, 0x87, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE1, 0xE0, 0x07, 0x0F, + 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0xE0, 0x0F, 0x0F, 0x80, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0xF8, 0xF0, 0x0E, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0xF8, 0x70, 0x1C, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x30, 0x18, 0x7E, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x18, 0x30, 0xFC, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x1F, 0x88, 0x21, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0F, 0xC4, 0x47, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x0F, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x06, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x06, + 0x00, 0x00, 0x60, 0x00, 0x7E, 0x3F, 0x0F, 0xC3, 0xF0, 0xFA, 0x0F, 0xDF, 0xE1, 0x9F, 0xEC, 0x7E, + 0xE6, 0x73, 0x9C, 0xE7, 0x39, 0xCE, 0x1C, 0xDF, 0xE1, 0xB9, 0xEC, 0xE7, 0xE0, 0x61, 0xD8, 0x66, + 0x1B, 0x86, 0x1C, 0x06, 0x61, 0xB0, 0x6D, 0xC3, 0x7C, 0x7F, 0xFF, 0xFF, 0xFF, 0x06, 0x0F, 0x86, + 0x61, 0xB0, 0x6D, 0x83, 0x3E, 0x7F, 0xFF, 0xFF, 0xFF, 0x06, 0x07, 0xC6, 0x61, 0xB0, 0x6D, 0x83, + 0xC3, 0x61, 0x18, 0x46, 0x03, 0x86, 0x18, 0x66, 0x61, 0xB0, 0x6D, 0xC3, 0xFE, 0x7F, 0x9F, 0xE7, + 0xF9, 0xFE, 0x1F, 0xE6, 0x3F, 0x9F, 0xEC, 0xFE, 0x7E, 0x3F, 0x0F, 0xC3, 0xF0, 0xFA, 0x0F, 0xC6, + 0x3F, 0x9F, 0xEC, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, + 0x00, 0x20, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x20, 0x82, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0xF3, 0xCF, 0x70, 0x9E, 0x79, 0xE7, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x7D, 0x9E, 0x68, 0x20, 0xB2, 0xC8, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x9E, + 0x6F, 0x20, 0xB2, 0xF9, 0xE7, 0x80, 0x00, 0x00, 0x00, 0x00, 0x46, 0x9A, 0x61, 0x20, 0xB2, 0xCB, + 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xF3, 0xCF, 0x30, 0x9E, 0x79, 0xE7, 0x90, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x02, 0x00, 0x00, 0x82, 0x60, 0x00, 0x00, + 0xF8, 0x00, 0x00, 0x40, 0x40, 0x02, 0x00, 0x00, 0x83, 0x60, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x40, + 0x60, 0xB7, 0x79, 0xE7, 0x81, 0xC7, 0x92, 0x70, 0x89, 0xE7, 0x9E, 0x78, 0x7C, 0xE2, 0xC9, 0x2C, + 0x81, 0xCC, 0xD2, 0x40, 0xFB, 0x21, 0xB2, 0x48, 0x40, 0x62, 0xF9, 0x2C, 0x80, 0x8C, 0xD2, 0x40, + 0x8B, 0xE7, 0xB0, 0x48, 0x40, 0xE2, 0xC9, 0x2C, 0x80, 0x84, 0xD2, 0x40, 0x8B, 0x2D, 0x92, 0x48, + 0x7D, 0xB3, 0x79, 0x27, 0x80, 0x87, 0x9E, 0x40, 0x8D, 0xE7, 0x9E, 0x48, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - upm::SSD1327 *lcd = new upm::SSD1327(BUS_NUMBER, 0x3C); + //! [Interesting] + upm::SSD1327 lcd(BUS_NUMBER, 0x3C); - // If you don't set the display to be white, the seeed logo will appear jagged - lcd->setGrayLevel(12); - lcd->draw(SeeedLogo, 96 * 96 / 8); + // If you don't set the display to be white, the seeed logo will appear jagged + lcd.setGrayLevel(12); + lcd.draw(SeeedLogo, 96 * 96 / 8); - // Simple print hello world - for (uint8_t i = 0; i < 12; i++) - { - lcd->setCursor(i, 0); - lcd->setGrayLevel(i); - lcd->write("Hello World"); - } + // Simple print hello world + for (uint8_t i = 0; i < 12; i++) { + lcd.setCursor(i, 0); + lcd.setGrayLevel(i); + lcd.write("Hello World"); + } - delete lcd; -//! [Interesting] - return 0; + //! [Interesting] + return 0; } diff --git a/examples/c++/ili9341.cxx b/examples/c++/ili9341.cxx index 139615c9..cde0ff6a 100644 --- a/examples/c++/ili9341.cxx +++ b/examples/c++/ili9341.cxx @@ -22,63 +22,63 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include - #include "ili9341.hpp" +#include "upm_utilities.h" -int main(int argc, char **argv) { -//! [Interesting] +int +main(int argc, char** argv) +{ + //! [Interesting] // Pins (Edison) // CS_LCD GP44 (MRAA 31) // CS_SD GP43 (MRAA 38) unused // DC GP12 (MRAA 20) // RESEST GP13 (MRAA 14) - upm::ILI9341 * lcd = new upm::ILI9341(31, 38, 20, 14); + upm::ILI9341 lcd(31, 38, 20, 14); // Fill the screen with a solid color - lcd->fillScreen(lcd->color565(0, 40, 16)); + lcd.fillScreen(lcd.color565(0, 40, 16)); // Draw some shapes - lcd->drawFastVLine(10, 10, 100, ILI9341_RED); - lcd->drawFastHLine(20, 10, 50, ILI9341_CYAN); - lcd->drawLine(160, 30, 200, 60, ILI9341_GREEN); - lcd->fillRect(20, 30, 75, 60, ILI9341_ORANGE); - lcd->drawCircle(70, 50, 20, ILI9341_PURPLE); - lcd->fillCircle(120, 50, 20, ILI9341_PURPLE); - lcd->drawTriangle(50, 100, 10, 140, 90, 140, ILI9341_YELLOW); - lcd->fillTriangle(150, 100, 110, 140, 190, 140, ILI9341_YELLOW); - lcd->drawRoundRect(20, 150, 50, 30, 10, ILI9341_RED); - lcd->drawRoundRect(130, 150, 50, 30, 10, ILI9341_RED); - lcd->fillRoundRect(75, 150, 50, 30, 10, ILI9341_RED); + lcd.drawFastVLine(10, 10, 100, ILI9341_RED); + lcd.drawFastHLine(20, 10, 50, ILI9341_CYAN); + lcd.drawLine(160, 30, 200, 60, ILI9341_GREEN); + lcd.fillRect(20, 30, 75, 60, ILI9341_ORANGE); + lcd.drawCircle(70, 50, 20, ILI9341_PURPLE); + lcd.fillCircle(120, 50, 20, ILI9341_PURPLE); + lcd.drawTriangle(50, 100, 10, 140, 90, 140, ILI9341_YELLOW); + lcd.fillTriangle(150, 100, 110, 140, 190, 140, ILI9341_YELLOW); + lcd.drawRoundRect(20, 150, 50, 30, 10, ILI9341_RED); + lcd.drawRoundRect(130, 150, 50, 30, 10, ILI9341_RED); + lcd.fillRoundRect(75, 150, 50, 30, 10, ILI9341_RED); // Write some text - lcd->setCursor(0, 200); - lcd->setTextColor(ILI9341_LIGHTGREY); - lcd->setTextWrap(true); - lcd->setTextSize(1); - lcd->print("Text 1\n"); - lcd->setTextSize(2); - lcd->print("Text 2\n"); - lcd->setTextSize(3); - lcd->print("Text 3\n"); - lcd->setTextSize(4); - lcd->print("Text 4\n"); - + lcd.setCursor(0, 200); + lcd.setTextColor(ILI9341_LIGHTGREY); + lcd.setTextWrap(true); + lcd.setTextSize(1); + lcd.print("Text 1\n"); + lcd.setTextSize(2); + lcd.print("Text 2\n"); + lcd.setTextSize(3); + lcd.print("Text 3\n"); + lcd.setTextSize(4); + lcd.print("Text 4\n"); + // Test screen rotation - for(int r = 0; r < 4; r++) { - lcd->setRotation(r); - lcd->fillRect(0, 0, 5, 5, ILI9341_WHITE); - sleep(1); + for (int r = 0; r < 4; r++) { + lcd.setRotation(r); + lcd.fillRect(0, 0, 5, 5, ILI9341_WHITE); + upm_delay(1); } // Invert colors, wait, then revert back - lcd->invertDisplay(true); - sleep(2); - lcd->invertDisplay(false); + lcd.invertDisplay(true); + upm_delay(2); + lcd.invertDisplay(false); // Don't forget to free up that memory! - delete lcd; -//! [Interesting] + //! [Interesting] return 0; -} \ No newline at end of file +} diff --git a/examples/c++/ims.cxx b/examples/c++/ims.cxx index 6d96ab56..ae574dc8 100644 --- a/examples/c++/ims.cxx +++ b/examples/c++/ims.cxx @@ -22,23 +22,26 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "ims.hpp" +#include "ims_defs.h" +#include "upm_utilities.h" using namespace upm; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); //! [Interesting] @@ -47,25 +50,17 @@ int main(int argc, char **argv) upm::IMS sensor(0); int i2c_addr_cur = IMS_ADDRESS_DEFAULT + 1; - while (shouldRun) - { - std::cout << std::hex << "Version: 0x" - << sensor.get_version() - << " light: 0x" - << sensor.get_light() - << " moisture: 0x" - << sensor.get_moisture() - << " temp: " - << sensor.get_temperature() - << " C" - << std::endl; + while (shouldRun) { + std::cout << std::hex << "Version: 0x" << sensor.get_version() << " light: 0x" + << sensor.get_light() << " moisture: 0x" << sensor.get_moisture() + << " temp: " << sensor.get_temperature() << " C" << std::endl; // Change the address and continue - if (i2c_addr_cur >= 128) i2c_addr_cur = 1; - std::cout << "Changing device address to 0x" << i2c_addr_cur - << std::endl; + if (i2c_addr_cur >= 128) + i2c_addr_cur = 1; + std::cout << "Changing device address to 0x" << i2c_addr_cur << std::endl; sensor.reset_i2c_address(i2c_addr_cur++); - sleep(1); + upm_delay(1); } //! [Interesting] diff --git a/examples/c++/ina132.cxx b/examples/c++/ina132.cxx index 7bc6457e..8cb6ee43 100644 --- a/examples/c++/ina132.cxx +++ b/examples/c++/ina132.cxx @@ -22,40 +22,41 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "ina132.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the INA132 Differential Amplifier Sensor module. + //! [Interesting] + // The was tested with the INA132 Differential Amplifier Sensor module. - // Instantiate an INA132 on analog pin A0 - upm::INA132 *amplifier = new upm::INA132(0); + // Instantiate an INA132 on analog pin A0 + upm::INA132 amplifier(0); - while (shouldRun) - { - cout << amplifier->value() << endl; - sleep(1); - } -//! [Interesting] + while (shouldRun) { + cout << amplifier.value() << endl; + upm_delay(1); + } + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete amplifier; - return 0; + return 0; } diff --git a/examples/c++/interfaces-humiditysensor.cxx b/examples/c++/interfaces-humiditysensor.cxx index 8bb7eb4f..52973dc3 100644 --- a/examples/c++/interfaces-humiditysensor.cxx +++ b/examples/c++/interfaces-humiditysensor.cxx @@ -22,62 +22,67 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include -#include "si7005.hpp" -#include "bme280.hpp" +#include -#define EDISON_I2C_BUS 1 +#include "bme280.hpp" +#include "iHumiditySensor.hpp" +#include "mraa/common.h" +#include "si7005.hpp" +#include "upm_utilities.h" + +#define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 - #define EDISON_GPIO_SI7005_CS 20 +#define EDISON_GPIO_SI7005_CS 20 //! [Interesting] -// Simple example of using ILightSensor to determine +// Simple example of using ILightSensor to determine // which sensor is present and return its name. // ILightSensor is then used to get readings from sensor - -upm::IHumiditySensor* getHumiditySensor() +upm::IHumiditySensor* +getHumiditySensor() { - upm::IHumiditySensor* humiditySensor = NULL; + upm::IHumiditySensor* humiditySensor = NULL; - try { - humiditySensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); - return humiditySensor ; - } catch (std::exception& e) - { - std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; - while (true) { - try { - int value = humiditySensor->getHumidityRelative(); - std::cout << "Humidity = " << value << "%" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - sleep(1); - } - delete humiditySensor; - return 0; + upm::IHumiditySensor* humiditySensor = getHumiditySensor(); + if (humiditySensor == NULL) { + std::cout << "Humidity sensor not detected" << std::endl; + return 1; + } + std::cout << "Humidity sensor " << humiditySensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + int value = humiditySensor->getHumidityRelative(); + std::cout << "Humidity = " << value << "%" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + upm_delay(1); + } + delete humiditySensor; + return 0; } -//! [Interesting] +//! [Interesting] diff --git a/examples/c++/interfaces-lightcontroller.cxx b/examples/c++/interfaces-lightcontroller.cxx index 8907074d..cc99a119 100644 --- a/examples/c++/interfaces-lightcontroller.cxx +++ b/examples/c++/interfaces-lightcontroller.cxx @@ -1,98 +1,98 @@ -#include -#include -#include -#include -#include -#include "lp8860.hpp" -#include "ds1808lc.hpp" -#include "hlg150h.hpp" - -#define EDISON_I2C_BUS 1 // Edison I2C-1 -#define GPIO_SI7005_CS 20 // Edison GP12 -#define HLG150H_GPIO_RELAY 21 -#define HLG150H_GPIO_PWM 22 -#define LP8860_GPIO_PWR 45 // Edison GP45 -#define DS1808_GPIO_PWR 15 // Edison GP165 -#define DS1808_GPIO_EDISON_LIVE 36 // Edison GP14 - -//! [Interesting] -// Simple example of using ILightController to determine -// which controller is present and return its name. -// ILightController is then used to get readings from sensor - -upm::ILightController* getLightController() -{ - upm::ILightController* lightController = NULL; - try { - lightController = new upm::LP8860(LP8860_GPIO_PWR, EDISON_I2C_BUS); - return lightController; - } catch (std::exception& e) { - std::cerr << "LP8860: " << e.what() << std::endl; - } - try { - lightController = new upm::DS1808LC(DS1808_GPIO_PWR, EDISON_I2C_BUS); - return lightController; - } catch (std::exception& e) { - std::cerr << "DS1808LC: " << e.what() << std::endl; - } - try { - lightController = new upm::HLG150H(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM); - return lightController; - } catch (std::exception& e) { - std::cerr << "HLG150H: " << e.what() << std::endl; - } - return lightController; -} - - -void printState(upm::ILightController *lightController) -{ - if (lightController->isPowered()) - { - std::cout << "Light is powered, brightness = " << lightController->getBrightness() << std::endl; - } - else - { - std::cout << "Light is not powered." << std::endl; - } -} - -int main( int argc, char **argv ) -{ - int status = 0; - // MraaUtils::setGpio(GPIO_SI7005_CS, 1); - - upm::ILightController *lightController = getLightController(); - if (lightController != NULL) - { - std::cout << "Detected light controller " << lightController->getModuleName() << std::endl; - } - else - { - std::cerr << "Error. Unsupported platform." << std::endl; - return 1; - } - - try { - std::cout << "Existing state: "; printState(lightController); - if (argc == 2) - { - std::string arg = argv[1]; - int brightness = ::atoi(argv[1]); - if (brightness > 0) { - lightController->setPowerOn(); - lightController->setBrightness(brightness); - } else - lightController->setPowerOff(); - } - std::cout << "Now: ";printState(lightController); - } catch (std::exception& e) { - std::cout << "Error: " << e.what() << std::endl; - status = 1; - } - - delete lightController; - return status; -} - -//! [Interesting] +#include +#include +#include +#include +#include + +#include "ds1808lc.hpp" +#include "hlg150h.hpp" +#include "iLightController.hpp" +#include "lp8860.hpp" + +#define EDISON_I2C_BUS 1 // Edison I2C-1 +#define GPIO_SI7005_CS 20 // Edison GP12 +#define HLG150H_GPIO_RELAY 21 +#define HLG150H_GPIO_PWM 22 +#define LP8860_GPIO_PWR 45 // Edison GP45 +#define DS1808_GPIO_PWR 15 // Edison GP165 +#define DS1808_GPIO_EDISON_LIVE 36 // Edison GP14 + +//! [Interesting] +// Simple example of using ILightController to determine +// which controller is present and return its name. +// ILightController is then used to get readings from sensor + +upm::ILightController* +getLightController() +{ + upm::ILightController* lightController = NULL; + try { + lightController = new upm::LP8860(LP8860_GPIO_PWR, EDISON_I2C_BUS); + return lightController; + } catch (std::exception& e) { + std::cerr << "LP8860: " << e.what() << std::endl; + } + try { + lightController = new upm::DS1808LC(DS1808_GPIO_PWR, EDISON_I2C_BUS); + return lightController; + } catch (std::exception& e) { + std::cerr << "DS1808LC: " << e.what() << std::endl; + } + try { + lightController = new upm::HLG150H(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM); + return lightController; + } catch (std::exception& e) { + std::cerr << "HLG150H: " << e.what() << std::endl; + } + return lightController; +} + +void +printState(upm::ILightController* lightController) +{ + if (lightController->isPowered()) { + std::cout << "Light is powered, brightness = " << lightController->getBrightness() + << std::endl; + } else { + std::cout << "Light is not powered." << std::endl; + } +} + +int +main(int argc, char** argv) +{ + int status = 0; + // MraaUtils::setGpio(GPIO_SI7005_CS, 1); + + upm::ILightController* lightController = getLightController(); + if (lightController != NULL) { + std::cout << "Detected light controller " << lightController->getModuleName() << std::endl; + } else { + std::cerr << "Error. Unsupported platform." << std::endl; + return 1; + } + + try { + std::cout << "Existing state: "; + printState(lightController); + if (argc == 2) { + std::string arg = argv[1]; + int brightness = ::atoi(argv[1]); + if (brightness > 0) { + lightController->setPowerOn(); + lightController->setBrightness(brightness); + } else + lightController->setPowerOff(); + } + std::cout << "Now: "; + printState(lightController); + } catch (std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + status = 1; + } + + delete lightController; + return status; +} + +//! [Interesting] diff --git a/examples/c++/interfaces-lightsensor.cxx b/examples/c++/interfaces-lightsensor.cxx index c4b87ea5..9f754f89 100644 --- a/examples/c++/interfaces-lightsensor.cxx +++ b/examples/c++/interfaces-lightsensor.cxx @@ -22,57 +22,63 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include -#include "max44009.hpp" -#include "si1132.hpp" +#include -#define EDISON_I2C_BUS 1 +#include "iLightSensor.hpp" +#include "max44009.hpp" +#include "mraa/common.h" +#include "si1132.hpp" +#include "upm_utilities.h" + +#define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 //! [Interesting] -// Simple example of using ILightSensor to determine +// Simple example of using ILightSensor to determine // which sensor is present and return its name. // ILightSensor is then used to get readings from sensor - -upm::ILightSensor* getLightSensor() +upm::ILightSensor* +getLightSensor() { - upm::ILightSensor* lightSensor = NULL; - try { - lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS)); - return lightSensor; - } catch (std::exception& e) { - std::cerr << "SI1132: " << e.what() << std::endl; - } - try { - lightSensor = new upm::MAX44009(EDISON_I2C_BUS); - return lightSensor; - } catch (std::exception& e) { - std::cerr << "MAX44009: " << e.what() << std::endl; - } - return lightSensor; + upm::ILightSensor* lightSensor = NULL; + try { + lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return lightSensor; + } catch (std::exception& e) { + std::cerr << "SI1132: " << e.what() << std::endl; + } + try { + lightSensor = new upm::MAX44009(EDISON_I2C_BUS); + return lightSensor; + } catch (std::exception& e) { + std::cerr << "MAX44009: " << e.what() << std::endl; + } + return lightSensor; } -int main () +int +main() { - upm::ILightSensor* lightSensor = getLightSensor(); - if (lightSensor == NULL) { - std::cout << "Light sensor not detected" << std::endl; - return 1; - } - std::cout << "Light sensor " << lightSensor->getModuleName() << " detected" << std::endl; - while (true) { - try { - float value = lightSensor->getVisibleLux(); - std::cout << "Light level = " << value << " lux" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - sleep(1); - } - delete lightSensor; - return 0; + upm::ILightSensor* lightSensor = getLightSensor(); + if (lightSensor == NULL) { + std::cout << "Light sensor not detected" << std::endl; + return 1; + } + std::cout << "Light sensor " << lightSensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + float value = lightSensor->getVisibleLux(); + std::cout << "Light level = " << value << " lux" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + upm_delay(1); + } + delete lightSensor; + return 0; } -//! [Interesting] \ No newline at end of file +//! [Interesting] diff --git a/examples/c++/interfaces-pressuresensor.cxx b/examples/c++/interfaces-pressuresensor.cxx index 891f6a66..437011a2 100644 --- a/examples/c++/interfaces-pressuresensor.cxx +++ b/examples/c++/interfaces-pressuresensor.cxx @@ -22,60 +22,64 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include -#include "bmpx8x.hpp" +#include + #include "bme280.hpp" +#include "bmpx8x.hpp" +#include "iPressureSensor.hpp" +#include "mraa/common.h" +#include "upm_utilities.h" - -#define EDISON_I2C_BUS 1 +#define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 //! [Interesting] -// Simple example of using ILightSensor to determine +// Simple example of using ILightSensor to determine // which sensor is present and return its name. // ILightSensor is then used to get readings from sensor - -upm::IPressureSensor* getPressureSensor() +upm::IPressureSensor* +getPressureSensor() { - upm::IPressureSensor* pressureSensor = NULL; - try { - pressureSensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); - return pressureSensor ; - } catch (std::exception& e) - { - std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; - while (true) { - try { - int value = pressureSensor->getPressurePa(); - std::cout << "Pressure = " << value << " Pa" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - sleep(1); - } - delete pressureSensor; - return 0; + upm::IPressureSensor* pressureSensor = getPressureSensor(); + if (pressureSensor == NULL) { + std::cout << "Pressure sensor not detected" << std::endl; + return 1; + } + std::cout << "Pressure sensor " << pressureSensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + int value = pressureSensor->getPressurePa(); + std::cout << "Pressure = " << value << " Pa" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + upm_delay(1); + } + delete pressureSensor; + return 0; } -//! [Interesting] +//! [Interesting] diff --git a/examples/c++/interfaces-temperaturesensor.cxx b/examples/c++/interfaces-temperaturesensor.cxx index f077217a..8871166f 100644 --- a/examples/c++/interfaces-temperaturesensor.cxx +++ b/examples/c++/interfaces-temperaturesensor.cxx @@ -22,69 +22,75 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include -#include "si7005.hpp" -#include "bmpx8x.hpp" -#include "bme280.hpp" +#include -#define EDISON_I2C_BUS 1 +#include "bme280.hpp" +#include "bmpx8x.hpp" +#include "iTemperatureSensor.hpp" +#include "mraa/common.h" +#include "si7005.hpp" +#include "upm_utilities.h" + +#define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 - #define EDISON_GPIO_SI7005_CS 20 +#define EDISON_GPIO_SI7005_CS 20 //! [Interesting] -// Simple example of using ITemperatureSensor to determine +// Simple example of using ITemperatureSensor to determine // which sensor is present and return its name. // ITemperatureSensor is then used to get readings from sensor - -upm::ITemperatureSensor* getTemperatureSensor() +upm::ITemperatureSensor* +getTemperatureSensor() { - upm::ITemperatureSensor* temperatureSensor = NULL; + upm::ITemperatureSensor* temperatureSensor = NULL; - try { - temperatureSensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); - return temperatureSensor; - } catch (std::exception& e) - { - std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; - while (true) { - try { - int value = temperatureSensor->getTemperatureCelsius(); - std::cout << "Temperature = " << value << "C" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - sleep(1); - } - delete temperatureSensor; - return 0; + upm::ITemperatureSensor* temperatureSensor = getTemperatureSensor(); + if (temperatureSensor == NULL) { + std::cout << "Temperature sensor not detected" << std::endl; + return 1; + } + std::cout << "Temperature sensor " << temperatureSensor->getModuleName() << " detected" + << std::endl; + while (true) { + try { + int value = temperatureSensor->getTemperatureCelsius(); + std::cout << "Temperature = " << value << "C" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + upm_delay(1); + } + delete temperatureSensor; + return 0; } -//! [Interesting] +//! [Interesting] diff --git a/examples/c++/isd1820.cxx b/examples/c++/isd1820.cxx index 75d77989..77f9ddf1 100644 --- a/examples/c++/isd1820.cxx +++ b/examples/c++/isd1820.cxx @@ -22,59 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include + #include "isd1820.hpp" +#include "upm_utilities.h" using namespace std; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a ISD1820 on digital pins 2 (play) and 3 (record) - // This example was tested on the Grove Recorder. + //! [Interesting] + // Instantiate a ISD1820 on digital pins 2 (play) and 3 (record) + // This example was tested on the Grove Recorder. - upm::ISD1820* recorder = new upm::ISD1820(2, 3); - - // if an argument was specified (any argument), go into record mode, - // else playback a previously recorded sample + upm::ISD1820 recorder(2, 3); - cout << "Supply any argument to the command line to record." << endl; - cout << "Running this example without arguments will play back any " << endl; - cout << "previously recorded sound." << endl; - cout << "There is approximately 10 seconds of recording time." << endl; + // if an argument was specified (any argument), go into record mode, + // else playback a previously recorded sample - cout << endl; + cout << "Supply any argument to the command line to record." << endl; + cout << "Running this example without arguments will play back any " << endl; + cout << "previously recorded sound." << endl; + cout << "There is approximately 10 seconds of recording time." << endl; - bool doRecord = false; + cout << endl; - if (argc > 1) - doRecord = true; - - // depending on what was selected, do it, and sleep for 15 seconds + bool doRecord = false; - if (doRecord) - recorder->record(true); - else - recorder->play(true); - - // There are about 10 seconds of recording/playback time, so we will - // sleep for a little extra time. - cout << "Sleeping for 15 seconds..." << endl; - sleep(15); + if (argc > 1) + doRecord = true; - // now, turn off whatever we were doing. + // depending on what was selected, do it, and upm_delay for 15 seconds - if (doRecord) - recorder->record(false); - else - recorder->play(false); + if (doRecord) + recorder.record(true); + else + recorder.play(true); -//! [Interesting] + // There are about 10 seconds of recording/playback time, so we will + // upm_delay for a little extra time. + cout << "Sleeping for 15 seconds..." << endl; + upm_delay(15); - cout << "Exiting..." << endl; + // now, turn off whatever we were doing. - delete recorder; - return 0; + if (doRecord) + recorder.record(false); + else + recorder.play(false); + + //! [Interesting] + + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/itg3200.cxx b/examples/c++/itg3200.cxx index a313cb64..1fab3bae 100644 --- a/examples/c++/itg3200.cxx +++ b/examples/c++/itg3200.cxx @@ -22,30 +22,32 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include + #include "itg3200.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - int16_t *rot; - float *ang; + //! [Interesting] + int16_t* rot; + float* ang; // Note: Sensor not supported on Intel Edison with Arduino breakout - upm::Itg3200* gyro = new upm::Itg3200(0); + upm::Itg3200 gyro(0); - while(true){ - gyro->update(); // Update the data - rot = gyro->getRawValues(); // Read raw sensor data - ang = gyro->getRotation(); // Read rotational speed (deg/sec) + while (true) { + gyro.update(); // Update the data + rot = gyro.getRawValues(); // Read raw sensor data + ang = gyro.getRotation(); // Read rotational speed (deg/sec) fprintf(stdout, "Raw: %6d %6d %6d\n", rot[0], rot[1], rot[2]); fprintf(stdout, "AngX: %5.2f\n", ang[0]); fprintf(stdout, "AngY: %5.2f\n", ang[1]); fprintf(stdout, "AngZ: %5.2f\n", ang[2]); - fprintf(stdout, "Temp: %5.2f Raw: %6d\n", gyro->getTemperature(), gyro->getRawTemp()); - sleep(1); + fprintf(stdout, "Temp: %5.2f Raw: %6d\n", gyro.getTemperature(), gyro.getRawTemp()); + upm_delay(1); } -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/jhd1313m1-lcd.cxx b/examples/c++/jhd1313m1-lcd.cxx index 9bbe26d7..f7f712f0 100644 --- a/examples/c++/jhd1313m1-lcd.cxx +++ b/examples/c++/jhd1313m1-lcd.cxx @@ -23,6 +23,8 @@ */ #include +#include +#include #include #include "jhd1313m1.hpp" @@ -30,14 +32,15 @@ int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } int -main(int argc, char **argv) +main(int argc, char** argv) { signal(SIGINT, sig_handler); @@ -46,22 +49,16 @@ main(int argc, char **argv) upm::Jhd1313m1 lcd(0, 0x3E, 0x62); int ndx = 0; - uint8_t rgb[7][3] = { - {0xd1, 0x00, 0x00}, - {0xff, 0x66, 0x22}, - {0xff, 0xda, 0x21}, - {0x33, 0xdd, 0x00}, - {0x11, 0x33, 0xcc}, - {0x22, 0x00, 0x66}, - {0x33, 0x00, 0x44}}; - while (shouldRun) - { + uint8_t rgb[7][3] = { { 0xd1, 0x00, 0x00 }, { 0xff, 0x66, 0x22 }, { 0xff, 0xda, 0x21 }, + { 0x33, 0xdd, 0x00 }, { 0x11, 0x33, 0xcc }, { 0x22, 0x00, 0x66 }, + { 0x33, 0x00, 0x44 } }; + while (shouldRun) { // Alternate rows on the LCD - lcd.setCursor(ndx%2,0); + lcd.setCursor(ndx % 2, 0); // Change the color - uint8_t r = rgb[ndx%7][0]; - uint8_t g = rgb[ndx%7][1]; - uint8_t b = rgb[ndx%7][2]; + uint8_t r = rgb[ndx % 7][0]; + uint8_t g = rgb[ndx % 7][1]; + uint8_t b = rgb[ndx % 7][2]; lcd.setColor(r, g, b); lcd.write("Hello World " + std::to_string(ndx)); // Echo via printf diff --git a/examples/c++/joystick12.cxx b/examples/c++/joystick12.cxx index 9e9013bc..0a88eb8b 100644 --- a/examples/c++/joystick12.cxx +++ b/examples/c++/joystick12.cxx @@ -22,15 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "joystick12.hpp" -#include -#include +#include "upm_utilities.h" int is_running = 0; -upm::Joystick12 *sensor = NULL; void sig_handler(int signo) @@ -43,24 +42,22 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { // Instantiate a joystick on analog pins A0 and A1 - sensor = new upm::Joystick12(0,1); + upm::Joystick12 sensor(0, 1); signal(SIGINT, sig_handler); // Print the X and Y input values every second while (!is_running) { - float x = sensor->getXInput(); - float y = sensor->getYInput(); + float x = sensor.getXInput(); + float y = sensor.getYInput(); std::cout << "Driving X:" << x << ": and Y:" << y << std::endl; - sleep(1); + upm_delay(1); } std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/kxcjk1013.cxx b/examples/c++/kxcjk1013.cxx index 76767854..389b4adc 100644 --- a/examples/c++/kxcjk1013.cxx +++ b/examples/c++/kxcjk1013.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include #include + #include "kxcjk1013.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -upm::KXCJK1013* accelerometer; void sig_handler(int signo) @@ -41,9 +41,11 @@ sig_handler(int signo) } void -data_callback(char* data) +data_callback(char* data, void* args) { float x, y, z; + upm::KXCJK1013* accelerometer = static_cast(args); + accelerometer->extract3Axis(data, &x, &y, &z); cout << fixed << setprecision(1); cout << x << '\t' << y << '\t' << z << "[m/s^2]" << endl; @@ -55,25 +57,24 @@ main() signal(SIGINT, sig_handler); //! [Interesting] // Instantiate a KXCJK1013 Accelerometer Sensor on iio device 0 - accelerometer = new upm::KXCJK1013(0); + upm::KXCJK1013 accelerometer(0); // Available scales are 0.009582(2g), 0.019163(4g), and 0.038326(8g) - accelerometer->setScale(0.019163); - // Available sampling frequency are 0.781000, 1.563000, 3.125000, 6.250000, 12.500000, 25, 50, + accelerometer.setScale(0.019163); + // Available sampling frequency are 0.781000, 1.563000, 3.125000, 6.250000, + // 12.500000, 25, 50, // 100, 200, 400, 800, and 1600 - accelerometer->setSamplingFrequency(25.0); - accelerometer->enable3AxisChannel(); - accelerometer->installISR(data_callback, NULL); - accelerometer->enableBuffer(16); + accelerometer.setSamplingFrequency(25.0); + accelerometer.enable3AxisChannel(); + accelerometer.installISR(data_callback, &accelerometer); + accelerometer.enableBuffer(16); while (shouldRun) { - sleep(1); + upm_delay(1); } - accelerometer->disableBuffer(); + accelerometer.disableBuffer(); //! [Interesting] cout << "Exiting" << endl; - delete accelerometer; - return 0; } diff --git a/examples/c++/l298-stepper.cxx b/examples/c++/l298-stepper.cxx index 84361752..2e05a461 100644 --- a/examples/c++/l298-stepper.cxx +++ b/examples/c++/l298-stepper.cxx @@ -22,40 +22,40 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "l298.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { - //! [Interesting] + //! [Interesting] - // Instantiate a Stepper motor on a L298 Dual H-Bridge. + // Instantiate a Stepper motor on a L298 Dual H-Bridge. - // This was tested with the NEMA-17 12V, 350mA, with 200 steps per rev. - upm::L298* l298 = new upm::L298(200, 3, 4, 7, 8, 9); + // This was tested with the NEMA-17 12V, 350mA, with 200 steps per rev. + upm::L298 l298(200, 3, 4, 7, 8, 9); - l298->setSpeed(10); // 10 RPMs - l298->setDirection(upm::L298::DIR_CW); - l298->enable(true); - cout << "Rotating 1 full revolution at 10 RPM speed." << endl; - // move 200 steps, a full rev - l298->stepperSteps(200); - cout << "Sleeping for 2 seconds..." << endl; - sleep(2); - cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." - << endl; - l298->setDirection(upm::L298::DIR_CCW); - l298->stepperSteps(100); - // release - l298->enable(false); + l298.setSpeed(10); // 10 RPMs + l298.setDirection(upm::L298::DIR_CW); + l298.enable(true); + cout << "Rotating 1 full revolution at 10 RPM speed." << endl; + // move 200 steps, a full rev + l298.stepperSteps(200); + cout << "Sleeping for 2 seconds..." << endl; + upm_delay(2); + cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed." << endl; + l298.setDirection(upm::L298::DIR_CCW); + l298.stepperSteps(100); + // release + l298.enable(false); - //! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete l298; - return 0; + return 0; } diff --git a/examples/c++/l298.cxx b/examples/c++/l298.cxx index ff972ee4..8bc37cb8 100644 --- a/examples/c++/l298.cxx +++ b/examples/c++/l298.cxx @@ -22,40 +22,41 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "l298.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { - //! [Interesting] + //! [Interesting] - // Instantiate one of the 2 possible DC motors on a L298 Dual - // H-Bridge. For controlling a stepper motor, see the l298-stepper - // example. - upm::L298* l298 = new upm::L298(3, 4, 7); - - cout << "Starting motor at 50% for 3 seconds..." << endl; - l298->setSpeed(50); - l298->setDirection(upm::L298::DIR_CW); - l298->enable(true); + // Instantiate one of the 2 possible DC motors on a L298 Dual + // H-Bridge. For controlling a stepper motor, see the l298-stepper + // example. + upm::L298 l298(3, 4, 7); - sleep(3); + cout << "Starting motor at 50% for 3 seconds..." << endl; + l298.setSpeed(50); + l298.setDirection(upm::L298::DIR_CW); + l298.enable(true); - cout << "Reversing direction..." << endl; - l298->setDirection(upm::L298::DIR_NONE); // fast stop - l298->setDirection(upm::L298::DIR_CCW); - sleep(3); + upm_delay(3); - l298->setSpeed(0); - l298->enable(false); - - //! [Interesting] + cout << "Reversing direction..." << endl; + l298.setDirection(upm::L298::DIR_NONE); // fast stop + l298.setDirection(upm::L298::DIR_CCW); + upm_delay(3); - cout << "Exiting..." << endl; + l298.setSpeed(0); + l298.enable(false); - delete l298; - return 0; + //! [Interesting] + + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/l3gd20-i2c.cxx b/examples/c++/l3gd20-i2c.cxx index 6fd8598e..129359bb 100644 --- a/examples/c++/l3gd20-i2c.cxx +++ b/examples/c++/l3gd20-i2c.cxx @@ -22,102 +22,89 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include #include #include + #include "l3gd20.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -float rad2deg(float x) +float +rad2deg(float x) { - return x * (180.0 / M_PI); + return x * (180.0 / M_PI); } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an L3GD20 using default parameters - upm::L3GD20 *sensor = new upm::L3GD20(L3GD20_DEFAULT_I2C_BUS, - L3GD20_DEFAULT_I2C_ADDR); + // Instantiate an L3GD20 using default parameters + upm::L3GD20 sensor(L3GD20_DEFAULT_I2C_BUS, L3GD20_DEFAULT_I2C_ADDR); - // set some parameters (these are already the defaults, but are - // provided here as an example) + // set some parameters (these are already the defaults, but are + // provided here as an example) - // 250 deg/s sensitivity - sensor->setRange(sensor->FS_250); + // 250 deg/s sensitivity + sensor.setRange(sensor.FS_250); - // Set ODR to 95Hz, 25Hz cut-off - sensor->setODR(sensor->ODR_CUTOFF_95_25); + // Set ODR to 95Hz, 25Hz cut-off + sensor.setODR(sensor.ODR_CUTOFF_95_25); - // If you already have calibration data, you can specify it here - // sensor->loadCalibratedData(-0.0296269637, -0.0080939643, -0.0077121737); + // If you already have calibration data, you can specify it here + // sensor.loadCalibratedData(-0.0296269637, -0.0080939643, -0.0077121737); - // now output data every 100 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 100 milliseconds + while (shouldRun) { + float x, y, z; - sensor->update(); + sensor.update(); - cout << "Calibrated: " << sensor->getCalibratedStatus() << endl; + cout << "Calibrated: " << sensor.getCalibratedStatus() << endl; - // output is in radians/s - sensor->getGyroscope(&x, &y, &z); - cout << fixed << setprecision(1) - << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " radians" - << endl; + // output is in radians/s + sensor.getGyroscope(&x, &y, &z); + cout << fixed << setprecision(1) << "Gyroscope x: " << x << " y: " << y << " z: " << z + << " radians" << endl; - // same data converted to degrees/s - cout << "Gyroscope x: " << rad2deg(x) - << " y: " << rad2deg(y) - << " z: " << rad2deg(z) - << " degrees" - << endl; + // same data converted to degrees/s + cout << "Gyroscope x: " << rad2deg(x) << " y: " << rad2deg(y) << " z: " << rad2deg(z) + << " degrees" << endl; - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor->getTemperature(false) - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature(false) << " C / " + << sensor.getTemperature(true) << " F" << endl; - cout << endl; + cout << endl; - usleep(100000); + upm_delay_us(100000); } - // dump the calibration values if we managed to calibrate - if (sensor->getCalibratedStatus()) - { - float calX, calY, calZ; - sensor->getCalibratedData(&calX, &calY, &calZ); + // dump the calibration values if we managed to calibrate + if (sensor.getCalibratedStatus()) { + float calX, calY, calZ; + sensor.getCalibratedData(&calX, &calY, &calZ); - cout << setprecision(10) - << "Calibration values x: " << calX - << " y: " << calY - << " z: " << calZ - << endl; + cout << setprecision(10) << "Calibration values x: " << calX << " y: " << calY + << " z: " << calZ << endl; } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; - -//! [Interesting] - return 0; + //! [Interesting] + return 0; } diff --git a/examples/c++/l3gd20.cxx b/examples/c++/l3gd20.cxx index 2795686a..d19abd7e 100644 --- a/examples/c++/l3gd20.cxx +++ b/examples/c++/l3gd20.cxx @@ -22,16 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include #include + #include "l3gd20.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -upm::L3GD20* gyroscope; void sig_handler(int signo) @@ -41,9 +41,11 @@ sig_handler(int signo) } void -data_callback(char* data) +data_callback(char* data, void* args) { float x, y, z; + upm::L3GD20* gyroscope = static_cast(args); + if (gyroscope->extract3Axis(data, &x, &y, &z)) { cout << fixed << setprecision(1); cout << x << '\t' << y << '\t' << z << "[rad/sec]" << endl; @@ -55,25 +57,24 @@ main() { signal(SIGINT, sig_handler); //! [Interesting] + upm::L3GD20 gyroscope(3); // Instantiate a L3GD20 Gyroscope Sensor on iio device 3 - gyroscope = new upm::L3GD20(3); - // Available scales are 0.000153(250dps), 0.000305(500dps), and 0.001222(2000dps) - gyroscope->setScale(0.001222); + // Available scales are 0.000153(250dps), 0.000305(500dps), and + // 0.001222(2000dps) + gyroscope.setScale(0.001222); // Available sampling frequency are 95, 190, 380, and 760 - gyroscope->setSamplingFrequency(95.0); - gyroscope->enable3AxisChannel(); - gyroscope->installISR(data_callback, NULL); - gyroscope->enableBuffer(16); + gyroscope.setSamplingFrequency(95.0); + gyroscope.enable3AxisChannel(); + gyroscope.installISR(data_callback, &gyroscope); + gyroscope.enableBuffer(16); while (shouldRun) { - sleep(1); + upm_delay(1); } - gyroscope->disableBuffer(); + gyroscope.disableBuffer(); //! [Interesting] cout << "Exiting" << endl; - delete gyroscope; - return 0; } diff --git a/examples/c++/lcdks.cxx b/examples/c++/lcdks.cxx index c7988f2a..07722e0a 100644 --- a/examples/c++/lcdks.cxx +++ b/examples/c++/lcdks.cxx @@ -24,50 +24,48 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include #include "lcdks.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a LCDKS (LCD Keypad Shield) using default pins // NOTE: The default pins do not include support for a gpio // controlled backlight. If you need one, you will need to specify // all neccessary pins to the constructor. - upm::LCDKS* lcd = new upm::LCDKS(); + upm::LCDKS lcd; - lcd->setCursor(0,0); - lcd->write("LCDKS driver"); - lcd->setCursor(1,2); - lcd->write("Hello World"); + lcd.setCursor(0, 0); + lcd.write("LCDKS driver"); + lcd.setCursor(1, 2); + lcd.write("Hello World"); // output current key value every second. - while (shouldRun) - { - cout << "Button value: " << lcd->getKeyValue() << endl; - sleep(1); + while (shouldRun) { + cout << "Button value: " << lcd.getKeyValue() << endl; + upm_delay(1); } - delete lcd; - -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/lcm1602-i2c.cxx b/examples/c++/lcm1602-i2c.cxx index e8b9c09d..f71c4fa4 100644 --- a/examples/c++/lcm1602-i2c.cxx +++ b/examples/c++/lcm1602-i2c.cxx @@ -26,24 +26,22 @@ #include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - upm::Lcm1602* lcd = new upm::Lcm1602(0, 0x27); - lcd->setCursor(0,0); - lcd->write("Hello World"); -//! [Interesting] + //! [Interesting] + upm::Lcm1602 lcd(0, 0x27); + lcd.setCursor(0, 0); + lcd.write("Hello World"); + //! [Interesting] upm_delay(3); - lcd->setCursor(1,2); - lcd->write("Hello World"); + lcd.setCursor(1, 2); + lcd.write("Hello World"); upm_delay(3); - lcd->setCursor(2,4); - lcd->write("Hello World"); + lcd.setCursor(2, 4); + lcd.write("Hello World"); upm_delay(3); - lcd->setCursor(3,6); - lcd->write("Hello World"); + lcd.setCursor(3, 6); + lcd.write("Hello World"); upm_delay(3); - delete lcd; - return 0; } diff --git a/examples/c++/lcm1602-parallel.cxx b/examples/c++/lcm1602-parallel.cxx index 39754d30..b5533c72 100644 --- a/examples/c++/lcm1602-parallel.cxx +++ b/examples/c++/lcm1602-parallel.cxx @@ -23,13 +23,15 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include "lcm1602.hpp" #include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // LCD connection: // LCD RS pin to digital pin 8 // LCD Enable pin to digital pin 13 @@ -41,15 +43,14 @@ main(int argc, char **argv) // 10K trimmer potentiometer: // ends to +5V and ground // wiper to LCD VO pin (pin 3) - upm::Lcm1602 *lcd = new upm::Lcm1602(8, 13, 2, 3, 4, 5, 20, 2); - lcd->setCursor(0,0); - lcd->write("Hello World"); - lcd->setCursor(1,2); - lcd->write("Hello World"); + upm::Lcm1602 lcd(8, 13, 2, 3, 4, 5, 20, 2); + lcd.setCursor(0, 0); + lcd.write("Hello World"); + lcd.setCursor(1, 2); + lcd.write("Hello World"); printf("Sleeping for 5 seconds\n"); upm_delay(5); - delete lcd; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/ldt0028.cxx b/examples/c++/ldt0028.cxx index d0b40937..8754610f 100644 --- a/examples/c++/ldt0028.cxx +++ b/examples/c++/ldt0028.cxx @@ -22,69 +22,70 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include #include +#include +#include +#include + #include "ldt0028.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] const int NUMBER_OF_SECONDS = 10; const int MICROSECONDS_PER_SECOND = 1000000; const int SAMPLES_PER_SECOND = 50; const int THRESHOLD = 100; // Create the LDT0-028 Piezo Vibration Sensor object using AIO pin 0 - upm::LDT0028* sensor = new upm::LDT0028(0); + upm::LDT0028 sensor(0); // Read the signal every 20 milliseconds for 10 seconds - std::cout << "For the next " << NUMBER_OF_SECONDS << " seconds, " - << SAMPLES_PER_SECOND << " samples will be taken every second." - << std::endl << std::endl; + std::cout << "For the next " << NUMBER_OF_SECONDS << " seconds, " << SAMPLES_PER_SECOND + << " samples will be taken every second." << std::endl + << std::endl; uint16_t buffer[NUMBER_OF_SECONDS * SAMPLES_PER_SECOND]; - for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { - buffer[i] = (uint16_t) sensor->getSample(); - usleep(MICROSECONDS_PER_SECOND / SAMPLES_PER_SECOND); + for (int i = 0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + buffer[i] = (uint16_t) sensor.getSample(); + upm_delay_us(MICROSECONDS_PER_SECOND / SAMPLES_PER_SECOND); } // Print the number of times the reading was greater than the threshold int count = 0; - for (int i=0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { + for (int i = 0; i < NUMBER_OF_SECONDS * SAMPLES_PER_SECOND; i++) { if (buffer[i] > THRESHOLD) { count++; } } - std::cout << sensor->name() << " exceeded the threshold value of " << - THRESHOLD << " a total of " << count << " times," << std::endl - << "out of a total of " << NUMBER_OF_SECONDS*SAMPLES_PER_SECOND - << " readings." << std::endl << std::endl; + std::cout << sensor.name() << " exceeded the threshold value of " << THRESHOLD << " a total of " + << count << " times," << std::endl + << "out of a total of " << NUMBER_OF_SECONDS * SAMPLES_PER_SECOND << " readings." + << std::endl + << std::endl; // Print a graphical representation of the average value sampled // each second for the past 10 seconds, using a scale factor of 15 - std::cout << "Now printing a graphical representation of the average reading " - << std::endl << "each second for the last " - << NUMBER_OF_SECONDS << " seconds." << std::endl; + std::cout << "Now printing a graphical representation of the average reading " << std::endl + << "each second for the last " << NUMBER_OF_SECONDS << " seconds." << std::endl; const int SCALE_FACTOR = 15; - for (int i=0; i < NUMBER_OF_SECONDS; i++) { + for (int i = 0; i < NUMBER_OF_SECONDS; i++) { long sum = 0; - for (int j=0; j < SAMPLES_PER_SECOND; j++) { - sum += buffer[i*SAMPLES_PER_SECOND + j]; + for (int j = 0; j < SAMPLES_PER_SECOND; j++) { + sum += buffer[i * SAMPLES_PER_SECOND + j]; } double average = (double) sum / (double) SAMPLES_PER_SECOND; int stars_to_print = (int) round(average / SCALE_FACTOR); std::cout << "(" << std::setw(4) << (int) round(average) << ") | "; - for (int j=0; j #include + #include "led.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Create the Grove LED object using GPIO pin 2 - upm::Led* led = new upm::Led(2); + upm::Led led(2); // 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 // between transitions - for (int i=0; i < 10; i++) { - led->on(); - sleep(1); - led->off(); - sleep(1); + for (int i = 0; i < 10; i++) { + led.on(); + upm_delay(1); + led.off(); + upm_delay(1); } // Delete the Grove LED object - delete led; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/lidarlitev3.cxx b/examples/c++/lidarlitev3.cxx index f52548d7..a865a0ff 100644 --- a/examples/c++/lidarlitev3.cxx +++ b/examples/c++/lidarlitev3.cxx @@ -23,10 +23,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "lidarlitev3.hpp" +#include "upm_utilities.h" volatile int doWork = 0; @@ -40,25 +42,22 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { - // Register signal handler signal(SIGINT, sig_handler); //! [Interesting] - upm::LIDARLITEV3 *sensor = new upm::LIDARLITEV3(0, ADDR); + upm::LIDARLITEV3 sensor(0, ADDR); while (!doWork) { - std::cout << "Distance = " << sensor->getDistance () << std::endl; - usleep (50000); + std::cout << "Distance = " << sensor.getDistance() << std::endl; + upm_delay_us(50000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/light.cxx b/examples/c++/light.cxx index cf26ee3c..3c94bf3c 100644 --- a/examples/c++/light.cxx +++ b/examples/c++/light.cxx @@ -23,28 +23,27 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "light.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Create the light sensor object using AIO pin 0 - upm::Light* light = new upm::Light(0); + upm::Light light(0); // Read the input and print both the normalized ADC value and a // rough lux value, waiting one second between readings - while( 1 ) { - std::cout << light->name() << " normalized value is " - << light->getNormalized() << - ", which is roughly " << light->value() << " lux" << std::endl; - sleep(1); + while (1) { + std::cout << light.name() << " normalized value is " << light.getNormalized() + << ", which is roughly " << light.value() << " lux" << std::endl; + upm_delay(1); } // Delete the light sensor object - delete light; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/linefinder.cxx b/examples/c++/linefinder.cxx index 72e76aad..f5ade6de 100644 --- a/examples/c++/linefinder.cxx +++ b/examples/c++/linefinder.cxx @@ -22,45 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "linefinder.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Line Finder sensor on digital pin D2 - upm::LineFinder* finder = new upm::LineFinder(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; + //! [Interesting] + // Instantiate a Line Finder sensor on digital pin D2 + upm::LineFinder finder(2); - 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; } diff --git a/examples/c++/lis2ds12.cxx b/examples/c++/lis2ds12.cxx index 8845027a..203916ac 100644 --- a/examples/c++/lis2ds12.cxx +++ b/examples/c++/lis2ds12.cxx @@ -22,60 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lis2ds12.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an LIS2DS12 using default I2C parameters - upm::LIS2DS12 sensor; + // Instantiate an LIS2DS12 using default I2C parameters + upm::LIS2DS12 sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin - // for CS: LIS2DS12(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin + // for CS: LIS2DS12(0, -1, 10); - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/lm35.cxx b/examples/c++/lm35.cxx index abdfd91b..4e96e163 100644 --- a/examples/c++/lm35.cxx +++ b/examples/c++/lm35.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lm35.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a LM35 on analog pin A0, with a default analog - // reference voltage of 5.0 - upm::LM35 *sensor = new upm::LM35(0); - - // Every half second, sample the sensor and output the temperature + // Instantiate a LM35 on analog pin A0, with a default analog + // reference voltage of 5.0 + upm::LM35 sensor(0); - while (shouldRun) - { - cout << "Temperature: " << sensor->getTemperature() << " C" << endl; - - usleep(500000); + // Every half second, sample the sensor and output the temperature + + while (shouldRun) { + cout << "Temperature: " << sensor.getTemperature() << " C" << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/lol.cxx b/examples/c++/lol.cxx index f58ae5ac..34ba4568 100644 --- a/examples/c++/lol.cxx +++ b/examples/c++/lol.cxx @@ -22,15 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "lol.hpp" -#include -#include +#include "upm_utilities.h" int is_running = 0; -upm::LoL *sensor = NULL; void sig_handler(int signo) @@ -43,24 +42,26 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { - sensor = new upm::LoL(); + upm::LoL sensor; signal(SIGINT, sig_handler); - //buffer = sensor->getFramebuffer(); + // buffer = sensor.getFramebuffer(); int x = 0, y = 0; while (!is_running) { - sensor->setPixel(x, y, !(sensor->getPixel(x, y))); - if (++x == 13) { x = 0; y++; } - if (y == 9) y = 0; - usleep(10000); + sensor.setPixel(x, y, !(sensor.getPixel(x, y))); + if (++x == 13) { + x = 0; + y++; + } + if (y == 9) + y = 0; + upm_delay_us(10000); } std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/loudness.cxx b/examples/c++/loudness.cxx index 9eaa59b3..bb4e4e8f 100644 --- a/examples/c++/loudness.cxx +++ b/examples/c++/loudness.cxx @@ -22,47 +22,48 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "loudness.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -#define LOUDNESS_AREF 5.0 +#define LOUDNESS_AREF 5.0 -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a Loudness sensor on analog pin A0, with an analog - // reference voltage of LOUDNESS_AREF - upm::Loudness *loud = new upm::Loudness(0, LOUDNESS_AREF); - - // Every tenth of a second, sample the loudness and output it's - // corresponding analog voltage. + // Instantiate a Loudness sensor on analog pin A0, with an analog + // reference voltage of LOUDNESS_AREF + upm::Loudness loud(0, LOUDNESS_AREF); - while (shouldRun) - { - cout << "Detected loudness (volts): " << loud->loudness() << endl; - - usleep(100000); + // Every tenth of a second, sample the loudness and output it's + // corresponding analog voltage. + + while (shouldRun) { + cout << "Detected loudness (volts): " << loud.loudness() << endl; + + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete loud; - return 0; + return 0; } diff --git a/examples/c++/lp8860.cxx b/examples/c++/lp8860.cxx index 48257961..181c1ce9 100644 --- a/examples/c++/lp8860.cxx +++ b/examples/c++/lp8860.cxx @@ -1,45 +1,43 @@ -#include -#include -#include -#include -#include -#include "lp8860.hpp" - -#define EDISON_I2C_BUS 1 // Edison I2C-1 -#define LP8860_GPIO_PWR 45 // Edison GP45 - - -void printState(upm::ILightController &lightController) -{ - if (lightController.isPowered()) - { - std::cout << "Light is powered, brightness = " << lightController.getBrightness() << std::endl; - } - else - { - std::cout << "Light is not powered." << std::endl; - } -} - -int main( int argc, char **argv ) -{ - //! [Interesting] - upm::LP8860 lightController(LP8860_GPIO_PWR, EDISON_I2C_BUS); - std::cout << "Existing state: "; printState(lightController); - if (argc == 2) - { - std::string arg = argv[1]; - int brightness = ::atoi(argv[1]); - if (brightness > 0) - { - lightController.setPowerOn(); - lightController.setBrightness(brightness); - } - else - lightController.setPowerOff(); - } - std::cout << "Now: ";printState(lightController); - //! [Interesting] - - return 0; -} +#include +#include +#include + +#include "iLightController.hpp" +#include "lp8860.hpp" + +#define EDISON_I2C_BUS 1 // Edison I2C-1 +#define LP8860_GPIO_PWR 45 // Edison GP45 + +void +printState(upm::ILightController& lightController) +{ + if (lightController.isPowered()) { + std::cout << "Light is powered, brightness = " << lightController.getBrightness() + << std::endl; + } else { + std::cout << "Light is not powered." << std::endl; + } +} + +int +main(int argc, char** argv) +{ + //! [Interesting] + upm::LP8860 lightController(LP8860_GPIO_PWR, EDISON_I2C_BUS); + std::cout << "Existing state: "; + printState(lightController); + if (argc == 2) { + std::string arg = argv[1]; + int brightness = ::atoi(argv[1]); + if (brightness > 0) { + lightController.setPowerOn(); + lightController.setBrightness(brightness); + } else + lightController.setPowerOff(); + } + std::cout << "Now: "; + printState(lightController); + //! [Interesting] + + return 0; +} diff --git a/examples/c++/lpd8806.cxx b/examples/c++/lpd8806.cxx index 472f36ec..cad0938e 100644 --- a/examples/c++/lpd8806.cxx +++ b/examples/c++/lpd8806.cxx @@ -22,15 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "lpd8806.hpp" #include +#include -void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait); +#include "lpd8806.hpp" +#include "upm_utilities.h" + +void scanner(upm::LPD8806& sensor, uint8_t r, uint8_t g, uint8_t b, uint8_t wait); int doWork = 0; -upm::LPD8806 *sensor = NULL; void sig_handler(int signo) @@ -43,61 +44,61 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::LPD8806(0, 7, 10); - usleep (1000000); + upm::LPD8806 sensor(0, 7, 10); + upm_delay_us(1000000); - sensor->show (); + sensor.show(); while (!doWork) { // Back-and-forth lights - scanner(127, 0, 0, 30); // red, slow - scanner(0, 0, 127, 15); // blue, fast - usleep (1000000); + scanner(sensor, 127, 0, 0, 30); // red, slow + scanner(sensor, 0, 0, 127, 15); // blue, fast + upm_delay_us(1000000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } -void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) { +void +scanner(upm::LPD8806& sensor, uint8_t r, uint8_t g, uint8_t b, uint8_t wait) +{ int i, j, pos, dir; pos = 0; dir = 1; - for(i=0; i < ((sensor->getStripLength() - 1) * 8); i++) { + for (i = 0; i < ((sensor.getStripLength() - 1) * 8); i++) { // Draw 5 pixels centered on pos. setPixelColor() will clip // any pixels off the ends of the strip, no worries there. // we'll make the colors dimmer at the edges for a nice pulse // look - sensor->setPixelColor(pos - 2, r/4, g/4, b/4); - sensor->setPixelColor(pos - 1, r/2, g/2, b/2); - sensor->setPixelColor(pos, r, g, b); - sensor->setPixelColor(pos + 1, r/2, g/2, b/2); - sensor->setPixelColor(pos + 2, r/4, g/4, b/4); + sensor.setPixelColor(pos - 2, r / 4, g / 4, b / 4); + sensor.setPixelColor(pos - 1, r / 2, g / 2, b / 2); + sensor.setPixelColor(pos, r, g, b); + sensor.setPixelColor(pos + 1, r / 2, g / 2, b / 2); + sensor.setPixelColor(pos + 2, r / 4, g / 4, b / 4); - sensor->show(); - usleep (wait * 1000); + sensor.show(); + upm_delay_us(wait * 1000); // If we wanted to be sneaky we could erase just the tail end // pixel, but it's much easier just to erase the whole thing // and draw a new one next time. - for(j=-2; j<= 2; j++) { - sensor->setPixelColor(pos+j, 0,0,0); + for (j = -2; j <= 2; j++) { + sensor.setPixelColor(pos + j, 0, 0, 0); } // Bounce off ends of strip pos += dir; - if(pos < 0) { + if (pos < 0) { pos = 1; dir = -dir; - } else if (pos >= sensor->getStripLength()) { - pos = sensor->getStripLength() - 2; + } else if (pos >= sensor.getStripLength()) { + pos = sensor.getStripLength() - 2; dir = -dir; } } diff --git a/examples/c++/lsm303agr.cxx b/examples/c++/lsm303agr.cxx index 21435ecd..491ec0a8 100644 --- a/examples/c++/lsm303agr.cxx +++ b/examples/c++/lsm303agr.cxx @@ -24,64 +24,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lsm303agr.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an LSM303AGR using default I2C parameters - upm::LSM303AGR sensor; + // Instantiate an LSM303AGR using default I2C parameters + upm::LSM303AGR sensor; - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getMagnetometer(&x, &y, &z); - cout << "Magnetometer x: " << x - << " y: " << y - << " z: " << z - << " uT" - << endl; + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer x: " << x << " y: " << y << " z: " << z << " uT" << endl; - cout << "Temperature: " - << sensor.getTemperature() - << " C" - << endl; + cout << "Temperature: " << sensor.getTemperature() << " C" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/lsm303d.cxx b/examples/c++/lsm303d.cxx index 255ee12b..338d2a42 100644 --- a/examples/c++/lsm303d.cxx +++ b/examples/c++/lsm303d.cxx @@ -24,64 +24,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lsm303d.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an LSM303D using default I2C parameters - upm::LSM303D sensor; + // Instantiate an LSM303D using default I2C parameters + upm::LSM303D sensor; - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getMagnetometer(&x, &y, &z); - cout << "Magnetometer x: " << x - << " y: " << y - << " z: " << z - << " uT" - << endl; + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer x: " << x << " y: " << y << " z: " << z << " uT" << endl; - cout << "Temperature: " - << sensor.getTemperature() - << " C" - << endl; + cout << "Temperature: " << sensor.getTemperature() << " C" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/lsm303dlh.cxx b/examples/c++/lsm303dlh.cxx index c1a869df..2a26a7f7 100644 --- a/examples/c++/lsm303dlh.cxx +++ b/examples/c++/lsm303dlh.cxx @@ -23,48 +23,39 @@ */ #include +#include //! [Interesting] #include "lsm303dlh.hpp" int -main(int argc, char **argv) +main(int argc, char** argv) { - // Instantiate LSM303DLH compass on I2C - upm::LSM303DLH *sensor = new upm::LSM303DLH(0); + // Instantiate LSM303DLH compass on I2C + upm::LSM303DLH sensor(0); // Get the coordinate data - sensor->getCoordinates(); - int16_t* coor = sensor->getRawCoorData(); // in XYZ order. + sensor.getCoordinates(); + int16_t* coor = sensor.getRawCoorData(); // in XYZ order. // The sensor returns XZY, but the driver compensates and makes it XYZ // Print out the X, Y, and Z coordinate data using two different methods - std::cout << "coor: rX " << (int)coor[0] - << " - rY " << (int)coor[1] - << " - rZ " << (int)coor[2] - << std::endl; - std::cout << "coor: gX " << sensor->getCoorX() - << " - gY " << sensor->getCoorY() - << " - gZ " << sensor->getCoorZ() - << std::endl; + std::cout << "coor: rX " << (int) coor[0] << " - rY " << (int) coor[1] << " - rZ " + << (int) coor[2] << std::endl; + std::cout << "coor: gX " << sensor.getCoorX() << " - gY " << sensor.getCoorY() << " - gZ " + << sensor.getCoorZ() << std::endl; // Get and print out the heading - std::cout << "heading: " - << sensor->getHeading() - << std::endl; + std::cout << "heading: " << sensor.getHeading() << std::endl; // Get the acceleration - sensor->getAcceleration(); - int16_t* accel = sensor->getRawAccelData(); + sensor.getAcceleration(); + int16_t* accel = sensor.getRawAccelData(); // Print out the X, Y, and Z acceleration data using two different methods - std::cout << "acc: rX " << (int)accel[0] - << " - rY " << (int)accel[1] - << " - Z " << (int)accel[2] - << std::endl; - std::cout << "acc: gX " << sensor->getAccelX() - << " - gY " << sensor->getAccelY() - << " - gZ " << sensor->getAccelZ() - << std::endl; + std::cout << "acc: rX " << (int) accel[0] << " - rY " << (int) accel[1] << " - Z " + << (int) accel[2] << std::endl; + std::cout << "acc: gX " << sensor.getAccelX() << " - gY " << sensor.getAccelY() << " - gZ " + << sensor.getAccelZ() << std::endl; return 0; } diff --git a/examples/c++/lsm6ds3h.cxx b/examples/c++/lsm6ds3h.cxx index 55b8b5f5..1c9a2d90 100644 --- a/examples/c++/lsm6ds3h.cxx +++ b/examples/c++/lsm6ds3h.cxx @@ -22,67 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lsm6ds3h.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an LSM6DS3H using default I2C parameters - upm::LSM6DS3H sensor; + // Instantiate an LSM6DS3H using default I2C parameters + upm::LSM6DS3H sensor; - // For SPI, bus 0, you would pass -1 as the address, and a valid pin - // for CS: LSM6DS3H(0, -1, 10); + // For SPI, bus 0, you would pass -1 as the address, and a valid pin + // for CS: LSM6DS3H(0, -1, 10); - // now output data every 250 milliseconds - while (shouldRun) - { - float x, y, z; + // now output data every 250 milliseconds + while (shouldRun) { + float x, y, z; - sensor.update(); + sensor.update(); - sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; - sensor.getGyroscope(&x, &y, &z); - cout << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " dps" - << endl; + sensor.getGyroscope(&x, &y, &z); + cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " dps" << endl; - // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; - cout << endl; + cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - return 0; + return 0; } diff --git a/examples/c++/lsm6dsl.cxx b/examples/c++/lsm6dsl.cxx index 78835244..fb4afe16 100644 --- a/examples/c++/lsm6dsl.cxx +++ b/examples/c++/lsm6dsl.cxx @@ -22,26 +22,28 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lsm6dsl.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate an LSM6DSL using default I2C parameters upm::LSM6DSL sensor; @@ -50,37 +52,27 @@ int main(int argc, char **argv) // for CS: LSM6DSL(0, -1, 10); // now output data every 250 milliseconds - while (shouldRun) - { + while (shouldRun) { float x, y, z; sensor.update(); sensor.getAccelerometer(&x, &y, &z); - cout << "Accelerometer x: " << x - << " y: " << y - << " z: " << z - << " g" - << endl; + cout << "Accelerometer x: " << x << " y: " << y << " z: " << z << " g" << endl; sensor.getGyroscope(&x, &y, &z); - cout << "Gyroscope x: " << x - << " y: " << y - << " z: " << z - << " dps" - << endl; + cout << "Gyroscope x: " << x << " y: " << y << " z: " << z << " dps" << endl; // we show both C and F for temperature - cout << "Compensation Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + cout << "Compensation Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; cout << endl; - usleep(250000); + upm_delay_us(250000); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; diff --git a/examples/c++/lsm9ds0.cxx b/examples/c++/lsm9ds0.cxx index 655e02cb..64ecabdf 100644 --- a/examples/c++/lsm9ds0.cxx +++ b/examples/c++/lsm9ds0.cxx @@ -22,62 +22,61 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "lsm9ds0.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - // Instantiate an LSM9DS0 using default parameters (bus 1, gyro addr 6b, - // xm addr 1d) - upm::LSM9DS0 *sensor = new upm::LSM9DS0(); + // Instantiate an LSM9DS0 using default parameters (bus 1, gyro addr 6b, + // xm addr 1d) + upm::LSM9DS0 sensor; - sensor->init(); + sensor.init(); - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getAccelerometer(&x, &y, &z); - cout << "Accelerometer: "; - cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + while (shouldRun) { + sensor.update(); - sensor->getGyroscope(&x, &y, &z); - cout << "Gryoscope: "; - cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; - - sensor->getMagnetometer(&x, &y, &z); - cout << "Magnetometer: "; - cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + float x, y, z; - cout << "Temperature: " << sensor->getTemperature() << endl; - cout << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer: "; + cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; - usleep(500000); + sensor.getGyroscope(&x, &y, &z); + cout << "Gryoscope: "; + cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; + + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer: "; + cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + + cout << "Temperature: " << sensor.getTemperature() << endl; + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/m24lr64e.cxx b/examples/c++/m24lr64e.cxx index 63b186f9..2ef08997 100644 --- a/examples/c++/m24lr64e.cxx +++ b/examples/c++/m24lr64e.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include + #include "m24lr64e.hpp" using namespace std; using namespace upm; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an M24LR64E on I2C bus 0 + //! [Interesting] + // Instantiate an M24LR64E on I2C bus 0 - upm::M24LR64E *nfcTag = new upm::M24LR64E(M24LR64E_I2C_BUS); + upm::M24LR64E nfcTag(M24LR64E_I2C_BUS); - // This example accesses the device in the 'user' (default) mode, - // reads the last byte of data in the EEPROM, inverts it, writes - // it back, and then re-reads it. + // This example accesses the device in the 'user' (default) mode, + // reads the last byte of data in the EEPROM, inverts it, writes + // it back, and then re-reads it. - // Read the last byte of the EEPROM area + // Read the last byte of the EEPROM area - int addr = M24LR64E::EEPROM_I2C_LENGTH - 1; - printf("Address: %d\n", addr); - uint8_t byte = nfcTag->readByte(addr); - printf("Read byte: %02x\n", byte); + int addr = M24LR64E::EEPROM_I2C_LENGTH - 1; + printf("Address: %d\n", addr); + uint8_t byte = nfcTag.readByte(addr); + printf("Read byte: %02x\n", byte); - // Now change it to it's opposite and write it - byte = ~byte; - nfcTag->writeByte(addr, byte); - printf("Wrote inverted byte: %02x\n", byte); + // Now change it to it's opposite and write it + byte = ~byte; + nfcTag.writeByte(addr, byte); + printf("Wrote inverted byte: %02x\n", byte); - // Now read it back. - byte = nfcTag->readByte(addr); - printf("Read byte: %02x\n", byte); + // Now read it back. + byte = nfcTag.readByte(addr); + printf("Read byte: %02x\n", byte); -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete nfcTag; - return 0; + return 0; } diff --git a/examples/c++/mag3110.cxx b/examples/c++/mag3110.cxx index 8ae7e300..2aeccd2c 100644 --- a/examples/c++/mag3110.cxx +++ b/examples/c++/mag3110.cxx @@ -21,41 +21,42 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "mag3110.hpp" +#include "upm_utilities.h" using namespace upm; bool run = true; -void sig_handler(int sig) +void +sig_handler(int sig) { - if (sig == SIGINT) - run = false; + if (sig == SIGINT) + run = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - upm::mag3110_data_t data; + //! [Interesting] + upm::mag3110_data_t data; - std::cout << "Initializing test-application..." << std::endl; + std::cout << "Initializing test-application..." << std::endl; - // Instantiate an MAG3110 instance on bus 1 - upm::MAG3110 *mySensor = new upm::MAG3110(1); + // Instantiate an MAG3110 instance on bus 1 + upm::MAG3110 mySensor(1); - // activate periodic measurements - mySensor->setActive(); + // activate periodic measurements + mySensor.setActive(); - // update and print available values every second - while (run) - { - mySensor->getData (&data, true); + // update and print available values every second + while (run) { + mySensor.getData(&data, true); std::cout << "x: " << data.x << std::endl << "y: " << data.y << std::endl << "z: " << data.z << std::endl @@ -64,13 +65,12 @@ int main(int argc, char **argv) std::cout << std::endl; - sleep(1); + upm_delay(1); } - std::cout << "Exiting test-application..." << std::endl; + std::cout << "Exiting test-application..." << std::endl; - delete mySensor; -//! [Interesting] + //! [Interesting] - return 0; -} \ No newline at end of file + return 0; +} diff --git a/examples/c++/max30100.cxx b/examples/c++/max30100.cxx index 5ed691a5..870a0f8d 100644 --- a/examples/c++/max30100.cxx +++ b/examples/c++/max30100.cxx @@ -22,18 +22,19 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "max30100.hpp" +#include "max30100_regs.h" #include "upm_utilities.h" using namespace upm; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; @@ -42,15 +43,16 @@ void sig_handler(int signo) // Example Callback handler class mycallback : public Callback { - public: - virtual void run(max30100_value samp) - { - std::cout << "My callback sample IR: " - << samp.IR << " R: " << samp.R << std::endl; - } + public: + virtual void + run(max30100_value samp) + { + std::cout << "My callback sample IR: " << samp.IR << " R: " << samp.R << std::endl; + } }; -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); //! [Interesting] @@ -74,20 +76,17 @@ int main(int argc, char **argv) // Read continuously, stepping up the LED current every second, // us GPIO 0 as the interrupt pin sensor.sample_continuous(0, false, &cb); - for (int i = MAX30100_LED_CURRENT_0_0_MA; - i <= MAX30100_LED_CURRENT_50_0_MA && shouldRun; i++) - { + for (int i = MAX30100_LED_CURRENT_0_0_MA; i <= MAX30100_LED_CURRENT_50_0_MA && shouldRun; i++) { // Toggle the LED current std::cout << "Setting LED current = " << i << std::endl; - sensor.current((MAX30100_LED_CURRENT)i, (MAX30100_LED_CURRENT)i); + sensor.current((MAX30100_LED_CURRENT) i, (MAX30100_LED_CURRENT) i); upm_delay(1); } // Read individual samples - for (int i = 0; i < 10; i++) - { + for (int i = 0; i < 10; i++) { max30100_value val = sensor.sample(); std::cout << "Single value IR: " << val.IR << " R: " << val.R << std::endl; } diff --git a/examples/c++/max31723.cxx b/examples/c++/max31723.cxx index fe798c82..d125ad8d 100644 --- a/examples/c++/max31723.cxx +++ b/examples/c++/max31723.cxx @@ -22,13 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "max31723.hpp" #include +#include + +#include "max31723.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::MAX31723 *sensor = NULL; void sig_handler(int signo) @@ -41,21 +42,19 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::MAX31723(0, 7); - usleep (1000000); + upm::MAX31723 sensor(0, 7); + upm_delay_us(1000000); while (!doWork) { - std::cout << "Temperature " << sensor->getTemperature() << std::endl; - usleep (1000000); + std::cout << "Temperature " << sensor.getTemperature() << std::endl; + upm_delay_us(1000000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/max31855.cxx b/examples/c++/max31855.cxx index 6e729981..e949e3b3 100644 --- a/examples/c++/max31855.cxx +++ b/examples/c++/max31855.cxx @@ -22,19 +22,17 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include //! [Interesting] #include "max31855.hpp" int -main(int argc, char **argv) +main(int argc, char** argv) { - upm::MAX31855 *temp = new upm::MAX31855(0, 8); + upm::MAX31855 temp(0, 8); - std::cout << temp->getTemp() << std::endl; + std::cout << temp.getTemp() << std::endl; return 0; } diff --git a/examples/c++/max44000.cxx b/examples/c++/max44000.cxx index c344bdf3..fbf64f59 100644 --- a/examples/c++/max44000.cxx +++ b/examples/c++/max44000.cxx @@ -22,21 +22,19 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "max44000.hpp" int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - upm::MAX44000 *sensor = new upm::MAX44000(0, ADDR); - std::cout << "proximity value = " << sensor->getAmbient () << std::endl; + upm::MAX44000 sensor(0, ADDR); + std::cout << "proximity value = " << sensor.getAmbient() << std::endl; //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/max44009.cxx b/examples/c++/max44009.cxx index 95de4b7d..07b1d3d2 100644 --- a/examples/c++/max44009.cxx +++ b/examples/c++/max44009.cxx @@ -22,20 +22,22 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "max44009.hpp" +#include "upm_utilities.h" #define EDISON_I2C_BUS 1 -int main () +int +main() { //! [Interesting] upm::MAX44009 lightSensor(EDISON_I2C_BUS); while (true) { float value = lightSensor.getVisibleLux(); std::cout << "Light level = " << value << " lux" << std::endl; - sleep(1); + upm_delay(1); } //! [Interesting] return 0; diff --git a/examples/c++/max5487.cxx b/examples/c++/max5487.cxx index a13223ac..befe4c13 100644 --- a/examples/c++/max5487.cxx +++ b/examples/c++/max5487.cxx @@ -22,12 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "max5487.hpp" #include +#include -upm::MAX5487 *sensor = NULL; +#include "max5487.hpp" +#include "upm_utilities.h" void sig_handler(int signo) @@ -39,28 +39,26 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::MAX5487(7); + upm::MAX5487 sensor(7); // Power LED UP for (int i = 0; i < 255; i++) { - sensor->setWiperA(i); - usleep (5000); + sensor.setWiperA(i); + upm_delay_us(5000); } // Power LED DOWN for (int i = 0; i < 255; i++) { - sensor->setWiperA(255 - i); - usleep (5000); + sensor.setWiperA(255 - i); + upm_delay_us(5000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/maxds3231m.cxx b/examples/c++/maxds3231m.cxx index ef5f95f6..7e6af858 100644 --- a/examples/c++/maxds3231m.cxx +++ b/examples/c++/maxds3231m.cxx @@ -22,13 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "maxds3231m.hpp" #include +#include + +#include "maxds3231m.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::MAXDS3231M *sensor = NULL; void sig_handler(int signo) @@ -41,35 +42,33 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] upm::Time3231 t; - sensor = new upm::MAXDS3231M(0, ADDR); + upm::MAXDS3231M sensor(0, ADDR); - t.second = 1; - t.minute = 3; - t.hour = 3; - t.day = 3; - t.month = 3; - t.year = 3; - t.weekDay = 3; - sensor->setDate (t); // Note, second should be set to 1. + t.second = 1; + t.minute = 3; + t.hour = 3; + t.day = 3; + t.month = 3; + t.year = 3; + t.weekDay = 3; + sensor.setDate(t); // Note, second should be set to 1. - usleep (500000); + upm_delay_us(500000); while (!doWork) { - if (sensor->getDate (t)) { - std::cout << (int)t.hour << ":" << (int)t.minute << ":" << (int)t.second << std::endl; + if (sensor.getDate(t)) { + std::cout << (int) t.hour << ":" << (int) t.minute << ":" << (int) t.second << std::endl; } - std::cout << "Temperature " << sensor->getTemperature() << std::endl; - usleep (1000000); + std::cout << "Temperature " << sensor.getTemperature() << std::endl; + upm_delay_us(1000000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/maxsonarez.cxx b/examples/c++/maxsonarez.cxx index 5ffb5c10..42797ca8 100644 --- a/examples/c++/maxsonarez.cxx +++ b/examples/c++/maxsonarez.cxx @@ -22,51 +22,51 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "maxsonarez.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -#define MAXSONAREZ_AREF 5.0 +#define MAXSONAREZ_AREF 5.0 -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a MaxSonarEZ on analog pin A1, with an analog - // reference voltage of MAXSONAREZ_AREF - upm::MAXSONAREZ *sonar = new upm::MAXSONAREZ(1, MAXSONAREZ_AREF); - - // Every second, sample the sonar and output the distance to an - // object in inches. + // Instantiate a MaxSonarEZ on analog pin A1, with an analog + // reference voltage of MAXSONAREZ_AREF + upm::MAXSONAREZ sonar(1, MAXSONAREZ_AREF); - // With the EZ3 version, the minimum and maximum ranges seemed to be - // between 6 and 33 inches + // Every second, sample the sonar and output the distance to an + // object in inches. - while (shouldRun) - { - cout << "AREF: " << MAXSONAREZ_AREF - << ", distance in inches: " << sonar->inches() << endl; - - sleep(1); + // With the EZ3 version, the minimum and maximum ranges seemed to be + // between 6 and 33 inches + + while (shouldRun) { + cout << "AREF: " << MAXSONAREZ_AREF << ", distance in inches: " << sonar.inches() << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sonar; - return 0; + return 0; } diff --git a/examples/c++/mb704x.cxx b/examples/c++/mb704x.cxx index ebcb9241..856b5cc3 100644 --- a/examples/c++/mb704x.cxx +++ b/examples/c++/mb704x.cxx @@ -22,49 +22,43 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include - -#include +#include #include +#include +#include using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a MB704X sensor using default parameters (bus 0, // address 112) - upm::MB704X *sensor = new upm::MB704X(); + upm::MB704X sensor; - while (shouldRun) - { - cout << "Range: " - << sensor->getRange() - << " cm" - << endl; + while (shouldRun) { + cout << "Range: " << sensor.getRange() << " cm" << endl; upm_delay_ms(500); } - cout << "Exiting..." << endl; - delete sensor; - -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/mcp2515-txrx.cxx b/examples/c++/mcp2515-txrx.cxx index fb876688..3607725f 100644 --- a/examples/c++/mcp2515-txrx.cxx +++ b/examples/c++/mcp2515-txrx.cxx @@ -22,26 +22,27 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include #include +#include #include #include "mcp2515.hpp" +#include "mcp2515_regs.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); @@ -51,7 +52,7 @@ int main(int argc, char **argv) // if an argument is given, we will transmit packets if (argc > 1) do_tx = true; -//! [Interesting] + //! [Interesting] // NOTE: This example assumes that only two devices are connected // to the CAN bus, and that both devices are running this example; @@ -68,7 +69,7 @@ int main(int argc, char **argv) // able to transmit a message. // Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1). - upm::MCP2515 *sensor = new upm::MCP2515(0, 9); + upm::MCP2515 sensor(0, 9); // By default, after initialization, the baud rate is set to // 50Kbps, and the mode is NORMAL, so we don't need to set any of @@ -78,37 +79,30 @@ int main(int argc, char **argv) // incremented (and rollover) on each transmission. string myPayload = "01234567"; - while (shouldRun) - { - if (do_tx) - { - cout << "Loading a packet of 8 numbers (0-7) into a TX buffer..." - << endl; - sensor->loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false, - myPayload); + while (shouldRun) { + if (do_tx) { + cout << "Loading a packet of 8 numbers (0-7) into a TX buffer..." << endl; + sensor.loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false, myPayload); // now lets try to transmit it cout << "Transmitting packet..." << endl; - sensor->transmitBuffer(MCP2515_TX_BUFFER0, true); + sensor.transmitBuffer(MCP2515_TX_BUFFER0, true); myPayload[0]++; cout << "Transmit successful" << endl; cout << endl; upm_delay_ms(500); - } - else - { + } else { // RX mode // Look for a packet waiting for us in RXB0 - if (sensor->rxStatusMsgs() == MCP2515_RXMSG_RXB0) - { + if (sensor.rxStatusMsgs() == MCP2515_RXMSG_RXB0) { cout << "Packet received in RXB0, decoding..." << endl; // now lets retrieve and print it - sensor->getRXMsg(MCP2515_RX_BUFFER0); + sensor.getRXMsg(MCP2515_RX_BUFFER0); - sensor->printMsg(); + sensor.printMsg(); cout << endl; } @@ -118,8 +112,7 @@ int main(int argc, char **argv) cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/mcp2515.cxx b/examples/c++/mcp2515.cxx index 3cc77924..35631849 100644 --- a/examples/c++/mcp2515.cxx +++ b/examples/c++/mcp2515.cxx @@ -22,58 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include #include "mcp2515.hpp" +#include "mcp2515_regs.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1). - upm::MCP2515 *sensor = new upm::MCP2515(0, -1); + upm::MCP2515 sensor(0, -1); cout << "Setting loopback mode..." << endl; // set the mode to loopback mode. In loopback mode, transmitted // packets are sent directly to an appropriate receive buffer // without actually going out onto the CAN bus. - sensor->setOpmode(MCP2515_OPMODE_LOOPBACK); + sensor.setOpmode(MCP2515_OPMODE_LOOPBACK); // lets build up a packet and try loading it (8 bytes max). string myPayload = "01234567"; cout << "Loading a packet of 8 numbers (0-7) into a TX buffer..." << endl; - sensor->loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false, myPayload); + sensor.loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false, myPayload); // now lets try to transmit it cout << "Transmitting packet..." << endl; - sensor->transmitBuffer(MCP2515_TX_BUFFER0, true); + sensor.transmitBuffer(MCP2515_TX_BUFFER0, true); cout << "Transmit successful" << endl; // There should now be a packet waiting for us in RXB0 - if (sensor->rxStatusMsgs() == MCP2515_RXMSG_RXB0) - { + if (sensor.rxStatusMsgs() == MCP2515_RXMSG_RXB0) { cout << "Packet received in RXB0, decoding..." << endl; // now lets retrieve and print it - sensor->getRXMsg(MCP2515_RX_BUFFER0); + sensor.getRXMsg(MCP2515_RX_BUFFER0); - sensor->printMsg(); - } - else - { + sensor.printMsg(); + } else { cout << "No packet found, how strange." << endl; } cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/mcp9808.cxx b/examples/c++/mcp9808.cxx index 0e8dc75a..f9e11d57 100644 --- a/examples/c++/mcp9808.cxx +++ b/examples/c++/mcp9808.cxx @@ -1,163 +1,158 @@ #include -#include + #include "mcp9808.hpp" -#include "mraa.hpp" - -int main() +int +main() { - using namespace std; + using namespace std; -//! [Interesting] - int command; - upm::MCP9808 *temp = new upm::MCP9808(6); + //! [Interesting] + int command; + upm::MCP9808 temp(6); - do - { - cout << endl; - cout << "1 - read temp \t" ; - cout << "2 - sleep mode \t"; - cout << "3 - wake up" << endl; - cout << "4 - set mode to " << (temp->isCelsius() == true ? "Fahrenheit" : "Celsius") << endl; - cout << "5 - show status bits" << endl; - cout << "6 - Set Tcrit \t" ; - cout << "7 - Set Tupper \t" ; - cout << "8 - Set Tlower " << endl; - cout << "9 - Display monitor temps " << endl; - cout << "10 - Enable alert default\t"; - cout << "11 - Enable alert interrupt" << endl; - cout << "12 - Clear interrupt \t" ; - cout << "13 - Clear alert mode" << endl; - cout << "14 - Get Hysteresis\t"; - cout << "15 - Set Hysteresis" << endl; - cout << "16 - Get Resolution\t"; - cout << "17 - Set Resolution" << endl; - cout << "18 - Get Manufacturer ID"<< endl; - cout << "19 - Get Device ID" << endl; - cout << "-1 - exit" << endl; - cout << "Enter a command: "; - cin >> command; + do { + cout << endl; + cout << "1 - read temp \t"; + cout << "2 - upm_delay mode \t"; + cout << "3 - wake up" << endl; + cout << "4 - set mode to " << (temp.isCelsius() == true ? "Fahrenheit" : "Celsius") << endl; + cout << "5 - show status bits" << endl; + cout << "6 - Set Tcrit \t"; + cout << "7 - Set Tupper \t"; + cout << "8 - Set Tlower " << endl; + cout << "9 - Display monitor temps " << endl; + cout << "10 - Enable alert default\t"; + cout << "11 - Enable alert interrupt" << endl; + cout << "12 - Clear interrupt \t"; + cout << "13 - Clear alert mode" << endl; + cout << "14 - Get Hysteresis\t"; + cout << "15 - Set Hysteresis" << endl; + cout << "16 - Get Resolution\t"; + cout << "17 - Set Resolution" << endl; + cout << "18 - Get Manufacturer ID" << endl; + cout << "19 - Get Device ID" << endl; + cout << "-1 - exit" << endl; + cout << "Enter a command: "; + cin >> command; - switch(command) - { - float t; - case 1: - std::cout << "Temp: " << temp->getTemp() << "* " << (temp->isCelsius()? "Celsius" : "Fahrenheit")<< std::endl; - break; - case 2: - cout << "shutdown sensor (sleep mode)" << endl; - temp->shutDown(); - break; - case 3: - cout << "wake up sensor" << endl; - temp->shutDown(false); - break; - case 4: - cout << "set mode to " << (temp->isCelsius() ? "Fahrenheit" : "Celsius") << endl; - temp->setMode(!temp->isCelsius()); - break; - case 5: - cout << "Tcrit = " << temp->isTcrit(); - cout << " Tupper = " << temp->isTupper(); - cout << " Tlower = " << temp->isTlower(); - break; - case 6: - cout << "enter a value"; - cin >> t; - temp->setMonitorReg(temp->CRIT_TEMP, t); - break; - case 7: - cout << "enter a value"; - cin >> t; - temp->setMonitorReg(temp->UPPER_TEMP, t); - break; - case 8: - cout << "enter a value"; - cin >> t; - temp->setMonitorReg(temp->LOWER_TEMP, t); - break; - case 9: - cout << "tcrit = " << temp->getMonitorReg(temp->CRIT_TEMP) << endl; - cout << "tupper = " << temp->getMonitorReg(temp->UPPER_TEMP) << endl; - cout << "tlower = " << temp->getMonitorReg(temp->LOWER_TEMP) << endl; - break; - case 10: - cout << "set alert mode default" ; - temp->setAlertMode(temp->ALERTCTRL); - break; - case 11: - cout << "set alert mode interrupt"; - temp->setAlertMode(temp->ALERTMODE | temp->ALERTCTRL ); - break; - case 12: - temp->clearInterrupt(); - break; - case 13: - cout << "Clear alerts" << endl; - temp->clearAlertMode(); - break; - case 14: - cout << "Hysteresis: " << temp->getHysteresis() << endl; - break; - case 15: - int u; - cout << "enter 1 to 4"; - cin >> u ; - switch(u) - { - case 1: - temp->setHysteresis(temp->HYST_0); - break; - case 2: - temp->setHysteresis(temp->HYST_1_5); - break; - case 3: - temp->setHysteresis(temp->HYST_3_0); - break; - case 4: - default: - temp->setHysteresis(temp->HYST_6_0); - break; - } - break; - case 16: - cout << "Resolution: " << temp->getResolution() << endl; - break; - case 17: - int v; - cout << "enter 1 to 4"; - cin >> v ; - switch(v) - { - case 1: - temp->setResolution(temp->RES_LOW); - break; - case 2: - temp->setResolution(temp->RES_MEDIUM); - break; - case 3: - temp->setResolution(temp->RES_HIGH); - break; - case 4: - default: - temp->setResolution(temp->RES_PRECISION); - break; - } - break; - case 18: - cout << "Manufacturer ID: " << std::hex << temp->getManufacturer() << endl; - break; - case 19: - cout << "Get device ID: " << std::hex << temp->getDevicedId() << endl; - break; - case -1: - break; - default: - cout << endl << "That option is not available. Try again" << endl; - break; - } - - }while (command != -1 ); -//! [Interesting] - return 0; + switch (command) { + float t; + case 1: + std::cout << "Temp: " << temp.getTemp() << "* " + << (temp.isCelsius() ? "Celsius" : "Fahrenheit") << std::endl; + break; + case 2: + cout << "shutdown sensor (upm_delay mode)" << endl; + temp.shutDown(); + break; + case 3: + cout << "wake up sensor" << endl; + temp.shutDown(false); + break; + case 4: + cout << "set mode to " << (temp.isCelsius() ? "Fahrenheit" : "Celsius") << endl; + temp.setMode(!temp.isCelsius()); + break; + case 5: + cout << "Tcrit = " << temp.isTcrit(); + cout << " Tupper = " << temp.isTupper(); + cout << " Tlower = " << temp.isTlower(); + break; + case 6: + cout << "enter a value"; + cin >> t; + temp.setMonitorReg(temp.CRIT_TEMP, t); + break; + case 7: + cout << "enter a value"; + cin >> t; + temp.setMonitorReg(temp.UPPER_TEMP, t); + break; + case 8: + cout << "enter a value"; + cin >> t; + temp.setMonitorReg(temp.LOWER_TEMP, t); + break; + case 9: + cout << "tcrit = " << temp.getMonitorReg(temp.CRIT_TEMP) << endl; + cout << "tupper = " << temp.getMonitorReg(temp.UPPER_TEMP) << endl; + cout << "tlower = " << temp.getMonitorReg(temp.LOWER_TEMP) << endl; + break; + case 10: + cout << "set alert mode default"; + temp.setAlertMode(temp.ALERTCTRL); + break; + case 11: + cout << "set alert mode interrupt"; + temp.setAlertMode(temp.ALERTMODE | temp.ALERTCTRL); + break; + case 12: + temp.clearInterrupt(); + break; + case 13: + cout << "Clear alerts" << endl; + temp.clearAlertMode(); + break; + case 14: + cout << "Hysteresis: " << temp.getHysteresis() << endl; + break; + case 15: + int u; + cout << "enter 1 to 4"; + cin >> u; + switch (u) { + case 1: + temp.setHysteresis(temp.HYST_0); + break; + case 2: + temp.setHysteresis(temp.HYST_1_5); + break; + case 3: + temp.setHysteresis(temp.HYST_3_0); + break; + case 4: + default: + temp.setHysteresis(temp.HYST_6_0); + break; + } + break; + case 16: + cout << "Resolution: " << temp.getResolution() << endl; + break; + case 17: + int v; + cout << "enter 1 to 4"; + cin >> v; + switch (v) { + case 1: + temp.setResolution(temp.RES_LOW); + break; + case 2: + temp.setResolution(temp.RES_MEDIUM); + break; + case 3: + temp.setResolution(temp.RES_HIGH); + break; + case 4: + default: + temp.setResolution(temp.RES_PRECISION); + break; + } + break; + case 18: + cout << "Manufacturer ID: " << std::hex << temp.getManufacturer() << endl; + break; + case 19: + cout << "Get device ID: " << std::hex << temp.getDevicedId() << endl; + break; + case -1: + break; + default: + cout << endl << "That option is not available. Try again" << endl; + break; + } + } while (command != -1); + //! [Interesting] + return 0; } diff --git a/examples/c++/md-stepper.cxx b/examples/c++/md-stepper.cxx index 976dea64..2fa5d09b 100644 --- a/examples/c++/md-stepper.cxx +++ b/examples/c++/md-stepper.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "md.hpp" +#include "md_defs.h" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { - //! [Interesting] - // Instantiate an I2C Motor Driver on I2C bus 0 + //! [Interesting] + // Instantiate an I2C Motor Driver on I2C bus 0 - upm::MD *motors = new upm::MD(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR); + upm::MD motors(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR); - // This example demonstrates using the MD to drive a stepper motor - - // configure it, for this example, we'll assume 200 steps per rev - motors->configStepper(200); + // This example demonstrates using the MD to drive a stepper motor - // set for half a rotation - motors->setStepperSteps(100); + // configure it, for this example, we'll assume 200 steps per rev + motors.configStepper(200); - // let it go - clockwise rotation, 10 RPM speed - motors->enableStepper(MD_STEP_DIR_CW, 10); + // set for half a rotation + motors.setStepperSteps(100); - sleep(3); + // let it go - clockwise rotation, 10 RPM speed + motors.enableStepper(MD_STEP_DIR_CW, 10); - // Now do it backwards... - motors->setStepperSteps(100); - motors->enableStepper(MD_STEP_DIR_CCW, 10); + upm_delay(3); - // now disable - motors->disableStepper(); + // Now do it backwards... + motors.setStepperSteps(100); + motors.enableStepper(MD_STEP_DIR_CCW, 10); - //! [Interesting] + // now disable + motors.disableStepper(); - cout << "Exiting..." << endl; + //! [Interesting] - delete motors; - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/md.cxx b/examples/c++/md.cxx index 8c028224..ef66c98a 100644 --- a/examples/c++/md.cxx +++ b/examples/c++/md.cxx @@ -22,38 +22,38 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "md.hpp" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { - //! [Interesting] - // Instantiate an I2C Motor Driver on I2C bus 0 + //! [Interesting] + // Instantiate an I2C Motor Driver on I2C bus 0 - upm::MD *motors = new upm::MD(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR); + upm::MD motors(MD_I2C_BUS, MD_DEFAULT_I2C_ADDR); - // set direction to CW and set speed to 50% - cout << "Spin M1 and M2 at half speed for 3 seconds" << endl; - motors->setMotorDirections(MD_DIR_CW, MD_DIR_CW); - motors->setMotorSpeeds(127, 127); - - sleep(3); - // counter clockwise - cout << "Reversing M1 and M2 for 3 seconds" << endl; - motors->setMotorDirections(MD_DIR_CCW, MD_DIR_CCW); - sleep(3); + // set direction to CW and set speed to 50% + cout << "Spin M1 and M2 at half speed for 3 seconds" << endl; + motors.setMotorDirections(MD_DIR_CW, MD_DIR_CW); + motors.setMotorSpeeds(127, 127); - //! [Interesting] + upm_delay(3); + // counter clockwise + cout << "Reversing M1 and M2 for 3 seconds" << endl; + motors.setMotorDirections(MD_DIR_CCW, MD_DIR_CCW); + upm_delay(3); - cout << "Stopping motors" << endl; - motors->setMotorSpeeds(0, 0); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Stopping motors" << endl; + motors.setMotorSpeeds(0, 0); - delete motors; - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/mg811.cxx b/examples/c++/mg811.cxx index 3d334c1e..6e695a25 100644 --- a/examples/c++/mg811.cxx +++ b/examples/c++/mg811.cxx @@ -22,47 +22,48 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "mg811.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -#define MG811_AREF 5.0 +#define MG811_AREF 5.0 -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate an MG811 on analog pin A0, and digital pin D2 with an - // analog reference voltage of MG811_AREF (5.0) - upm::MG811 *sensor = new upm::MG811(0, 2, MG811_AREF); - - // Every tenth of a second, sample the sensor and output it's - // detected CO2 concentration in parts per million (ppm) + // Instantiate an MG811 on analog pin A0, and digital pin D2 with an + // analog reference voltage of MG811_AREF (5.0) + upm::MG811 sensor(0, 2, MG811_AREF); - while (shouldRun) - { - cout << "CO2 concentration in PPM: " << sensor->ppm() << endl; - - usleep(100000); + // Every tenth of a second, sample the sensor and output it's + // detected CO2 concentration in parts per million (ppm) + + while (shouldRun) { + cout << "CO2 concentration in PPM: " << sensor.ppm() << endl; + + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/mhz16.cxx b/examples/c++/mhz16.cxx index dae109d4..b6db04e0 100644 --- a/examples/c++/mhz16.cxx +++ b/examples/c++/mhz16.cxx @@ -22,63 +22,63 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "mhz16.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main (int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a MHZ16 serial CO2 sensor on uart 0. - // This example was tested on the Grove CO2 sensor module. + //! [Interesting] + // Instantiate a MHZ16 serial CO2 sensor on uart 0. + // This example was tested on the Grove CO2 sensor module. - upm::MHZ16* co2 = new upm::MHZ16(0); - - // make sure port is initialized properly. 9600 baud is the default. - if (!co2->setupTty(B9600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; - } - - cout << "Make sure that the sensor has had at least 3 minutes to warm up" - << endl; - cout << "or you will not get valid results." << endl; - cout << "The temperature reported is not the ambient temperature, " << endl; - cout << "but rather the temperature of the sensor elements." << endl; + upm::MHZ16 co2(0); - sleep(1); - - while (shouldRun) - { - if(!co2->getData()){ - cerr << "Failed to retrieve data" << endl; - continue; - } - - cout << "CO2 concentration: " << co2->getGas() << " PPM, " - << "Temperature (in C): " << co2->getTemperature() < #include -#include "mic.hpp" #include -#include -#include +#include + +#include "mic.hpp" int is_running = 0; -uint16_t buffer [128]; -upm::Microphone *sensor = NULL; +uint16_t buffer[128]; void sig_handler(int signo) @@ -44,26 +42,26 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { // Attach microphone to analog port A0 - sensor = new upm::Microphone(0); + upm::Microphone sensor(0); signal(SIGINT, sig_handler); thresholdContext ctx; ctx.averageReading = 0; ctx.runningAverage = 0; - ctx.averagedOver = 2; + ctx.averagedOver = 2; // Infinite loop, ends when script is cancelled // Repeatedly, take a sample every 2 microseconds; // find the average of 128 samples; and // print a running graph of the averages while (!is_running) { - int len = sensor->getSampledWindow (2, 128, buffer); + int len = sensor.getSampledWindow(2, 128, buffer); if (len) { - int thresh = sensor->findThreshold (&ctx, 30, buffer, len); - sensor->printGraph(&ctx); + int thresh = sensor.findThreshold(&ctx, 30, buffer, len); + sensor.printGraph(&ctx); if (thresh) { // do something .... } @@ -72,8 +70,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/micsv89.cxx b/examples/c++/micsv89.cxx index f8b2adee..d8ee8bbd 100644 --- a/examples/c++/micsv89.cxx +++ b/examples/c++/micsv89.cxx @@ -23,9 +23,10 @@ */ #include -#include #include + #include "micsv89.hpp" +#include "upm_utilities.h" /* * An example for using the MICSV89 sensor library. @@ -52,27 +53,27 @@ sig_handler(int signo) } } -int main() +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] - upm::MICSV89 *sensor = new upm::MICSV89(6); + //! [Interesting] + upm::MICSV89 sensor(6); - while(running) - { - sensor->update(); - while(!sensor->valid()); - cout << "co2: " << sensor->co2equ() << endl; - cout << "short: " << sensor->vocshort() << endl; - cout << "tvoc: " << sensor->tvoc() << endl; - cout << "resistor: " << sensor->resistor() << endl; + while (running) { + sensor.update(); + while (!sensor.valid()) + ; + cout << "co2: " << sensor.co2equ() << endl; + cout << "short: " << sensor.vocshort() << endl; + cout << "tvoc: " << sensor.tvoc() << endl; + cout << "resistor: " << sensor.resistor() << endl; cout << "****************************" << endl; - sleep(5); + upm_delay(5); } - delete sensor; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/mlx90614.cxx b/examples/c++/mlx90614.cxx index c7f2a3f8..bc7d05e0 100644 --- a/examples/c++/mlx90614.cxx +++ b/examples/c++/mlx90614.cxx @@ -22,13 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mlx90614.hpp" #include +#include + +#include "mlx90614.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::MLX90614 *sensor = NULL; void sig_handler(int signo) @@ -41,20 +42,19 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::MLX90614(0, DEVICE_ADDR); + upm::MLX90614 sensor(0, DEVICE_ADDR); while (!doWork) { - std::cout << "Object Temperature (" << sensor->readObjectTempC() << ") Ambient Temperature (" << sensor->readAmbientTempC() << ")" << std::endl; - usleep (1000000); + std::cout << "Object Temperature (" << sensor.readObjectTempC() + << ") Ambient Temperature (" << sensor.readAmbientTempC() << ")" << std::endl; + upm_delay_us(1000000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/mma7361.cxx b/examples/c++/mma7361.cxx index 3e67d048..1dbd401a 100644 --- a/examples/c++/mma7361.cxx +++ b/examples/c++/mma7361.cxx @@ -22,73 +22,61 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include +#include #include "mma7361.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a MMA7361 sensor on analog pins A0 (X), A1 (Y) A2 - // (Z), selftest pin on D2, sleep pin on D3 nd an analog reference - // value of 5.0. The freefall pin and the range pin are unused - // (-1). - upm::MMA7361 *sensor = new upm::MMA7361(0, 1, 2, 2, 3, -1, -1, 5.0); + // Instantiate a MMA7361 sensor on analog pins A0 (X), A1 (Y) A2 + // (Z), selftest pin on D2, upm_delay pin on D3 nd an analog reference + // value of 5.0. The freefall pin and the range pin are unused + // (-1). + upm::MMA7361 sensor(0, 1, 2, 2, 3, -1, -1, 5.0); - // 1.5g (true = 6g) - sensor->setRange(false); + // 1.5g (true = 6g) + sensor.setRange(false); - // Every 10th of a second, update and print values + // Every 10th of a second, update and print values - while (shouldRun) - { - sensor->update(); + while (shouldRun) { + sensor.update(); - float x, y, z; + float x, y, z; - sensor->getAcceleration(&x, &y, &z); - cout << "Acceleration x = " - << x - << " y = " - << y - << "z = " - << z - << endl; + sensor.getAcceleration(&x, &y, &z); + cout << "Acceleration x = " << x << " y = " << y << "z = " << z << endl; - sensor->getVolts(&x, &y, &z); - cout << "Volts x = " - << x - << " y = " - << y - << "z = " - << z - << endl; + sensor.getVolts(&x, &y, &z); + cout << "Volts x = " << x << " y = " << y << "z = " << z << endl; - cout << endl; + cout << endl; - usleep(100000); + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - printf("Exiting...\n"); + printf("Exiting...\n"); - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/mma7455.cxx b/examples/c++/mma7455.cxx index 0e732337..25090e16 100644 --- a/examples/c++/mma7455.cxx +++ b/examples/c++/mma7455.cxx @@ -22,14 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "mma7455.hpp" #include -#include +#include + +#include "mma7455.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::MMA7455 *sensor = NULL; void sig_handler(int signo) @@ -42,23 +42,21 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::MMA7455(0, ADDR); - + upm::MMA7455 sensor(0, ADDR); + short x, y, z; while (!doWork) { - sensor->readData(&x, &y, &z); + sensor.readData(&x, &y, &z); std::cout << "Accelerometer X(" << x << ") Y(" << y << ") Z(" << z << ")" << std::endl; - usleep (100000); + upm_delay_us(100000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/mma7660.cxx b/examples/c++/mma7660.cxx index e4694d2d..28f8d531 100644 --- a/examples/c++/mma7660.cxx +++ b/examples/c++/mma7660.cxx @@ -22,58 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include + #include "mma7660.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an MMA7660 on I2C bus 0 + //! [Interesting] + // Instantiate an MMA7660 on I2C bus 0 - upm::MMA7660 *accel = new upm::MMA7660(MMA7660_DEFAULT_I2C_BUS, - MMA7660_DEFAULT_I2C_ADDR); + upm::MMA7660 accel(MMA7660_DEFAULT_I2C_BUS, MMA7660_DEFAULT_I2C_ADDR); - // place device in standby mode so we can write registers - accel->setModeStandby(); + // place device in standby mode so we can write registers + accel.setModeStandby(); - // enable 64 samples per second - accel->setSampleRate(MMA7660_AUTOSLEEP_64); + // enable 64 samples per second + accel.setSampleRate(MMA7660_AUTOSLEEP_64); - // place device into active mode - accel->setModeActive(); + // place device into active mode + accel.setModeActive(); - while (shouldRun) - { - float ax, ay, az; + while (shouldRun) { + float ax, ay, az; - accel->getAcceleration(&ax, &ay, &az); - cout << "Acceleration: x = " << ax - << "g y = " << ay - << "g z = " << az - << "g" << endl; + accel.getAcceleration(&ax, &ay, &az); + cout << "Acceleration: x = " << ax << "g y = " << ay << "g z = " << az << "g" << endl; - cout << endl; - usleep(500000); + cout << endl; + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete accel; - return 0; + return 0; } diff --git a/examples/c++/mma8x5x.cxx b/examples/c++/mma8x5x.cxx index bc108e2f..fea832ad 100644 --- a/examples/c++/mma8x5x.cxx +++ b/examples/c++/mma8x5x.cxx @@ -21,31 +21,33 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "mma8x5x.hpp" +#include "upm_utilities.h" using namespace upm; bool run = true; -void sig_handler(int sig) +void +sig_handler(int sig) { - if (sig == SIGINT) - run = false; + if (sig == SIGINT) + run = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - upm::mma8x5x_data_t data; + //! [Interesting] + upm::mma8x5x_data_t data; - // U can set your own parameters for an MMA8X5X instance with - /* + // U can set your own parameters for an MMA8X5X instance with + /* upm::mma8x5x_params_t params; params.type = ; device-id of your sensor params.rate = ; between 0<<3 and 7<<3 @@ -55,37 +57,35 @@ int main(int argc, char **argv) params.offsetZ = ; between 0 and 255 */ - std::cout << "Initializing test-application..." << std::endl; + std::cout << "Initializing test-application..." << std::endl; - // Instantiate an MMA8X5X instance on bus 1 with default parameters - // The sensor-type will be detected by reading out the device-id - upm::MMA8X5X *mySensor = new upm::MMA8X5X(1); + // Instantiate an MMA8X5X instance on bus 1 with default parameters + // The sensor-type will be detected by reading out the device-id + upm::MMA8X5X mySensor(1); - // If u have set own parameters use this one - /* - upm::MMA8X5X *mySensor = new upm::MMA8X5X(1, ¶ms); + // If u have set own parameters use this one + /* + upm::MMA8X5X mySensor(1, ¶ms); */ - // activate periodic measurements - mySensor->setActive(); + // activate periodic measurements + mySensor.setActive(); - // update and print available values every second - while (run) - { - mySensor->getData (&data, true); - std::cout << "x: " << (int)data.x << std::endl - << "y: " << (int)data.y << std::endl - << "z: " << (int)data.z << std::endl; + // update and print available values every second + while (run) { + mySensor.getData(&data, true); + std::cout << "x: " << (int) data.x << std::endl + << "y: " << (int) data.y << std::endl + << "z: " << (int) data.z << std::endl; std::cout << std::endl; - sleep(1); + upm_delay(1); } - std::cout << "Exiting test-application..." << std::endl; + std::cout << "Exiting test-application..." << std::endl; - delete mySensor; -//! [Interesting] + //! [Interesting] - return 0; -} \ No newline at end of file + return 0; +} diff --git a/examples/c++/mmc35240.cxx b/examples/c++/mmc35240.cxx index f048082c..a75f1f82 100644 --- a/examples/c++/mmc35240.cxx +++ b/examples/c++/mmc35240.cxx @@ -22,15 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include -#include #include +#include +#include + #include "mmc35240.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -upm::MMC35240* magnetometer; void sig_handler(int signo) @@ -40,18 +41,19 @@ sig_handler(int signo) } void -data_callback(char* data) +data_callback(char* data, void* args) { + upm::MMC35240* magnetometer = static_cast(args); float x, y, z; double azimuth; int level; magnetometer->extract3Axis(data, &x, &y, &z); /* calibrated level - * UNRELIABLE = 0 - * ACCURACY_LOW = 1 - * ACCURACY_MEDIUM = 2 - * ACCURACY_HIGH = >=3 - */ + * UNRELIABLE = 0 + * ACCURACY_LOW = 1 + * ACCURACY_MEDIUM = 2 + * ACCURACY_HIGH = >=3 + */ level = magnetometer->getCalibratedLevel(); if ((x == 0) && (y == 0)) { @@ -85,27 +87,27 @@ main() { signal(SIGINT, sig_handler); //! [Interesting] - // Instantiate a MMC35240 Magnetic Sensor on iio device 5. This configuration is a reference and + // Instantiate a MMC35240 Magnetic Sensor on iio device 5. This configuration + // is a reference and // should be changed per platform/board type. - magnetometer = new upm::MMC35240(5); - // Kernel driver does not allow changing the value of scale at run-time, default scale is + // Kernel driver does not allow changing the value of scale at run-time, + // default scale is // 0.001000 - magnetometer->setScale(0.001000); + upm::MMC35240 magnetometer(5); + magnetometer.setScale(0.001000); // Available sampling frequency are 1.5, 13, 25, 50 - magnetometer->setSamplingFrequency(25.000000); - magnetometer->enable3AxisChannel(); - magnetometer->installISR(data_callback, NULL); - magnetometer->enableBuffer(16); + magnetometer.setSamplingFrequency(25.000000); + magnetometer.enable3AxisChannel(); + magnetometer.installISR(data_callback, &magnetometer); + magnetometer.enableBuffer(16); while (shouldRun) { - sleep(1); + upm_delay(1); } - magnetometer->disableBuffer(); + magnetometer.disableBuffer(); //! [Interesting] cout << "Exiting" << endl; - delete magnetometer; - return 0; } diff --git a/examples/c++/moisture.cxx b/examples/c++/moisture.cxx index 03dff8dd..b7bfd241 100644 --- a/examples/c++/moisture.cxx +++ b/examples/c++/moisture.cxx @@ -22,54 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "moisture.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Moisture sensor on analog pin A0 - upm::Moisture* moisture = new upm::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"; + //! [Interesting] + // Instantiate a Moisture sensor on analog pin A0 + upm::Moisture moisture(0); - 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; } diff --git a/examples/c++/mpl3115a2.cxx b/examples/c++/mpl3115a2.cxx index 77890f14..f64f7a09 100644 --- a/examples/c++/mpl3115a2.cxx +++ b/examples/c++/mpl3115a2.cxx @@ -22,16 +22,15 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "mpl3115a2.hpp" +#include "upm_utilities.h" volatile int doWork = 0; -upm::MPL3115A2 *sensor = NULL; - void sig_handler(int signo) { @@ -42,42 +41,35 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { // Register signal handler signal(SIGINT, sig_handler); //! [Interesting] - float pressure = 0.0; + float pressure = 0.0; float temperature = 0.0; - float altitude = 0.0; - float sealevel = 0.0; + float altitude = 0.0; + float sealevel = 0.0; - sensor = new upm::MPL3115A2(0, MPL3115A2_I2C_ADDRESS); + upm::MPL3115A2 sensor(0, MPL3115A2_I2C_ADDRESS); - sensor->testSensor(); + sensor.testSensor(); while (!doWork) { - temperature = sensor->getTemperature(true); - pressure = sensor->getPressure(false); - altitude = sensor->getAltitude(); - sealevel = sensor->getSealevelPressure(); + temperature = sensor.getTemperature(true); + pressure = sensor.getPressure(false); + altitude = sensor.getAltitude(); + sealevel = sensor.getSealevelPressure(); - std::cout << "pressure value = " << - pressure << - ", altitude value = " << - altitude << - ", sealevel value = " << - sealevel << - ", temperature = " << - temperature << std::endl; - usleep (500000); + std::cout << "pressure value = " << pressure << ", altitude value = " << altitude + << ", sealevel value = " << sealevel << ", temperature = " << temperature + << std::endl; + upm_delay_us(500000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/mpr121.cxx b/examples/c++/mpr121.cxx index e8aa4b78..586a47d4 100644 --- a/examples/c++/mpr121.cxx +++ b/examples/c++/mpr121.cxx @@ -22,67 +22,67 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include + #include "mpr121.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -void printButtons(upm::MPR121 *touch) +void +printButtons(upm::MPR121& touch) { - bool buttonPressed = false; + bool buttonPressed = false; - cout << "Buttons Pressed: "; - for (int i=0; i<12; i++) - { - if (touch->m_buttonStates & (1 << i)) - { - cout << i << " "; - buttonPressed = true; + cout << "Buttons Pressed: "; + for (int i = 0; i < 12; i++) { + if (touch.m_buttonStates & (1 << i)) { + cout << i << " "; + buttonPressed = true; } } - if (!buttonPressed) - cout << "None"; + if (!buttonPressed) + cout << "None"; - if (touch->m_overCurrentFault) - cout << "Over Current Fault detected!" << endl; + if (touch.m_overCurrentFault) + cout << "Over Current Fault detected!" << endl; - cout << endl; + cout << endl; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an MPR121 on I2C bus 0 + //! [Interesting] + // Instantiate an MPR121 on I2C bus 0 - upm::MPR121 *touch = new upm::MPR121(MPR121_I2C_BUS, MPR121_DEFAULT_I2C_ADDR); + upm::MPR121 touch(MPR121_I2C_BUS, MPR121_DEFAULT_I2C_ADDR); - // init according to AN3944 defaults - touch->configAN3944(); + // init according to AN3944 defaults + touch.configAN3944(); - while (shouldRun) - { - touch->readButtons(); - printButtons(touch); - sleep(1); + while (shouldRun) { + touch.readButtons(); + printButtons(touch); + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete touch; - return 0; + return 0; } diff --git a/examples/c++/mpu9150-ak8975.cxx b/examples/c++/mpu9150-ak8975.cxx index c2f8607f..b19b0561 100644 --- a/examples/c++/mpu9150-ak8975.cxx +++ b/examples/c++/mpu9150-ak8975.cxx @@ -22,51 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include "mpu9150.hpp" + +#include "ak8975.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - upm::AK8975 *sensor = new upm::AK8975(); + upm::AK8975 sensor; - sensor->init(); + sensor.init(); - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getMagnetometer(&x, &y, &z); - cout << "Magnetometer: "; - cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + while (shouldRun) { + sensor.update(); - cout << endl; + float x, y, z; - usleep(500000); + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer: "; + cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/mpu9150-mpu60x0.cxx b/examples/c++/mpu9150-mpu60x0.cxx index 63882cd6..04c03bdf 100644 --- a/examples/c++/mpu9150-mpu60x0.cxx +++ b/examples/c++/mpu9150-mpu60x0.cxx @@ -22,56 +22,55 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include "mpu9150.hpp" + +#include "mpu60x0.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - upm::MPU60X0 *sensor = new upm::MPU60X0(); + upm::MPU60X0 sensor; - sensor->init(); + sensor.init(); - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getAccelerometer(&x, &y, &z); - cout << "Accelerometer: "; - cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + while (shouldRun) { + sensor.update(); - sensor->getGyroscope(&x, &y, &z); - cout << "Gryoscope: "; - cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; - - cout << "Temperature: " << sensor->getTemperature() << endl; - cout << endl; + float x, y, z; - usleep(500000); + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer: "; + cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + + sensor.getGyroscope(&x, &y, &z); + cout << "Gryoscope: "; + cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; + + cout << "Temperature: " << sensor.getTemperature() << endl; + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/mpu9150-mpu9250.cxx b/examples/c++/mpu9150-mpu9250.cxx index ecd88771..93ff415b 100644 --- a/examples/c++/mpu9150-mpu9250.cxx +++ b/examples/c++/mpu9150-mpu9250.cxx @@ -22,60 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "mpu9250.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - upm::MPU9250 *sensor = new upm::MPU9250(); + upm::MPU9250 sensor; - sensor->init(); + sensor.init(); - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getAccelerometer(&x, &y, &z); - cout << "Accelerometer: "; - cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + while (shouldRun) { + sensor.update(); - sensor->getGyroscope(&x, &y, &z); - cout << "Gryoscope: "; - cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; - - sensor->getMagnetometer(&x, &y, &z); - cout << "Magnetometer: "; - cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + float x, y, z; - cout << "Temperature: " << sensor->getTemperature() << endl; - cout << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer: "; + cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; - usleep(500000); + sensor.getGyroscope(&x, &y, &z); + cout << "Gryoscope: "; + cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; + + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer: "; + cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + + cout << "Temperature: " << sensor.getTemperature() << endl; + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/mpu9150.cxx b/examples/c++/mpu9150.cxx index df75057f..58860384 100644 --- a/examples/c++/mpu9150.cxx +++ b/examples/c++/mpu9150.cxx @@ -22,60 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "mpu9150.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - upm::MPU9150 *sensor = new upm::MPU9150(); + upm::MPU9150 sensor; - sensor->init(); + sensor.init(); - while (shouldRun) - { - sensor->update(); - - float x, y, z; - - sensor->getAccelerometer(&x, &y, &z); - cout << "Accelerometer: "; - cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; + while (shouldRun) { + sensor.update(); - sensor->getGyroscope(&x, &y, &z); - cout << "Gryoscope: "; - cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; - - sensor->getMagnetometer(&x, &y, &z); - cout << "Magnetometer: "; - cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + float x, y, z; - cout << "Temperature: " << sensor->getTemperature() << endl; - cout << endl; + sensor.getAccelerometer(&x, &y, &z); + cout << "Accelerometer: "; + cout << "AX: " << x << " AY: " << y << " AZ: " << z << endl; - usleep(500000); + sensor.getGyroscope(&x, &y, &z); + cout << "Gryoscope: "; + cout << "GX: " << x << " GY: " << y << " GZ: " << z << endl; + + sensor.getMagnetometer(&x, &y, &z); + cout << "Magnetometer: "; + cout << "MX = " << x << " MY = " << y << " MZ = " << z << endl; + + cout << "Temperature: " << sensor.getTemperature() << endl; + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/mq303a.cxx b/examples/c++/mq303a.cxx index 8a8764d1..9a2d5d9a 100644 --- a/examples/c++/mq303a.cxx +++ b/examples/c++/mq303a.cxx @@ -22,57 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "mq303a.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an mq303a sensor on analog pin A0 + //! [Interesting] + // Instantiate an mq303a sensor on analog pin A0 - // This device uses a heater powered from an analog I/O pin. - // If using A0 as the data pin, then you need to use A1, as the heater - // pin (if using a grove mq303a). For A1, we can use the D15 gpio, - // setup as an output, and drive it low to power the heater. + // This device uses a heater powered from an analog I/O pin. + // If using A0 as the data pin, then you need to use A1, as the heater + // pin (if using a grove mq303a). For A1, we can use the D15 gpio, + // setup as an output, and drive it low to power the heater. - upm::MQ303A *mq303a = new upm::MQ303A(0, 15); - - cout << "Enabling heater and waiting 2 minutes for warmup." << endl; - mq303a->heaterEnable(true); - sleep(120); + upm::MQ303A mq303a(0, 15); - cout << "This sensor may need to warm until the value drops below about 450." - << endl; + cout << "Enabling heater and waiting 2 minutes for warmup." << endl; + mq303a.heaterEnable(true); + upm_delay(120); - // Print the detected alcohol value every second - while (shouldRun) - { - int val = mq303a->value(); + cout << "This sensor may need to warm until the value drops below about 450." << endl; - cout << "Alcohol detected (higher means stronger alcohol): " - << val << endl; + // Print the detected alcohol value every second + while (shouldRun) { + int val = mq303a.value(); - sleep(1); + cout << "Alcohol detected (higher means stronger alcohol): " << val << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; - mq303a->heaterEnable(false); + cout << "Exiting" << endl; + mq303a.heaterEnable(false); - delete mq303a; - return 0; + return 0; } diff --git a/examples/c++/ms5611.cxx b/examples/c++/ms5611.cxx index f3622d49..5c4c738f 100644 --- a/examples/c++/ms5611.cxx +++ b/examples/c++/ms5611.cxx @@ -22,11 +22,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include "ms5611.hpp" +#include +#include "ms5611.hpp" +#include "upm_utilities.h" static int is_running = 0; @@ -41,23 +42,21 @@ sig_handler(int signo) //! [Interesting] int -main(int argc, char **argv) +main(int argc, char** argv) { - upm::MS5611* sensor = new upm::MS5611(0); + upm::MS5611 sensor(0); signal(SIGINT, sig_handler); - while (!is_running) { - int value = sensor->getTemperatureCelsius(); + while (!is_running) { + int value = sensor.getTemperatureCelsius(); std::cout << "Temperature = " << value << "C" << std::endl; - value = sensor->getPressurePa(); + value = sensor.getPressurePa(); std::cout << "Pressure = " << value << "Pa" << std::endl; - sleep(1); + upm_delay(1); } std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } //! [Interesting] diff --git a/examples/c++/ms5803.cxx b/examples/c++/ms5803.cxx index f06ae5b9..b2594262 100644 --- a/examples/c++/ms5803.cxx +++ b/examples/c++/ms5803.cxx @@ -22,57 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "ms5803.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a MS5803 instance using i2c bus 0 and default address - upm::MS5803 *sensor = new upm::MS5803(0); + upm::MS5803 sensor(0); // For SPI, bus 0, you would pass -1 as the address, and a valid // pin for CS (or -1 if you are using a hw pin you have no control // over, like edison): // MS5803(0, -1, 9); - while (shouldRun) - { + while (shouldRun) { // update our values from the sensor - sensor->update(); + sensor.update(); - cout << "Temperature: " - << sensor->getTemperature() - << " C, " - << "Pressure: " - << sensor->getPressure() - << " mbar" - << endl; + cout << "Temperature: " << sensor.getTemperature() << " C, " + << "Pressure: " << sensor.getPressure() << " mbar" << endl; - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; - delete sensor; - return 0; } diff --git a/examples/c++/my9221-grovecircularled.cxx b/examples/c++/my9221-grovecircularled.cxx index 54af0cf8..41094360 100644 --- a/examples/c++/my9221-grovecircularled.cxx +++ b/examples/c++/my9221-grovecircularled.cxx @@ -22,43 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "grovecircularled.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); - -//! [Interesting] - - // Instantiate a Grove Circular LED on D9 for data, D8 for clock - - upm::GroveCircularLED *circle = new upm::GroveCircularLED(9, 8); - - int level = 0; - while (shouldRun) - { - circle->setSpinner(level); - level = (level + 1) % 24; - usleep(100000); + signal(SIGINT, sig_handler); + + //! [Interesting] + + // Instantiate a Grove Circular LED on D9 for data, D8 for clock + + upm::GroveCircularLED circle(9, 8); + + int level = 0; + while (shouldRun) { + circle.setSpinner(level); + level = (level + 1) % 24; + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete circle; - return 0; + return 0; } diff --git a/examples/c++/my9221-groveledbar.cxx b/examples/c++/my9221-groveledbar.cxx index bc511838..53dbf887 100644 --- a/examples/c++/my9221-groveledbar.cxx +++ b/examples/c++/my9221-groveledbar.cxx @@ -22,56 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "groveledbar.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a GroveLEDBar, we use D8 for the data, and D9 for the - // clock. This was tested with a Grove LED bar. - upm::GroveLEDBar* bar = new upm::GroveLEDBar(8, 9); + // Instantiate a GroveLEDBar, we use D8 for the data, and D9 for the + // clock. This was tested with a Grove LED bar. + upm::GroveLEDBar bar(8, 9); - while (shouldRun) - { - // count up from green to red - for (int i=0; i<=10; i++) - { - bar->setBarLevel(i, true); - usleep(100000); + while (shouldRun) { + // count up from green to red + for (int i = 0; i <= 10; i++) { + bar.setBarLevel(i, true); + upm_delay_us(100000); } - sleep(1); + upm_delay(1); - // count down from red to green - for (int i=0; i<=10; i++) - { - bar->setBarLevel(i, false); - usleep(100000); + // count down from red to green + for (int i = 0; i <= 10; i++) { + bar.setBarLevel(i, false); + upm_delay_us(100000); } - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - // turn off the LED's - bar->setBarLevel(0); + cout << "Exiting..." << endl; + // turn off the LED's + bar.setBarLevel(0); - delete bar; - return 0; + return 0; } diff --git a/examples/c++/nlgpio16.cxx b/examples/c++/nlgpio16.cxx index 5e19ce68..27bc0cc2 100644 --- a/examples/c++/nlgpio16.cxx +++ b/examples/c++/nlgpio16.cxx @@ -22,32 +22,30 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "nlgpio16.hpp" using namespace std; using namespace upm; //! [Interesting] -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Instantiate a NLGPIO16 Module on the default UART (/dev/ttyACM0) - upm::NLGPIO16* sensor = new upm::NLGPIO16(); + // Instantiate a NLGPIO16 Module on the default UART (/dev/ttyACM0) + upm::NLGPIO16 sensor; - // get the Version - cout << "Device Version: " << sensor->getVersion() << endl; - // read the gpio at pin 3 - cout << "GPIO 3 Value: " << sensor->gpioRead(3) << endl; - // read the analog voltage at pin 5 - cout << "Analog 5 Voltage: " << sensor->analogReadVolts(5) << endl; - // set the gpio at pin 14 to HIGH - sensor->gpioSet(14); + // get the Version + cout << "Device Version: " << sensor.getVersion() << endl; + // read the gpio at pin 3 + cout << "GPIO 3 Value: " << sensor.gpioRead(3) << endl; + // read the analog voltage at pin 5 + cout << "Analog 5 Voltage: " << sensor.analogReadVolts(5) << endl; + // set the gpio at pin 14 to HIGH + sensor.gpioSet(14); - delete sensor; - return 0; + return 0; } //! [Interesting] diff --git a/examples/c++/nmea_gps-i2c.cxx b/examples/c++/nmea_gps-i2c.cxx index 74e4fa21..226e0e70 100644 --- a/examples/c++/nmea_gps-i2c.cxx +++ b/examples/c++/nmea_gps-i2c.cxx @@ -22,11 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "nmea_gps.hpp" +#include "upm_utilities.h" using namespace std; @@ -34,36 +34,35 @@ bool shouldRun = true; const size_t bufferLength = 128; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a NMEA_GPS UBLOX based i2c sensor on i2c bus 0 at - // address 0x42 - upm::NMEAGPS *sensor = new upm::NMEAGPS(0, 0x42); + // Instantiate a NMEA_GPS UBLOX based i2c sensor on i2c bus 0 at + // address 0x42 + upm::NMEAGPS sensor(0, 0x42); - // loop, dumping NMEA data out as fast as it comes in - while (shouldRun) - { - if (sensor->dataAvailable(0)) - usleep(100); - else - cout << sensor->readStr(bufferLength); + // loop, dumping NMEA data out as fast as it comes in + while (shouldRun) { + if (sensor.dataAvailable(0)) + upm_delay_us(100); + else + cout << sensor.readStr(bufferLength); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/nmea_gps.cxx b/examples/c++/nmea_gps.cxx index 8d4f62b0..457fc887 100644 --- a/examples/c++/nmea_gps.cxx +++ b/examples/c++/nmea_gps.cxx @@ -22,7 +22,6 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include @@ -34,36 +33,35 @@ bool shouldRun = true; const size_t bufferLength = 256; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a NMEA_GPS sensor on uart 0 at 9600 baud with enable - // pin on D3. If you do not need an enable pin, you can specify -1. - upm::NMEAGPS *sensor = new upm::NMEAGPS(0, 9600, 3); + // Instantiate a NMEA_GPS sensor on uart 0 at 9600 baud with enable + // pin on D3. If you do not need an enable pin, you can specify -1. + upm::NMEAGPS sensor(0, 9600, 3); - // loop, dumping NMEA data out as fast as it comes in - while (shouldRun && sensor->dataAvailable(5000)) - { - cout << sensor->readStr(bufferLength); + // loop, dumping NMEA data out as fast as it comes in + while (shouldRun && sensor.dataAvailable(5000)) { + cout << sensor.readStr(bufferLength); } - if (shouldRun) - cerr << "Timed out" << endl; + if (shouldRun) + cerr << "Timed out" << endl; -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/nrf24l01-broadcast.cxx b/examples/c++/nrf24l01-broadcast.cxx index 81c70e0c..180497fb 100644 --- a/examples/c++/nrf24l01-broadcast.cxx +++ b/examples/c++/nrf24l01-broadcast.cxx @@ -22,16 +22,17 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include -#include "nrf24l01.hpp" #include +#include #include +#include #include +#include "nrf24l01.hpp" +#include "upm_utilities.h" + int running = 0; -upm::NRF24L01 *sensor = NULL; void sig_handler(int signo) @@ -43,24 +44,26 @@ sig_handler(int signo) } } -void nrf_handler () { +void +nrf_handler() +{ } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - sensor = new upm::NRF24L01(7, 8); - sensor->setBeaconingMode (); + upm::NRF24L01 sensor(7, 8); + sensor.setBeaconingMode(); std::vector msgs; - msgs.push_back ("Hello World 1!!!"); - msgs.push_back ("Hello World 2!!!"); - msgs.push_back ("Hello World 3!!!"); - msgs.push_back ("Hello World 4!!!"); - msgs.push_back ("Hello World 5!!!"); + msgs.push_back("Hello World 1!!!"); + msgs.push_back("Hello World 2!!!"); + msgs.push_back("Hello World 3!!!"); + msgs.push_back("Hello World 4!!!"); + msgs.push_back("Hello World 5!!!"); signal(SIGINT, sig_handler); @@ -69,8 +72,8 @@ main(int argc, char **argv) std::cout << "BROADCASTING " << (*item).c_str() << std::endl; for (int i = 0; i < 3; i++) { - sensor->sendBeaconingMsg ((uint8_t*) (*item).c_str()); - usleep (1000000); + sensor.sendBeaconingMsg((uint8_t*) (*item).c_str()); + upm_delay_us(1000000); } } } @@ -78,8 +81,6 @@ main(int argc, char **argv) std::cout << "exiting application" << std::endl; msgs.clear(); - delete sensor; - //! [Interesting] return 0; diff --git a/examples/c++/nrf24l01-receiver.cxx b/examples/c++/nrf24l01-receiver.cxx index a2430afb..75cddd14 100644 --- a/examples/c++/nrf24l01-receiver.cxx +++ b/examples/c++/nrf24l01-receiver.cxx @@ -22,17 +22,18 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include -#include "nrf24l01.hpp" #include +#include +#include + +#include "nrf24l01.hpp" int running = 0; -upm::NRF24L01 *comm = NULL; -uint8_t local_address[5] = {0x01, 0x01, 0x01, 0x01, 0x01}; -uint8_t broadcast_address[5] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; +uint8_t local_address[5] = { 0x01, 0x01, 0x01, 0x01, 0x01 }; +uint8_t broadcast_address[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +upm::NRF24L01 comm(7, 8); void sig_handler(int signo) @@ -45,32 +46,31 @@ sig_handler(int signo) } //! [Interesting] -void nrf_handler () { - std::cout << "Reciever :: " << *((uint32_t *)&(comm->m_rxBuffer[0])) << std::endl; +void +nrf_handler() +{ + std::cout << "Reciever :: " << *((uint32_t*) &(comm.m_rxBuffer[0])) << std::endl; } int -main(int argc, char **argv) +main(int argc, char** argv) { - - comm = new upm::NRF24L01(7, 8); - comm->setSourceAddress ((uint8_t *) local_address); - comm->setDestinationAddress ((uint8_t *) broadcast_address); - comm->setPayload (MAX_BUFFER); - comm->configure (); - comm->setSpeedRate (upm::NRF_250KBPS); - comm->setChannel (99); - comm->setDataReceivedHandler (nrf_handler); + comm.setSourceAddress((uint8_t*) local_address); + comm.setDestinationAddress((uint8_t*) broadcast_address); + comm.setPayload(MAX_BUFFER); + comm.configure(); + comm.setSpeedRate(upm::NRF_250KBPS); + comm.setChannel(99); + comm.setDataReceivedHandler(nrf_handler); signal(SIGINT, sig_handler); while (!running) { - comm->pollListener (); + comm.pollListener(); } std::cout << "exiting application" << std::endl; - delete comm; return 0; } //! [Interesting] diff --git a/examples/c++/nrf24l01-transmitter.cxx b/examples/c++/nrf24l01-transmitter.cxx index d64cf2e2..2ad879cd 100644 --- a/examples/c++/nrf24l01-transmitter.cxx +++ b/examples/c++/nrf24l01-transmitter.cxx @@ -22,17 +22,19 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include -#include "nrf24l01.hpp" #include +#include +#include +#include + +#include "nrf24l01.hpp" +#include "upm_utilities.h" int running = 0; -upm::NRF24L01 *comm = NULL; -uint8_t destAddress[5] = {0x01, 0x01, 0x01, 0x01, 0x01}; -uint8_t srcAddress[5] = {0x01, 0x01, 0x01, 0x01, 0x02}; +uint8_t destAddress[5] = { 0x01, 0x01, 0x01, 0x01, 0x01 }; +uint8_t srcAddress[5] = { 0x01, 0x01, 0x01, 0x01, 0x02 }; void sig_handler(int signo) @@ -44,35 +46,36 @@ sig_handler(int signo) } } -void nrf_handler () { +void +nrf_handler() +{ } int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] uint32_t dummyData = 0; - comm = new upm::NRF24L01(7, 8); - comm->setSourceAddress ((uint8_t *) srcAddress); - comm->setDestinationAddress ((uint8_t *) destAddress); - comm->setPayload (MAX_BUFFER); - comm->setChannel (99); - comm->configure (); - comm->setDataReceivedHandler (nrf_handler); + upm::NRF24L01 comm(7, 8); + comm.setSourceAddress((uint8_t*) srcAddress); + comm.setDestinationAddress((uint8_t*) destAddress); + comm.setPayload(MAX_BUFFER); + comm.setChannel(99); + comm.configure(); + comm.setDataReceivedHandler(nrf_handler); signal(SIGINT, sig_handler); while (!running) { - memcpy (comm->m_txBuffer, &dummyData, sizeof (dummyData)); - comm->send (); + memcpy(comm.m_txBuffer, &dummyData, sizeof(dummyData)); + comm.send(); std::cout << "devi2 :: sending data ...." << dummyData << std::endl; - usleep (3000000); + upm_delay_us(3000000); dummyData += 3000; } std::cout << "exiting application" << std::endl; - delete comm; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/nrf8001-broadcast.cxx b/examples/c++/nrf8001-broadcast.cxx index 444f6a58..be99a98f 100644 --- a/examples/c++/nrf8001-broadcast.cxx +++ b/examples/c++/nrf8001-broadcast.cxx @@ -22,21 +22,25 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include "nrf8001.hpp" -#include "nrf8001-broadcast.hpp" -#include #include -#include #include +#include +#include +#include +#include + +#include "aci_evts.h" +#include "hal_aci_tl.h" +#include "nrf8001-broadcast.hpp" +#include "nrf8001.hpp" +#include "upm_utilities.h" #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT - static services_pipe_type_mapping_t - services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT; +static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = +SERVICES_PIPE_TYPE_MAPPING_CONTENT; #else - #define NUMBER_OF_PIPES 0 - static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL; +#define NUMBER_OF_PIPES 0 +static services_pipe_type_mapping_t* services_pipe_type_mapping = NULL; #endif /** @@ -48,7 +52,7 @@ static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT; * aci_struct that will contain * total initial credits * current credit - * current state of the aci (setup/standby/active/sleep) + * current state of the aci (setup/standby/active/upm_delay) * open remote pipe pending * close remote pipe pending * Current pipe available bitmap @@ -62,7 +66,7 @@ static struct aci_state_t aci_state; /** * Temporary buffers for sending ACI commands */ -static hal_aci_evt_t aci_data; +static hal_aci_evt_t aci_data; void sig_handler(int signo) @@ -74,90 +78,93 @@ sig_handler(int signo) } void -init_aci_setup () { +init_aci_setup() +{ /** - * Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001 - */ + * Point ACI data structures to the the setup data that the nRFgo studio + * generated for the nRF8001 + */ // abort if this is NULL assert(services_pipe_type_mapping != NULL); - aci_state.aci_setup_info.services_pipe_type_mapping = - &services_pipe_type_mapping[0]; + aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0]; - aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES; - aci_state.aci_setup_info.setup_msgs = setup_msgs; - aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES; + aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES; + aci_state.aci_setup_info.setup_msgs = setup_msgs; + aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES; } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - init_aci_setup (); - init_local_interfaces (&aci_state, 10, 8, 4); + init_aci_setup(); + init_local_interfaces(&aci_state, 10, 8, 4); while (1) { static bool setup_required = false; - if (lib_aci_event_get (&aci_state, &aci_data)) { - aci_evt_t * aci_evt; + if (lib_aci_event_get(&aci_state, &aci_data)) { + aci_evt_t* aci_evt; aci_evt = &aci_data.evt; - switch(aci_evt->evt_opcode) { + switch (aci_evt->evt_opcode) { /** - As soon as you reset the nRF8001 you will get an ACI Device Started Event - */ + As soon as you reset the nRF8001 you will get an ACI Device Started Event + */ case ACI_EVT_DEVICE_STARTED: { - aci_state.data_credit_available = aci_evt->params.device_started.credit_available; - switch(aci_evt->params.device_started.device_mode) { + aci_state.data_credit_available = + aci_evt->params.device_started.credit_available; + switch (aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /** - When the device is in the setup mode - */ - printf ("Evt Device Started: Setup\n"); + When the device is in the setup mode + */ + printf("Evt Device Started: Setup\n"); setup_required = true; - break; + break; case ACI_DEVICE_STANDBY: - printf ("Evt Device Started: Standby\n"); - lib_aci_broadcast(10/* in seconds */, 0x0100 /* advertising interval 100ms */); - printf ("Broadcasting started\n"); - break; + printf("Evt Device Started: Standby\n"); + lib_aci_broadcast(10 /* in seconds */, + 0x0100 /* advertising interval 100ms */); + printf("Broadcasting started\n"); + break; default: - break; + break; } - } - break; //ACI Device Started Event + } break; // ACI Device Started Event case ACI_EVT_CMD_RSP: if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { - printf ("ACI_EVT_CMD_RSP\n"); - while (1); + printf("ACI_EVT_CMD_RSP\n"); + while (1) + ; } - break; + break; case ACI_EVT_CONNECTED: - printf ("ACI_EVT_CONNECTED\n"); + printf("ACI_EVT_CONNECTED\n"); break; case ACI_EVT_PIPE_STATUS: - printf ("ACI_EVT_PIPE_STATUS\n"); + printf("ACI_EVT_PIPE_STATUS\n"); break; case ACI_EVT_DISCONNECTED: if (ACI_STATUS_ERROR_ADVT_TIMEOUT == aci_evt->params.disconnected.aci_status) { - printf ("Broadcasting timed out\n"); + printf("Broadcasting timed out\n"); } else { - printf ("Evt Disconnected. Link Loss\n"); + printf("Evt Disconnected. Link Loss\n"); } break; case ACI_EVT_DATA_RECEIVED: - printf ("ACI_EVT_DATA_RECEIVED\n"); + printf("ACI_EVT_DATA_RECEIVED\n"); break; case ACI_EVT_HW_ERROR: - printf ("ACI_EVT_HW_ERROR\n"); + printf("ACI_EVT_HW_ERROR\n"); break; default: break; @@ -169,10 +176,10 @@ main(int argc, char **argv) setup_required = false; } } - usleep (100); + upm_delay_us(100); } - close_local_interfaces (&aci_state); + close_local_interfaces(&aci_state); //! [Interesting] diff --git a/examples/c++/nrf8001-broadcast.hpp b/examples/c++/nrf8001-broadcast.hpp index b6f8cb5a..3b6e6175 100644 --- a/examples/c++/nrf8001-broadcast.hpp +++ b/examples/c++/nrf8001-broadcast.hpp @@ -1,5 +1,5 @@ -#include "hal_platform.h" #include "aci.h" +#include "hal_platform.h" #pragma once diff --git a/examples/c++/nrf8001-helloworld.cxx b/examples/c++/nrf8001-helloworld.cxx index 6b25ecd2..64fd2861 100644 --- a/examples/c++/nrf8001-helloworld.cxx +++ b/examples/c++/nrf8001-helloworld.cxx @@ -22,27 +22,35 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include "nrf8001.hpp" -#include "nrf8001-helloworld.hpp" -#include #include -#include #include +#include +#include +#include +#include +#include +#include + +#include "aci_cmds.h" +#include "aci_evts.h" +#include "hal_aci_tl.h" +#include "nrf8001-helloworld.hpp" +#include "nrf8001.hpp" #include "uart_over_ble.h" +#include "upm_utilities.h" /* - * You can use the nRF UART app in the Apple iOS app store and Google Play for Android 4.3 for Samsung Galaxy S4 + * You can use the nRF UART app in the Apple iOS app store and Google Play for + * Android 4.3 for Samsung Galaxy S4 * with this helloworld application */ #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT - static services_pipe_type_mapping_t - services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT; +static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = +SERVICES_PIPE_TYPE_MAPPING_CONTENT; #else - #define NUMBER_OF_PIPES 0 - static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL; +#define NUMBER_OF_PIPES 0 +static services_pipe_type_mapping_t* services_pipe_type_mapping = NULL; #endif /** @@ -54,7 +62,7 @@ static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT; * aci_struct that will contain * total initial credits * current credit - * current state of the aci (setup/standby/active/sleep) + * current state of the aci (setup/standby/active/upm_delay) * open remote pipe pending * close remote pipe pending * Current pipe available bitmap @@ -68,19 +76,19 @@ static struct aci_state_t aci_state; /** * Temporary buffers for sending ACI commands */ -static hal_aci_evt_t aci_data; +static hal_aci_evt_t aci_data; /* Timing change state variable */ -static bool timing_change_done = false; +static bool timing_change_done = false; /* Used to test the UART TX characteristic notification */ static uart_over_ble_t uart_over_ble; -static uint8_t uart_buffer[20]; -static uint8_t uart_buffer_len = 0; +static uint8_t uart_buffer[20]; +static uint8_t uart_buffer_len = 0; void sig_handler(int signo) @@ -92,31 +100,34 @@ sig_handler(int signo) } void -init_aci_setup () { +init_aci_setup() +{ /** - * Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001 - */ + * Point ACI data structures to the the setup data that the nRFgo studio + * generated for the nRF8001 + */ // abort if this is NULL assert(services_pipe_type_mapping != NULL); - aci_state.aci_setup_info.services_pipe_type_mapping = - &services_pipe_type_mapping[0]; + aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0]; - aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES; - aci_state.aci_setup_info.setup_msgs = setup_msgs; - aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES; + aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES; + aci_state.aci_setup_info.setup_msgs = setup_msgs; + aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES; } void -uart_over_ble_init (void) { +uart_over_ble_init(void) +{ uart_over_ble.uart_rts_local = true; } bool -uart_tx (uint8_t *buffer, uint8_t buffer_len) { +uart_tx(uint8_t* buffer, uint8_t buffer_len) +{ bool status = false; - if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && + if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (aci_state.data_credit_available >= 1)) { status = lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, buffer, buffer_len); if (status) { @@ -128,67 +139,68 @@ uart_tx (uint8_t *buffer, uint8_t buffer_len) { } bool -uart_process_control_point_rx(uint8_t *byte, uint8_t length) { +uart_process_control_point_rx(uint8_t* byte, uint8_t length) +{ bool status = false; - aci_ll_conn_params_t *conn_params; + aci_ll_conn_params_t* conn_params; - if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX) ) { + if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX)) { switch (*byte) { /* - Queues a ACI Disconnect to the nRF8001 when this packet is received. - May cause some of the UART packets being sent to be dropped - */ + Queues a ACI Disconnect to the nRF8001 when this packet is received. + May cause some of the UART packets being sent to be dropped + */ case UART_OVER_BLE_DISCONNECT: /* - Parameters: - None - */ + Parameters: + None + */ lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE); status = true; break; /* - Queues an ACI Change Timing to the nRF8001 - */ + Queues an ACI Change Timing to the nRF8001 + */ case UART_OVER_BLE_LINK_TIMING_REQ: /* - Parameters: - Connection interval min: 2 bytes - Connection interval max: 2 bytes - Slave latency: 2 bytes - Timeout: 2 bytes - Same format as Peripheral Preferred Connection Parameters (See nRFgo studio -> nRF8001 Configuration -> GAP Settings - Refer to the ACI Change Timing Request in the nRF8001 Product Specifications - */ - conn_params = (aci_ll_conn_params_t *)(byte+1); - lib_aci_change_timing( conn_params->min_conn_interval, - conn_params->max_conn_interval, - conn_params->slave_latency, - conn_params->timeout_mult); + Parameters: + Connection interval min: 2 bytes + Connection interval max: 2 bytes + Slave latency: 2 bytes + Timeout: 2 bytes + Same format as Peripheral Preferred Connection Parameters (See nRFgo studio -> + nRF8001 Configuration -> GAP Settings + Refer to the ACI Change Timing Request in the nRF8001 Product Specifications + */ + conn_params = (aci_ll_conn_params_t*) (byte + 1); + lib_aci_change_timing(conn_params->min_conn_interval, + conn_params->max_conn_interval, + conn_params->slave_latency, + conn_params->timeout_mult); status = true; break; /* - Clears the RTS of the UART over BLE - */ + Clears the RTS of the UART over BLE + */ case UART_OVER_BLE_TRANSMIT_STOP: /* - Parameters: - None - */ + Parameters: + None + */ uart_over_ble.uart_rts_local = false; status = true; break; - /* - Set the RTS of the UART over BLE - */ + Set the RTS of the UART over BLE + */ case UART_OVER_BLE_TRANSMIT_OK: /* - Parameters: - None - */ + Parameters: + None + */ uart_over_ble.uart_rts_local = true; status = true; break; @@ -199,109 +211,132 @@ uart_process_control_point_rx(uint8_t *byte, uint8_t length) { } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - init_aci_setup (); - init_local_interfaces (&aci_state, 10, 8, 4); + init_aci_setup(); + init_local_interfaces(&aci_state, 10, 8, 4); while (1) { static bool setup_required = false; - // We enter the if statement only when there is a ACI event available to be processed + // We enter the if statement only when there is a ACI event available to be + // processed if (lib_aci_event_get(&aci_state, &aci_data)) { - aci_evt_t * aci_evt; + aci_evt_t* aci_evt; aci_evt = &aci_data.evt; - switch(aci_evt->evt_opcode) { + switch (aci_evt->evt_opcode) { /** - As soon as you reset the nRF8001 you will get an ACI Device Started Event - */ + As soon as you reset the nRF8001 you will get an ACI Device Started Event + */ case ACI_EVT_DEVICE_STARTED: { aci_state.data_credit_total = aci_evt->params.device_started.credit_available; - switch(aci_evt->params.device_started.device_mode) { + switch (aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /** - When the device is in the setup mode - */ - printf ("Evt Device Started: Setup \n"); + When the device is in the setup mode + */ + printf("Evt Device Started: Setup \n"); setup_required = true; break; case ACI_DEVICE_STANDBY: - printf ("Evt Device Started: Standby \n"); - // Looking for an iPhone by sending radio advertisements - // When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 + printf("Evt Device Started: Standby \n"); + // Looking for an iPhone by sending radio + // advertisements + // When an iPhone connects to us we will get an + // ACI_EVT_CONNECTED + // event from the nRF8001 if (aci_evt->params.device_started.hw_error) { - usleep (20000); //Handle the HW error event correctly. + upm_delay_us(20000); // Handle the HW error + // event correctly. } else { - lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/); - printf ("Advertising started \n"); + lib_aci_connect(0 /* in seconds : 0 means forever */, + 0x0050 /* advertising interval 50ms*/); + printf("Advertising started \n"); } break; default: break; } - } - break; // ACI Device Started Event + } break; // ACI Device Started Event case ACI_EVT_CMD_RSP: - //If an ACI command response event comes with an error -> stop + // If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { - //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of - //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE - //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command - printf ("ACI_EVT_CMD_RSP \n"); + // ACI ReadDynamicData and ACI WriteDynamicData will have + // status + // codes of + // TRANSACTION_CONTINUE and TRANSACTION_COMPLETE + // all other ACI commands will have status code of + // ACI_STATUS_SCUCCESS for a successful command + printf("ACI_EVT_CMD_RSP \n"); } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { - //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic - lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, - (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); + // Store the version and configuration information of the + // nRF8001 in + // the Hardware Revision String Characteristic + lib_aci_set_local_data(&aci_state, + PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, + (uint8_t*) &( + aci_evt->params.cmd_rsp.params.get_device_version), + sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: - printf ("ACI_EVT_CONNECTED"); - uart_over_ble_init (); + printf("ACI_EVT_CONNECTED"); + uart_over_ble_init(); timing_change_done = false; aci_state.data_credit_available = aci_state.data_credit_total; /* - Get the device version of the nRF8001 and store it in the Hardware Revision String - */ + Get the device version of the nRF8001 and store it in the Hardware Revision + String + */ lib_aci_device_version(); break; case ACI_EVT_PIPE_STATUS: - printf ("ACI_EVT_PIPE_STATUS \n"); - if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { - lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. - // Used to increase or decrease bandwidth + printf("ACI_EVT_PIPE_STATUS \n"); + if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && + (false == timing_change_done)) { + lib_aci_change_timing_GAP_PPCP(); // change the timing on + // the link + // as specified in the nRFgo + // studio -> nRF8001 conf. -> + // GAP. + // Used to increase or decrease bandwidth timing_change_done = true; - char hello[]="Hello World, works"; - uart_tx((uint8_t *)&hello[0], strlen(hello)); + char hello[] = "Hello World, works"; + uart_tx((uint8_t*) &hello[0], strlen(hello)); } break; case ACI_EVT_TIMING: - printf ("Evt link connection interval changed \n"); + printf("Evt link connection interval changed \n"); lib_aci_set_local_data(&aci_state, - PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET, - (uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */ - PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE); + PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET, + (uint8_t*) &( + aci_evt->params.timing.conn_rf_interval), /* Byte + aligned */ + PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE); break; case ACI_EVT_DISCONNECTED: - printf ("ACI_EVT_DISCONNECTED \n"); - lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/); - printf ("Advertising started \n"); + printf("ACI_EVT_DISCONNECTED \n"); + lib_aci_connect(0 /* in seconds : 0 means forever */, + 0x0050 /* advertising interval 50ms*/); + printf("Advertising started \n"); break; case ACI_EVT_DATA_RECEIVED: - if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number) { - for(int i=0; ilen - 2; i++) { - uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i]; + if (PIPE_UART_OVER_BTLE_UART_RX_RX == + aci_evt->params.data_received.rx_data.pipe_number) { + for (int i = 0; i < aci_evt->len - 2; i++) { + uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i]; } uart_buffer_len = aci_evt->len - 2; @@ -309,53 +344,60 @@ main(int argc, char **argv) } } - if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == aci_evt->params.data_received.rx_data.pipe_number) { - //Subtract for Opcode and Pipe number - uart_process_control_point_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); + if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == + aci_evt->params.data_received.rx_data.pipe_number) { + // Subtract for Opcode and Pipe number + uart_process_control_point_rx( + &aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); } - printf ("Incomming data - %s\n", uart_buffer); + printf("Incomming data - %s\n", uart_buffer); break; case ACI_EVT_DATA_CREDIT: - printf ("ACI_EVT_DATA_CREDIT \n"); - aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; + printf("ACI_EVT_DATA_CREDIT \n"); + aci_state.data_credit_available = + aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: - printf ("ACI_EVT_PIPE_ERROR \n"); - //Increment the credit available as the data packet was not sent. - //The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted - //for the credit. + printf("ACI_EVT_PIPE_ERROR \n"); + // Increment the credit available as the data packet was not sent. + // The pipe error also represents the Attribute protocol Error + // Response sent from the peer and that should not be counted + // for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } break; case ACI_EVT_HW_ERROR: - printf ("ACI_EVT_HW_ERROR \n"); - lib_aci_connect(0/* in seconds, 0 means forever */, 0x0050 /* advertising interval 50ms*/); - printf ("Advertising started \n"); - break; + printf("ACI_EVT_HW_ERROR \n"); + lib_aci_connect(0 /* in seconds, 0 means forever */, + 0x0050 /* advertising interval 50ms*/); + printf("Advertising started \n"); + break; default: - break; + break; } } - /* setup_required is set to true when the device starts up and enters setup mode. - * It indicates that do_aci_setup() should be called. The flag should be cleared if - * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. - */ - if(setup_required) { + /* setup_required is set to true when the device starts up and enters setup + * mode. + * It indicates that do_aci_setup() should be called. The flag should be cleared + * if + * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. + */ + if (setup_required) { if (SETUP_SUCCESS == do_aci_setup(&aci_state)) { setup_required = false; } } - usleep (100); + upm_delay_us(100); } - close_local_interfaces (&aci_state); + close_local_interfaces(&aci_state); //! [Interesting] diff --git a/examples/c++/nrf8001-helloworld.hpp b/examples/c++/nrf8001-helloworld.hpp index ec566b52..e9809c5c 100644 --- a/examples/c++/nrf8001-helloworld.hpp +++ b/examples/c++/nrf8001-helloworld.hpp @@ -1,5 +1,5 @@ -#include "hal_platform.h" #include "aci.h" +#include "hal_platform.h" #pragma once diff --git a/examples/c++/nunchuck.cxx b/examples/c++/nunchuck.cxx index b004df5d..968e1f6a 100644 --- a/examples/c++/nunchuck.cxx +++ b/examples/c++/nunchuck.cxx @@ -22,50 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "nunchuck.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a nunchuck controller bus 3 - upm::NUNCHUCK *nunchuck = new upm::NUNCHUCK(3); + //! [Interesting] + // Instantiate a nunchuck controller bus 3 + upm::NUNCHUCK nunchuck(3); - while (shouldRun) - { - nunchuck->update(); + while (shouldRun) { + nunchuck.update(); - cout << "stickX: " << nunchuck->stickX - << ", stickY: " << nunchuck->stickY << endl; - cout << "accelX: " << nunchuck->accelX - << ", accelY: " << nunchuck->accelY - << ", accelZ: " << nunchuck->accelZ << endl; + cout << "stickX: " << nunchuck.stickX << ", stickY: " << nunchuck.stickY << endl; + cout << "accelX: " << nunchuck.accelX << ", accelY: " << nunchuck.accelY + << ", accelZ: " << nunchuck.accelZ << endl; - cout << "button C: " - << ((nunchuck->buttonC) ? "pressed" : "not pressed") << endl; - cout << "button Z: " - << ((nunchuck->buttonZ) ? "pressed" : "not pressed") << endl; - cout << endl; + cout << "button C: " << ((nunchuck.buttonC) ? "pressed" : "not pressed") << endl; + cout << "button Z: " << ((nunchuck.buttonZ) ? "pressed" : "not pressed") << endl; + cout << endl; - usleep(100000); + upm_delay_us(100000); } - //! [Interesting] + //! [Interesting] - delete nunchuck; - return 0; + return 0; } diff --git a/examples/c++/o2.cxx b/examples/c++/o2.cxx index 25140cc3..c82f2fd9 100644 --- a/examples/c++/o2.cxx +++ b/examples/c++/o2.cxx @@ -21,37 +21,38 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "o2.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the O2 Oxygen Concentration Sensor Module - // Instantiate a O2 on analog pin A0 - upm::O2 *O2 = new upm::O2(0); - while (shouldRun) - { - cout << "The output voltage is: " << O2->voltageValue() << "mV" << endl; - usleep(100000); - } + //! [Interesting] + // The was tested with the O2 Oxygen Concentration Sensor Module + // Instantiate a O2 on analog pin A0 + upm::O2 O2(0); + while (shouldRun) { + cout << "The output voltage is: " << O2.voltageValue() << "mV" << endl; + upm_delay_us(100000); + } -//! [Interesting] - cout << "Exiting" << endl; - delete O2; - return 0; + //! [Interesting] + cout << "Exiting" << endl; + return 0; } diff --git a/examples/c++/otp538u.cxx b/examples/c++/otp538u.cxx index b32df2d9..efaefe0f 100644 --- a/examples/c++/otp538u.cxx +++ b/examples/c++/otp538u.cxx @@ -22,61 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include -#include +#include #include +#include +#include + #include "otp538u.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; // analog voltage, usually 3.3 or 5.0 -#define OTP538U_AREF 5.0 +#define OTP538U_AREF 5.0 -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a OTP538U on analog pins A0 and A1 - // A0 is used for the Ambient Temperature and A1 is used for the - // Object temperature. - upm::OTP538U *temps = new upm::OTP538U(0, 1, OTP538U_AREF); - - // enable debugging if you would like - // temps->setDebug(true); + // Instantiate a OTP538U on analog pins A0 and A1 + // A0 is used for the Ambient Temperature and A1 is used for the + // Object temperature. + upm::OTP538U temps(0, 1, OTP538U_AREF); - // Output ambient and object temperatures - while (shouldRun) - { - try { - cout << "Ambient temp: " << std::fixed << setprecision(2) - << temps->ambientTemperature() - << " C, Object temp: " << temps->objectTemperature() - << " C" << endl; - } - catch (std::out_of_range& e) { - cerr << "Temperature(s) are out of range: " << e.what() - << endl; - } + // enable debugging if you would like + // temps.setDebug(true); - cout << endl; - sleep(1); + // Output ambient and object temperatures + while (shouldRun) { + try { + cout << "Ambient temp: " << std::fixed << setprecision(2) << temps.ambientTemperature() + << " C, Object temp: " << temps.objectTemperature() << " C" << endl; + } catch (std::out_of_range& e) { + cerr << "Temperature(s) are out of range: " << e.what() << endl; + } + + cout << endl; + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete temps; - return 0; + return 0; } diff --git a/examples/c++/ozw-aeotecdsb09104.cxx b/examples/c++/ozw-aeotecdsb09104.cxx index f0939342..8d8110df 100644 --- a/examples/c++/ozw-aeotecdsb09104.cxx +++ b/examples/c++/ozw-aeotecdsb09104.cxx @@ -22,103 +22,79 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "aeotecdsb09104.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate an Aeotec DSB09104 instance, on device node 12. You - // will almost certainly need to change this to reflect your own - // network. Use the ozwdump example to see what nodes are - // available. - upm::AeotecDSB09104 *sensor = new upm::AeotecDSB09104(12); + // Instantiate an Aeotec DSB09104 instance, on device node 12. You + // will almost certainly need to change this to reflect your own + // network. Use the ozwdump example to see what nodes are + // available. + upm::AeotecDSB09104 sensor(12); - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - cout << "Querying data..." << endl; - while (shouldRun) - { - sensor->update(); + cout << "Querying data..." << endl; + while (shouldRun) { + sensor.update(); - cout << "Watts, Channel 1: " - << sensor->getWattsC1() - << " W" - << endl; + cout << "Watts, Channel 1: " << sensor.getWattsC1() << " W" << endl; - cout << "Watts, Channel 2: " - << sensor->getWattsC2() - << " W" - << endl; + cout << "Watts, Channel 2: " << sensor.getWattsC2() << " W" << endl; - cout << "Watts, Channel 3: " - << sensor->getWattsC3() - << " W" - << endl; + cout << "Watts, Channel 3: " << sensor.getWattsC3() << " W" << endl; - cout << "Energy, Channel 1: " - << sensor->getEnergyC1() - << " kWh" - << endl; + cout << "Energy, Channel 1: " << sensor.getEnergyC1() << " kWh" << endl; - cout << "Energy, Channel 2: " - << sensor->getEnergyC2() - << " kWh" - << endl; + cout << "Energy, Channel 2: " << sensor.getEnergyC2() << " kWh" << endl; - cout << "Energy, Channel 3: " - << sensor->getEnergyC3() - << " kWh" - << endl; + cout << "Energy, Channel 3: " << sensor.getEnergyC3() << " kWh" << endl; - cout << "Battery Level: " - << sensor->getBatteryLevel() - << "%" - << endl; + cout << "Battery Level: " << sensor.getBatteryLevel() << "%" << endl; - cout << endl; + cout << endl; - sleep(3); + upm_delay(3); } + // sensor.dumpNodes(true); + cout << "Exiting..." << endl; - // sensor->dumpNodes(true); - cout << "Exiting..." << endl; - - delete sensor; - - //! [Interesting] - return 0; + //! [Interesting] + return 0; } diff --git a/examples/c++/ozw-aeotecdw2e.cxx b/examples/c++/ozw-aeotecdw2e.cxx index 671dd905..0f7ee3c4 100644 --- a/examples/c++/ozw-aeotecdw2e.cxx +++ b/examples/c++/ozw-aeotecdw2e.cxx @@ -22,89 +22,76 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "aeotecdw2e.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate an Aeotec Door/Window 2nd Edition sensor instance, on - // device node 10. You will almost certainly need to change this to - // reflect your own network. Use the ozwdump example to see what - // nodes are available. - upm::AeotecDW2E *sensor = new upm::AeotecDW2E(10); + // Instantiate an Aeotec Door/Window 2nd Edition sensor instance, on + // device node 10. You will almost certainly need to change this to + // reflect your own network. Use the ozwdump example to see what + // nodes are available. + upm::AeotecDW2E sensor(10); - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - cout << "Querying data..." << endl; - while (shouldRun) - { - if (sensor->isDeviceAvailable()) - { - cout << "Alarm status: " - << sensor->isAlarmTripped() - << endl; + cout << "Querying data..." << endl; + while (shouldRun) { + if (sensor.isDeviceAvailable()) { + cout << "Alarm status: " << sensor.isAlarmTripped() << endl; - cout << "Tamper Switch status: " - << sensor->isTamperTripped() - << endl; + cout << "Tamper Switch status: " << sensor.isTamperTripped() << endl; - cout << "Battery Level: " - << sensor->getBatteryLevel() - << "%" - << endl; - cout << endl; - } - else - { - cout << "Device has not yet responded to probe." - << endl; + cout << "Battery Level: " << sensor.getBatteryLevel() << "%" << endl; + cout << endl; + } else { + cout << "Device has not yet responded to probe." << endl; - cout << "Try waking it, or wait until it wakes itself if configured" - << " to do so." - << endl; - cout << endl; + cout << "Try waking it, or wait until it wakes itself if configured" + << " to do so." << endl; + cout << endl; } - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] + //! [Interesting] - return 0; + return 0; } diff --git a/examples/c++/ozw-aeotecsdg2.cxx b/examples/c++/ozw-aeotecsdg2.cxx index 9f64f8bb..679c89da 100644 --- a/examples/c++/ozw-aeotecsdg2.cxx +++ b/examples/c++/ozw-aeotecsdg2.cxx @@ -22,107 +22,93 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "aeotecsdg2.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate an Aeotec Smart Dimmer Gen2 instance, on device node - // 9. You will almost certainly need to change this to reflect your - // own network. Use the ozwdump example to see what nodes are - // available. - upm::AeotecSDG2 *sensor = new upm::AeotecSDG2(9); + // Instantiate an Aeotec Smart Dimmer Gen2 instance, on device node + // 9. You will almost certainly need to change this to reflect your + // own network. Use the ozwdump example to see what nodes are + // available. + upm::AeotecSDG2 sensor(9); - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - // turn light on - cout << "Turning switch on, then sleeping for 5 secs" << endl; - sensor->on(); - sleep(5); + // turn light on + cout << "Turning switch on, then upm_delaying for 5 secs" << endl; + sensor.on(); + upm_delay(5); - cout << "Querying data..." << endl; - bool dim = false; - while (shouldRun) - { - // put on a light show... - if (dim) - sensor->setLevel(25); - else - sensor->on(); + cout << "Querying data..." << endl; + bool dim = false; + while (shouldRun) { + // put on a light show... + if (dim) + sensor.setLevel(25); + else + sensor.on(); - dim = !dim; + dim = !dim; - sensor->update(); + sensor.update(); - cout << "Current Level: " - << sensor->getLevel() - << endl; + cout << "Current Level: " << sensor.getLevel() << endl; - cout << "Volts: " - << sensor->getVolts() - << " volts" - << endl; + cout << "Volts: " << sensor.getVolts() << " volts" << endl; - cout << "Energy Consumption: " - << sensor->getEnergy() - << " kWh" - << endl; + cout << "Energy Consumption: " << sensor.getEnergy() << " kWh" << endl; - cout << "Watts: " - << sensor->getWatts() - << endl; + cout << "Watts: " << sensor.getWatts() << endl; - cout << "Current: " - << sensor->getCurrent() - << " amps" - << endl; + cout << "Current: " << sensor.getCurrent() << " amps" << endl; - cout << endl; + cout << endl; - sleep(5); + upm_delay(5); } - cout << "Turning switch off and sleeping for 5 seconds..." << endl; - sensor->off(); - sleep(5); + cout << "Turning switch off and upm_delaying for 5 seconds..." << endl; + sensor.off(); + upm_delay(5); - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] + //! [Interesting] - return 0; + return 0; } diff --git a/examples/c++/ozw-aeotecss6.cxx b/examples/c++/ozw-aeotecss6.cxx index 6e889c4b..cff91ef4 100644 --- a/examples/c++/ozw-aeotecss6.cxx +++ b/examples/c++/ozw-aeotecss6.cxx @@ -22,97 +22,83 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "aeotecss6.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate an Aeotec SS6 instance, on device node 11. You will - // almost certainly need to change this to reflect your own network. - // Use the ozwdump example to see what nodes are available. - upm::AeotecSS6 *sensor = new upm::AeotecSS6(11); + // Instantiate an Aeotec SS6 instance, on device node 11. You will + // almost certainly need to change this to reflect your own network. + // Use the ozwdump example to see what nodes are available. + upm::AeotecSS6 sensor(11); - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - // turn light on - cout << "Turning switch on, then sleeping for 5 secs" << endl; - sensor->on(); - sleep(5); + // turn light on + cout << "Turning switch on, then upm_delaying for 5 secs" << endl; + sensor.on(); + upm_delay(5); - cout << "Querying data..." << endl; - while (shouldRun) - { - sensor->update(); + cout << "Querying data..." << endl; + while (shouldRun) { + sensor.update(); - cout << "Switch status: " - << sensor->isOn() - << endl; + cout << "Switch status: " << sensor.isOn() << endl; - cout << "Volts: " - << sensor->getVolts() - << " volts" - << endl; + cout << "Volts: " << sensor.getVolts() << " volts" << endl; - cout << "Energy Consumption: " - << sensor->getEnergy() - << " kWh" - << endl; + cout << "Energy Consumption: " << sensor.getEnergy() << " kWh" << endl; - cout << "Watts: " - << sensor->getWatts() - << endl; + cout << "Watts: " << sensor.getWatts() << endl; - cout << "Current: " - << sensor->getCurrent() - << " amps" - << endl; + cout << "Current: " << sensor.getCurrent() << " amps" << endl; - cout << endl; + cout << endl; - sleep(3); + upm_delay(3); } - cout << "Turning switch off and sleeping for 5 seconds..." << endl; - sensor->off(); - sleep(5); + cout << "Turning switch off and upm_delaying for 5 seconds..." << endl; + sensor.off(); + upm_delay(5); - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; -//! [Interesting] + //! [Interesting] - return 0; + return 0; } diff --git a/examples/c++/ozw-ozwdump.cxx b/examples/c++/ozw-ozwdump.cxx index 211b1f8d..574cbe44 100644 --- a/examples/c++/ozw-ozwdump.cxx +++ b/examples/c++/ozw-ozwdump.cxx @@ -22,91 +22,89 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include #include "ozwdump.hpp" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - string defaultDev = "/dev/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate an OZWDUMP instance - upm::OZWDUMP *sensor = new upm::OZWDUMP(); + // Instantiate an OZWDUMP instance + upm::OZWDUMP sensor; - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - cout << "Dumping nodes..." << endl; + cout << "Dumping nodes..." << endl; - sensor->dumpNodes(true); + sensor.dumpNodes(true); - // The following is example output of dumpNodes: - // - // Dumping nodes... - // Node 1: Z-Stick Gen5 - // Node 2: Smart Switch 6 - // Index: 0, Type: bool, Label: Switch, Value: False - // Index: 2, Type: float, Label: Energy, Value: 1.190 kWh - // Index: 3, Type: float, Label: Previous Reading, Value: 1.190 kWh - // Index: 4, Type: int32, Label: Interval, Value: 1521 seconds - // Index: 5, Type: float, Label: Power, Value: 0.000 W - // Index: 6, Type: float, Label: Voltage, Value: 121.256 V - // Index: 7, Type: float, Label: Current, Value: 0.000 A - // Index: 8, Type: bool, Label: Exporting, Value: False - // Index: 45, Type: list, Label: Day, Value: Friday - // Index: 46, Type: byte, Label: Hour, Value: 5 - // Index: 47, Type: byte, Label: Minute, Value: 53 - // Node 3: Multi Sensor - // Index: 0, Type: bool, Label: Sensor, Value: True - // Index: 1, Type: float, Label: Temperature, Value: 72.8 F - // Index: 2, Type: float, Label: Luminance, Value: 4 lux - // Index: 3, Type: float, Label: Relative Humidity, Value: 22 % - // Index: 17, Type: byte, Label: Battery Level, Value: 98 % - // Node 5: Minimote - // Node 6: Smart Energy Switch - // Index: 0, Type: bool, Label: Switch, Value: False - // Index: 2, Type: float, Label: Power, Value: 0.000 W - // Index: 3, Type: float, Label: Energy, Value: 1.609 kWh - // Index: 4, Type: float, Label: Previous Reading, Value: 1.609 kWh - // Index: 5, Type: int32, Label: Interval, Value: 1521 seconds - // Index: 6, Type: float, Label: Power, Value: 0.000 W - // Index: 7, Type: float, Label: Previous Reading, Value: 1.609 W - // Index: 8, Type: int32, Label: Interval, Value: 1521 seconds - // Index: 9, Type: bool, Label: Exporting, Value: False - // Node 7: Smart Energy Switch - // Index: 0, Type: bool, Label: Switch, Value: False - // Index: 2, Type: float, Label: Power, Value: 0.000 W - // Index: 3, Type: float, Label: Energy, Value: 0.000 kWh - // Index: 4, Type: float, Label: Previous Reading, Value: 0.000 kWh - // Index: 5, Type: int32, Label: Interval, Value: 1521 seconds - // Index: 6, Type: float, Label: Power, Value: 0.000 W - // Index: 7, Type: float, Label: Previous Reading, Value: 0.000 W - // Index: 8, Type: int32, Label: Interval, Value: 1521 seconds - // Index: 9, Type: bool, Label: Exporting, Value: False - // + // The following is example output of dumpNodes: + // + // Dumping nodes... + // Node 1: Z-Stick Gen5 + // Node 2: Smart Switch 6 + // Index: 0, Type: bool, Label: Switch, Value: False + // Index: 2, Type: float, Label: Energy, Value: 1.190 kWh + // Index: 3, Type: float, Label: Previous Reading, Value: 1.190 kWh + // Index: 4, Type: int32, Label: Interval, Value: 1521 seconds + // Index: 5, Type: float, Label: Power, Value: 0.000 W + // Index: 6, Type: float, Label: Voltage, Value: 121.256 V + // Index: 7, Type: float, Label: Current, Value: 0.000 A + // Index: 8, Type: bool, Label: Exporting, Value: False + // Index: 45, Type: list, Label: Day, Value: Friday + // Index: 46, Type: byte, Label: Hour, Value: 5 + // Index: 47, Type: byte, Label: Minute, Value: 53 + // Node 3: Multi Sensor + // Index: 0, Type: bool, Label: Sensor, Value: True + // Index: 1, Type: float, Label: Temperature, Value: 72.8 F + // Index: 2, Type: float, Label: Luminance, Value: 4 lux + // Index: 3, Type: float, Label: Relative Humidity, Value: 22 % + // Index: 17, Type: byte, Label: Battery Level, Value: 98 % + // Node 5: Minimote + // Node 6: Smart Energy Switch + // Index: 0, Type: bool, Label: Switch, Value: False + // Index: 2, Type: float, Label: Power, Value: 0.000 W + // Index: 3, Type: float, Label: Energy, Value: 1.609 kWh + // Index: 4, Type: float, Label: Previous Reading, Value: 1.609 kWh + // Index: 5, Type: int32, Label: Interval, Value: 1521 seconds + // Index: 6, Type: float, Label: Power, Value: 0.000 W + // Index: 7, Type: float, Label: Previous Reading, Value: 1.609 W + // Index: 8, Type: int32, Label: Interval, Value: 1521 seconds + // Index: 9, Type: bool, Label: Exporting, Value: False + // Node 7: Smart Energy Switch + // Index: 0, Type: bool, Label: Switch, Value: False + // Index: 2, Type: float, Label: Power, Value: 0.000 W + // Index: 3, Type: float, Label: Energy, Value: 0.000 kWh + // Index: 4, Type: float, Label: Previous Reading, Value: 0.000 kWh + // Index: 5, Type: int32, Label: Interval, Value: 1521 seconds + // Index: 6, Type: float, Label: Power, Value: 0.000 W + // Index: 7, Type: float, Label: Previous Reading, Value: 0.000 W + // Index: 8, Type: int32, Label: Interval, Value: 1521 seconds + // Index: 9, Type: bool, Label: Exporting, Value: False + // -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; - - return 0; + return 0; } diff --git a/examples/c++/ozw-tzemt400.cxx b/examples/c++/ozw-tzemt400.cxx index fb58d09d..b89d2456 100644 --- a/examples/c++/ozw-tzemt400.cxx +++ b/examples/c++/ozw-tzemt400.cxx @@ -22,94 +22,81 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "tzemt400.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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/ttyACM0"; + string defaultDev = "/dev/ttyACM0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - // Instantiate a TZEMT400 instance, on device node 13. You will - // almost certainly need to change this to reflect your own network. - // Use the ozwdump example to see what nodes are available. - upm::TZEMT400 *sensor = new upm::TZEMT400(13); + // Instantiate a TZEMT400 instance, on device node 13. You will + // almost certainly need to change this to reflect your own network. + // Use the ozwdump example to see what nodes are available. + upm::TZEMT400 sensor(13); - // The first thing to do is create options, then lock them when done. - sensor->optionsCreate(); - sensor->optionsLock(); + // The first thing to do is create options, then lock them when done. + sensor.optionsCreate(); + sensor.optionsLock(); - // Next, initialize it. - cout << "Initializing, this may take awhile depending on your ZWave network" - << endl; + // Next, initialize it. + cout << "Initializing, this may take awhile depending on your ZWave network" << endl; - sensor->init(defaultDev); - cout << "Initialization complete" << endl; + sensor.init(defaultDev); + cout << "Initialization complete" << endl; - cout << "Querying data..." << endl; - while (shouldRun) - { - sensor->update(); + cout << "Querying data..." << endl; + while (shouldRun) { + sensor.update(); - // we show both C and F for temperatures - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperatures + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Mode: " - << sensor->getMode() - << endl; + cout << "Mode: " << sensor.getMode() << endl; - cout << "Operating State: " - << sensor->getOperatingState() - << endl; + cout << "Operating State: " << sensor.getOperatingState() << endl; - cout << "Heating Point: " << sensor->getHeatingPointTemperature() - << " C / " << sensor->getHeatingPointTemperature(true) << " F" - << endl; + cout << "Heating Point: " << sensor.getHeatingPointTemperature() << " C / " + << sensor.getHeatingPointTemperature(true) << " F" << endl; - cout << "Cooling Point: " << sensor->getCoolingPointTemperature() - << " C / " << sensor->getCoolingPointTemperature(true) << " F" - << endl; + cout << "Cooling Point: " << sensor.getCoolingPointTemperature() << " C / " + << sensor.getCoolingPointTemperature(true) << " F" << endl; - cout << "Fan Mode: " - << sensor->getFanMode() - << endl; + cout << "Fan Mode: " << sensor.getFanMode() << endl; - cout << "Fan State: " - << sensor->getFanState() - << endl; + cout << "Fan State: " << sensor.getFanState() << endl; - cout << endl; + cout << endl; - sleep(5); + upm_delay(5); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; - -//! [Interesting] - return 0; + //! [Interesting] + return 0; } diff --git a/examples/c++/p9813.cxx b/examples/c++/p9813.cxx index f624cc05..1442c1bd 100644 --- a/examples/c++/p9813.cxx +++ b/examples/c++/p9813.cxx @@ -24,9 +24,6 @@ */ #include "p9813.hpp" -#include -#include -#include using namespace std; diff --git a/examples/c++/pca9685.cxx b/examples/c++/pca9685.cxx index 82f39864..ee85ec9d 100644 --- a/examples/c++/pca9685.cxx +++ b/examples/c++/pca9685.cxx @@ -22,57 +22,56 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "pca9685.hpp" +#include "upm_utilities.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an PCA9685 on I2C bus 0 + //! [Interesting] + // Instantiate an PCA9685 on I2C bus 0 - upm::PCA9685 *leds = new upm::PCA9685(PCA9685_I2C_BUS, - PCA9685_DEFAULT_I2C_ADDR); + upm::PCA9685 leds(PCA9685_I2C_BUS, PCA9685_DEFAULT_I2C_ADDR); - // put device to sleep - leds->setModeSleep(true); + // put device to upm_delay + leds.setModeSleep(true); - // setup a period of 50Hz - leds->setPrescaleFromHz(50); - - // wake device up - leds->setModeSleep(false); + // setup a period of 50Hz + leds.setPrescaleFromHz(50); - // Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2) - // Set for all channels + // wake device up + leds.setModeSleep(false); - leds->ledOnTime(PCA9685_ALL_LED, 0); - leds->ledOffTime(PCA9685_ALL_LED, 2048); + // Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2) + // Set for all channels - // but, turn channel 3 full off and channel 4 full on + leds.ledOnTime(PCA9685_ALL_LED, 0); + leds.ledOffTime(PCA9685_ALL_LED, 2048); - cout << "Turning channel 3 off, and channel 4 on." << endl; - cout << "All other channels will be PWM'd at a 50% duty cycle." << endl; + // but, turn channel 3 full off and channel 4 full on - leds->ledFullOff(3, true); - leds->ledFullOn(4, true); + cout << "Turning channel 3 off, and channel 4 on." << endl; + cout << "All other channels will be PWM'd at a 50% duty cycle." << endl; - // now, just sleep for 5 seconds, reset channels 3 and 4, and exit. - cout << "Sleeping for 5 seconds..." << endl; + leds.ledFullOff(3, true); + leds.ledFullOn(4, true); - sleep(5); + // now, just upm_delay for 5 seconds, reset channels 3 and 4, and exit. + cout << "Sleeping for 5 seconds..." << endl; - cout << "Exiting..." << endl; + upm_delay(5); - // clear the bits we set earlier - leds->ledFullOff(3, false); - leds->ledFullOn(4, false); + cout << "Exiting..." << endl; -//! [Interesting] + // clear the bits we set earlier + leds.ledFullOff(3, false); + leds.ledFullOn(4, false); - delete leds; - return 0; + //! [Interesting] + + return 0; } diff --git a/examples/c++/pn532-writeurl.cxx b/examples/c++/pn532-writeurl.cxx index 8e63f54d..6c15899a 100644 --- a/examples/c++/pn532-writeurl.cxx +++ b/examples/c++/pn532-writeurl.cxx @@ -22,8 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include +#include +#include + #include "pn532.hpp" using namespace std; @@ -32,84 +35,74 @@ using namespace std; // NOTE: this cannot exceed 34 characters. static char url[] = "iotdk.intel.com"; - -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the - // IRQ, and gpio 2 for the reset pin. + //! [Interesting] + // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the + // IRQ, and gpio 2 for the reset pin. - upm::PN532 *nfc = new upm::PN532(3, 2); + upm::PN532 nfc(3, 2); - if (!nfc->init()) - cerr << "init() failed" << endl; + if (!nfc.init()) + cerr << "init() failed" << endl; - uint32_t vers = nfc->getFirmwareVersion(); + uint32_t vers = nfc.getFirmwareVersion(); - if (vers) - printf("Got firmware version: 0x%08x\n", vers); - else - { - printf("Could not identify PN532\n"); - return 1; + if (vers) + printf("Got firmware version: 0x%08x\n", vers); + else { + printf("Could not identify PN532\n"); + return 1; } - // Now scan and identify any cards that come in range (1 for now) + // Now scan and identify any cards that come in range (1 for now) - // Retry forever - nfc->setPassiveActivationRetries(0xff); + // Retry forever + nfc.setPassiveActivationRetries(0xff); - nfc->SAMConfig(); + nfc.SAMConfig(); - uint8_t uidSize; - uint8_t uid[7]; + uint8_t uidSize; + uint8_t uid[7]; - bool foundCard = false; - while (!foundCard) - { - memset(uid, 0, 7); - if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A, - uid, &uidSize, 2000)) - { - // found a card - printf("Found a card: UID len %d\n", uidSize); - printf("UID: "); - for (int i = 0; i < uidSize; i++) - printf("%02x ", uid[i]); - printf("\n"); - printf("SAK: 0x%02x\n", nfc->getSAK()); - printf("ATQA: 0x%04x\n\n", nfc->getATQA()); - foundCard = true; - } - else - { - printf("Waiting for a card...\n"); + bool foundCard = false; + while (!foundCard) { + memset(uid, 0, 7); + if (nfc.readPassiveTargetID(nfc.BAUD_MIFARE_ISO14443A, uid, &uidSize, 2000)) { + // found a card + printf("Found a card: UID len %d\n", uidSize); + printf("UID: "); + for (int i = 0; i < uidSize; i++) + printf("%02x ", uid[i]); + printf("\n"); + printf("SAK: 0x%02x\n", nfc.getSAK()); + printf("ATQA: 0x%04x\n\n", nfc.getATQA()); + foundCard = true; + } else { + printf("Waiting for a card...\n"); } } - if (uidSize != 7) - { - printf("This example will only write an NDEF URI to preformatted\n"); - printf("Mifare Ultralight or NTAG2XX tags\n"); - - return 1; + if (uidSize != 7) { + printf("This example will only write an NDEF URI to preformatted\n"); + printf("Mifare Ultralight or NTAG2XX tags\n"); + + return 1; } - // 48 bytes is maximum data area on ultralight cards, so we use that - // as the maximum datasize here. Obviously if you have a bigger - // card, you can write more data. - if (!nfc->ntag2xx_WriteNDEFURI(nfc->NDEF_URIPREFIX_HTTP, url, 48)) - { - // failure - printf("Failed to write NDEF record tag.\n"); - return 1; + // 48 bytes is maximum data area on ultralight cards, so we use that + // as the maximum datasize here. Obviously if you have a bigger + // card, you can write more data. + if (!nfc.ntag2xx_WriteNDEFURI(nfc.NDEF_URIPREFIX_HTTP, url, 48)) { + // failure + printf("Failed to write NDEF record tag.\n"); + return 1; } - printf("Success, URL record written to tag.\n"); + printf("Success, URL record written to tag.\n"); + //! [Interesting] -//! [Interesting] - - delete nfc; - return 0; + return 0; } diff --git a/examples/c++/pn532.cxx b/examples/c++/pn532.cxx index ea66a028..b571b71a 100644 --- a/examples/c++/pn532.cxx +++ b/examples/c++/pn532.cxx @@ -22,80 +22,76 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include +#include +#include + #include "pn532.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the - // IRQ, and gpio 2 for the reset pin. + //! [Interesting] + // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the + // IRQ, and gpio 2 for the reset pin. - upm::PN532 *nfc = new upm::PN532(3, 2); + upm::PN532 nfc(3, 2); - if (!nfc->init()) - cerr << "init() failed" << endl; + if (!nfc.init()) + cerr << "init() failed" << endl; - uint32_t vers = nfc->getFirmwareVersion(); + uint32_t vers = nfc.getFirmwareVersion(); - if (vers) - printf("Got firmware version: 0x%08x\n", vers); - else - { - printf("Could not identify PN532\n"); - return 1; + if (vers) + printf("Got firmware version: 0x%08x\n", vers); + else { + printf("Could not identify PN532\n"); + return 1; } - // Now scan and identify any cards that come in range (1 for now) + // Now scan and identify any cards that come in range (1 for now) - // Retry forever - nfc->setPassiveActivationRetries(0xff); + // Retry forever + nfc.setPassiveActivationRetries(0xff); - nfc->SAMConfig(); + nfc.SAMConfig(); - uint8_t uidSize; - uint8_t uid[7]; + uint8_t uidSize; + uint8_t uid[7]; - while (shouldRun) - { - memset(uid, 0, 7); - if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A, - uid, &uidSize, 2000)) - { - // found a card - printf("Found a card: UID len %d\n", uidSize); - printf("UID: "); - for (int i = 0; i < uidSize; i++) - printf("%02x ", uid[i]); - printf("\n"); - printf("SAK: 0x%02x\n", nfc->getSAK()); - printf("ATQA: 0x%04x\n\n", nfc->getATQA()); - sleep(1); - } - else - { - printf("Waiting for a card...\n"); + while (shouldRun) { + memset(uid, 0, 7); + if (nfc.readPassiveTargetID(nfc.BAUD_MIFARE_ISO14443A, uid, &uidSize, 2000)) { + // found a card + printf("Found a card: UID len %d\n", uidSize); + printf("UID: "); + for (int i = 0; i < uidSize; i++) + printf("%02x ", uid[i]); + printf("\n"); + printf("SAK: 0x%02x\n", nfc.getSAK()); + printf("ATQA: 0x%04x\n\n", nfc.getATQA()); + upm_delay(1); + } else { + printf("Waiting for a card...\n"); } } + //! [Interesting] -//! [Interesting] - - delete nfc; - return 0; + return 0; } diff --git a/examples/c++/ppd42ns.cxx b/examples/c++/ppd42ns.cxx index 29446399..6ff0562a 100644 --- a/examples/c++/ppd42ns.cxx +++ b/examples/c++/ppd42ns.cxx @@ -22,45 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "ppd42ns.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main () +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a dust sensor on GPIO pin D8 - upm::PPD42NS* dust = new upm::PPD42NS(8); + upm::PPD42NS dust(8); ppd42ns_dust_data data; cout << "This program will give readings every 30 seconds until " - << "you stop it" - << endl; - while (shouldRun) - { - data = dust->getData(); + << "you stop it" << endl; + while (shouldRun) { + data = dust.getData(); cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl; cout << "Ratio: " << data.ratio << endl; cout << "Concentration: " << data.concentration << endl; cout << endl; } -//! [Interesting] + //! [Interesting] cout << "Exiting" << endl; - delete dust; return 0; } diff --git a/examples/c++/pulsensor.cxx b/examples/c++/pulsensor.cxx index 1a4ce456..668b73e3 100644 --- a/examples/c++/pulsensor.cxx +++ b/examples/c++/pulsensor.cxx @@ -22,10 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include "pulsensor.hpp" #include +#include + +#include "pulsensor.hpp" +#include "upm_utilities.h" using namespace upm; @@ -42,21 +43,22 @@ sig_handler(int signo) } void -handler (clbk_data data) { - printf ("callback data (%d)\n", data.is_heart_beat); +handler(clbk_data data) +{ + printf("callback data (%d)\n", data.is_heart_beat); } int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - Pulsensor *sensor = new Pulsensor(handler); + //! [Interesting] + Pulsensor sensor(handler); - sensor->start_sampler(); + sensor.start_sampler(); while (!doWork) { - usleep (5); + upm_delay_us(5); } - sensor->stop_sampler(); -//! [Interesting] + sensor.stop_sampler(); + //! [Interesting] return 0; } diff --git a/examples/c++/relay.cxx b/examples/c++/relay.cxx index a44d6a2b..53eef74b 100644 --- a/examples/c++/relay.cxx +++ b/examples/c++/relay.cxx @@ -22,37 +22,37 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "relay.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { // This example uses GPIO 0 -//! [Interesting] + //! [Interesting] // Create the relay switch object using GPIO pin 0 - upm::Relay* relay = new upm::Relay(0); + upm::Relay relay(0); // Close and then open the relay switch 3 times, // waiting one second each time. The LED on the relay switch // will light up when the switch is on (closed). // The switch will also make a noise between transitions. - for ( int i = 0; i < 3; i++ ) { - relay->on(); - if ( relay->isOn() ) - std::cout << relay->name() << " is on" << std::endl; - sleep(1); - relay->off(); - if ( relay->isOff() ) - std::cout << relay->name() << " is off" << std::endl; - sleep(1); + for (int i = 0; i < 3; i++) { + relay.on(); + if (relay.isOn()) + std::cout << relay.name() << " is on" << std::endl; + upm_delay(1); + relay.off(); + if (relay.isOff()) + std::cout << relay.name() << " is off" << std::endl; + upm_delay(1); } // Delete the relay switch object - delete relay; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/rf22-client.cxx b/examples/c++/rf22-client.cxx index e0372d85..4de808b6 100644 --- a/examples/c++/rf22-client.cxx +++ b/examples/c++/rf22-client.cxx @@ -24,19 +24,23 @@ #include #include +#include + #include "rf22.hpp" bool amWorking = true; void -sig_handler (int signo) { +sig_handler(int signo) +{ if (signo == SIGINT) { amWorking = false; } } //! [Interesting] int -main (int argc, char ** argv) { +main(int argc, char** argv) +{ // SPI bus 0, CS pin 10, INTR pin 2 upm::RF22 rf22(0, 10, 2); @@ -49,18 +53,18 @@ main (int argc, char ** argv) { uint8_t buf[RF22_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); - signal (SIGINT, sig_handler); + signal(SIGINT, sig_handler); while (amWorking) { std::cout << "Sending to rf22_server" << std::endl; // Send a message to rf22_server rf22.send(data, sizeof(data)); rf22.waitPacketSent(); // Now wait for a reply - + rf22.waitAvailableTimeout(1000000); - + if (rf22.recv(buf, &len)) { - std::cout << "got response: " << (char*)buf << std::endl; + std::cout << "got response: " << (char*) buf << std::endl; } else { std::cout << "!!! NO RESPONSE !!!" << std::endl; } diff --git a/examples/c++/rf22-server.cxx b/examples/c++/rf22-server.cxx index d28e50d2..f05c1af1 100644 --- a/examples/c++/rf22-server.cxx +++ b/examples/c++/rf22-server.cxx @@ -24,19 +24,23 @@ #include #include +#include + #include "rf22.hpp" bool amWorking = true; void -sig_handler (int signo) { +sig_handler(int signo) +{ if (signo == SIGINT) { amWorking = false; } } //! [Interesting] int -main (int argc, char ** argv) { +main(int argc, char** argv) +{ // SPI bus 0, CS pin 10, INTR pin 2 upm::RF22 rf22(0, 10, 2); @@ -44,18 +48,18 @@ main (int argc, char ** argv) { std::cout << "RF22 init failed" << std::endl; return 0x1; } - + uint8_t buf[RF22_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); - - signal (SIGINT, sig_handler); + + signal(SIGINT, sig_handler); while (amWorking) { // rf22.waitAvailable(); - rf22.waitAvailableTimeout (500); + rf22.waitAvailableTimeout(500); - // Should be a message for us now + // Should be a message for us now if (rf22.recv(buf, &len)) { - std::cout << "got request: " << (char*)buf << std::endl; + std::cout << "got request: " << (char*) buf << std::endl; // Send a reply uint8_t data[] = "And hello back to you"; diff --git a/examples/c++/rfr359f.cxx b/examples/c++/rfr359f.cxx index 81b2d483..2d8863d4 100644 --- a/examples/c++/rfr359f.cxx +++ b/examples/c++/rfr359f.cxx @@ -22,46 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "rfr359f.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate an RFR359F digital pin D2 - // This was tested on the Grove IR Distance Interrupter + //! [Interesting] + // Instantiate an RFR359F digital pin D2 + // This was tested on the Grove IR Distance Interrupter - upm::RFR359F* dInterruptor = new upm::RFR359F(2); - - // The range appears to be about 4 inches, depending on adjustment - while (shouldRun) - { - if (dInterruptor->objectDetected()) - cout << "Object detected" << endl; - else - cout << "Area is clear" << endl; + upm::RFR359F dInterruptor(2); - usleep(100000); // 100ms + // The range appears to be about 4 inches, depending on adjustment + while (shouldRun) { + if (dInterruptor.objectDetected()) + cout << "Object detected" << endl; + else + cout << "Area is clear" << endl; + + upm_delay_us(100000); // 100ms } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete dInterruptor; - return 0; + return 0; } diff --git a/examples/c++/rgbringcoder.cxx b/examples/c++/rgbringcoder.cxx index c648bbad..9d74427e 100644 --- a/examples/c++/rgbringcoder.cxx +++ b/examples/c++/rgbringcoder.cxx @@ -22,89 +22,83 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "rgbringcoder.hpp" #include +#include "rgbringcoder.hpp" +#include "upm_utilities.h" + using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); - //! [Interesting] - - // There are a lot of pins to hook up. These pins are valid for the - // Edison board, but may need to be adjusted for other platforms. + //! [Interesting] - // In order: - // enable - 4 - // latch - 10 - // clear - 11 - // clock - 2 - // data - 9 - // switch - 7 + // There are a lot of pins to hook up. These pins are valid for the + // Edison board, but may need to be adjusted for other platforms. - // red pwm - 3 - // green pwm - 5 - // blue pwm - 6 + // In order: + // enable - 4 + // latch - 10 + // clear - 11 + // clock - 2 + // data - 9 + // switch - 7 - // encA - 12 - // encB - 13 + // red pwm - 3 + // green pwm - 5 + // blue pwm - 6 - upm::RGBRingCoder *ringCoder = - new upm::RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 5, 6); + // encA - 12 + // encB - 13 - uint16_t spin = 0x0001; - bool oldState = false; - int oldPos = 0; + upm::RGBRingCoder ringCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 5, 6); - // Lets go green - ringCoder->setRGBLED(0.99, 0.01, 0.99); + uint16_t spin = 0x0001; + bool oldState = false; + int oldPos = 0; - while (shouldRun) - { - // you spin me round... - if (spin == 0) - spin = 0x0001; + // Lets go green + ringCoder.setRGBLED(0.99, 0.01, 0.99); - ringCoder->setRingLEDS(spin); - spin <<= 1; + while (shouldRun) { + // you spin me round... + if (spin == 0) + spin = 0x0001; - // check button state - bool bstate = ringCoder->getButtonState(); - if (bstate != oldState) - { - cout << "Button state changed from " << oldState << " to " - << bstate << endl; - oldState = bstate; + ringCoder.setRingLEDS(spin); + spin <<= 1; + + // check button state + bool bstate = ringCoder.getButtonState(); + if (bstate != oldState) { + cout << "Button state changed from " << oldState << " to " << bstate << endl; + oldState = bstate; } - // check encoder position - int epos = ringCoder->getEncoderPosition(); - if (epos != oldPos) - { - cout << "Encoder position changed from " << oldPos << " to " - << epos << endl; - oldPos = epos; + // check encoder position + int epos = ringCoder.getEncoderPosition(); + if (epos != oldPos) { + cout << "Encoder position changed from " << oldPos << " to " << epos << endl; + oldPos = epos; } - usleep(100000); + upm_delay_us(100000); } - - //! [Interesting] - - delete ringCoder; - - return 0; + + //! [Interesting] + + return 0; } diff --git a/examples/c++/rhusb.cxx b/examples/c++/rhusb.cxx index 0b14aaa9..88955e5a 100644 --- a/examples/c++/rhusb.cxx +++ b/examples/c++/rhusb.cxx @@ -22,67 +22,66 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "rhusb.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an RHUSB instance on defaultDev - upm::RHUSB sensor(defaultDev); + // Instantiate an RHUSB instance on defaultDev + upm::RHUSB sensor(defaultDev); - // output the firmware ID - cout << "Firmware ID: " << sensor.getFirmwareID() << endl; - cout << endl; + // output the firmware ID + cout << "Firmware ID: " << sensor.getFirmwareID() << endl; + cout << endl; - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor.update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor.getTemperature() - << " C / " << sensor.getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor.getHumidity() - << " %" << endl; + cout << "Humidity: " << sensor.getHumidity() << " %" << endl; - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } + cout << "Exiting..." << endl; - cout << "Exiting..." << endl; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/rn2903-p2p-rx.cxx b/examples/c++/rn2903-p2p-rx.cxx index dd84c552..0e0f2903 100644 --- a/examples/c++/rn2903-p2p-rx.cxx +++ b/examples/c++/rn2903-p2p-rx.cxx @@ -22,30 +22,29 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include #include +#include #include "rn2903.hpp" -#include "upm_utilities.h" +#include "rn2903_defs.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] string defaultDev = "/dev/ttyUSB0"; if (argc > 1) @@ -54,7 +53,7 @@ int main(int argc, char **argv) cout << "Using device: " << defaultDev << endl; // Instantiate a RN2903 sensor on defaultDev at 57600 baud. - upm::RN2903 sensor (defaultDev, RN2903_DEFAULT_BAUDRATE); + upm::RN2903 sensor(defaultDev, RN2903_DEFAULT_BAUDRATE); // To use an internal UART understood by MRAA, use the following // to inititialize rather than the above, which by default uses a @@ -66,8 +65,7 @@ int main(int argc, char **argv) // sensor.setDebug(true); // get version - if (sensor.command("sys get ver")) - { + if (sensor.command("sys get ver")) { cout << "Failed to retrieve device version string" << endl; return 1; } @@ -88,26 +86,19 @@ int main(int argc, char **argv) // radio watch dog timer will expire every 15 seconds. We will // just loop here. - while (shouldRun) - { + while (shouldRun) { cout << "Waiting for packet..." << endl; RN2903_RESPONSE_T rv; rv = sensor.radioRx(0); - if (rv) - { + if (rv) { cout << "radioRx() failed with code " << int(rv) << endl; - } - else - { + } else { string resp = sensor.getResponse(); string payload = sensor.getRadioRxPayload(); if (!payload.size()) cout << "Got response: '" << resp << "'" << endl; else - cout <<"Got payload: '" - << sensor.fromHex(payload) - << "'" - << endl; + cout << "Got payload: '" << sensor.fromHex(payload) << "'" << endl; } cout << endl; @@ -115,7 +106,7 @@ int main(int argc, char **argv) cout << "Exiting" << endl; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/rn2903-p2p-tx.cxx b/examples/c++/rn2903-p2p-tx.cxx index be4d1fbf..b7ffbfd9 100644 --- a/examples/c++/rn2903-p2p-tx.cxx +++ b/examples/c++/rn2903-p2p-tx.cxx @@ -22,30 +22,31 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include #include #include -#include #include "rn2903.hpp" +#include "rn2903_defs.h" #include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] string defaultDev = "/dev/ttyUSB0"; if (argc > 1) @@ -66,8 +67,7 @@ int main(int argc, char **argv) // sensor.setDebug(true); // get version - if (sensor.command("sys get ver")) - { + if (sensor.command("sys get ver")) { cout << "Failed to retrieve device version string" << endl; return 1; } @@ -90,19 +90,14 @@ int main(int argc, char **argv) // frequently. int count = 0; - while (shouldRun) - { - ostringstream output; + while (shouldRun) { + std::ostringstream output; output << "Ping " << count++; // All payloads must be hex encoded string payload = sensor.toHex(output.str()); - cout << "Transmitting a packet, data: '" - << output.str() - << "' -> hex: '" - << payload - << "'" + cout << "Transmitting a packet, data: '" << output.str() << "' -> hex: '" << payload << "'" << endl; RN2903_RESPONSE_T rv; @@ -119,7 +114,7 @@ int main(int argc, char **argv) cout << "Exiting" << endl; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/rn2903.cxx b/examples/c++/rn2903.cxx index 84c47212..086bc6a5 100644 --- a/examples/c++/rn2903.cxx +++ b/examples/c++/rn2903.cxx @@ -22,18 +22,18 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "rn2903.hpp" -#include "upm_utilities.h" +#include "rn2903_defs.h" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] string defaultDev = "/dev/ttyUSB0"; if (argc > 1) @@ -54,8 +54,7 @@ int main(int argc, char **argv) // sensor.setDebug(true); // get version - if (sensor.command("sys get ver")) - { + if (sensor.command("sys get ver")) { cout << "Failed to retrieve device version string" << endl; return 1; } @@ -92,8 +91,7 @@ int main(int argc, char **argv) sensor.setApplicationSessionKey("00112233001122330011223300112233"); RN2903_JOIN_STATUS_T rv = sensor.join(RN2903_JOIN_TYPE_ABP); - if (rv == RN2903_JOIN_STATUS_ACCEPTED) - { + if (rv == RN2903_JOIN_STATUS_ACCEPTED) { cout << "Join successful." << endl; // All transmit payloads must be hex encoded strings, so @@ -109,28 +107,21 @@ int main(int argc, char **argv) if (trv == RN2903_MAC_TX_STATUS_TX_OK) cout << "Transmit successful." << endl; - else - { + else { // check to see if we got a downlink packet - if (trv == RN2903_MAC_TX_STATUS_RX_RECEIVED) - { - cout << "Transmit successful, downlink packet received: " - << sensor.getResponse(); - } - else - { + if (trv == RN2903_MAC_TX_STATUS_RX_RECEIVED) { + cout << "Transmit successful, downlink packet received: " << sensor.getResponse(); + } else { cout << "Transmit failed with code " << int(trv) << endl; } } - } - else - { + } else { cout << "Join failed with code " << int(rv) << endl; } cout << "Exiting" << endl; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/rotary.cxx b/examples/c++/rotary.cxx index 2d92982d..417fc07a 100644 --- a/examples/c++/rotary.cxx +++ b/examples/c++/rotary.cxx @@ -22,36 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include +#include + #include "rotary.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { -//! [Interesting] + //! [Interesting] // Instantiate a rotary sensor on analog pin A0 - upm::Rotary* knob = new upm::Rotary(0); + upm::Rotary knob(0); // Print sensor name to confirm it initialized properly - cout << knob->name() << endl; + cout << knob.name() << endl; - while(true) { - float abs_value = knob->abs_value(); // Absolute raw value - float abs_deg = knob->abs_deg(); // Absolute degrees - float abs_rad = knob->abs_rad(); // Absolute radians - float rel_value = knob->rel_value(); // Relative raw value - float rel_deg = knob->rel_deg(); // Relative degrees - float rel_rad = knob->rel_rad(); // Relative radians + while (true) { + float abs_value = knob.abs_value(); // Absolute raw value + float abs_deg = knob.abs_deg(); // Absolute degrees + float abs_rad = knob.abs_rad(); // Absolute radians + float rel_value = knob.rel_value(); // Relative raw value + float rel_deg = knob.rel_deg(); // Relative degrees + 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", - (int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg, rel_rad); + fprintf(stdout, + "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] - delete knob; + //! [Interesting] return 0; } diff --git a/examples/c++/rotaryencoder.cxx b/examples/c++/rotaryencoder.cxx index e211f032..3f56666d 100644 --- a/examples/c++/rotaryencoder.cxx +++ b/examples/c++/rotaryencoder.cxx @@ -22,40 +22,40 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "rotaryencoder.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Rotary Encoder, using signal pins D2 and D3 - upm::RotaryEncoder* rotaryencoder = new upm::RotaryEncoder(2, 3); - - while (shouldRun) - { - cout << "Position: " << rotaryencoder->position() << endl; - usleep(100000); + //! [Interesting] + // Instantiate a Grove Rotary Encoder, using signal pins D2 and D3 + upm::RotaryEncoder rotaryencoder(2, 3); + + while (shouldRun) { + cout << "Position: " << rotaryencoder.position() << endl; + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete rotaryencoder; - return 0; + return 0; } diff --git a/examples/c++/rpr220-intr.cxx b/examples/c++/rpr220-intr.cxx index 401640c4..79c499c2 100644 --- a/examples/c++/rpr220-intr.cxx +++ b/examples/c++/rpr220-intr.cxx @@ -22,57 +22,59 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "rpr220.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } volatile unsigned int counter = 0; // Our interrupt handler -void rprISR(void *arg) +void +rprISR(void* arg) { - counter++; + counter++; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // This example uses an interrupt handler to increment a counter - - // Instantiate an RPR220 digital pin D2 - // This was tested on the Grove IR Reflective Sensor + //! [Interesting] + // This example uses an interrupt handler to increment a counter - upm::RPR220* rpr220 = new upm::RPR220(2); - - // Here, we setup our Interupt Service Routine (ISR) to count - // 'black' pulses detected. + // Instantiate an RPR220 digital pin D2 + // This was tested on the Grove IR Reflective Sensor - rpr220->installISR(rprISR, NULL); + upm::RPR220 rpr220(2); - while (shouldRun) - { - cout << "Counter: " << counter << endl; + // Here, we setup our Interupt Service Routine (ISR) to count + // 'black' pulses detected. - sleep(1); + rpr220.installISR(rprISR, NULL); + + while (shouldRun) { + cout << "Counter: " << counter << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete rpr220; - return 0; + return 0; } diff --git a/examples/c++/rpr220.cxx b/examples/c++/rpr220.cxx index dffffc8e..1912d728 100644 --- a/examples/c++/rpr220.cxx +++ b/examples/c++/rpr220.cxx @@ -22,47 +22,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "rpr220.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // This example uses a simple method to determine current status + //! [Interesting] + // This example uses a simple method to determine current status - // Instantiate an RPR220 digital pin D2 - // This was tested on the Grove IR Reflective Sensor + // Instantiate an RPR220 digital pin D2 + // This was tested on the Grove IR Reflective Sensor - upm::RPR220* rpr220 = new upm::RPR220(2); - - while (shouldRun) - { - if (rpr220->blackDetected()) - cout << "Black detected" << endl; - else - cout << "Black NOT detected" << endl; + upm::RPR220 rpr220(2); - usleep(100000); // 100ms + while (shouldRun) { + if (rpr220.blackDetected()) + cout << "Black detected" << endl; + else + cout << "Black NOT detected" << endl; + + upm_delay_us(100000); // 100ms } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete rpr220; - return 0; + return 0; } diff --git a/examples/c++/rsc.cxx b/examples/c++/rsc.cxx index 3b1fead7..152e70b3 100644 --- a/examples/c++/rsc.cxx +++ b/examples/c++/rsc.cxx @@ -22,43 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "rsc.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main () +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] - - upm::RSC* rsc = new upm::RSC(0, 9, 8); + //! [Interesting] - cout << "Sensor Name: " << rsc->getSensorName() << endl; - rsc->setMode(NORMAL_MODE); - rsc->setDataRate(N_DR_330_SPS); + upm::RSC rsc(0, 9, 8); + + cout << "Sensor Name: " << rsc.getSensorName() << endl; + rsc.setMode(NORMAL_MODE); + rsc.setDataRate(N_DR_330_SPS); while (shouldRun) { - cout << "inH2O pressure: " << rsc->getPressure() << endl; - cout << "Temp (C): " << rsc->getTemperature() << endl; + cout << "inH2O pressure: " << rsc.getPressure() << endl; + cout << "Temp (C): " << rsc.getTemperature() << endl; - sleep(1); + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; - delete rsc; return 0; } diff --git a/examples/c++/scam.cxx b/examples/c++/scam.cxx index a7afc73a..d088a1f1 100644 --- a/examples/c++/scam.cxx +++ b/examples/c++/scam.cxx @@ -22,56 +22,52 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "scam.hpp" using namespace std; using namespace upm; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - // Instantiate a Serial Camera on UART 0 - upm::SCAM* camera = new upm::SCAM(0); + // Instantiate a Serial Camera on UART 0 + upm::SCAM camera(0); - // make sure port is initialized properly. 115200 baud is the default. - if (!camera->setupTty()) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 115200 baud is the default. + if (!camera.setupTty()) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - if (camera->init()) - cout << "Initialized..." << endl; - else - cout << "init() failed" << endl; + if (camera.init()) + cout << "Initialized..." << endl; + else + cout << "init() failed" << endl; - if (camera->preCapture()) - cout << "preCapture succeeded..." << endl; - else - cout << "preCapture failed." << endl; + if (camera.preCapture()) + cout << "preCapture succeeded..." << endl; + else + cout << "preCapture failed." << endl; - if (camera->doCapture()) - cout << "doCapture succeeded..." << endl; - else - cout << "doCapture failed." << endl; + if (camera.doCapture()) + cout << "doCapture succeeded..." << endl; + else + cout << "doCapture failed." << endl; - cout << "Image size is " << camera->getImageSize() << " bytes" << endl; + cout << "Image size is " << camera.getImageSize() << " bytes" << endl; - if (camera->getImageSize() > 0) - { - cout << "Storing image.jpg..." << endl; - if (camera->storeImage("image.jpg")) - cout << "storeImage succeeded..." << endl; - else - cout << "storeImage failed." << endl; + if (camera.getImageSize() > 0) { + cout << "Storing image.jpg..." << endl; + if (camera.storeImage("image.jpg")) + cout << "storeImage succeeded..." << endl; + else + cout << "storeImage failed." << endl; } -//! [Interesting] + //! [Interesting] - delete camera; - return 0; + return 0; } diff --git a/examples/c++/sensortemplate.cxx b/examples/c++/sensortemplate.cxx index 8d1ad221..15e5ccf6 100644 --- a/examples/c++/sensortemplate.cxx +++ b/examples/c++/sensortemplate.cxx @@ -4,43 +4,49 @@ * Author: Your Full Name * Copyright (c) * - * Permission is hereby granted, free of charge, to any person obtaining a copy of + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of + * the Software, and to permit persons to whom the Software is furnished to do + * so, * subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all + * The above copyright notice and this permission notice shall be included in + * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + * OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - #include -#include +#include #include "sensortemplate.hpp" +#include "upm_utilities.h" -int main () +int +main() { //! [Interesting] // Create an instance of SensorTemplate upm::SensorTemplate sensor(0); - while(true) - { - std::cout << "SensorTemplate says: " - << sensor.helloWorld() << std::endl; + while (true) { + std::cout << "SensorTemplate says: " << sensor.helloWorld() << std::endl; // Repeat every 2 seconds - usleep(2000000); + upm_delay_us(2000000); } //! [Interesting] return 0; diff --git a/examples/c++/servo-es08a.cxx b/examples/c++/servo-es08a.cxx index c4800128..6790759f 100644 --- a/examples/c++/servo-es08a.cxx +++ b/examples/c++/servo-es08a.cxx @@ -22,40 +22,37 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "es08a.hpp" -#include -#include +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] - upm::ES08A *servo = new upm::ES08A(5); + upm::ES08A servo(5); // Sets the shaft to 180, then to 90, then to 0, // then back to 90, and finally back to 180, // pausing for a second in between each angle - servo->setAngle (180); + servo.setAngle(180); std::cout << "Set angle to 180" << std::endl; - sleep(1); - servo->setAngle (90); + upm_delay(1); + servo.setAngle(90); std::cout << "Set angle to 90" << std::endl; - sleep(1); - servo->setAngle (0); + upm_delay(1); + servo.setAngle(0); std::cout << "Set angle to 0" << std::endl; - sleep(1); - servo->setAngle (90); + upm_delay(1); + servo.setAngle(90); std::cout << "Set angle to 90" << std::endl; - sleep(1); - servo->setAngle (180); + upm_delay(1); + servo.setAngle(180); std::cout << "Set angle to 180" << std::endl; //! [Interesting] std::cout << "exiting application" << std::endl; - delete servo; - return 0; } diff --git a/examples/c++/sht1x.cxx b/examples/c++/sht1x.cxx index 324f9ab4..35982bc7 100644 --- a/examples/c++/sht1x.cxx +++ b/examples/c++/sht1x.cxx @@ -22,56 +22,50 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "sht1x.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a SHT1X sensor using D2 as the clock, and D3 as the - // data pin. - upm::SHT1X *sensor = new upm::SHT1X(2, 3); + // Instantiate a SHT1X sensor using D2 as the clock, and D3 as the + // data pin. + upm::SHT1X sensor(2, 3); - // Every 2 seconds, update and print values - while (shouldRun) - { - sensor->update(); + // Every 2 seconds, update and print values + while (shouldRun) { + sensor.update(); - cout << "Temperature: " - << sensor->getTemperature() - << " C" - << endl; + cout << "Temperature: " << sensor.getTemperature() << " C" << endl; - cout << "Humidity: " - << sensor->getHumidity() - << " RH" - << endl; + cout << "Humidity: " << sensor.getHumidity() << " RH" << endl; - cout << endl; + cout << endl; - sleep(2); + upm_delay(2); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/si1132.cxx b/examples/c++/si1132.cxx index ab4062c7..4cd47710 100644 --- a/examples/c++/si1132.cxx +++ b/examples/c++/si1132.cxx @@ -22,20 +22,23 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + +#include "mraa/common.h" #include "si1132.hpp" +#include "upm_utilities.h" #define FT4222_I2C_BUS 1 -int main () +int +main() { //! [Interesting] upm::SI1132 lightSensor(mraa_get_sub_platform_id(FT4222_I2C_BUS)); while (true) { float value = lightSensor.getVisibleLux(); std::cout << "Light level = " << value << " lux" << std::endl; - sleep(1); + upm_delay(1); } //! [Interesting] return 0; diff --git a/examples/c++/si114x.cxx b/examples/c++/si114x.cxx index cf9d1689..bb2e5975 100644 --- a/examples/c++/si114x.cxx +++ b/examples/c++/si114x.cxx @@ -22,57 +22,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "si114x.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a SI114x UV Sensor on I2C bus 0 - upm::SI114X* uvi = new upm::SI114X(0); - - // First initialize it - uvi->initialize(); + //! [Interesting] + // Instantiate a SI114x UV Sensor on I2C bus 0 + upm::SI114X uvi(0); - cout << "UV Index Scale:" << endl; - cout << "---------------" << endl; - cout << "11+ Extreme" << endl; - cout << "8-10 Very High" << endl; - cout << "6-7 High" << endl; - cout << "3-5 Moderate" << endl; - cout << "0-2 Low" << endl; - cout << endl; + // First initialize it + uvi.initialize(); - // update every second and print the currently measured UV Index - while (shouldRun) - { - // update current value(s) - uvi->update(); + cout << "UV Index Scale:" << endl; + cout << "---------------" << endl; + cout << "11+ Extreme" << endl; + cout << "8-10 Very High" << endl; + cout << "6-7 High" << endl; + cout << "3-5 Moderate" << endl; + cout << "0-2 Low" << endl; + cout << endl; - // print detected value - cout << "UV Index: " << uvi->getUVIndex() << endl; + // update every second and print the currently measured UV Index + while (shouldRun) { + // update current value(s) + uvi.update(); - sleep(1); + // print detected value + cout << "UV Index: " << uvi.getUVIndex() << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete uvi; - return 0; + return 0; } diff --git a/examples/c++/si7005.cxx b/examples/c++/si7005.cxx index ff51f3e6..82b0ec87 100644 --- a/examples/c++/si7005.cxx +++ b/examples/c++/si7005.cxx @@ -22,30 +22,31 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include + #include "si7005.hpp" +#include "upm_utilities.h" #define EDISON_I2C_BUS 1 #define EDISON_GPIO_SI7005_CS 20 - //! [Interesting] -int main () +int +main() { - try { - upm::SI7005* sensor = new upm::SI7005(EDISON_I2C_BUS, EDISON_GPIO_SI7005_CS); - while (true) { - int temperature = sensor->getTemperatureCelsius(); - int humidity = sensor->getHumidityRelative(); - std::cout << "Temperature = " << temperature << "C" << std::endl; - std::cout << "Humidity = " << humidity << "%" << std::endl; - sleep(1); - } - delete sensor; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - return 0; + try { + upm::SI7005 sensor(EDISON_I2C_BUS, EDISON_GPIO_SI7005_CS); + while (true) { + int temperature = sensor.getTemperatureCelsius(); + int humidity = sensor.getHumidityRelative(); + std::cout << "Temperature = " << temperature << "C" << std::endl; + std::cout << "Humidity = " << humidity << "%" << std::endl; + upm_delay(1); + } + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + return 0; } //! [Interesting] diff --git a/examples/c++/slide.cxx b/examples/c++/slide.cxx index ba8ebf5a..6593f9a4 100644 --- a/examples/c++/slide.cxx +++ b/examples/c++/slide.cxx @@ -22,29 +22,30 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include #include -#include +#include +#include + #include "slide.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { -//! [Interesting] - upm::Slide* slide = new upm::Slide(0); // Instantiate new grove slide potentiometer on analog pin A0 + //! [Interesting] + upm::Slide slide(0); - cout << slide->name() << endl; + cout << slide.name() << endl; - while(true) { - float adc_value = slide->raw_value(); // Read raw value - float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V - fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts); + while (true) { + float adc_value = slide.raw_value(); // Read raw value + float volts = slide.voltage_value(); // Read voltage, board reference set at 5.0V + 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] - delete slide; + //! [Interesting] return 0; } diff --git a/examples/c++/sm130.cxx b/examples/c++/sm130.cxx index 27127901..fcfcb733 100644 --- a/examples/c++/sm130.cxx +++ b/examples/c++/sm130.cxx @@ -22,53 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include -#include -#include + #include "sm130.hpp" using namespace std; using namespace upm; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - // Instantiate a UART based SM130 RFID Module using defaults - upm::SM130* sensor = new upm::SM130(); + // Instantiate a UART based SM130 RFID Module using defaults + upm::SM130 sensor; - // set the baud rate. 19200 baud is the default. - if (sensor->setBaudRate(19200)) - { - cerr << "Failed to set baud rate" << endl; - return 1; + // set the baud rate. 19200 baud is the default. + if (sensor.setBaudRate(19200)) { + cerr << "Failed to set baud rate" << endl; + return 1; } - cout << "Resetting..." << endl; - sensor->reset(); + cout << "Resetting..." << endl; + sensor.reset(); - cout << "Firmware revision: " << sensor->getFirmwareVersion() << endl; + cout << "Firmware revision: " << sensor.getFirmwareVersion() << endl; - cout << "Waiting up to 5 seconds for a tag..." << endl; + cout << "Waiting up to 5 seconds for a tag..." << endl; - if (sensor->waitForTag(5000)) - { - cout << "Found tag, UID: " - << sensor->string2HexString(sensor->getUID()) << endl; - cout << "Tag Type: " << sensor->tag2String(sensor->getTagType()) - << endl; - } - else - { - // error - cout << "waitForTag failed: " << sensor->getLastErrorString() << endl; + if (sensor.waitForTag(5000)) { + cout << "Found tag, UID: " << sensor.string2HexString(sensor.getUID()) << endl; + cout << "Tag Type: " << sensor.tag2String(sensor.getTagType()) << endl; + } else { + // error + cout << "waitForTag failed: " << sensor.getLastErrorString() << endl; } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; - delete sensor; - return 0; + cout << "Exiting" << endl; + return 0; } diff --git a/examples/c++/smartdrive.cxx b/examples/c++/smartdrive.cxx index 9b2c2a86..0705ab0b 100644 --- a/examples/c++/smartdrive.cxx +++ b/examples/c++/smartdrive.cxx @@ -4,30 +4,37 @@ * Author: Oussema Harbi * Copyright (c) <2016> * - * Permission is hereby granted, free of charge, to any person obtaining a copy of + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of + * the Software, and to permit persons to whom the Software is furnished to do + * so, * subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all + * The above copyright notice and this permission notice shall be included in + * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS + * OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "smartdrive.hpp" #include +#include -upm::SmartDrive *drive = NULL; +#include "smartdrive.hpp" +#include "upm_utilities.h" void sig_handler(int signo) @@ -35,39 +42,39 @@ sig_handler(int signo) printf("got signal\n"); if (signo == SIGINT) { printf("exiting application\n"); - if (drive != NULL) - delete drive; - exit (0); } } int -main(int argc, char **argv) +main(int argc, char** argv) { - std::cout << "SmartDrive demo is starting. Please make sure drive is connected to board" << std::endl; - sleep(2); //Wait for 2 seconds in case you want to fix your h/w setup - + std::cout << "SmartDrive demo is starting. Please make sure drive is " + "connected to board" + << std::endl; + upm_delay(2); // Wait for 2 seconds in case you want to fix your h/w setup + //! [Interesting] // Instantiate a SmartDrive connected to /dev/i2c-0 bus, using DefaultAddress - drive = new upm::SmartDrive(0); + upm::SmartDrive drive(0); - std::cout << "Battery Voltage before motor run : " << drive->GetBattVoltage() << std::endl; - //Set motor M1 to run for 120 seconds, with speed of 15RPM, waith for it to finish and then Brake It - drive->Run_Seconds(SmartDrive_Motor_ID_1, SmartDrive_Dir_Forward, 15, 120, true, SmartDrive_Action_Brake); - std::cout << "Battery Voltage after motor run : " << drive->GetBattVoltage() << std::endl; - //Rotate motor M2 2270 degrees, in reverse sense, with speed of 10RPM, return immediately from function call - drive->Run_Degrees(SmartDrive_Motor_ID_2, SmartDrive_Dir_Reverse, 10, 2270, false, SmartDrive_Action_Float); - //While motor is running, Display its status - drive->PrintMotorStatus(SmartDrive_Motor_ID_2); - sleep(2); //Sleep for 2 seconds - //Stop motor M2 and then finish program - drive->StopMotor(SmartDrive_Motor_ID_2, SmartDrive_Action_BrakeHold); + std::cout << "Battery Voltage before motor run : " << drive.GetBattVoltage() << std::endl; + // Set motor M1 to run for 120 seconds, with speed of 15RPM, waith for it to + // finish and then Brake It + drive.Run_Seconds( + SmartDrive_Motor_ID_1, SmartDrive_Dir_Forward, 15, 120, true, SmartDrive_Action_Brake); + std::cout << "Battery Voltage after motor run : " << drive.GetBattVoltage() << std::endl; + // Rotate motor M2 2270 degrees, in reverse sense, with speed of 10RPM, return + // immediately from function call + drive.Run_Degrees( + SmartDrive_Motor_ID_2, SmartDrive_Dir_Reverse, 10, 2270, false, SmartDrive_Action_Float); + // While motor is running, Display its status + drive.PrintMotorStatus(SmartDrive_Motor_ID_2); + upm_delay(2); // Sleep for 2 seconds + // Stop motor M2 and then finish program + drive.StopMotor(SmartDrive_Motor_ID_2, SmartDrive_Action_BrakeHold); //! [Interesting] std::cout << "Demo complete. GoodBye" << std::endl; - delete drive; - drive = NULL; - return 0; } diff --git a/examples/c++/speaker-pwm.cxx b/examples/c++/speaker-pwm.cxx index 95236890..f4e9d015 100644 --- a/examples/c++/speaker-pwm.cxx +++ b/examples/c++/speaker-pwm.cxx @@ -22,34 +22,33 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "speaker.hpp" using namespace std; -int main () +int +main() { -//! [Interesting] + //! [Interesting] // Instantiate a Speaker (PWM) pin D3 in PWM mode - upm::Speaker* speaker = new upm::Speaker(3, true); + upm::Speaker speaker(3, true); // emit a series of frequencies for 500ms each - speaker->emit(50, 500); - speaker->emit(75, 500); - speaker->emit(125, 500); - speaker->emit(250, 500); - speaker->emit(500, 500); - speaker->emit(1000, 500); - speaker->emit(2000, 500); - speaker->emit(3000, 500); - speaker->emit(5000, 500); - speaker->emit(10000, 500); + speaker.emit(50, 500); + speaker.emit(75, 500); + speaker.emit(125, 500); + speaker.emit(250, 500); + speaker.emit(500, 500); + speaker.emit(1000, 500); + speaker.emit(2000, 500); + speaker.emit(3000, 500); + speaker.emit(5000, 500); + speaker.emit(10000, 500); cout << "Exiting" << endl; - delete speaker; - -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/speaker.cxx b/examples/c++/speaker.cxx index 0fad75c4..f30b309f 100644 --- a/examples/c++/speaker.cxx +++ b/examples/c++/speaker.cxx @@ -22,28 +22,27 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include + #include "speaker.hpp" using namespace std; -int main () +int +main() { -//! [Interesting] - // Instantiate a Speaker on digital pin D2 - upm::Speaker* speaker = new upm::Speaker(2); + //! [Interesting] + // Instantiate a Speaker on digital pin D2 + upm::Speaker speaker(2); - // Play all 7 of the lowest notes - speaker->playAll(); + // Play all 7 of the lowest notes + speaker.playAll(); - // Play a medium C-sharp - speaker->playSound('c', true, "med"); -//! [Interesting] + // Play a medium C-sharp + speaker.playSound('c', true, "med"); + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete speaker; - return 0; + return 0; } diff --git a/examples/c++/ssd1351.cxx b/examples/c++/ssd1351.cxx index dec565d2..06851259 100644 --- a/examples/c++/ssd1351.cxx +++ b/examples/c++/ssd1351.cxx @@ -1,61 +1,57 @@ -#include "mraa.hpp" -#include -#include - #include "ssd1351.hpp" +#include "upm_utilities.h" -#define BLACK 0x0000 -#define WHITE 0xFFFF -#define INTEL_BLUE 0x0BF8 +#define BLACK 0x0000 +#define WHITE 0xFFFF +#define INTEL_BLUE 0x0BF8 -int main(int argc, char **argv) +int +main(int argc, char** argv) { // Define colors (16-bit RGB on 5/6/5 bits) - int colors[] = {0x0000, 0x000F, 0x03E0, 0x03EF, - 0x7800, 0x780F, 0x7BE0, 0xC618, - 0x7BEF, 0x001F, 0x07E0, 0x07FF, - 0xF800, 0xF81F, 0xFFE0, 0xFFFF}; -//! [Interesting] + int colors[] = { 0x0000, 0x000F, 0x03E0, 0x03EF, 0x7800, 0x780F, 0x7BE0, 0xC618, + 0x7BEF, 0x001F, 0x07E0, 0x07FF, 0xF800, 0xF81F, 0xFFE0, 0xFFFF }; + //! [Interesting] // Initialize display with pins // oc = 0, dc = 1, r = 2, si = 11, cl = 13 - upm::SSD1351* display = new upm::SSD1351(0, 1, 2); + upm::SSD1351 display(0, 1, 2); // Test lines pixel by pixel - for(int i = 0; i < SSD1351HEIGHT; i++) { - for(int j = 0; j < SSD1351WIDTH; j++) { - display->drawPixel(i, j, colors[i/8]); + for (int i = 0; i < SSD1351HEIGHT; i++) { + for (int j = 0; j < SSD1351WIDTH; j++) { + display.drawPixel(i, j, colors[i / 8]); } } - display->refresh(); - sleep(5); + display.refresh(); + upm_delay(5); // Test rectangles - for(int i = 0; i < SSD1351HEIGHT/32; i++) { - for (int j = 0; j < SSD1351WIDTH/32; j++) { - display->fillRect(i * 32, j * 32, 32, 32, colors[i * 4 + j]); + for (int i = 0; i < SSD1351HEIGHT / 32; i++) { + for (int j = 0; j < SSD1351WIDTH / 32; j++) { + display.fillRect(i * 32, j * 32, 32, 32, colors[i * 4 + j]); } } - display->refresh(); - sleep(5); + display.refresh(); + upm_delay(5); // Test circles - display->fillScreen(0x2104); - for(int i = 0; i < SSD1351HEIGHT/32; i++) { - for (int j = 0; j < SSD1351WIDTH/32; j++) { - display->drawCircle(i * 32 + 15, j * 32 + 15, 15, colors[i * 4 + j]); + display.fillScreen(0x2104); + for (int i = 0; i < SSD1351HEIGHT / 32; i++) { + for (int j = 0; j < SSD1351WIDTH / 32; j++) { + display.drawCircle(i * 32 + 15, j * 32 + 15, 15, colors[i * 4 + j]); } } - display->refresh(); - sleep(5); + display.refresh(); + upm_delay(5); // Test Text - display->fillScreen(INTEL_BLUE); - display->setTextColor(WHITE, INTEL_BLUE); - display->setTextSize(4); - display->setCursor(7, 30); - display->print("Intel"); - display->setCursor(5, 70); - display->print("IoTDK"); - display->refresh(); -//! [Interesting] + display.fillScreen(INTEL_BLUE); + display.setTextColor(WHITE, INTEL_BLUE); + display.setTextSize(4); + display.setCursor(7, 30); + display.print("Intel"); + display.setCursor(5, 70); + display.print("IoTDK"); + display.refresh(); + //! [Interesting] } diff --git a/examples/c++/st7735.cxx b/examples/c++/st7735.cxx index 5dedd8b3..83606b27 100644 --- a/examples/c++/st7735.cxx +++ b/examples/c++/st7735.cxx @@ -22,58 +22,54 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include + #include "st7735.hpp" -#include int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - upm::ST7735 * lcd = new upm::ST7735(7, 4, 9, 8); - lcd->fillScreen (ST7735_RED); - lcd->refresh (); + //! [Interesting] + upm::ST7735 lcd(7, 4, 9, 8); + lcd.fillScreen(ST7735_RED); + lcd.refresh(); - lcd->fillScreen (ST7735_CYAN); - lcd->refresh (); + lcd.fillScreen(ST7735_CYAN); + lcd.refresh(); - lcd->fillScreen (ST7735_BLACK); - lcd->refresh (); + lcd.fillScreen(ST7735_BLACK); + lcd.refresh(); - lcd->drawLine(10, 10, 10, 100, ST7735_MAGENTA); - lcd->drawLine(20, 20, 10, 100, ST7735_YELLOW); - lcd->drawLine(30, 30, 50, 100, ST7735_WHITE); - lcd->refresh (); + lcd.drawLine(10, 10, 10, 100, ST7735_MAGENTA); + lcd.drawLine(20, 20, 10, 100, ST7735_YELLOW); + lcd.drawLine(30, 30, 50, 100, ST7735_WHITE); + lcd.refresh(); - lcd->drawPixel (20, 20, ST7735_GREEN); - lcd->refresh (); + lcd.drawPixel(20, 20, ST7735_GREEN); + lcd.refresh(); - lcd->drawTriangle (50, 50, 80, 80, 60, 90, ST7735_GREEN); - lcd->refresh (); + lcd.drawTriangle(50, 50, 80, 80, 60, 90, ST7735_GREEN); + lcd.refresh(); - lcd->drawCircle (100, 110, 10, ST7735_BLUE); - lcd->refresh (); + lcd.drawCircle(100, 110, 10, ST7735_BLUE); + lcd.refresh(); + lcd.setTextWrap(0x0); - lcd->setTextWrap(0x0); + lcd.setCursor(0, 30); + lcd.setTextColor(ST7735_RED, ST7735_RED); + lcd.setTextSize(1); + lcd.print("Hello World!"); - lcd->setCursor(0, 30); - lcd->setTextColor(ST7735_RED, ST7735_RED); - lcd->setTextSize(1); - lcd->print("Hello World!"); + lcd.setCursor(10, 50); + lcd.setTextColor(ST7735_RED, ST7735_YELLOW); + lcd.setTextSize(2); + lcd.print("BIG"); - lcd->setCursor(10, 50); - lcd->setTextColor(ST7735_RED, ST7735_YELLOW); - lcd->setTextSize(2); - lcd->print("BIG"); - - lcd->refresh (); + lcd.refresh(); std::cout << "exiting application" << std::endl; - delete lcd; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/stepmotor.cxx b/examples/c++/stepmotor.cxx index 8f4194c3..371b61e9 100644 --- a/examples/c++/stepmotor.cxx +++ b/examples/c++/stepmotor.cxx @@ -23,15 +23,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include + #include "stepmotor.hpp" +#include "upm_utilities.h" using namespace std; int doWork = 1; -upm::StepMotor *sensor = NULL; void sig_handler(int signo) @@ -44,35 +45,34 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] - sensor = new upm::StepMotor(2, 3); + //! [Interesting] + upm::StepMotor sensor(2, 3); while (doWork) { cout << "1 Revolution forward and back at 60 rpm" << endl; - sensor->setSpeed(60); - sensor->stepForward(200); - usleep(1000000); - sensor->stepBackward(200); - usleep(1000000); + sensor.setSpeed(60); + sensor.stepForward(200); + upm_delay_us(1000000); + sensor.stepBackward(200); + upm_delay_us(1000000); cout << "1 Revolution forward and back at 150 rpm" << endl; - sensor->setSpeed(150); - sensor->stepForward(200); - usleep(1000000); - sensor->stepBackward(200); - usleep(1000000); + sensor.setSpeed(150); + sensor.stepForward(200); + upm_delay_us(1000000); + sensor.stepBackward(200); + upm_delay_us(1000000); cout << "1 Revolution forward and back at 300 rpm" << endl; - sensor->setSpeed(300); - sensor->stepForward(200); - usleep (1000000); - sensor->stepBackward(200); - usleep (1000000); + sensor.setSpeed(300); + sensor.stepForward(200); + upm_delay_us(1000000); + sensor.stepBackward(200); + upm_delay_us(1000000); } - delete sensor; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/sx1276-fsk.cxx b/examples/c++/sx1276-fsk.cxx index 375e3e5d..197bf2b7 100644 --- a/examples/c++/sx1276-fsk.cxx +++ b/examples/c++/sx1276-fsk.cxx @@ -22,100 +22,89 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include #include +#include +#include + #include "sx1276.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] + signal(SIGINT, sig_handler); + //! [Interesting] - cout << "Specify an argument to go into receive mode. Default is transmit" - << endl; + cout << "Specify an argument to go into receive mode. Default is transmit" << endl; - bool rx = false; - if (argc > 1) - rx = true; + bool rx = false; + if (argc > 1) + rx = true; - // Instantiate an SX1276 using default parameters - upm::SX1276 *sensor = new upm::SX1276(); + // Instantiate an SX1276 using default parameters + upm::SX1276 sensor; - // 915Mhz - sensor->setChannel(915000000); + // 915Mhz + sensor.setChannel(915000000); - // FSK configuration (rx and tx must be configured the same): - // Tx output power = 14 dBm - // FSK freq deviation = 25000 Hz - // FSK bandwidth = 50000 bps - // FSK AFC bandwidth = 83333 Hz - // FSK datarate = 50000 bps - // FSK preamble len = 5 - // FSK fixed length payload = false - // FSK CRC check = true - // FSK (rx) continuous Rx mode = false + // FSK configuration (rx and tx must be configured the same): + // Tx output power = 14 dBm + // FSK freq deviation = 25000 Hz + // FSK bandwidth = 50000 bps + // FSK AFC bandwidth = 83333 Hz + // FSK datarate = 50000 bps + // FSK preamble len = 5 + // FSK fixed length payload = false + // FSK CRC check = true + // FSK (rx) continuous Rx mode = false - sensor->setTxConfig(sensor->MODEM_FSK, 14, 25000, 0, - 50000, 0, 5, false, true, false, 0, false); - - sensor->setRxConfig(sensor->MODEM_FSK, 50000, 50000, - 0, 83333, 5, 0, false, 0, true, - false, 0, false, true); + sensor.setTxConfig(sensor.MODEM_FSK, 14, 25000, 0, 50000, 0, 5, false, true, false, 0, false); - int count = 0; - int buflen = 64; - char buffer[buflen]; + sensor.setRxConfig( + sensor.MODEM_FSK, 50000, 50000, 0, 83333, 5, 0, false, 0, true, false, 0, false, true); - while (shouldRun) - { - if (!rx) - { - snprintf(buffer, buflen, "Ping %d", count++); - cout << "Sending..." << std::string(buffer) << endl; - sensor->sendStr(string(buffer), 3000); - - sensor->setSleep(); - sleep(1); - } - else - { - // receiving - cout << "Attempting to receive..." << endl; - int rv; - if ((rv = sensor->setRx(3000))) - { - cout << "setRx returned " << rv << endl; - } - else - { - cout << "Received Buffer: " << sensor->getRxBufferStr() << endl; + int count = 0; + int buflen = 64; + char buffer[buflen]; + + while (shouldRun) { + if (!rx) { + snprintf(buffer, buflen, "Ping %d", count++); + cout << "Sending..." << std::string(buffer) << endl; + sensor.sendStr(string(buffer), 3000); + + sensor.setSleep(); + upm_delay(1); + } else { + // receiving + cout << "Attempting to receive..." << endl; + int rv; + if ((rv = sensor.setRx(3000))) { + cout << "setRx returned " << rv << endl; + } else { + cout << "Received Buffer: " << sensor.getRxBufferStr() << endl; } - // go back to sleep when done - sensor->setSleep(); - usleep(250000); + // go back to upm_delay when done + sensor.setSleep(); + upm_delay_us(250000); } - - } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/sx1276-lora.cxx b/examples/c++/sx1276-lora.cxx index f8902f25..1585445a 100644 --- a/examples/c++/sx1276-lora.cxx +++ b/examples/c++/sx1276-lora.cxx @@ -22,98 +22,88 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include #include +#include +#include + #include "sx1276.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); -//! [Interesting] - cout << "Specify an argument to go into receive mode. Default is transmit" - << endl; + signal(SIGINT, sig_handler); + //! [Interesting] + cout << "Specify an argument to go into receive mode. Default is transmit" << endl; - bool rx = false; - if (argc > 1) - rx = true; + bool rx = false; + if (argc > 1) + rx = true; - // Instantiate an SX1276 using default parameters - upm::SX1276 *sensor = new upm::SX1276(); + // Instantiate an SX1276 using default parameters + upm::SX1276 sensor; - // 915Mhz - sensor->setChannel(915000000); + // 915Mhz + sensor.setChannel(915000000); - // LORA configuration (rx and tx must be configured the same): - // Tx output power = 14 dBm - // LORA bandwidth = 125000 (can also be 250K and 500K) - // LORA spreading factor = 7 - // LORA coding rate = 1 (4/5) - // LORA preamble len = 8 - // LORA symbol timeout = 5 - // LORA fixed payload = false - // LORA IQ inversion = false - // LORA (rx) continuous Rx mode = true + // LORA configuration (rx and tx must be configured the same): + // Tx output power = 14 dBm + // LORA bandwidth = 125000 (can also be 250K and 500K) + // LORA spreading factor = 7 + // LORA coding rate = 1 (4/5) + // LORA preamble len = 8 + // LORA symbol timeout = 5 + // LORA fixed payload = false + // LORA IQ inversion = false + // LORA (rx) continuous Rx mode = true - sensor->setTxConfig(sensor->MODEM_LORA, 14, 0, 125000, - 7, 1, 8, false, true, false, 0, false); - - sensor->setRxConfig(sensor->MODEM_LORA, 125000, 7, - 1, 0, 8, 5, false, 0, true, false, 0, false, true); - - int count = 0; - int buflen = 64; - char buffer[buflen]; + sensor.setTxConfig(sensor.MODEM_LORA, 14, 0, 125000, 7, 1, 8, false, true, false, 0, false); - while (shouldRun) - { - if (!rx) - { - snprintf(buffer, buflen, "Ping %d", count++); - cout << "Sending..." << std::string(buffer) << endl; - sensor->sendStr(string(buffer), 3000); - sensor->setSleep(); - sleep(1); - } - else - { - // receiving - cout << "Attempting to receive..." << endl; - int rv; - if ((rv = sensor->setRx(3000))) - { - cout << "setRx returned " << rv << endl; - } - else - { - cout << "Received Buffer: " << sensor->getRxBufferStr() << endl; + sensor.setRxConfig( + sensor.MODEM_LORA, 125000, 7, 1, 0, 8, 5, false, 0, true, false, 0, false, true); + + int count = 0; + int buflen = 64; + char buffer[buflen]; + + while (shouldRun) { + if (!rx) { + snprintf(buffer, buflen, "Ping %d", count++); + cout << "Sending..." << std::string(buffer) << endl; + sensor.sendStr(string(buffer), 3000); + sensor.setSleep(); + upm_delay(1); + } else { + // receiving + cout << "Attempting to receive..." << endl; + int rv; + if ((rv = sensor.setRx(3000))) { + cout << "setRx returned " << rv << endl; + } else { + cout << "Received Buffer: " << sensor.getRxBufferStr() << endl; } - // go back to sleep when done - sensor->setSleep(); - usleep(5000); + // go back to upm_delay when done + sensor.setSleep(); + upm_delay_us(5000); } - - } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; - - delete sensor; - - return 0; + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/sx6119.cxx b/examples/c++/sx6119.cxx index 0d1178fd..2a6ee6f2 100644 --- a/examples/c++/sx6119.cxx +++ b/examples/c++/sx6119.cxx @@ -22,48 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include + #include "sx6119.hpp" using namespace std; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a SX6119 on digital pins 2 (power) and 3 (seek) - // This example was tested on the Grove FM Receiver. + //! [Interesting] + // Instantiate a SX6119 on digital pins 2 (power) and 3 (seek) + // This example was tested on the Grove FM Receiver. - upm::SX6119* radio = new upm::SX6119(2, 3); - - // if an argument was specified (any argument), seek to the next - // station, else just toggle the power. + upm::SX6119 radio(2, 3); + // if an argument was specified (any argument), seek to the next + // station, else just toggle the power. - cout << "Supply any argument to the command line to seek to the" << endl; - cout << "next station." << endl; - cout << "Running the example without an argument will toggle the" < 1) - doSeek = true; - - // depending on what was selected, do it + if (argc > 1) + doSeek = true; - if (doSeek) - radio->seek(); - else - radio->togglePower(); - -//! [Interesting] + // depending on what was selected, do it - cout << "Exiting..." << endl; + if (doSeek) + radio.seek(); + else + radio.togglePower(); - delete radio; - return 0; + //! [Interesting] + + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/t3311.cxx b/examples/c++/t3311.cxx index 4401a1b5..517af4ce 100644 --- a/examples/c++/t3311.cxx +++ b/examples/c++/t3311.cxx @@ -22,94 +22,86 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "t3311.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Initializing..." << endl; + cout << "Initializing..." << endl; - // Instantiate an T3311 instance, using MODBUS slave address 1, and - // default comm parameters (9600, 8, N, 2) - upm::T3311 *sensor = new upm::T3311(defaultDev, 1); + // Instantiate an T3311 instance, using MODBUS slave address 1, and + // default comm parameters (9600, 8, N, 2) + upm::T3311 sensor(defaultDev, 1); - // output the serial number and firmware revision - cout << "Serial Number: " << sensor->getSerialNumber() << endl; - cout << "Firmware Revision: " << sensor->getFirmwareMajor() - << "." << sensor->getFirmwareMinor() << endl; - cout << endl; + // output the serial number and firmware revision + cout << "Serial Number: " << sensor.getSerialNumber() << endl; + cout << "Firmware Revision: " << sensor.getFirmwareMajor() << "." << sensor.getFirmwareMinor() + << endl; + cout << endl; - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor->getHumidity() - << " %" << endl; + cout << "Humidity: " << sensor.getHumidity() << " %" << endl; - // this value depends on the sensor configuration -- by default - // it is the dew point temperature - cout << "Computed Value: " << sensor->getComputedValue() << endl; + // this value depends on the sensor configuration -- by default + // it is the dew point temperature + cout << "Computed Value: " << sensor.getComputedValue() << endl; - // with FW revisions > 2.44, extended computed data is available - if (sensor->extendedDataAvailable()) - { - cout << "Dew Point Temperature: " << sensor->getDewPointTemperature() - << " C / " << sensor->getDewPointTemperature(true) << " F" - << endl; + // with FW revisions > 2.44, extended computed data is available + if (sensor.extendedDataAvailable()) { + cout << "Dew Point Temperature: " << sensor.getDewPointTemperature() << " C / " + << sensor.getDewPointTemperature(true) << " F" << endl; - cout << "Absolute Humidity: " << sensor->getAbsoluteHumidity() - << " g/m3" << endl; + cout << "Absolute Humidity: " << sensor.getAbsoluteHumidity() << " g/m3" << endl; - cout << "Specific Humidity: " << sensor->getSpecificHumidity() - << " g/kg" << endl; + cout << "Specific Humidity: " << sensor.getSpecificHumidity() << " g/kg" << endl; - cout << "Mixing Ratio: " << sensor->getMixingRatio() - << " g/kg" << endl; + cout << "Mixing Ratio: " << sensor.getMixingRatio() << " g/kg" << endl; - cout << "Specific Enthalpy: " << sensor->getSpecificEnthalpy() - << " kJ/kg" << endl; + cout << "Specific Enthalpy: " << sensor.getSpecificEnthalpy() << " kJ/kg" << endl; } - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/t6713-co2-sensor.cxx b/examples/c++/t6713-co2-sensor.cxx index da978ad7..849c5d16 100644 --- a/examples/c++/t6713-co2-sensor.cxx +++ b/examples/c++/t6713-co2-sensor.cxx @@ -22,50 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include -#include "t6713.hpp" +#include +#include -#define EDISON_I2C_BUS 1 +#include "iCO2Sensor.hpp" +#include "mraa/common.h" +#include "t6713.hpp" +#include "upm_utilities.h" + +#define EDISON_I2C_BUS 1 #define FT4222_I2C_BUS 0 //! [Interesting] -// Simple example of using ICO2Sensor to determine -// which sensor is present and return its name. -// ICO2Sensor is then used to get readings from sensor - - -upm::ICO2Sensor* getCO2Sensor() +int +main() { - upm::ICO2Sensor* cO2Sensor = NULL; - try { - cO2Sensor = new upm::T6713(mraa_get_sub_platform_id(FT4222_I2C_BUS)); - return cO2Sensor; - } catch (std::exception& e) { - std::cerr << "T6713: " << e.what() << std::endl; - } - return cO2Sensor; + /* Create an instance of the T6713 sensor */ + upm::T6713 sensor(mraa_get_sub_platform_id(FT4222_I2C_BUS)); + + /* Show usage from the ICO2Sensor interface */ + upm::ICO2Sensor* cO2Sensor = static_cast(&sensor); + + if (cO2Sensor == NULL) { + std::cout << "CO2 sensor not detected" << std::endl; + return 1; + } + std::cout << "CO2 sensor " << cO2Sensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + uint16_t value = cO2Sensor->getPpm(); + std::cout << "CO2 level = " << value << " ppm" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + upm_delay(1); + } + + return 0; } -int main () -{ - upm::ICO2Sensor* cO2Sensor = getCO2Sensor(); - if (cO2Sensor == NULL) { - std::cout << "CO2 sensor not detected" << std::endl; - return 1; - } - std::cout << "CO2 sensor " << cO2Sensor->getModuleName() << " detected" << std::endl; - while (true) { - try { - uint16_t value = cO2Sensor->getPpm(); - std::cout << "CO2 level = " << value << " ppm" << std::endl; - } catch (std::exception& e) { - std::cerr << e.what() << std::endl; - } - sleep(1); - } - delete cO2Sensor; - return 0; -} - -//! [Interesting] +//! [Interesting] diff --git a/examples/c++/t6713.cxx b/examples/c++/t6713.cxx index 81b8512c..e0f14c0d 100644 --- a/examples/c++/t6713.cxx +++ b/examples/c++/t6713.cxx @@ -22,13 +22,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + +#include "mraa/common.h" #include "t6713.hpp" +#include "upm_utilities.h" #define FT4222_I2C_BUS 0 -int main () +int +main() { //! [Interesting] upm::T6713 cO2Sensor(mraa_get_sub_platform_id(FT4222_I2C_BUS)); @@ -36,7 +39,7 @@ int main () while (true) { uint16_t value = cO2Sensor.getPpm(); std::cout << "CO2 level = " << value << " ppm" << std::endl; - sleep(1); + upm_delay(1); } //! [Interesting] return 0; diff --git a/examples/c++/t8100.cxx b/examples/c++/t8100.cxx index 3cecbac4..06815e0e 100644 --- a/examples/c++/t8100.cxx +++ b/examples/c++/t8100.cxx @@ -22,96 +22,87 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "t8100.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // You will need to edit this example to conform to your site and your - // devices, specifically the Device Object Instance ID passed to the - // constructor, and the arguments to initMaster() that are - // appropriate for your BACnet network. + //! [Interesting] + // You will need to edit this example to conform to your site and your + // devices, specifically the Device Object Instance ID passed to the + // constructor, and the arguments to initMaster() that are + // 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an T8100 object for an T8100 device that has 568000 - // as it's unique Device Object Instance ID. NOTE: You will - // certainly want to change this to the correct value for your - // device(s). - T8100 *sensor = new T8100(568000); + // Instantiate an T8100 object for an T8100 device that has 568000 + // as it's unique Device Object Instance ID. NOTE: You will + // certainly want to change this to the correct value for your + // device(s). + T8100 sensor(568000); - // Initialize our BACnet master, if it has not already been - // initialized, with the device and baudrate, choosing 1000001 as - // our unique Device Object Instance ID, 2 as our MAC address and - // using default values for maxMaster and maxInfoFrames - sensor->initMaster(defaultDev, 38400, 1000001, 2); + // Initialize our BACnet master, if it has not already been + // initialized, with the device and baudrate, choosing 1000001 as + // our unique Device Object Instance ID, 2 as our MAC address and + // using default values for maxMaster and maxInfoFrames + sensor.initMaster(defaultDev, 38400, 1000001, 2); - // Uncomment to enable debugging output - // sensor->setDebug(true); + // Uncomment to enable debugging output + // sensor.setDebug(true); - cout << endl; - cout << "Device Description: " << sensor->getDeviceDescription() << endl; - cout << "Device Location: " << sensor->getDeviceLocation() << endl; - cout << endl; + cout << endl; + cout << "Device Description: " << sensor.getDeviceDescription() << endl; + cout << "Device Location: " << sensor.getDeviceLocation() << endl; + cout << endl; - // update and print a few values every 5 seconds - while (shouldRun) - { - // update our values - sensor->update(); + // update and print a few values every 5 seconds + while (shouldRun) { + // update our values + sensor.update(); - cout << "CO2 Concentration: " - << sensor->getCO2() - << " ppm" - << endl; + cout << "CO2 Concentration: " << sensor.getCO2() << " ppm" << endl; - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " - << sensor->getTemperature(true) - << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << "Humidity: " << sensor->getHumidity() - << " %RH" - << endl; + cout << "Humidity: " << sensor.getHumidity() << " %RH" << endl; - cout << "Relay State: " << sensor->getRelayState() - << endl; + cout << "Relay State: " << sensor.getRelayState() << endl; - cout << endl; - sleep(5); + cout << endl; + upm_delay(5); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/ta12200.cxx b/examples/c++/ta12200.cxx index 8fdffd8d..58e5ab3d 100644 --- a/examples/c++/ta12200.cxx +++ b/examples/c++/ta12200.cxx @@ -22,44 +22,43 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + #include "ta12200.hpp" +#include "upm_utilities.h" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a TA12-200 sensor on analog pin A0 - upm::TA12200* electricity = new upm::TA12200(0); - - while (shouldRun) - { - unsigned int maxVal = electricity->highestValue(); - float current = electricity->milliAmps(maxVal); + //! [Interesting] + // Instantiate a TA12-200 sensor on analog pin A0 + upm::TA12200 electricity(0); - cout << "Max ADC Value: " << maxVal << ", current: " << current - << "mA" << endl; - usleep(100000); + while (shouldRun) { + unsigned int maxVal = electricity.highestValue(); + float current = electricity.milliAmps(maxVal); + + cout << "Max ADC Value: " << maxVal << ", current: " << current << "mA" << endl; + upm_delay_us(100000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete electricity; - return 0; + return 0; } diff --git a/examples/c++/tb7300.cxx b/examples/c++/tb7300.cxx index 0aab8ac9..0d2a3ba4 100644 --- a/examples/c++/tb7300.cxx +++ b/examples/c++/tb7300.cxx @@ -22,103 +22,92 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "tb7300.hpp" +#include "upm_utilities.h" using namespace std; using namespace upm; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // You will need to edit this example to conform to your site and your - // devices, specifically the Device Object Instance ID passed to the - // constructor, and the arguments to initMaster() that are - // appropriate for your BACnet network. + //! [Interesting] + // You will need to edit this example to conform to your site and your + // devices, specifically the Device Object Instance ID passed to the + // constructor, and the arguments to initMaster() that are + // 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 (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an TB7300 object for an TB7300 device that has 73001 - // as it's unique Device Object Instance ID. NOTE: You will - // certainly want to change this to the correct value for your - // device(s). - TB7300 *sensor = new TB7300(73001); + // Instantiate an TB7300 object for an TB7300 device that has 73001 + // as it's unique Device Object Instance ID. NOTE: You will + // certainly want to change this to the correct value for your + // device(s). + TB7300 sensor(73001); - // Initialize our BACnet master, if it has not already been - // initialized, with the device and baudrate, choosing 1000001 as - // our unique Device Object Instance ID, 2 as our MAC address and - // using default values for maxMaster and maxInfoFrames - sensor->initMaster(defaultDev, 38400, 1000001, 2); + // Initialize our BACnet master, if it has not already been + // initialized, with the device and baudrate, choosing 1000001 as + // our unique Device Object Instance ID, 2 as our MAC address and + // using default values for maxMaster and maxInfoFrames + sensor.initMaster(defaultDev, 38400, 1000001, 2); - // Uncomment to enable debugging output - // sensor->setDebug(true); + // Uncomment to enable debugging output + // sensor.setDebug(true); - cout << endl; - cout << "Device Name: " << sensor->getDeviceName() << endl; - cout << "Device Description: " << sensor->getDeviceDescription() << endl; - cout << "Device Location: " << sensor->getDeviceLocation() << endl; - cout << endl; + cout << endl; + cout << "Device Name: " << sensor.getDeviceName() << endl; + cout << "Device Description: " << sensor.getDeviceDescription() << endl; + cout << "Device Location: " << sensor.getDeviceLocation() << endl; + cout << endl; - cout << "Fan Mode: " - << sensor->getMultiStateValueText(TB7300::MV_Fan_Mode) - << endl; + cout << "Fan Mode: " << sensor.getMultiStateValueText(TB7300::MV_Fan_Mode) << endl; - cout << "Fan Status: " - << sensor->getMultiStateValueText(TB7300::MV_Fan_Status) - << endl; + cout << "Fan Status: " << sensor.getMultiStateValueText(TB7300::MV_Fan_Status) << endl; - cout << "System Mode: " - << sensor->getMultiStateValueText(TB7300::MV_System_Mode) - << endl; + cout << "System Mode: " << sensor.getMultiStateValueText(TB7300::MV_System_Mode) << endl; - cout << "Service Alarm: " - << sensor->getBinaryInputText(TB7300::BI_Service_Alarm) - << endl; + cout << "Service Alarm: " << sensor.getBinaryInputText(TB7300::BI_Service_Alarm) << endl; - cout << endl; + cout << endl; - // update and print the room temperature every 5 seconds - while (shouldRun) - { - // update our values - sensor->update(); + // update and print the room temperature every 5 seconds + while (shouldRun) { + // update our values + sensor.update(); - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " - << sensor->getTemperature(true) - << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << endl; - sleep(5); + cout << endl; + upm_delay(5); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/tca9548a.cxx b/examples/c++/tca9548a.cxx index df29ad2f..01a42d1e 100644 --- a/examples/c++/tca9548a.cxx +++ b/examples/c++/tca9548a.cxx @@ -23,12 +23,9 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include -#include #include + #include "tca9548a.hpp" using namespace std; @@ -40,64 +37,63 @@ using namespace upm; */ void -showAllPorts(upm::TCA9548A *multiplex) { +showAllPorts(upm::TCA9548A& multiplex) +{ int i; - char ports [80]; + char ports[80]; std::string convert; - for (i = 0; i<8; i++) { - if (multiplex->getPort(i)) - strcat (ports, "1." ); + for (i = 0; i < 8; i++) { + if (multiplex.getPort(i)) + strcat(ports, "1."); else - strcat (ports,"0."); + strcat(ports, "0."); } cout << "Ports: " << ports; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - // Instantiate a TCA9548A instance of i2c multiplexer - upm::TCA9548A *multiplex = new upm::TCA9548A(0); + upm::TCA9548A multiplex(0); - //Clean - multiplex->disableAllPorts(); + // Clean + multiplex.disableAllPorts(); cout << "All ports disabled."; // Open i2c bus on multiplexer channel 4 - multiplex->setPort(4,multiplex->ENABLED,multiplex->EXCLUSIVE); + multiplex.setPort(4, multiplex.ENABLED, multiplex.EXCLUSIVE); cout << "Port 4 enabled with exclusive access."; showAllPorts(multiplex); // Close com with channel 4 - multiplex->disableAllPorts(); + multiplex.disableAllPorts(); // Open i2c bus on multiplexer channel 2 - multiplex->setPort(2,multiplex->ENABLED,multiplex->EXCLUSIVE); + multiplex.setPort(2, multiplex.ENABLED, multiplex.EXCLUSIVE); cout << "Port 2 enabled with exclusive access."; showAllPorts(multiplex); - //close com with channel 2 - multiplex->disableAllPorts(); + // close com with channel 2 + multiplex.disableAllPorts(); // Open 2 i2c bus on multiplexer channel 4 and channel2 - multiplex->setPort(4,multiplex->ENABLED,multiplex->EXCLUSIVE); - multiplex->setPort(2,multiplex->ENABLED,multiplex->INCLUSIVE); + multiplex.setPort(4, multiplex.ENABLED, multiplex.EXCLUSIVE); + multiplex.setPort(2, multiplex.ENABLED, multiplex.INCLUSIVE); cout << "Port 4 enabled with exclusive access."; cout << "Port 2 enabled with exclusive access."; showAllPorts(multiplex); - //close com with all channels - multiplex->disableAllPorts(); + // close com with all channels + multiplex.disableAllPorts(); // Open i2c bus on all multiplexer channels - multiplex->enableAllPorts(); + multiplex.enableAllPorts(); cout << "All ports enabled."; showAllPorts(multiplex); - //close com with all channels - multiplex->disableAllPorts(); - - delete multiplex; + // close com with all channels + multiplex.disableAllPorts(); return 0; } diff --git a/examples/c++/tcs3414cs.cxx b/examples/c++/tcs3414cs.cxx index 7efcebd1..cd976899 100644 --- a/examples/c++/tcs3414cs.cxx +++ b/examples/c++/tcs3414cs.cxx @@ -22,13 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "tcs3414cs.hpp" #include +#include + +#include "tcs3414cs.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::TCS3414CS *sensor = NULL; void sig_handler(int signo) @@ -41,25 +42,24 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] upm::tcs3414sc_rgb_t rgb; // Instantiate the color sensor on I2C - sensor = new upm::TCS3414CS (); + upm::TCS3414CS sensor; // Print out the r, g, b, and clr value every 0.5 seconds while (!doWork) { - sensor->readRGB (&rgb); - std::cout << (int)rgb.r << ", " << (int)rgb.g << ", " << (int)rgb.b << ", " << rgb.clr << std::endl; - usleep (500000); + sensor.readRGB(&rgb); + std::cout << (int) rgb.r << ", " << (int) rgb.g << ", " << (int) rgb.b << ", " << rgb.clr + << std::endl; + upm_delay_us(500000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/tcs37727.cxx b/examples/c++/tcs37727.cxx index 909c49c2..0b38d919 100644 --- a/examples/c++/tcs37727.cxx +++ b/examples/c++/tcs37727.cxx @@ -21,57 +21,57 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "tcs37727.hpp" +#include "upm_utilities.h" using namespace upm; bool run = true; -void sig_handler(int sig) +void +sig_handler(int sig) { - if (sig == SIGINT) - run = false; + if (sig == SIGINT) + run = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - upm::tcs37727_data_t data; + //! [Interesting] + upm::tcs37727_data_t data; - std::cout << "Initializing test-application..." << std::endl; + std::cout << "Initializing test-application..." << std::endl; - // Instantiate an TCS37727 instance on bus 1 - upm::TCS37727 *mySensor = new upm::TCS37727(1); + // Instantiate an TCS37727 instance on bus 1 + upm::TCS37727 mySensor(1); - // activate periodic measurements - mySensor->setActive(); + // activate periodic measurements + mySensor.setActive(); - // update and print available values every second - while (run) - { - mySensor->getData (&data, true); - std::cout << "Red: " << (int)data.red << std::endl - << "Green: " << (int)data.green << std::endl - << "Blue: " << (int)data.blue << std::endl - << "Clear: " << (int)data.clear << std::endl - << "Lux: " << (int)data.lux << std::endl - << "Color temperature: " << (int)data.ct << std::endl; + // update and print available values every second + while (run) { + mySensor.getData(&data, true); + std::cout << "Red: " << (int) data.red << std::endl + << "Green: " << (int) data.green << std::endl + << "Blue: " << (int) data.blue << std::endl + << "Clear: " << (int) data.clear << std::endl + << "Lux: " << (int) data.lux << std::endl + << "Color temperature: " << (int) data.ct << std::endl; std::cout << std::endl; - sleep(1); + upm_delay(1); } - std::cout << "Exiting test-application..." << std::endl; + std::cout << "Exiting test-application..." << std::endl; - delete mySensor; -//! [Interesting] + //! [Interesting] - return 0; + return 0; } diff --git a/examples/c++/teams.cxx b/examples/c++/teams.cxx index 40570ada..354f001f 100644 --- a/examples/c++/teams.cxx +++ b/examples/c++/teams.cxx @@ -22,63 +22,61 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "teams.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 TEAMS instance, using A0 for temperature, and - // 165.0 ohms for the rResistor value (for the libelium 4-20ma - // interface) - upm::TEAMS *sensor = new upm::TEAMS(0, 165.0); + // Instantiate an TEAMS instance, using A0 for temperature, and + // 165.0 ohms for the rResistor value (for the libelium 4-20ma + // interface) + upm::TEAMS sensor(0, 165.0); - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); - // is the sensor connected? (current >= 4ma) - cout << "Is Connected: " << sensor->isConnected() << endl; + // is the sensor connected? (current >= 4ma) + cout << "Is Connected: " << sensor.isConnected() << endl; - // print computed current on the loop. This includes the offset, - // if one was set by setOffsetMilliamps(). - cout << "Milliamps: " << sensor->getRawMilliamps() << endl; + // print computed current on the loop. This includes the offset, + // if one was set by setOffsetMilliamps(). + cout << "Milliamps: " << sensor.getRawMilliamps() << endl; - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " << sensor.getTemperature(true) + << " F" << endl; - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/temperature.cxx b/examples/c++/temperature.cxx index f8eb7dc1..b48a5d73 100644 --- a/examples/c++/temperature.cxx +++ b/examples/c++/temperature.cxx @@ -23,35 +23,34 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include + #include "temperature.hpp" +#include "upm_utilities.h" using namespace std; int -main(int argc, char **argv) +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] // Create the temperature sensor object using AIO pin 0 - upm::Temperature* temp = new upm::Temperature(0); - std::cout << temp->name() << std::endl; + upm::Temperature temp(0); + std::cout << temp.name() << std::endl; // Read the temperature ten times, printing both the Celsius and // equivalent Fahrenheit temperature, waiting one second between readings - for (int i=0; i < 10; i++) { - int celsius = temp->value(); - int fahrenheit = (int) (celsius * 9.0/5.0 + 32.0); - printf("%d degrees Celsius, or %d degrees Fahrenheit\n", - celsius, fahrenheit); - sleep(1); + for (int i = 0; i < 10; i++) { + int celsius = temp.value(); + int fahrenheit = (int) (celsius * 9.0 / 5.0 + 32.0); + printf("%d degrees Celsius, or %d degrees Fahrenheit\n", celsius, fahrenheit); + upm_delay(1); } // Delete the temperature sensor object - delete temp; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/tex00.cxx b/examples/c++/tex00.cxx index 8fdb9cb0..311423c6 100644 --- a/examples/c++/tex00.cxx +++ b/examples/c++/tex00.cxx @@ -22,70 +22,62 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "tex00.hpp" +#include "upm_utilities.h" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + 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 TEX00 instance, using A0 for the analog input. In - // this example, we are using a 10K Ohm balance resistor and a TED - // (10k type 2) thermistor. - upm::TEX00 *sensor = new upm::TEX00(0, 10000, - upm::TEX00::STYPE_THERMISTOR_TED); + // Instantiate an TEX00 instance, using A0 for the analog input. In + // this example, we are using a 10K Ohm balance resistor and a TED + // (10k type 2) thermistor. + upm::TEX00 sensor(0, 10000, upm::TEX00::STYPE_THERMISTOR_TED); - cout << "Minimum temperature: " << sensor->getTemperatureRangeMin() - << " C" << endl; - cout << "Maximum temperature: " << sensor->getTemperatureRangeMax() - << " C" << endl; - cout << endl; - - // update and print available values every second - while (shouldRun) - { - // update our values from the sensor - sensor->update(); + cout << "Minimum temperature: " << sensor.getTemperatureRangeMin() << " C" << endl; + cout << "Maximum temperature: " << sensor.getTemperatureRangeMax() << " C" << endl; + cout << endl; - if (sensor->isOutOfRange()) - { - cout << "Temperature out of range" << endl; - } - else - { - // we show both C and F for temperature - cout << "Temperature: " << sensor->getTemperature() - << " C / " << sensor->getTemperature(true) << " F" - << endl; + // update and print available values every second + while (shouldRun) { + // update our values from the sensor + sensor.update(); + + if (sensor.isOutOfRange()) { + cout << "Temperature out of range" << endl; + } else { + // we show both C and F for temperature + cout << "Temperature: " << sensor.getTemperature() << " C / " + << sensor.getTemperature(true) << " F" << endl; } - cout << endl; + cout << endl; - sleep(1); + upm_delay(1); } - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/th02.cxx b/examples/c++/th02.cxx index 3079d8d7..8f09563d 100644 --- a/examples/c++/th02.cxx +++ b/examples/c++/th02.cxx @@ -22,13 +22,14 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include "th02.hpp" #include +#include + +#include "th02.hpp" +#include "upm_utilities.h" int doWork = 0; -upm::TH02 *sensor = NULL; void sig_handler(int signo) @@ -41,24 +42,22 @@ sig_handler(int signo) } int -main(int argc, char **argv) +main(int argc, char** argv) { //! [Interesting] float temperature = 0.0; float humidity = 0.0; - sensor = new upm::TH02 (); + upm::TH02 sensor; while (!doWork) { - temperature = sensor->getTemperature (); - humidity = sensor->getHumidity (); + temperature = sensor.getTemperature(); + humidity = sensor.getHumidity(); std::cout << "Temperature = " << temperature << ", Humidity = " << humidity << std::endl; - usleep (500000); + upm_delay_us(500000); } //! [Interesting] std::cout << "exiting application" << std::endl; - delete sensor; - return 0; } diff --git a/examples/c++/tm1637.cxx b/examples/c++/tm1637.cxx index 14ec3a2a..efc77075 100644 --- a/examples/c++/tm1637.cxx +++ b/examples/c++/tm1637.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "tm1637.hpp" #include -#include -#include +#include #include +#include "tm1637.hpp" +#include "upm_utilities.h" + using namespace std; using namespace upm; bool run = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - run = false; + if (signo == SIGINT) + run = false; } int main(int argc, char** argv) { - //! [Interesting] - bool point = true; - int timezone = -7; // Your UTC offset - time_t rawtime; - struct tm * gmt; - char myTime[5]; + //! [Interesting] + bool point = true; + int timezone = -7; // Your UTC offset + time_t rawtime; + struct tm* gmt; + char myTime[5]; fprintf(stdout, "TM1637 Display Example\n"); signal(SIGINT, sig_handler); - TM1637 myDisplay = TM1637(0, 1); // TM1637 on pins 0 (clk) and 1 (dio) + TM1637 myDisplay = TM1637(0, 1); // TM1637 on pins 0 (clk) and 1 (dio) myDisplay.write(0x39, 0x09, 0x09); // Start a box using 7-segment encoding - myDisplay.writeAt(3, ']'); // Finish box using writeAt function - sleep(3); // Wait 3 seconds + myDisplay.writeAt(3, ']'); // Finish box using writeAt function + upm_delay(3); // Wait 3 seconds - while(run) - { - time(&rawtime); // Update raw time + while (run) { + time(&rawtime); // Update raw time gmt = gmtime(&rawtime); // Get current time int hour = (gmt) ? gmt->tm_hour : 0; @@ -67,13 +68,11 @@ main(int argc, char** argv) // Format and store the time in 24 hour format snprintf(myTime, 5, "%2d%02d", (hour + timezone + 24) % 24, min); - myDisplay.write(myTime); // Write to display as string - myDisplay.setColon(point ^= true); // Toggle the dots on the display - sleep(1); // Only update once every second + myDisplay.write(myTime); // Write to display as string + myDisplay.setColon(point ^= true); // Toggle the dots on the display + upm_delay(1); // Only update once every second } - //! [Interesting] - return 0; + //! [Interesting] + return 0; } - - diff --git a/examples/c++/tmp006.cxx b/examples/c++/tmp006.cxx index b7e3119e..78f62e2e 100644 --- a/examples/c++/tmp006.cxx +++ b/examples/c++/tmp006.cxx @@ -21,54 +21,53 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include "tmp006.hpp" +#include "upm_utilities.h" using namespace upm; bool run = true; -void sig_handler(int sig) +void +sig_handler(int sig) { - if (sig == SIGINT) - run = false; + if (sig == SIGINT) + run = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - std::cout << "Initializing test-application..." << std::endl; + //! [Interesting] + std::cout << "Initializing test-application..." << std::endl; - // please make sure that you have the right i2c address for your device - // the correct range of addresses is 0x40 - 0x47 - // Instantiate an TMP006 instance on bus 1 - upm::TMP006 *mySensor = new upm::TMP006(1, TMP006_CONFIG_CR_DEF, TMP006_I2C_ADDRESS); + // please make sure that you have the right i2c address for your device + // the correct range of addresses is 0x40 - 0x47 + // Instantiate an TMP006 instance on bus 1 + upm::TMP006 mySensor(1, TMP006_CONFIG_CR_DEF, TMP006_I2C_ADDRESS); - // you can also get basic tmp007 functionality by initializing it as follows - //upm::TMP006 *mySensor = new upm::TMP006(1, TMP006_CONFIG_CR_DEF, TMP007_I2C_ADDRESS); + // you can also get basic tmp007 functionality by initializing it as follows + // upm::TMP006 mySensor(1, TMP006_CONFIG_CR_DEF, TMP007_I2C_ADDRESS); - // activate periodic measurements - mySensor->setActive(); + // activate periodic measurements + mySensor.setActive(); - // update and print available values every second - while (run) - { - // Print out temperature value in *C - std::cout << "Temperature: " << mySensor->getTemperature(true) << " *C" - << std::endl; + // update and print available values every second + while (run) { + // Print out temperature value in *C + std::cout << "Temperature: " << mySensor.getTemperature(true) << " *C" << std::endl; - sleep(1); + upm_delay(1); } - std::cout << "Exiting test-application..." << std::endl; + std::cout << "Exiting test-application..." << std::endl; - delete mySensor; -//! [Interesting] + //! [Interesting] - return 0; + return 0; } diff --git a/examples/c++/tsl2561.cxx b/examples/c++/tsl2561.cxx index d50c6dbf..a3264df6 100644 --- a/examples/c++/tsl2561.cxx +++ b/examples/c++/tsl2561.cxx @@ -24,30 +24,29 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include +#include -#include #include "tsl2561.hpp" -int main (int argc, char **argv) +int +main(int argc, char** argv) { - upm::TSL2561 *sensor = NULL; int loopCount = 100; -//! [Interesting] + //! [Interesting] if (argc < 2) { printf("Provide loop count \n"); } else { loopCount = atoi(argv[1]); } - sensor = new upm::TSL2561(); + upm::TSL2561 sensor; - for(int i=0; i< loopCount; i++){ - fprintf(stdout, "Lux = %d\n", sensor->getLux()); + for (int i = 0; i < loopCount; i++) { + fprintf(stdout, "Lux = %d\n", sensor.getLux()); } -//! [Interesting] - - delete(sensor); + //! [Interesting] return (0); } diff --git a/examples/c++/ttp223.cxx b/examples/c++/ttp223.cxx index 88cbcf08..863072aa 100644 --- a/examples/c++/ttp223.cxx +++ b/examples/c++/ttp223.cxx @@ -22,33 +22,33 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "ttp223.hpp" +#include "upm_utilities.h" int -main(int argc, char **argv) +main(int argc, char** argv) { // This example uses GPIO 0 -//! [Interesting] + //! [Interesting] // Create the TTP223 touch sensor object using GPIO pin 0 - upm::TTP223* touch = new upm::TTP223(0); + upm::TTP223 touch(0); // Check whether or not a finger is near the touch sensor and // print accordingly, waiting one second between readings - while( 1 ) { - if ( touch->isPressed() ) { - std::cout << touch->name() << " is pressed" << std::endl; + while (1) { + if (touch.isPressed()) { + std::cout << touch.name() << " is pressed" << std::endl; } else { - std::cout << touch->name() << " is not pressed" << std::endl; + std::cout << touch.name() << " is not pressed" << std::endl; } - sleep(1); + upm_delay(1); } // Delete the touch sensor object - delete touch; -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/uartat-le910.cxx b/examples/c++/uartat-le910.cxx index e8eebeb2..69901260 100644 --- a/examples/c++/uartat-le910.cxx +++ b/examples/c++/uartat-le910.cxx @@ -26,15 +26,15 @@ #include #include "uartat.hpp" -#include "upm_utilities.h" using namespace std; const size_t bufferLength = 256; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] string defaultDev = string("/dev/ttyUSB0"); if (argc > 1) @@ -43,7 +43,7 @@ int main(int argc, char **argv) cout << "Using device: " << defaultDev << endl; // Instantiate a UARTAT sensor on defaultDev at 115200 baud. - upm::UARTAT *sensor = new upm::UARTAT(defaultDev, 115200); + upm::UARTAT sensor(defaultDev, 115200); // This is a simplistic example that tries to configure the LE910, // and use it's built-in socket capabilities to connect to a @@ -66,62 +66,54 @@ int main(int argc, char **argv) // interactive terminal emulator like minicom or screen. // make sure we are in command mode - if (!sensor->inCommandMode()) - { + if (!sensor.inCommandMode()) { cout << "Not in command mode, switching..." << endl; - sensor->commandMode("+++", 1000); + sensor.commandMode("+++", 1000); } // flter out CR's in responses by default - sensor->filterCR(true); + sensor.filterCR(true); cout << "Configuring modem..." << endl; // discard any waiting characters - sensor->drain(); + sensor.drain(); // reset modem - sensor->command("ATZ\r"); + sensor.command("ATZ\r"); // turn off command echo, set verbosity to 1, enable data // connection mode - sensor->command("ATE0 V1 +FCLASS=0\r"); - sensor->drain(); + sensor.command("ATE0 V1 +FCLASS=0\r"); + sensor.drain(); // Now issue some commands and output the results. cout << "Modem and SIM information:" << endl; std::string buffer; - buffer = sensor->commandWithResponse("AT+ICCID\r", bufferLength); + buffer = sensor.commandWithResponse("AT+ICCID\r", bufferLength); if (!buffer.empty()) - cout << "ICCID (SIM ID): " - << buffer - << endl; + cout << "ICCID (SIM ID): " << buffer << endl; - buffer = sensor->commandWithResponse("AT+CGSN=1\r", bufferLength); + buffer = sensor.commandWithResponse("AT+CGSN=1\r", bufferLength); if (!buffer.empty()) - cout << "IMEI: " - << buffer - << endl; + cout << "IMEI: " << buffer << endl; // see if we are on the network.... - buffer = sensor->commandWithResponse("AT+CREG?\r", bufferLength); - if (!buffer.empty()) - { + buffer = sensor.commandWithResponse("AT+CREG?\r", bufferLength); + if (!buffer.empty()) { cout << buffer << endl; // look for "CGREG: 0,1" or "CGREG: 0,5" - if (sensor->find(buffer, "CREG: 0,1") || - sensor->find(buffer, "CREG: 0,5")) - { + if (sensor.find(buffer, "CREG: 0,1") || sensor.find(buffer, "CREG: 0,5")) { cout << "Connected to the cell data network." << endl; // wait up to 5 seconds for responses now... - sensor->setResponseWaitTime(5000); + sensor.setResponseWaitTime(5000); // setup PDP context (socket 1). An ERROR repsonse is // possible if the PDP context is already set up. - sensor->command("AT#SGACT=1,1\r"); + sensor.command("AT#SGACT=1,1\r"); // setup a TCP socket to nist.gov and read the timestamp. @@ -131,45 +123,33 @@ int main(int argc, char **argv) // string, which will be present at the end, if the // connection succeeded and the requested data was // obtained. - buffer = - sensor->commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r", - bufferLength, "\nNO CARRIER\n", 60000); - if (!buffer.empty()) - { + buffer = sensor.commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r", + bufferLength, + "\nNO CARRIER\n", + 60000); + if (!buffer.empty()) { // print out the response - cout << "RESPONSE: " - << endl - << buffer - << endl; - } - else - { + cout << "RESPONSE: " << endl << buffer << endl; + } else { cout << "No response." << endl; } // destroy PDP context - sensor->setResponseWaitTime(250); - sensor->command("AT#SGACT=1,0\r"); + sensor.setResponseWaitTime(250); + sensor.command("AT#SGACT=1,0\r"); + } else { + cout << "You do not appear to be connected to the network..." << endl; } - else - { - cout << "You do not appear to be connected to the network..." - << endl; - } - } - else - { + } else { cout << "Error executing query\n" << endl; } // reset the modem - sensor->command("ATZ\r"); + sensor.command("ATZ\r"); cout << "Exiting" << endl; - delete sensor; - -//! [Interesting] + //! [Interesting] return 0; } diff --git a/examples/c++/uln200xa.cxx b/examples/c++/uln200xa.cxx index 486d20d3..413a916c 100644 --- a/examples/c++/uln200xa.cxx +++ b/examples/c++/uln200xa.cxx @@ -22,41 +22,41 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include + #include "uln200xa.hpp" +#include "upm_utilities.h" using namespace std; -int main () +int +main() { - //! [Interesting] + //! [Interesting] - // Instantiate a Stepper motor on a ULN200XA Darlington controller. + // Instantiate a Stepper motor on a ULN200XA Darlington controller. - // This was tested with the Grove Gear Stepper Motor with Driver + // This was tested with the Grove Gear Stepper Motor with Driver - // Wire the pins so that I1 is pin D8, I2 is pin D9, I3 is pin D10 and - // I4 is pin D11 - upm::ULN200XA* uln200xa = new upm::ULN200XA(4096, 8, 9, 10, 11); + // Wire the pins so that I1 is pin D8, I2 is pin D9, I3 is pin D10 and + // I4 is pin D11 + upm::ULN200XA uln200xa(4096, 8, 9, 10, 11); - uln200xa->setSpeed(5); - uln200xa->setDirection(ULN200XA_DIR_CW); - cout << "Rotating 1 revolution clockwise." << endl; - uln200xa->stepperSteps(4096); - cout << "Sleeping for 2 seconds..." << endl; - sleep(2); - cout << "Rotating 1/2 revolution counter clockwise." << endl; - uln200xa->setDirection(ULN200XA_DIR_CCW); - uln200xa->stepperSteps(2048); + uln200xa.setSpeed(5); + uln200xa.setDirection(ULN200XA_DIR_CW); + cout << "Rotating 1 revolution clockwise." << endl; + uln200xa.stepperSteps(4096); + cout << "Sleeping for 2 seconds..." << endl; + upm_delay(2); + cout << "Rotating 1/2 revolution counter clockwise." << endl; + uln200xa.setDirection(ULN200XA_DIR_CCW); + uln200xa.stepperSteps(2048); - // turn off the power - uln200xa->release(); + // turn off the power + uln200xa.release(); - //! [Interesting] - cout << "Exiting..." << endl; + //! [Interesting] + cout << "Exiting..." << endl; - delete uln200xa; - return 0; + return 0; } - diff --git a/examples/c++/ultrasonic.cxx b/examples/c++/ultrasonic.cxx index 7bdecd65..203ecf99 100644 --- a/examples/c++/ultrasonic.cxx +++ b/examples/c++/ultrasonic.cxx @@ -24,37 +24,35 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include "ultrasonic.hpp" #include -#include -#include +#include + +#include "ultrasonic.hpp" +#include "upm_utilities.h" -upm::UltraSonic *sonar = NULL; bool running = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) running = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { signal(SIGINT, sig_handler); //! [Interesting] // upm::UltraSonic *sonar = NULL; - sonar = new upm::UltraSonic(2); - while(running) - { - int width = sonar->getDistance(); + upm::UltraSonic sonar(2); + while (running) { + int width = sonar.getDistance(); printf("Echo width = %d\n", width); - printf("Distance inches = %f.2\n\n", width/148.0); - sleep(3); + printf("Distance inches = %f.2\n\n", width / 148.0); + upm_delay(3); } //! [Interesting] printf("exiting application\n"); - delete sonar; return 0; } diff --git a/examples/c++/urm37-uart.cxx b/examples/c++/urm37-uart.cxx index 085e84b2..02ecd2a4 100644 --- a/examples/c++/urm37-uart.cxx +++ b/examples/c++/urm37-uart.cxx @@ -22,46 +22,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "urm37.hpp" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a URM37 sensor on UART 0, with the reset pin on D2 - upm::URM37 *sensor = new upm::URM37(0, 2); - - // Every half a second, sample the URM37 and output the measured - // distance in cm, and temperature in degrees C + // Instantiate a URM37 sensor on UART 0, with the reset pin on D2 + upm::URM37 sensor(0, 2); - while (shouldRun) - { - cout << "Detected distance (cm): " << sensor->getDistance() << endl; - cout << "Temperature (C): " << sensor->getTemperature() << endl; - cout << endl; + // Every half a second, sample the URM37 and output the measured + // distance in cm, and temperature in degrees C - usleep(500000); + while (shouldRun) { + cout << "Detected distance (cm): " << sensor.getDistance() << endl; + cout << "Temperature (C): " << sensor.getTemperature() << endl; + cout << endl; + + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/urm37.cxx b/examples/c++/urm37.cxx index a7a7e240..a5899bab 100644 --- a/examples/c++/urm37.cxx +++ b/examples/c++/urm37.cxx @@ -22,44 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "urm37.hpp" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] - // Instantiate a URM37 sensor on analog pin A0, reset pin on D2, - // trigger pin on D3 with an analog reference voltage of 5.0 - upm::URM37 *sensor = new upm::URM37(0, 2, 3, 5.0); + // Instantiate a URM37 sensor on analog pin A0, reset pin on D2, + // trigger pin on D3 with an analog reference voltage of 5.0 + upm::URM37 sensor(0, 2, 3, 5.0); - // Every half a second, sample the URM37 and output the measured - // distance in cm. + // Every half a second, sample the URM37 and output the measured + // distance in cm. - while (shouldRun) - { - cout << "Detected distance (cm): " << sensor->getDistance() << endl; - usleep(500000); + while (shouldRun) { + cout << "Detected distance (cm): " << sensor.getDistance() << endl; + upm_delay_us(500000); } -//! [Interesting] + //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/vcap.cxx b/examples/c++/vcap.cxx index fe0d8a79..b3f7f87d 100644 --- a/examples/c++/vcap.cxx +++ b/examples/c++/vcap.cxx @@ -22,49 +22,48 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include #include "vcap.hpp" using namespace std; -int main(int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] + //! [Interesting] - string defaultDev = "/dev/video0"; + string defaultDev = "/dev/video0"; - // if an argument was specified, use it as the device instead - if (argc > 1) - defaultDev = string(argv[1]); + // if an argument was specified, use it as the device instead + if (argc > 1) + defaultDev = string(argv[1]); - cout << "Using device " << defaultDev << endl; - cout << "Initializing..." << endl; + cout << "Using device " << defaultDev << endl; + cout << "Initializing..." << endl; - // Instantiate an VCAP instance, using the specified video device - upm::VCAP *sensor = new upm::VCAP(defaultDev); + // Instantiate an VCAP instance, using the specified video device + upm::VCAP sensor(defaultDev); - // enable some debug/verbose output - sensor->setDebug(true); + // enable some debug/verbose output + sensor.setDebug(true); - // This is just a hint. The kernel can change this to a lower - // resolution that the hardware supports. Use getWidth() and - // getHeight() methods to see what the kernel actually chose if you - // care. - sensor->setResolution(1920, 1080); + // This is just a hint. The kernel can change this to a lower + // resolution that the hardware supports. Use getWidth() and + // getHeight() methods to see what the kernel actually chose if you + // care. + sensor.setResolution(1920, 1080); - // capture an image - sensor->captureImage(); + // capture an image + sensor.captureImage(); - // convert and save it as a jpeg - sensor->saveImage("video-img1.jpg"); + // convert and save it as a jpeg + sensor.saveImage("video-img1.jpg"); - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; + //! [Interesting] -//! [Interesting] - - return 0; + return 0; } diff --git a/examples/c++/vdiv.cxx b/examples/c++/vdiv.cxx index 6a704bff..d6b3f732 100644 --- a/examples/c++/vdiv.cxx +++ b/examples/c++/vdiv.cxx @@ -22,46 +22,46 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "vdiv.hpp" using namespace std; bool shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Grove Voltage Divider sensor on analog pin A0 - upm::VDiv* vDiv = new upm::VDiv(0); + //! [Interesting] + // Instantiate a Grove Voltage Divider sensor on analog pin A0 + upm::VDiv vDiv(0); - // collect data and output measured voltage according to the setting - // of the scaling switch (3 or 10) - while (shouldRun) - { - unsigned int val = vDiv->value(100); - float gain3val = vDiv->computedValue(3, val); - float gain10val = vDiv->computedValue(10, val); - cout << "ADC value: " << val << " Gain 3: " << gain3val - << "v Gain 10: " << gain10val << "v" << endl; + // collect data and output measured voltage according to the setting + // of the scaling switch (3 or 10) + while (shouldRun) { + unsigned int val = vDiv.value(100); + float gain3val = vDiv.computedValue(3, val); + float gain10val = vDiv.computedValue(10, val); + cout << "ADC value: " << val << " Gain 3: " << gain3val << "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; } diff --git a/examples/c++/veml6070.cxx b/examples/c++/veml6070.cxx index ffb2fc55..6e5e5d4a 100644 --- a/examples/c++/veml6070.cxx +++ b/examples/c++/veml6070.cxx @@ -21,38 +21,39 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include + #include #include + +#include "upm_utilities.h" #include "veml6070.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main () +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate an VEML6070 sensor on i2c bus 0 - upm::VEML6070* veml = new upm::VEML6070(0); + upm::VEML6070 veml(0); while (shouldRun) { - cout << "Retrieved UV value: " << veml->getUVIntensity() << endl; - sleep(1); + cout << "Retrieved UV value: " << veml.getUVIntensity() << endl; + upm_delay(1); } -//! [Interesting] + //! [Interesting] cout << "Exiting..." << endl; - delete veml; return 0; } diff --git a/examples/c++/water.cxx b/examples/c++/water.cxx index c7605451..e8c82267 100644 --- a/examples/c++/water.cxx +++ b/examples/c++/water.cxx @@ -22,44 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "water.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a Water sensor on digital pin D2 - upm::Water* water = new upm::Water(2); - - while (shouldRun) - { - bool val = water->isWet(); - if (val) - cout << "Sensor is wet." << endl; - else - cout << "Sensor is dry." << endl; + //! [Interesting] + // Instantiate a Water sensor on digital pin D2 + upm::Water water(2); - 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; } diff --git a/examples/c++/waterlevel.cxx b/examples/c++/waterlevel.cxx index 301e4891..8ab5dda8 100644 --- a/examples/c++/waterlevel.cxx +++ b/examples/c++/waterlevel.cxx @@ -22,41 +22,42 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include + +#include "upm_utilities.h" #include "waterlevel.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // The was tested with the Water Level Sensor - // Instantiate a Water Level Sensor on digital pin D2 - upm::WaterLevel* waterlevel = new upm::WaterLevel(2); + //! [Interesting] + // The was tested with the Water Level Sensor + // Instantiate a Water Level Sensor on digital pin D2 + upm::WaterLevel waterlevel(2); - while (shouldRun) - { - if (waterlevel->isSubmerged()) - cout << "Sensor is submerged in liquid" << endl; - else - cout << "Liquid is below water level sensor" << endl; - sleep(1); - } + while (shouldRun) { + if (waterlevel.isSubmerged()) + cout << "Sensor is submerged in liquid" << endl; + else + cout << "Liquid is below water level sensor" << endl; + upm_delay(1); + } -//! [Interesting] - cout << "Exiting" << endl; + //! [Interesting] + cout << "Exiting" << endl; - delete waterlevel; - return 0; + return 0; } diff --git a/examples/c++/wfs.cxx b/examples/c++/wfs.cxx index b02072fb..eb4b3ec9 100644 --- a/examples/c++/wfs.cxx +++ b/examples/c++/wfs.cxx @@ -22,43 +22,44 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "wfs.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { if (signo == SIGINT) shouldRun = false; } - -int main() +int +main() { signal(SIGINT, sig_handler); -//! [Interesting] + //! [Interesting] // Instantiate a Water Flow Sensor on digital pin D2. This must // be an interrupt capable pin. - upm::WFS* flow = new upm::WFS(2); + upm::WFS flow(2); // set the flow counter to 0 and start counting - flow->clearFlowCounter(); - flow->startFlowCounter(); + flow.clearFlowCounter(); + flow.startFlowCounter(); - while (shouldRun) - { + while (shouldRun) { // we grab these (millis and flowCount) just for display // purposes in this example - uint32_t millis = flow->getMillis(); - uint32_t flowCount = flow->flowCounter(); + uint32_t millis = flow.getMillis(); + uint32_t flowCount = flow.flowCounter(); - float fr = flow->flowRate(); + float fr = flow.flowRate(); // output milliseconds passed, flow count, and computed flow rate cout << "Millis: " << millis << " Flow Count: " << flowCount; @@ -66,14 +67,13 @@ int main() // best to gather data for at least one second for reasonable // results. - sleep(2); + upm_delay(2); } - flow->stopFlowCounter(); -//! [Interesting] + flow.stopFlowCounter(); + //! [Interesting] cout << "Exiting..." << endl; - delete flow; return 0; } diff --git a/examples/c++/wheelencoder.cxx b/examples/c++/wheelencoder.cxx index 1741b971..432c884d 100644 --- a/examples/c++/wheelencoder.cxx +++ b/examples/c++/wheelencoder.cxx @@ -22,48 +22,47 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "wheelencoder.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main() +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a DFRobot Wheel Encoder on digital pin D2 - upm::WheelEncoder* sensor = new upm::WheelEncoder(2); - - // set the counter to 0 and start counting - sensor->clearCounter(); - sensor->startCounter(); + //! [Interesting] + // Instantiate a DFRobot Wheel Encoder on digital pin D2 + upm::WheelEncoder sensor(2); - while (shouldRun) - { - // output milliseconds passed and current sensor count - cout << "Millis: " << sensor->getMillis() << " Count: " - << sensor->counter() << endl; + // set the counter to 0 and start counting + sensor.clearCounter(); + sensor.startCounter(); - sleep(1); + while (shouldRun) { + // output milliseconds passed and current sensor count + cout << "Millis: " << sensor.getMillis() << " Count: " << sensor.counter() << endl; + + upm_delay(1); } - sensor->stopCounter(); -//! [Interesting] + sensor.stopCounter(); + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete sensor; - return 0; + return 0; } diff --git a/examples/c++/wt5001.cxx b/examples/c++/wt5001.cxx index 92a65815..3324058a 100644 --- a/examples/c++/wt5001.cxx +++ b/examples/c++/wt5001.cxx @@ -22,109 +22,108 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include +#include + #include "wt5001.hpp" using namespace std; -void printUsage(char *progname) +void +printUsage(char* progname) { - cout << "Usage:" << progname << " " << endl; - cout << "Commands:" << endl; - cout << "0 - stop playing" << endl; - cout << "1 - start playing track 1" << endl; - cout << "2 - pause/un-pause playback" << endl; - cout << "3 - next track" << endl; - cout << "4 - previous track" << endl; -} - -int main (int argc, char **argv) -{ -//! [Interesting] - // Instantiate a WT5001 serial MP3 player on uart 0. - // This example was tested on the Grove Serial MP3 module. - - upm::WT5001* mp3 = new upm::WT5001(0); - - int cmd = -1; - if (argc > 1) - cmd = atoi(argv[1]); - - // make sure port is initialized properly. 9600 baud is the default. - if (!mp3->setupTty(B9600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; - } - - switch (cmd) - { - case 0: - mp3->stop(); - break; - - case 1: - mp3->play(upm::WT5001::SD, 1); - break; - - case 2: - mp3->pause(); - break; - - case 3: - mp3->next(); - break; - - case 4: - mp3->previous(); - break; - - default: - // nothing, just output usage, and info below - printUsage(argv[0]); - break; - } - - // Example: set the date - // mp3->setDate(2015, 1, 1); - - // Example: set the time - // mp3->setTime(12, 30, 30); - - // print out some information - uint8_t vol = 0; - if (mp3->getVolume(&vol)) - cout << "The current volume is: " << int(vol) << endl; - - uint8_t ps = 0; - if (mp3->getPlayState(&ps)) - cout << "The current play state is: " << int(ps) << endl; - - uint16_t numf = 0; - if (mp3->getNumFiles(upm::WT5001::SD, &numf)) - cout << "The number of files on the SD card is: " << int(numf) << endl; - - uint16_t curf = 0; - if (mp3->getCurrentFile(&curf)) - cout << "The current file is: " << int(curf) << endl; - - uint16_t year = 0; - uint8_t month = 0, day = 0; - if (mp3->getDate(&year, &month, &day)) - cout << "The device date is: " << int(month) << "/" << int(day) - << "/" << int(year) << endl; - - uint8_t hour = 0, minute = 0, second = 0; - if (mp3->getTime(&hour, &minute, &second)) - cout << "The device time is: " << int(hour) << ":" << int(minute) - << ":" << int(second) << endl; - -//! [Interesting] - - cout << "Exiting..." << endl; - - delete mp3; - return 0; + cout << "Usage:" << progname << " " << endl; + cout << "Commands:" << endl; + cout << "0 - stop playing" << endl; + cout << "1 - start playing track 1" << endl; + cout << "2 - pause/un-pause playback" << endl; + cout << "3 - next track" << endl; + cout << "4 - previous track" << endl; +} + +int +main(int argc, char** argv) +{ + //! [Interesting] + // Instantiate a WT5001 serial MP3 player on uart 0. + // This example was tested on the Grove Serial MP3 module. + + upm::WT5001 mp3(0); + + int cmd = -1; + if (argc > 1) + cmd = atoi(argv[1]); + + // make sure port is initialized properly. 9600 baud is the default. + if (!mp3.setupTty(B9600)) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; + } + + switch (cmd) { + case 0: + mp3.stop(); + break; + + case 1: + mp3.play(upm::WT5001::SD, 1); + break; + + case 2: + mp3.pause(); + break; + + case 3: + mp3.next(); + break; + + case 4: + mp3.previous(); + break; + + default: + // nothing, just output usage, and info below + printUsage(argv[0]); + break; + } + + // Example: set the date + // mp3.setDate(2015, 1, 1); + + // Example: set the time + // mp3.setTime(12, 30, 30); + + // print out some information + uint8_t vol = 0; + if (mp3.getVolume(&vol)) + cout << "The current volume is: " << int(vol) << endl; + + uint8_t ps = 0; + if (mp3.getPlayState(&ps)) + cout << "The current play state is: " << int(ps) << endl; + + uint16_t numf = 0; + if (mp3.getNumFiles(upm::WT5001::SD, &numf)) + cout << "The number of files on the SD card is: " << int(numf) << endl; + + uint16_t curf = 0; + if (mp3.getCurrentFile(&curf)) + cout << "The current file is: " << int(curf) << endl; + + uint16_t year = 0; + uint8_t month = 0, day = 0; + if (mp3.getDate(&year, &month, &day)) + cout << "The device date is: " << int(month) << "/" << int(day) << "/" << int(year) << endl; + + uint8_t hour = 0, minute = 0, second = 0; + if (mp3.getTime(&hour, &minute, &second)) + cout << "The device time is: " << int(hour) << ":" << int(minute) << ":" << int(second) + << endl; + + //! [Interesting] + + cout << "Exiting..." << endl; + + return 0; } diff --git a/examples/c++/xbee.cxx b/examples/c++/xbee.cxx index d56052e4..b6041cdd 100644 --- a/examples/c++/xbee.cxx +++ b/examples/c++/xbee.cxx @@ -22,124 +22,113 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include -#include + #include "xbee.hpp" using namespace std; using namespace upm; -void printUsage(char *progname) +void +printUsage(char* progname) { - cout << "Usage: " << progname << " [AT command]" << endl; - cout << endl; + cout << "Usage: " << progname << " [AT command]" << endl; + cout << endl; - cout << "If an argument is supplied on the command line, that argument is" - << endl; - cout << "sent to the module and the response is printed out." << endl; - cout << endl; - cout << "If no argument is used, then the firmware revision, serial number" - << endl; - cout << "and the current IP address (if set) are queried." << endl; - cout << endl; - cout << endl; + cout << "If an argument is supplied on the command line, that argument is" << endl; + cout << "sent to the module and the response is printed out." << endl; + cout << endl; + cout << "If no argument is used, then the firmware revision, serial number" << endl; + cout << "and the current IP address (if set) are queried." << endl; + cout << endl; + cout << endl; } //! [Interesting] // simple helper function to send a command and wait for a response -void sendCommand(upm::XBee* sensor, string cmd) +void +sendCommand(upm::XBee& sensor, string cmd) { - // commands need to be terminated with a carriage return - cmd += "\r"; + // commands need to be terminated with a carriage return + cmd += "\r"; - sensor->writeDataStr(cmd); - - string resp; - while (sensor->dataAvailable(2000)) - { - resp += sensor->readDataStr(1024); + sensor.writeDataStr(cmd); + + string resp; + while (sensor.dataAvailable(2000)) { + resp += sensor.readDataStr(1024); } - if (resp.empty()) - { - cerr << "Timed out waiting for response" << endl; - return; + if (resp.empty()) { + cerr << "Timed out waiting for response" << endl; + return; } - resp = sensor->stringCR2LF(resp); - cout << "Returned (" << resp.size() << " bytes): " << endl; - cout << resp << endl; + resp = sensor.stringCR2LF(resp); + cout << "Returned (" << resp.size() << " bytes): " << endl; + cout << resp << endl; } -int main(int argc, char **argv) +int +main(int argc, char** argv) { + // Instantiate a XBee Module on UART 0 + upm::XBee sensor(0); - // Instantiate a XBee Module on UART 0 - upm::XBee* sensor = new upm::XBee(0); - - // Set the baud rate, 9600 baud is the default. - if (sensor->setBaudRate(9600)) - { - cerr << "Failed to set tty baud rate" << endl; - return 1; + // Set the baud rate, 9600 baud is the default. + if (sensor.setBaudRate(9600)) { + cerr << "Failed to set tty baud rate" << endl; + return 1; } - printUsage(argv[0]); + printUsage(argv[0]); - if (argc > 1) - { - // enable command mode - sensor->commandMode(); - cout << "Sending command line argument (" << argv[1] << ")..." << endl; - sendCommand(sensor, argv[1]); - } - else - { - // enable command mode - sensor->commandMode(); - // query the verbose firmware revision - cout << "Querying verbose firmware revision (ATVL)..." << endl; - sendCommand(sensor, "ATVL"); - // query the number - cout << "Querying Serial Number High (ATSH)..." << endl; - sendCommand(sensor, "ATSH"); - cout << "Querying Serial Number Low (ATSL)..." << endl; - sendCommand(sensor, "ATSL"); + if (argc > 1) { + // enable command mode + sensor.commandMode(); + cout << "Sending command line argument (" << argv[1] << ")..." << endl; + sendCommand(sensor, argv[1]); + } else { + // enable command mode + sensor.commandMode(); + // query the verbose firmware revision + cout << "Querying verbose firmware revision (ATVL)..." << endl; + sendCommand(sensor, "ATVL"); + // query the number + cout << "Querying Serial Number High (ATSH)..." << endl; + sendCommand(sensor, "ATSH"); + cout << "Querying Serial Number Low (ATSL)..." << endl; + sendCommand(sensor, "ATSL"); - cout << "Querying address, if set (ATMY)..." << endl; - sendCommand(sensor, "ATMY"); + cout << "Querying address, if set (ATMY)..." << endl; + sendCommand(sensor, "ATMY"); - // For the XBee WiFi S6B - // A comprehensive list of commands and command modes is - // available from the datasheet at: - // ftp1.digi.com/support/documentation/90002180_L.pdf + // For the XBee WiFi S6B + // A comprehensive list of commands and command modes is + // available from the datasheet at: + // ftp1.digi.com/support/documentation/90002180_L.pdf - // For the XBee S1 - // A comprehensive list of commands and command modes is - // available from the datasheet at: - // http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf + // For the XBee S1 + // A comprehensive list of commands and command modes is + // available from the datasheet at: + // http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf - // For the XBee WiFi module: - // An example using AT commands to connect to an AP, with a - // private Key using WPA2: + // For the XBee WiFi module: + // An example using AT commands to connect to an AP, with a + // private Key using WPA2: - // Connect to AP with SSID 'mySSID': - // ATIDmySSID + // Connect to AP with SSID 'mySSID': + // ATIDmySSID - // Provide the private key 'secret': - // ATPKsecret - - // Use WPA2 encryption - // ATEE2 + // Provide the private key 'secret': + // ATPKsecret + // Use WPA2 encryption + // ATEE2 } - - delete sensor; - return 0; + return 0; } //! [Interesting] diff --git a/examples/c++/yg1006.cxx b/examples/c++/yg1006.cxx index 6b944218..ce619a11 100644 --- a/examples/c++/yg1006.cxx +++ b/examples/c++/yg1006.cxx @@ -22,45 +22,45 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include + +#include "upm_utilities.h" #include "yg1006.hpp" using namespace std; int shouldRun = true; -void sig_handler(int signo) +void +sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } - -int main () +int +main() { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); -//! [Interesting] - // Instantiate a yg1006 flame sensor on digital pin D2 - upm::YG1006* flame = new upm::YG1006(2); - - // check every second for the presence of a flame - while (shouldRun) - { - bool val = flame->flameDetected(); - if (val) - cout << "Flame detected." << endl; - else - cout << "No flame detected." << endl; + //! [Interesting] + // Instantiate a yg1006 flame sensor on digital pin D2 + upm::YG1006 flame(2); - sleep(1); + // check every second for the presence of a flame + while (shouldRun) { + bool val = flame.flameDetected(); + if (val) + cout << "Flame detected." << endl; + else + cout << "No flame detected." << endl; + + upm_delay(1); } -//! [Interesting] + //! [Interesting] - cout << "Exiting..." << endl; + cout << "Exiting..." << endl; - delete flame; - return 0; + return 0; } diff --git a/examples/c++/zfm20-register.cxx b/examples/c++/zfm20-register.cxx index c1f68d77..6104c483 100644 --- a/examples/c++/zfm20-register.cxx +++ b/examples/c++/zfm20-register.cxx @@ -22,119 +22,113 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include -#include +#include +#include + +#include "upm_utilities.h" #include "zfm20.hpp" using namespace std; using namespace upm; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a ZFM20 Fingerprint reader on UART 0 + //! [Interesting] + // Instantiate a ZFM20 Fingerprint reader on UART 0 - upm::ZFM20* fp = new upm::ZFM20(0); + upm::ZFM20 fp(0); - // make sure port is initialized properly. 57600 baud is the default. - if (!fp->setupTty(B57600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 57600 baud is the default. + if (!fp.setupTty(B57600)) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - // This example demonstrates registering a fingerprint on the zfm20 - // module. The procedure is as follows: - // - // 1. get an image, store it in characteristics buffer 1 - // 2. get another image, store it in characteristics buffer 2 - // 3. store the image, assuming the two fingerprints match + // This example demonstrates registering a fingerprint on the zfm20 + // module. The procedure is as follows: + // + // 1. get an image, store it in characteristics buffer 1 + // 2. get another image, store it in characteristics buffer 2 + // 3. store the image, assuming the two fingerprints match - // first, we need to register our address and password - - fp->setPassword(ZFM20_DEFAULT_PASSWORD); - fp->setAddress(ZFM20_DEFAULT_ADDRESS); - - // now verify the password. If this fails, any other commands - // will be ignored, so we just bail. - if (fp->verifyPassword()) - { - cout << "Password verified." << endl; - } - else - { - cerr << "Password verification failed." << endl; - return 1; - } - - cout << endl; + // first, we need to register our address and password - uint8_t rv; - // get the first image + fp.setPassword(ZFM20_DEFAULT_PASSWORD); + fp.setAddress(ZFM20_DEFAULT_ADDRESS); - cout << "Place a finger on the sensor." << endl; - while (fp->generateImage() != ZFM20::ERR_OK) - ; - - // in theory, we have an image - cout << "Image captured, converting..." << endl; - - if ((rv = fp->image2Tz(1)) != ZFM20::ERR_OK) - { - cerr << "Image conversion failed with error code " << int(rv) <generateImage() != ZFM20::ERR_NO_FINGER) - ; - - cout << endl; - cout << "Now place the same finger on the sensor." << endl; + uint8_t rv; + // get the first image - while (fp->generateImage() != ZFM20::ERR_OK) - ; + cout << "Place a finger on the sensor." << endl; + while (fp.generateImage() != ZFM20::ERR_OK) + ; - cout << "Image captured, converting..." << endl; + // in theory, we have an image + cout << "Image captured, converting..." << endl; - // save this one in slot 2 - if ((rv = fp->image2Tz(2)) != ZFM20::ERR_OK) - { - cerr << "Image conversion failed with error code " << int(rv) <createModel()) != ZFM20::ERR_OK) - { - if (rv == ZFM20::ERR_FP_ENROLLMISMATCH) - cerr << "Fingerprints did not match." << endl; - else - cerr << "createModel failed with error code " << int(rv) <storeModel(1, 1)) != ZFM20::ERR_OK) - { - cerr << "storeModel failed with error code " << int(rv) < #include -#include + #include "zfm20.hpp" using namespace std; using namespace upm; -int main (int argc, char **argv) +int +main(int argc, char** argv) { -//! [Interesting] - // Instantiate a ZFM20 Fingerprint reader on UART 0 + //! [Interesting] + // Instantiate a ZFM20 Fingerprint reader on UART 0 - upm::ZFM20* fp = new upm::ZFM20(0); + upm::ZFM20 fp(0); - // make sure port is initialized properly. 57600 baud is the default. - if (!fp->setupTty(B57600)) - { - cerr << "Failed to setup tty port parameters" << endl; - return 1; + // make sure port is initialized properly. 57600 baud is the default. + if (!fp.setupTty(B57600)) { + cerr << "Failed to setup tty port parameters" << endl; + return 1; } - // first, set the default password and address - fp->setPassword(ZFM20_DEFAULT_PASSWORD); - fp->setAddress(ZFM20_DEFAULT_ADDRESS); - - // now verify the password. If this fails, any other commands - // will be ignored, so we just bail. - if (fp->verifyPassword()) - { - cout << "Password verified." << endl; - } - else - { - cerr << "Password verification failed." << endl; - return 1; + // first, set the default password and address + fp.setPassword(ZFM20_DEFAULT_PASSWORD); + fp.setAddress(ZFM20_DEFAULT_ADDRESS); + + // now verify the password. If this fails, any other commands + // will be ignored, so we just bail. + if (fp.verifyPassword()) { + cout << "Password verified." << endl; + } else { + cerr << "Password verification failed." << endl; + return 1; } - // how many valid stored templates (fingerprints) do we have? - cout << "Total stored templates: " << fp->getNumTemplates() << endl; - cout << endl; + // how many valid stored templates (fingerprints) do we have? + cout << "Total stored templates: " << fp.getNumTemplates() << endl; + cout << endl; - // now spin waiting for a fingerprint to successfully image - cout << "Waiting for finger print..." << endl; + // now spin waiting for a fingerprint to successfully image + cout << "Waiting for finger print..." << endl; - while (fp->generateImage() == ZFM20::ERR_NO_FINGER) - ; + while (fp.generateImage() == ZFM20::ERR_NO_FINGER) + ; - // in theory, we have an image - cout << "Image captured, converting..." << endl; + // in theory, we have an image + cout << "Image captured, converting..." << endl; - uint8_t rv; - if ((rv = fp->image2Tz(1)) != ZFM20::ERR_OK) - { - cerr << "Image conversion failed with error code " << int(rv) <search(1, &id, &score)) != ZFM20::ERR_OK) - { - if (rv == ZFM20::ERR_FP_NOTFOUND) - { - cout << "Finger Print not found" << endl; - return 0; - } - else - { - cerr << "Search failed with error code " << int(rv) < +#include "lib_aci.h" + #ifndef H_ACI_SETUP #define H_ACI_SETUP diff --git a/src/rsc/rsc.hpp b/src/rsc/rsc.hpp index 606bf3f5..802bdd2b 100644 --- a/src/rsc/rsc.hpp +++ b/src/rsc/rsc.hpp @@ -25,6 +25,7 @@ #pragma once #include "rsc.h" +#include namespace upm { /**