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