![](/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@14:25430e88728f, 2014-10-04 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Oct 04 05:12:45 2014 +0000
- Revision:
- 14:25430e88728f
- Parent:
- 11:3e5ac016a106
- Child:
- 15:2f2b3eaa51a6
Undo accidental fork and update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:9101343a70cd | 1 | #include "mbed.h" |
michaeljkoster | 14:25430e88728f | 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 | 14:25430e88728f | 7 | #include "IAP.h" |
michaeljkoster | 2:4166c3e5b321 | 8 | #include "PololuLedStrip.h" |
michaeljkoster | 14:25430e88728f | 9 | PololuLedStrip ledStrip(P0_4); |
michaeljkoster | 9:a580c20d6bb5 | 10 | //#define LED_COUNT 60 |
michaeljkoster | 9:a580c20d6bb5 | 11 | #define LED_COUNT 120 |
michaeljkoster | 2:4166c3e5b321 | 12 | rgb_color colors[LED_COUNT]; |
michaeljkoster | 2:4166c3e5b321 | 13 | |
michaeljkoster | 0:9101343a70cd | 14 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:9101343a70cd | 15 | |
michaeljkoster | 0:9101343a70cd | 16 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 17 | // Configuration section |
michaeljkoster | 0:9101343a70cd | 18 | |
michaeljkoster | 0:9101343a70cd | 19 | // Ethernet configuration |
michaeljkoster | 0:9101343a70cd | 20 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:9101343a70cd | 21 | #define DHCP |
michaeljkoster | 0:9101343a70cd | 22 | |
michaeljkoster | 0:9101343a70cd | 23 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 1:09a525977925 | 24 | #define IP "10.0.0.199" |
michaeljkoster | 0:9101343a70cd | 25 | #define MASK "255.255.255.0" |
michaeljkoster | 1:09a525977925 | 26 | #define GW "10.0.0.1" |
michaeljkoster | 0:9101343a70cd | 27 | |
michaeljkoster | 14:25430e88728f | 28 | extern "C" void mbed_mac_address(char *mac) |
michaeljkoster | 14:25430e88728f | 29 | { |
michaeljkoster | 14:25430e88728f | 30 | static char buf[64] = {0}; |
michaeljkoster | 14:25430e88728f | 31 | IAP iap; |
michaeljkoster | 14:25430e88728f | 32 | int32_t *block = iap.read_serial(); |
michaeljkoster | 14:25430e88728f | 33 | uint32_t serial_number[5] = {0}; |
michaeljkoster | 14:25430e88728f | 34 | |
michaeljkoster | 14:25430e88728f | 35 | memset(buf, 0, sizeof(buf)); |
michaeljkoster | 14:25430e88728f | 36 | serial_number[0] = *(block); |
michaeljkoster | 14:25430e88728f | 37 | serial_number[1] = *(block+1); |
michaeljkoster | 14:25430e88728f | 38 | // we only want bottom 16 bits of word1 (MAC bits 32-47) |
michaeljkoster | 14:25430e88728f | 39 | // and bit 9 forced to 1, bit 8 forced to 0 |
michaeljkoster | 14:25430e88728f | 40 | // Locally administered MAC, reduced conflicts |
michaeljkoster | 14:25430e88728f | 41 | // http://en.wikipedia.org/wiki/MAC_address |
michaeljkoster | 14:25430e88728f | 42 | //serial_number[0] |= 0x00000200; |
michaeljkoster | 14:25430e88728f | 43 | //serial_number[0] &= 0x0000FEFF; |
michaeljkoster | 14:25430e88728f | 44 | memcpy(mac, (uint8_t*) &serial_number[0], 6); |
michaeljkoster | 14:25430e88728f | 45 | mac[0] |= 0x02; |
michaeljkoster | 14:25430e88728f | 46 | mac[0] &= 0xFE; |
michaeljkoster | 14:25430e88728f | 47 | mac[5] |= 0x02; |
michaeljkoster | 14:25430e88728f | 48 | mac[5] &= 0xFE; |
michaeljkoster | 14:25430e88728f | 49 | |
michaeljkoster | 14:25430e88728f | 50 | // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); |
michaeljkoster | 14:25430e88728f | 51 | } |
michaeljkoster | 10:5ecaf86d3936 | 52 | |
michaeljkoster | 10:5ecaf86d3936 | 53 | |
michaeljkoster | 0:9101343a70cd | 54 | // NSP configuration |
michaeljkoster | 0:9101343a70cd | 55 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:9101343a70cd | 56 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 14:25430e88728f | 57 | static const char* NSP_ADDRESS = "23.99.99.252"; // barista.cloudapp.net |
michaeljkoster | 1:09a525977925 | 58 | //static const char* NSP_ADDRESS = "192.168.1.200"; |
michaeljkoster | 0:9101343a70cd | 59 | static const int NSP_PORT = 5683; |
michaeljkoster | 4:71b211dbe14c | 60 | char endpoint_name[24] = "LED-booth-"; |
michaeljkoster | 7:59c7b96ace5b | 61 | uint8_t ep_type[] = {"LED-STRIP"}; |
michaeljkoster | 1:09a525977925 | 62 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 0:9101343a70cd | 63 | |
michaeljkoster | 0:9101343a70cd | 64 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 65 | // Ethernet initialization |
michaeljkoster | 0:9101343a70cd | 66 | |
michaeljkoster | 14:25430e88728f | 67 | EthernetInterface eth; |
michaeljkoster | 0:9101343a70cd | 68 | static void ethernet_init() |
michaeljkoster | 0:9101343a70cd | 69 | { |
michaeljkoster | 0:9101343a70cd | 70 | /* Initialize network */ |
michaeljkoster | 0:9101343a70cd | 71 | #ifdef DHCP |
michaeljkoster | 0:9101343a70cd | 72 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:9101343a70cd | 73 | eth.init(); |
michaeljkoster | 1:09a525977925 | 74 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 75 | #else |
michaeljkoster | 0:9101343a70cd | 76 | eth.init(IP, MASK, GW); |
michaeljkoster | 1:09a525977925 | 77 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 78 | #endif |
michaeljkoster | 14:25430e88728f | 79 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:9101343a70cd | 80 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:9101343a70cd | 81 | |
michaeljkoster | 0:9101343a70cd | 82 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 83 | } |
michaeljkoster | 0:9101343a70cd | 84 | |
michaeljkoster | 0:9101343a70cd | 85 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 86 | // NSP initialization |
michaeljkoster | 0:9101343a70cd | 87 | |
michaeljkoster | 0:9101343a70cd | 88 | UDPSocket server; |
michaeljkoster | 0:9101343a70cd | 89 | Endpoint nsp; |
michaeljkoster | 7:59c7b96ace5b | 90 | |
michaeljkoster | 5:fcfba34d1aeb | 91 | char * ipstring; |
michaeljkoster | 5:fcfba34d1aeb | 92 | char * uid; |
michaeljkoster | 0:9101343a70cd | 93 | |
michaeljkoster | 0:9101343a70cd | 94 | static void nsp_init() |
michaeljkoster | 0:9101343a70cd | 95 | { |
michaeljkoster | 0:9101343a70cd | 96 | server.init(); |
michaeljkoster | 0:9101343a70cd | 97 | server.bind(NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 98 | |
michaeljkoster | 0:9101343a70cd | 99 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 100 | |
michaeljkoster | 5:fcfba34d1aeb | 101 | ipstring = eth.getIPAddress(); |
michaeljkoster | 5:fcfba34d1aeb | 102 | uid = ipstring; |
michaeljkoster | 5:fcfba34d1aeb | 103 | |
michaeljkoster | 5:fcfba34d1aeb | 104 | while(*ipstring) |
michaeljkoster | 5:fcfba34d1aeb | 105 | { |
michaeljkoster | 5:fcfba34d1aeb | 106 | if(*ipstring =='.') |
michaeljkoster | 5:fcfba34d1aeb | 107 | *ipstring='-'; |
michaeljkoster | 5:fcfba34d1aeb | 108 | ipstring++; |
michaeljkoster | 5:fcfba34d1aeb | 109 | |
michaeljkoster | 5:fcfba34d1aeb | 110 | } |
michaeljkoster | 5:fcfba34d1aeb | 111 | strncat( endpoint_name, uid, strlen(uid) ); |
michaeljkoster | 5:fcfba34d1aeb | 112 | pc.printf("\r\nep name: %s\r\n", endpoint_name); |
michaeljkoster | 4:71b211dbe14c | 113 | |
michaeljkoster | 0:9101343a70cd | 114 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 115 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 116 | |
michaeljkoster | 0:9101343a70cd | 117 | } |
michaeljkoster | 0:9101343a70cd | 118 | |
michaeljkoster | 0:9101343a70cd | 119 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 120 | // Resource creation |
michaeljkoster | 0:9101343a70cd | 121 | |
michaeljkoster | 0:9101343a70cd | 122 | static int create_resources() |
michaeljkoster | 0:9101343a70cd | 123 | { |
michaeljkoster | 0:9101343a70cd | 124 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 125 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 126 | |
michaeljkoster | 0:9101343a70cd | 127 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:9101343a70cd | 128 | |
michaeljkoster | 0:9101343a70cd | 129 | /* Create resources */ |
michaeljkoster | 0:9101343a70cd | 130 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 131 | if(!resource_ptr) |
michaeljkoster | 0:9101343a70cd | 132 | return 0; |
michaeljkoster | 0:9101343a70cd | 133 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 134 | |
michaeljkoster | 0:9101343a70cd | 135 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 136 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:9101343a70cd | 137 | { |
michaeljkoster | 0:9101343a70cd | 138 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 139 | return 0; |
michaeljkoster | 0:9101343a70cd | 140 | } |
michaeljkoster | 0:9101343a70cd | 141 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 142 | |
michaeljkoster | 0:9101343a70cd | 143 | // Static resources |
michaeljkoster | 2:4166c3e5b321 | 144 | 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 | 145 | 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 | 146 | |
michaeljkoster | 0:9101343a70cd | 147 | // Dynamic resources |
michaeljkoster | 0:9101343a70cd | 148 | create_light_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 149 | |
michaeljkoster | 0:9101343a70cd | 150 | /* Register with NSP */ |
michaeljkoster | 0:9101343a70cd | 151 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:9101343a70cd | 152 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:9101343a70cd | 153 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 154 | else |
michaeljkoster | 0:9101343a70cd | 155 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:9101343a70cd | 156 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:9101343a70cd | 157 | |
michaeljkoster | 0:9101343a70cd | 158 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:9101343a70cd | 159 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 160 | return 1; |
michaeljkoster | 0:9101343a70cd | 161 | } |
michaeljkoster | 0:9101343a70cd | 162 | |
michaeljkoster | 2:4166c3e5b321 | 163 | void set_led_color(char *color_string) |
michaeljkoster | 2:4166c3e5b321 | 164 | { |
michaeljkoster | 2:4166c3e5b321 | 165 | static uint8_t red, green, blue, index ; |
michaeljkoster | 2:4166c3e5b321 | 166 | int color_int ; |
michaeljkoster | 2:4166c3e5b321 | 167 | |
michaeljkoster | 3:e3aa908cbb51 | 168 | sscanf(color_string, "%X", &color_int); |
michaeljkoster | 2:4166c3e5b321 | 169 | |
michaeljkoster | 2:4166c3e5b321 | 170 | index = color_int & 255; |
michaeljkoster | 2:4166c3e5b321 | 171 | blue = color_int >> 8 & 255; |
michaeljkoster | 2:4166c3e5b321 | 172 | green = color_int >> 16 & 255; |
michaeljkoster | 2:4166c3e5b321 | 173 | red = color_int >> 24 & 255; |
michaeljkoster | 3:e3aa908cbb51 | 174 | |
michaeljkoster | 2:4166c3e5b321 | 175 | if(index > 0 and index <= LED_COUNT) |
michaeljkoster | 2:4166c3e5b321 | 176 | { |
michaeljkoster | 2:4166c3e5b321 | 177 | colors[index-1] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 2:4166c3e5b321 | 178 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 2:4166c3e5b321 | 179 | } |
michaeljkoster | 2:4166c3e5b321 | 180 | else if(index == 0) |
michaeljkoster | 2:4166c3e5b321 | 181 | { |
michaeljkoster | 2:4166c3e5b321 | 182 | for(int i = 0; i < LED_COUNT; i++) |
michaeljkoster | 9:a580c20d6bb5 | 183 | { |
michaeljkoster | 2:4166c3e5b321 | 184 | colors[i] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 9:a580c20d6bb5 | 185 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 9:a580c20d6bb5 | 186 | wait_ms(10); |
michaeljkoster | 9:a580c20d6bb5 | 187 | } |
michaeljkoster | 2:4166c3e5b321 | 188 | } |
michaeljkoster | 2:4166c3e5b321 | 189 | } |
michaeljkoster | 2:4166c3e5b321 | 190 | |
michaeljkoster | 2:4166c3e5b321 | 191 | |
michaeljkoster | 0:9101343a70cd | 192 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 193 | // Program entry point |
michaeljkoster | 0:9101343a70cd | 194 | |
michaeljkoster | 0:9101343a70cd | 195 | int main() |
michaeljkoster | 0:9101343a70cd | 196 | { |
michaeljkoster | 10:5ecaf86d3936 | 197 | char foo[6] = {0}; |
michaeljkoster | 10:5ecaf86d3936 | 198 | |
michaeljkoster | 10:5ecaf86d3936 | 199 | mbed_mac_address(foo); |
michaeljkoster | 10:5ecaf86d3936 | 200 | |
michaeljkoster | 10:5ecaf86d3936 | 201 | pc.printf("mac:\r\n"); |
michaeljkoster | 10:5ecaf86d3936 | 202 | for (int i = 0; i < 6; i++) { |
michaeljkoster | 10:5ecaf86d3936 | 203 | pc.printf("%x, ", foo[i]); |
michaeljkoster | 10:5ecaf86d3936 | 204 | } |
michaeljkoster | 2:4166c3e5b321 | 205 | |
michaeljkoster | 0:9101343a70cd | 206 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
michaeljkoster | 0:9101343a70cd | 207 | |
michaeljkoster | 0:9101343a70cd | 208 | // Initialize Ethernet interface first |
michaeljkoster | 0:9101343a70cd | 209 | ethernet_init(); |
michaeljkoster | 0:9101343a70cd | 210 | |
michaeljkoster | 0:9101343a70cd | 211 | // Initialize NSP node |
michaeljkoster | 0:9101343a70cd | 212 | nsp_init(); |
michaeljkoster | 0:9101343a70cd | 213 | |
michaeljkoster | 0:9101343a70cd | 214 | // Initialize NSDL stack |
michaeljkoster | 0:9101343a70cd | 215 | nsdl_init(); |
michaeljkoster | 0:9101343a70cd | 216 | |
michaeljkoster | 0:9101343a70cd | 217 | // Create NSDL resources |
michaeljkoster | 0:9101343a70cd | 218 | create_resources(); |
michaeljkoster | 0:9101343a70cd | 219 | |
michaeljkoster | 0:9101343a70cd | 220 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:9101343a70cd | 221 | nsdl_event_loop(); |
michaeljkoster | 0:9101343a70cd | 222 | } |