A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.

Dependencies:   TextLCD mbed

Fork of mbed_TANK_PS3 by Yasuhiko YAMAMOTO

Revision:
1:ae49669c5e92
Parent:
0:44619612f575
--- a/main.cpp	Tue Jul 05 08:25:40 2011 +0000
+++ b/main.cpp	Sun Dec 30 05:16:28 2012 +0000
@@ -23,99 +23,92 @@
 #include "mbed.h"
 #include "USBHost.h"
 #include "Utils.h"
-#include "FATFileSystem.h"
-
-int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize);
-int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
-int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
+#include "m3pi.h"
+#include "ps3BT.h"
 
-class USBFileSystem : public FATFileSystem
-{
-    int _device;
-    u32 _blockSize;
-    u32 _blockCount;
-    
-public:
-    USBFileSystem() : FATFileSystem("usb"),_device(0),_blockSize(0),_blockCount(0)
-    {
-    }
-    
-    void SetDevice(int device)
-    {
-        _device = device;
-    }
-    
-    virtual int disk_initialize()
-    {
-        return MassStorage_ReadCapacity(_device,&_blockCount,&_blockSize);
-    }
-    
-    virtual int disk_write(const char *buffer, int block_number)
-    {
-        return MassStorage_Write(_device,block_number,1,(u8*)buffer,_blockSize);
-    }
+typedef struct {        
+     u8 ReportType;     //Report Type 01
+     u8 Reserved1;      // Unknown
+     u16  ButtonState;    // Main buttons
+     u8 PSButtonState;  // PS button
+     u8 Reserved2;      // Unknown
+     u8 LeftStickX;     // left Joystick X axis 0 - 255, 128 is mid
+     u8 LeftStickY;     // left Joystick Y axis 0 - 255, 128 is mid
+     u8 RightStickX;    // right Joystick X axis 0 - 255, 128 is mid
+     u8 RightStickY;    // right Joystick Y axis 0 - 255, 128 is mid
+     u8 Reserved3[4];   // Unknown
+     u8 PressureUp;     // digital Pad Up button Pressure 0 - 255
+     u8 PressureRight;  // digital Pad Right button Pressure 0 - 255
+     u8 PressureDown;   // digital Pad Down button Pressure 0 - 255
+     u8 PressureLeft;   // digital Pad Left button Pressure 0 - 255
+     u8 PressureL2;     // digital Pad L2 button Pressure 0 - 255
+     u8 PressureR2;     // digital Pad R2 button Pressure 0 - 255
+     u8 PressureL1;     // digital Pad L1 button Pressure 0 - 255
+     u8 PressureR1;     // digital Pad R1 button Pressure 0 - 255
+     u8 PressureTriangle;   // digital Pad Triangle button Pressure 0 - 255
+     u8 PressureCircle;     // digital Pad Circle button Pressure 0 - 255
+     u8 PressureCross;      // digital Pad Cross button Pressure 0 - 255
+     u8 PressureSquare;     // digital Pad Square button Pressure 0 - 255
+     u8 Reserved4[3];   // Unknown
+     u8 Charge;         // charging status ? 02 = charge, 03 = normal
+     u8 Power;          // Battery status ?
+     u8 Connection;     // Connection Type ?
+     u8 Reserved5[9];   // Unknown
+     u16 AccelX;          // X axis accelerometer Big Endian 0 - 1023
+     u16 AccelY;          // Y axis accelerometer Big Endian 0 - 1023
+     u16 AccelZ;          // Z axis accelerometer Big Endian 0 - 1023
+     u16 GyroZ;           // Z axis Gyro Big Endian 0 - 1023
     
-    virtual int disk_read(char *buffer, int block_number)
-    {
-        return MassStorage_Read(_device,block_number,1,(u8*)buffer,_blockSize);
-    }
-        
-    virtual int disk_sectors()
-    {
-        return _blockCount;
-    }
-};
-
-void DumpFS(int depth, int count)
-{
-    DIR *d = opendir("/usb");
-    if (!d)
-    {
-        printf("USB file system borked\r\n");
-        return;
-    }
-
-    printf("\nDumping root dir\r\n");
-    struct dirent *p;
-    for(;;)
-    {
-        p = readdir(d);
-        if (!p)
-            break;
-        int len = sizeof( dirent);
-        printf("%s %d\n", p->d_name, len);
-    }
-    closedir(d);
-}
-
-int OnDiskInsert(int device)
-{
-    USBFileSystem fs;
-    fs.SetDevice(device);
-    DumpFS(0,0);
-    return 0;
-}
-
-/*
-    Simple test shell to exercise mouse,keyboard,mass storage and hubs.
-    Add 2 15k pulldown resistors between D+/D- and ground, attach a usb socket and have at it.
-*/
+} ps3report;
 
 Serial pc(USBTX, USBRX);
-int GetConsoleChar()
-{
-    if (!pc.readable())
-        return -1;
-    char c = pc.getc();
-    pc.putc(c); // echo
-    return c;
+m3pi m3pi;
+
+void PS3_data(const u8* data) {
+    float speed;
+    ps3report* _ps3report = (ps3report*)data;
+    PS3BT ps3;
+    ps3.decode(data);
+    ps3.dump(data);
+    if(100 <= _ps3report->LeftStickY && _ps3report->LeftStickY <= 155)
+        {
+            m3pi.stop();
+        } else if(0 <= _ps3report->LeftStickY && _ps3report->LeftStickY <= 99) {
+            speed = (100 - _ps3report->LeftStickY) * 0.01;
+            m3pi.left_motor(speed);
+            printf("A:%f", speed);
+        } else {
+            speed = (155 - _ps3report->LeftStickY) * 0.01;
+            m3pi.left_motor(speed);
+            printf("A:%f", speed);
+        }
+        
+        if(100 <= _ps3report->RightStickY && _ps3report->RightStickY <= 155)
+        {
+            m3pi.stop();
+        } else if(0 <= _ps3report->RightStickY && _ps3report->RightStickY <= 99) {
+            speed = (100 - _ps3report->RightStickY) * 0.01;
+            m3pi.right_motor(speed);
+            printf("B:%f", speed);
+        } else {
+            speed = (155 - _ps3report->RightStickY) * 0.01;
+            m3pi.right_motor(speed);
+            printf("B:%f", speed);
+        }
+    
+    
+    
 }
-
-void TestShell();
-
 int main()
 {
-    pc.baud(460800);
-    printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\r\n");
-    TestShell();
+    pc.baud(230400);
+    
+    m3pi.locate(0,1);
+    m3pi.printf("M3pi - PS3");
+    
+    USBInit();
+    
+    while(1){
+    USBLoop();
+    }
 }