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: simple-mbed-client
Fork of simple-mbed-client-example by
Diff: main.cpp
- Revision:
- 34:1e8d914a2876
- Parent:
- 29:dd6231df71bb
--- a/main.cpp Wed Sep 28 12:39:13 2016 +0300
+++ b/main.cpp Fri Dec 02 15:44:32 2016 +0800
@@ -3,62 +3,84 @@
#include "easy-connect.h"
#include "simple-mbed-client.h"
-Serial pc(USBTX, USBRX);
+EventQueue eventQueue(32 * EVENTS_EVENT_SIZE); // Event queue!
+
SimpleMbedClient client;
-DigitalOut led(LED1);
-DigitalOut blinkLed(LED2);
-InterruptIn btn(MBED_CONF_APP_BUTTON);
-Semaphore updates(0);
+// Declare peripherals
+DigitalOut connectivityLed(LED2); // Blinks while connecting, turns solid when connected
+DigitalOut augmentedLed(LED1, BUILTIN_LED_OFF); // LED that can be controlled through Connector
+InterruptIn btn(MBED_CONF_APP_BUTTON); // Button that sends it's count to Connector
+Ticker connectivityTicker;
+
+// Callback function when the pattern resource is updated
void patternUpdated(string v) {
printf("New pattern: %s\n", v.c_str());
}
+// Define resources here. They act as normal variables, but are exposed to the internet...
SimpleResourceInt btn_count = client.define_resource("button/0/clicks", 0, M2MBase::GET_ALLOWED);
SimpleResourceString pattern = client.define_resource("led/0/pattern", "500:500:500:500:500:500:500", &patternUpdated);
void fall() {
- updates.release();
+ btn_count = btn_count + 1;
+ printf("Button count is now %d\r\n", static_cast<int>(btn_count));
}
-void toggleLed() {
- led = !led;
+void toggleConnectivityLed() {
+ connectivityLed = !connectivityLed;
}
void registered() {
- pc.printf("Registered\r\n");
-}
-void unregistered() {
- pc.printf("Unregistered\r\n");
+ printf("Registered\r\n");
+
+ connectivityTicker.detach();
+ connectivityLed = BUILTIN_LED_ON;
}
void play(void* args) {
+ connectivityLed = BUILTIN_LED_OFF;
+
+ // Parse the pattern string, and toggle the LED in that pattern
stringstream ss(pattern);
string item;
while(getline(ss, item, ':')) {
wait_ms(atoi((const char*)item.c_str()));
- blinkLed = !blinkLed;
+ augmentedLed = !augmentedLed;
}
+
+ augmentedLed = BUILTIN_LED_OFF;
+ connectivityLed = BUILTIN_LED_ON;
}
int main() {
- pc.baud(115200);
+ // Run a separate Thread to process events in
+ rtos::Thread et;
+ et.start(callback(&eventQueue, &EventQueue::dispatch_forever));
- btn.fall(&fall);
+ // Handle interrupts on the eventQueue - so we don't do things in interrupt context
+ btn.fall(eventQueue.event(&fall));
- Ticker t;
- t.attach(&toggleLed, 1.0f);
+ // Toggle the built-in LED every second (until we're connected, see `registered` function)
+ connectivityTicker.attach(eventQueue.event(toggleConnectivityLed), 1.0f);
+ // Keep alive ticker (every 25 seconds)
+ eventQueue.call_every(25000, callback(&client, &SimpleMbedClient::keep_alive));
+
+ // Functions can be executed through mbed Device Connector via POST calls
client.define_function("led/0/play", &play);
+ // Connect to the internet (see mbed_app.json for the connectivity method)
NetworkInterface* network = easy_connect(true);
if (!network) {
return 1;
}
+ // Connect to mbed Device Connector
struct MbedClientOptions opts = client.get_default_options();
opts.ServerAddress = MBED_SERVER_ADDRESS;
+
bool setup = client.setup(opts, network);
if (!setup) {
printf("Client setup failed\n");
@@ -66,14 +88,6 @@
}
client.on_registered(®istered);
- client.on_unregistered(&unregistered);
- while (1) {
- int v = updates.wait(25000);
- if (v == 1) {
- btn_count = btn_count + 1;
- printf("Button count is now %d\n", static_cast<int>(btn_count));
- }
- client.keep_alive();
- }
+ while (1) {}
}
