Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Beep C12832_lcd EthernetInterface MMA7660 mbed-rtos mbed nsdl_lib
joystick.cpp
00001 // Joystick resource implementation 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "nsdl_support.h" 00006 #include "joystick.h" 00007 #define JOY_RES_ID "joy" 00008 00009 BusIn joy(p15,p12,p13,p16); 00010 DigitalIn fire(p14); 00011 static uint8_t old_joy_val = 0; 00012 static uint8_t joy_val = 0; 00013 static int old_fire_val = 0; 00014 static int fire_val = 0; 00015 static uint8_t obs_number = 0; 00016 static uint8_t *obs_token_ptr = NULL; 00017 static uint8_t obs_token_len = 0; 00018 static char joy_msg[10]; 00019 extern Serial pc; 00020 00021 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ 00022 /* Node detects joystick state changes and then sends data. Notification sending is done here. */ 00023 static void exec_call_thread(void const *args) 00024 { 00025 int32_t time = 0; 00026 while (true) 00027 { 00028 // Only if joystick state has changed 00029 joy_val = joy.read(); 00030 fire_val = fire.read(); 00031 if (fire_val != old_fire_val) { 00032 time++; 00033 sn_nsdl_exec(time); 00034 if(obs_token_ptr != NULL) 00035 { 00036 obs_number++; 00037 if (fire_val == 1) { 00038 sprintf(joy_msg, "fire on"); 00039 } 00040 else if (fire_val == 0) { 00041 sprintf(joy_msg, "fire off"); 00042 } 00043 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) { 00044 pc.printf("Joystick data sending failed!\r\n"); 00045 } 00046 } 00047 old_fire_val = fire_val; 00048 } 00049 if (joy_val != old_joy_val) { 00050 time++; 00051 sn_nsdl_exec(time); 00052 if(obs_token_ptr != NULL) 00053 { 00054 obs_number++; 00055 if (joy_val == 0x08) { 00056 sprintf(joy_msg, "down"); 00057 } 00058 else if (joy_val == 0x04) { 00059 sprintf(joy_msg, "up"); 00060 } 00061 else if (joy_val == 0x02) { 00062 sprintf(joy_msg, "left"); 00063 } 00064 else if (joy_val == 0x01) { 00065 sprintf(joy_msg, "right"); 00066 } 00067 else if (joy_val == 0x00) { 00068 sprintf(joy_msg, "release"); 00069 } 00070 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) { 00071 pc.printf("Joystick data sending failed!\r\n"); 00072 } 00073 } 00074 old_joy_val = joy_val; 00075 } 00076 } 00077 } 00078 00079 // Only GET allowed 00080 static uint8_t joy_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00081 { 00082 old_joy_val = joy.read(); 00083 old_fire_val = fire.read(); 00084 sn_coap_hdr_s *coap_res_ptr = 0; 00085 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00086 00087 coap_res_ptr->payload_len = 10; 00088 coap_res_ptr->payload_ptr = (uint8_t*)joy_msg; 00089 00090 if(received_coap_ptr->token_ptr) 00091 { 00092 if(obs_token_ptr) 00093 { 00094 free(obs_token_ptr); 00095 obs_token_ptr = 0; 00096 } 00097 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); 00098 if(obs_token_ptr) 00099 { 00100 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); 00101 obs_token_len = received_coap_ptr->token_len; 00102 } 00103 } 00104 00105 if(received_coap_ptr->options_list_ptr->observe) 00106 { 00107 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00108 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00109 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; 00110 coap_res_ptr->options_list_ptr->observe_len = 1; 00111 obs_number++; 00112 } 00113 00114 sn_nsdl_send_coap_message(address, coap_res_ptr); 00115 00116 coap_res_ptr->options_list_ptr->observe_ptr = 0; 00117 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00118 return 0; 00119 } 00120 00121 int create_joystick_resource(sn_nsdl_resource_info_s *resource_ptr) 00122 { 00123 static Thread exec_thread(exec_call_thread); 00124 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); 00125 return 0; 00126 }
Generated on Wed Jul 13 2022 01:21:07 by
1.7.2