MicroBit as BLE gamepad
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
source/core/MicroBitListener.cpp@75:df904445f561, 2017-01-30 (annotated)
- Committer:
- rengro01
- Date:
- Mon Jan 30 08:29:06 2017 +0000
- Revision:
- 75:df904445f561
- Parent:
- 1:8aa5cdb4ab67
MicroBit as BLE gamepad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
1:8aa5cdb4ab67 | 1 | /* |
Jonathan Austin |
1:8aa5cdb4ab67 | 2 | The MIT License (MIT) |
Jonathan Austin |
1:8aa5cdb4ab67 | 3 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
Jonathan Austin |
1:8aa5cdb4ab67 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
Jonathan Austin |
1:8aa5cdb4ab67 | 6 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
Jonathan Austin |
1:8aa5cdb4ab67 | 8 | copy of this software and associated documentation files (the "Software"), |
Jonathan Austin |
1:8aa5cdb4ab67 | 9 | to deal in the Software without restriction, including without limitation |
Jonathan Austin |
1:8aa5cdb4ab67 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Jonathan Austin |
1:8aa5cdb4ab67 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
Jonathan Austin |
1:8aa5cdb4ab67 | 12 | Software is furnished to do so, subject to the following conditions: |
Jonathan Austin |
1:8aa5cdb4ab67 | 13 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 14 | The above copyright notice and this permission notice shall be included in |
Jonathan Austin |
1:8aa5cdb4ab67 | 15 | all copies or substantial portions of the Software. |
Jonathan Austin |
1:8aa5cdb4ab67 | 16 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Jonathan Austin |
1:8aa5cdb4ab67 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Jonathan Austin |
1:8aa5cdb4ab67 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
Jonathan Austin |
1:8aa5cdb4ab67 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
Jonathan Austin |
1:8aa5cdb4ab67 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
Jonathan Austin |
1:8aa5cdb4ab67 | 23 | DEALINGS IN THE SOFTWARE. |
Jonathan Austin |
1:8aa5cdb4ab67 | 24 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 25 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 26 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | * This structure defines a MicroBitListener used to invoke functions, or member |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | * functions if an instance of EventModel receives an event whose id and value |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | * match this MicroBitListener's id and value. |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitListener.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | * Create a new Message Bus Listener. |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | * @param id The ID of the component you want to listen to. |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * @param value The event value you would like to listen to from that component |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | * @param handler A function pointer to call when the event is detected. |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | * @param flags User specified, implementation specific flags, that allow behaviour of this events listener |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | * to be tuned. |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent), uint16_t flags) |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | this->id = id; |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | this->value = value; |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | this->cb = handler; |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | this->cb_arg = NULL; |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | this->flags = flags; |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | this->next = NULL; |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | this->evt_queue = NULL; |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | * Create a new Message Bus Listener, this constructor accepts an additional |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | * parameter "arg", which is passed to the handler. |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | * @param id The ID of the component you want to listen to. |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * @param value The event value you would like to listen to from that component |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | * @param handler A function pointer to call when the event is detected. |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | * @param arg A pointer to some data that will be given to the handler. |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | * @param flags User specified, implementation specific flags, that allow behaviour of this events listener |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | * to be tuned. |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent, void *), void* arg, uint16_t flags) |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | this->id = id; |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | this->value = value; |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | this->cb_param = handler; |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | this->cb_arg = arg; |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | this->flags = flags | MESSAGE_BUS_LISTENER_PARAMETERISED; |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | this->next = NULL; |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | this->evt_queue = NULL; |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | * Destructor. Ensures all resources used by this listener are freed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | MicroBitListener::~MicroBitListener() |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | if(this->flags & MESSAGE_BUS_LISTENER_METHOD) |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | delete cb_method; |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * Queues and event up to be processed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | * @param e The event to queue |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | void MicroBitListener::queue(MicroBitEvent e) |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | int queueDepth; |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | MicroBitEventQueueItem *p = evt_queue; |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | if (evt_queue == NULL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | evt_queue = new MicroBitEventQueueItem(e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | queueDepth = 1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | while (p->next != NULL) |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | p = p->next; |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | queueDepth++; |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | if (queueDepth < MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH) |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | p->next = new MicroBitEventQueueItem(e); |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | } |