Private
Public Access
2
0

examples: added more cpp examples.

Mostly just the same functionality as the C examples yet using the cpp
api.

Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
Thomas Ingleby
2014-05-22 14:17:33 +01:00
parent bd1fabab5e
commit 8d21d477b8
4 changed files with 278 additions and 0 deletions

View File

@@ -1,7 +1,13 @@
add_executable (AioA0 AioA0.cpp)
add_executable (blink-io-cpp Blink-IO.cpp)
add_executable (Pwm3-cycle Pwm3-cycle.cpp)
add_executable (I2c-compass I2c-compass.cpp)
add_executable (Spi-pot Spi-pot.cpp)
include_directories(${PROJECT_SOURCE_DIR}/api)
target_link_libraries (AioA0 maa stdc++)
target_link_libraries (blink-io-cpp maa stdc++)
target_link_libraries (Pwm3-cycle maa stdc++)
target_link_libraries (I2c-compass maa stdc++)
target_link_libraries (Spi-pot maa stdc++)

View File

@@ -0,0 +1,134 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <signal.h>
#include "i2c.hpp"
#include "math.h"
#define MAX_BUFFER_LENGTH 6
#define HMC5883L_I2C_ADDR 0x1E
//configuration registers
#define HMC5883L_CONF_REG_A 0x00
#define HMC5883L_CONF_REG_B 0x01
//mode register
#define HMC5883L_MODE_REG 0x02
//data register
#define HMC5883L_X_MSB_REG 0
#define HMC5883L_X_LSB_REG 1
#define HMC5883L_Z_MSB_REG 2
#define HMC5883L_Z_LSB_REG 3
#define HMC5883L_Y_MSB_REG 4
#define HMC5883L_Y_LSB_REG 5
#define DATA_REG_SIZE 6
//status register
#define HMC5883L_STATUS_REG 0x09
//ID registers
#define HMC5883L_ID_A_REG 0x0A
#define HMC5883L_ID_B_REG 0x0B
#define HMC5883L_ID_C_REG 0x0C
#define HMC5883L_CONT_MODE 0x00
#define HMC5883L_DATA_REG 0x03
//scales
#define GA_0_88_REG 0x00 << 5
#define GA_1_3_REG 0x01 << 5
#define GA_1_9_REG 0x02 << 5
#define GA_2_5_REG 0x03 << 5
#define GA_4_0_REG 0x04 << 5
#define GA_4_7_REG 0x05 << 5
#define GA_5_6_REG 0x06 << 5
#define GA_8_1_REG 0x07 << 5
//digital resolutions
#define SCALE_0_73_MG 0.73
#define SCALE_0_92_MG 0.92
#define SCALE_1_22_MG 1.22
#define SCALE_1_52_MG 1.52
#define SCALE_2_27_MG 2.27
#define SCALE_2_56_MG 2.56
#define SCALE_3_03_MG 3.03
#define SCALE_4_35_MG 4.35
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing PWM nicely\n");
running = -1;
}
}
int main ()
{
maa::I2c* i2c;
i2c = new maa::I2c(0);
float direction = 0;
int16_t x = 0, y = 0, z = 0;
char rx_tx_buf[MAX_BUFFER_LENGTH];
i2c->address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG;
i2c->write(rx_tx_buf, 2);
signal(SIGINT, sig_handler);
while (running == 0) {
i2c->address(HMC5883L_I2C_ADDR);
i2c->write_byte(HMC5883L_DATA_REG);
i2c->address(HMC5883L_I2C_ADDR);
i2c->read(rx_tx_buf, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ;
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ;
y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Y_LSB_REG] ;
//scale and calculate direction
direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
//check if the signs are reversed
if (direction < 0)
direction += 2 * M_PI;
printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG, z * SCALE_0_92_MG) ;
printf("Heading : %f\n", direction * 180/M_PI);
sleep(1);
}
delete i2c;
return MAA_SUCCESS;
}

View File

@@ -0,0 +1,65 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <signal.h>
#include "pwm.hpp"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing PWM nicely\n");
running = -1;
}
}
int main ()
{
maa::Pwm* pwm;
pwm = new maa::Pwm(3);
if (pwm == NULL) {
return MAA_ERROR_UNSPECIFIED;
}
fprintf(stdout, "Cycling PWM on IO3 (pwm3) \n");
signal(SIGINT, sig_handler);
float value = 0.0f;
while (running == 0) {
value = value + 0.01f;
pwm->write(value);
usleep(50000);
if (value >= 1.0f) {
value = 0.0f;
}
}
delete pwm;
return MAA_SUCCESS;
}

73
examples/c++/Spi-pot.cpp Normal file
View File

@@ -0,0 +1,73 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#include "spi.hpp"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing spi nicely\n");
running = -1;
}
}
int main ()
{
maa::Spi* spi;
spi = new maa::Spi(0);
signal(SIGINT, sig_handler);
uint8_t data[] = {0x00, 100};
uint8_t *recv;
while (running == 0) {
int i;
for (i = 90; i < 130; i++) {
data[1] = i;
recv = spi->write_buf(data, 2);
printf("Writing -%i",i);
printf("RECIVED-%i-%i\n",recv[0],recv[1]);
usleep(100000);
}
for (i = 130; i > 90; i--) {
data[1] = i;
recv = spi->write_buf(data, 2);
printf("Writing -%i",i);
printf("RECIVED-%i-%i\n",recv[0],recv[1]);
usleep(100000);
}
}
delete spi;
return MAA_SUCCESS;
}