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
main.cpp
- Committer:
- Jan Jongboom
- Date:
- 2016-09-06
- Revision:
- 29:dd6231df71bb
- Parent:
- 28:f5e41738699d
- Child:
- 34:1e8d914a2876
File content as of revision 29:dd6231df71bb:
#include "mbed.h"
#include "security.h"
#include "easy-connect.h"
#include "simple-mbed-client.h"
Serial pc(USBTX, USBRX);
SimpleMbedClient client;
DigitalOut led(LED1);
DigitalOut blinkLed(LED2);
InterruptIn btn(MBED_CONF_APP_BUTTON);
Semaphore updates(0);
void patternUpdated(string v) {
printf("New pattern: %s\n", v.c_str());
}
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();
}
void toggleLed() {
led = !led;
}
void registered() {
pc.printf("Registered\r\n");
}
void unregistered() {
pc.printf("Unregistered\r\n");
}
void play(void* args) {
stringstream ss(pattern);
string item;
while(getline(ss, item, ':')) {
wait_ms(atoi((const char*)item.c_str()));
blinkLed = !blinkLed;
}
}
int main() {
pc.baud(115200);
btn.fall(&fall);
Ticker t;
t.attach(&toggleLed, 1.0f);
client.define_function("led/0/play", &play);
NetworkInterface* network = easy_connect(true);
if (!network) {
return 1;
}
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");
return 1;
}
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();
}
}
