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: EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer
Fork of mbed_nsdl by
resources/nespresso.cpp@6:8729a0db0e25, 2014-07-18 (annotated)
- Committer:
- bjblazkowicz
- Date:
- Fri Jul 18 10:47:23 2014 +0000
- Revision:
- 6:8729a0db0e25
- Parent:
- 4:ab3c8d25260e
Added buffered_rgb_resource. Resource is now array of up to 300 samples.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| GeofferyOmlette | 2:88a30cc88a86 | 1 | #include "mbed.h" |
| GeofferyOmlette | 2:88a30cc88a86 | 2 | #include "rtos.h" |
| GeofferyOmlette | 2:88a30cc88a86 | 3 | #include "nsdl_support.h" |
| GeofferyOmlette | 2:88a30cc88a86 | 4 | #include "nespresso.h" |
| bjblazkowicz | 4:ab3c8d25260e | 5 | #include "detection.h" |
| GeofferyOmlette | 2:88a30cc88a86 | 6 | |
| bjblazkowicz | 4:ab3c8d25260e | 7 | #define NESPRESSO_RES_ID "sensor/capsule" |
| GeofferyOmlette | 2:88a30cc88a86 | 8 | |
| GeofferyOmlette | 2:88a30cc88a86 | 9 | extern Serial pc; |
| GeofferyOmlette | 2:88a30cc88a86 | 10 | /* stored data for observable resource */ |
| GeofferyOmlette | 2:88a30cc88a86 | 11 | static uint8_t obs_number = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 12 | static uint8_t *obs_token_ptr = NULL; |
| GeofferyOmlette | 2:88a30cc88a86 | 13 | static uint8_t obs_token_len = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 14 | |
| bjblazkowicz | 4:ab3c8d25260e | 15 | static const char* g_name = "no_capsule"; |
| bjblazkowicz | 4:ab3c8d25260e | 16 | |
| bjblazkowicz | 6:8729a0db0e25 | 17 | //static RGB_Detection g_detector(p23,p24,p25,p20); |
| bjblazkowicz | 3:8e1117ec91ba | 18 | |
| GeofferyOmlette | 2:88a30cc88a86 | 19 | /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ |
| GeofferyOmlette | 2:88a30cc88a86 | 20 | /* Node updates nespresso value every 10 seconds. Notification sending is done here. */ |
| GeofferyOmlette | 2:88a30cc88a86 | 21 | static void exec_call_thread(void const *args) |
| GeofferyOmlette | 2:88a30cc88a86 | 22 | { |
| bjblazkowicz | 4:ab3c8d25260e | 23 | pc.printf("Entering thread\r\n"); |
| bjblazkowicz | 4:ab3c8d25260e | 24 | int32_t time = 0; |
| bjblazkowicz | 4:ab3c8d25260e | 25 | while (true) |
| bjblazkowicz | 4:ab3c8d25260e | 26 | { |
| bjblazkowicz | 4:ab3c8d25260e | 27 | time++; |
| bjblazkowicz | 4:ab3c8d25260e | 28 | sn_nsdl_exec(time); |
| bjblazkowicz | 4:ab3c8d25260e | 29 | pc.printf("obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr); |
| bjblazkowicz | 4:ab3c8d25260e | 30 | // if (obs_number != 0 && obs_token_ptr != NULL) |
| bjblazkowicz | 4:ab3c8d25260e | 31 | { |
| bjblazkowicz | 4:ab3c8d25260e | 32 | obs_number++; |
| bjblazkowicz | 4:ab3c8d25260e | 33 | pc.printf("Waiting for detector...\r\n"); |
| bjblazkowicz | 6:8729a0db0e25 | 34 | g_name = ""; // g_detector.run(); |
| bjblazkowicz | 4:ab3c8d25260e | 35 | int len = strlen(g_name); |
| bjblazkowicz | 4:ab3c8d25260e | 36 | pc.printf("Detected %s\r\n", g_name); |
| bjblazkowicz | 4:ab3c8d25260e | 37 | if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)g_name, len, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) |
| bjblazkowicz | 4:ab3c8d25260e | 38 | { |
| bjblazkowicz | 4:ab3c8d25260e | 39 | pc.printf("Failed to send observation\r\n"); |
| bjblazkowicz | 4:ab3c8d25260e | 40 | } |
| bjblazkowicz | 4:ab3c8d25260e | 41 | else |
| bjblazkowicz | 4:ab3c8d25260e | 42 | { |
| bjblazkowicz | 4:ab3c8d25260e | 43 | pc.printf("Sent observation successfully\r\n"); |
| bjblazkowicz | 4:ab3c8d25260e | 44 | } |
| bjblazkowicz | 4:ab3c8d25260e | 45 | } |
| bjblazkowicz | 4:ab3c8d25260e | 46 | } |
| bjblazkowicz | 4:ab3c8d25260e | 47 | } |
| bjblazkowicz | 4:ab3c8d25260e | 48 | |
| bjblazkowicz | 4:ab3c8d25260e | 49 | #if 0 |
| bjblazkowicz | 4:ab3c8d25260e | 50 | static void exec_call_thread(void const *args) |
| bjblazkowicz | 4:ab3c8d25260e | 51 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 52 | int32_t time = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 53 | while (true) |
| GeofferyOmlette | 2:88a30cc88a86 | 54 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 55 | time++; |
| GeofferyOmlette | 2:88a30cc88a86 | 56 | sn_nsdl_exec(time); |
| GeofferyOmlette | 2:88a30cc88a86 | 57 | if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL) |
| GeofferyOmlette | 2:88a30cc88a86 | 58 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 59 | obs_number++; |
| bjblazkowicz | 4:ab3c8d25260e | 60 | if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)g_res_nespresso_val, (strlen(res_nespresso_val)-1), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) |
| bjblazkowicz | 3:8e1117ec91ba | 61 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 62 | pc.printf("Observation sending failed\r\n"); |
| bjblazkowicz | 3:8e1117ec91ba | 63 | } |
| bjblazkowicz | 3:8e1117ec91ba | 64 | else |
| bjblazkowicz | 3:8e1117ec91ba | 65 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 66 | pc.printf("Observation\r\n"); |
| bjblazkowicz | 3:8e1117ec91ba | 67 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 68 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 69 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 70 | } |
| bjblazkowicz | 4:ab3c8d25260e | 71 | #endif |
| bjblazkowicz | 4:ab3c8d25260e | 72 | |
| bjblazkowicz | 4:ab3c8d25260e | 73 | |
| GeofferyOmlette | 2:88a30cc88a86 | 74 | |
| GeofferyOmlette | 2:88a30cc88a86 | 75 | /* Only GET method allowed */ |
| GeofferyOmlette | 2:88a30cc88a86 | 76 | static uint8_t nespresso_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
| GeofferyOmlette | 2:88a30cc88a86 | 77 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 78 | sn_coap_hdr_s *coap_res_ptr = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 79 | |
| GeofferyOmlette | 2:88a30cc88a86 | 80 | pc.printf("nespresso callback\r\n"); |
| GeofferyOmlette | 2:88a30cc88a86 | 81 | |
| GeofferyOmlette | 2:88a30cc88a86 | 82 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
| GeofferyOmlette | 2:88a30cc88a86 | 83 | |
| bjblazkowicz | 4:ab3c8d25260e | 84 | coap_res_ptr->payload_len = strlen(g_name)-1; |
| bjblazkowicz | 4:ab3c8d25260e | 85 | coap_res_ptr->payload_ptr = (uint8_t*)g_name; |
| GeofferyOmlette | 2:88a30cc88a86 | 86 | |
| GeofferyOmlette | 2:88a30cc88a86 | 87 | if(received_coap_ptr->token_ptr) |
| GeofferyOmlette | 2:88a30cc88a86 | 88 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 89 | pc.printf("Token included\r\n"); |
| GeofferyOmlette | 2:88a30cc88a86 | 90 | if(obs_token_ptr) |
| GeofferyOmlette | 2:88a30cc88a86 | 91 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 92 | free(obs_token_ptr); |
| GeofferyOmlette | 2:88a30cc88a86 | 93 | obs_token_ptr = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 94 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 95 | obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
| GeofferyOmlette | 2:88a30cc88a86 | 96 | if(obs_token_ptr) |
| GeofferyOmlette | 2:88a30cc88a86 | 97 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 98 | memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
| GeofferyOmlette | 2:88a30cc88a86 | 99 | obs_token_len = received_coap_ptr->token_len; |
| GeofferyOmlette | 2:88a30cc88a86 | 100 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 101 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 102 | |
| GeofferyOmlette | 2:88a30cc88a86 | 103 | if(received_coap_ptr->options_list_ptr->observe) |
| GeofferyOmlette | 2:88a30cc88a86 | 104 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 105 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
| GeofferyOmlette | 2:88a30cc88a86 | 106 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
| GeofferyOmlette | 2:88a30cc88a86 | 107 | coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; |
| GeofferyOmlette | 2:88a30cc88a86 | 108 | coap_res_ptr->options_list_ptr->observe_len = 1; |
| GeofferyOmlette | 2:88a30cc88a86 | 109 | obs_number++; |
| GeofferyOmlette | 2:88a30cc88a86 | 110 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 111 | |
| GeofferyOmlette | 2:88a30cc88a86 | 112 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
| GeofferyOmlette | 2:88a30cc88a86 | 113 | |
| GeofferyOmlette | 2:88a30cc88a86 | 114 | coap_res_ptr->options_list_ptr->observe_ptr = 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 115 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
| GeofferyOmlette | 2:88a30cc88a86 | 116 | |
| GeofferyOmlette | 2:88a30cc88a86 | 117 | return 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 118 | } |
| GeofferyOmlette | 2:88a30cc88a86 | 119 | |
| GeofferyOmlette | 2:88a30cc88a86 | 120 | int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr) |
| GeofferyOmlette | 2:88a30cc88a86 | 121 | { |
| GeofferyOmlette | 2:88a30cc88a86 | 122 | static Thread exec_thread(exec_call_thread); |
| bjblazkowicz | 4:ab3c8d25260e | 123 | pc.printf("Created thread\r\n"); |
| GeofferyOmlette | 2:88a30cc88a86 | 124 | nsdl_create_dynamic_resource(resource_ptr, sizeof(NESPRESSO_RES_ID)-1, (uint8_t*)NESPRESSO_RES_ID, 0, 0, 1, &nespresso_resource_cb, SN_GRS_GET_ALLOWED); |
| bjblazkowicz | 4:ab3c8d25260e | 125 | pc.printf("Created resource\r\n"); |
| GeofferyOmlette | 2:88a30cc88a86 | 126 | return 0; |
| GeofferyOmlette | 2:88a30cc88a86 | 127 | } |
