Skip to content

Commit 2a17fe0

Browse files
committed
kx122: Added C and C++ examples
Signed-off-by: Samuli Rissanen <samuli.rissanen@hotmail.com> Signed-off-by: Noel Eck <noel.eck@intel.com>
1 parent 8f99289 commit 2a17fe0

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

examples/c++/kx122.cxx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Author: Samuli Rissanen <samuli.rissanen@hotmail.com>
5+
* Copyright (c) 2018 Rohm Semiconductor.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
* this software and associated documentation files (the "Software"), to deal in
9+
* the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
* the Software, and to permit persons to whom the Software is furnished to do so,
12+
* subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
#include <iostream>
26+
27+
#include <signal.h>
28+
#include <unistd.h>
29+
30+
#include "kx122.hpp"
31+
32+
bool shouldRun = true;
33+
34+
void sig_handler(int signo)
35+
{
36+
if (signo == SIGINT){
37+
shouldRun = false;
38+
}
39+
}
40+
41+
int main(int argc, char **argv)
42+
{
43+
signal(SIGINT,sig_handler);
44+
45+
upm::KX122 *sensor = new upm::KX122(0,-1,24);
46+
47+
sensor->softwareReset();
48+
sensor->deviceInit(KX122_ODR_50,HIGH_RES,KX122_RANGE_2G);
49+
50+
float x,y,z;
51+
float wait_time = sensor->getSamplePeriod() * MICRO_S;
52+
53+
while(shouldRun){
54+
sensor->getAccelerationData(&x,&y,&z);
55+
56+
printf("%.02f | %.02f | %.02f\n",x,y,z);
57+
usleep(wait_time);
58+
}
59+
60+
delete sensor;
61+
sensor = nullptr;
62+
63+
return 0;
64+
}

examples/c/kx122.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Author: Samuli Rissanen <samuli.rissanen@hotmail.com>
5+
* Copyright (c) 2018 Rohm Semiconductor.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
* this software and associated documentation files (the "Software"), to deal in
9+
* the Software without restriction, including without limitation the rights to
10+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
* the Software, and to permit persons to whom the Software is furnished to do so,
12+
* subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
#include <stdio.h>
26+
#include <unistd.h>
27+
#include <signal.h>
28+
29+
#include "kx122.h"
30+
31+
bool shouldRun = true;
32+
33+
void sig_handler(int signo)
34+
{
35+
if(signo == SIGINT){
36+
shouldRun = false;
37+
}
38+
}
39+
40+
int main(int argc, char **argv)
41+
{
42+
signal(SIGINT,sig_handler);
43+
kx122_context sensor = kx122_init(0,-1,24);
44+
if (!sensor)
45+
{
46+
printf("kx122_init() failed.\n");
47+
return 1;
48+
}
49+
50+
kx122_sensor_software_reset(sensor);
51+
kx122_device_init(sensor,KX122_ODR_50,HIGH_RES,KX122_RANGE_2G);
52+
53+
float x,y,z;
54+
float wait_time = (kx122_get_sample_period(sensor) * MICRO_S);
55+
56+
while(shouldRun){
57+
kx122_get_acceleration_data(sensor,&x,&y,&z);
58+
59+
printf("%.02f | %.02f | %.02f\n",x,y,z);
60+
usleep(wait_time);
61+
}
62+
63+
kx122_close(sensor);
64+
return 0;
65+
}

0 commit comments

Comments
 (0)