Ad van der Weiden / Mbed 2 deprecated tcpft

Dependencies:   mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 #include "myconfig.h"
00005 #define MBED
00006 #include "ft.h"
00007 #include "ftlib.h "
00008 #include "ftserver.h"
00009 #include "msgSerial.h"
00010 
00011 /****************************************************
00012  * pin configuration for ft test board
00013  *
00014  * 5 mosi for external SD card pin3 pin2=3.3V pin1=GND
00015  * 6 miso for external SD card pin4
00016  * 7 sck  for external SD card pin5
00017  * 11 cs  for external SD card pin6
00018  *
00019  * 8 dir for RS485
00020  * 9  TX for RS485/TTL/RS232(D)  (COM1)
00021  * 10 RX for RS485/TTL/RS232(D)
00022  *
00023  * 13 TX for RS232/MIDI          (COM2)
00024  * 14 RX for RS232/MIDI
00025  *
00026  * 12 spare6
00027  * 15 spare5
00028  * 16 spare4
00029  * 17 spare3
00030  * 18 spare2 (spare1 is GND)
00031  *
00032  * 19 analog1 (analog3 is +3.3V)
00033  * 20 analog2 (analog4 is GND)
00034  *
00035  * 21 motor en3-4
00036  * 22 motor 3
00037  * 23 motor 4
00038  * 24 motor 2
00039  * 25 motor 1
00040  * 26 motor en1-2
00041  *
00042  * 27 SCL
00043  * 28 SDA
00044  *
00045  * 29 yellow LED on MAGJACK
00046  * 30 green LED on MAGJACK
00047 *****************************************************/
00048 //#define FIO1PIN (*(unsigned long*)0x2009C034UL)
00049 #define FIO1PIN (LPC_GPIO1->FIOPIN)
00050 
00051 #define HWID    1
00052 #define SUBID   100
00053 
00054 EthernetNetIf eth;
00055 HTTPServer svr;
00056 ftServer *server;
00057 myConfig config;
00058 
00059 LocalFileSystem local("local");
00060 LocalFileSystem fs("webfs");
00061 
00062 DigitalOut Green(p30), Yellow(p29);
00063 
00064 msgSerial *serialport=0;
00065 
00066 FT_TRANSFER_AREA *ta = 0;
00067 FT_HANDLE h = 0;
00068 int trigger_interface = 0;
00069 
00070 char *names[] = {"NO_FT_DEVICE","FT_AUTO_TYPE","FT_INTELLIGENT_IF","FT_INTELLIGENT_IF_SLAVE","FT_ROBO_IF_IIM",
00071                  "FT_ROBO_IF_USB","FT_ROBO_IF_COM","FT_ROBO_IF_OVER_RF","FT_ROBO_IO_EXTENSION","FT_ROBO_RF_DATA_LINK"
00072                 };
00073 int types[] = {NO_FT_DEVICE,FT_AUTO_TYPE,FT_INTELLIGENT_IF,FT_INTELLIGENT_IF_SLAVE,FT_ROBO_IF_IIM,
00074                FT_ROBO_IF_USB,FT_ROBO_IF_COM,FT_ROBO_IF_OVER_RF,FT_ROBO_IO_EXTENSION,FT_ROBO_RF_DATA_LINK,NO_FT_DEVICE//this last one is the default
00075               };
00076 char *modes[] = {"cable","ftlib","serial"};
00077 int type = NO_FT_DEVICE;
00078 int mode = 0;
00079 
00080 unsigned short encode(char*);
00081 void callback(void*ctx);
00082 void *context = 0;
00083 void messageCB(SMESSAGE*);
00084 NOTIFICATION_EVENTS ne = {callback, context, 0, 0, 0, messageCB};
00085 unsigned short msgID = encode("set");
00086 
00087 void http_server() {
00088     FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
00089     svr.addHandler<FSHandler>("/files");
00090     svr.bind(80);
00091     printf("Listening for http requests...\n");
00092 }
00093 
00094 unsigned short encode(char* s) { //convert 3 character command to 16bit value
00095     unsigned short r = 0;
00096     for (int i = 0; i < 3 && *s != '\0'; i++) {
00097         r *= 40;
00098         if (s[i] >= '0' && s[i] <= '9') {
00099             r += s[i];
00100             r -= '0' - 27;
00101         } else if (s[i] >= 'A' && s[i] <= 'Z') {
00102             r += s[i];
00103             r -= 'A' - 1;
00104         } else if (s[i] >= 'a' && s[i] <= 'z') {
00105             r += s[i];
00106             r -= 'a' - 1;
00107         } else
00108             switch (s[i]) {
00109                 case '+':
00110                     r += 37;
00111                     break;
00112                 case '-':
00113                     r += 38;
00114                     break;
00115                 case '!':
00116                     r += 39;
00117                     break;
00118             }
00119     }
00120     return r;
00121 }
00122 
00123 void handleMessage(char m[7]) { //called when a valid tcpip message is received
00124     SMESSAGE *msg = (SMESSAGE*)(m+1);
00125     switch (mode) {
00126         case 0://cable
00127             if (msg->ucHwId == HWID && msg->ucSubId == SUBID && msg->uiMsgId == msgID && ta != 0) { //check message values
00128                 ta->M_Main = msg->ucB2; //long cable semantics, copy the message value to the motor outputs (16 bit only!)
00129                 ta->M_Sub1 = msg->ucB3;
00130             }
00131             break;
00132         case 1://ftlib, not (yet) implemented
00133             if (h)
00134                 SendFtMessage(h, msg->ucHwId, msg->ucSubId, msg->dw, 0, MSG_SEND_NORMAL); //use the ftlib function to forward the message to the interface
00135             break;
00136         case 2://serial
00137             if (serialport)
00138                 serialport->write(m);
00139             break;
00140     }
00141 }
00142 
00143 void OnSerMsg(char *m) {
00144     if (server && mode==2)//serial
00145         server->writeStream((SMESSAGE*)(m+1));
00146 }
00147 
00148 void callback(void*ctx) { //new data from interface has arrived
00149     if (mode==0 && ta->ChangeEg) {//cable
00150         SMESSAGE m = { {HWID, SUBID} };
00151         m.uiMsgId = msgID;
00152         m.ucB2 = ta->E_Main;
00153         m.ucB3 = ta->E_Sub1;
00154         if (server)
00155             server->writeStream(&m);
00156 //        printf("%02X\n", ta->E_Main);
00157     }
00158 }
00159 
00160 void messageCB(SMESSAGE *msg) {// not (yet) implemented
00161     if (server && mode==1)//ftlib
00162         server->writeStream(msg);
00163 }
00164 
00165 void init_ft_interface() {
00166     char comport[5]= "COM1";
00167     InitFtLib();
00168     unsigned ver = GetLibVersion();
00169     printf("libver = %08X\n", ver);
00170 
00171     if (!config.read("/local/ft.ini"))
00172         printf("reading of configfile failed!\n");
00173     mode = config.getLookup("mode", modes, 3);
00174     switch (mode) {
00175         case 0://cable (requires ftlib too)
00176         case 1://ftlib
00177             type =  types[config.getLookup("FtInterface", names, sizeof(names)/sizeof(char*))];
00178             if (type==FT_INTELLIGENT_IF || type==FT_INTELLIGENT_IF_SLAVE || type==FT_ROBO_IF_IIM || type==FT_ROBO_IF_COM) {//serial device
00179                 if (!config.getValue("FtCommPort", comport, sizeof(comport)))
00180                     printf("FtCommPort not found, using default\n");
00181                 printf("opening port %s by Ftlib\n", comport);
00182                 h = OpenFtCommDevice(comport, type, 5);
00183                 if (h) {
00184                     printf("OpenFtCommDevice on %s succeeded\n", comport);
00185                     //long v = GetFtFirmware(h);
00186                     //printf("FW: %08lX\n", v);
00187                     ta = GetFtTransferAreaAddress(h);//always get before starting otherwise not imm avail in callback
00188                     int err = StartFtTransferAreaWithCommunication(h, &ne);
00189                     if (err==0) {
00190                         printf("transfer started\n");
00191                         for (int i = 0; i < 8; i++) {
00192                             ta->MPWM_Main[i] = 7;
00193                             ta->MPWM_Sub1[i] = 7;
00194                         }
00195                     } else
00196                         printf("Error %08X: %s\n", err, GetFtLibErrorString(err,1));
00197                 }
00198                 printf("Opening Ft device failed\n");
00199             } else {
00200                 printf("devicetype %d is not supported\n", type);
00201             }
00202             break;
00203         case 2://serial
00204             if (!config.getValue("FtCommPort", comport, sizeof(comport)))
00205                 printf("FtCommPort not found, using default\n");
00206             printf("opening port %s for messaging\n", comport);
00207             switch (comport[3]) {
00208                 case '1':
00209                     serialport = new msgSerial(p9, p10, OnSerMsg);
00210                     break;
00211                 case '2':
00212                     serialport = new msgSerial(p13, p14, OnSerMsg);
00213                     break;
00214                 case '3':
00215                     serialport = new msgSerial(p28, p27, OnSerMsg);
00216                     break;
00217                 default:
00218                     // serialport = &viaUsb;
00219                     printf("USB testoutput not supported for messages\n");
00220                     break;
00221             }
00222             break;
00223         default:
00224             printf("Mode %d is not supported or unknown\n",mode);
00225     }
00226 }
00227 
00228 int main() {
00229     init_ft_interface();
00230 
00231     printf("Setting up ethernet...\n");
00232     EthernetErr ethErr = eth.setup();
00233     if (ethErr) {
00234         printf("Error %d in ethernet setup.\n", ethErr);
00235         return -1;
00236     }
00237     printf("Setup of ethernet OK\n");
00238 
00239     printf("starting http server\n");
00240     http_server();
00241 
00242     printf("creating ft socket server\n");
00243     server = new ftServer(IpAddr(),config.getInt("port",5000));
00244 
00245     printf("starting ft socket server\n");
00246     if (server->startServer()) {
00247         for (;;) {//main loop
00248             Net::poll();
00249 //            server->pollServer();
00250             if (trigger_interface) {
00251                 if (h->guardedFtThreadBegin())
00252                     trigger_interface = 0;
00253             }
00254             Green = !(FIO1PIN&(1<<25));//link
00255             Yellow= !(FIO1PIN&(1<<26));//speed
00256         }
00257     } else
00258         printf("server could not be started\n");
00259 
00260     if (server)
00261         delete server;
00262     if (serialport)
00263         delete serialport;
00264     if (h) {
00265         StopFtTransferArea(h);
00266         CloseFtDevice(h);
00267     }
00268     CloseFtLib();
00269     printf("end of program");
00270     return 0;
00271 }