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-06-30
- Revision:
- 17:6d69aa1b393f
- Parent:
- 16:71ec73ea9b6d
- Child:
- 23:16f397924e3d
File content as of revision 17:6d69aa1b393f:
#include "mbed.h"
#include "security.h"
#include "easy-connect.h"
#include "simple-mbed-client.h"
Serial pc(USBTX, USBRX);
SimpleMbedClient client;
DigitalOut led(LED_RED);
DigitalOut blinkLed(LED_GREEN);
InterruptIn btn(SW3);
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() {
printf("registered\n");
}
void unregistered() {
printf("unregistered\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();
if (!network) {
return 1;
}
bool setup = client.setup(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();
}
}
