Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf mbed TMP102 HTTPServer ADJD-S371_ColourSens
main.cpp@0:3abbad5a358a, 2010-10-01 (annotated)
- Committer:
- MichaelW
- Date:
- Fri Oct 01 13:43:20 2010 +0000
- Revision:
- 0:3abbad5a358a
- Child:
- 1:0f7aff70292e
Publish to accompany applet demo
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MichaelW | 0:3abbad5a358a | 1 | /** |
| MichaelW | 0:3abbad5a358a | 2 | Copyright (c) 2010 ARM Ltd |
| MichaelW | 0:3abbad5a358a | 3 | |
| MichaelW | 0:3abbad5a358a | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| MichaelW | 0:3abbad5a358a | 5 | of this software and associated documentation files (the "Software"), to deal |
| MichaelW | 0:3abbad5a358a | 6 | in the Software without restriction, including without limitation the rights |
| MichaelW | 0:3abbad5a358a | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| MichaelW | 0:3abbad5a358a | 8 | copies of the Software, and to permit persons to whom the Software is |
| MichaelW | 0:3abbad5a358a | 9 | furnished to do so, subject to the following conditions: |
| MichaelW | 0:3abbad5a358a | 10 | |
| MichaelW | 0:3abbad5a358a | 11 | The above copyright notice and this permission notice shall be included in |
| MichaelW | 0:3abbad5a358a | 12 | all copies or substantial portions of the Software. |
| MichaelW | 0:3abbad5a358a | 13 | |
| MichaelW | 0:3abbad5a358a | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| MichaelW | 0:3abbad5a358a | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| MichaelW | 0:3abbad5a358a | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| MichaelW | 0:3abbad5a358a | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| MichaelW | 0:3abbad5a358a | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| MichaelW | 0:3abbad5a358a | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| MichaelW | 0:3abbad5a358a | 20 | THE SOFTWARE. |
| MichaelW | 0:3abbad5a358a | 21 | */ |
| MichaelW | 0:3abbad5a358a | 22 | #include "mbed.h" |
| MichaelW | 0:3abbad5a358a | 23 | #include "EthernetNetIf.h" |
| MichaelW | 0:3abbad5a358a | 24 | #include "HTTPServer.h" |
| MichaelW | 0:3abbad5a358a | 25 | #include "RPCFunction.h" |
| MichaelW | 0:3abbad5a358a | 26 | #include "RPCVariable.h" |
| MichaelW | 0:3abbad5a358a | 27 | #include "SerialRPCInterface.h" |
| MichaelW | 0:3abbad5a358a | 28 | #include "ADJDColourSensor.h" |
| MichaelW | 0:3abbad5a358a | 29 | #include "TMP102.h" |
| MichaelW | 0:3abbad5a358a | 30 | #include "scp1000.h" |
| MichaelW | 0:3abbad5a358a | 31 | LocalFileSystem fs("webfs"); |
| MichaelW | 0:3abbad5a358a | 32 | |
| MichaelW | 0:3abbad5a358a | 33 | EthernetNetIf eth; |
| MichaelW | 0:3abbad5a358a | 34 | HTTPServer svr; |
| MichaelW | 0:3abbad5a358a | 35 | |
| MichaelW | 0:3abbad5a358a | 36 | |
| MichaelW | 0:3abbad5a358a | 37 | void poll(void){ |
| MichaelW | 0:3abbad5a358a | 38 | Net::poll(); |
| MichaelW | 0:3abbad5a358a | 39 | } |
| MichaelW | 0:3abbad5a358a | 40 | |
| MichaelW | 0:3abbad5a358a | 41 | Ticker poller; |
| MichaelW | 0:3abbad5a358a | 42 | |
| MichaelW | 0:3abbad5a358a | 43 | void getColour(char * input, char * Output); |
| MichaelW | 0:3abbad5a358a | 44 | void getTemperature(char *input, char * Output); |
| MichaelW | 0:3abbad5a358a | 45 | void getPressure(char *input, char * Output); |
| MichaelW | 0:3abbad5a358a | 46 | void getSCPTemperature(char *input, char * Output); |
| MichaelW | 0:3abbad5a358a | 47 | |
| MichaelW | 0:3abbad5a358a | 48 | //Set up sensors |
| MichaelW | 0:3abbad5a358a | 49 | TMP102 Temperature(p9, p10, 0x90); |
| MichaelW | 0:3abbad5a358a | 50 | ADJDColourSensor Colour(p28,p27, p22); |
| MichaelW | 0:3abbad5a358a | 51 | SCP1000 Pressure(p5,p6,p7,p8); |
| MichaelW | 0:3abbad5a358a | 52 | DigitalIn PIR(p21, "PIR"); |
| MichaelW | 0:3abbad5a358a | 53 | AnalogIn Light(p20, "Light"); |
| MichaelW | 0:3abbad5a358a | 54 | |
| MichaelW | 0:3abbad5a358a | 55 | //Set up custom RPC |
| MichaelW | 0:3abbad5a358a | 56 | RPCFunction GetTemp(&getTemperature, "Temperature"); |
| MichaelW | 0:3abbad5a358a | 57 | RPCFunction GetPressure(&getPressure, "Pressure"); |
| MichaelW | 0:3abbad5a358a | 58 | RPCFunction GetSCPTemperature(&getSCPTemperature, "SCPtemperature"); |
| MichaelW | 0:3abbad5a358a | 59 | RPCFunction GetColour(&getColour, "Colour"); |
| MichaelW | 0:3abbad5a358a | 60 | |
| MichaelW | 0:3abbad5a358a | 61 | //Serial RPC used for testing |
| MichaelW | 0:3abbad5a358a | 62 | //SerialRPCInterface applet(USBTX, USBRX, 115200); |
| MichaelW | 0:3abbad5a358a | 63 | |
| MichaelW | 0:3abbad5a358a | 64 | |
| MichaelW | 0:3abbad5a358a | 65 | int main() { |
| MichaelW | 0:3abbad5a358a | 66 | Base::add_rpc_class<AnalogIn>(); |
| MichaelW | 0:3abbad5a358a | 67 | Base::add_rpc_class<AnalogOut>(); |
| MichaelW | 0:3abbad5a358a | 68 | Base::add_rpc_class<DigitalIn>(); |
| MichaelW | 0:3abbad5a358a | 69 | Base::add_rpc_class<DigitalOut>(); |
| MichaelW | 0:3abbad5a358a | 70 | Base::add_rpc_class<DigitalInOut>(); |
| MichaelW | 0:3abbad5a358a | 71 | Base::add_rpc_class<PwmOut>(); |
| MichaelW | 0:3abbad5a358a | 72 | Base::add_rpc_class<Timer>(); |
| MichaelW | 0:3abbad5a358a | 73 | Base::add_rpc_class<BusOut>(); |
| MichaelW | 0:3abbad5a358a | 74 | Base::add_rpc_class<BusIn>(); |
| MichaelW | 0:3abbad5a358a | 75 | Base::add_rpc_class<BusInOut>(); |
| MichaelW | 0:3abbad5a358a | 76 | Base::add_rpc_class<Serial>(); |
| MichaelW | 0:3abbad5a358a | 77 | |
| MichaelW | 0:3abbad5a358a | 78 | printf("Setting up...\n"); |
| MichaelW | 0:3abbad5a358a | 79 | EthernetErr ethErr = eth.setup(); |
| MichaelW | 0:3abbad5a358a | 80 | if(ethErr) |
| MichaelW | 0:3abbad5a358a | 81 | { |
| MichaelW | 0:3abbad5a358a | 82 | printf("Error %d in setup.\n", ethErr); |
| MichaelW | 0:3abbad5a358a | 83 | return -1; |
| MichaelW | 0:3abbad5a358a | 84 | } |
| MichaelW | 0:3abbad5a358a | 85 | printf("Setup OK\n"); |
| MichaelW | 0:3abbad5a358a | 86 | |
| MichaelW | 0:3abbad5a358a | 87 | FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
| MichaelW | 0:3abbad5a358a | 88 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
| MichaelW | 0:3abbad5a358a | 89 | |
| MichaelW | 0:3abbad5a358a | 90 | svr.addHandler<SimpleHandler>("/hello"); |
| MichaelW | 0:3abbad5a358a | 91 | svr.addHandler<RPCHandler>("/rpc"); |
| MichaelW | 0:3abbad5a358a | 92 | svr.addHandler<FSHandler>("/files"); |
| MichaelW | 0:3abbad5a358a | 93 | svr.addHandler<FSHandler>("/"); //Default handler |
| MichaelW | 0:3abbad5a358a | 94 | //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm |
| MichaelW | 0:3abbad5a358a | 95 | |
| MichaelW | 0:3abbad5a358a | 96 | svr.bind(80); |
| MichaelW | 0:3abbad5a358a | 97 | |
| MichaelW | 0:3abbad5a358a | 98 | poller.attach(&poll, 0.05); |
| MichaelW | 0:3abbad5a358a | 99 | printf("Listening...\n"); |
| MichaelW | 0:3abbad5a358a | 100 | //Listen indefinitely |
| MichaelW | 0:3abbad5a358a | 101 | while(true){ |
| MichaelW | 0:3abbad5a358a | 102 | //Do own thing here, the poll is handled by a ticker. |
| MichaelW | 0:3abbad5a358a | 103 | |
| MichaelW | 0:3abbad5a358a | 104 | //This could for example be data logging. |
| MichaelW | 0:3abbad5a358a | 105 | |
| MichaelW | 0:3abbad5a358a | 106 | } |
| MichaelW | 0:3abbad5a358a | 107 | |
| MichaelW | 0:3abbad5a358a | 108 | } |
| MichaelW | 0:3abbad5a358a | 109 | |
| MichaelW | 0:3abbad5a358a | 110 | //RPC Functions - these are used to wrap the sensor libraries that other wise would not be acessible over RPC. |
| MichaelW | 0:3abbad5a358a | 111 | void getTemperature(char *input, char * Output){ |
| MichaelW | 0:3abbad5a358a | 112 | float f = Temperature.read(); |
| MichaelW | 0:3abbad5a358a | 113 | sprintf(Output, "%f", f); |
| MichaelW | 0:3abbad5a358a | 114 | } |
| MichaelW | 0:3abbad5a358a | 115 | void getPressure(char *input, char * Output){ |
| MichaelW | 0:3abbad5a358a | 116 | float f = Pressure.read(); |
| MichaelW | 0:3abbad5a358a | 117 | sprintf(Output, "%f",f); |
| MichaelW | 0:3abbad5a358a | 118 | } |
| MichaelW | 0:3abbad5a358a | 119 | void getSCPTemperature(char *input, char * Output){ |
| MichaelW | 0:3abbad5a358a | 120 | float f = Pressure.readTemperature(); |
| MichaelW | 0:3abbad5a358a | 121 | sprintf(Output, "%f", f); |
| MichaelW | 0:3abbad5a358a | 122 | } |
| MichaelW | 0:3abbad5a358a | 123 | void getColour(char *input, char * Output){ |
| MichaelW | 0:3abbad5a358a | 124 | float clear = Colour.read(); |
| MichaelW | 0:3abbad5a358a | 125 | sprintf(Output, "%f,%f,%f,%f,", clear/500, Colour.red/500, Colour.green/500, Colour.blue/500); |
| MichaelW | 0:3abbad5a358a | 126 | } |