Generalized adaptation of the WiiChuk_compat library.
Fork of WiiChuk_compat by
Diff: WiiChuck.h
- Revision:
- 3:fcc2f24d0644
- Parent:
- 2:9c4ac1a64c30
- Child:
- 4:e39bbb89c4e9
--- a/WiiChuck.h Sat Dec 20 01:22:58 2014 +0000 +++ b/WiiChuck.h Sat Dec 20 21:45:03 2014 +0000 @@ -33,29 +33,30 @@ #include "mbed.h" -#define NUNCHUCK_ADDR 0xA4 // 0x52 << 1 -#define NUNCHUCK_READLEN 0x06 // -#define I2C_ACK 0 -#define I2C_READ_DELAY 0.0001 - -#define JOY_X_IDX 0 -#define JOY_Y_IDX 1 -#define ACC_X_IDX 2 -#define ACC_Y_IDX 3 -#define ACC_Z_IDX 4 -#define BUTTON_IDX 5 +#define NUNCHUCK_ADDR 0xA4 +#define NUNCHUCK_READLEN 0x06 +#define I2C_ACK 0 +#define I2C_READ_DELAY 0.0001 +#define I2C_DEFAULT_FREQUENCY 400000 -// ---------------------------------------------------------------------------- -// Control values -// ---------------------------------------------------------------------------- -#define BUTTON_PREV_VALUE '1' -#define BUTTON_NEXT_VALUE '2' -#define BUTTON_OK_VALUE '3' -#define BUTTON_CANCEL_VALUE '4' -#define BUTTON_RESET_VALUE 'r' -#define BUTTON_VOLUME_PLUS '+' -#define BUTTON_VOLUME_MINUS '-' +#define JOY_X_IDX 0 +#define JOY_Y_IDX 1 +#define ACC_X_IDX 2 +#define ACC_Y_IDX 3 +#define ACC_Z_IDX 4 +#define BUTTON_IDX 5 +/** + * @struct nunchuck_data_s + * @brief Data structure used for holding nunchuck data + * @var joyX Single byte indicating the position of the joystick in the X axis + * @var joyY Single byte indicating the position of the joystick in the Y axis + * @var accX 10-bit value representing the acceleration in the X axis + * @var accY 10-bit value representing the acceleration in the Y axis + * @var accZ 10-bit value representing the acceleration in the Z axis + * @var buttonC Pressed state of the C button + * @var buttonZ Pressed state of the Z button + */ typedef struct nunchuck_data_s { uint8_t joyX; uint8_t joyY; @@ -66,6 +67,9 @@ bool buttonZ; } nunchuck_data_t; +/** + * @typedef pt2Func Callback function typedef + */ typedef void(*pt2Func)(nunchuck_data_t*); /** @@ -77,7 +81,7 @@ * @param data I2C data pin * @param clk I2C clock pin */ - WiiChuck(PinName data, PinName clk); + WiiChuck(PinName data, PinName clk, uint32_t i2cFrequency=I2C_DEFAULT_FREQUENCY); /** * Reads a packet of data from the nunchuck and fills in the supplied data structure. @@ -85,16 +89,27 @@ * @param data Pointer to the data that will be filled in. */ bool read(nunchuck_data_t* data); - void attach(pt2Func, float t); + + /** + * Attach a callback function that can be called when new data is available. + * + * @param callback Pointer to function to be called when data has been read + * @param readInterval Time interval in seconds to perform successive reads from the nunchuck + */ + void attach(pt2Func callback, float readInterval); + + /** + * Detach the callback and stop polling the nunchuck + */ void detach(); - bool Error; +private: + void getValues(); -private: I2C _i2c; pt2Func _callback; Ticker _getValues; - void getValues(); + bool _initialized; nunchuck_data_t _data; };