Library to use Arduino USB host shield on mbed
ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。
Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です
シールドについて
3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください
接続例
使い方
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"); } } }
USB_Host/PS4Parser.h@1:da31140f2a1c, 2020-05-02 (annotated)
- Committer:
- robo_ichinoseki_a
- Date:
- Sat May 02 05:56:48 2020 +0000
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | /* Copyright (C) 2014 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 | |
kotakku | 0:b1ce54272580 | 18 | #ifndef _ps4parser_h_ |
kotakku | 0:b1ce54272580 | 19 | #define _ps4parser_h_ |
kotakku | 0:b1ce54272580 | 20 | |
kotakku | 0:b1ce54272580 | 21 | #include "Usb.h" |
kotakku | 0:b1ce54272580 | 22 | #include "controllerEnums.h" |
kotakku | 0:b1ce54272580 | 23 | |
kotakku | 0:b1ce54272580 | 24 | /** Buttons on the controller */ |
kotakku | 0:b1ce54272580 | 25 | const uint8_t PS4_BUTTONS[] PROGMEM = { |
kotakku | 0:b1ce54272580 | 26 | UP, // UP |
kotakku | 0:b1ce54272580 | 27 | RIGHT, // RIGHT |
kotakku | 0:b1ce54272580 | 28 | DOWN, // DOWN |
kotakku | 0:b1ce54272580 | 29 | LEFT, // LEFT |
kotakku | 0:b1ce54272580 | 30 | |
kotakku | 0:b1ce54272580 | 31 | 0x0C, // SHARE |
kotakku | 0:b1ce54272580 | 32 | 0x0D, // OPTIONS |
kotakku | 0:b1ce54272580 | 33 | 0x0E, // L3 |
kotakku | 0:b1ce54272580 | 34 | 0x0F, // R3 |
kotakku | 0:b1ce54272580 | 35 | |
kotakku | 0:b1ce54272580 | 36 | 0x0A, // L2 |
kotakku | 0:b1ce54272580 | 37 | 0x0B, // R2 |
kotakku | 0:b1ce54272580 | 38 | 0x08, // L1 |
kotakku | 0:b1ce54272580 | 39 | 0x09, // R1 |
kotakku | 0:b1ce54272580 | 40 | |
kotakku | 0:b1ce54272580 | 41 | 0x07, // TRIANGLE |
kotakku | 0:b1ce54272580 | 42 | 0x06, // CIRCLE |
kotakku | 0:b1ce54272580 | 43 | 0x05, // CROSS |
kotakku | 0:b1ce54272580 | 44 | 0x04, // SQUARE |
kotakku | 0:b1ce54272580 | 45 | |
kotakku | 0:b1ce54272580 | 46 | 0x10, // PS |
kotakku | 0:b1ce54272580 | 47 | 0x11, // TOUCHPAD |
kotakku | 0:b1ce54272580 | 48 | }; |
kotakku | 0:b1ce54272580 | 49 | |
kotakku | 0:b1ce54272580 | 50 | union PS4Buttons { |
kotakku | 0:b1ce54272580 | 51 | struct { |
kotakku | 0:b1ce54272580 | 52 | uint8_t dpad : 4; |
kotakku | 0:b1ce54272580 | 53 | uint8_t square : 1; |
kotakku | 0:b1ce54272580 | 54 | uint8_t cross : 1; |
kotakku | 0:b1ce54272580 | 55 | uint8_t circle : 1; |
kotakku | 0:b1ce54272580 | 56 | uint8_t triangle : 1; |
kotakku | 0:b1ce54272580 | 57 | |
kotakku | 0:b1ce54272580 | 58 | uint8_t l1 : 1; |
kotakku | 0:b1ce54272580 | 59 | uint8_t r1 : 1; |
kotakku | 0:b1ce54272580 | 60 | uint8_t l2 : 1; |
kotakku | 0:b1ce54272580 | 61 | uint8_t r2 : 1; |
kotakku | 0:b1ce54272580 | 62 | uint8_t share : 1; |
kotakku | 0:b1ce54272580 | 63 | uint8_t options : 1; |
kotakku | 0:b1ce54272580 | 64 | uint8_t l3 : 1; |
kotakku | 0:b1ce54272580 | 65 | uint8_t r3 : 1; |
kotakku | 0:b1ce54272580 | 66 | |
kotakku | 0:b1ce54272580 | 67 | uint8_t ps : 1; |
kotakku | 0:b1ce54272580 | 68 | uint8_t touchpad : 1; |
kotakku | 0:b1ce54272580 | 69 | uint8_t reportCounter : 6; |
kotakku | 0:b1ce54272580 | 70 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 71 | uint32_t val : 24; |
kotakku | 0:b1ce54272580 | 72 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 73 | |
kotakku | 0:b1ce54272580 | 74 | struct touchpadXY { |
kotakku | 0:b1ce54272580 | 75 | uint8_t dummy; // I can not figure out what this data is for, it seems to change randomly, maybe a timestamp? |
kotakku | 0:b1ce54272580 | 76 | struct { |
kotakku | 0:b1ce54272580 | 77 | uint8_t counter : 7; // Increments every time a finger is touching the touchpad |
kotakku | 0:b1ce54272580 | 78 | uint8_t touching : 1; // The top bit is cleared if the finger is touching the touchpad |
kotakku | 0:b1ce54272580 | 79 | uint16_t x : 12; |
kotakku | 0:b1ce54272580 | 80 | uint16_t y : 12; |
kotakku | 0:b1ce54272580 | 81 | } __attribute__((packed)) finger[2]; // 0 = first finger, 1 = second finger |
kotakku | 0:b1ce54272580 | 82 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 83 | |
kotakku | 0:b1ce54272580 | 84 | struct PS4Status { |
kotakku | 0:b1ce54272580 | 85 | uint8_t battery : 4; |
kotakku | 0:b1ce54272580 | 86 | uint8_t usb : 1; |
kotakku | 0:b1ce54272580 | 87 | uint8_t audio : 1; |
kotakku | 0:b1ce54272580 | 88 | uint8_t mic : 1; |
kotakku | 0:b1ce54272580 | 89 | uint8_t unknown : 1; // Extension port? |
kotakku | 0:b1ce54272580 | 90 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 91 | |
kotakku | 0:b1ce54272580 | 92 | struct PS4Data { |
kotakku | 0:b1ce54272580 | 93 | /* Button and joystick values */ |
kotakku | 0:b1ce54272580 | 94 | uint8_t hatValue[4]; |
kotakku | 0:b1ce54272580 | 95 | PS4Buttons btn; |
kotakku | 0:b1ce54272580 | 96 | uint8_t trigger[2]; |
kotakku | 0:b1ce54272580 | 97 | |
kotakku | 0:b1ce54272580 | 98 | /* Gyro and accelerometer values */ |
kotakku | 0:b1ce54272580 | 99 | uint8_t dummy[3]; // First two looks random, while the third one might be some kind of status - it increments once in a while |
kotakku | 0:b1ce54272580 | 100 | int16_t gyroY, gyroZ, gyroX; |
kotakku | 0:b1ce54272580 | 101 | int16_t accX, accZ, accY; |
kotakku | 0:b1ce54272580 | 102 | |
kotakku | 0:b1ce54272580 | 103 | uint8_t dummy2[5]; |
kotakku | 0:b1ce54272580 | 104 | PS4Status status; |
kotakku | 0:b1ce54272580 | 105 | uint8_t dummy3[3]; |
kotakku | 0:b1ce54272580 | 106 | |
kotakku | 0:b1ce54272580 | 107 | /* The rest is data for the touchpad */ |
kotakku | 0:b1ce54272580 | 108 | touchpadXY xy[3]; // It looks like it sends out three coordinates each time, this might be because the microcontroller inside the PS4 controller is much faster than the Bluetooth connection. |
kotakku | 0:b1ce54272580 | 109 | // The last data is read from the last position in the array while the oldest measurement is from the first position. |
kotakku | 0:b1ce54272580 | 110 | // The first position will also keep it's value after the finger is released, while the other two will set them to zero. |
kotakku | 0:b1ce54272580 | 111 | // Note that if you read fast enough from the device, then only the first one will contain any data. |
kotakku | 0:b1ce54272580 | 112 | |
kotakku | 0:b1ce54272580 | 113 | // The last three bytes are always: 0x00, 0x80, 0x00 |
kotakku | 0:b1ce54272580 | 114 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 115 | |
kotakku | 0:b1ce54272580 | 116 | struct PS4Output { |
kotakku | 0:b1ce54272580 | 117 | uint8_t bigRumble, smallRumble; // Rumble |
kotakku | 0:b1ce54272580 | 118 | uint8_t r, g, b; // RGB |
kotakku | 0:b1ce54272580 | 119 | uint8_t flashOn, flashOff; // Time to flash bright/dark (255 = 2.5 seconds) |
kotakku | 0:b1ce54272580 | 120 | bool reportChanged; // The data is send when data is received from the controller |
kotakku | 0:b1ce54272580 | 121 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 122 | |
kotakku | 0:b1ce54272580 | 123 | /** This class parses all the data sent by the PS4 controller */ |
kotakku | 0:b1ce54272580 | 124 | class PS4Parser { |
kotakku | 0:b1ce54272580 | 125 | public: |
kotakku | 0:b1ce54272580 | 126 | /** Constructor for the PS4Parser class. */ |
kotakku | 0:b1ce54272580 | 127 | PS4Parser() { |
kotakku | 0:b1ce54272580 | 128 | Reset(); |
kotakku | 0:b1ce54272580 | 129 | }; |
kotakku | 0:b1ce54272580 | 130 | |
kotakku | 0:b1ce54272580 | 131 | /** @name PS4 Controller functions */ |
kotakku | 0:b1ce54272580 | 132 | /** |
kotakku | 0:b1ce54272580 | 133 | * getButtonPress(ButtonEnum b) will return true as long as the button is held down. |
kotakku | 0:b1ce54272580 | 134 | * |
kotakku | 0:b1ce54272580 | 135 | * While getButtonClick(ButtonEnum b) will only return it once. |
kotakku | 0:b1ce54272580 | 136 | * |
kotakku | 0:b1ce54272580 | 137 | * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), |
kotakku | 0:b1ce54272580 | 138 | * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). |
kotakku | 0:b1ce54272580 | 139 | * @param b ::ButtonEnum to read. |
kotakku | 0:b1ce54272580 | 140 | * @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 | 141 | */ |
kotakku | 0:b1ce54272580 | 142 | bool getButtonPress(ButtonEnum b); |
kotakku | 0:b1ce54272580 | 143 | bool getButtonClick(ButtonEnum b); |
kotakku | 0:b1ce54272580 | 144 | /**@}*/ |
kotakku | 0:b1ce54272580 | 145 | /** @name PS4 Controller functions */ |
kotakku | 0:b1ce54272580 | 146 | /** |
kotakku | 0:b1ce54272580 | 147 | * Used to get the analog value from button presses. |
kotakku | 0:b1ce54272580 | 148 | * @param b The ::ButtonEnum to read. |
kotakku | 0:b1ce54272580 | 149 | * The supported buttons are: |
kotakku | 0:b1ce54272580 | 150 | * ::L2 and ::R2. |
kotakku | 0:b1ce54272580 | 151 | * @return Analog value in the range of 0-255. |
kotakku | 0:b1ce54272580 | 152 | */ |
kotakku | 0:b1ce54272580 | 153 | uint8_t getAnalogButton(ButtonEnum b); |
kotakku | 0:b1ce54272580 | 154 | |
kotakku | 0:b1ce54272580 | 155 | /** |
kotakku | 0:b1ce54272580 | 156 | * Used to read the analog joystick. |
kotakku | 0:b1ce54272580 | 157 | * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY. |
kotakku | 0:b1ce54272580 | 158 | * @return Return the analog value in the range of 0-255. |
kotakku | 0:b1ce54272580 | 159 | */ |
kotakku | 0:b1ce54272580 | 160 | uint8_t getAnalogHat(AnalogHatEnum a); |
kotakku | 0:b1ce54272580 | 161 | |
kotakku | 0:b1ce54272580 | 162 | /** |
kotakku | 0:b1ce54272580 | 163 | * Get the x-coordinate of the touchpad. Position 0 is in the top left. |
kotakku | 0:b1ce54272580 | 164 | * @param finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. |
kotakku | 0:b1ce54272580 | 165 | * @param xyId The controller sends out three packets with the same structure. |
kotakku | 0:b1ce54272580 | 166 | * The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. |
kotakku | 0:b1ce54272580 | 167 | * For that reason it will be set to 0 if the argument is omitted. |
kotakku | 0:b1ce54272580 | 168 | * @return Returns the x-coordinate of the finger. |
kotakku | 0:b1ce54272580 | 169 | */ |
kotakku | 0:b1ce54272580 | 170 | uint16_t getX(uint8_t finger = 0, uint8_t xyId = 0) { |
kotakku | 0:b1ce54272580 | 171 | return ps4Data.xy[xyId].finger[finger].x; |
kotakku | 0:b1ce54272580 | 172 | }; |
kotakku | 0:b1ce54272580 | 173 | |
kotakku | 0:b1ce54272580 | 174 | /** |
kotakku | 0:b1ce54272580 | 175 | * Get the y-coordinate of the touchpad. Position 0 is in the top left. |
kotakku | 0:b1ce54272580 | 176 | * @param finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. |
kotakku | 0:b1ce54272580 | 177 | * @param xyId The controller sends out three packets with the same structure. |
kotakku | 0:b1ce54272580 | 178 | * The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. |
kotakku | 0:b1ce54272580 | 179 | * For that reason it will be set to 0 if the argument is omitted. |
kotakku | 0:b1ce54272580 | 180 | * @return Returns the y-coordinate of the finger. |
kotakku | 0:b1ce54272580 | 181 | */ |
kotakku | 0:b1ce54272580 | 182 | uint16_t getY(uint8_t finger = 0, uint8_t xyId = 0) { |
kotakku | 0:b1ce54272580 | 183 | return ps4Data.xy[xyId].finger[finger].y; |
kotakku | 0:b1ce54272580 | 184 | }; |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | /** |
kotakku | 0:b1ce54272580 | 187 | * Returns whenever the user is toucing the touchpad. |
kotakku | 0:b1ce54272580 | 188 | * @param finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. |
kotakku | 0:b1ce54272580 | 189 | * @param xyId The controller sends out three packets with the same structure. |
kotakku | 0:b1ce54272580 | 190 | * The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. |
kotakku | 0:b1ce54272580 | 191 | * For that reason it will be set to 0 if the argument is omitted. |
kotakku | 0:b1ce54272580 | 192 | * @return Returns true if the specific finger is touching the touchpad. |
kotakku | 0:b1ce54272580 | 193 | */ |
kotakku | 0:b1ce54272580 | 194 | bool isTouching(uint8_t finger = 0, uint8_t xyId = 0) { |
kotakku | 0:b1ce54272580 | 195 | return !(ps4Data.xy[xyId].finger[finger].touching); // The bit is cleared when a finger is touching the touchpad |
kotakku | 0:b1ce54272580 | 196 | }; |
kotakku | 0:b1ce54272580 | 197 | |
kotakku | 0:b1ce54272580 | 198 | /** |
kotakku | 0:b1ce54272580 | 199 | * This counter increments every time a finger touches the touchpad. |
kotakku | 0:b1ce54272580 | 200 | * @param finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. |
kotakku | 0:b1ce54272580 | 201 | * @param xyId The controller sends out three packets with the same structure. |
kotakku | 0:b1ce54272580 | 202 | * The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. |
kotakku | 0:b1ce54272580 | 203 | * For that reason it will be set to 0 if the argument is omitted. |
kotakku | 0:b1ce54272580 | 204 | * @return Return the value of the counter, note that it is only a 7-bit value. |
kotakku | 0:b1ce54272580 | 205 | */ |
kotakku | 0:b1ce54272580 | 206 | uint8_t getTouchCounter(uint8_t finger = 0, uint8_t xyId = 0) { |
kotakku | 0:b1ce54272580 | 207 | return ps4Data.xy[xyId].finger[finger].counter; |
kotakku | 0:b1ce54272580 | 208 | }; |
kotakku | 0:b1ce54272580 | 209 | |
kotakku | 0:b1ce54272580 | 210 | /** |
kotakku | 0:b1ce54272580 | 211 | * Get the angle of the controller calculated using the accelerometer. |
kotakku | 0:b1ce54272580 | 212 | * @param a Either ::Pitch or ::Roll. |
kotakku | 0:b1ce54272580 | 213 | * @return Return the angle in the range of 0-360. |
kotakku | 0:b1ce54272580 | 214 | */ |
kotakku | 0:b1ce54272580 | 215 | float getAngle(AngleEnum a) { |
kotakku | 0:b1ce54272580 | 216 | if (a == Pitch) |
kotakku | 0:b1ce54272580 | 217 | return (atan2f(ps4Data.accY, ps4Data.accZ) + PI) * RAD_TO_DEG; |
kotakku | 0:b1ce54272580 | 218 | else |
kotakku | 0:b1ce54272580 | 219 | return (atan2f(ps4Data.accX, ps4Data.accZ) + PI) * RAD_TO_DEG; |
kotakku | 0:b1ce54272580 | 220 | }; |
kotakku | 0:b1ce54272580 | 221 | |
kotakku | 0:b1ce54272580 | 222 | /** |
kotakku | 0:b1ce54272580 | 223 | * Used to get the raw values from the 3-axis gyroscope and 3-axis accelerometer inside the PS4 controller. |
kotakku | 0:b1ce54272580 | 224 | * @param s The sensor to read. |
kotakku | 0:b1ce54272580 | 225 | * @return Returns the raw sensor reading. |
kotakku | 0:b1ce54272580 | 226 | */ |
kotakku | 0:b1ce54272580 | 227 | int16_t getSensor(SensorEnum s) { |
kotakku | 0:b1ce54272580 | 228 | switch(s) { |
kotakku | 0:b1ce54272580 | 229 | case gX: |
kotakku | 0:b1ce54272580 | 230 | return ps4Data.gyroX; |
kotakku | 0:b1ce54272580 | 231 | case gY: |
kotakku | 0:b1ce54272580 | 232 | return ps4Data.gyroY; |
kotakku | 0:b1ce54272580 | 233 | case gZ: |
kotakku | 0:b1ce54272580 | 234 | return ps4Data.gyroZ; |
kotakku | 0:b1ce54272580 | 235 | case aX: |
kotakku | 0:b1ce54272580 | 236 | return ps4Data.accX; |
kotakku | 0:b1ce54272580 | 237 | case aY: |
kotakku | 0:b1ce54272580 | 238 | return ps4Data.accY; |
kotakku | 0:b1ce54272580 | 239 | case aZ: |
kotakku | 0:b1ce54272580 | 240 | return ps4Data.accZ; |
kotakku | 0:b1ce54272580 | 241 | default: |
kotakku | 0:b1ce54272580 | 242 | return 0; |
kotakku | 0:b1ce54272580 | 243 | } |
kotakku | 0:b1ce54272580 | 244 | }; |
kotakku | 0:b1ce54272580 | 245 | |
kotakku | 0:b1ce54272580 | 246 | /** |
kotakku | 0:b1ce54272580 | 247 | * Return the battery level of the PS4 controller. |
kotakku | 0:b1ce54272580 | 248 | * @return The battery level in the range 0-15. |
kotakku | 0:b1ce54272580 | 249 | */ |
kotakku | 0:b1ce54272580 | 250 | uint8_t getBatteryLevel() { |
kotakku | 0:b1ce54272580 | 251 | return ps4Data.status.battery; |
kotakku | 0:b1ce54272580 | 252 | }; |
kotakku | 0:b1ce54272580 | 253 | |
kotakku | 0:b1ce54272580 | 254 | /** |
kotakku | 0:b1ce54272580 | 255 | * Use this to check if an USB cable is connected to the PS4 controller. |
kotakku | 0:b1ce54272580 | 256 | * @return Returns true if an USB cable is connected. |
kotakku | 0:b1ce54272580 | 257 | */ |
kotakku | 0:b1ce54272580 | 258 | bool getUsbStatus() { |
kotakku | 0:b1ce54272580 | 259 | return ps4Data.status.usb; |
kotakku | 0:b1ce54272580 | 260 | }; |
kotakku | 0:b1ce54272580 | 261 | |
kotakku | 0:b1ce54272580 | 262 | /** |
kotakku | 0:b1ce54272580 | 263 | * Use this to check if an audio jack cable is connected to the PS4 controller. |
kotakku | 0:b1ce54272580 | 264 | * @return Returns true if an audio jack cable is connected. |
kotakku | 0:b1ce54272580 | 265 | */ |
kotakku | 0:b1ce54272580 | 266 | bool getAudioStatus() { |
kotakku | 0:b1ce54272580 | 267 | return ps4Data.status.audio; |
kotakku | 0:b1ce54272580 | 268 | }; |
kotakku | 0:b1ce54272580 | 269 | |
kotakku | 0:b1ce54272580 | 270 | /** |
kotakku | 0:b1ce54272580 | 271 | * Use this to check if a microphone is connected to the PS4 controller. |
kotakku | 0:b1ce54272580 | 272 | * @return Returns true if a microphone is connected. |
kotakku | 0:b1ce54272580 | 273 | */ |
kotakku | 0:b1ce54272580 | 274 | bool getMicStatus() { |
kotakku | 0:b1ce54272580 | 275 | return ps4Data.status.mic; |
kotakku | 0:b1ce54272580 | 276 | }; |
kotakku | 0:b1ce54272580 | 277 | |
kotakku | 0:b1ce54272580 | 278 | /** Turn both rumble and the LEDs off. */ |
kotakku | 0:b1ce54272580 | 279 | void setAllOff() { |
kotakku | 0:b1ce54272580 | 280 | setRumbleOff(); |
kotakku | 0:b1ce54272580 | 281 | setLedOff(); |
kotakku | 0:b1ce54272580 | 282 | }; |
kotakku | 0:b1ce54272580 | 283 | |
kotakku | 0:b1ce54272580 | 284 | /** Set rumble off. */ |
kotakku | 0:b1ce54272580 | 285 | void setRumbleOff() { |
kotakku | 0:b1ce54272580 | 286 | setRumbleOn(0, 0); |
kotakku | 0:b1ce54272580 | 287 | }; |
kotakku | 0:b1ce54272580 | 288 | |
kotakku | 0:b1ce54272580 | 289 | /** |
kotakku | 0:b1ce54272580 | 290 | * Turn on rumble. |
kotakku | 0:b1ce54272580 | 291 | * @param mode Either ::RumbleHigh or ::RumbleLow. |
kotakku | 0:b1ce54272580 | 292 | */ |
kotakku | 0:b1ce54272580 | 293 | void setRumbleOn(RumbleEnum mode) { |
kotakku | 0:b1ce54272580 | 294 | if (mode == RumbleLow) |
kotakku | 0:b1ce54272580 | 295 | setRumbleOn(0x00, 0xFF); |
kotakku | 0:b1ce54272580 | 296 | else |
kotakku | 0:b1ce54272580 | 297 | setRumbleOn(0xFF, 0x00); |
kotakku | 0:b1ce54272580 | 298 | }; |
kotakku | 0:b1ce54272580 | 299 | |
kotakku | 0:b1ce54272580 | 300 | /** |
kotakku | 0:b1ce54272580 | 301 | * Turn on rumble. |
kotakku | 0:b1ce54272580 | 302 | * @param bigRumble Value for big motor. |
kotakku | 0:b1ce54272580 | 303 | * @param smallRumble Value for small motor. |
kotakku | 0:b1ce54272580 | 304 | */ |
kotakku | 0:b1ce54272580 | 305 | void setRumbleOn(uint8_t bigRumble, uint8_t smallRumble) { |
kotakku | 0:b1ce54272580 | 306 | ps4Output.bigRumble = bigRumble; |
kotakku | 0:b1ce54272580 | 307 | ps4Output.smallRumble = smallRumble; |
kotakku | 0:b1ce54272580 | 308 | ps4Output.reportChanged = true; |
kotakku | 0:b1ce54272580 | 309 | }; |
kotakku | 0:b1ce54272580 | 310 | |
kotakku | 0:b1ce54272580 | 311 | /** Turn all LEDs off. */ |
kotakku | 0:b1ce54272580 | 312 | void setLedOff() { |
kotakku | 0:b1ce54272580 | 313 | setLed(0, 0, 0); |
kotakku | 0:b1ce54272580 | 314 | }; |
kotakku | 0:b1ce54272580 | 315 | |
kotakku | 0:b1ce54272580 | 316 | /** |
kotakku | 0:b1ce54272580 | 317 | * Use this to set the color using RGB values. |
kotakku | 0:b1ce54272580 | 318 | * @param r,g,b RGB value. |
kotakku | 0:b1ce54272580 | 319 | */ |
kotakku | 0:b1ce54272580 | 320 | void setLed(uint8_t r, uint8_t g, uint8_t b) { |
kotakku | 0:b1ce54272580 | 321 | ps4Output.r = r; |
kotakku | 0:b1ce54272580 | 322 | ps4Output.g = g; |
kotakku | 0:b1ce54272580 | 323 | ps4Output.b = b; |
kotakku | 0:b1ce54272580 | 324 | ps4Output.reportChanged = true; |
kotakku | 0:b1ce54272580 | 325 | }; |
kotakku | 0:b1ce54272580 | 326 | |
kotakku | 0:b1ce54272580 | 327 | /** |
kotakku | 0:b1ce54272580 | 328 | * Use this to set the color using the predefined colors in ::ColorsEnum. |
kotakku | 0:b1ce54272580 | 329 | * @param color The desired color. |
kotakku | 0:b1ce54272580 | 330 | */ |
kotakku | 0:b1ce54272580 | 331 | void setLed(ColorsEnum color) { |
kotakku | 0:b1ce54272580 | 332 | setLed((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color)); |
kotakku | 0:b1ce54272580 | 333 | }; |
kotakku | 0:b1ce54272580 | 334 | |
kotakku | 0:b1ce54272580 | 335 | /** |
kotakku | 0:b1ce54272580 | 336 | * Set the LEDs flash time. |
kotakku | 0:b1ce54272580 | 337 | * @param flashOn Time to flash bright (255 = 2.5 seconds). |
kotakku | 0:b1ce54272580 | 338 | * @param flashOff Time to flash dark (255 = 2.5 seconds). |
kotakku | 0:b1ce54272580 | 339 | */ |
kotakku | 0:b1ce54272580 | 340 | void setLedFlash(uint8_t flashOn, uint8_t flashOff) { |
kotakku | 0:b1ce54272580 | 341 | ps4Output.flashOn = flashOn; |
kotakku | 0:b1ce54272580 | 342 | ps4Output.flashOff = flashOff; |
kotakku | 0:b1ce54272580 | 343 | ps4Output.reportChanged = true; |
kotakku | 0:b1ce54272580 | 344 | }; |
kotakku | 0:b1ce54272580 | 345 | /**@}*/ |
kotakku | 0:b1ce54272580 | 346 | |
kotakku | 0:b1ce54272580 | 347 | protected: |
kotakku | 0:b1ce54272580 | 348 | /** |
kotakku | 0:b1ce54272580 | 349 | * Used to parse data sent from the PS4 controller. |
kotakku | 0:b1ce54272580 | 350 | * @param len Length of the data. |
kotakku | 0:b1ce54272580 | 351 | * @param buf Pointer to the data buffer. |
kotakku | 0:b1ce54272580 | 352 | */ |
kotakku | 0:b1ce54272580 | 353 | void Parse(uint8_t len, uint8_t *buf); |
kotakku | 0:b1ce54272580 | 354 | |
kotakku | 0:b1ce54272580 | 355 | /** Used to reset the different buffers to their default values */ |
kotakku | 0:b1ce54272580 | 356 | void Reset(); |
kotakku | 0:b1ce54272580 | 357 | |
kotakku | 0:b1ce54272580 | 358 | /** |
kotakku | 0:b1ce54272580 | 359 | * Send the output to the PS4 controller. This is implemented in PS4BT.h and PS4USB.h. |
kotakku | 0:b1ce54272580 | 360 | * @param output Pointer to PS4Output buffer; |
kotakku | 0:b1ce54272580 | 361 | */ |
kotakku | 0:b1ce54272580 | 362 | virtual void sendOutputReport(PS4Output *output) = 0; |
kotakku | 0:b1ce54272580 | 363 | |
kotakku | 0:b1ce54272580 | 364 | private: |
kotakku | 0:b1ce54272580 | 365 | bool checkDpad(ButtonEnum b); // Used to check PS4 DPAD buttons |
kotakku | 0:b1ce54272580 | 366 | |
kotakku | 0:b1ce54272580 | 367 | PS4Data ps4Data; |
kotakku | 0:b1ce54272580 | 368 | PS4Buttons oldButtonState, buttonClickState; |
kotakku | 0:b1ce54272580 | 369 | PS4Output ps4Output; |
kotakku | 0:b1ce54272580 | 370 | uint8_t oldDpad; |
kotakku | 0:b1ce54272580 | 371 | }; |
kotakku | 0:b1ce54272580 | 372 | #endif |
kotakku | 0:b1ce54272580 | 373 |