class library to access fischertechnik interfaces via USB

Dependencies:   FatFileSystem mbed myBlueUSB neigbourhood rfcomm sdp

Revision:
0:7da612835693
Child:
1:4676e8b9b357
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 15 19:12:25 2011 +0000
@@ -0,0 +1,190 @@
+/*
+Copyright (c) 2010 Peter Barrett
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "mbed.h"
+#include "USBHost.h"
+#include "Utils.h"
+#include "ftlibclass.h"
+#include "ftlibclassusb.h"
+#include "ftlibclasstxc.h"
+#include "ftlibclasstxcbt.h"
+
+Serial pc(USBTX, USBRX);
+
+FT_TRANSFER_AREA *ta = 0;
+TA *tax = 0;
+NOTIFICATION_EVENTS ne;
+
+int main() {
+    pc.baud(460800);
+    printf("USBShell\nNow get a bunch of usb things and plug them in\n");
+    USBInit();
+    int state = 0;
+    Timer time;
+    time.start();
+    int n, r0, r1, r2, r3=0;
+//    ftusbdev* h = 0;
+    ftusbdevtxbt* h = 0;
+    printf("Start enumeration\n");
+    for (;;) {
+        switch (state) {
+            case 0: //enumeration, allow 3 seconds
+                if (time.read() >= 20) {
+                    state = 1;
+                    printf("Start initialisation\n");
+                }
+                break;
+            case 1: //init program
+                r0 = ftlib::InitFtLib();
+                printf("InitFtLib() returns %d\n", r0);
+                r1 = ftusbdevtxbt::InitFtUsbDeviceList();
+                printf("InitFtUsbDeviceList() returns %d\n", r1);
+                n = ftusbdev::GetNumFtUsbDevice();
+                printf("GetNumFtUsbDevice() returns %d\n", n);
+                for (int i = 0; i < n; i++) {
+                    ftdev* h = ftusbdev::GetFtUsbDeviceHandle(i);
+                    printf("handle = %p\n", h);
+                    unsigned sn = h->GetFtSerialNr();
+                    printf("S/N=%d\n", sn);
+                    char *name = h->GetFtLongNameStrg();
+                    printf("%d: %s SN=%08d\n", i, name, sn);
+                    delete[] name;
+                    //USBLoop();
+                }
+                h = dynamic_cast<ftusbdevtxbt*>(ftusbdev::GetFtUsbDeviceHandle(0));
+                if (h==0) {
+                    printf("Cast failed\n");
+                    state = 4;
+                    break;
+                }
+                r2 = h->OpenFtUsbDevice();
+                printf("OpenFtUsbDevice() returns %08x\n", r2);
+//                ta = h->GetFtTransferAreaAddress();
+                tax = h->GetFtTransferAreaAddress(0);
+                r3 = h->StartFtTransferArea(&ne);
+                if (r3) {
+                    printf("Could not start thread %08X\n", r3);
+                    state = 3;
+                    break;
+                }
+                state = 2;
+                printf("Start main loop\n");
+                break;
+            case 2: //main loop
+                ftlib::poll();
+                /*                if (ta->ChangeEg) {
+                                    printf("%02X %d %d %d\n", ta->E_Main, ta->AX, ta->A1, ta->AV);
+                                    ta->ChangeEg = 0;
+                                }*/
+                //printf("%02X %d %d %d\r", tax->input.uni[0],tax->input.uni[1],tax->input.uni[2],tax->input.uni[3]);
+                if (time.read() >= 30) {
+                    state = 3;
+                    printf("\nEnd of main loop\n");
+                }
+                break;
+            case 3: //finalizing
+                h->StopFtTransferArea();
+                h->CloseFtDevice();
+                ftlib::CloseFtLib();
+                state = 4;
+                break;
+            case 4:
+                break;
+            default:
+                return 0;
+        }
+        USBLoop();
+    }
+}
+
+int OnDiskInsert(int device) {
+    return 0;
+}
+
+
+#if 0
+#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);
+
+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);
+    }
+
+    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\n");
+        return;
+    }
+
+    printf("\nDumping root dir\n");
+    struct dirent *p;
+    while ((p = readdir(d))) {
+        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.
+*/
+
+#endif
\ No newline at end of file