Delta / Mbed OS Delta_BLE_LEDBlinker

Fork of BLE_LEDBlinker by Bluetooth Low Energy

Committer:
tsungta
Date:
Fri Dec 09 09:12:57 2016 +0000
Revision:
12:f0ffc006e62d
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsungta 12:f0ffc006e62d 1 /* events
tsungta 12:f0ffc006e62d 2 * Copyright (c) 2006-2013 ARM Limited
tsungta 12:f0ffc006e62d 3 *
tsungta 12:f0ffc006e62d 4 * Licensed under the Apache License, Version 2.0 (the "License");
tsungta 12:f0ffc006e62d 5 * you may not use this file except in compliance with the License.
tsungta 12:f0ffc006e62d 6 * You may obtain a copy of the License at
tsungta 12:f0ffc006e62d 7 *
tsungta 12:f0ffc006e62d 8 * http://www.apache.org/licenses/LICENSE-2.0
tsungta 12:f0ffc006e62d 9 *
tsungta 12:f0ffc006e62d 10 * Unless required by applicable law or agreed to in writing, software
tsungta 12:f0ffc006e62d 11 * distributed under the License is distributed on an "AS IS" BASIS,
tsungta 12:f0ffc006e62d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tsungta 12:f0ffc006e62d 13 * See the License for the specific language governing permissions and
tsungta 12:f0ffc006e62d 14 * limitations under the License.
tsungta 12:f0ffc006e62d 15 */
tsungta 12:f0ffc006e62d 16 #ifndef EVENT_QUEUE_H
tsungta 12:f0ffc006e62d 17 #define EVENT_QUEUE_H
tsungta 12:f0ffc006e62d 18
tsungta 12:f0ffc006e62d 19 #include "events-c/events.h"
tsungta 12:f0ffc006e62d 20 #include "Callback.h"
tsungta 12:f0ffc006e62d 21 #include <cstddef>
tsungta 12:f0ffc006e62d 22 #include <new>
tsungta 12:f0ffc006e62d 23
tsungta 12:f0ffc006e62d 24 namespace events {
tsungta 12:f0ffc006e62d 25
tsungta 12:f0ffc006e62d 26
tsungta 12:f0ffc006e62d 27 /** DEFAULT_QUEUE_SIZE
tsungta 12:f0ffc006e62d 28 * default size of buffer for events
tsungta 12:f0ffc006e62d 29 */
tsungta 12:f0ffc006e62d 30 #define DEFAULT_QUEUE_SIZE \
tsungta 12:f0ffc006e62d 31 (32*(sizeof(struct event) + sizeof(mbed::Callback<void()>)))
tsungta 12:f0ffc006e62d 32
tsungta 12:f0ffc006e62d 33
tsungta 12:f0ffc006e62d 34 /** EventQueue
tsungta 12:f0ffc006e62d 35 *
tsungta 12:f0ffc006e62d 36 * Flexible event queue
tsungta 12:f0ffc006e62d 37 */
tsungta 12:f0ffc006e62d 38 class EventQueue {
tsungta 12:f0ffc006e62d 39 public:
tsungta 12:f0ffc006e62d 40 /** Create an event queue
tsungta 12:f0ffc006e62d 41 *
tsungta 12:f0ffc006e62d 42 * @param queue_size Size of buffer to use for events
tsungta 12:f0ffc006e62d 43 * (default: DEFAULT_QUEUE_SIZE)
tsungta 12:f0ffc006e62d 44 * @param queue_pointer Pointer to memory region to use for events
tsungta 12:f0ffc006e62d 45 * (default: NULL)
tsungta 12:f0ffc006e62d 46 */
tsungta 12:f0ffc006e62d 47 EventQueue(unsigned queue_size=DEFAULT_QUEUE_SIZE,
tsungta 12:f0ffc006e62d 48 unsigned char *queue_pointer=NULL);
tsungta 12:f0ffc006e62d 49
tsungta 12:f0ffc006e62d 50 /** Destroy an event queue
tsungta 12:f0ffc006e62d 51 */
tsungta 12:f0ffc006e62d 52 ~EventQueue();
tsungta 12:f0ffc006e62d 53
tsungta 12:f0ffc006e62d 54 /** Dispatch pending events
tsungta 12:f0ffc006e62d 55 * @param ms Time to wait for events in milliseconds, 0 will return
tsungta 12:f0ffc006e62d 56 * immediately if no events are pending, a negative
tsungta 12:f0ffc006e62d 57 * value will dispatch events forever
tsungta 12:f0ffc006e62d 58 * (default: -1)
tsungta 12:f0ffc006e62d 59 */
tsungta 12:f0ffc006e62d 60 void dispatch(int ms=-1);
tsungta 12:f0ffc006e62d 61
tsungta 12:f0ffc006e62d 62 /* Monotonic counter for the event queue
tsungta 12:f0ffc006e62d 63 * @return A monotonically incrementing counter in milliseconds
tsungta 12:f0ffc006e62d 64 * this count intentionally overflows to 0 after 2^32-1
tsungta 12:f0ffc006e62d 65 */
tsungta 12:f0ffc006e62d 66 unsigned get_tick();
tsungta 12:f0ffc006e62d 67
tsungta 12:f0ffc006e62d 68 /** Cancel events that are in flight
tsungta 12:f0ffc006e62d 69 *
tsungta 12:f0ffc006e62d 70 * If event has already been dispatched or does not exist, no error occurs.
tsungta 12:f0ffc006e62d 71 *
tsungta 12:f0ffc006e62d 72 * @param id Event id to cancel
tsungta 12:f0ffc006e62d 73 * @note This can not stop a currently executing event
tsungta 12:f0ffc006e62d 74 */
tsungta 12:f0ffc006e62d 75 void cancel(int id);
tsungta 12:f0ffc006e62d 76
tsungta 12:f0ffc006e62d 77 /** Post an event to the queue
tsungta 12:f0ffc006e62d 78 *
tsungta 12:f0ffc006e62d 79 * @param f Function to call on event dispatch
tsungta 12:f0ffc006e62d 80 * @param a0..a4 Arguments to pass to the callback
tsungta 12:f0ffc006e62d 81 * @return A positive id representing the event in the queue,
tsungta 12:f0ffc006e62d 82 * or 0 on failure
tsungta 12:f0ffc006e62d 83 */
tsungta 12:f0ffc006e62d 84 template <typename F>
tsungta 12:f0ffc006e62d 85 int post(F f) {
tsungta 12:f0ffc006e62d 86 void *p = event_alloc(&_equeue, sizeof(F));
tsungta 12:f0ffc006e62d 87 if (!p) {
tsungta 12:f0ffc006e62d 88 return 0;
tsungta 12:f0ffc006e62d 89 }
tsungta 12:f0ffc006e62d 90
tsungta 12:f0ffc006e62d 91 F *e = new (p) F(f);
tsungta 12:f0ffc006e62d 92 event_dtor(e, &EventQueue::dtor<F>);
tsungta 12:f0ffc006e62d 93 return event_post(&_equeue, &EventQueue::call<F>, e);
tsungta 12:f0ffc006e62d 94 }
tsungta 12:f0ffc006e62d 95
tsungta 12:f0ffc006e62d 96 template <typename F, typename A0>
tsungta 12:f0ffc006e62d 97 int post(F f, A0 a0) {
tsungta 12:f0ffc006e62d 98 return post(Context1<F,A0>(f,a0));
tsungta 12:f0ffc006e62d 99 }
tsungta 12:f0ffc006e62d 100
tsungta 12:f0ffc006e62d 101 template <typename F, typename A0, typename A1>
tsungta 12:f0ffc006e62d 102 int post(F f, A0 a0, A1 a1) {
tsungta 12:f0ffc006e62d 103 return post(Context2<F,A0,A1>(f,a0,a1));
tsungta 12:f0ffc006e62d 104 }
tsungta 12:f0ffc006e62d 105
tsungta 12:f0ffc006e62d 106 template <typename F, typename A0, typename A1, typename A2>
tsungta 12:f0ffc006e62d 107 int post(F f, A0 a0, A1 a1, A2 a2) {
tsungta 12:f0ffc006e62d 108 return post(Context3<F,A0,A1,A2>(f,a0,a1,a2));
tsungta 12:f0ffc006e62d 109 }
tsungta 12:f0ffc006e62d 110
tsungta 12:f0ffc006e62d 111 template <typename F, typename A0, typename A1, typename A2, typename A3>
tsungta 12:f0ffc006e62d 112 int post(F f, A0 a0, A1 a1, A2 a2, A3 a3) {
tsungta 12:f0ffc006e62d 113 return post(Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
tsungta 12:f0ffc006e62d 114 }
tsungta 12:f0ffc006e62d 115
tsungta 12:f0ffc006e62d 116 template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
tsungta 12:f0ffc006e62d 117 int post(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
tsungta 12:f0ffc006e62d 118 return post(Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
tsungta 12:f0ffc006e62d 119 }
tsungta 12:f0ffc006e62d 120
tsungta 12:f0ffc006e62d 121 /** Post an event to the queue after a specified delay
tsungta 12:f0ffc006e62d 122 *
tsungta 12:f0ffc006e62d 123 * @param f Function to call on event dispatch
tsungta 12:f0ffc006e62d 124 * @param a0..a4 Arguments to pass to the callback
tsungta 12:f0ffc006e62d 125 * @param ms Time to delay in milliseconds
tsungta 12:f0ffc006e62d 126 * @return A positive id representing the event in the queue,
tsungta 12:f0ffc006e62d 127 * or 0 on failure
tsungta 12:f0ffc006e62d 128 */
tsungta 12:f0ffc006e62d 129 template <typename F>
tsungta 12:f0ffc006e62d 130 int post_in(int ms, F f) {
tsungta 12:f0ffc006e62d 131 void *p = event_alloc(&_equeue, sizeof(F));
tsungta 12:f0ffc006e62d 132 if (!p) {
tsungta 12:f0ffc006e62d 133 return 0;
tsungta 12:f0ffc006e62d 134 }
tsungta 12:f0ffc006e62d 135
tsungta 12:f0ffc006e62d 136 F *e = new (p) F(f);
tsungta 12:f0ffc006e62d 137 event_delay(e, ms);
tsungta 12:f0ffc006e62d 138 event_dtor(e, &EventQueue::dtor<F>);
tsungta 12:f0ffc006e62d 139 return event_post(&_equeue, &EventQueue::call<F>, e);
tsungta 12:f0ffc006e62d 140 }
tsungta 12:f0ffc006e62d 141
tsungta 12:f0ffc006e62d 142 template <typename F, typename A0>
tsungta 12:f0ffc006e62d 143 int post_in(int ms, F f, A0 a0) {
tsungta 12:f0ffc006e62d 144 return post_in(ms, Context1<F,A0>(f,a0));
tsungta 12:f0ffc006e62d 145 }
tsungta 12:f0ffc006e62d 146
tsungta 12:f0ffc006e62d 147 template <typename F, typename A0, typename A1>
tsungta 12:f0ffc006e62d 148 int post_in(int ms, F f, A0 a0, A1 a1) {
tsungta 12:f0ffc006e62d 149 return post_in(ms, Context2<F,A0,A1>(f,a0,a1));
tsungta 12:f0ffc006e62d 150 }
tsungta 12:f0ffc006e62d 151
tsungta 12:f0ffc006e62d 152 template <typename F, typename A0, typename A1, typename A2>
tsungta 12:f0ffc006e62d 153 int post_in(int ms, F f, A0 a0, A1 a1, A2 a2) {
tsungta 12:f0ffc006e62d 154 return post_in(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
tsungta 12:f0ffc006e62d 155 }
tsungta 12:f0ffc006e62d 156
tsungta 12:f0ffc006e62d 157 template <typename F, typename A0, typename A1, typename A2, typename A3>
tsungta 12:f0ffc006e62d 158 int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
tsungta 12:f0ffc006e62d 159 return post_in(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
tsungta 12:f0ffc006e62d 160 }
tsungta 12:f0ffc006e62d 161
tsungta 12:f0ffc006e62d 162 template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
tsungta 12:f0ffc006e62d 163 int post_in(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
tsungta 12:f0ffc006e62d 164 return post_in(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
tsungta 12:f0ffc006e62d 165 }
tsungta 12:f0ffc006e62d 166
tsungta 12:f0ffc006e62d 167 /** Post an event to the queue periodically
tsungta 12:f0ffc006e62d 168 *
tsungta 12:f0ffc006e62d 169 * @param f Function to call on event dispatch
tsungta 12:f0ffc006e62d 170 * @param a0..a4 Arguments to pass to the callback
tsungta 12:f0ffc006e62d 171 * @param ms Period of the event in milliseconds
tsungta 12:f0ffc006e62d 172 * @return A positive id representing the event in the queue,
tsungta 12:f0ffc006e62d 173 * or 0 on failure
tsungta 12:f0ffc006e62d 174 */
tsungta 12:f0ffc006e62d 175 template <typename F>
tsungta 12:f0ffc006e62d 176 int post_every(int ms, F f) {
tsungta 12:f0ffc006e62d 177 void *p = event_alloc(&_equeue, sizeof(F));
tsungta 12:f0ffc006e62d 178 if (!p) {
tsungta 12:f0ffc006e62d 179 return 0;
tsungta 12:f0ffc006e62d 180 }
tsungta 12:f0ffc006e62d 181
tsungta 12:f0ffc006e62d 182 F *e = new (p) F(f);
tsungta 12:f0ffc006e62d 183 event_delay(e, ms);
tsungta 12:f0ffc006e62d 184 event_period(e, ms);
tsungta 12:f0ffc006e62d 185 event_dtor(e, &EventQueue::dtor<F>);
tsungta 12:f0ffc006e62d 186 return event_post(&_equeue, &EventQueue::call<F>, e);
tsungta 12:f0ffc006e62d 187 }
tsungta 12:f0ffc006e62d 188
tsungta 12:f0ffc006e62d 189 template <typename F, typename A0>
tsungta 12:f0ffc006e62d 190 int post_every(int ms, F f, A0 a0) {
tsungta 12:f0ffc006e62d 191 return post_every(ms, Context1<F,A0>(f,a0));
tsungta 12:f0ffc006e62d 192 }
tsungta 12:f0ffc006e62d 193
tsungta 12:f0ffc006e62d 194 template <typename F, typename A0, typename A1>
tsungta 12:f0ffc006e62d 195 int post_every(int ms, F f, A0 a0, A1 a1) {
tsungta 12:f0ffc006e62d 196 return post_every(ms, Context2<F,A0,A1>(f,a0,a1));
tsungta 12:f0ffc006e62d 197 }
tsungta 12:f0ffc006e62d 198
tsungta 12:f0ffc006e62d 199 template <typename F, typename A0, typename A1, typename A2>
tsungta 12:f0ffc006e62d 200 int post_every(int ms, F f, A0 a0, A1 a1, A2 a2) {
tsungta 12:f0ffc006e62d 201 return post_every(ms, Context3<F,A0,A1,A2>(f,a0,a1,a2));
tsungta 12:f0ffc006e62d 202 }
tsungta 12:f0ffc006e62d 203
tsungta 12:f0ffc006e62d 204 template <typename F, typename A0, typename A1, typename A2, typename A3>
tsungta 12:f0ffc006e62d 205 int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3) {
tsungta 12:f0ffc006e62d 206 return post_every(ms, Context4<F,A0,A1,A2,A3>(f,a0,a1,a2,a3));
tsungta 12:f0ffc006e62d 207 }
tsungta 12:f0ffc006e62d 208
tsungta 12:f0ffc006e62d 209 template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
tsungta 12:f0ffc006e62d 210 int post_every(int ms, F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
tsungta 12:f0ffc006e62d 211 return post_every(ms, Context5<F,A0,A1,A2,A3,A4>(f,a0,a1,a2,a3,a4));
tsungta 12:f0ffc006e62d 212 }
tsungta 12:f0ffc006e62d 213
tsungta 12:f0ffc006e62d 214 protected:
tsungta 12:f0ffc006e62d 215 void break_();
tsungta 12:f0ffc006e62d 216
tsungta 12:f0ffc006e62d 217 struct equeue _equeue;
tsungta 12:f0ffc006e62d 218
tsungta 12:f0ffc006e62d 219 template <typename F, typename A0, typename A1, typename A2, typename A3, typename A4>
tsungta 12:f0ffc006e62d 220 struct Context5 {
tsungta 12:f0ffc006e62d 221 F f; A0 a0; A1 a1; A2 a2; A3 a3; A4 a4;
tsungta 12:f0ffc006e62d 222
tsungta 12:f0ffc006e62d 223 Context5(F f, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
tsungta 12:f0ffc006e62d 224 : f(f), a0(a0), a1(a1), a2(a2), a3(a3), a4(a4) {}
tsungta 12:f0ffc006e62d 225
tsungta 12:f0ffc006e62d 226 void operator()() {
tsungta 12:f0ffc006e62d 227 f(a0, a1, a2, a3, a4);
tsungta 12:f0ffc006e62d 228 }
tsungta 12:f0ffc006e62d 229 };
tsungta 12:f0ffc006e62d 230
tsungta 12:f0ffc006e62d 231 template <typename F, typename A0, typename A1, typename A2, typename A3>
tsungta 12:f0ffc006e62d 232 struct Context4 {
tsungta 12:f0ffc006e62d 233 F f; A0 a0; A1 a1; A2 a2; A3 a3;
tsungta 12:f0ffc006e62d 234
tsungta 12:f0ffc006e62d 235 Context4(F f, A0 a0, A1 a1, A2 a2, A3 a3)
tsungta 12:f0ffc006e62d 236 : f(f), a0(a0), a1(a1), a2(a2), a3(a3) {}
tsungta 12:f0ffc006e62d 237
tsungta 12:f0ffc006e62d 238 void operator()() {
tsungta 12:f0ffc006e62d 239 f(a0, a1, a2, a3);
tsungta 12:f0ffc006e62d 240 }
tsungta 12:f0ffc006e62d 241 };
tsungta 12:f0ffc006e62d 242
tsungta 12:f0ffc006e62d 243 template <typename F, typename A0, typename A1, typename A2>
tsungta 12:f0ffc006e62d 244 struct Context3 {
tsungta 12:f0ffc006e62d 245 F f; A0 a0; A1 a1; A2 a2;
tsungta 12:f0ffc006e62d 246
tsungta 12:f0ffc006e62d 247 Context3(F f, A0 a0, A1 a1, A2 a2)
tsungta 12:f0ffc006e62d 248 : f(f), a0(a0), a1(a1), a2(a2) {}
tsungta 12:f0ffc006e62d 249
tsungta 12:f0ffc006e62d 250 void operator()() {
tsungta 12:f0ffc006e62d 251 f(a0, a1, a2);
tsungta 12:f0ffc006e62d 252 }
tsungta 12:f0ffc006e62d 253 };
tsungta 12:f0ffc006e62d 254
tsungta 12:f0ffc006e62d 255 template <typename F, typename A0, typename A1>
tsungta 12:f0ffc006e62d 256 struct Context2 {
tsungta 12:f0ffc006e62d 257 F f; A0 a0; A1 a1;
tsungta 12:f0ffc006e62d 258
tsungta 12:f0ffc006e62d 259 Context2(F f, A0 a0, A1 a1)
tsungta 12:f0ffc006e62d 260 : f(f), a0(a0), a1(a1) {}
tsungta 12:f0ffc006e62d 261
tsungta 12:f0ffc006e62d 262 void operator()() {
tsungta 12:f0ffc006e62d 263 f(a0, a1);
tsungta 12:f0ffc006e62d 264 }
tsungta 12:f0ffc006e62d 265 };
tsungta 12:f0ffc006e62d 266
tsungta 12:f0ffc006e62d 267 template <typename F, typename A0>
tsungta 12:f0ffc006e62d 268 struct Context1 {
tsungta 12:f0ffc006e62d 269 F f; A0 a0;
tsungta 12:f0ffc006e62d 270
tsungta 12:f0ffc006e62d 271 Context1(F f, A0 a0)
tsungta 12:f0ffc006e62d 272 : f(f), a0(a0) {}
tsungta 12:f0ffc006e62d 273
tsungta 12:f0ffc006e62d 274 void operator()() {
tsungta 12:f0ffc006e62d 275 f(a0);
tsungta 12:f0ffc006e62d 276 }
tsungta 12:f0ffc006e62d 277 };
tsungta 12:f0ffc006e62d 278
tsungta 12:f0ffc006e62d 279 template <typename T>
tsungta 12:f0ffc006e62d 280 static void call(void *p) {
tsungta 12:f0ffc006e62d 281 (*static_cast<T*>(p))();
tsungta 12:f0ffc006e62d 282 }
tsungta 12:f0ffc006e62d 283
tsungta 12:f0ffc006e62d 284 template <typename T>
tsungta 12:f0ffc006e62d 285 static void dtor(void *p) {
tsungta 12:f0ffc006e62d 286 static_cast<T*>(p)->~T();
tsungta 12:f0ffc006e62d 287 }
tsungta 12:f0ffc006e62d 288 };
tsungta 12:f0ffc006e62d 289
tsungta 12:f0ffc006e62d 290
tsungta 12:f0ffc006e62d 291 }
tsungta 12:f0ffc006e62d 292
tsungta 12:f0ffc006e62d 293 #endif
tsungta 12:f0ffc006e62d 294