mbed_controller / Mbed 2 deprecated mbed_controller_demo

Dependencies:   CMDB EthernetInterface HTTPD dconfig mbed-rpc mbed-rtos mbed storage_on_flash

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 Typical controller demo program based on Seeed Arch Max.
00003 The mbed has potential power by combining variable library, this program unveils that kind of thing.
00004 Features:
00005 - Multi-thread architecture
00006 - Inter-thread message communication
00007 - Independent command shell using thread
00008 - HTTPD with CGI, WS, RPC
00009 - Key & value pair configuration load/save
00010 */
00011 
00012 #include "mbed.h"
00013 #include "rtos.h"
00014 #include "console.h"
00015 #include "httpd_service.h"
00016 #include "util.h"
00017 #include "EthernetInterface.h"
00018 #include "main.h"
00019 
00020 #define SUPPORT_CONSOLE         1
00021 #define SUPPORT_ETH             1
00022 #define SUPPORT_HTTPD           1
00023 
00024 const unsigned connect_timeout_ms = 2000;
00025 
00026 static Mail<MainMessage_t, 5> _mainq;
00027 
00028 static DigitalOut _alive_led(LED1);
00029 
00030 EthernetInterface _eth;
00031 MainConfig _config;
00032 
00033 static void eth_up_dhcp()
00034 {
00035 
00036     printf("eth_up_dhcp: init()\r\n");
00037     _eth.init();
00038     _eth.connect(connect_timeout_ms);
00039 }
00040 
00041 static void eth_up_static(const char *ip, const char *netmask, const char *gateway)
00042 {
00043     const unsigned static_connect_timeout_ms = 3;
00044 
00045     printf("eth_up_static: init('%s', '%s', '%s')\r\n", ip, netmask, gateway);
00046     _eth.init(ip, netmask, gateway);
00047     _eth.connect(static_connect_timeout_ms);
00048 }
00049 
00050 static void eth_up()
00051 {
00052     _config.lock_config();
00053     if (_config.lookup("eth") == "dhcp")
00054         eth_up_dhcp();
00055     else
00056         eth_up_static(_config.lookup_as_cstr("ip", ""), _config.lookup_as_cstr("mask", ""), _config.lookup_as_cstr("gw", ""));
00057     _config.unlock_config();
00058 }
00059 
00060 static void eth_stat()
00061 {
00062     printf("eth '%s', '%s', '%s'\r\n", _eth.getIPAddress(), _eth.getNetworkMask(), _eth.getGateway());
00063 }
00064 
00065 
00066 bool send_main_message(MainMessageId msg_id, uint32_t msg_p1, uint32_t msg_p2)
00067 {
00068     MainMessage_t *msg = (MainMessage_t*)_mainq.alloc();
00069 
00070     if (msg) {
00071         msg->msg_id = msg_id;
00072         msg->msg_p1 = msg_p1;
00073         msg->msg_p2 = msg_p2;
00074         _mainq.put(msg);
00075         return true;
00076     } else {
00077         printf("_mainq.alloc fail\r\n");
00078         return false;
00079     }
00080 }
00081 
00082 static void on_main_message(MainMessageId msg_id, uint32_t msg_p1, uint32_t msg_p2)
00083 {
00084     switch (msg_id) {
00085         case MSG_IFUP:
00086             printf("eth connect: %d\r\n", _eth.connect(1000));
00087             break;
00088         case MSG_IFDOWN:
00089             printf("eth disconnect\r\n");
00090             _eth.disconnect();
00091             break;
00092         case MSG_IFSTAT:
00093             eth_stat();
00094             break;
00095     }
00096 }
00097 
00098 int main()
00099 {
00100     Serial pc(USBTX, USBRX);
00101 
00102     pc.baud(115200);
00103     pc.printf("built at " __DATE__ " " __TIME__ "\r\n");
00104     pc.printf("memory stat:\r\n");
00105     print_memstat();
00106 
00107     if (!_config.load_config()) {
00108         printf("load_config fail: reset_default()\r\n");
00109         _config.reset_default();
00110     }
00111 
00112 #if SUPPORT_CONSOLE
00113     printf("SUPPORT_CONSOLE\r\n");
00114     Thread console(console_thread, &pc, osPriorityNormal, CONSOLE_STACK_SIZE);
00115 #endif
00116 
00117 #if SUPPORT_ETH
00118     printf("SUPPORT_ETH\r\n");
00119     eth_up();
00120 
00121 #if SUPPORT_HTTPD
00122     httpd_start(80);
00123 #endif
00124 #endif
00125 
00126     while (1) {
00127         const uint32_t wait_ms = 100;
00128         osEvent evt = _mainq.get(wait_ms);
00129 
00130         if (evt.status == osEventMail) {
00131             MainMessage_t *msg = (MainMessage_t*)evt.value.p;
00132 
00133             on_main_message(msg->msg_id, msg->msg_p1, msg->msg_p2);
00134             _mainq.free(msg);
00135         }
00136 
00137         _alive_led = !_alive_led;
00138     }
00139 
00140     return 0;
00141 }
00142 
00143 /**
00144  * Overriding MAC address
00145  */
00146 void mbed_mac_address(char *mac)
00147 {
00148     // Change ethernet mac address of device.
00149     mac[0] = 0x00;
00150     mac[1] = 0x80;
00151     mac[2] = 0xE1;
00152     mac[3] = 0x00;
00153     mac[4] = 0x02;
00154     mac[5] = 0x01;
00155 }