Groep E3 - EMDEV
Dependencies: EthernetInterface HTTPServer RemoteIR SDFileSystem mbed-rpc mbed-rtos mbed
Fork of SmartRemote by
main.cpp@8:858f7605c769, 2013-12-03 (annotated)
- Committer:
- sammacjunkie
- Date:
- Tue Dec 03 14:41:51 2013 +0000
- Revision:
- 8:858f7605c769
- Parent:
- 7:7b3f53b5e737
- Child:
- 9:47c413188c0d
update
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 | 3:a3b4d032f48f | 16 | // Start DB |
sarvagyavaish | 3:a3b4d032f48f | 17 | #include <stdio.h> |
sarvagyavaish | 3:a3b4d032f48f | 18 | #include <stdlib.h> |
sarvagyavaish | 3:a3b4d032f48f | 19 | #include "SDFileSystem.h" |
sarvagyavaish | 3:a3b4d032f48f | 20 | #include "db.h" |
sarvagyavaish | 3:a3b4d032f48f | 21 | // End DB |
sarvagyavaish | 3:a3b4d032f48f | 22 | |
sarvagyavaish | 3:a3b4d032f48f | 23 | // Start RPC |
sarvagyavaish | 3:a3b4d032f48f | 24 | #include "RPCVariable.h" |
sammacjunkie | 4:36e0aa194b45 | 25 | int Request = 0; |
sarvagyavaish | 3:a3b4d032f48f | 26 | int Learn = 0; |
sammacjunkie | 7:7b3f53b5e737 | 27 | char Learn_name0; |
sammacjunkie | 7:7b3f53b5e737 | 28 | char Learn_name1; |
sammacjunkie | 7:7b3f53b5e737 | 29 | char Learn_name2; |
sammacjunkie | 7:7b3f53b5e737 | 30 | char Learn_name3; |
sammacjunkie | 7:7b3f53b5e737 | 31 | char Learn_name4; |
sammacjunkie | 7:7b3f53b5e737 | 32 | char Learn_name5; |
sammacjunkie | 7:7b3f53b5e737 | 33 | char Learn_name6; |
sammacjunkie | 7:7b3f53b5e737 | 34 | char Learn_name7; |
sammacjunkie | 7:7b3f53b5e737 | 35 | char Learn_name8; |
sammacjunkie | 7:7b3f53b5e737 | 36 | char Learn_name9; |
sarvagyavaish | 3:a3b4d032f48f | 37 | //Make these variables accessible over RPC by attaching them to an RPCVariable |
sammacjunkie | 4:36e0aa194b45 | 38 | RPCVariable<int> RPCRequest(&Request, "Request"); |
sammacjunkie | 4:36e0aa194b45 | 39 | RPCVariable<int> RPCLearn(&Learn, "Learn"); |
sammacjunkie | 7:7b3f53b5e737 | 40 | RPCVariable<char> RPCLearner0(&Learn_name0, "Learn_name0"); |
sammacjunkie | 7:7b3f53b5e737 | 41 | RPCVariable<char> RPCLearner1(&Learn_name1, "Learn_name1"); |
sammacjunkie | 7:7b3f53b5e737 | 42 | RPCVariable<char> RPCLearner2(&Learn_name2, "Learn_name2"); |
sammacjunkie | 7:7b3f53b5e737 | 43 | RPCVariable<char> RPCLearner3(&Learn_name3, "Learn_name3"); |
sammacjunkie | 7:7b3f53b5e737 | 44 | RPCVariable<char> RPCLearner4(&Learn_name4, "Learn_name4"); |
sammacjunkie | 7:7b3f53b5e737 | 45 | RPCVariable<char> RPCLearner5(&Learn_name5, "Learn_name5"); |
sammacjunkie | 7:7b3f53b5e737 | 46 | RPCVariable<char> RPCLearner6(&Learn_name6, "Learn_name6"); |
sammacjunkie | 7:7b3f53b5e737 | 47 | RPCVariable<char> RPCLearner7(&Learn_name7, "Learn_name7"); |
sammacjunkie | 7:7b3f53b5e737 | 48 | RPCVariable<char> RPCLearner8(&Learn_name8, "Learn_name8"); |
sammacjunkie | 7:7b3f53b5e737 | 49 | RPCVariable<char> RPCLearner9(&Learn_name9, "Learn_name9"); |
sammacjunkie | 7:7b3f53b5e737 | 50 | |
sammacjunkie | 7:7b3f53b5e737 | 51 | |
sammacjunkie | 7:7b3f53b5e737 | 52 | |
sarvagyavaish | 3:a3b4d032f48f | 53 | // End RPC |
sarvagyavaish | 3:a3b4d032f48f | 54 | |
sammacjunkie | 4:36e0aa194b45 | 55 | Serial pc(USBTX, USBRX, "pc"); |
sarvagyavaish | 3:a3b4d032f48f | 56 | |
sammacjunkie | 4:36e0aa194b45 | 57 | // Instantiate a HTTPServer to handle incoming requests |
sammacjunkie | 4:36e0aa194b45 | 58 | HTTPServer svr; |
sammacjunkie | 4:36e0aa194b45 | 59 | // Instantiate a local file system handler named 'local' which will be used later to access files on the mbed. |
sammacjunkie | 4:36e0aa194b45 | 60 | LocalFileSystem local("local"); |
donatien | 0:5630d7e58a0b | 61 | |
sammacjunkie | 4:36e0aa194b45 | 62 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed |
donatien | 0:5630d7e58a0b | 63 | |
sarvagyavaish | 3:a3b4d032f48f | 64 | int main() |
sarvagyavaish | 3:a3b4d032f48f | 65 | { |
sammacjunkie | 4:36e0aa194b45 | 66 | |
sammacjunkie | 4:36e0aa194b45 | 67 | printf("Setting up Apache...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 68 | |
sammacjunkie | 4:36e0aa194b45 | 69 | HTTPFsRequestHandler::mount("/local/", "/"); |
sammacjunkie | 4:36e0aa194b45 | 70 | svr.addHandler<HTTPFsRequestHandler>("/"); |
sammacjunkie | 4:36e0aa194b45 | 71 | svr.addHandler<HTTPRpcRequestHandler>("/rpc"); |
sammacjunkie | 4:36e0aa194b45 | 72 | |
sammacjunkie | 4:36e0aa194b45 | 73 | EthernetInterface eth; |
sammacjunkie | 8:858f7605c769 | 74 | eth.setup(300000); |
sammacjunkie | 4:36e0aa194b45 | 75 | eth.init(); //Use DHCP |
sammacjunkie | 4:36e0aa194b45 | 76 | eth.connect(); |
sammacjunkie | 4:36e0aa194b45 | 77 | |
sammacjunkie | 4:36e0aa194b45 | 78 | // Now start the server on port 80. |
sammacjunkie | 4:36e0aa194b45 | 79 | if (!svr.start(80, ð)) { |
sammacjunkie | 4:36e0aa194b45 | 80 | error("Server not starting !"); |
sammacjunkie | 4:36e0aa194b45 | 81 | exit(0); |
sarvagyavaish | 3:a3b4d032f48f | 82 | } |
sarvagyavaish | 3:a3b4d032f48f | 83 | |
sammacjunkie | 4:36e0aa194b45 | 84 | printf("IP: %s\n \r", eth.getIPAddress()); |
sammacjunkie | 4:36e0aa194b45 | 85 | printf("Setup OK\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 86 | |
sammacjunkie | 4:36e0aa194b45 | 87 | |
sarvagyavaish | 3:a3b4d032f48f | 88 | |
sarvagyavaish | 3:a3b4d032f48f | 89 | |
sarvagyavaish | 3:a3b4d032f48f | 90 | // DB Init |
sarvagyavaish | 3:a3b4d032f48f | 91 | mkdir("/sd/SmartRemote", 0777); |
sarvagyavaish | 3:a3b4d032f48f | 92 | |
sarvagyavaish | 3:a3b4d032f48f | 93 | char code[] = "123AB"; |
sarvagyavaish | 3:a3b4d032f48f | 94 | char name[] = "Button Name"; |
sammacjunkie | 4:36e0aa194b45 | 95 | |
sarvagyavaish | 3:a3b4d032f48f | 96 | char results_code[128]; |
sarvagyavaish | 3:a3b4d032f48f | 97 | char results_name[128]; |
sammacjunkie | 4:36e0aa194b45 | 98 | /* |
sarvagyavaish | 3:a3b4d032f48f | 99 | db_insert_tuple(code, name); |
sarvagyavaish | 3:a3b4d032f48f | 100 | FILE *fread = fopen("/sd/SmartRemote/db.txt", "r"); |
sammacjunkie | 4:36e0aa194b45 | 101 | db_find_tuple(fread, Request, results_name, results_code); |
sarvagyavaish | 3:a3b4d032f48f | 102 | fclose(fread); |
sammacjunkie | 4:36e0aa194b45 | 103 | */ |
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 | 4:36e0aa194b45 | 112 | RemoteIR::Format format; |
sammacjunkie | 4:36e0aa194b45 | 113 | memset(buf1, 0x00, sizeof(buf1)); |
sammacjunkie | 4:36e0aa194b45 | 114 | memset(buf2, 0x00, sizeof(buf2)); |
sammacjunkie | 4:36e0aa194b45 | 115 | //END IR Init |
sammacjunkie | 4:36e0aa194b45 | 116 | |
sammacjunkie | 4:36e0aa194b45 | 117 | printf("Listening...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 118 | |
sarvagyavaish | 3:a3b4d032f48f | 119 | |
sarvagyavaish | 3:a3b4d032f48f | 120 | Timer tm; |
sarvagyavaish | 3:a3b4d032f48f | 121 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 122 | //Listen indefinitely |
sarvagyavaish | 3:a3b4d032f48f | 123 | while(true) { |
sammacjunkie | 4:36e0aa194b45 | 124 | svr.poll(); |
sarvagyavaish | 3:a3b4d032f48f | 125 | if(tm.read()>.5) { |
sarvagyavaish | 3:a3b4d032f48f | 126 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 127 | } |
sammacjunkie | 6:70a0af38edcc | 128 | if (Learn) { |
sammacjunkie | 6:70a0af38edcc | 129 | led1 = 1; |
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 | } |
sammacjunkie | 6:70a0af38edcc | 139 | printf("%d",buf1); |
sammacjunkie | 7:7b3f53b5e737 | 140 | //printf("%s",Learn_name); |
sammacjunkie | 6:70a0af38edcc | 141 | led1 = 0; |
sammacjunkie | 6:70a0af38edcc | 142 | Learn = 0; |
sammacjunkie | 6:70a0af38edcc | 143 | } |
sammacjunkie | 6:70a0af38edcc | 144 | |
donatien | 0:5630d7e58a0b | 145 | } |
sarvagyavaish | 3:a3b4d032f48f | 146 | |
sarvagyavaish | 3:a3b4d032f48f | 147 | return 0; |
donatien | 0:5630d7e58a0b | 148 | } |
sammacjunkie | 4:36e0aa194b45 | 149 |