smartcontrol remote
Dependencies: EthernetInterface HTTPServer RemoteIR SDFileSystem mbed-rpc mbed-rtos mbed
Fork of SmartRemote by
main.cpp@6:70a0af38edcc, 2013-12-03 (annotated)
- Committer:
- sammacjunkie
- Date:
- Tue Dec 03 03:49:12 2013 +0000
- Revision:
- 6:70a0af38edcc
- Parent:
- 4:36e0aa194b45
- Child:
- 7:7b3f53b5e737
updated
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 | |
sarvagyavaish | 3:a3b4d032f48f | 24 | // Start RPC |
sarvagyavaish | 3:a3b4d032f48f | 25 | #include "RPCVariable.h" |
sammacjunkie | 4:36e0aa194b45 | 26 | int Request = 0; |
sarvagyavaish | 3:a3b4d032f48f | 27 | int Learn = 0; |
sarvagyavaish | 3:a3b4d032f48f | 28 | //Make these variables accessible over RPC by attaching them to an RPCVariable |
sammacjunkie | 4:36e0aa194b45 | 29 | RPCVariable<int> RPCRequest(&Request, "Request"); |
sammacjunkie | 4:36e0aa194b45 | 30 | RPCVariable<int> RPCLearn(&Learn, "Learn"); |
sarvagyavaish | 3:a3b4d032f48f | 31 | // End RPC |
sarvagyavaish | 3:a3b4d032f48f | 32 | |
sammacjunkie | 4:36e0aa194b45 | 33 | Serial pc(USBTX, USBRX, "pc"); |
sarvagyavaish | 3:a3b4d032f48f | 34 | |
sammacjunkie | 4:36e0aa194b45 | 35 | // Instantiate a HTTPServer to handle incoming requests |
sammacjunkie | 4:36e0aa194b45 | 36 | HTTPServer svr; |
sammacjunkie | 4:36e0aa194b45 | 37 | // Instantiate a local file system handler named 'local' which will be used later to access files on the mbed. |
sammacjunkie | 4:36e0aa194b45 | 38 | LocalFileSystem local("local"); |
donatien | 0:5630d7e58a0b | 39 | |
sammacjunkie | 4:36e0aa194b45 | 40 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed |
donatien | 0:5630d7e58a0b | 41 | |
sarvagyavaish | 3:a3b4d032f48f | 42 | int main() |
sarvagyavaish | 3:a3b4d032f48f | 43 | { |
sammacjunkie | 4:36e0aa194b45 | 44 | |
sammacjunkie | 4:36e0aa194b45 | 45 | printf("Setting up Apache...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 46 | |
sammacjunkie | 4:36e0aa194b45 | 47 | HTTPFsRequestHandler::mount("/local/", "/"); |
sammacjunkie | 4:36e0aa194b45 | 48 | svr.addHandler<HTTPFsRequestHandler>("/"); |
sammacjunkie | 4:36e0aa194b45 | 49 | svr.addHandler<HTTPRpcRequestHandler>("/rpc"); |
sammacjunkie | 4:36e0aa194b45 | 50 | |
sammacjunkie | 4:36e0aa194b45 | 51 | EthernetInterface eth; |
sammacjunkie | 4:36e0aa194b45 | 52 | eth.init(); //Use DHCP |
sammacjunkie | 4:36e0aa194b45 | 53 | eth.connect(); |
sammacjunkie | 4:36e0aa194b45 | 54 | |
sammacjunkie | 4:36e0aa194b45 | 55 | // Now start the server on port 80. |
sammacjunkie | 4:36e0aa194b45 | 56 | if (!svr.start(80, ð)) { |
sammacjunkie | 4:36e0aa194b45 | 57 | error("Server not starting !"); |
sammacjunkie | 4:36e0aa194b45 | 58 | exit(0); |
sarvagyavaish | 3:a3b4d032f48f | 59 | } |
sarvagyavaish | 3:a3b4d032f48f | 60 | |
sammacjunkie | 4:36e0aa194b45 | 61 | printf("IP: %s\n \r", eth.getIPAddress()); |
sammacjunkie | 4:36e0aa194b45 | 62 | printf("Setup OK\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 63 | |
sammacjunkie | 4:36e0aa194b45 | 64 | |
sarvagyavaish | 3:a3b4d032f48f | 65 | |
sarvagyavaish | 3:a3b4d032f48f | 66 | |
sarvagyavaish | 3:a3b4d032f48f | 67 | // DB Init |
sarvagyavaish | 3:a3b4d032f48f | 68 | mkdir("/sd/SmartRemote", 0777); |
sarvagyavaish | 3:a3b4d032f48f | 69 | |
sarvagyavaish | 3:a3b4d032f48f | 70 | char code[] = "123AB"; |
sarvagyavaish | 3:a3b4d032f48f | 71 | char name[] = "Button Name"; |
sammacjunkie | 4:36e0aa194b45 | 72 | |
sarvagyavaish | 3:a3b4d032f48f | 73 | char results_code[128]; |
sarvagyavaish | 3:a3b4d032f48f | 74 | char results_name[128]; |
sammacjunkie | 4:36e0aa194b45 | 75 | /* |
sarvagyavaish | 3:a3b4d032f48f | 76 | db_insert_tuple(code, name); |
sarvagyavaish | 3:a3b4d032f48f | 77 | FILE *fread = fopen("/sd/SmartRemote/db.txt", "r"); |
sammacjunkie | 4:36e0aa194b45 | 78 | db_find_tuple(fread, Request, results_name, results_code); |
sarvagyavaish | 3:a3b4d032f48f | 79 | fclose(fread); |
sammacjunkie | 4:36e0aa194b45 | 80 | */ |
sarvagyavaish | 3:a3b4d032f48f | 81 | //End DB init |
sarvagyavaish | 3:a3b4d032f48f | 82 | |
sarvagyavaish | 3:a3b4d032f48f | 83 | |
sammacjunkie | 4:36e0aa194b45 | 84 | //IR Init |
sammacjunkie | 4:36e0aa194b45 | 85 | uint8_t buf1[32]; |
sammacjunkie | 4:36e0aa194b45 | 86 | uint8_t buf2[32]; |
sammacjunkie | 4:36e0aa194b45 | 87 | int bitlength1; |
sammacjunkie | 4:36e0aa194b45 | 88 | int bitlength2; |
sammacjunkie | 4:36e0aa194b45 | 89 | RemoteIR::Format format; |
sammacjunkie | 4:36e0aa194b45 | 90 | memset(buf1, 0x00, sizeof(buf1)); |
sammacjunkie | 4:36e0aa194b45 | 91 | memset(buf2, 0x00, sizeof(buf2)); |
sammacjunkie | 4:36e0aa194b45 | 92 | //END IR Init |
sammacjunkie | 4:36e0aa194b45 | 93 | |
sammacjunkie | 4:36e0aa194b45 | 94 | printf("Listening...\n \r"); |
sammacjunkie | 4:36e0aa194b45 | 95 | |
sarvagyavaish | 3:a3b4d032f48f | 96 | |
sarvagyavaish | 3:a3b4d032f48f | 97 | Timer tm; |
sarvagyavaish | 3:a3b4d032f48f | 98 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 99 | //Listen indefinitely |
sarvagyavaish | 3:a3b4d032f48f | 100 | while(true) { |
sammacjunkie | 4:36e0aa194b45 | 101 | svr.poll(); |
sarvagyavaish | 3:a3b4d032f48f | 102 | if(tm.read()>.5) { |
sarvagyavaish | 3:a3b4d032f48f | 103 | tm.start(); |
sarvagyavaish | 3:a3b4d032f48f | 104 | } |
sammacjunkie | 6:70a0af38edcc | 105 | if (Learn) { |
sammacjunkie | 6:70a0af38edcc | 106 | led1 = 1; |
sammacjunkie | 6:70a0af38edcc | 107 | { |
sammacjunkie | 6:70a0af38edcc | 108 | bitlength1 = receive(&format, buf1, sizeof(buf1)); |
sammacjunkie | 6:70a0af38edcc | 109 | if (bitlength1 < 0) { |
sammacjunkie | 6:70a0af38edcc | 110 | continue; |
sammacjunkie | 6:70a0af38edcc | 111 | } |
sammacjunkie | 6:70a0af38edcc | 112 | display_status("RECV", bitlength1); |
sammacjunkie | 6:70a0af38edcc | 113 | display_data(buf1, bitlength1); |
sammacjunkie | 6:70a0af38edcc | 114 | //display_format(format); |
sammacjunkie | 6:70a0af38edcc | 115 | } |
sammacjunkie | 6:70a0af38edcc | 116 | printf("%d",buf1); |
sammacjunkie | 6:70a0af38edcc | 117 | led1 = 0; |
sammacjunkie | 6:70a0af38edcc | 118 | Learn = 0; |
sammacjunkie | 6:70a0af38edcc | 119 | } |
sammacjunkie | 6:70a0af38edcc | 120 | |
donatien | 0:5630d7e58a0b | 121 | } |
sarvagyavaish | 3:a3b4d032f48f | 122 | |
sarvagyavaish | 3:a3b4d032f48f | 123 | return 0; |
donatien | 0:5630d7e58a0b | 124 | } |
sammacjunkie | 4:36e0aa194b45 | 125 |