Library to use Arduino USB host shield on mbed

Dependents:   USBHOST_PS5

ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。

Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です https://os.mbed.com/media/uploads/kotakku/dffgfddswa.png

シールドについて

3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください

接続例

https://os.mbed.com/media/uploads/kotakku/esgsvfvhjrekldkcjxvb.png

使い方

Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。

仕様

  • Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。

main.cpp

#include "mbed.h"
#include <PS3BT.h>
#include <usbhub.h>

Serial pc(USBTX, USBRX, 115200);

//Nucleo f303k8用
USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int main()
{
    bool printAngle = false;

    if (Usb.Init() == -1)
    {
        pc.printf("\r\nOSC did not start");
        while (1); // Halt
    }
    pc.printf("\r\nPS3 USB Library Started");

    while (1)
    {
        Usb.Task();
        
        if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
            if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117)
            {
                pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX));
                pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY));
                if (PS3.PS3Connected)
                { // The Navigation controller only have one joystick
                    pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX));
                    pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY));
                }
            }
            // Analog button values can be read from almost all buttons
            if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2))
            {
                pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2));
                if (!PS3.PS3NavigationConnected)
                {
                    pc.printf("\tR2: %d", PS3.getAnalogButton(R2));
                }
            }
            if (PS3.getButtonClick(PS))
            {
                PS3.disconnect();
                pc.printf("\r\nPS");
            }
    
            if (PS3.getButtonClick(TRIANGLE))
                pc.printf("\r\nTriangle");
            if (PS3.getButtonClick(CIRCLE))
                pc.printf("\r\nCircle");
            if (PS3.getButtonClick(CROSS))
                pc.printf("\r\nCross");
            if (PS3.getButtonClick(SQUARE))
                pc.printf("\r\nSquare");
    
            if (PS3.getButtonClick(UP))
            {
                pc.printf("\r\nUp");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED4);
            }
            if (PS3.getButtonClick(RIGHT))
            {
                pc.printf("\r\nRight");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED1);
            }
            if (PS3.getButtonClick(DOWN))
            {
                pc.printf("\r\nDown");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED2);
            }
            if (PS3.getButtonClick(LEFT))
            {
                pc.printf("\r\nLeft");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED3);
            }
    
            if (PS3.getButtonClick(L1))
                pc.printf("\r\nL1");
            if (PS3.getButtonClick(L3))
                pc.printf("\r\nL3");
            if (PS3.getButtonClick(R1))
                pc.printf("\r\nR1");
            if (PS3.getButtonClick(R3))
                pc.printf("\r\nR3");
    
            if (PS3.getButtonClick(SELECT))
            {
                pc.printf("\r\nSelect - ");
                PS3.printStatusString();
            }
            if (PS3.getButtonClick(START))
            {
                pc.printf("\r\nStart");
                printAngle = !printAngle;
            }
            if (printAngle)
            {
                pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch));
                pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll));
            }
        }
        else
        {
            pc.printf("not connect\n");
        }
    }
}
Committer:
kotakku
Date:
Sat Jan 18 15:06:35 2020 +0000
Revision:
0:b1ce54272580
1.0.0 first commit

Who changed what in which revision?

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