NanoService device game controller for NSPong.
Dependencies: Beep C12832_lcd EthernetInterface MMA7660 mbed-rtos mbed nsdl_lib
NS_Game_Controller
NS_Game_Controller is a game controller software for NSPong.
NSPong is a HTML5 demo game developed on top of ARM Sensinode’s NanoService Platform. The game uses for example RealTimeMultiplayerNodeJS, Box2D and CAAT libraries, which are specifically built for HTML5 multiplayer games with the client/server model.
NSPong is available at https://github.com/NSPong/NSPong.
Demo video is available at https://vimeo.com/95207889.
Revision 0:e8d7be634e3c, committed 2014-05-13
- Comitter:
- Shage
- Date:
- Tue May 13 17:17:38 2014 +0000
- Commit message:
- Initial version
Changed in this revision
diff -r 000000000000 -r e8d7be634e3c Beep.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Beep.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shimniok/code/Beep/#3eb39e374fc3
diff -r 000000000000 -r e8d7be634e3c C12832_lcd.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832_lcd.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
diff -r 000000000000 -r e8d7be634e3c EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#dd9794ce1d64
diff -r 000000000000 -r e8d7be634e3c MMA7660.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/Sissors/code/MMA7660/#a8e20db7901e
diff -r 000000000000 -r e8d7be634e3c dbg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbg.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,17 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#include "nsdl_support.h" +#include "mbed.h" + +//Debug is disabled by default +#define DEBUG 1 + +#if (DEBUG) +extern Serial pc; +#define NSDL_DEBUG(x, ...) pc.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); +#else +#define NSDL_DEBUG(x, ...) +#endif + +#endif \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,153 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "C12832_lcd.h" +#include "nsdl_support.h" +#include "dbg.h" + +// Include resources +#include "accelerometer.h" +#include "joystick.h" +#include "buzzer.h" +#include "led.h" +#include "lcd.h" + +static C12832_LCD lcd; +Serial pc(USBTX, USBRX); // tx, rx + +// **************************************************************************** +// Configuration section + +// Ethernet configuration +/* Define this to enable DHCP, otherwise manual address configuration is used */ +//#define DHCP + +/* Manual IP configurations, if DHCP not defined */ +#define IP "192.168.1.108" +#define MASK "255.255.255.0" +#define GW "" + +// NSP configuration +/* Change this IP address to that of your NanoService Platform installation */ +static const char* NSP_ADDRESS = "192.168.1.101"; /* demo NSP address */ +static const int NSP_PORT = 5683; +char endpoint_name[16] = "mbed-"; +uint8_t ep_type[] = {"mbed_device"}; +uint8_t lifetime_ptr[] = {"1200"}; + +// **************************************************************************** +// Ethernet initialization + +EthernetInterface eth; + +static void ethernet_init() +{ + char mbed_uid[33]; // for creating unique name for the board + + /* Initialize network */ +#ifdef DHCP + NSDL_DEBUG("DHCP in use\r\n"); + eth.init(); +#else + eth.init(IP, MASK, GW); +#endif + if(eth.connect(30000) == 0) + pc.printf("Connect OK\n\r"); + + mbed_interface_uid(mbed_uid); + mbed_uid[32] = '\0'; + strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); + + lcd.locate(0,11); + lcd.printf("IP:%s", eth.getIPAddress()); + + NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); +} + +// **************************************************************************** +// NSP initialization + +UDPSocket server; +Endpoint nsp; + +static void nsp_init() +{ + server.init(); + server.bind(NSP_PORT); + + nsp.set_address(NSP_ADDRESS, NSP_PORT); + + NSDL_DEBUG("name: %s", endpoint_name); + NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); + + lcd.locate(0,22); + lcd.printf("EP name:%s\n", endpoint_name); +} + +// **************************************************************************** +// Resource creation + +static int create_resources() +{ + sn_nsdl_resource_info_s *resource_ptr = NULL; + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + NSDL_DEBUG("Creating resources"); + + /* Create resources */ + resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); + if(!resource_ptr) + return 0; + memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); + + resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); + if(!resource_ptr->resource_parameters_ptr) + { + nsdl_free(resource_ptr); + return 0; + } + memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); + + // Dynamic resources + create_accelerometer_resource(resource_ptr); + create_joystick_resource(resource_ptr); + create_buzz_resource(resource_ptr); + create_led_resource(resource_ptr); + create_lcd_resource(resource_ptr); + + /* Register with NSP */ + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + pc.printf("NSP registering failed\r\n"); + else + pc.printf("NSP registering OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + + nsdl_free(resource_ptr->resource_parameters_ptr); + nsdl_free(resource_ptr); + return 1; +} + +// **************************************************************************** +// Program entry point + +int main() +{ + lcd.cls(); + lcd.locate(0,0); + lcd.printf("NS Game Controller Demo\n"); + + // Initialize Ethernet interface first + ethernet_init(); + + // Initialize NSP node + nsp_init(); + + // Initialize NSDL stack + nsdl_init(); + + // Create NSDL resources + create_resources(); + + // Run the NSDL event loop (never returns) + nsdl_event_loop(); +}
diff -r 000000000000 -r e8d7be634e3c mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#53e6cccd8782
diff -r 000000000000 -r e8d7be634e3c mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c nsdl_lib.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_lib.lib Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Sensinode/code/nsdl_lib/#050d0f54a0dc
diff -r 000000000000 -r e8d7be634e3c nsdl_support.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,170 @@ +// NSDL library support functions + +#include "mbed.h" +#include "nsdl_support.h" +#include "mbed.h" +#include "rtos.h" +#include "EthernetInterface.h" + +extern Serial pc; +extern EthernetInterface eth; +extern Endpoint nsp; +extern UDPSocket server; +extern char endpoint_name[16]; +extern uint8_t ep_type[]; +extern uint8_t lifetime_ptr[]; + +/* The number of seconds between NSP registration messages */ +#define RD_UPDATE_PERIOD 60 + +void *nsdl_alloc(uint16_t size) +{ + return malloc(size); +} + +void nsdl_free(void* ptr_to_free) +{ + free(ptr_to_free); +} + +/* + * Create a static resoure + * Only get is allowed + */ +void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len) +{ + resource_structure->access = SN_GRS_GET_ALLOWED; + resource_structure->mode = SN_GRS_STATIC; + resource_structure->pathlen = pt_len; + resource_structure->path = pt; + resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; + resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; + resource_structure->resource = rsc; + resource_structure->resourcelen = rsc_len; + sn_nsdl_create_resource(resource_structure); +} + +void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right) +{ + resource_structure->access = (sn_grs_resource_acl_e)access_right; + resource_structure->resource = 0; + resource_structure->resourcelen = 0; + resource_structure->sn_grs_dyn_res_callback = callback_ptr; + resource_structure->mode = SN_GRS_DYNAMIC; + resource_structure->pathlen = pt_len; + resource_structure->path = pt; + resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; + resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; + resource_structure->resource_parameters_ptr->observable = is_observable; + sn_nsdl_create_resource(resource_structure); +} + +sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) +{ + if (NULL == endpoint_structure) + { + endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); + } + if (endpoint_structure) + { + memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); + uint8_t domain_name[5] = "pong"; + endpoint_structure->endpoint_name_ptr = name; + endpoint_structure->endpoint_name_len = strlen((char*)name); + endpoint_structure->domain_name_ptr = domain_name; + endpoint_structure->domain_name_len = strlen((char*)domain_name); + endpoint_structure->type_ptr = typename_ptr; + endpoint_structure->type_len = strlen((char*)typename_ptr); + endpoint_structure->lifetime_ptr = lifetime_ptr; + endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); + } + return endpoint_structure; +} + +void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) +{ + if (*endpoint_structure) + { + nsdl_free(*endpoint_structure); + *endpoint_structure = NULL; + } +} + +static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) +{ + //pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); + + if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) + pc.printf("sending failed\n\r"); + + return 1; +} + +static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) +{ + pc.printf("RX callback!\r\n"); + return 0; +} + +static void registration_update_thread(void const *args) +{ + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + while(true) + { + wait(RD_UPDATE_PERIOD); + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + pc.printf("NSP re-registering failed\r\n"); + else + pc.printf("NSP re-registering OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + } +} + +void nsdl_init() +{ + uint8_t nsp_addr[4]; + sn_nsdl_mem_s memory_cbs; + + /* Initialize libNsdl */ + memory_cbs.sn_nsdl_alloc = &nsdl_alloc; + memory_cbs.sn_nsdl_free = &nsdl_free; + if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) + pc.printf("libNsdl init failed\r\n"); + else + pc.printf("libNsdl init done\r\n"); + + /* Set nsp address for library */ + set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); +} + +void nsdl_event_loop() +{ + // Re-registration thread disabled + //Thread registration_thread(registration_update_thread); + + sn_nsdl_addr_s received_packet_address; + uint8_t received_address[4]; + char buffer[1024]; + Endpoint from; + + memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); + received_packet_address.addr_ptr = received_address; + + while(1) + { + int n = server.receiveFrom(from, buffer, sizeof(buffer)); + if (n < 0) + { + pc.printf("Socket error\n\r"); + } + else + { + pc.printf("Received %d bytes\r\n", n); + sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); + } + } +} + + \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c nsdl_support.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nsdl_support.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,24 @@ +// Support functions for the NSDL library + +#ifndef NSDL_SUPPORT_H +#define NSDL_SUPPORT_H + +#include "mbed.h" +#include <stdint.h> +#include "sn_nsdl.h" +#include "sn_coap_header.h" +#include "sn_coap_protocol.h" +#include "sn_nsdl_lib.h" + +typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); + +extern "C" void *nsdl_alloc(uint16_t size); +extern "C" void nsdl_free(void* ptr_to_free); +void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len); +void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right); +sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr); +void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure); +void nsdl_init(); +void nsdl_event_loop(); + +#endif // NSDL_SUPPORT_H
diff -r 000000000000 -r e8d7be634e3c resources/accelerometer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/accelerometer.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,122 @@ +// Accelerometer resource implementation + +#include "mbed.h" +#include "rtos.h" +#include "nsdl_support.h" +#include "accelerometer.h" +#include "MMA7660.h" +#define ACC_RES_ID "acc" +#define X_AXIS "x" +#define Y_AXIS "y" +#define Z_AXIS "z" + +static MMA7660 MMA(p28, p27); +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +//static char acc_val[8]; +static char acc_val[20]; +// Data from PUT request +static char received_cmd[20]; +static char selected_axis[20]; +extern Serial pc; + +/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ +static void exec_call_thread(void const *args) +{ + int32_t time = 0; + strcpy(selected_axis, "xy"); + MMA.setSampleRate(120); + while (true) + { + time++; + sn_nsdl_exec(time); + if(obs_token_ptr != NULL) + { + obs_number++; + if (!strcmp(selected_axis, X_AXIS Y_AXIS Z_AXIS)) { + sprintf(acc_val,"%2.2f;%2.2f;%2.2f", MMA.x(), MMA.y(), MMA.z()); + } + else if (!strcmp(selected_axis, X_AXIS Y_AXIS)) { + sprintf(acc_val,"%2.2f;%2.2f", MMA.x(), MMA.y()); + } + else if (!strcmp(selected_axis, X_AXIS Z_AXIS)) { + sprintf(acc_val,"%2.2f;%2.2f", MMA.x(), MMA.z()); + } + else if (!strcmp(selected_axis, Y_AXIS Z_AXIS)) { + sprintf(acc_val,"%2.2f;%2.2f", MMA.y(), MMA.z()); + } + else if (!strcmp(selected_axis, X_AXIS)) { + sprintf(acc_val,"%2.2f", MMA.x()); + } + else if (!strcmp(selected_axis, Y_AXIS)) { + sprintf(acc_val,"%2.2f", MMA.y()); + } + else { + sprintf(acc_val,"%2.2f", MMA.z()); + } + if (sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)acc_val, strlen(acc_val), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) { + pc.printf("Accelerometer data sending failed!\r\n"); + } + } + } +} + +// GET and PUT allowed +static uint8_t acc_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + + // GET, fetches accelerometer value for selected axis + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = 20; + coap_res_ptr->payload_ptr = (uint8_t*)acc_val; + + if(received_coap_ptr->token_ptr) + { + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + } + // PUT, sets the selected axis from request (msg body 'xyz', 'xy', 'xz', 'yz', 'x', 'y' or 'z') + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) { + memcpy(received_cmd, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + received_cmd[received_coap_ptr->payload_len] = '\0'; + sprintf(selected_axis, "%s", received_cmd); + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_accelerometer_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + static Thread exec_thread(exec_call_thread); + nsdl_create_dynamic_resource(resource_ptr, sizeof(ACC_RES_ID)-1, (uint8_t*)ACC_RES_ID, 0, 0, 1, &acc_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED)); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c resources/accelerometer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/accelerometer.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,10 @@ +// Accelerometer resource implementation + +#ifndef ACCELEROMETER_H +#define ACCELEROMETER_H + +#include "nsdl_support.h" + +int create_accelerometer_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r e8d7be634e3c resources/buzzer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/buzzer.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,58 @@ +// Buzzer resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "buzzer.h" +#include "Beep.h" + +#define BUZZ_RES_ID "buzz" + +extern Serial pc; +static Beep buzzer(p26); + +// Data from PUT request +static char received_cmd[20]; +static char cmd[10]; + +/* Only PUT method allowed */ +static uint8_t buzz_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + memcpy(received_cmd, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + received_cmd[received_coap_ptr->payload_len] = '\0'; + sprintf(cmd, "%s", received_cmd); + + if (!strcmp(cmd, "beep")) { + buzzer.beep(300,0); + wait(0.05); + buzzer.nobeep(); + } + else if (!strcmp(cmd, "score")) { + buzzer.beep(400,0); + wait(0.2); + buzzer.nobeep(); + buzzer.beep(600,0); + wait(0.3); + buzzer.nobeep(); + } + else if (!strcmp(cmd, "win")) { + buzzer.beep(700,0); + wait(0.2); + buzzer.nobeep(); + buzzer.beep(700,0); + wait(0.3); + buzzer.nobeep(); + } + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + memset(cmd, 0, 10); + return 0; +} + +int create_buzz_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(BUZZ_RES_ID)-1, (uint8_t*)BUZZ_RES_ID, 0, 0, 0, &buzz_resource_cb, SN_GRS_PUT_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c resources/buzzer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/buzzer.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,10 @@ +// Buzzer resource implementation + +#ifndef BUZZER_H +#define BUZZER_H + +#include "nsdl_support.h" + +int create_buzz_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r e8d7be634e3c resources/joystick.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/joystick.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,126 @@ +// Joystick resource implementation + +#include "mbed.h" +#include "rtos.h" +#include "nsdl_support.h" +#include "joystick.h" +#define JOY_RES_ID "joy" + +BusIn joy(p15,p12,p13,p16); +DigitalIn fire(p14); +static uint8_t old_joy_val = 0; +static uint8_t joy_val = 0; +static int old_fire_val = 0; +static int fire_val = 0; +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +static char joy_msg[10]; +extern Serial pc; + +/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ +/* Node detects joystick state changes and then sends data. Notification sending is done here. */ +static void exec_call_thread(void const *args) +{ + int32_t time = 0; + while (true) + { + // Only if joystick state has changed + joy_val = joy.read(); + fire_val = fire.read(); + if (fire_val != old_fire_val) { + time++; + sn_nsdl_exec(time); + if(obs_token_ptr != NULL) + { + obs_number++; + if (fire_val == 1) { + sprintf(joy_msg, "fire on"); + } + else if (fire_val == 0) { + sprintf(joy_msg, "fire off"); + } + if (sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)joy_msg, strlen(joy_msg), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) { + pc.printf("Joystick data sending failed!\r\n"); + } + } + old_fire_val = fire_val; + } + if (joy_val != old_joy_val) { + time++; + sn_nsdl_exec(time); + if(obs_token_ptr != NULL) + { + obs_number++; + if (joy_val == 0x08) { + sprintf(joy_msg, "down"); + } + else if (joy_val == 0x04) { + sprintf(joy_msg, "up"); + } + else if (joy_val == 0x02) { + sprintf(joy_msg, "left"); + } + else if (joy_val == 0x01) { + sprintf(joy_msg, "right"); + } + else if (joy_val == 0x00) { + sprintf(joy_msg, "release"); + } + if (sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)joy_msg, strlen(joy_msg), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) { + pc.printf("Joystick data sending failed!\r\n"); + } + } + old_joy_val = joy_val; + } + } +} + +// Only GET allowed +static uint8_t joy_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + old_joy_val = joy.read(); + old_fire_val = fire.read(); + sn_coap_hdr_s *coap_res_ptr = 0; + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = 10; + coap_res_ptr->payload_ptr = (uint8_t*)joy_msg; + + if(received_coap_ptr->token_ptr) + { + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_joystick_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + static Thread exec_thread(exec_call_thread); + nsdl_create_dynamic_resource(resource_ptr, sizeof(JOY_RES_ID)-1, (uint8_t*)JOY_RES_ID, 0, 0, 1, &joy_resource_cb, SN_GRS_GET_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c resources/joystick.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/joystick.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,10 @@ +// Joystick resource implementation + +#ifndef JOYSTICK_H +#define JOYSTICK_H + +#include "nsdl_support.h" + +int create_joystick_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r e8d7be634e3c resources/lcd.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/lcd.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,48 @@ +// Lcd resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "C12832_lcd.h" +#include "lcd.h" + +#define LCD_RES_ID "lcd" + +extern Serial pc; +static C12832_LCD lcd; +// Data from PUT request +static char received_cmd[20]; +static char player_in_selection[5]; + +/* Only PUT method allowed */ +static uint8_t lcd_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + + memcpy(received_cmd, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + received_cmd[received_coap_ptr->payload_len] = '\0'; + sprintf(player_in_selection, "%s", received_cmd); + + lcd.cls(); + lcd.locate(0,0); + + if (!strcmp(player_in_selection, "1")) { + lcd.printf("You are player 1\nYour paddle is on the left\nGood luck and have fun!"); + } + else if (!strcmp(player_in_selection, "2")) { + lcd.printf("You are player 2\nYour paddle is on the right\nGood luck and have fun!"); + } + else if(!strcmp(player_in_selection, "info")) { + lcd.printf("You have been registered\nPress joystick to start!"); + } + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_lcd_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(LCD_RES_ID)-1, (uint8_t*)LCD_RES_ID, 0, 0, 0, &lcd_resource_cb, SN_GRS_PUT_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c resources/lcd.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/lcd.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,10 @@ +// Lcd resource implementation + +#ifndef LCD_H +#define LCD_H + +#include "nsdl_support.h" + +int create_lcd_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
diff -r 000000000000 -r e8d7be634e3c resources/led.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/led.cpp Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,122 @@ +// Led resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "led.h" + +#define LED_RES_ID "led" + +extern Serial pc; +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +PwmOut rled(p23); +PwmOut gled(p24); +PwmOut bled(p25); + +// Data from PUT request +static char received_cmd[20]; +static char cmd[10]; + +/* Only PUT method allowed */ +static uint8_t led_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + + memcpy(received_cmd, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + received_cmd[received_coap_ptr->payload_len] = '\0'; + sprintf(cmd, "%s", received_cmd); + + if (!strcmp(cmd, "reset")) { + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + } + + else if (!strcmp(cmd, "score")) { + + if(led4){ + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + } + else if(led3){ + led4 = 1; + } + else if(led2){ + led3 = 1; + } + else if(led1){ + led2 = 1; + } + else{ + led1 = 1; + } + + rled.period(0.001); + for(float i = 0.0; i < 1.0 ; i += 0.01) { + float p = 3 * i; + rled = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0); + gled = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p); + bled = 1.0 - ((p < 1.0) ? 0.0 : (p > 2.0) ? 3.0 - p : p - 1.0); ; + wait(0.005); + } + } + + else if (!strcmp(cmd, "paddle")) { + rled = 0; + gled = 0; + bled = 0; + wait(0.1); + } + + else if (!strcmp(cmd, "win")) { + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + wait(0.25); + led1 = 1; + led2 = 1; + led3 = 1; + led4 = 1; + wait(0.25); + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + wait(0.25); + led1 = 1; + led2 = 1; + led3 = 1; + led4 = 1; + wait(0.25); + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + } + + rled = 1; + gled = 1; + bled = 1; + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + memset(cmd, 0, 10); + return 0; +} + +int create_led_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + rled = 1; + gled = 1; + bled = 1; + nsdl_create_dynamic_resource(resource_ptr, sizeof(LED_RES_ID)-1, (uint8_t*)LED_RES_ID, 0, 0, 0, &led_resource_cb, SN_GRS_PUT_ALLOWED); + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e8d7be634e3c resources/led.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/led.h Tue May 13 17:17:38 2014 +0000 @@ -0,0 +1,10 @@ +// Led resource implementation + +#ifndef LED_H +#define LED_H + +#include "nsdl_support.h" + +int create_led_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif