Skip to content

Commit 6cd2c84

Browse files
authored
Merge pull request #52 from ARMmbed/replace_button_with_timer
Add a timer for incrementing example value.
2 parents 0fb4b24 + 4bd5097 commit 6cd2c84

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

main.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,23 @@ static M2MResource* m2m_put_res;
3737
static M2MResource* m2m_post_res;
3838
static M2MResource* m2m_deregister_res;
3939

40+
EventQueue queue(32 * EVENTS_EVENT_SIZE);
41+
Thread t;
42+
Mutex value_increment_mutex;
43+
4044
void print_client_ids(void)
4145
{
4246
printf("Account ID: %s\n", cloud_client->endpoint_info()->account_id.c_str());
4347
printf("Endpoint name: %s\n", cloud_client->endpoint_info()->internal_endpoint_name.c_str());
4448
printf("Device ID: %s\n\n", cloud_client->endpoint_info()->endpoint_name.c_str());
4549
}
4650

47-
void button_press(void)
51+
void value_increment(void)
4852
{
53+
value_increment_mutex.lock();
4954
m2m_get_res->set_value(m2m_get_res->get_value_int() + 1);
5055
printf("Counter %" PRIu64 "\n", m2m_get_res->get_value_int());
56+
value_increment_mutex.unlock();
5157
}
5258

5359
void put_update(const char* /*object_name*/)
@@ -160,7 +166,7 @@ int main(void)
160166
// PUT resource 3201/0/5853
161167
m2m_put_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5853, M2MResourceInstance::INTEGER, M2MBase::GET_PUT_ALLOWED);
162168
if (m2m_put_res->set_value(0) != true) {
163-
printf("m2m_led_res->set_value() failed\n");
169+
printf("m2m_put_res->set_value() failed\n");
164170
return -1;
165171
}
166172
if (m2m_put_res->set_value_updated_function(put_update) != true) {
@@ -197,6 +203,9 @@ int main(void)
197203
cloud_client->add_objects(m2m_obj_list);
198204
cloud_client->setup(network); // cloud_client->setup(NULL); -- https://jira.arm.com/browse/IOTCLT-3114
199205

206+
t.start(callback(&queue, &EventQueue::dispatch_forever));
207+
queue.call_every(5000, value_increment);
208+
200209
while(cloud_client_running) {
201210
int in_char = getchar();
202211
if (in_char == 'i') {
@@ -208,7 +217,7 @@ int main(void)
208217
wait(1);
209218
NVIC_SystemReset();
210219
} else if (in_char > 0 && in_char != 0x03) { // Ctrl+C is 0x03 in Mbed OS and Linux returns negative number
211-
button_press(); // Simulate button press
220+
value_increment(); // Simulate button press
212221
continue;
213222
}
214223
deregister_client();

0 commit comments

Comments
 (0)