TeamE7
Dependencies: EthernetInterface HTTPServer RemoteIR SDFileSystem mbed-rpc mbed-rtos mbed
Fork of SmartRemote by
main.cpp@17:66d69f97b861, 2017-01-30 (annotated)
- Committer:
- TeamE7
- Date:
- Mon Jan 30 12:55:27 2017 +0000
- Revision:
- 17:66d69f97b861
- Parent:
- 16:2d23297857bc
TeamE7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:5630d7e58a0b | 1 | #include "mbed.h" |
sammacjunkie | 4:36e0aa194b45 | 2 | #include "EthernetInterface.h" |
donatien | 0:5630d7e58a0b | 3 | #include "HTTPServer.h" |
sammacjunkie | 4:36e0aa194b45 | 4 | #include "FsHandler.h" |
sammacjunkie | 4:36e0aa194b45 | 5 | #include "RpcHandler.h" |
sammacjunkie | 4:36e0aa194b45 | 6 | #include "rtos.h" |
sammacjunkie | 16:2d23297857bc | 7 | #include <string> |
donatien | 0:5630d7e58a0b | 8 | |
TeamE7 | 17:66d69f97b861 | 9 | /* Team E7*/ |
TeamE7 | 17:66d69f97b861 | 10 | |
sammacjunkie | 6:70a0af38edcc | 11 | DigitalOut led1(LED1); |
sammacjunkie | 14:2b63c0f795cb | 12 | DigitalOut led2(LED2); |
sammacjunkie | 6:70a0af38edcc | 13 | |
sammacjunkie | 4:36e0aa194b45 | 14 | //Start IR |
sammacjunkie | 4:36e0aa194b45 | 15 | #include "ReceiverIR.h" |
sammacjunkie | 4:36e0aa194b45 | 16 | #include "TransmitterIR.h" |
sammacjunkie | 4:36e0aa194b45 | 17 | #include "IR.h" |
sammacjunkie | 4:36e0aa194b45 | 18 | //END IR |
sarvagyavaish | 3:a3b4d032f48f | 19 | |
sarvagyavaish | 12:20ba5378ede2 | 20 | |
sarvagyavaish | 3:a3b4d032f48f | 21 | // Start DB |
sarvagyavaish | 3:a3b4d032f48f | 22 | #include <stdio.h> |
sarvagyavaish | 3:a3b4d032f48f | 23 | #include <stdlib.h> |
sarvagyavaish | 3:a3b4d032f48f | 24 | #include "SDFileSystem.h" |
sarvagyavaish | 3:a3b4d032f48f | 25 | #include "db.h" |
sarvagyavaish | 3:a3b4d032f48f | 26 | // End DB |
sarvagyavaish | 3:a3b4d032f48f | 27 | |
sarvagyavaish | 12:20ba5378ede2 | 28 | |
sarvagyavaish | 3:a3b4d032f48f | 29 | // Start RPC |
sarvagyavaish | 3:a3b4d032f48f | 30 | #include "RPCVariable.h" |
sammacjunkie | 4:36e0aa194b45 | 31 | int Request = 0; |
sarvagyavaish | 3:a3b4d032f48f | 32 | int Learn = 0; |
sammacjunkie | 7:7b3f53b5e737 | 33 | char Learn_name0; |
sammacjunkie | 7:7b3f53b5e737 | 34 | char Learn_name1; |
sammacjunkie | 7:7b3f53b5e737 | 35 | char Learn_name2; |
sammacjunkie | 7:7b3f53b5e737 | 36 | char Learn_name3; |
sammacjunkie | 7:7b3f53b5e737 | 37 | char Learn_name4; |
sammacjunkie | 7:7b3f53b5e737 | 38 | char Learn_name5; |
sammacjunkie | 7:7b3f53b5e737 | 39 | char Learn_name6; |
sammacjunkie | 7:7b3f53b5e737 | 40 | char Learn_name7; |
sammacjunkie | 7:7b3f53b5e737 | 41 | char Learn_name8; |
sammacjunkie | 7:7b3f53b5e737 | 42 | char Learn_name9; |
sarvagyavaish | 12:20ba5378ede2 | 43 | |
sarvagyavaish | 3:a3b4d032f48f | 44 | //Make these variables accessible over RPC by attaching them to an RPCVariable |
sammacjunkie | 4:36e0aa194b45 | 45 | RPCVariable<int> RPCRequest(&Request, "Request"); |
sammacjunkie | 4:36e0aa194b45 | 46 | RPCVariable<int> RPCLearn(&Learn, "Learn"); |
sammacjunkie | 7:7b3f53b5e737 | 47 | RPCVariable<char> RPCLearner0(&Learn_name0, "Learn_name0"); |
sammacjunkie | 7:7b3f53b5e737 | 48 | RPCVariable<char> RPCLearner1(&Learn_name1, "Learn_name1"); |
sammacjunkie | 7:7b3f53b5e737 | 49 | RPCVariable<char> RPCLearner2(&Learn_name2, "Learn_name2"); |
sammacjunkie | 7:7b3f53b5e737 | 50 | RPCVariable<char> RPCLearner3(&Learn_name3, "Learn_name3"); |
sammacjunkie | 7:7b3f53b5e737 | 51 | RPCVariable<char> RPCLearner4(&Learn_name4, "Learn_name4"); |
sammacjunkie | 7:7b3f53b5e737 | 52 | RPCVariable<char> RPCLearner5(&Learn_name5, "Learn_name5"); |
sammacjunkie | 7:7b3f53b5e737 | 53 | RPCVariable<char> RPCLearner6(&Learn_name6, "Learn_name6"); |
sammacjunkie | 7:7b3f53b5e737 | 54 | RPCVariable<char> RPCLearner7(&Learn_name7, "Learn_name7"); |
sammacjunkie | 7:7b3f53b5e737 | 55 | RPCVariable<char> RPCLearner8(&Learn_name8, "Learn_name8"); |
sammacjunkie | 7:7b3f53b5e737 | 56 | RPCVariable<char> RPCLearner9(&Learn_name9, "Learn_name9"); |
sarvagyavaish | 12:20ba5378ede2 | 57 | // End RPC |
sammacjunkie | 7:7b3f53b5e737 | 58 | |
sammacjunkie | 7:7b3f53b5e737 | 59 | |
sammacjunkie | 4:36e0aa194b45 | 60 | Serial pc(USBTX, USBRX, "pc"); |
sarvagyavaish | 3:a3b4d032f48f | 61 | |
sammacjunkie | 4:36e0aa194b45 | 62 | // Instantiate a HTTPServer to handle incoming requests |
sammacjunkie | 4:36e0aa194b45 | 63 | HTTPServer svr; |
sammacjunkie | 4:36e0aa194b45 | 64 | // Instantiate a local file system handler named 'local' which will be used later to access files on the mbed. |
sammacjunkie | 4:36e0aa194b45 | 65 | LocalFileSystem local("local"); |
donatien | 0:5630d7e58a0b | 66 | |
sammacjunkie | 4:36e0aa194b45 | 67 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed |
donatien | 0:5630d7e58a0b | 68 | |
sarvagyavaish | 3:a3b4d032f48f | 69 | int main() |
sarvagyavaish | 3:a3b4d032f48f | 70 | { |
sammacjunkie | 4:36e0aa194b45 | 71 | |
sammacjunkie | 4:36e0aa194b45 | 72 | printf("Setting up Apache...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 73 | |
sammacjunkie | 4:36e0aa194b45 | 74 | HTTPFsRequestHandler::mount("/local/", "/"); |
sammacjunkie | 13:12749822ba56 | 75 | HTTPFsRequestHandler::mount("/sd/", "/sd/"); |
sammacjunkie | 4:36e0aa194b45 | 76 | svr.addHandler<HTTPFsRequestHandler>("/"); |
sammacjunkie | 4:36e0aa194b45 | 77 | svr.addHandler<HTTPRpcRequestHandler>("/rpc"); |
sammacjunkie | 4:36e0aa194b45 | 78 | |
sammacjunkie | 4:36e0aa194b45 | 79 | EthernetInterface eth; |
sammacjunkie | 4:36e0aa194b45 | 80 | eth.init(); //Use DHCP |
sammacjunkie | 13:12749822ba56 | 81 | eth.connect(); |
sammacjunkie | 4:36e0aa194b45 | 82 | |
sammacjunkie | 4:36e0aa194b45 | 83 | // Now start the server on port 80. |
sammacjunkie | 4:36e0aa194b45 | 84 | if (!svr.start(80, ð)) { |
sammacjunkie | 4:36e0aa194b45 | 85 | error("Server not starting !"); |
sammacjunkie | 4:36e0aa194b45 | 86 | exit(0); |
sarvagyavaish | 3:a3b4d032f48f | 87 | } |
sarvagyavaish | 3:a3b4d032f48f | 88 | |
sammacjunkie | 4:36e0aa194b45 | 89 | printf("IP: %s\n \r", eth.getIPAddress()); |
sammacjunkie | 4:36e0aa194b45 | 90 | printf("Setup OK\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 91 | |
sammacjunkie | 4:36e0aa194b45 | 92 | |
sarvagyavaish | 3:a3b4d032f48f | 93 | |
sarvagyavaish | 3:a3b4d032f48f | 94 | |
sarvagyavaish | 3:a3b4d032f48f | 95 | // DB Init |
sarvagyavaish | 3:a3b4d032f48f | 96 | mkdir("/sd/SmartRemote", 0777); |
sarvagyavaish | 3:a3b4d032f48f | 97 | |
sarvagyavaish | 12:20ba5378ede2 | 98 | char tuple_code[128]; |
sarvagyavaish | 12:20ba5378ede2 | 99 | char tuple_name[128]; |
sarvagyavaish | 12:20ba5378ede2 | 100 | char tuple_bitlength[128]; |
sammacjunkie | 14:2b63c0f795cb | 101 | char tuple_format[128]; |
sammacjunkie | 13:12749822ba56 | 102 | char temp[20]; |
sarvagyavaish | 15:c8074f5f241a | 103 | int n; |
sarvagyavaish | 3:a3b4d032f48f | 104 | //End DB init |
sarvagyavaish | 3:a3b4d032f48f | 105 | |
sarvagyavaish | 3:a3b4d032f48f | 106 | |
sammacjunkie | 4:36e0aa194b45 | 107 | //IR Init |
sammacjunkie | 4:36e0aa194b45 | 108 | uint8_t buf1[32]; |
sammacjunkie | 4:36e0aa194b45 | 109 | uint8_t buf2[32]; |
sammacjunkie | 4:36e0aa194b45 | 110 | int bitlength1; |
sammacjunkie | 4:36e0aa194b45 | 111 | int bitlength2; |
sammacjunkie | 16:2d23297857bc | 112 | char tempstr[3]; |
sammacjunkie | 4:36e0aa194b45 | 113 | RemoteIR::Format format; |
sammacjunkie | 4:36e0aa194b45 | 114 | memset(buf1, 0x00, sizeof(buf1)); |
sammacjunkie | 4:36e0aa194b45 | 115 | memset(buf2, 0x00, sizeof(buf2)); |
sammacjunkie | 4:36e0aa194b45 | 116 | //END IR Init |
sammacjunkie | 4:36e0aa194b45 | 117 | |
sammacjunkie | 4:36e0aa194b45 | 118 | printf("Listening...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 119 | |
sarvagyavaish | 3:a3b4d032f48f | 120 | |
sarvagyavaish | 3:a3b4d032f48f | 121 | Timer tm; |
sarvagyavaish | 3:a3b4d032f48f | 122 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 123 | //Listen indefinitely |
sarvagyavaish | 3:a3b4d032f48f | 124 | while(true) { |
sammacjunkie | 4:36e0aa194b45 | 125 | svr.poll(); |
sarvagyavaish | 3:a3b4d032f48f | 126 | if(tm.read()>.5) { |
sarvagyavaish | 3:a3b4d032f48f | 127 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 128 | } |
sammacjunkie | 6:70a0af38edcc | 129 | if (Learn) { |
sarvagyavaish | 12:20ba5378ede2 | 130 | // Debug LED |
sammacjunkie | 6:70a0af38edcc | 131 | led1 = 1; |
sammacjunkie | 13:12749822ba56 | 132 | |
sarvagyavaish | 12:20ba5378ede2 | 133 | // Receive the code |
sammacjunkie | 6:70a0af38edcc | 134 | { |
sammacjunkie | 6:70a0af38edcc | 135 | bitlength1 = receive(&format, buf1, sizeof(buf1)); |
sammacjunkie | 6:70a0af38edcc | 136 | if (bitlength1 < 0) { |
sammacjunkie | 6:70a0af38edcc | 137 | continue; |
sammacjunkie | 6:70a0af38edcc | 138 | } |
sammacjunkie | 6:70a0af38edcc | 139 | display_status("RECV", bitlength1); |
sammacjunkie | 6:70a0af38edcc | 140 | display_data(buf1, bitlength1); |
sammacjunkie | 6:70a0af38edcc | 141 | //display_format(format); |
sammacjunkie | 6:70a0af38edcc | 142 | } |
sarvagyavaish | 12:20ba5378ede2 | 143 | // Reset |
sammacjunkie | 6:70a0af38edcc | 144 | led1 = 0; |
sammacjunkie | 6:70a0af38edcc | 145 | Learn = 0; |
sammacjunkie | 13:12749822ba56 | 146 | |
sammacjunkie | 13:12749822ba56 | 147 | |
sammacjunkie | 13:12749822ba56 | 148 | |
sammacjunkie | 13:12749822ba56 | 149 | // Set up the variables |
sammacjunkie | 13:12749822ba56 | 150 | sprintf(tuple_name, "%c%c%c%c%c%c%c%c%c%c", Learn_name0,Learn_name1,Learn_name2,Learn_name3,Learn_name4,Learn_name5,Learn_name6,Learn_name7,Learn_name8,Learn_name9); |
sammacjunkie | 13:12749822ba56 | 151 | //sprintf(tuple_code, "%X", buf1); |
sammacjunkie | 13:12749822ba56 | 152 | sprintf(tuple_bitlength, "%d", bitlength1); |
sammacjunkie | 13:12749822ba56 | 153 | sprintf(tuple_format, "%d", format); |
sammacjunkie | 13:12749822ba56 | 154 | for (int i = 0; i < 10; i++) { |
sammacjunkie | 13:12749822ba56 | 155 | if (tuple_name[i] == '~') tuple_name[i] = ' '; |
sammacjunkie | 13:12749822ba56 | 156 | } |
sammacjunkie | 14:2b63c0f795cb | 157 | |
sammacjunkie | 13:12749822ba56 | 158 | const int n = bitlength1 / 8 + (((bitlength1 % 8) != 0) ? 1 : 0); |
sammacjunkie | 13:12749822ba56 | 159 | strcpy(tuple_code, ""); |
sammacjunkie | 13:12749822ba56 | 160 | for (int i = 0; i < n; i++) { |
sammacjunkie | 13:12749822ba56 | 161 | sprintf(temp, "%02X", buf1[i]); |
sammacjunkie | 14:2b63c0f795cb | 162 | strcat(tuple_code, temp); |
sammacjunkie | 13:12749822ba56 | 163 | } |
sammacjunkie | 13:12749822ba56 | 164 | |
sammacjunkie | 13:12749822ba56 | 165 | // Insert into DB |
sammacjunkie | 13:12749822ba56 | 166 | db_insert_tuple(tuple_name, tuple_code, tuple_bitlength, tuple_format); |
sammacjunkie | 13:12749822ba56 | 167 | |
sammacjunkie | 6:70a0af38edcc | 168 | } |
sammacjunkie | 6:70a0af38edcc | 169 | |
sammacjunkie | 14:2b63c0f795cb | 170 | if ( Request != 0) { |
sammacjunkie | 14:2b63c0f795cb | 171 | led2 = 1; |
sammacjunkie | 14:2b63c0f795cb | 172 | db_find_tuple(Request, tuple_name, tuple_code, tuple_bitlength, tuple_format); |
sarvagyavaish | 15:c8074f5f241a | 173 | |
sarvagyavaish | 15:c8074f5f241a | 174 | n = atoi(tuple_bitlength) / 8 + (((atoi(tuple_bitlength) % 8) != 0) ? 1 : 0); |
sarvagyavaish | 15:c8074f5f241a | 175 | memset(buf1, 0x00, sizeof(buf1)); |
sammacjunkie | 16:2d23297857bc | 176 | int j = 0 ; |
sammacjunkie | 16:2d23297857bc | 177 | for (int i = 0; i < 2*n; i+=2) { |
sammacjunkie | 16:2d23297857bc | 178 | |
sarvagyavaish | 15:c8074f5f241a | 179 | // printf("%02X", buf[i]); |
sarvagyavaish | 15:c8074f5f241a | 180 | // buf1[i] = (uint8_t)(atoi( tuple_code.substr(i, 2) )); |
sammacjunkie | 16:2d23297857bc | 181 | sprintf(tempstr,"%c%c", tuple_code[i],tuple_code[i+1] ); |
sammacjunkie | 16:2d23297857bc | 182 | printf("%s - ", tempstr); |
sammacjunkie | 16:2d23297857bc | 183 | printf("%02X\n", (uint8_t)strtol(tempstr,NULL,16) ); |
sammacjunkie | 16:2d23297857bc | 184 | buf1[j] = (uint8_t)strtol(tempstr,NULL,16); |
sammacjunkie | 16:2d23297857bc | 185 | j++; |
sarvagyavaish | 15:c8074f5f241a | 186 | } |
sammacjunkie | 16:2d23297857bc | 187 | display_data(buf1,atoi(tuple_bitlength)); |
sarvagyavaish | 15:c8074f5f241a | 188 | |
sammacjunkie | 14:2b63c0f795cb | 189 | { |
sammacjunkie | 14:2b63c0f795cb | 190 | RemoteIR::Format f = static_cast<RemoteIR::Format>(atoi(tuple_format)); |
sammacjunkie | 16:2d23297857bc | 191 | bitlength1 = transmit(f, buf1, atoi(tuple_bitlength)); |
sammacjunkie | 14:2b63c0f795cb | 192 | if (bitlength1 < 0) { |
sammacjunkie | 14:2b63c0f795cb | 193 | continue; |
sammacjunkie | 14:2b63c0f795cb | 194 | } |
sammacjunkie | 14:2b63c0f795cb | 195 | display_status("TRAN", bitlength1); |
sammacjunkie | 14:2b63c0f795cb | 196 | //display_data(buf1, bitlength1); |
sammacjunkie | 14:2b63c0f795cb | 197 | //display_format(format); |
sammacjunkie | 14:2b63c0f795cb | 198 | } |
sammacjunkie | 14:2b63c0f795cb | 199 | led2 = 0; |
sammacjunkie | 14:2b63c0f795cb | 200 | Request = 0; |
sammacjunkie | 14:2b63c0f795cb | 201 | |
sammacjunkie | 14:2b63c0f795cb | 202 | } |
sammacjunkie | 14:2b63c0f795cb | 203 | |
donatien | 0:5630d7e58a0b | 204 | } |
sarvagyavaish | 3:a3b4d032f48f | 205 | |
sarvagyavaish | 3:a3b4d032f48f | 206 | return 0; |
donatien | 0:5630d7e58a0b | 207 | } |
sammacjunkie | 4:36e0aa194b45 | 208 |