You can turn on the TV everywhere.

Dependencies:   RemoteIR WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * RemoteIR library - Test program.
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007  
00008 #include <mbed.h>
00009 
00010 #include "EthernetInterface.h"
00011 #include "TransmitterIR.h"
00012 #include "RemoteIR.h"
00013 
00014 TransmitterIR ir_tx(D5);
00015 Serial pc(USBTX, USBRX);
00016 
00017 
00018 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 1000) {
00019     int cnt = 0;
00020     while (ir_tx.getState() != TransmitterIR::Idle) {
00021         cnt++;
00022         if (timeout < cnt) {
00023             return -1;
00024         }
00025     }
00026     return ir_tx.setData(format, buf, bitlength);
00027 }
00028 
00029 
00030 void sort_data(char *buf, int n, char *type, char *cmd){
00031    
00032     bool flag = 0;
00033     int s = 0;
00034     int i, j;
00035     int addBinary = 1;
00036     int pontOfcommand = 0;
00037     //devide
00038     for(i=0;i<n;i++){
00039         if(buf[i]==','){
00040             flag = 1;
00041             s = i;
00042             continue;
00043         }    
00044         if(flag){
00045             j = i-(s+1);
00046             
00047             if(j%8 == 0 && j != 0) {pontOfcommand++; addBinary = 1;} 
00048             
00049             if(buf[i] == '1') cmd[pontOfcommand] += addBinary;
00050 
00051             addBinary*=2;
00052         }
00053         else
00054             type[i] = buf[i];
00055     }       
00056 }
00057 
00058 /**
00059  * Entry point.
00060  */
00061 int main(void) {
00062     pc.baud(115200);
00063     RemoteIR::Format format;
00064     
00065     pc.printf("Wait a second...\r\n");
00066 
00067     int phy_link;
00068     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
00069     EthernetInterface eth;
00070     eth.init(mac_addr); //Use DHCP
00071     eth.connect();
00072     do{
00073         phy_link = eth.ethernet_link();
00074          pc.printf("...");
00075          wait(2);
00076      }while(!phy_link);
00077      printf("\r\n");
00078      pc.printf("Server IP Address is %s\r\n", eth.getIPAddress());
00079     
00080      TCPSocketServer server;
00081      server.bind(5000);
00082      server.listen();
00083     
00084     
00085     while (1) {
00086 
00087         //char buf[50]="NEC,00100000110111110001000011101111\n";
00088    
00089         printf("Wait for new connection...\r\n");
00090         TCPSocketConnection client;
00091         server.accept(client);
00092         client.set_blocking(false, 15000); // Timeout after (1.5)s
00093         printf("Connection from: %s\r\n", client.get_address());
00094              
00095             while (true) {
00096                 char buf[50] = {0};
00097                 char type[10] = {0};
00098                 char cmd[4] = {0};
00099                 int bitlength=0;
00100                 int n = client.receive(buf, sizeof(buf));
00101                 if(n <= 0) continue;
00102                 
00103                 // print received message to terminal
00104                 //buf[n] = '\0';
00105                 pc.printf("Received message from Client :\n %s\n",buf);
00106                 
00107                 sort_data(buf, n, type, cmd);
00108                
00109                 if(!strncmp(type,"NEC",strlen("NEC")))
00110                     format = RemoteIR::NEC;
00111                 else if(!strncmp(type,"AEHA",strlen("AEHA")))
00112                     format = RemoteIR::AEHA;
00113                 else if(!strncmp(type,"SONY",strlen("SONY")))
00114                     format = RemoteIR::SONY;
00115                 else if(!strncmp(type,"NEC_REPEAT",strlen("NEC_REPEAT")))
00116                     format = RemoteIR::NEC_REPEAT;
00117                 else if(!strncmp(type,"AEHA_REPEAT",strlen("AEHA_REPEAT")))
00118                     format = RemoteIR::AEHA_REPEAT;
00119                 else
00120                     format = RemoteIR::UNKNOWN;
00121 
00122                 pc.printf("SEND HEX: %2x %2x %2x %2x\n",cmd[0],cmd[1],cmd[2],cmd[3]);
00123 
00124                     {
00125                         bitlength = transmit(format, (uint8_t*)cmd, 32);
00126                         if (bitlength < 0) {
00127                              continue;
00128                         }
00129                     }
00130                     client.close();
00131         }
00132         
00133    }
00134 }
00135