Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit cc782b4

Browse files
added separate resource for timer
1 parent 6f7a131 commit cc782b4

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

main.cpp

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ class InteractionProvider {
9393

9494
// Use the counter button handler to send an update of endpoint resource values
9595
// to connector every 15 seconds periodically.
96-
timer.attach(this, &InteractionProvider::counter_button_handler, 15.0);
96+
timer.attach(this, &InteractionProvider::timer_handler, 15.0);
9797
}
9898

9999
// flags for interaction, these are read from outside interrupt context
100-
volatile bool registered = false;
100+
volatile bool timer_ticked = false;
101101
volatile bool clicked = false;
102102

103+
103104
private:
104105

105-
void unreg_button_handler() {
106-
registered = false;
106+
void timer_handler() {
107+
timer_ticked = true;
107108
updates.release();
108109
}
109110

@@ -307,6 +308,61 @@ class ButtonResource {
307308
uint16_t counter;
308309
};
309310

311+
/*
312+
* The timer contains one property: counter.
313+
* When `handle_timer_tick` is executed, the counter updates.
314+
*/
315+
class TimerResource {
316+
public:
317+
TimerResource(): counter(0) {
318+
// create ObjectID with metadata tag of '3200', which is 'digital input'
319+
btn_object = M2MInterfaceFactory::create_object("3200");
320+
M2MObjectInstance* btn_inst = btn_object->create_object_instance();
321+
// create resource with ID '5502', which is digital input counter
322+
M2MResource* btn_res = btn_inst->create_dynamic_resource("5502", "Timer",
323+
M2MResourceInstance::INTEGER, true /* observable */);
324+
// we can read this value
325+
btn_res->set_operation(M2MBase::GET_ALLOWED);
326+
// set initial value (all values in mbed Client are buffers)
327+
// to be able to read this data easily in the Connector console, we'll use a string
328+
btn_res->set_value((uint8_t*)"0", 1);
329+
}
330+
331+
~TimerResource() {
332+
}
333+
334+
M2MObject* get_object() {
335+
return btn_object;
336+
}
337+
338+
/*
339+
* When the timer ticks, we read the current value of the click counter
340+
* from mbed Device Connector, then up the value with one.l
341+
*/
342+
void handle_timer_tick() {
343+
if (mbed_client.register_successful()) {
344+
M2MObjectInstance* inst = btn_object->object_instance();
345+
M2MResource* res = inst->resource("5502");
346+
347+
// up counter
348+
counter++;
349+
printf("handle_timer_click, new value of counter is %d\n", counter);
350+
// serialize the value of counter as a string, and tell connector
351+
char buffer[20];
352+
int size = sprintf(buffer,"%d",counter);
353+
res->set_value((uint8_t*)buffer, size);
354+
} else {
355+
printf("handle_timer_tick, device not registered\n");
356+
}
357+
}
358+
359+
private:
360+
M2MObject* btn_object;
361+
uint16_t counter;
362+
};
363+
364+
365+
310366
class BigPayloadResource {
311367
public:
312368
BigPayloadResource() {
@@ -408,10 +464,11 @@ Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app
408464
return -1;
409465
}
410466
411-
// we create our button and LED resources
467+
// we create our button, timer and LED resources
412468
ButtonResource button_resource;
413469
LedResource led_resource;
414470
BigPayloadResource big_payload_resource;
471+
TimerResource timer_resource;
415472
416473
// Network interaction must be performed outside of interrupt context
417474
Semaphore updates(0);
@@ -434,17 +491,18 @@ Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app
434491
object_list.push_back(button_resource.get_object());
435492
object_list.push_back(led_resource.get_object());
436493
object_list.push_back(big_payload_resource.get_object());
494+
object_list.push_back(timer_resource.get_object());
437495
438496
// Set endpoint registration object
439497
mbed_client.set_register_object(register_object);
440498
441499
// Register with mbed Device Connector
442500
mbed_client.test_register(register_object, object_list);
443-
interaction_provider.registered = true;
501+
volatile bool registered = true;
444502
445503
while (true) {
446504
updates.wait(25000);
447-
if(interaction_provider.registered) {
505+
if(registered) {
448506
if(!interaction_provider.clicked) {
449507
mbed_client.test_update_register();
450508
}
@@ -455,6 +513,10 @@ Add MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY in mbed_app
455513
interaction_provider.clicked = false;
456514
button_resource.handle_button_click();
457515
}
516+
if(interaction_provider.timer_ticked) {
517+
interaction_provider.timer_ticked = false;
518+
timer_resource.handle_timer_tick();
519+
}
458520
}
459521
460522
mbed_client.test_unregister();

0 commit comments

Comments
 (0)