upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Data Structures | Functions | Typedefs
Include dependency graph for nunchuck.h:

API Description

/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 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 <stdio.h>
#include <signal.h>
#include <upm_utilities.h>
#include "nunchuck.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
// Instantiate a nunchuck controller bus 3
if (!sensor)
{
printf("%s: nunchuck_init() failed\n", __FUNCTION__);
return 1;
}
while (shouldRun)
{
if (nunchuck_update(sensor))
{
printf("%s: nunchuck_update() failed\n", __FUNCTION__);
nunchuck_close(sensor);
return 1;
}
int x, y, z;
nunchuck_get_stick(sensor, &x, &y);
printf("stickX: %d stickY: %d\n", x, y);
nunchuck_get_acceleration(sensor, &x, &y, &z);
printf("accelX: %d accelY: %d accelZ: %d\n", x, y, z);
bool bc, bz;
nunchuck_get_buttons(sensor, &bc, &bz);
printf("button C: %s\n",
((bc) ? "pressed" : "not pressed"));
printf("button Z: %s\n\n",
((bz) ? "pressed" : "not pressed"));
upm_delay_ms(100);
}
nunchuck_close(sensor);
return 0;
}

Go to the source code of this file.

Data Structures

struct  _nunchuck_context
 

Functions

nunchuck_context nunchuck_init (int bus)
 
void nunchuck_close (nunchuck_context dev)
 
upm_result_t nunchuck_update (const nunchuck_context dev)
 
void nunchuck_get_stick (const nunchuck_context dev, int *x, int *y)
 
void nunchuck_get_acceleration (const nunchuck_context dev, int *x, int *y, int *z)
 
void nunchuck_get_buttons (const nunchuck_context dev, bool *c, bool *z)
 

Typedefs

typedef struct _nunchuck_contextnunchuck_context
 

Function Documentation

nunchuck_context nunchuck_init ( int  bus)

NUNCHUCK constructor

Parameters
busI2C bus to use
Returns
A Device context, or NULL on error

Here is the call graph for this function:

void nunchuck_close ( nunchuck_context  dev)

Close the device and deallocate all resources.

Parameters
devDevice context

Here is the caller graph for this function:

upm_result_t nunchuck_update ( const nunchuck_context  dev)

Reads and updates the current state of the controller.

Parameters
devDevice context
Returns
UPM result

Here is the caller graph for this function:

void nunchuck_get_stick ( const nunchuck_context  dev,
int *  x,
int *  y 
)

Returns the current analog stick X and Y positions. nunchuck_update() must have been called prior to calling this function.

Parameters
devDevice context
xPointer in which the X value will be stored
yPointer in which the Y value will be stored

Here is the caller graph for this function:

void nunchuck_get_acceleration ( const nunchuck_context  dev,
int *  x,
int *  y,
int *  z 
)

Returns the current accelerometer values. nunchuck_update() must have been called prior to calling this function.

Parameters
devDevice context
xPointer in which the X value will be stored
yPointer in which the Y value will be stored
zPointer in which the Z value will be stored

Here is the caller graph for this function:

void nunchuck_get_buttons ( const nunchuck_context  dev,
bool *  c,
bool *  z 
)

Returns the current button values values. nunchuck_update() must have been called prior to calling this function.

Parameters
devDevice context
cPointer in which the C button value will be stored
zPointer in which the Z button value will be stored

Here is the caller graph for this function:

Typedef Documentation

Device context