Nigel Rantor / azure_c_shared_utility

Fork of azure_c_shared_utility by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tickcounter_mbed.cpp Source File

tickcounter_mbed.cpp

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include "mbed.h"
00005 #include "azure_c_shared_utility/optimize_size.h"
00006 #include "azure_c_shared_utility/tickcounter.h"
00007 
00008 class TICK_COUNTER_INSTANCE_TAG
00009 {
00010 public:
00011     clock_t last_clock_value;
00012     tickcounter_ms_t current_ms;
00013 };
00014 
00015 TICK_COUNTER_HANDLE tickcounter_create(void)
00016 {
00017     TICK_COUNTER_INSTANCE_TAG* result;
00018     result = new TICK_COUNTER_INSTANCE_TAG();
00019     result->last_clock_value = clock();
00020     result->current_ms = result->last_clock_value * 1000 / CLOCKS_PER_SEC;
00021     return result;
00022 }
00023 
00024 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
00025 {
00026     if (tick_counter != NULL)
00027     {
00028         delete tick_counter;
00029     }
00030 }
00031 
00032 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms)
00033 {
00034     int result;
00035     if (tick_counter == NULL || current_ms == NULL)
00036     {
00037         result = __FAILURE__;
00038     }
00039     else
00040     {
00041         TICK_COUNTER_INSTANCE_TAG* tick_counter_instance = (TICK_COUNTER_INSTANCE_TAG*)tick_counter;
00042 
00043         clock_t clock_value = clock();
00044 
00045         tick_counter_instance->current_ms += (clock_value - tick_counter_instance->last_clock_value) * 1000 / CLOCKS_PER_SEC;
00046         tick_counter_instance->last_clock_value = clock_value;
00047         *current_ms = tick_counter_instance->current_ms;
00048 
00049         result = 0;
00050     }
00051     return result;
00052 }