Fx0とLEDを使うサンプルです。Ethernet用です。

Dependencies:   EthernetNetIf HTTPServer RPCInterface mbed

Fork of ethernet_test_http_2 by Yasushi TAUCHI

Committer:
komoritan
Date:
Sat Feb 14 00:12:39 2015 +0000
Revision:
2:7e3c2cd97026
Parent:
1:09c09f50c156
First ver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 1:09c09f50c156 1 /* (c) KDDI Technology
komoritan 1:09c09f50c156 2 *
komoritan 1:09c09f50c156 3 * This Source Code Form is subject to the terms of the Mozilla Public
komoritan 1:09c09f50c156 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
komoritan 1:09c09f50c156 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
komoritan 1:09c09f50c156 6
yueee_yt 0:c14da41d70ea 7 #include "mbed.h"
yueee_yt 0:c14da41d70ea 8 #include "EthernetNetIf.h"
yueee_yt 0:c14da41d70ea 9 #include "HTTPServer.h"
yueee_yt 0:c14da41d70ea 10 #include "RPCFunction.h"
yueee_yt 0:c14da41d70ea 11
yueee_yt 0:c14da41d70ea 12 DigitalOut led1(LED1,"led1");
yueee_yt 0:c14da41d70ea 13 DigitalOut led2(LED2,"led2");
yueee_yt 0:c14da41d70ea 14 DigitalOut led3(LED3,"led3");
yueee_yt 0:c14da41d70ea 15 DigitalOut led4(LED4,"led4");
yueee_yt 0:c14da41d70ea 16
yueee_yt 0:c14da41d70ea 17 #if 1
yueee_yt 0:c14da41d70ea 18 /*
yueee_yt 0:c14da41d70ea 19 * Use DHCP
yueee_yt 0:c14da41d70ea 20 */
yueee_yt 0:c14da41d70ea 21 EthernetNetIf ethif;
yueee_yt 0:c14da41d70ea 22 #else
yueee_yt 0:c14da41d70ea 23 /*
yueee_yt 0:c14da41d70ea 24 * Use "static IP address" (Parameters:IP, Subnet mask, Gateway, DNS)
yueee_yt 0:c14da41d70ea 25 */
yueee_yt 0:c14da41d70ea 26 EthernetNetIf ethif(IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx), IpAddr(xxx,xxx,xxx,xxx));
yueee_yt 0:c14da41d70ea 27 #endif
yueee_yt 0:c14da41d70ea 28
yueee_yt 0:c14da41d70ea 29 HTTPServer server;
yueee_yt 0:c14da41d70ea 30 LocalFileSystem local("local");
komoritan 1:09c09f50c156 31
komoritan 1:09c09f50c156 32 /* RFC Interface */
komoritan 1:09c09f50c156 33 // inputデータに従い、LED1~4の点灯・消灯を設定する。inputデータはカンマ区切りを想定。
komoritan 1:09c09f50c156 34 void doSetLed(char* input, char* output);
komoritan 1:09c09f50c156 35 //RPC Interfaceを宣言(setLed:外部IF公開名、doSetLed:外部からのコール後、処理する関数名)
komoritan 1:09c09f50c156 36 RPCFunction test(&doSetLed, "setLed");
komoritan 1:09c09f50c156 37 // LEDの点灯・消灯情報を取得可能な外部IF
komoritan 1:09c09f50c156 38 void doGetLed(char* input, char* output);
komoritan 1:09c09f50c156 39 RPCFunction getdata(&doGetLed, "getLed");
komoritan 1:09c09f50c156 40
komoritan 1:09c09f50c156 41 /* locan func */
komoritan 1:09c09f50c156 42 void _createOutputString(char* output);
yueee_yt 0:c14da41d70ea 43
yueee_yt 0:c14da41d70ea 44 int main(void) {
yueee_yt 0:c14da41d70ea 45
yueee_yt 0:c14da41d70ea 46 if (ethif.setup()) {
yueee_yt 0:c14da41d70ea 47 error("Ethernet setup failed.");
yueee_yt 0:c14da41d70ea 48 return 1;
yueee_yt 0:c14da41d70ea 49 }
yueee_yt 0:c14da41d70ea 50
yueee_yt 0:c14da41d70ea 51 wait(1);
yueee_yt 0:c14da41d70ea 52 server.addHandler<RPCHandler>("/rpc");
yueee_yt 0:c14da41d70ea 53 FSHandler::mount("/local", "/");
yueee_yt 0:c14da41d70ea 54 server.addHandler<FSHandler>("/");
yueee_yt 0:c14da41d70ea 55 server.bind(80);
yueee_yt 0:c14da41d70ea 56 while (1) {
yueee_yt 0:c14da41d70ea 57 Net::poll();
yueee_yt 0:c14da41d70ea 58 }
yueee_yt 0:c14da41d70ea 59 }
yueee_yt 0:c14da41d70ea 60
komoritan 1:09c09f50c156 61 // Led1-4の点灯・消灯処理
komoritan 1:09c09f50c156 62 void doSetLed(char* input, char* output) {
komoritan 1:09c09f50c156 63
komoritan 1:09c09f50c156 64 char *tok;
komoritan 1:09c09f50c156 65 char s2[] = ","; /* カンマで区切られていることを想定 */
komoritan 1:09c09f50c156 66 int cnt = 1;
komoritan 1:09c09f50c156 67 tok = strtok( input, "," );
komoritan 1:09c09f50c156 68
komoritan 1:09c09f50c156 69 // カンマ区切りの文字列を取得し、1:LED点灯、0(1以外):消灯とする
komoritan 1:09c09f50c156 70 while( tok != NULL ){
komoritan 1:09c09f50c156 71 int ret;
komoritan 1:09c09f50c156 72 int setled = 0;
komoritan 1:09c09f50c156 73 ret = strcmp( tok, "1" );
komoritan 1:09c09f50c156 74 if (ret == 0) {
komoritan 1:09c09f50c156 75 setled = 1;
komoritan 1:09c09f50c156 76 } else {
komoritan 1:09c09f50c156 77 setled = 0;
komoritan 1:09c09f50c156 78 }
komoritan 1:09c09f50c156 79 switch (cnt) {
komoritan 1:09c09f50c156 80 case 1:
komoritan 1:09c09f50c156 81 led1 = setled;
komoritan 1:09c09f50c156 82 break;
komoritan 1:09c09f50c156 83 case 2:
komoritan 1:09c09f50c156 84 led2 = setled;
komoritan 1:09c09f50c156 85 break;
komoritan 1:09c09f50c156 86 case 3:
komoritan 1:09c09f50c156 87 led3 = setled;
komoritan 1:09c09f50c156 88 break;
komoritan 1:09c09f50c156 89 case 4:
komoritan 1:09c09f50c156 90 led4 = setled;
komoritan 1:09c09f50c156 91 break;
komoritan 1:09c09f50c156 92 default:
komoritan 1:09c09f50c156 93 break;
komoritan 1:09c09f50c156 94 }
komoritan 1:09c09f50c156 95 cnt++;
komoritan 1:09c09f50c156 96 tok = strtok( NULL, s2 ); /* 2回目以降 */
komoritan 1:09c09f50c156 97 }
komoritan 1:09c09f50c156 98
komoritan 1:09c09f50c156 99 // 完了時には文字列"finish"を返却する
komoritan 1:09c09f50c156 100 _createOutputString(output);
komoritan 1:09c09f50c156 101 }
komoritan 1:09c09f50c156 102
komoritan 1:09c09f50c156 103 // LEDの点灯情報をJSONデータで返却する
komoritan 1:09c09f50c156 104 void doGetLed(char* input, char* output) {
komoritan 1:09c09f50c156 105
komoritan 1:09c09f50c156 106 _createOutputString(output);
komoritan 1:09c09f50c156 107 }
komoritan 1:09c09f50c156 108
komoritan 1:09c09f50c156 109 // outputにJSON形式の文字列を設定する
komoritan 1:09c09f50c156 110 void _createOutputString(char* output) {
komoritan 1:09c09f50c156 111
komoritan 1:09c09f50c156 112 sprintf(output, "{\"led1\":%d, \"led2\":%d, \"led3\":%d, \"led4\":%d}",(int)led1, (int)led2, (int)led3, (int)led4);
komoritan 1:09c09f50c156 113 }