![](/media/cache/profiles/8474b8609e772af467ac0fc4acad4dd4.jpg.50x50_q85.jpg)
A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.
Fork of mbed_TANK_PS3 by
main.cpp@2:895f70862eb9, 2013-01-01 (annotated)
- Committer:
- srsmitherman
- Date:
- Tue Jan 01 02:10:08 2013 +0000
- Revision:
- 2:895f70862eb9
- Parent:
- 1:ae49669c5e92
M3pi support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenbumono | 0:44619612f575 | 1 | /* |
kenbumono | 0:44619612f575 | 2 | Copyright (c) 2010 Peter Barrett |
kenbumono | 0:44619612f575 | 3 | |
kenbumono | 0:44619612f575 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kenbumono | 0:44619612f575 | 5 | of this software and associated documentation files (the "Software"), to deal |
kenbumono | 0:44619612f575 | 6 | in the Software without restriction, including without limitation the rights |
kenbumono | 0:44619612f575 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenbumono | 0:44619612f575 | 8 | copies of the Software, and to permit persons to whom the Software is |
kenbumono | 0:44619612f575 | 9 | furnished to do so, subject to the following conditions: |
kenbumono | 0:44619612f575 | 10 | |
kenbumono | 0:44619612f575 | 11 | The above copyright notice and this permission notice shall be included in |
kenbumono | 0:44619612f575 | 12 | all copies or substantial portions of the Software. |
kenbumono | 0:44619612f575 | 13 | |
kenbumono | 0:44619612f575 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenbumono | 0:44619612f575 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenbumono | 0:44619612f575 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenbumono | 0:44619612f575 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenbumono | 0:44619612f575 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenbumono | 0:44619612f575 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kenbumono | 0:44619612f575 | 20 | THE SOFTWARE. |
kenbumono | 0:44619612f575 | 21 | */ |
kenbumono | 0:44619612f575 | 22 | |
kenbumono | 0:44619612f575 | 23 | #include "mbed.h" |
kenbumono | 0:44619612f575 | 24 | #include "USBHost.h" |
kenbumono | 0:44619612f575 | 25 | #include "Utils.h" |
srsmitherman | 1:ae49669c5e92 | 26 | #include "m3pi.h" |
srsmitherman | 1:ae49669c5e92 | 27 | #include "ps3BT.h" |
kenbumono | 0:44619612f575 | 28 | |
srsmitherman | 1:ae49669c5e92 | 29 | typedef struct { |
srsmitherman | 1:ae49669c5e92 | 30 | u8 ReportType; //Report Type 01 |
srsmitherman | 1:ae49669c5e92 | 31 | u8 Reserved1; // Unknown |
srsmitherman | 1:ae49669c5e92 | 32 | u16 ButtonState; // Main buttons |
srsmitherman | 1:ae49669c5e92 | 33 | u8 PSButtonState; // PS button |
srsmitherman | 1:ae49669c5e92 | 34 | u8 Reserved2; // Unknown |
srsmitherman | 1:ae49669c5e92 | 35 | u8 LeftStickX; // left Joystick X axis 0 - 255, 128 is mid |
srsmitherman | 1:ae49669c5e92 | 36 | u8 LeftStickY; // left Joystick Y axis 0 - 255, 128 is mid |
srsmitherman | 1:ae49669c5e92 | 37 | u8 RightStickX; // right Joystick X axis 0 - 255, 128 is mid |
srsmitherman | 1:ae49669c5e92 | 38 | u8 RightStickY; // right Joystick Y axis 0 - 255, 128 is mid |
srsmitherman | 1:ae49669c5e92 | 39 | u8 Reserved3[4]; // Unknown |
srsmitherman | 1:ae49669c5e92 | 40 | u8 PressureUp; // digital Pad Up button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 41 | u8 PressureRight; // digital Pad Right button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 42 | u8 PressureDown; // digital Pad Down button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 43 | u8 PressureLeft; // digital Pad Left button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 44 | u8 PressureL2; // digital Pad L2 button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 45 | u8 PressureR2; // digital Pad R2 button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 46 | u8 PressureL1; // digital Pad L1 button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 47 | u8 PressureR1; // digital Pad R1 button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 48 | u8 PressureTriangle; // digital Pad Triangle button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 49 | u8 PressureCircle; // digital Pad Circle button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 50 | u8 PressureCross; // digital Pad Cross button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 51 | u8 PressureSquare; // digital Pad Square button Pressure 0 - 255 |
srsmitherman | 1:ae49669c5e92 | 52 | u8 Reserved4[3]; // Unknown |
srsmitherman | 1:ae49669c5e92 | 53 | u8 Charge; // charging status ? 02 = charge, 03 = normal |
srsmitherman | 1:ae49669c5e92 | 54 | u8 Power; // Battery status ? |
srsmitherman | 1:ae49669c5e92 | 55 | u8 Connection; // Connection Type ? |
srsmitherman | 1:ae49669c5e92 | 56 | u8 Reserved5[9]; // Unknown |
srsmitherman | 1:ae49669c5e92 | 57 | u16 AccelX; // X axis accelerometer Big Endian 0 - 1023 |
srsmitherman | 1:ae49669c5e92 | 58 | u16 AccelY; // Y axis accelerometer Big Endian 0 - 1023 |
srsmitherman | 1:ae49669c5e92 | 59 | u16 AccelZ; // Z axis accelerometer Big Endian 0 - 1023 |
srsmitherman | 1:ae49669c5e92 | 60 | u16 GyroZ; // Z axis Gyro Big Endian 0 - 1023 |
kenbumono | 0:44619612f575 | 61 | |
srsmitherman | 1:ae49669c5e92 | 62 | } ps3report; |
kenbumono | 0:44619612f575 | 63 | |
kenbumono | 0:44619612f575 | 64 | Serial pc(USBTX, USBRX); |
srsmitherman | 1:ae49669c5e92 | 65 | m3pi m3pi; |
srsmitherman | 1:ae49669c5e92 | 66 | |
srsmitherman | 1:ae49669c5e92 | 67 | void PS3_data(const u8* data) { |
srsmitherman | 1:ae49669c5e92 | 68 | float speed; |
srsmitherman | 1:ae49669c5e92 | 69 | ps3report* _ps3report = (ps3report*)data; |
srsmitherman | 1:ae49669c5e92 | 70 | PS3BT ps3; |
srsmitherman | 1:ae49669c5e92 | 71 | ps3.decode(data); |
srsmitherman | 1:ae49669c5e92 | 72 | ps3.dump(data); |
srsmitherman | 1:ae49669c5e92 | 73 | if(100 <= _ps3report->LeftStickY && _ps3report->LeftStickY <= 155) |
srsmitherman | 1:ae49669c5e92 | 74 | { |
srsmitherman | 1:ae49669c5e92 | 75 | m3pi.stop(); |
srsmitherman | 1:ae49669c5e92 | 76 | } else if(0 <= _ps3report->LeftStickY && _ps3report->LeftStickY <= 99) { |
srsmitherman | 1:ae49669c5e92 | 77 | speed = (100 - _ps3report->LeftStickY) * 0.01; |
srsmitherman | 1:ae49669c5e92 | 78 | m3pi.left_motor(speed); |
srsmitherman | 1:ae49669c5e92 | 79 | printf("A:%f", speed); |
srsmitherman | 1:ae49669c5e92 | 80 | } else { |
srsmitherman | 1:ae49669c5e92 | 81 | speed = (155 - _ps3report->LeftStickY) * 0.01; |
srsmitherman | 1:ae49669c5e92 | 82 | m3pi.left_motor(speed); |
srsmitherman | 1:ae49669c5e92 | 83 | printf("A:%f", speed); |
srsmitherman | 1:ae49669c5e92 | 84 | } |
srsmitherman | 1:ae49669c5e92 | 85 | |
srsmitherman | 1:ae49669c5e92 | 86 | if(100 <= _ps3report->RightStickY && _ps3report->RightStickY <= 155) |
srsmitherman | 1:ae49669c5e92 | 87 | { |
srsmitherman | 1:ae49669c5e92 | 88 | m3pi.stop(); |
srsmitherman | 1:ae49669c5e92 | 89 | } else if(0 <= _ps3report->RightStickY && _ps3report->RightStickY <= 99) { |
srsmitherman | 1:ae49669c5e92 | 90 | speed = (100 - _ps3report->RightStickY) * 0.01; |
srsmitherman | 1:ae49669c5e92 | 91 | m3pi.right_motor(speed); |
srsmitherman | 1:ae49669c5e92 | 92 | printf("B:%f", speed); |
srsmitherman | 1:ae49669c5e92 | 93 | } else { |
srsmitherman | 1:ae49669c5e92 | 94 | speed = (155 - _ps3report->RightStickY) * 0.01; |
srsmitherman | 1:ae49669c5e92 | 95 | m3pi.right_motor(speed); |
srsmitherman | 1:ae49669c5e92 | 96 | printf("B:%f", speed); |
srsmitherman | 1:ae49669c5e92 | 97 | } |
srsmitherman | 1:ae49669c5e92 | 98 | |
srsmitherman | 1:ae49669c5e92 | 99 | |
srsmitherman | 1:ae49669c5e92 | 100 | |
kenbumono | 0:44619612f575 | 101 | } |
kenbumono | 0:44619612f575 | 102 | int main() |
kenbumono | 0:44619612f575 | 103 | { |
srsmitherman | 1:ae49669c5e92 | 104 | pc.baud(230400); |
srsmitherman | 1:ae49669c5e92 | 105 | |
srsmitherman | 1:ae49669c5e92 | 106 | m3pi.locate(0,1); |
srsmitherman | 1:ae49669c5e92 | 107 | m3pi.printf("M3pi - PS3"); |
srsmitherman | 1:ae49669c5e92 | 108 | |
srsmitherman | 1:ae49669c5e92 | 109 | USBInit(); |
srsmitherman | 1:ae49669c5e92 | 110 | |
srsmitherman | 1:ae49669c5e92 | 111 | while(1){ |
srsmitherman | 1:ae49669c5e92 | 112 | USBLoop(); |
srsmitherman | 1:ae49669c5e92 | 113 | } |
kenbumono | 0:44619612f575 | 114 | } |