class library to access fischertechnik interfaces via USB
Dependencies: FatFileSystem mbed myBlueUSB neigbourhood rfcomm sdp
Diff: main.cpp
- Revision:
- 1:4676e8b9b357
- Parent:
- 0:7da612835693
--- a/main.cpp Wed Jun 15 19:12:25 2011 +0000 +++ b/main.cpp Mon Mar 11 08:04:37 2013 +0000 @@ -27,6 +27,93 @@ #include "ftlibclassusb.h" #include "ftlibclasstxc.h" #include "ftlibclasstxcbt.h" +#include "neighbourhood.h" +#include "btserial.h" + +class tx_emul { + typedef void (*handler)(unsigned char *, int); + int sock; + unsigned char sndbuf[20], recbuf[20]; + int pos; + int reclen, chksum; + handler onMsg; + void send(int len) { + sndbuf[2] = len; + sndbuf[len+3] = 0; + for (int i = 2; i < len + 3; i++) + sndbuf[len+3] += sndbuf[i]; + Socket_Send(sock, sndbuf, len+4); + printfBytes("Sent msg:", sndbuf, len+4); + } + static void callback(int socket, SocketState state, const unsigned char *data, int len, void* userData) { + tx_emul *self = (tx_emul*)userData; + self->cb(socket, state, data, len); + } + void cb(int socket, SocketState state, const unsigned char *data, int len) { + int i = 0; + if (state == SocketState_Open) + if (len > 0) + while (i<len) { + switch (pos) { + case 0: + if (data[i] == 0xBE) pos = 1; + break; + case 1: + if (data[i] == 0xEF) pos = 2; + else pos = 0; + break; + case 2: + chksum = reclen = data[i]; + break; + default: + if (pos < reclen+3) { + chksum += data[i]; + recbuf[pos-3] = data[i]; + pos++; + } else { + if (chksum==data[i]) { + if (onMsg) onMsg(recbuf, reclen); + printfBytes("BEEF msg:", recbuf, reclen); + } else + printf("BEEF checksum error, should be %02x, got %02x\n", chksum, data[i]); + pos = 0; + } + } + i++; + } + else { + const char *ack = "CONN_ACK"; + strcpy((char*)sndbuf+3, ack); + send(strlen(ack)); + printf("Sent %s\n", ack); + } + else if (state == SocketState_Closed) { + //reopen it again + pos = 0; + sock = Socket_Listen(SOCKET_RFCOM, 1, callback, this); + } + } + +public: + tx_emul(handler h=0): onMsg(h) { + sndbuf[0] = 0xBE; + sndbuf[1] = 0xEF; + pos = 0; + sock = Socket_Listen(SOCKET_RFCOM, 1, callback, this); + } + ~tx_emul() { + Socket_Close(sock); + } + void sendMsg(unsigned char hwid, unsigned char subid, unsigned short cmd, unsigned short val) { + sndbuf[3] = hwid; + sndbuf[4] = subid; + sndbuf[5] = cmd; + sndbuf[6] = cmd>>8; + sndbuf[7] = val; + sndbuf[8] = val>>8; + send(6); + } +}; Serial pc(USBTX, USBRX); @@ -42,14 +129,18 @@ Timer time; time.start(); int n, r0, r1, r2, r3=0; -// ftusbdev* h = 0; + // ftusbdev* h = 0; ftusbdevtxbt* h = 0; + tx_emul *fttxbt = 0; printf("Start enumeration\n"); for (;;) { switch (state) { case 0: //enumeration, allow 3 seconds if (time.read() >= 20) { - state = 1; + if (tx_emulation) + state = 10; + else + state = 1; printf("Start initialisation\n"); } break; @@ -78,7 +169,7 @@ } r2 = h->OpenFtUsbDevice(); printf("OpenFtUsbDevice() returns %08x\n", r2); -// ta = h->GetFtTransferAreaAddress(); + // ta = h->GetFtTransferAreaAddress(); tax = h->GetFtTransferAreaAddress(0); r3 = h->StartFtTransferArea(&ne); if (r3) { @@ -92,9 +183,9 @@ 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\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; @@ -105,10 +196,17 @@ h->StopFtTransferArea(); h->CloseFtDevice(); ftlib::CloseFtLib(); + //neighbors->write(); //succeeds but nothing gets written state = 4; break; case 4: break; + case 10://init tx emulation + fttxbt = new tx_emul; + state = 11; + break; + case 11: + break; default: return 0; } @@ -120,71 +218,3 @@ 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