You can turn on the TV everywhere.
Dependencies: RemoteIR WIZnetInterface mbed
main.cpp@0:5cc1166c695e, 2015-08-26 (annotated)
- Committer:
- jehoon
- Date:
- Wed Aug 26 14:04:54 2015 +0000
- Revision:
- 0:5cc1166c695e
You can turn on the TV everywhere.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jehoon | 0:5cc1166c695e | 1 | /** |
jehoon | 0:5cc1166c695e | 2 | * RemoteIR library - Test program. |
jehoon | 0:5cc1166c695e | 3 | * |
jehoon | 0:5cc1166c695e | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
jehoon | 0:5cc1166c695e | 5 | * http://shinta.main.jp/ |
jehoon | 0:5cc1166c695e | 6 | */ |
jehoon | 0:5cc1166c695e | 7 | |
jehoon | 0:5cc1166c695e | 8 | #include <mbed.h> |
jehoon | 0:5cc1166c695e | 9 | |
jehoon | 0:5cc1166c695e | 10 | #include "EthernetInterface.h" |
jehoon | 0:5cc1166c695e | 11 | #include "TransmitterIR.h" |
jehoon | 0:5cc1166c695e | 12 | #include "RemoteIR.h" |
jehoon | 0:5cc1166c695e | 13 | |
jehoon | 0:5cc1166c695e | 14 | TransmitterIR ir_tx(D5); |
jehoon | 0:5cc1166c695e | 15 | Serial pc(USBTX, USBRX); |
jehoon | 0:5cc1166c695e | 16 | |
jehoon | 0:5cc1166c695e | 17 | |
jehoon | 0:5cc1166c695e | 18 | int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 1000) { |
jehoon | 0:5cc1166c695e | 19 | int cnt = 0; |
jehoon | 0:5cc1166c695e | 20 | while (ir_tx.getState() != TransmitterIR::Idle) { |
jehoon | 0:5cc1166c695e | 21 | cnt++; |
jehoon | 0:5cc1166c695e | 22 | if (timeout < cnt) { |
jehoon | 0:5cc1166c695e | 23 | return -1; |
jehoon | 0:5cc1166c695e | 24 | } |
jehoon | 0:5cc1166c695e | 25 | } |
jehoon | 0:5cc1166c695e | 26 | return ir_tx.setData(format, buf, bitlength); |
jehoon | 0:5cc1166c695e | 27 | } |
jehoon | 0:5cc1166c695e | 28 | |
jehoon | 0:5cc1166c695e | 29 | |
jehoon | 0:5cc1166c695e | 30 | void sort_data(char *buf, int n, char *type, char *cmd){ |
jehoon | 0:5cc1166c695e | 31 | |
jehoon | 0:5cc1166c695e | 32 | bool flag = 0; |
jehoon | 0:5cc1166c695e | 33 | int s = 0; |
jehoon | 0:5cc1166c695e | 34 | int i, j; |
jehoon | 0:5cc1166c695e | 35 | int addBinary = 1; |
jehoon | 0:5cc1166c695e | 36 | int pontOfcommand = 0; |
jehoon | 0:5cc1166c695e | 37 | //devide |
jehoon | 0:5cc1166c695e | 38 | for(i=0;i<n;i++){ |
jehoon | 0:5cc1166c695e | 39 | if(buf[i]==','){ |
jehoon | 0:5cc1166c695e | 40 | flag = 1; |
jehoon | 0:5cc1166c695e | 41 | s = i; |
jehoon | 0:5cc1166c695e | 42 | continue; |
jehoon | 0:5cc1166c695e | 43 | } |
jehoon | 0:5cc1166c695e | 44 | if(flag){ |
jehoon | 0:5cc1166c695e | 45 | j = i-(s+1); |
jehoon | 0:5cc1166c695e | 46 | |
jehoon | 0:5cc1166c695e | 47 | if(j%8 == 0 && j != 0) {pontOfcommand++; addBinary = 1;} |
jehoon | 0:5cc1166c695e | 48 | |
jehoon | 0:5cc1166c695e | 49 | if(buf[i] == '1') cmd[pontOfcommand] += addBinary; |
jehoon | 0:5cc1166c695e | 50 | |
jehoon | 0:5cc1166c695e | 51 | addBinary*=2; |
jehoon | 0:5cc1166c695e | 52 | } |
jehoon | 0:5cc1166c695e | 53 | else |
jehoon | 0:5cc1166c695e | 54 | type[i] = buf[i]; |
jehoon | 0:5cc1166c695e | 55 | } |
jehoon | 0:5cc1166c695e | 56 | } |
jehoon | 0:5cc1166c695e | 57 | |
jehoon | 0:5cc1166c695e | 58 | /** |
jehoon | 0:5cc1166c695e | 59 | * Entry point. |
jehoon | 0:5cc1166c695e | 60 | */ |
jehoon | 0:5cc1166c695e | 61 | int main(void) { |
jehoon | 0:5cc1166c695e | 62 | pc.baud(115200); |
jehoon | 0:5cc1166c695e | 63 | RemoteIR::Format format; |
jehoon | 0:5cc1166c695e | 64 | |
jehoon | 0:5cc1166c695e | 65 | pc.printf("Wait a second...\r\n"); |
jehoon | 0:5cc1166c695e | 66 | |
jehoon | 0:5cc1166c695e | 67 | int phy_link; |
jehoon | 0:5cc1166c695e | 68 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; |
jehoon | 0:5cc1166c695e | 69 | EthernetInterface eth; |
jehoon | 0:5cc1166c695e | 70 | eth.init(mac_addr); //Use DHCP |
jehoon | 0:5cc1166c695e | 71 | eth.connect(); |
jehoon | 0:5cc1166c695e | 72 | do{ |
jehoon | 0:5cc1166c695e | 73 | phy_link = eth.ethernet_link(); |
jehoon | 0:5cc1166c695e | 74 | pc.printf("..."); |
jehoon | 0:5cc1166c695e | 75 | wait(2); |
jehoon | 0:5cc1166c695e | 76 | }while(!phy_link); |
jehoon | 0:5cc1166c695e | 77 | printf("\r\n"); |
jehoon | 0:5cc1166c695e | 78 | pc.printf("Server IP Address is %s\r\n", eth.getIPAddress()); |
jehoon | 0:5cc1166c695e | 79 | |
jehoon | 0:5cc1166c695e | 80 | TCPSocketServer server; |
jehoon | 0:5cc1166c695e | 81 | server.bind(5000); |
jehoon | 0:5cc1166c695e | 82 | server.listen(); |
jehoon | 0:5cc1166c695e | 83 | |
jehoon | 0:5cc1166c695e | 84 | |
jehoon | 0:5cc1166c695e | 85 | while (1) { |
jehoon | 0:5cc1166c695e | 86 | |
jehoon | 0:5cc1166c695e | 87 | //char buf[50]="NEC,00100000110111110001000011101111\n"; |
jehoon | 0:5cc1166c695e | 88 | |
jehoon | 0:5cc1166c695e | 89 | printf("Wait for new connection...\r\n"); |
jehoon | 0:5cc1166c695e | 90 | TCPSocketConnection client; |
jehoon | 0:5cc1166c695e | 91 | server.accept(client); |
jehoon | 0:5cc1166c695e | 92 | client.set_blocking(false, 15000); // Timeout after (1.5)s |
jehoon | 0:5cc1166c695e | 93 | printf("Connection from: %s\r\n", client.get_address()); |
jehoon | 0:5cc1166c695e | 94 | |
jehoon | 0:5cc1166c695e | 95 | while (true) { |
jehoon | 0:5cc1166c695e | 96 | char buf[50] = {0}; |
jehoon | 0:5cc1166c695e | 97 | char type[10] = {0}; |
jehoon | 0:5cc1166c695e | 98 | char cmd[4] = {0}; |
jehoon | 0:5cc1166c695e | 99 | int bitlength=0; |
jehoon | 0:5cc1166c695e | 100 | int n = client.receive(buf, sizeof(buf)); |
jehoon | 0:5cc1166c695e | 101 | if(n <= 0) continue; |
jehoon | 0:5cc1166c695e | 102 | |
jehoon | 0:5cc1166c695e | 103 | // print received message to terminal |
jehoon | 0:5cc1166c695e | 104 | //buf[n] = '\0'; |
jehoon | 0:5cc1166c695e | 105 | pc.printf("Received message from Client :\n %s\n",buf); |
jehoon | 0:5cc1166c695e | 106 | |
jehoon | 0:5cc1166c695e | 107 | sort_data(buf, n, type, cmd); |
jehoon | 0:5cc1166c695e | 108 | |
jehoon | 0:5cc1166c695e | 109 | if(!strncmp(type,"NEC",strlen("NEC"))) |
jehoon | 0:5cc1166c695e | 110 | format = RemoteIR::NEC; |
jehoon | 0:5cc1166c695e | 111 | else if(!strncmp(type,"AEHA",strlen("AEHA"))) |
jehoon | 0:5cc1166c695e | 112 | format = RemoteIR::AEHA; |
jehoon | 0:5cc1166c695e | 113 | else if(!strncmp(type,"SONY",strlen("SONY"))) |
jehoon | 0:5cc1166c695e | 114 | format = RemoteIR::SONY; |
jehoon | 0:5cc1166c695e | 115 | else if(!strncmp(type,"NEC_REPEAT",strlen("NEC_REPEAT"))) |
jehoon | 0:5cc1166c695e | 116 | format = RemoteIR::NEC_REPEAT; |
jehoon | 0:5cc1166c695e | 117 | else if(!strncmp(type,"AEHA_REPEAT",strlen("AEHA_REPEAT"))) |
jehoon | 0:5cc1166c695e | 118 | format = RemoteIR::AEHA_REPEAT; |
jehoon | 0:5cc1166c695e | 119 | else |
jehoon | 0:5cc1166c695e | 120 | format = RemoteIR::UNKNOWN; |
jehoon | 0:5cc1166c695e | 121 | |
jehoon | 0:5cc1166c695e | 122 | pc.printf("SEND HEX: %2x %2x %2x %2x\n",cmd[0],cmd[1],cmd[2],cmd[3]); |
jehoon | 0:5cc1166c695e | 123 | |
jehoon | 0:5cc1166c695e | 124 | { |
jehoon | 0:5cc1166c695e | 125 | bitlength = transmit(format, (uint8_t*)cmd, 32); |
jehoon | 0:5cc1166c695e | 126 | if (bitlength < 0) { |
jehoon | 0:5cc1166c695e | 127 | continue; |
jehoon | 0:5cc1166c695e | 128 | } |
jehoon | 0:5cc1166c695e | 129 | } |
jehoon | 0:5cc1166c695e | 130 | client.close(); |
jehoon | 0:5cc1166c695e | 131 | } |
jehoon | 0:5cc1166c695e | 132 | |
jehoon | 0:5cc1166c695e | 133 | } |
jehoon | 0:5cc1166c695e | 134 | } |
jehoon | 0:5cc1166c695e | 135 |