AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
HRM1017/nordic/app_common/app_scheduler.c@7:505a9a98b776, 2015-03-14 (annotated)
- Committer:
- lipoyang
- Date:
- Sat Mar 14 11:51:38 2015 +0000
- Revision:
- 7:505a9a98b776
- Parent:
- 4:ebda47d22091
- adjust servo center position
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 1:48f6e08a3ac2 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
jksoft | 1:48f6e08a3ac2 | 2 | * |
jksoft | 1:48f6e08a3ac2 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
jksoft | 1:48f6e08a3ac2 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
jksoft | 1:48f6e08a3ac2 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
jksoft | 1:48f6e08a3ac2 | 6 | * |
jksoft | 1:48f6e08a3ac2 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
jksoft | 1:48f6e08a3ac2 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jksoft | 1:48f6e08a3ac2 | 9 | * the file. |
jksoft | 1:48f6e08a3ac2 | 10 | * |
jksoft | 1:48f6e08a3ac2 | 11 | */ |
jksoft | 1:48f6e08a3ac2 | 12 | |
jksoft | 1:48f6e08a3ac2 | 13 | #include "app_scheduler.h" |
jksoft | 1:48f6e08a3ac2 | 14 | #include <stdlib.h> |
jksoft | 1:48f6e08a3ac2 | 15 | #include <stdint.h> |
jksoft | 1:48f6e08a3ac2 | 16 | #include <string.h> |
jksoft | 1:48f6e08a3ac2 | 17 | #include "nrf_soc.h" |
jksoft | 1:48f6e08a3ac2 | 18 | #include "nrf_assert.h" |
jksoft | 1:48f6e08a3ac2 | 19 | #include "app_util.h" |
jksoft | 1:48f6e08a3ac2 | 20 | #include "app_util_platform.h" |
jksoft | 1:48f6e08a3ac2 | 21 | |
jksoft | 1:48f6e08a3ac2 | 22 | /**@brief Structure for holding a scheduled event header. */ |
jksoft | 1:48f6e08a3ac2 | 23 | typedef struct |
jksoft | 1:48f6e08a3ac2 | 24 | { |
jksoft | 1:48f6e08a3ac2 | 25 | app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */ |
jksoft | 1:48f6e08a3ac2 | 26 | uint16_t event_data_size; /**< Size of event data. */ |
jksoft | 1:48f6e08a3ac2 | 27 | } event_header_t; |
jksoft | 1:48f6e08a3ac2 | 28 | |
jksoft | 1:48f6e08a3ac2 | 29 | STATIC_ASSERT(sizeof(event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE); |
jksoft | 1:48f6e08a3ac2 | 30 | |
jksoft | 1:48f6e08a3ac2 | 31 | static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */ |
jksoft | 1:48f6e08a3ac2 | 32 | static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */ |
jksoft | 1:48f6e08a3ac2 | 33 | static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */ |
jksoft | 1:48f6e08a3ac2 | 34 | static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */ |
jksoft | 1:48f6e08a3ac2 | 35 | static uint16_t m_queue_event_size; /**< Maximum event size in queue. */ |
jksoft | 1:48f6e08a3ac2 | 36 | static uint16_t m_queue_size; /**< Number of queue entries. */ |
jksoft | 1:48f6e08a3ac2 | 37 | |
jksoft | 1:48f6e08a3ac2 | 38 | /**@brief Macro for checking if a queue is full. */ |
jksoft | 1:48f6e08a3ac2 | 39 | #define APP_SCHED_QUEUE_FULL() (next_index(m_queue_end_index) == m_queue_start_index) |
jksoft | 1:48f6e08a3ac2 | 40 | |
jksoft | 1:48f6e08a3ac2 | 41 | /**@brief Macro for checking if a queue is empty. */ |
jksoft | 1:48f6e08a3ac2 | 42 | #define APP_SCHED_QUEUE_EMPTY() (m_queue_end_index == m_queue_start_index) |
jksoft | 1:48f6e08a3ac2 | 43 | |
jksoft | 1:48f6e08a3ac2 | 44 | |
jksoft | 1:48f6e08a3ac2 | 45 | /**@brief Function for incrementing a queue index, and handle wrap-around. |
jksoft | 1:48f6e08a3ac2 | 46 | * |
jksoft | 1:48f6e08a3ac2 | 47 | * @param[in] index Old index. |
jksoft | 1:48f6e08a3ac2 | 48 | * |
jksoft | 1:48f6e08a3ac2 | 49 | * @return New (incremented) index. |
jksoft | 1:48f6e08a3ac2 | 50 | */ |
jksoft | 1:48f6e08a3ac2 | 51 | static __INLINE uint8_t next_index(uint8_t index) |
jksoft | 1:48f6e08a3ac2 | 52 | { |
jksoft | 1:48f6e08a3ac2 | 53 | return (index < m_queue_size) ? (index + 1) : 0; |
jksoft | 1:48f6e08a3ac2 | 54 | } |
jksoft | 1:48f6e08a3ac2 | 55 | |
jksoft | 1:48f6e08a3ac2 | 56 | |
jksoft | 1:48f6e08a3ac2 | 57 | uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer) |
jksoft | 1:48f6e08a3ac2 | 58 | { |
jksoft | 1:48f6e08a3ac2 | 59 | uint16_t data_start_index = (queue_size + 1) * sizeof(event_header_t); |
jksoft | 1:48f6e08a3ac2 | 60 | |
jksoft | 1:48f6e08a3ac2 | 61 | // Check that buffer is correctly aligned |
jksoft | 1:48f6e08a3ac2 | 62 | if (!is_word_aligned(p_event_buffer)) |
jksoft | 1:48f6e08a3ac2 | 63 | { |
jksoft | 1:48f6e08a3ac2 | 64 | return NRF_ERROR_INVALID_PARAM; |
jksoft | 1:48f6e08a3ac2 | 65 | } |
jksoft | 1:48f6e08a3ac2 | 66 | |
jksoft | 1:48f6e08a3ac2 | 67 | // Initialize event scheduler |
jksoft | 1:48f6e08a3ac2 | 68 | m_queue_event_headers = p_event_buffer; |
jksoft | 1:48f6e08a3ac2 | 69 | m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index]; |
jksoft | 1:48f6e08a3ac2 | 70 | m_queue_end_index = 0; |
jksoft | 1:48f6e08a3ac2 | 71 | m_queue_start_index = 0; |
jksoft | 1:48f6e08a3ac2 | 72 | m_queue_event_size = event_size; |
jksoft | 1:48f6e08a3ac2 | 73 | m_queue_size = queue_size; |
jksoft | 1:48f6e08a3ac2 | 74 | |
jksoft | 1:48f6e08a3ac2 | 75 | return NRF_SUCCESS; |
jksoft | 1:48f6e08a3ac2 | 76 | } |
jksoft | 1:48f6e08a3ac2 | 77 | |
jksoft | 1:48f6e08a3ac2 | 78 | |
jksoft | 1:48f6e08a3ac2 | 79 | uint32_t app_sched_event_put(void * p_event_data, |
jksoft | 1:48f6e08a3ac2 | 80 | uint16_t event_data_size, |
jksoft | 1:48f6e08a3ac2 | 81 | app_sched_event_handler_t handler) |
jksoft | 1:48f6e08a3ac2 | 82 | { |
jksoft | 1:48f6e08a3ac2 | 83 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 84 | |
jksoft | 1:48f6e08a3ac2 | 85 | if (event_data_size <= m_queue_event_size) |
jksoft | 1:48f6e08a3ac2 | 86 | { |
jksoft | 1:48f6e08a3ac2 | 87 | uint16_t event_index = 0xFFFF; |
jksoft | 1:48f6e08a3ac2 | 88 | |
jksoft | 1:48f6e08a3ac2 | 89 | CRITICAL_REGION_ENTER(); |
jksoft | 1:48f6e08a3ac2 | 90 | |
jksoft | 1:48f6e08a3ac2 | 91 | if (!APP_SCHED_QUEUE_FULL()) |
jksoft | 1:48f6e08a3ac2 | 92 | { |
jksoft | 1:48f6e08a3ac2 | 93 | event_index = m_queue_end_index; |
jksoft | 1:48f6e08a3ac2 | 94 | m_queue_end_index = next_index(m_queue_end_index); |
jksoft | 1:48f6e08a3ac2 | 95 | } |
jksoft | 1:48f6e08a3ac2 | 96 | |
jksoft | 1:48f6e08a3ac2 | 97 | CRITICAL_REGION_EXIT(); |
jksoft | 1:48f6e08a3ac2 | 98 | |
jksoft | 1:48f6e08a3ac2 | 99 | if (event_index != 0xFFFF) |
jksoft | 1:48f6e08a3ac2 | 100 | { |
jksoft | 1:48f6e08a3ac2 | 101 | // NOTE: This can be done outside the critical region since the event consumer will |
jksoft | 1:48f6e08a3ac2 | 102 | // always be called from the main loop, and will thus never interrupt this code. |
jksoft | 1:48f6e08a3ac2 | 103 | m_queue_event_headers[event_index].handler = handler; |
jksoft | 1:48f6e08a3ac2 | 104 | if ((p_event_data != NULL) && (event_data_size > 0)) |
jksoft | 1:48f6e08a3ac2 | 105 | { |
jksoft | 1:48f6e08a3ac2 | 106 | memcpy(&m_queue_event_data[event_index * m_queue_event_size], |
jksoft | 1:48f6e08a3ac2 | 107 | p_event_data, |
jksoft | 1:48f6e08a3ac2 | 108 | event_data_size); |
jksoft | 1:48f6e08a3ac2 | 109 | m_queue_event_headers[event_index].event_data_size = event_data_size; |
jksoft | 1:48f6e08a3ac2 | 110 | } |
jksoft | 1:48f6e08a3ac2 | 111 | else |
jksoft | 1:48f6e08a3ac2 | 112 | { |
jksoft | 1:48f6e08a3ac2 | 113 | m_queue_event_headers[event_index].event_data_size = 0; |
jksoft | 1:48f6e08a3ac2 | 114 | } |
jksoft | 1:48f6e08a3ac2 | 115 | |
jksoft | 1:48f6e08a3ac2 | 116 | err_code = NRF_SUCCESS; |
jksoft | 1:48f6e08a3ac2 | 117 | } |
jksoft | 1:48f6e08a3ac2 | 118 | else |
jksoft | 1:48f6e08a3ac2 | 119 | { |
jksoft | 1:48f6e08a3ac2 | 120 | err_code = NRF_ERROR_NO_MEM; |
jksoft | 1:48f6e08a3ac2 | 121 | } |
jksoft | 1:48f6e08a3ac2 | 122 | } |
jksoft | 1:48f6e08a3ac2 | 123 | else |
jksoft | 1:48f6e08a3ac2 | 124 | { |
jksoft | 1:48f6e08a3ac2 | 125 | err_code = NRF_ERROR_INVALID_LENGTH; |
jksoft | 1:48f6e08a3ac2 | 126 | } |
jksoft | 1:48f6e08a3ac2 | 127 | |
jksoft | 1:48f6e08a3ac2 | 128 | return err_code; |
jksoft | 1:48f6e08a3ac2 | 129 | } |
jksoft | 1:48f6e08a3ac2 | 130 | |
jksoft | 1:48f6e08a3ac2 | 131 | |
jksoft | 1:48f6e08a3ac2 | 132 | /**@brief Function for reading the next event from specified event queue. |
jksoft | 1:48f6e08a3ac2 | 133 | * |
jksoft | 1:48f6e08a3ac2 | 134 | * @param[out] pp_event_data Pointer to pointer to event data. |
jksoft | 1:48f6e08a3ac2 | 135 | * @param[out] p_event_data_size Pointer to size of event data. |
jksoft | 1:48f6e08a3ac2 | 136 | * @param[out] p_event_handler Pointer to event handler function pointer. |
jksoft | 1:48f6e08a3ac2 | 137 | * |
jksoft | 1:48f6e08a3ac2 | 138 | * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty. |
jksoft | 1:48f6e08a3ac2 | 139 | */ |
jksoft | 1:48f6e08a3ac2 | 140 | static uint32_t app_sched_event_get(void ** pp_event_data, |
jksoft | 1:48f6e08a3ac2 | 141 | uint16_t * p_event_data_size, |
jksoft | 1:48f6e08a3ac2 | 142 | app_sched_event_handler_t * p_event_handler) |
jksoft | 1:48f6e08a3ac2 | 143 | { |
jksoft | 1:48f6e08a3ac2 | 144 | uint32_t err_code = NRF_ERROR_NOT_FOUND; |
jksoft | 1:48f6e08a3ac2 | 145 | |
jksoft | 1:48f6e08a3ac2 | 146 | if (!APP_SCHED_QUEUE_EMPTY()) |
jksoft | 1:48f6e08a3ac2 | 147 | { |
jksoft | 1:48f6e08a3ac2 | 148 | uint16_t event_index; |
jksoft | 1:48f6e08a3ac2 | 149 | |
jksoft | 1:48f6e08a3ac2 | 150 | // NOTE: There is no need for a critical region here, as this function will only be called |
jksoft | 1:48f6e08a3ac2 | 151 | // from app_sched_execute() from inside the main loop, so it will never interrupt |
jksoft | 1:48f6e08a3ac2 | 152 | // app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be |
jksoft | 1:48f6e08a3ac2 | 153 | // an atomic operation. |
jksoft | 1:48f6e08a3ac2 | 154 | event_index = m_queue_start_index; |
jksoft | 1:48f6e08a3ac2 | 155 | m_queue_start_index = next_index(m_queue_start_index); |
jksoft | 1:48f6e08a3ac2 | 156 | |
jksoft | 1:48f6e08a3ac2 | 157 | *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size]; |
jksoft | 1:48f6e08a3ac2 | 158 | *p_event_data_size = m_queue_event_headers[event_index].event_data_size; |
jksoft | 1:48f6e08a3ac2 | 159 | *p_event_handler = m_queue_event_headers[event_index].handler; |
jksoft | 1:48f6e08a3ac2 | 160 | |
jksoft | 1:48f6e08a3ac2 | 161 | err_code = NRF_SUCCESS; |
jksoft | 1:48f6e08a3ac2 | 162 | } |
jksoft | 1:48f6e08a3ac2 | 163 | |
jksoft | 1:48f6e08a3ac2 | 164 | return err_code; |
jksoft | 1:48f6e08a3ac2 | 165 | } |
jksoft | 1:48f6e08a3ac2 | 166 | |
jksoft | 1:48f6e08a3ac2 | 167 | |
jksoft | 1:48f6e08a3ac2 | 168 | void app_sched_execute(void) |
jksoft | 1:48f6e08a3ac2 | 169 | { |
jksoft | 1:48f6e08a3ac2 | 170 | void * p_event_data; |
jksoft | 1:48f6e08a3ac2 | 171 | uint16_t event_data_size; |
jksoft | 1:48f6e08a3ac2 | 172 | app_sched_event_handler_t event_handler; |
jksoft | 1:48f6e08a3ac2 | 173 | |
jksoft | 1:48f6e08a3ac2 | 174 | // Get next event (if any), and execute handler |
jksoft | 1:48f6e08a3ac2 | 175 | while ((app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS)) |
jksoft | 1:48f6e08a3ac2 | 176 | { |
jksoft | 1:48f6e08a3ac2 | 177 | event_handler(p_event_data, event_data_size); |
jksoft | 1:48f6e08a3ac2 | 178 | } |
jksoft | 1:48f6e08a3ac2 | 179 | } |