Brian Daniels / Mbed 2 deprecated nespresso_demo

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Committer:
bjblazkowicz
Date:
Mon Jul 14 10:45:33 2014 +0000
Revision:
4:ab3c8d25260e
Parent:
3:8e1117ec91ba
Child:
6:8729a0db0e25
First successful end-to-end integration.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 2:88a30cc88a86 1 // GPS resource implementation
GeofferyOmlette 2:88a30cc88a86 2
GeofferyOmlette 2:88a30cc88a86 3 #include "mbed.h"
GeofferyOmlette 2:88a30cc88a86 4 #include "rtos.h"
GeofferyOmlette 2:88a30cc88a86 5 #include "nsdl_support.h"
GeofferyOmlette 2:88a30cc88a86 6 #include "nespresso.h"
bjblazkowicz 4:ab3c8d25260e 7 #include "detection.h"
GeofferyOmlette 2:88a30cc88a86 8
bjblazkowicz 4:ab3c8d25260e 9 #define NESPRESSO_RES_ID "sensor/capsule"
GeofferyOmlette 2:88a30cc88a86 10
GeofferyOmlette 2:88a30cc88a86 11 extern Serial pc;
GeofferyOmlette 2:88a30cc88a86 12 /* stored data for observable resource */
GeofferyOmlette 2:88a30cc88a86 13 static uint8_t obs_number = 0;
GeofferyOmlette 2:88a30cc88a86 14 static uint8_t *obs_token_ptr = NULL;
GeofferyOmlette 2:88a30cc88a86 15 static uint8_t obs_token_len = 0;
GeofferyOmlette 2:88a30cc88a86 16
bjblazkowicz 4:ab3c8d25260e 17 static const char* g_name = "no_capsule";
bjblazkowicz 4:ab3c8d25260e 18
bjblazkowicz 4:ab3c8d25260e 19 static RGB_Detection g_detector(p23,p24,p25,p20);
bjblazkowicz 4:ab3c8d25260e 20
GeofferyOmlette 2:88a30cc88a86 21
bjblazkowicz 3:8e1117ec91ba 22 /*
bjblazkowicz 3:8e1117ec91ba 23 *
bjblazkowicz 3:8e1117ec91ba 24 *
bjblazkowicz 3:8e1117ec91ba 25 */
bjblazkowicz 4:ab3c8d25260e 26 /*
bjblazkowicz 3:8e1117ec91ba 27 const char* do_detection()
bjblazkowicz 3:8e1117ec91ba 28 {
bjblazkowicz 3:8e1117ec91ba 29 static int counter = 0;
bjblazkowicz 3:8e1117ec91ba 30
bjblazkowicz 3:8e1117ec91ba 31 const char *names[] = {
bjblazkowicz 4:ab3c8d25260e 32 "KAZAAR",
bjblazkowicz 4:ab3c8d25260e 33 "DHARKAN",
bjblazkowicz 4:ab3c8d25260e 34 "RISTRETTO",
bjblazkowicz 4:ab3c8d25260e 35 "ARPEGGIO",
bjblazkowicz 4:ab3c8d25260e 36 "ROMA",
bjblazkowicz 4:ab3c8d25260e 37 "LIVANTO",
bjblazkowicz 4:ab3c8d25260e 38 "CAPRICCIO",
bjblazkowicz 4:ab3c8d25260e 39 "VOLLUTO",
bjblazkowicz 4:ab3c8d25260e 40 "COSI",
bjblazkowicz 4:ab3c8d25260e 41 "INDRIYA FROM INDIA",
bjblazkowicz 4:ab3c8d25260e 42 "ROSABAYA DE COLUMBIA",
bjblazkowicz 4:ab3c8d25260e 43 "DULSAO DO BRASIL",
bjblazkowicz 4:ab3c8d25260e 44 "BUKEELA KA ETHIOPIA",
bjblazkowicz 4:ab3c8d25260e 45 "FORTISSIMO LUNGO RENOVATION",
bjblazkowicz 4:ab3c8d25260e 46 "DECAFFEINATO LUNGO",
bjblazkowicz 4:ab3c8d25260e 47 "DECAFFEINATO",
bjblazkowicz 4:ab3c8d25260e 48 "DECAFFEINATO INTENSO"
bjblazkowicz 3:8e1117ec91ba 49 };
bjblazkowicz 3:8e1117ec91ba 50 const int num_names = sizeof(names) / sizeof(names[0]);
bjblazkowicz 3:8e1117ec91ba 51
bjblazkowicz 3:8e1117ec91ba 52 wait_ms(1500);
bjblazkowicz 3:8e1117ec91ba 53
bjblazkowicz 3:8e1117ec91ba 54 counter++;
bjblazkowicz 3:8e1117ec91ba 55 return names[counter % num_names];
bjblazkowicz 3:8e1117ec91ba 56 }
bjblazkowicz 4:ab3c8d25260e 57 */
bjblazkowicz 4:ab3c8d25260e 58 /*
bjblazkowicz 4:ab3c8d25260e 59 console.printf("Started\r\n");
bjblazkowicz 4:ab3c8d25260e 60 RGB_Detection detector(p23,p24,p25,p20);
bjblazkowicz 4:ab3c8d25260e 61
bjblazkowicz 4:ab3c8d25260e 62 console.baud(115200);
bjblazkowicz 4:ab3c8d25260e 63
bjblazkowicz 4:ab3c8d25260e 64 while (1)
bjblazkowicz 4:ab3c8d25260e 65 {
bjblazkowicz 4:ab3c8d25260e 66 const char* name = detector.run();
bjblazkowicz 4:ab3c8d25260e 67 console.printf("%s\r\n", name);
bjblazkowicz 4:ab3c8d25260e 68 }
bjblazkowicz 4:ab3c8d25260e 69 */
bjblazkowicz 3:8e1117ec91ba 70
GeofferyOmlette 2:88a30cc88a86 71 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
GeofferyOmlette 2:88a30cc88a86 72 /* Node updates nespresso value every 10 seconds. Notification sending is done here. */
GeofferyOmlette 2:88a30cc88a86 73 static void exec_call_thread(void const *args)
GeofferyOmlette 2:88a30cc88a86 74 {
bjblazkowicz 4:ab3c8d25260e 75 pc.printf("Entering thread\r\n");
bjblazkowicz 4:ab3c8d25260e 76 int32_t time = 0;
bjblazkowicz 4:ab3c8d25260e 77 while (true)
bjblazkowicz 4:ab3c8d25260e 78 {
bjblazkowicz 4:ab3c8d25260e 79 time++;
bjblazkowicz 4:ab3c8d25260e 80 sn_nsdl_exec(time);
bjblazkowicz 4:ab3c8d25260e 81 pc.printf("obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
bjblazkowicz 4:ab3c8d25260e 82 // if (obs_number != 0 && obs_token_ptr != NULL)
bjblazkowicz 4:ab3c8d25260e 83 {
bjblazkowicz 4:ab3c8d25260e 84 obs_number++;
bjblazkowicz 4:ab3c8d25260e 85 pc.printf("Waiting for detector...\r\n");
bjblazkowicz 4:ab3c8d25260e 86 g_name = g_detector.run(); //do_detection();
bjblazkowicz 4:ab3c8d25260e 87 int len = strlen(g_name);
bjblazkowicz 4:ab3c8d25260e 88 pc.printf("Detected %s\r\n", g_name);
bjblazkowicz 4:ab3c8d25260e 89 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 90 {
bjblazkowicz 4:ab3c8d25260e 91 pc.printf("Failed to send observation\r\n");
bjblazkowicz 4:ab3c8d25260e 92 }
bjblazkowicz 4:ab3c8d25260e 93 else
bjblazkowicz 4:ab3c8d25260e 94 {
bjblazkowicz 4:ab3c8d25260e 95 pc.printf("Sent observation successfully\r\n");
bjblazkowicz 4:ab3c8d25260e 96 }
bjblazkowicz 4:ab3c8d25260e 97 }
bjblazkowicz 4:ab3c8d25260e 98 }
bjblazkowicz 4:ab3c8d25260e 99 }
bjblazkowicz 4:ab3c8d25260e 100
bjblazkowicz 4:ab3c8d25260e 101 #if 0
bjblazkowicz 4:ab3c8d25260e 102 static void exec_call_thread(void const *args)
bjblazkowicz 4:ab3c8d25260e 103 {
GeofferyOmlette 2:88a30cc88a86 104 int32_t time = 0;
GeofferyOmlette 2:88a30cc88a86 105 while (true)
GeofferyOmlette 2:88a30cc88a86 106 {
GeofferyOmlette 2:88a30cc88a86 107 time++;
GeofferyOmlette 2:88a30cc88a86 108 sn_nsdl_exec(time);
GeofferyOmlette 2:88a30cc88a86 109 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
GeofferyOmlette 2:88a30cc88a86 110 {
GeofferyOmlette 2:88a30cc88a86 111 obs_number++;
bjblazkowicz 4:ab3c8d25260e 112 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 113 {
GeofferyOmlette 2:88a30cc88a86 114 pc.printf("Observation sending failed\r\n");
bjblazkowicz 3:8e1117ec91ba 115 }
bjblazkowicz 3:8e1117ec91ba 116 else
bjblazkowicz 3:8e1117ec91ba 117 {
GeofferyOmlette 2:88a30cc88a86 118 pc.printf("Observation\r\n");
bjblazkowicz 3:8e1117ec91ba 119 }
GeofferyOmlette 2:88a30cc88a86 120 }
GeofferyOmlette 2:88a30cc88a86 121 }
GeofferyOmlette 2:88a30cc88a86 122 }
bjblazkowicz 4:ab3c8d25260e 123 #endif
bjblazkowicz 4:ab3c8d25260e 124
bjblazkowicz 4:ab3c8d25260e 125
GeofferyOmlette 2:88a30cc88a86 126
GeofferyOmlette 2:88a30cc88a86 127 /* Only GET method allowed */
GeofferyOmlette 2:88a30cc88a86 128 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 129 {
GeofferyOmlette 2:88a30cc88a86 130 sn_coap_hdr_s *coap_res_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 131
GeofferyOmlette 2:88a30cc88a86 132 pc.printf("nespresso callback\r\n");
GeofferyOmlette 2:88a30cc88a86 133
GeofferyOmlette 2:88a30cc88a86 134 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
GeofferyOmlette 2:88a30cc88a86 135
bjblazkowicz 4:ab3c8d25260e 136 coap_res_ptr->payload_len = strlen(g_name)-1;
bjblazkowicz 4:ab3c8d25260e 137 coap_res_ptr->payload_ptr = (uint8_t*)g_name;
GeofferyOmlette 2:88a30cc88a86 138
GeofferyOmlette 2:88a30cc88a86 139 if(received_coap_ptr->token_ptr)
GeofferyOmlette 2:88a30cc88a86 140 {
GeofferyOmlette 2:88a30cc88a86 141 pc.printf("Token included\r\n");
GeofferyOmlette 2:88a30cc88a86 142 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 143 {
GeofferyOmlette 2:88a30cc88a86 144 free(obs_token_ptr);
GeofferyOmlette 2:88a30cc88a86 145 obs_token_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 146 }
GeofferyOmlette 2:88a30cc88a86 147 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 148 if(obs_token_ptr)
GeofferyOmlette 2:88a30cc88a86 149 {
GeofferyOmlette 2:88a30cc88a86 150 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
GeofferyOmlette 2:88a30cc88a86 151 obs_token_len = received_coap_ptr->token_len;
GeofferyOmlette 2:88a30cc88a86 152 }
GeofferyOmlette 2:88a30cc88a86 153 }
GeofferyOmlette 2:88a30cc88a86 154
GeofferyOmlette 2:88a30cc88a86 155 if(received_coap_ptr->options_list_ptr->observe)
GeofferyOmlette 2:88a30cc88a86 156 {
GeofferyOmlette 2:88a30cc88a86 157 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 158 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 2:88a30cc88a86 159 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
GeofferyOmlette 2:88a30cc88a86 160 coap_res_ptr->options_list_ptr->observe_len = 1;
GeofferyOmlette 2:88a30cc88a86 161 obs_number++;
GeofferyOmlette 2:88a30cc88a86 162 }
GeofferyOmlette 2:88a30cc88a86 163
GeofferyOmlette 2:88a30cc88a86 164 sn_nsdl_send_coap_message(address, coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 165
GeofferyOmlette 2:88a30cc88a86 166 coap_res_ptr->options_list_ptr->observe_ptr = 0;
GeofferyOmlette 2:88a30cc88a86 167 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
GeofferyOmlette 2:88a30cc88a86 168
GeofferyOmlette 2:88a30cc88a86 169 return 0;
GeofferyOmlette 2:88a30cc88a86 170 }
GeofferyOmlette 2:88a30cc88a86 171
GeofferyOmlette 2:88a30cc88a86 172 int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr)
GeofferyOmlette 2:88a30cc88a86 173 {
GeofferyOmlette 2:88a30cc88a86 174 static Thread exec_thread(exec_call_thread);
bjblazkowicz 4:ab3c8d25260e 175 pc.printf("Created thread\r\n");
GeofferyOmlette 2:88a30cc88a86 176 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 177 pc.printf("Created resource\r\n");
GeofferyOmlette 2:88a30cc88a86 178 return 0;
GeofferyOmlette 2:88a30cc88a86 179 }