This application translates HTTP GET requests into the proper RS232 commands to control a Sharp Aquos TV
Dependencies: EthernetInterface mbed-rtos mbed
Revision 2:3637af74f7f0, committed 2014-09-06
- Comitter:
- davisw00
- Date:
- Sat Sep 06 23:47:21 2014 +0000
- Parent:
- 1:64bfc05e387d
- Commit message:
- Implemented Channel commands. Wait for response is not working; therefore commands are followed by a 50ms wait
Changed in this revision
--- a/AquosHTTP.cpp Tue Jul 29 23:10:14 2014 +0000 +++ b/AquosHTTP.cpp Sat Sep 06 23:47:21 2014 +0000 @@ -1,8 +1,9 @@ #include "AquosHTTP.h" -AquosHTTP::AquosHTTP(DebugPort *dbg) { +AquosHTTP::AquosHTTP(DebugPort *dbg, AquosTV* tv) { m_dbg = dbg; + m_tv = tv; init(); } @@ -30,15 +31,20 @@ m_dbg->send("\n\r"); } - wait(0.1); - m_eth.connect(1000); //usually fails, so no throw - wait(0.1); - //m_server.set_blocking(true); - ret = m_server.bind(80); - if(ret<0) m_dbg->send("Server failed to bind",1); - ret = m_server.listen(1); - if(ret<0) m_dbg->send("Server failed to listen",1); - + ret = -1; + while(ret<0) { + wait(0.1); + m_eth.connect(1000); //usually fails, so no throw + wait(0.1); + //m_server.set_blocking(true); + ret = m_server.bind(80); + if(ret<0) { + m_dbg->send("Server failed to bind"); + } else { + ret = m_server.listen(1); + if(ret<0) m_dbg->send("Server failed to listen"); + } + } return *this; } @@ -78,6 +84,8 @@ return *this; } + + AquosHTTP& AquosHTTP::returnFailure() { int msglen = setResponse("FAILED!"); char* buf = new char[msglen + 256]; @@ -86,4 +94,14 @@ m_client.close(); delete [] buf; return *this; +} + +AquosHTTP& AquosHTTP::returnHelp() { + int helplen = strlen(gblHelp); + char* buf = new char[helplen+256]; + snprintf(buf,helplen+255,"HTTP/1.1 200 OK\r\nContent-Length: %i\r\nConnection: close\r\nContent-type: text/html\r\n\r\n%s",helplen,gblHelp); + m_client.send(buf,strlen(buf)); + m_client.close(); + delete [] buf; + return *this; } \ No newline at end of file
--- a/AquosHTTP.h Tue Jul 29 23:10:14 2014 +0000 +++ b/AquosHTTP.h Sat Sep 06 23:47:21 2014 +0000 @@ -5,11 +5,13 @@ #include "rtos.h" #include <string.h> #include "DebugPort.h" +#include "AquosTV.h" class AquosHTTP { private: const static unsigned int BUF_SIZE = 256; DebugPort* m_dbg; + AquosTV* m_tv; protected: EthernetInterface m_eth; @@ -25,7 +27,7 @@ int setResponse(const char* msg); public: - AquosHTTP(DebugPort* dbg); + AquosHTTP(DebugPort* dbg, AquosTV* tv); ~AquosHTTP(); AquosHTTP& init(const char *ipaddr, const char* netmask, const char* gateway); @@ -35,6 +37,8 @@ AquosHTTP& returnSuccess(); AquosHTTP& returnFailure(); + + AquosHTTP& returnHelp(); };
--- a/AquosTV.cpp Tue Jul 29 23:10:14 2014 +0000 +++ b/AquosTV.cpp Sat Sep 06 23:47:21 2014 +0000 @@ -13,21 +13,57 @@ m_tv = new Serial(PTC17,PTC16); m_tv->baud(9600); m_tv->format(8,SerialBase::None,1); + //m_tv->set_flow_control(SerialBase::Disabled); memset(httpcmd,0,sizeof(char)*BUF_SIZE); memset(rs232cmd,0,sizeof(char)*BUF_SIZE); m_tv->printf("RSPW1 \r"); //--- enable rs232 commands for power on/off } + +bool AquosTV::sendcmd(const char* cmd) { + bool ret=false; + char status[5]; + int i = 0; + m_dbg->send("\n\rSending TV Command: "); + m_dbg->send(cmd); + m_dbg->send("\n"); + m_tv->printf(cmd); + wait(0.05); + + ret=true; + /* + for(i=0;i<5;++i) status[i]='\0'; + for(i=0; !ret && i<5; ++i) { + status[i] = m_tv->getc(); + ret = (status[i]=='\r'); + } + ret = (strncmp(status,"OK",2)==0); + */ + if(ret) m_dbg->send("Command Received 'OK' Status\n\r"); + else m_dbg->send("Command did NOT receive 'OK' Status -- Error condition\n\r"); + return ret; +} + +int AquosTV::getNumber(char** input, const char eol) { + int ret=0; + for(;**input!=eol;++(*input)) { + ret*=10; + ret+=**input-'0'; + } + ++(*input); + return ret; +} bool AquosTV::processCommand(const char* httpin) { int i; int j; + char* p; bool ret=false; char request[BUF_SIZE]; strncpy(httpcmd,httpin,BUF_SIZE-1); httpcmd[AQUOSBUF-1] = '\0'; rs232cmd[0]='\0'; - if(httpcmd[0] == 'G' && httpcmd[1] == 'E' && httpcmd[2]=='T' && httpcmd[3]==' ' && httpcmd[4]=='/') { + if(strncmp(httpcmd,"GET /",5)==0) { for(i=5;httpcmd[i]!=' '&&httpcmd[i]!='\0'&&i<AQUOSBUF-1;++i) { request[i-5]=(httpcmd[i] | 0x20); //make lower case } @@ -38,44 +74,97 @@ if(strncmp(request,"poweron",BUF_SIZE-1)==0) { strncpy(rs232cmd,"POWR1 \r",BUF_SIZE-1); - ret = true; + ret = this->sendcmd(rs232cmd); } else if(strncmp(request,"poweroff",BUF_SIZE-1)==0) { strncpy(rs232cmd,"POWR0 \r",BUF_SIZE-1); - ret = true; + ret = this->sendcmd(rs232cmd); } else if(strncmp(request,"selecttv",BUF_SIZE-1)==0) { strncpy(rs232cmd,"ITVD0 \r",BUF_SIZE-1); - ret = true; + ret = this->sendcmd(rs232cmd); } else if(strncmp(request,"input",5)==0) { - i=request[5]-'0'; + p=&(request[5]); + i=getNumber(&p); if(i>0 && i<10) { - snprintf(rs232cmd,BUF_SIZE-1,"IAVD%i \r",i); - ret = true; + snprintf(rs232cmd,BUF_SIZE-1,"IAVD%i \r",i); + ret = this->sendcmd(rs232cmd); } } else if(strncmp(request,"volume",6)==0) { - i=request[6]-'0'; - j=request[7]-'0'; - if(i>=0&&i<10) { - if(j>=0&&j<10) { - i = 10*i + j; - } + p=&(request[6]); + i=getNumber(&p); + if(i>=0 && i<100) { snprintf(rs232cmd,BUF_SIZE-1,"VOLM%02i \r",i); - ret = true; + ret = this->sendcmd(rs232cmd); + } + } + else if(strncmp(request,"chanalog",8)==0) { + p=&(request[8]); + i=getNumber(&p); + if(i>0 && i<136) { + snprintf(rs232cmd,BUF_SIZE-1,"DCCH%03i \r",i); + ret = this->sendcmd(rs232cmd); + } + } + + else if(strncmp(request,"chdigiair",9)==0) { + p=&(request[9]); + i=getNumber(&p); + if(i>0 && i<10000) { + snprintf(rs232cmd,BUF_SIZE-1,"DA2P%04i\r",i); + ret = this->sendcmd(rs232cmd); } } + else if(strncmp(request,"chdigicable",11)==0) { + p=&(request[11]); + i=getNumber(&p,'.'); + if(i>0 && i<1000) { + j=getNumber(&p); + if(j>=0 && j<1000) { + snprintf(rs232cmd,BUF_SIZE-1,"DC2U%03i \r",i); + ret = this->sendcmd(rs232cmd); + wait(0.1); + snprintf(rs232cmd,BUF_SIZE-1,"DC2L%03i \r",j); + ret = this->sendcmd(rs232cmd); + } + } + } + else if(strncmp(request,"chdigi2cable",12)==0) { + p=&(request[12]); + i=getNumber(&p); + if(i>0 && i<=63839999) { + j=i%10000; + i-=j; + i/=10000; + snprintf(rs232cmd,BUF_SIZE-1,"DC10%04i\r",j); + ret = this + ->sendcmd(rs232cmd); + wait(0.1); + snprintf(rs232cmd,BUF_SIZE-1,"DC11%04i\r",i); + ret = this->sendcmd(rs232cmd); + } + } + else if(strncmp(request,"chup",4)==0) { + snprintf(rs232cmd,BUF_SIZE-1,"CHUP1 \r"); + ret = this->sendcmd(rs232cmd); + } + else if(strncmp(request,"chdw",4)==0) { + snprintf(rs232cmd,BUF_SIZE-1,"CHDW1 \r"); + ret = this->sendcmd(rs232cmd); + } + + else { + m_dbg->send("Unknown Command Request\n\r"); + ret = false; + } + } - if(ret) { - m_dbg->send("\n\rCommand Interpreted: "); - m_dbg->send(rs232cmd); - m_dbg->send("\n"); - m_tv->printf(rs232cmd); - } else { - m_dbg->send("Unknown Command Request\n\r"); + m_dbg->send("Improper Command Request\n\r"); + ret = false; } return ret; }
--- a/AquosTV.h Tue Jul 29 23:10:14 2014 +0000 +++ b/AquosTV.h Sat Sep 06 23:47:21 2014 +0000 @@ -6,6 +6,19 @@ #include "DebugPort.h" +const char gblHelp[] = " \n\ +poweron Power on SharpAquos \n\ +poweroff Power off SharpAquos \n\ +volume## Set volume to two digit number \n\ +input# Set input to one digit number \n\ +selecttv Set input to tv \n\ +chanalog Set TV channel to analog 1-136 \n\ +chdigiair#### Set TV to digital air 1-9999 \n\ +chdigicable####.#### Set TV to digital cable with major.minor \n\ +chdigi2cable##### Set TV to digital cable with 0-63839999 \n\ +chup TV Channel Up \n\ +chdw TV Channel Down \n\ +"; #define AQUOSBUF 128 @@ -22,6 +35,9 @@ void init(); + int getNumber(char** input, const char eol='\0'); + bool sendcmd(const char* cmd); + public: AquosTV(DebugPort *dbg); virtual ~AquosTV();
--- a/DebugPort.h Tue Jul 29 23:10:14 2014 +0000 +++ b/DebugPort.h Sat Sep 06 23:47:21 2014 +0000 @@ -10,6 +10,8 @@ DigitalOut* led_green; DigitalOut* led_blue; + int m_level; + public: void setLED(bool r, bool g, bool b) { *led_red = !r; @@ -18,6 +20,8 @@ } DebugPort() { + m_level = 0; + m_pc = new Serial(USBTX,USBRX); led_red = new DigitalOut(LED_RED); led_green = new DigitalOut(LED_GREEN); @@ -36,8 +40,9 @@ } void send(const char* msg, int level=0) { m_pc->printf(msg); - if(level>0) { // major fail. stop processing and toggle led - while(true) { + m_level=level; + if(m_level>0) { // major fail. stop processing and toggle led + for(int i =0; i<10; ++i) { setLED(0,0,0); wait(0.1); setLED(1,0,0); @@ -45,8 +50,13 @@ } } } + + int level() { + int ret=m_level; + m_level=0; + return ret; + } }; - #endif \ No newline at end of file
--- a/EthernetInterface.lib Tue Jul 29 23:10:14 2014 +0000 +++ b/EthernetInterface.lib Sat Sep 06 23:47:21 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/EthernetInterface/#e6b79f0ccd95 +http://mbed.org/users/mbed_official/code/EthernetInterface/#5887ae6c0c2c
--- a/main.cpp Tue Jul 29 23:10:14 2014 +0000 +++ b/main.cpp Sat Sep 06 23:47:21 2014 +0000 @@ -15,24 +15,36 @@ int main() { bool loop=true; DebugPort dbg; - dbg.setLED(0,1,1); //yellow for init - wait(0.1); + while(true) { + loop=true; + dbg.setLED(1,1,0); //yellow for init + wait(0.1); - AquosHTTP server(&dbg); - server.init(ip,mask,gateway); - AquosTV tv(&dbg); - + AquosTV tv(&dbg); + AquosHTTP server(&dbg,&tv); + server.init(ip,mask,gateway); - while(loop) { - dbg.setLED(0,0,0); //off while waiting - server.waitForRequest(); - dbg.setLED(0,0,1); //blue in progress - if( tv.processCommand( server.getRequest() )) { - dbg.setLED(0,1,0); - server.returnSuccess(); - } else { - dbg.setLED(1,0,0); - server.returnFailure(); + while(loop) { + dbg.setLED(0,0,0); //off while waiting + server.waitForRequest(); + dbg.setLED(0,0,1); //blue in progress + if( strncmp(server.getRequest(),"GET /help",9)==0) { + dbg.setLED(0,1,0); + server.returnHelp(); + } + else { + if( tv.processCommand( server.getRequest() )) { + dbg.setLED(0,1,0); + dbg.send("Command Success!\n\r"); + server.returnSuccess(); + } + else { + dbg.setLED(1,0,0); + dbg.send("Command Fail!\n\r"); + server.returnFailure(); + } + } + loop=(dbg.level()==0); } } }
--- a/mbed-rtos.lib Tue Jul 29 23:10:14 2014 +0000 +++ b/mbed-rtos.lib Sat Sep 06 23:47:21 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#ff95651f53c7 +http://mbed.org/users/mbed_official/code/mbed-rtos/#bd07334df5b1
--- a/mbed.bld Tue Jul 29 23:10:14 2014 +0000 +++ b/mbed.bld Sat Sep 06 23:47:21 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file