MAX3421E-based USB Host Shield Library

Dependents:   UsbHostMAX3421E_Hello

Committer:
hudakz
Date:
Sun Jul 12 20:39:26 2020 +0000
Revision:
0:84353c479782
MAX3421E-based USB Host Shield Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:84353c479782 1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
hudakz 0:84353c479782 2
hudakz 0:84353c479782 3 This software may be distributed and modified under the terms of the GNU
hudakz 0:84353c479782 4 General Public License version 2 (GPL2) as published by the Free Software
hudakz 0:84353c479782 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
hudakz 0:84353c479782 6 this file. Please note that GPL2 Section 2[b] requires that all works based
hudakz 0:84353c479782 7 on this software must also be made publicly available under the terms of
hudakz 0:84353c479782 8 the GPL2 ("Copyleft").
hudakz 0:84353c479782 9
hudakz 0:84353c479782 10 Contact information
hudakz 0:84353c479782 11 -------------------
hudakz 0:84353c479782 12
hudakz 0:84353c479782 13 Kristian Lauszus, TKJ Electronics
hudakz 0:84353c479782 14 Web : http://www.tkjelectronics.com
hudakz 0:84353c479782 15 e-mail : kristianl@tkjelectronics.com
hudakz 0:84353c479782 16
hudakz 0:84353c479782 17 IR camera support added by Allan Glover (adglover9.81@gmail.com) and Kristian Lauszus
hudakz 0:84353c479782 18 */
hudakz 0:84353c479782 19
hudakz 0:84353c479782 20 #ifndef _wii_h_
hudakz 0:84353c479782 21 #define _wii_h_
hudakz 0:84353c479782 22
hudakz 0:84353c479782 23 #include "BTD.h"
hudakz 0:84353c479782 24 #include "controllerEnums.h"
hudakz 0:84353c479782 25
hudakz 0:84353c479782 26 /* Wii event flags */
hudakz 0:84353c479782 27 #define WII_FLAG_MOTION_PLUS_CONNECTED (1 << 0)
hudakz 0:84353c479782 28 #define WII_FLAG_NUNCHUCK_CONNECTED (1 << 1)
hudakz 0:84353c479782 29 #define WII_FLAG_CALIBRATE_BALANCE_BOARD (1 << 2)
hudakz 0:84353c479782 30
hudakz 0:84353c479782 31 #define wii_check_flag(flag) (wii_event_flag & (flag))
hudakz 0:84353c479782 32 #define wii_set_flag(flag) (wii_event_flag |= (flag))
hudakz 0:84353c479782 33 #define wii_clear_flag(flag) (wii_event_flag &= ~(flag))
hudakz 0:84353c479782 34
hudakz 0:84353c479782 35 /** Enum used to read the joystick on the Nunchuck. */
hudakz 0:84353c479782 36 enum HatEnum {
hudakz 0:84353c479782 37 /** Read the x-axis on the Nunchuck joystick. */
hudakz 0:84353c479782 38 HatX = 0,
hudakz 0:84353c479782 39 /** Read the y-axis on the Nunchuck joystick. */
hudakz 0:84353c479782 40 HatY = 1,
hudakz 0:84353c479782 41 };
hudakz 0:84353c479782 42
hudakz 0:84353c479782 43 /** Enum used to read the weight on Wii Balance Board. */
hudakz 0:84353c479782 44 enum BalanceBoardEnum {
hudakz 0:84353c479782 45 TopRight = 0,
hudakz 0:84353c479782 46 BotRight = 1,
hudakz 0:84353c479782 47 TopLeft = 2,
hudakz 0:84353c479782 48 BotLeft = 3,
hudakz 0:84353c479782 49 };
hudakz 0:84353c479782 50
hudakz 0:84353c479782 51 /**
hudakz 0:84353c479782 52 * This BluetoothService class implements support for the Wiimote including the Nunchuck and Motion Plus extension.
hudakz 0:84353c479782 53 *
hudakz 0:84353c479782 54 * It also support the Wii U Pro Controller.
hudakz 0:84353c479782 55 */
hudakz 0:84353c479782 56 class WII : public BluetoothService {
hudakz 0:84353c479782 57 public:
hudakz 0:84353c479782 58 /**
hudakz 0:84353c479782 59 * Constructor for the WII class.
hudakz 0:84353c479782 60 * @param p Pointer to BTD class instance.
hudakz 0:84353c479782 61 * @param pair Set this to true in order to pair with the Wiimote. If the argument is omitted then it won't pair with it.
hudakz 0:84353c479782 62 * One can use ::PAIR to set it to true.
hudakz 0:84353c479782 63 */
hudakz 0:84353c479782 64 WII(BTD *p, bool pair = false);
hudakz 0:84353c479782 65
hudakz 0:84353c479782 66 /** @name BluetoothService implementation */
hudakz 0:84353c479782 67 /** Used this to disconnect any of the controllers. */
hudakz 0:84353c479782 68 void disconnect();
hudakz 0:84353c479782 69 /**@}*/
hudakz 0:84353c479782 70
hudakz 0:84353c479782 71 /** @name Wii Controller functions */
hudakz 0:84353c479782 72 /**
hudakz 0:84353c479782 73 * getButtonPress(Button b) will return true as long as the button is held down.
hudakz 0:84353c479782 74 *
hudakz 0:84353c479782 75 * While getButtonClick(Button b) will only return it once.
hudakz 0:84353c479782 76 *
hudakz 0:84353c479782 77 * So you instance if you need to increase a variable once you would use getButtonClick(Button b),
hudakz 0:84353c479782 78 * but if you need to drive a robot forward you would use getButtonPress(Button b).
hudakz 0:84353c479782 79 * @param b ::ButtonEnum to read.
hudakz 0:84353c479782 80 * @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
hudakz 0:84353c479782 81 */
hudakz 0:84353c479782 82 bool getButtonPress(ButtonEnum b);
hudakz 0:84353c479782 83 bool getButtonClick(ButtonEnum b);
hudakz 0:84353c479782 84 /**@}*/
hudakz 0:84353c479782 85
hudakz 0:84353c479782 86 /** @name Wii Controller functions */
hudakz 0:84353c479782 87
hudakz 0:84353c479782 88 /** Call this to start the pairing sequence with a controller */
hudakz 0:84353c479782 89 void pair(void) {
hudakz 0:84353c479782 90 if(pBtd)
hudakz 0:84353c479782 91 pBtd->pairWithWiimote();
hudakz 0:84353c479782 92 };
hudakz 0:84353c479782 93 /**
hudakz 0:84353c479782 94 * Used to read the joystick of the Nunchuck.
hudakz 0:84353c479782 95 * @param a Either ::HatX or ::HatY.
hudakz 0:84353c479782 96 * @return Return the analog value in the range from approximately 25-230.
hudakz 0:84353c479782 97 */
hudakz 0:84353c479782 98 uint8_t getAnalogHat(HatEnum a);
hudakz 0:84353c479782 99 /**
hudakz 0:84353c479782 100 * Used to read the joystick of the Wii U Pro Controller.
hudakz 0:84353c479782 101 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
hudakz 0:84353c479782 102 * @return Return the analog value in the range from approximately 800-3200.
hudakz 0:84353c479782 103 */
hudakz 0:84353c479782 104 uint16_t getAnalogHat(AnalogHatEnum a);
hudakz 0:84353c479782 105
hudakz 0:84353c479782 106 /**
hudakz 0:84353c479782 107 * Pitch calculated from the Wiimote. A complimentary filter is used if the Motion Plus is connected.
hudakz 0:84353c479782 108 * @return Pitch in the range from 0-360.
hudakz 0:84353c479782 109 */
hudakz 0:84353c479782 110 float getPitch() {
hudakz 0:84353c479782 111 if(motionPlusConnected)
hudakz 0:84353c479782 112 return compPitch;
hudakz 0:84353c479782 113 return getWiimotePitch();
hudakz 0:84353c479782 114 };
hudakz 0:84353c479782 115
hudakz 0:84353c479782 116 /**
hudakz 0:84353c479782 117 * Roll calculated from the Wiimote. A complimentary filter is used if the Motion Plus is connected.
hudakz 0:84353c479782 118 * @return Roll in the range from 0-360.
hudakz 0:84353c479782 119 */
hudakz 0:84353c479782 120 float getRoll() {
hudakz 0:84353c479782 121 if(motionPlusConnected)
hudakz 0:84353c479782 122 return compRoll;
hudakz 0:84353c479782 123 return getWiimoteRoll();
hudakz 0:84353c479782 124 };
hudakz 0:84353c479782 125
hudakz 0:84353c479782 126 /**
hudakz 0:84353c479782 127 * This is the yaw calculated by the gyro.
hudakz 0:84353c479782 128 *
hudakz 0:84353c479782 129 * <B>NOTE:</B> This angle will drift a lot and is only available if the Motion Plus extension is connected.
hudakz 0:84353c479782 130 * @return The angle calculated using the gyro.
hudakz 0:84353c479782 131 */
hudakz 0:84353c479782 132 float getYaw() {
hudakz 0:84353c479782 133 return gyroYaw;
hudakz 0:84353c479782 134 };
hudakz 0:84353c479782 135
hudakz 0:84353c479782 136 /** Used to set all LEDs and rumble off. */
hudakz 0:84353c479782 137 void setAllOff();
hudakz 0:84353c479782 138 /** Turn off rumble. */
hudakz 0:84353c479782 139 void setRumbleOff();
hudakz 0:84353c479782 140 /** Turn on rumble. */
hudakz 0:84353c479782 141 void setRumbleOn();
hudakz 0:84353c479782 142 /** Toggle rumble. */
hudakz 0:84353c479782 143 void setRumbleToggle();
hudakz 0:84353c479782 144
hudakz 0:84353c479782 145 /**
hudakz 0:84353c479782 146 * Set LED value without using the ::LEDEnum.
hudakz 0:84353c479782 147 * @param value See: ::LEDEnum.
hudakz 0:84353c479782 148 */
hudakz 0:84353c479782 149 void setLedRaw(uint8_t value);
hudakz 0:84353c479782 150
hudakz 0:84353c479782 151 /** Turn all LEDs off. */
hudakz 0:84353c479782 152 void setLedOff() {
hudakz 0:84353c479782 153 setLedRaw(0);
hudakz 0:84353c479782 154 };
hudakz 0:84353c479782 155 /**
hudakz 0:84353c479782 156 * Turn the specific ::LEDEnum off.
hudakz 0:84353c479782 157 * @param a The ::LEDEnum to turn off.
hudakz 0:84353c479782 158 */
hudakz 0:84353c479782 159 void setLedOff(LEDEnum a);
hudakz 0:84353c479782 160 /**
hudakz 0:84353c479782 161 * Turn the specific ::LEDEnum on.
hudakz 0:84353c479782 162 * @param a The ::LEDEnum to turn on.
hudakz 0:84353c479782 163 */
hudakz 0:84353c479782 164 void setLedOn(LEDEnum a);
hudakz 0:84353c479782 165 /**
hudakz 0:84353c479782 166 * Toggle the specific ::LEDEnum.
hudakz 0:84353c479782 167 * @param a The ::LEDEnum to toggle.
hudakz 0:84353c479782 168 */
hudakz 0:84353c479782 169 void setLedToggle(LEDEnum a);
hudakz 0:84353c479782 170 /**
hudakz 0:84353c479782 171 * This will set the LEDs, so the user can see which connections are active.
hudakz 0:84353c479782 172 *
hudakz 0:84353c479782 173 * The first ::LEDEnum indicate that the Wiimote is connected,
hudakz 0:84353c479782 174 * the second ::LEDEnum indicate indicate that a Motion Plus is also connected
hudakz 0:84353c479782 175 * the third ::LEDEnum will indicate that a Nunchuck controller is also connected.
hudakz 0:84353c479782 176 */
hudakz 0:84353c479782 177 void setLedStatus();
hudakz 0:84353c479782 178
hudakz 0:84353c479782 179 /**
hudakz 0:84353c479782 180 * Return the battery level of the Wiimote.
hudakz 0:84353c479782 181 * @return The battery level in the range 0-255.
hudakz 0:84353c479782 182 */
hudakz 0:84353c479782 183 uint8_t getBatteryLevel();
hudakz 0:84353c479782 184
hudakz 0:84353c479782 185 /**
hudakz 0:84353c479782 186 * Return the Wiimote state.
hudakz 0:84353c479782 187 * @return See: http://wiibrew.org/wiki/Wiimote#0x20:_Status.
hudakz 0:84353c479782 188 */
hudakz 0:84353c479782 189 uint8_t getWiiState() {
hudakz 0:84353c479782 190 return wiiState;
hudakz 0:84353c479782 191 };
hudakz 0:84353c479782 192 /**@}*/
hudakz 0:84353c479782 193
hudakz 0:84353c479782 194 /**@{*/
hudakz 0:84353c479782 195 /** Variable used to indicate if a Wiimote is connected. */
hudakz 0:84353c479782 196 bool wiimoteConnected;
hudakz 0:84353c479782 197 /** Variable used to indicate if a Nunchuck controller is connected. */
hudakz 0:84353c479782 198 bool nunchuckConnected;
hudakz 0:84353c479782 199 /** Variable used to indicate if a Nunchuck controller is connected. */
hudakz 0:84353c479782 200 bool motionPlusConnected;
hudakz 0:84353c479782 201 /** Variable used to indicate if a Wii U Pro controller is connected. */
hudakz 0:84353c479782 202 bool wiiUProControllerConnected;
hudakz 0:84353c479782 203 /** Variable used to indicate if a Wii Balance Board is connected. */
hudakz 0:84353c479782 204 bool wiiBalanceBoardConnected;
hudakz 0:84353c479782 205 /**@}*/
hudakz 0:84353c479782 206
hudakz 0:84353c479782 207 /* IMU Data, might be usefull if you need to do something more advanced than just calculating the angle */
hudakz 0:84353c479782 208
hudakz 0:84353c479782 209 /**@{*/
hudakz 0:84353c479782 210
hudakz 0:84353c479782 211 /** Pitch and roll calculated from the accelerometer inside the Wiimote. */
hudakz 0:84353c479782 212 float getWiimotePitch() {
hudakz 0:84353c479782 213 return (atan2f(accYwiimote, accZwiimote) + M_PI) * RAD_TO_DEG;
hudakz 0:84353c479782 214 };
hudakz 0:84353c479782 215
hudakz 0:84353c479782 216 float getWiimoteRoll() {
hudakz 0:84353c479782 217 return (atan2f(accXwiimote, accZwiimote) + M_PI) * RAD_TO_DEG;
hudakz 0:84353c479782 218 };
hudakz 0:84353c479782 219 /**@}*/
hudakz 0:84353c479782 220
hudakz 0:84353c479782 221 /**@{*/
hudakz 0:84353c479782 222
hudakz 0:84353c479782 223 /** Pitch and roll calculated from the accelerometer inside the Nunchuck. */
hudakz 0:84353c479782 224 float getNunchuckPitch() {
hudakz 0:84353c479782 225 return (atan2f(accYnunchuck, accZnunchuck) + M_PI) * RAD_TO_DEG;
hudakz 0:84353c479782 226 };
hudakz 0:84353c479782 227
hudakz 0:84353c479782 228 float getNunchuckRoll() {
hudakz 0:84353c479782 229 return (atan2f(accXnunchuck, accZnunchuck) + M_PI) * RAD_TO_DEG;
hudakz 0:84353c479782 230 };
hudakz 0:84353c479782 231 /**@}*/
hudakz 0:84353c479782 232
hudakz 0:84353c479782 233 /**@{*/
hudakz 0:84353c479782 234 /** Accelerometer values used to calculate pitch and roll. */
hudakz 0:84353c479782 235 int16_t accXwiimote, accYwiimote, accZwiimote;
hudakz 0:84353c479782 236 int16_t accXnunchuck, accYnunchuck, accZnunchuck;
hudakz 0:84353c479782 237 /**@}*/
hudakz 0:84353c479782 238
hudakz 0:84353c479782 239 /* Variables for the gyro inside the Motion Plus */
hudakz 0:84353c479782 240 /** This is the pitch calculated by the gyro - use this to tune WII#pitchGyroScale. */
hudakz 0:84353c479782 241 float gyroPitch;
hudakz 0:84353c479782 242 /** This is the roll calculated by the gyro - use this to tune WII#rollGyroScale. */
hudakz 0:84353c479782 243 float gyroRoll;
hudakz 0:84353c479782 244 /** This is the yaw calculated by the gyro - use this to tune WII#yawGyroScale. */
hudakz 0:84353c479782 245 float gyroYaw;
hudakz 0:84353c479782 246
hudakz 0:84353c479782 247 /**@{*/
hudakz 0:84353c479782 248 /** The speed in deg/s from the gyro. */
hudakz 0:84353c479782 249 float pitchGyroSpeed;
hudakz 0:84353c479782 250 float rollGyroSpeed;
hudakz 0:84353c479782 251 float yawGyroSpeed;
hudakz 0:84353c479782 252 /**@}*/
hudakz 0:84353c479782 253
hudakz 0:84353c479782 254 /**@{*/
hudakz 0:84353c479782 255 /** You might need to fine-tune these values. */
hudakz 0:84353c479782 256 uint16_t pitchGyroScale;
hudakz 0:84353c479782 257 uint16_t rollGyroScale;
hudakz 0:84353c479782 258 uint16_t yawGyroScale;
hudakz 0:84353c479782 259 /**@}*/
hudakz 0:84353c479782 260
hudakz 0:84353c479782 261 /**@{*/
hudakz 0:84353c479782 262 /** Raw value read directly from the Motion Plus. */
hudakz 0:84353c479782 263 int16_t gyroYawRaw;
hudakz 0:84353c479782 264 int16_t gyroRollRaw;
hudakz 0:84353c479782 265 int16_t gyroPitchRaw;
hudakz 0:84353c479782 266 /**@}*/
hudakz 0:84353c479782 267
hudakz 0:84353c479782 268 /**@{*/
hudakz 0:84353c479782 269 /** These values are set when the controller is first initialized. */
hudakz 0:84353c479782 270 int16_t gyroYawZero;
hudakz 0:84353c479782 271 int16_t gyroRollZero;
hudakz 0:84353c479782 272 int16_t gyroPitchZero;
hudakz 0:84353c479782 273 /**@}*/
hudakz 0:84353c479782 274
hudakz 0:84353c479782 275 /** @name Wii Balance Board functions */
hudakz 0:84353c479782 276
hudakz 0:84353c479782 277 /**
hudakz 0:84353c479782 278 * Used to get the weight at the specific position on the Wii Balance Board.
hudakz 0:84353c479782 279 * @param pos ::BalanceBoardEnum to read from.
hudakz 0:84353c479782 280 * @return Returns the weight in kg.
hudakz 0:84353c479782 281 */
hudakz 0:84353c479782 282 float getWeight(BalanceBoardEnum pos);
hudakz 0:84353c479782 283
hudakz 0:84353c479782 284 /**
hudakz 0:84353c479782 285 * Used to get total weight on the Wii Balance Board.
hudakz 0:84353c479782 286 * @return Returns the weight in kg.
hudakz 0:84353c479782 287 */
hudakz 0:84353c479782 288 float getTotalWeight();
hudakz 0:84353c479782 289
hudakz 0:84353c479782 290 /**
hudakz 0:84353c479782 291 * Used to get the raw reading at the specific position on the Wii Balance Board.
hudakz 0:84353c479782 292 * @param pos ::BalanceBoardEnum to read from.
hudakz 0:84353c479782 293 * @return Returns the raw reading.
hudakz 0:84353c479782 294 */
hudakz 0:84353c479782 295 uint16_t getWeightRaw(BalanceBoardEnum pos) {
hudakz 0:84353c479782 296 return wiiBalanceBoardRaw[pos];
hudakz 0:84353c479782 297 };
hudakz 0:84353c479782 298 /**@}*/
hudakz 0:84353c479782 299
hudakz 0:84353c479782 300 #ifdef WIICAMERA
hudakz 0:84353c479782 301 /** @name Wiimote IR camera functions
hudakz 0:84353c479782 302 * You will have to set ::ENABLE_WII_IR_CAMERA in settings.h to 1 in order use the IR camera.
hudakz 0:84353c479782 303 */
hudakz 0:84353c479782 304 /** Initialises the camera as per the steps from: http://wiibrew.org/wiki/Wiimote#IR_Camera */
hudakz 0:84353c479782 305 void IRinitialize();
hudakz 0:84353c479782 306
hudakz 0:84353c479782 307 /**
hudakz 0:84353c479782 308 * IR object 1 x-position read from the Wii IR camera.
hudakz 0:84353c479782 309 * @return The x-position of the object in the range 0-1023.
hudakz 0:84353c479782 310 */
hudakz 0:84353c479782 311 uint16_t getIRx1() {
hudakz 0:84353c479782 312 return IR_object_x1;
hudakz 0:84353c479782 313 };
hudakz 0:84353c479782 314
hudakz 0:84353c479782 315 /**
hudakz 0:84353c479782 316 * IR object 1 y-position read from the Wii IR camera.
hudakz 0:84353c479782 317 * @return The y-position of the object in the range 0-767.
hudakz 0:84353c479782 318 */
hudakz 0:84353c479782 319 uint16_t getIRy1() {
hudakz 0:84353c479782 320 return IR_object_y1;
hudakz 0:84353c479782 321 };
hudakz 0:84353c479782 322
hudakz 0:84353c479782 323 /**
hudakz 0:84353c479782 324 * IR object 1 size read from the Wii IR camera.
hudakz 0:84353c479782 325 * @return The size of the object in the range 0-15.
hudakz 0:84353c479782 326 */
hudakz 0:84353c479782 327 uint8_t getIRs1() {
hudakz 0:84353c479782 328 return IR_object_s1;
hudakz 0:84353c479782 329 };
hudakz 0:84353c479782 330
hudakz 0:84353c479782 331 /**
hudakz 0:84353c479782 332 * IR object 2 x-position read from the Wii IR camera.
hudakz 0:84353c479782 333 * @return The x-position of the object in the range 0-1023.
hudakz 0:84353c479782 334 */
hudakz 0:84353c479782 335 uint16_t getIRx2() {
hudakz 0:84353c479782 336 return IR_object_x2;
hudakz 0:84353c479782 337 };
hudakz 0:84353c479782 338
hudakz 0:84353c479782 339 /**
hudakz 0:84353c479782 340 * IR object 2 y-position read from the Wii IR camera.
hudakz 0:84353c479782 341 * @return The y-position of the object in the range 0-767.
hudakz 0:84353c479782 342 */
hudakz 0:84353c479782 343 uint16_t getIRy2() {
hudakz 0:84353c479782 344 return IR_object_y2;
hudakz 0:84353c479782 345 };
hudakz 0:84353c479782 346
hudakz 0:84353c479782 347 /**
hudakz 0:84353c479782 348 * IR object 2 size read from the Wii IR camera.
hudakz 0:84353c479782 349 * @return The size of the object in the range 0-15.
hudakz 0:84353c479782 350 */
hudakz 0:84353c479782 351 uint8_t getIRs2() {
hudakz 0:84353c479782 352 return IR_object_s2;
hudakz 0:84353c479782 353 };
hudakz 0:84353c479782 354
hudakz 0:84353c479782 355 /**
hudakz 0:84353c479782 356 * IR object 3 x-position read from the Wii IR camera.
hudakz 0:84353c479782 357 * @return The x-position of the object in the range 0-1023.
hudakz 0:84353c479782 358 */
hudakz 0:84353c479782 359 uint16_t getIRx3() {
hudakz 0:84353c479782 360 return IR_object_x3;
hudakz 0:84353c479782 361 };
hudakz 0:84353c479782 362
hudakz 0:84353c479782 363 /**
hudakz 0:84353c479782 364 * IR object 3 y-position read from the Wii IR camera.
hudakz 0:84353c479782 365 * @return The y-position of the object in the range 0-767.
hudakz 0:84353c479782 366 */
hudakz 0:84353c479782 367 uint16_t getIRy3() {
hudakz 0:84353c479782 368 return IR_object_y3;
hudakz 0:84353c479782 369 };
hudakz 0:84353c479782 370
hudakz 0:84353c479782 371 /**
hudakz 0:84353c479782 372 * IR object 3 size read from the Wii IR camera.
hudakz 0:84353c479782 373 * @return The size of the object in the range 0-15.
hudakz 0:84353c479782 374 */
hudakz 0:84353c479782 375 uint8_t getIRs3() {
hudakz 0:84353c479782 376 return IR_object_s3;
hudakz 0:84353c479782 377 };
hudakz 0:84353c479782 378
hudakz 0:84353c479782 379 /**
hudakz 0:84353c479782 380 * IR object 4 x-position read from the Wii IR camera.
hudakz 0:84353c479782 381 * @return The x-position of the object in the range 0-1023.
hudakz 0:84353c479782 382 */
hudakz 0:84353c479782 383 uint16_t getIRx4() {
hudakz 0:84353c479782 384 return IR_object_x4;
hudakz 0:84353c479782 385 };
hudakz 0:84353c479782 386
hudakz 0:84353c479782 387 /**
hudakz 0:84353c479782 388 * IR object 4 y-position read from the Wii IR camera.
hudakz 0:84353c479782 389 * @return The y-position of the object in the range 0-767.
hudakz 0:84353c479782 390 */
hudakz 0:84353c479782 391 uint16_t getIRy4() {
hudakz 0:84353c479782 392 return IR_object_y4;
hudakz 0:84353c479782 393 };
hudakz 0:84353c479782 394
hudakz 0:84353c479782 395 /**
hudakz 0:84353c479782 396 * IR object 4 size read from the Wii IR camera.
hudakz 0:84353c479782 397 * @return The size of the object in the range 0-15.
hudakz 0:84353c479782 398 */
hudakz 0:84353c479782 399 uint8_t getIRs4() {
hudakz 0:84353c479782 400 return IR_object_s4;
hudakz 0:84353c479782 401 };
hudakz 0:84353c479782 402
hudakz 0:84353c479782 403 /**
hudakz 0:84353c479782 404 * Use this to check if the camera is enabled or not.
hudakz 0:84353c479782 405 * If not call WII#IRinitialize to initialize the IR camera.
hudakz 0:84353c479782 406 * @return True if it's enabled, false if not.
hudakz 0:84353c479782 407 */
hudakz 0:84353c479782 408 bool isIRCameraEnabled() {
hudakz 0:84353c479782 409 return (wiiState & 0x08);
hudakz 0:84353c479782 410 };
hudakz 0:84353c479782 411 /**@}*/
hudakz 0:84353c479782 412 #endif
hudakz 0:84353c479782 413
hudakz 0:84353c479782 414 protected:
hudakz 0:84353c479782 415 /** @name BluetoothService implementation */
hudakz 0:84353c479782 416 /**
hudakz 0:84353c479782 417 * Used to pass acldata to the services.
hudakz 0:84353c479782 418 * @param ACLData Incoming acldata.
hudakz 0:84353c479782 419 */
hudakz 0:84353c479782 420 void ACLData(uint8_t* ACLData);
hudakz 0:84353c479782 421 /** Used to run part of the state machine. */
hudakz 0:84353c479782 422 void Run();
hudakz 0:84353c479782 423 /** Use this to reset the service. */
hudakz 0:84353c479782 424 void Reset();
hudakz 0:84353c479782 425 /**
hudakz 0:84353c479782 426 * Called when the controller is successfully initialized.
hudakz 0:84353c479782 427 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
hudakz 0:84353c479782 428 * This is useful for instance if you want to set the LEDs in a specific way.
hudakz 0:84353c479782 429 */
hudakz 0:84353c479782 430 void onInit();
hudakz 0:84353c479782 431 /**@}*/
hudakz 0:84353c479782 432
hudakz 0:84353c479782 433 private:
hudakz 0:84353c479782 434
hudakz 0:84353c479782 435 void L2CAP_task(); // L2CAP state machine
hudakz 0:84353c479782 436
hudakz 0:84353c479782 437 /* Variables filled from HCI event management */
hudakz 0:84353c479782 438 bool activeConnection; // Used to indicate if it's already has established a connection
hudakz 0:84353c479782 439
hudakz 0:84353c479782 440 /* Variables used by high level L2CAP task */
hudakz 0:84353c479782 441 uint8_t l2cap_state;
hudakz 0:84353c479782 442 uint8_t wii_event_flag; // Used for Wii flags
hudakz 0:84353c479782 443
hudakz 0:84353c479782 444 uint32_t ButtonState;
hudakz 0:84353c479782 445 uint32_t OldButtonState;
hudakz 0:84353c479782 446 uint32_t ButtonClickState;
hudakz 0:84353c479782 447 uint16_t hatValues[4];
hudakz 0:84353c479782 448
hudakz 0:84353c479782 449 uint8_t HIDBuffer[3]; // Used to store HID commands
hudakz 0:84353c479782 450
hudakz 0:84353c479782 451 uint16_t stateCounter;
hudakz 0:84353c479782 452 bool unknownExtensionConnected;
hudakz 0:84353c479782 453 bool extensionConnected;
hudakz 0:84353c479782 454 bool checkBatteryLevel; // Set to true when getBatteryLevel() is called otherwise if should be false
hudakz 0:84353c479782 455 bool motionPlusInside; // True if it's a new Wiimote with the Motion Plus extension build into it
hudakz 0:84353c479782 456
hudakz 0:84353c479782 457 /* L2CAP Channels */
hudakz 0:84353c479782 458 uint8_t control_scid[2]; // L2CAP source CID for HID_Control
hudakz 0:84353c479782 459 uint8_t control_dcid[2]; // 0x0060
hudakz 0:84353c479782 460 uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
hudakz 0:84353c479782 461 uint8_t interrupt_dcid[2]; // 0x0061
hudakz 0:84353c479782 462
hudakz 0:84353c479782 463 /* HID Commands */
hudakz 0:84353c479782 464 void HID_Command(uint8_t* data, uint8_t nbytes);
hudakz 0:84353c479782 465 void setReportMode(bool continuous, uint8_t mode);
hudakz 0:84353c479782 466
hudakz 0:84353c479782 467 void writeData(uint32_t offset, uint8_t size, uint8_t* data);
hudakz 0:84353c479782 468 void initExtension1();
hudakz 0:84353c479782 469 void initExtension2();
hudakz 0:84353c479782 470
hudakz 0:84353c479782 471 void statusRequest(); // Used to update the Wiimote state and battery level
hudakz 0:84353c479782 472
hudakz 0:84353c479782 473 void readData(uint32_t offset, uint16_t size, bool EEPROM);
hudakz 0:84353c479782 474 void readExtensionType();
hudakz 0:84353c479782 475 void readCalData();
hudakz 0:84353c479782 476 void readWiiBalanceBoardCalibration(); // Used by the library to read the Wii Balance Board calibration values
hudakz 0:84353c479782 477
hudakz 0:84353c479782 478 void checkMotionPresent(); // Used to see if a Motion Plus is connected to the Wiimote
hudakz 0:84353c479782 479 void initMotionPlus();
hudakz 0:84353c479782 480 void activateMotionPlus();
hudakz 0:84353c479782 481
hudakz 0:84353c479782 482 uint16_t wiiBalanceBoardRaw[4]; // Wii Balance Board raw values
hudakz 0:84353c479782 483 uint16_t wiiBalanceBoardCal[3][4]; // Wii Balance Board calibration values
hudakz 0:84353c479782 484
hudakz 0:84353c479782 485 float compPitch; // Fusioned angle using a complimentary filter if the Motion Plus is connected
hudakz 0:84353c479782 486 float compRoll; // Fusioned angle using a complimentary filter if the Motion Plus is connected
hudakz 0:84353c479782 487
hudakz 0:84353c479782 488 bool activateNunchuck;
hudakz 0:84353c479782 489 bool motionValuesReset; // This bool is true when the gyro values has been reset
hudakz 0:84353c479782 490 uint32_t timer;
hudakz 0:84353c479782 491
hudakz 0:84353c479782 492 uint8_t wiiState; // Stores the value in l2capinbuf[12] - (0x01: Battery is nearly empty), (0x02: An Extension Controller is connected), (0x04: Speaker enabled), (0x08: IR enabled), (0x10: LED1, 0x20: LED2, 0x40: LED3, 0x80: LED4)
hudakz 0:84353c479782 493 uint8_t batteryLevel;
hudakz 0:84353c479782 494
hudakz 0:84353c479782 495 #ifdef WIICAMERA
hudakz 0:84353c479782 496 /* Private function and variables for the readings from the IR Camera */
hudakz 0:84353c479782 497 void enableIRCamera1(); // Sets bit 2 of output report 13
hudakz 0:84353c479782 498 void enableIRCamera2(); // Sets bit 2 of output report 1A
hudakz 0:84353c479782 499 void writeSensitivityBlock1();
hudakz 0:84353c479782 500 void writeSensitivityBlock2();
hudakz 0:84353c479782 501 void write0x08Value();
hudakz 0:84353c479782 502 void setWiiModeNumber(uint8_t mode_number);
hudakz 0:84353c479782 503
hudakz 0:84353c479782 504 uint16_t IR_object_x1; // IR x position 10 bits
hudakz 0:84353c479782 505 uint16_t IR_object_y1; // IR y position 10 bits
hudakz 0:84353c479782 506 uint8_t IR_object_s1; // IR size value
hudakz 0:84353c479782 507 uint16_t IR_object_x2;
hudakz 0:84353c479782 508 uint16_t IR_object_y2;
hudakz 0:84353c479782 509 uint8_t IR_object_s2;
hudakz 0:84353c479782 510 uint16_t IR_object_x3; // IR x position 10 bits
hudakz 0:84353c479782 511 uint16_t IR_object_y3; // IR y position 10 bits
hudakz 0:84353c479782 512 uint8_t IR_object_s3; // IR size value
hudakz 0:84353c479782 513 uint16_t IR_object_x4;
hudakz 0:84353c479782 514 uint16_t IR_object_y4;
hudakz 0:84353c479782 515 uint8_t IR_object_s4;
hudakz 0:84353c479782 516 #endif
hudakz 0:84353c479782 517 };
hudakz 0:84353c479782 518 #endif