![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
LED Demo for Tech Con 2014
Dependencies: EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X
Fork of LPC1768_LWM2M_Client by
main.cpp@4:71b211dbe14c, 2014-09-28 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sun Sep 28 01:00:23 2014 +0000
- Revision:
- 4:71b211dbe14c
- Parent:
- 3:e3aa908cbb51
- Child:
- 5:fcfba34d1aeb
MAC address based EP name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:9101343a70cd | 1 | #include "mbed.h" |
michaeljkoster | 0:9101343a70cd | 2 | #include "EthernetInterface.h" |
michaeljkoster | 0:9101343a70cd | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:9101343a70cd | 4 | #include "dbg.h" |
michaeljkoster | 1:09a525977925 | 5 | // Include resources |
michaeljkoster | 0:9101343a70cd | 6 | #include "light.h" |
michaeljkoster | 0:9101343a70cd | 7 | |
michaeljkoster | 2:4166c3e5b321 | 8 | #include "PololuLedStrip.h" |
michaeljkoster | 2:4166c3e5b321 | 9 | PololuLedStrip ledStrip(P0_4); |
michaeljkoster | 2:4166c3e5b321 | 10 | #define LED_COUNT 60 |
michaeljkoster | 2:4166c3e5b321 | 11 | //#define LED_COUNT 120 |
michaeljkoster | 2:4166c3e5b321 | 12 | rgb_color colors[LED_COUNT]; |
michaeljkoster | 2:4166c3e5b321 | 13 | |
michaeljkoster | 4:71b211dbe14c | 14 | extern "C" void mbed_mac_address(char *mac); |
michaeljkoster | 4:71b211dbe14c | 15 | |
michaeljkoster | 0:9101343a70cd | 16 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:9101343a70cd | 17 | |
michaeljkoster | 0:9101343a70cd | 18 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 19 | // Configuration section |
michaeljkoster | 0:9101343a70cd | 20 | |
michaeljkoster | 0:9101343a70cd | 21 | // Ethernet configuration |
michaeljkoster | 0:9101343a70cd | 22 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:9101343a70cd | 23 | #define DHCP |
michaeljkoster | 0:9101343a70cd | 24 | |
michaeljkoster | 0:9101343a70cd | 25 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 1:09a525977925 | 26 | #define IP "10.0.0.199" |
michaeljkoster | 0:9101343a70cd | 27 | #define MASK "255.255.255.0" |
michaeljkoster | 1:09a525977925 | 28 | #define GW "10.0.0.1" |
michaeljkoster | 0:9101343a70cd | 29 | |
michaeljkoster | 0:9101343a70cd | 30 | // NSP configuration |
michaeljkoster | 0:9101343a70cd | 31 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:9101343a70cd | 32 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 1:09a525977925 | 33 | static const char* NSP_ADDRESS = "23.99.99.252"; // barista.cloudapp.net |
michaeljkoster | 1:09a525977925 | 34 | //static const char* NSP_ADDRESS = "192.168.1.200"; |
michaeljkoster | 0:9101343a70cd | 35 | static const int NSP_PORT = 5683; |
michaeljkoster | 4:71b211dbe14c | 36 | char endpoint_name[24] = "LED-booth-"; |
michaeljkoster | 0:9101343a70cd | 37 | uint8_t ep_type[] = {"mbed_device"}; |
michaeljkoster | 1:09a525977925 | 38 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 4:71b211dbe14c | 39 | char mac[6]; |
michaeljkoster | 4:71b211dbe14c | 40 | char macstring[13] ; |
michaeljkoster | 4:71b211dbe14c | 41 | |
michaeljkoster | 0:9101343a70cd | 42 | |
michaeljkoster | 0:9101343a70cd | 43 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 44 | // Ethernet initialization |
michaeljkoster | 0:9101343a70cd | 45 | |
michaeljkoster | 0:9101343a70cd | 46 | EthernetInterface eth; |
michaeljkoster | 0:9101343a70cd | 47 | static void ethernet_init() |
michaeljkoster | 0:9101343a70cd | 48 | { |
michaeljkoster | 0:9101343a70cd | 49 | |
michaeljkoster | 0:9101343a70cd | 50 | /* Initialize network */ |
michaeljkoster | 0:9101343a70cd | 51 | #ifdef DHCP |
michaeljkoster | 0:9101343a70cd | 52 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:9101343a70cd | 53 | eth.init(); |
michaeljkoster | 1:09a525977925 | 54 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 55 | #else |
michaeljkoster | 0:9101343a70cd | 56 | eth.init(IP, MASK, GW); |
michaeljkoster | 1:09a525977925 | 57 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 58 | #endif |
michaeljkoster | 0:9101343a70cd | 59 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:9101343a70cd | 60 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:9101343a70cd | 61 | |
michaeljkoster | 0:9101343a70cd | 62 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 63 | } |
michaeljkoster | 0:9101343a70cd | 64 | |
michaeljkoster | 0:9101343a70cd | 65 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 66 | // NSP initialization |
michaeljkoster | 0:9101343a70cd | 67 | |
michaeljkoster | 0:9101343a70cd | 68 | UDPSocket server; |
michaeljkoster | 0:9101343a70cd | 69 | Endpoint nsp; |
michaeljkoster | 0:9101343a70cd | 70 | |
michaeljkoster | 0:9101343a70cd | 71 | static void nsp_init() |
michaeljkoster | 0:9101343a70cd | 72 | { |
michaeljkoster | 0:9101343a70cd | 73 | server.init(); |
michaeljkoster | 0:9101343a70cd | 74 | server.bind(NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 75 | |
michaeljkoster | 0:9101343a70cd | 76 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 77 | |
michaeljkoster | 4:71b211dbe14c | 78 | mbed_mac_address(mac); |
michaeljkoster | 4:71b211dbe14c | 79 | sprintf(macstring, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
michaeljkoster | 4:71b211dbe14c | 80 | strncat(endpoint_name, macstring, 12); |
michaeljkoster | 4:71b211dbe14c | 81 | |
michaeljkoster | 0:9101343a70cd | 82 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 83 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 84 | |
michaeljkoster | 0:9101343a70cd | 85 | } |
michaeljkoster | 0:9101343a70cd | 86 | |
michaeljkoster | 0:9101343a70cd | 87 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 88 | // Resource creation |
michaeljkoster | 0:9101343a70cd | 89 | |
michaeljkoster | 0:9101343a70cd | 90 | static int create_resources() |
michaeljkoster | 0:9101343a70cd | 91 | { |
michaeljkoster | 0:9101343a70cd | 92 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 93 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 94 | |
michaeljkoster | 0:9101343a70cd | 95 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:9101343a70cd | 96 | |
michaeljkoster | 0:9101343a70cd | 97 | /* Create resources */ |
michaeljkoster | 0:9101343a70cd | 98 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 99 | if(!resource_ptr) |
michaeljkoster | 0:9101343a70cd | 100 | return 0; |
michaeljkoster | 0:9101343a70cd | 101 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 102 | |
michaeljkoster | 0:9101343a70cd | 103 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 104 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:9101343a70cd | 105 | { |
michaeljkoster | 0:9101343a70cd | 106 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 107 | return 0; |
michaeljkoster | 0:9101343a70cd | 108 | } |
michaeljkoster | 0:9101343a70cd | 109 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 110 | |
michaeljkoster | 0:9101343a70cd | 111 | // Static resources |
michaeljkoster | 2:4166c3e5b321 | 112 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "ARMDEMO", sizeof("ARMDEMO")-1); |
michaeljkoster | 2:4166c3e5b321 | 113 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "LED-STRIP", sizeof("LED-STRIP")-1); |
michaeljkoster | 0:9101343a70cd | 114 | |
michaeljkoster | 0:9101343a70cd | 115 | // Dynamic resources |
michaeljkoster | 0:9101343a70cd | 116 | create_light_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 117 | |
michaeljkoster | 0:9101343a70cd | 118 | /* Register with NSP */ |
michaeljkoster | 0:9101343a70cd | 119 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:9101343a70cd | 120 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:9101343a70cd | 121 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 122 | else |
michaeljkoster | 0:9101343a70cd | 123 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:9101343a70cd | 124 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:9101343a70cd | 125 | |
michaeljkoster | 0:9101343a70cd | 126 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:9101343a70cd | 127 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 128 | return 1; |
michaeljkoster | 0:9101343a70cd | 129 | } |
michaeljkoster | 0:9101343a70cd | 130 | |
michaeljkoster | 2:4166c3e5b321 | 131 | void set_led_color(char *color_string) |
michaeljkoster | 2:4166c3e5b321 | 132 | { |
michaeljkoster | 2:4166c3e5b321 | 133 | static uint8_t red, green, blue, index ; |
michaeljkoster | 2:4166c3e5b321 | 134 | int color_int ; |
michaeljkoster | 2:4166c3e5b321 | 135 | |
michaeljkoster | 3:e3aa908cbb51 | 136 | sscanf(color_string, "%X", &color_int); |
michaeljkoster | 2:4166c3e5b321 | 137 | |
michaeljkoster | 2:4166c3e5b321 | 138 | index = color_int & 255; |
michaeljkoster | 2:4166c3e5b321 | 139 | blue = color_int >> 8 & 255; |
michaeljkoster | 2:4166c3e5b321 | 140 | green = color_int >> 16 & 255; |
michaeljkoster | 2:4166c3e5b321 | 141 | red = color_int >> 24 & 255; |
michaeljkoster | 3:e3aa908cbb51 | 142 | |
michaeljkoster | 2:4166c3e5b321 | 143 | if(index > 0 and index <= LED_COUNT) |
michaeljkoster | 2:4166c3e5b321 | 144 | { |
michaeljkoster | 2:4166c3e5b321 | 145 | colors[index-1] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 2:4166c3e5b321 | 146 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 2:4166c3e5b321 | 147 | } |
michaeljkoster | 2:4166c3e5b321 | 148 | else if(index == 0) |
michaeljkoster | 2:4166c3e5b321 | 149 | { |
michaeljkoster | 2:4166c3e5b321 | 150 | for(int i = 0; i < LED_COUNT; i++) |
michaeljkoster | 2:4166c3e5b321 | 151 | colors[i] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 2:4166c3e5b321 | 152 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 2:4166c3e5b321 | 153 | } |
michaeljkoster | 2:4166c3e5b321 | 154 | } |
michaeljkoster | 2:4166c3e5b321 | 155 | |
michaeljkoster | 2:4166c3e5b321 | 156 | |
michaeljkoster | 0:9101343a70cd | 157 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 158 | // Program entry point |
michaeljkoster | 0:9101343a70cd | 159 | |
michaeljkoster | 0:9101343a70cd | 160 | int main() |
michaeljkoster | 0:9101343a70cd | 161 | { |
michaeljkoster | 2:4166c3e5b321 | 162 | |
michaeljkoster | 0:9101343a70cd | 163 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
michaeljkoster | 0:9101343a70cd | 164 | |
michaeljkoster | 0:9101343a70cd | 165 | // Initialize Ethernet interface first |
michaeljkoster | 0:9101343a70cd | 166 | ethernet_init(); |
michaeljkoster | 0:9101343a70cd | 167 | |
michaeljkoster | 0:9101343a70cd | 168 | // Initialize NSP node |
michaeljkoster | 0:9101343a70cd | 169 | nsp_init(); |
michaeljkoster | 0:9101343a70cd | 170 | |
michaeljkoster | 0:9101343a70cd | 171 | // Initialize NSDL stack |
michaeljkoster | 0:9101343a70cd | 172 | nsdl_init(); |
michaeljkoster | 0:9101343a70cd | 173 | |
michaeljkoster | 0:9101343a70cd | 174 | // Create NSDL resources |
michaeljkoster | 0:9101343a70cd | 175 | create_resources(); |
michaeljkoster | 0:9101343a70cd | 176 | |
michaeljkoster | 0:9101343a70cd | 177 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:9101343a70cd | 178 | nsdl_event_loop(); |
michaeljkoster | 0:9101343a70cd | 179 | } |