Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EventQueueMinar.h Source File

EventQueueMinar.h

00001 /*
00002  * Copyright (c) 2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef EVENTQUEUE_EVENTQUEUEMINAR_H_
00018 #define EVENTQUEUE_EVENTQUEUEMINAR_H_
00019 
00020 #include <minar/minar.h>
00021 #include "EventQueue.h"
00022 
00023 namespace eq {
00024 
00025 class EventQueueMinar: public EventQueue {
00026 
00027 public:
00028     /// Construct an empty event queue
00029     EventQueueMinar()  { }
00030 
00031     virtual ~EventQueueMinar() { }
00032 
00033     virtual bool cancel(event_handle_t event_handle) {
00034         return minar::Scheduler::cancelCallback(event_handle);
00035     }
00036 
00037 private:
00038 
00039     virtual event_handle_t do_post(const function_t& fn, ms_time_t ms_delay = 0, bool repeat = false) {
00040         // convert ms to minar time
00041         minar::tick_t tick = minar::milliseconds(ms_delay);
00042 
00043         // convert thunk to minar FunctionPointerBind
00044         mbed::util::Event func(
00045             mbed::util::FunctionPointer1<void, function_t>(
00046                 free_func_thunk_call
00047             ).bind(fn)
00048         );
00049 
00050         if (ms_delay == 0) {
00051             return minar::Scheduler::postCallback(func).getHandle();
00052         }
00053 
00054         if (repeat == false) {
00055             return minar::Scheduler::postCallback(func).delay(tick).tolerance(0).getHandle();
00056         } else {
00057             return minar::Scheduler::postCallback(func).period(tick).tolerance(0).getHandle();
00058         }
00059     }
00060 
00061     // due to design limitations in function pointer classes, it is not possible
00062     // to use reference here ... 
00063     static void free_func_thunk_call(function_t fn) {
00064         fn();
00065     }
00066 };
00067 
00068 } // namespace eq
00069 
00070 #endif /* EVENTQUEUE_EVENTQUEUEMINAR_H_ */