Home | History | Annotate | Download | only in nunchuck
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) ics.com>
      3  * Copyright (c) 2015 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 #pragma once
     25 
     26 #include <string>
     27 #include <mraa/i2c.h>
     28 
     29 #define NUNCHUCK_I2C_ADDR    0x52
     30 
     31 namespace upm {
     32 
     33   /**
     34    * @brief Wii Nunchuk library
     35    * @defgroup nunchuck libupm-nunchuck
     36    * @ingroup generic i2c accelerometer robok
     37    */
     38   /**
     39    * @library nunchuck
     40    * @sensor nunchuck
     41    * @comname Wii Nunchuk
     42    * @type accelerometer
     43    * @man generic
     44    * @web http://wiibrew.org/wiki/Wiimote/Extension_Controllers
     45    * @con i2c
     46    * @kit robok
     47    *
     48    * @brief API for the Wii* Nunchuk controller
     49    *
     50    * UPM module for the Wii Nunchuk controller. This module was tested with
     51    * Wii Nunchuk connected to I2C via a Grove Wii Nunchuk adapter.
     52    *
     53    * See http://wiibrew.org/wiki/Wiimote/Extension_Controllers and
     54    * http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck
     55    * for more details on the controller and its protocol.
     56    *
     57    * A warning for the Grove Wii Nunchuk adapter: it has 2 traces on one
     58    * side, and 3 traces on the other.  Do not match these up with the
     59    * Nunchuk connector's traces. The connector's 'Grove'
     60    * should be on the same side as the Grove interface socket on the
     61    * adapter.
     62    *
     63    * @image html nunchuck.jpg
     64    * @snippet nunchuck.cxx Interesting
     65    */
     66   class NUNCHUCK {
     67   public:
     68     /**
     69      * NUNCHUCK constructor
     70      *
     71      * @param bus I2C bus to use
     72      * @param addr I2C address to use
     73      */
     74     NUNCHUCK(int bus, uint8_t addr=NUNCHUCK_I2C_ADDR);
     75 
     76     /**
     77      * NUNCHUCK destructor
     78      */
     79     ~NUNCHUCK();
     80 
     81     /**
     82      * Writes value(s) into registers
     83      *
     84      * @param reg Register location to start writing into
     85      * @param byte Byte to write
     86      * @return True if successful
     87      */
     88     bool writeByte(uint8_t reg, uint8_t byte);
     89 
     90     /**
     91      * Reads value(s) from registers
     92      *
     93      * @param reg Register location to start reading from
     94      * @param buffer Buffer for data storage
     95      * @param len Number of bytes to read
     96      * @return Number of bytes read
     97      */
     98     int readBytes(uint8_t reg, uint8_t *buffer, int len);
     99 
    100     /**
    101      * Initializes the controller. Here, we disable encryption after
    102      * delaying for a time to ensure the controller is ready.
    103      *
    104      * @return True if initialization is successful
    105      */
    106     bool init();
    107 
    108     /**
    109      * Reads and updates the current state of the controller.
    110      *
    111      */
    112     void update();
    113 
    114     /**
    115      * Current analog stick X position
    116      *
    117      */
    118     int stickX;
    119 
    120     /**
    121      * Current analog stick Y position
    122      *
    123      */
    124     int stickY;
    125 
    126     /**
    127      * Current accelerometer X value
    128      *
    129      */
    130     int accelX;
    131 
    132     /**
    133      * Current accelerometer Y value
    134      *
    135      */
    136     int accelY;
    137 
    138     /**
    139      * Current accelerometer Z value
    140      *
    141      */
    142     int accelZ;
    143 
    144     /**
    145      * Button C pressed?
    146      *
    147      */
    148     bool buttonC;
    149 
    150     /**
    151      * Button Z pressed?
    152      *
    153      */
    154     bool buttonZ;
    155 
    156   private:
    157     mraa_i2c_context m_i2c;
    158   };
    159 }
    160 
    161 
    162