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.
Revision 28:f3b254af78b2, committed 2020-01-27
- Comitter:
- cvasilak
- Date:
- Mon Jan 27 16:29:54 2020 +0000
- Parent:
- 27:8b5b0fc59d47
- Commit message:
- blinking pattern refactoring
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Jan 27 15:29:50 2020 +0000 +++ b/main.cpp Mon Jan 27 16:29:54 2020 +0000 @@ -22,6 +22,8 @@ #include "FATFileSystem.h" #include "LittleFileSystem.h" +#include <string> + // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. NetworkInterface *net = NetworkInterface::get_default_instance(); @@ -55,7 +57,7 @@ // Declaring pointers for access to Pelion Device Management Client resources outside of main() MbedCloudClientResource *res_button; -MbedCloudClientResource *res_led; +MbedCloudClientResource *res_pattern_led; MbedCloudClientResource *res_post; // Additional resources for sensor readings @@ -71,39 +73,38 @@ // When the device is registered, this variable will be used to access various useful information, like device ID etc. static const ConnectorClientEndpointInfo* endpointInfo; -/** - * PUT handler - sets the value of the built-in LED - * @param resource The resource that triggered the callback - * @param newValue Updated value for the resource - */ -void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { - printf("PUT received. New value: %s\n", newValue.c_str()); - led = atoi(newValue.c_str()); +void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { + printf("PUT received, new value: %s\n", newValue.c_str()); +} + +void blink() { + led = !led; } /** - * POST handler - prints the content of the payload + * POST handler * @param resource The resource that triggered the callback * @param buffer If a body was passed to the POST function, this contains the data. * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. * @param size Size of the body */ -void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { - printf("POST received (length %u). Payload: ", size); - for (size_t ix = 0; ix < size; ix++) { - printf("%02x ", buffer[ix]); - } - printf("\n"); -} +void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { + printf("POST received. Going to blink LED pattern: %s\n", res_pattern_led->get_value().c_str()); -/** - * Button handler - * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) - */ -void button_press() { - int v = res_button->get_value_int() + 1; - res_button->set_value(v); - printf("Button clicked %d times\n", v); + // Parse the pattern string, and toggle the LED in that pattern + string s = string(res_pattern_led->get_value().c_str()); + size_t i = 0; + size_t pos = s.find(':'); + int total_len = 0; + while (pos != string::npos) { + int len = atoi(s.substr(i, pos - i).c_str()); + + mbed_event_queue()->call_in(total_len + len, &blink); + + total_len += len; + i = ++pos; + pos = s.find(':', pos); + } } /** @@ -116,13 +117,13 @@ } /** - * Registration callback handler - * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) - * When the device is registered, this variable will be used to access various useful information, like device ID etc. + * Button handler + * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below) */ -void registered(const ConnectorClientEndpointInfo *endpoint) { - printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); - endpointInfo = endpoint; +void button_press() { + int v = res_button->get_value_int() + 1; + res_button->set_value(v); + printf("Button clicked %d times\n", v); } /** @@ -139,6 +140,15 @@ } } +/** + * Registration callback handler + * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) + * When the device is registered, this variable will be used to access various useful information, like device ID etc. + */ +void registered(const ConnectorClientEndpointInfo *endpoint) { + printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); + endpointInfo = endpoint; +} int main(void) { printf("\nStarting Simple Pelion Device Management Client example\n"); @@ -191,14 +201,15 @@ res_button->observable(true); res_button->attach_notification_callback(button_callback); - res_led = client.create_resource("3201/0/5853", "LED State"); - res_led->set_value(led.read()); - res_led->methods(M2MMethod::GET | M2MMethod::PUT); - res_led->attach_put_callback(put_callback); + res_pattern_led = client.create_resource("3201/0/5853", "LED Pattern"); + //res_pattern_led->set_value("500:500:500:500:500:500:500:500"); + res_pattern_led->set_value("100:100:100:100:100:100:100:100"); + res_pattern_led->methods(M2MMethod::GET | M2MMethod::PUT); + res_pattern_led->attach_put_callback(pattern_updated); - res_post = client.create_resource("3300/0/5605", "Execute Function"); + res_post = client.create_resource("3201/0/5850", "Blink Action"); res_post->methods(M2MMethod::POST); - res_post->attach_post_callback(post_callback); + res_post->attach_post_callback(eventQueue.event(&blink_callback)); // Sensor resources res_temperature = client.create_resource("3303/0/5700", "Temperature (C)"); @@ -223,6 +234,7 @@ button.fall(eventQueue.event(&button_press)); printf("Press the user button to increment the LwM2M resource value...\n"); + // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations Ticker timer; timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);