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
accelerometer.cpp
00001 // Accelerometer resource implementation 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "nsdl_support.h" 00006 #include "accelerometer.h" 00007 #include "MMA7660.h" 00008 #define ACC_RES_ID "acc" 00009 #define X_AXIS "x" 00010 #define Y_AXIS "y" 00011 #define Z_AXIS "z" 00012 00013 static MMA7660 MMA(p28, p27); 00014 static uint8_t obs_number = 0; 00015 static uint8_t *obs_token_ptr = NULL; 00016 static uint8_t obs_token_len = 0; 00017 //static char acc_val[8]; 00018 static char acc_val[20]; 00019 // Data from PUT request 00020 static char received_cmd[20]; 00021 static char selected_axis[20]; 00022 extern Serial pc; 00023 00024 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ 00025 static void exec_call_thread(void const *args) 00026 { 00027 int32_t time = 0; 00028 strcpy(selected_axis, "xy"); 00029 MMA.setSampleRate(120); 00030 while (true) 00031 { 00032 time++; 00033 sn_nsdl_exec(time); 00034 if(obs_token_ptr != NULL) 00035 { 00036 obs_number++; 00037 if (!strcmp(selected_axis, X_AXIS Y_AXIS Z_AXIS)) { 00038 sprintf(acc_val,"%2.2f;%2.2f;%2.2f", MMA.x(), MMA.y(), MMA.z()); 00039 } 00040 else if (!strcmp(selected_axis, X_AXIS Y_AXIS)) { 00041 sprintf(acc_val,"%2.2f;%2.2f", MMA.x(), MMA.y()); 00042 } 00043 else if (!strcmp(selected_axis, X_AXIS Z_AXIS)) { 00044 sprintf(acc_val,"%2.2f;%2.2f", MMA.x(), MMA.z()); 00045 } 00046 else if (!strcmp(selected_axis, Y_AXIS Z_AXIS)) { 00047 sprintf(acc_val,"%2.2f;%2.2f", MMA.y(), MMA.z()); 00048 } 00049 else if (!strcmp(selected_axis, X_AXIS)) { 00050 sprintf(acc_val,"%2.2f", MMA.x()); 00051 } 00052 else if (!strcmp(selected_axis, Y_AXIS)) { 00053 sprintf(acc_val,"%2.2f", MMA.y()); 00054 } 00055 else { 00056 sprintf(acc_val,"%2.2f", MMA.z()); 00057 } 00058 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) { 00059 pc.printf("Accelerometer data sending failed!\r\n"); 00060 } 00061 } 00062 } 00063 } 00064 00065 // GET and PUT allowed 00066 static uint8_t acc_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00067 { 00068 sn_coap_hdr_s *coap_res_ptr = 0; 00069 00070 // GET, fetches accelerometer value for selected axis 00071 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { 00072 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00073 00074 coap_res_ptr->payload_len = 20; 00075 coap_res_ptr->payload_ptr = (uint8_t*)acc_val; 00076 00077 if(received_coap_ptr->token_ptr) 00078 { 00079 if(obs_token_ptr) 00080 { 00081 free(obs_token_ptr); 00082 obs_token_ptr = 0; 00083 } 00084 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); 00085 if(obs_token_ptr) 00086 { 00087 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); 00088 obs_token_len = received_coap_ptr->token_len; 00089 } 00090 } 00091 00092 if(received_coap_ptr->options_list_ptr->observe) 00093 { 00094 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00095 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00096 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; 00097 coap_res_ptr->options_list_ptr->observe_len = 1; 00098 obs_number++; 00099 } 00100 00101 sn_nsdl_send_coap_message(address, coap_res_ptr); 00102 00103 coap_res_ptr->options_list_ptr->observe_ptr = 0; 00104 } 00105 // PUT, sets the selected axis from request (msg body 'xyz', 'xy', 'xz', 'yz', 'x', 'y' or 'z') 00106 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) { 00107 memcpy(received_cmd, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); 00108 received_cmd[received_coap_ptr->payload_len] = '\0'; 00109 sprintf(selected_axis, "%s", received_cmd); 00110 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); 00111 sn_nsdl_send_coap_message(address, coap_res_ptr); 00112 } 00113 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00114 return 0; 00115 } 00116 00117 int create_accelerometer_resource(sn_nsdl_resource_info_s *resource_ptr) 00118 { 00119 static Thread exec_thread(exec_call_thread); 00120 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)); 00121 return 0; 00122 }
Generated on Wed Jul 13 2022 01:21:07 by
1.7.2