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