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
Revision 6:8729a0db0e25, committed 2014-07-18
- Comitter:
- bjblazkowicz
- Date:
- Fri Jul 18 10:47:23 2014 +0000
- Parent:
- 5:cb0df4fbe7a1
- Child:
- 7:4d873b7d187b
- Commit message:
- Added buffered_rgb_resource. Resource is now array of up to 300 samples.
Changed in this revision
--- a/EthernetInterface.lib Mon Jul 14 12:34:16 2014 +0000 +++ b/EthernetInterface.lib Fri Jul 18 10:47:23 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/EthernetInterface/#f6ec7a025939 +http://mbed.org/users/mbed_official/code/EthernetInterface/#e6b79f0ccd95
--- a/main.cpp Mon Jul 14 12:34:16 2014 +0000
+++ b/main.cpp Fri Jul 18 10:47:23 2014 +0000
@@ -3,6 +3,7 @@
#include "nsdl_support.h"
#include "dbg.h"
#include "nespresso.h"
+#include "buffered_rgb_resource.h"
Serial pc(USBTX, USBRX); // tx, rx
@@ -20,7 +21,7 @@
// NSP configuration
/* Change this IP address to that of your NanoService Platform installation */
-static const char* NSP_ADDRESS = "10.1.16.228"; // "23.97.218.184"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/
+static const char* NSP_ADDRESS = "23.99.96.113";
static const int NSP_PORT = 5683;
char endpoint_name[] = "nespresso-client";
uint8_t ep_type[] = {"mbed_device"};
@@ -89,7 +90,8 @@
}
memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
- create_nespresso_resource(resource_ptr);
+ //create_nespresso_resource(resource_ptr);
+ create_buffered_rgb_resource(resource_ptr);
/* Register with NSP */
endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
@@ -109,6 +111,8 @@
int main()
{
+ pc.baud(115200);
+
// Initialize Ethernet interface first
pc.printf("Initializing ethernet... ");
ethernet_init();
--- a/mbed-rtos.lib Mon Jul 14 12:34:16 2014 +0000 +++ b/mbed-rtos.lib Fri Jul 18 10:47:23 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d +http://mbed.org/users/mbed_official/code/mbed-rtos/#ff95651f53c7
--- a/mbed.bld Mon Jul 14 12:34:16 2014 +0000 +++ b/mbed.bld Fri Jul 18 10:47:23 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/buffered_rgb_resource.cpp Fri Jul 18 10:47:23 2014 +0000
@@ -0,0 +1,88 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "nsdl_support.h"
+#include "buffered_rgb_resource.h"
+#include "rgb_sensor_buffer.h"
+#include "payload.h"
+
+#define RESOURCE_ID "sensor/capsule"
+#define RGB_VALUES 512
+#define RGB_THRESHOLD 220
+
+extern Serial pc;
+/* stored data for observable resource */
+static uint8_t obs_number = 0;
+static uint8_t *obs_token_ptr = NULL;
+static uint8_t obs_token_len = 0;
+
+static TRGB g_buffer[RGB_VALUES];
+static RGB_SensorBuffer g_rgb(p23,p24,p25,p20);
+static Payload g_payload;
+
+static void exec_call_thread(void const *args)
+{
+ while (true)
+ {
+ if (obs_number == 0 && obs_token_ptr == NULL)
+ pc.printf("ERROR: obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
+
+ obs_number++;
+ pc.printf("Waiting for detector...\r\n");
+ int samples_captured = g_rgb.trigger(g_buffer, RGB_VALUES, RGB_THRESHOLD);
+
+ g_payload.build(g_buffer, samples_captured);
+
+ int result = sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, g_payload.raw_bytes(), g_payload.raw_bytes_size(), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
+
+ pc.printf("Observation send %s. [samples: %d, bytes: %d]\r\n", result == 0 ? "failed" : "succeeded", samples_captured, g_payload.raw_bytes_size());
+ }
+}
+
+/* Only GET method allowed */
+static uint8_t 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;
+
+ coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
+
+ coap_res_ptr->payload_len = g_payload.raw_bytes_size();
+ coap_res_ptr->payload_ptr = g_payload.raw_bytes();
+
+ 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_buffered_rgb_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+ static Thread exec_thread(exec_call_thread);
+ nsdl_create_dynamic_resource(resource_ptr, sizeof(RESOURCE_ID) - 1, (uint8_t*)RESOURCE_ID, 0, 0, 1, &resource_cb, SN_GRS_GET_ALLOWED);
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/buffered_rgb_resource.h Fri Jul 18 10:47:23 2014 +0000 @@ -0,0 +1,10 @@ +// Nespresso resource implementation + +#ifndef BUFFERED_RGB_RESOURCE_H +#define BUFFERED_RGB_RESOURCE_H + +#include "nsdl_support.h" + +int create_buffered_rgb_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
--- a/resources/nespresso.cpp Mon Jul 14 12:34:16 2014 +0000
+++ b/resources/nespresso.cpp Fri Jul 18 10:47:23 2014 +0000
@@ -1,5 +1,3 @@
-// GPS resource implementation
-
#include "mbed.h"
#include "rtos.h"
#include "nsdl_support.h"
@@ -16,57 +14,7 @@
static const char* g_name = "no_capsule";
-static RGB_Detection g_detector(p23,p24,p25,p20);
-
-
-/*
- *
- *
- */
-/*
-const char* do_detection()
-{
- static int counter = 0;
-
- const char *names[] = {
- "KAZAAR",
- "DHARKAN",
- "RISTRETTO",
- "ARPEGGIO",
- "ROMA",
- "LIVANTO",
- "CAPRICCIO",
- "VOLLUTO",
- "COSI",
- "INDRIYA FROM INDIA",
- "ROSABAYA DE COLUMBIA",
- "DULSAO DO BRASIL",
- "BUKEELA KA ETHIOPIA",
- "FORTISSIMO LUNGO RENOVATION",
- "DECAFFEINATO LUNGO",
- "DECAFFEINATO",
- "DECAFFEINATO INTENSO"
- };
- const int num_names = sizeof(names) / sizeof(names[0]);
-
- wait_ms(1500);
-
- counter++;
- return names[counter % num_names];
-}
-*/
-/*
- console.printf("Started\r\n");
- RGB_Detection detector(p23,p24,p25,p20);
-
- console.baud(115200);
-
- while (1)
- {
- const char* name = detector.run();
- console.printf("%s\r\n", name);
- }
-*/
+//static RGB_Detection g_detector(p23,p24,p25,p20);
/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
/* Node updates nespresso value every 10 seconds. Notification sending is done here. */
@@ -83,7 +31,7 @@
{
obs_number++;
pc.printf("Waiting for detector...\r\n");
- g_name = g_detector.run(); //do_detection();
+ g_name = ""; // g_detector.run();
int len = strlen(g_name);
pc.printf("Detected %s\r\n", g_name);
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)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/payload.cpp Fri Jul 18 10:47:23 2014 +0000
@@ -0,0 +1,78 @@
+/*
+ * Builds the payload to send to the server.
+ *
+ * Copyright (c) 2014 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <mbed.h>
+#include "payload.h"
+
+// Maximum number of samples to include in the payload.
+// Defined such that the payload fits within one UDP packet.
+#define MAX_SAMPLES 300
+
+Payload::Payload() :
+ m_mdb_bytes(0)
+{
+}
+
+//
+// Build the payload. The payload is defined by the Payload::MeanDeltaBuffer struct
+// and consists of:
+// * number of samples (32 bits)
+// * mean of red, green, blue channels (32 bits each)
+// * difference of sample to mean for red, green, blue channels (1 byte each)
+//
+// This encoding can store ~300 samples in a one UDP packet.
+//
+void Payload::build(const TRGB* samples, int sample_count)
+{
+ if (sample_count > MAX_SAMPLES)
+ sample_count = MAX_SAMPLES;
+
+ for (int i = 0; i < sample_count; i++)
+ {
+ m_mdb.mean[0] += (samples[i].data[0] / RGB_OVERSAMPLING);
+ m_mdb.mean[1] += (samples[i].data[1] / RGB_OVERSAMPLING);
+ m_mdb.mean[2] += (samples[i].data[2] / RGB_OVERSAMPLING);
+ }
+
+ m_mdb.sample_count = sample_count;
+ m_mdb.mean[0] /= sample_count;
+ m_mdb.mean[1] /= sample_count;
+ m_mdb.mean[2] /= sample_count;
+
+ for (int i = 0; i < sample_count; i++)
+ {
+ // TODO: clamp to signed char range [-128, 127]
+ m_mdb.deltas[i][0] = m_mdb.mean[0] - (samples[i].data[0] / RGB_OVERSAMPLING);
+ m_mdb.deltas[i][1] = m_mdb.mean[1] - (samples[i].data[1] / RGB_OVERSAMPLING);
+ m_mdb.deltas[i][2] = m_mdb.mean[2] - (samples[i].data[2] / RGB_OVERSAMPLING);
+ }
+
+ // calculate number of bytes in payload
+ m_mdb_bytes = sizeof(int) + // number of samples
+ (3 * sizeof(int)) + // mean of red, green & blue channels
+ (sample_count * sizeof(signed char) * 3); // one byte per channel per sample
+}
+
+int Payload::raw_bytes_size()
+{
+ return m_mdb_bytes;
+}
+
+uint8_t* Payload::raw_bytes() const
+{
+ return (uint8_t *)&m_mdb;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/payload.h Fri Jul 18 10:47:23 2014 +0000
@@ -0,0 +1,49 @@
+/*
+ * Builds the payload to send to the server.
+ *
+ * Copyright (c) 2014 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _PAYLOAD_H
+#define _PAYLOAD_H
+#include "rgb_sensor.h"
+
+// TODO: include from common file
+#ifndef RGB_VALUES
+#define RGB_VALUES 512
+#endif
+
+class Payload
+{
+public:
+ Payload();
+
+ void build(const TRGB *samples, int sample_count);
+ int raw_bytes_size();
+ uint8_t* raw_bytes() const;
+
+private:
+ typedef struct
+ {
+ int sample_count;
+ int mean[3];
+ signed char deltas[RGB_VALUES][3];
+ } MeanDeltaBuffer;
+
+ MeanDeltaBuffer m_mdb;
+ int m_mdb_bytes;
+};
+
+#endif // _PAYLOAD_H
\ No newline at end of file
