TEST
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Utilities/queue.cpp@1:f60eafbf009a, 2019-04-10 (annotated)
- Committer:
- gmehmet
- Date:
- Wed Apr 10 14:56:25 2019 +0300
- Revision:
- 1:f60eafbf009a
- Child:
- 3:2fe2ff1ca0dc
upload from local
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gmehmet | 1:f60eafbf009a | 1 | /******************************************************************************* |
gmehmet | 1:f60eafbf009a | 2 | * Author: Ismail Kose, Ismail.Kose@maximintegrated.com |
gmehmet | 1:f60eafbf009a | 3 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
gmehmet | 1:f60eafbf009a | 4 | * |
gmehmet | 1:f60eafbf009a | 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
gmehmet | 1:f60eafbf009a | 6 | * copy of this software and associated documentation files (the "Software"), |
gmehmet | 1:f60eafbf009a | 7 | * to deal in the Software without restriction, including without limitation |
gmehmet | 1:f60eafbf009a | 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
gmehmet | 1:f60eafbf009a | 9 | * and/or sell copies of the Software, and to permit persons to whom the |
gmehmet | 1:f60eafbf009a | 10 | * Software is furnished to do so, subject to the following conditions: |
gmehmet | 1:f60eafbf009a | 11 | * |
gmehmet | 1:f60eafbf009a | 12 | * The above copyright notice and this permission notice shall be included |
gmehmet | 1:f60eafbf009a | 13 | * in all copies or substantial portions of the Software. |
gmehmet | 1:f60eafbf009a | 14 | * |
gmehmet | 1:f60eafbf009a | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
gmehmet | 1:f60eafbf009a | 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
gmehmet | 1:f60eafbf009a | 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
gmehmet | 1:f60eafbf009a | 18 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
gmehmet | 1:f60eafbf009a | 19 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
gmehmet | 1:f60eafbf009a | 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
gmehmet | 1:f60eafbf009a | 21 | * OTHER DEALINGS IN THE SOFTWARE. |
gmehmet | 1:f60eafbf009a | 22 | * |
gmehmet | 1:f60eafbf009a | 23 | * Except as contained in this notice, the name of Maxim Integrated |
gmehmet | 1:f60eafbf009a | 24 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
gmehmet | 1:f60eafbf009a | 25 | * Products, Inc. Branding Policy. |
gmehmet | 1:f60eafbf009a | 26 | * |
gmehmet | 1:f60eafbf009a | 27 | * The mere transfer of this software does not imply any licenses |
gmehmet | 1:f60eafbf009a | 28 | * of trade secrets, proprietary technology, copyrights, patents, |
gmehmet | 1:f60eafbf009a | 29 | * trademarks, maskwork rights, or any other form of intellectual |
gmehmet | 1:f60eafbf009a | 30 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
gmehmet | 1:f60eafbf009a | 31 | * ownership rights. |
gmehmet | 1:f60eafbf009a | 32 | ******************************************************************************* |
gmehmet | 1:f60eafbf009a | 33 | */ |
gmehmet | 1:f60eafbf009a | 34 | |
gmehmet | 1:f60eafbf009a | 35 | /* |
gmehmet | 1:f60eafbf009a | 36 | * TODO: |
gmehmet | 1:f60eafbf009a | 37 | * Add a function to enqueue data block instead of one by one. |
gmehmet | 1:f60eafbf009a | 38 | * Write function definitions in the header file as doxygen format |
gmehmet | 1:f60eafbf009a | 39 | * Init function will also allocate memory for queue buffer, providing the buffer will not necessary |
gmehmet | 1:f60eafbf009a | 40 | * |
gmehmet | 1:f60eafbf009a | 41 | * */ |
gmehmet | 1:f60eafbf009a | 42 | |
gmehmet | 1:f60eafbf009a | 43 | #include "queue.h" |
gmehmet | 1:f60eafbf009a | 44 | #include "mbed.h" |
gmehmet | 1:f60eafbf009a | 45 | #include "Peripherals.h" |
gmehmet | 1:f60eafbf009a | 46 | |
gmehmet | 1:f60eafbf009a | 47 | int queue_reset(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 48 | { |
gmehmet | 1:f60eafbf009a | 49 | if (!q) |
gmehmet | 1:f60eafbf009a | 50 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 51 | |
gmehmet | 1:f60eafbf009a | 52 | q->wr = q->base; |
gmehmet | 1:f60eafbf009a | 53 | q->rd = q->base; |
gmehmet | 1:f60eafbf009a | 54 | q->num_item = 0; |
gmehmet | 1:f60eafbf009a | 55 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 56 | #ifdef QUEUE_USAGE_STATS |
gmehmet | 1:f60eafbf009a | 57 | q->pop_cnt = 0; |
gmehmet | 1:f60eafbf009a | 58 | q->push_cnt = 0; |
gmehmet | 1:f60eafbf009a | 59 | q->stats_period_cnt = 100; // Default |
gmehmet | 1:f60eafbf009a | 60 | #endif |
gmehmet | 1:f60eafbf009a | 61 | return 0; |
gmehmet | 1:f60eafbf009a | 62 | } |
gmehmet | 1:f60eafbf009a | 63 | |
gmehmet | 1:f60eafbf009a | 64 | int queue_update_items_size(struct queue_t *q, int item_size) |
gmehmet | 1:f60eafbf009a | 65 | { |
gmehmet | 1:f60eafbf009a | 66 | if (!q) |
gmehmet | 1:f60eafbf009a | 67 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 68 | |
gmehmet | 1:f60eafbf009a | 69 | queue_reset(q); |
gmehmet | 1:f60eafbf009a | 70 | q->item_size = item_size; |
gmehmet | 1:f60eafbf009a | 71 | q->buffer_size = q->max_buffer_size - (q->max_buffer_size % item_size); |
gmehmet | 1:f60eafbf009a | 72 | return 0; |
gmehmet | 1:f60eafbf009a | 73 | } |
gmehmet | 1:f60eafbf009a | 74 | |
gmehmet | 1:f60eafbf009a | 75 | int queue_len(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 76 | { |
gmehmet | 1:f60eafbf009a | 77 | int num_elements; |
gmehmet | 1:f60eafbf009a | 78 | |
gmehmet | 1:f60eafbf009a | 79 | if (!q) |
gmehmet | 1:f60eafbf009a | 80 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 81 | |
gmehmet | 1:f60eafbf009a | 82 | num_elements = q->num_item; |
gmehmet | 1:f60eafbf009a | 83 | return num_elements; |
gmehmet | 1:f60eafbf009a | 84 | } |
gmehmet | 1:f60eafbf009a | 85 | |
gmehmet | 1:f60eafbf009a | 86 | |
gmehmet | 1:f60eafbf009a | 87 | int queue_init(struct queue_t *q, void *buf, int item_size, int buffer_size) |
gmehmet | 1:f60eafbf009a | 88 | { |
gmehmet | 1:f60eafbf009a | 89 | if (!q || !buf) |
gmehmet | 1:f60eafbf009a | 90 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 91 | |
gmehmet | 1:f60eafbf009a | 92 | if (buffer_size % item_size != 0) |
gmehmet | 1:f60eafbf009a | 93 | return -EINVAL; // Padding problem |
gmehmet | 1:f60eafbf009a | 94 | |
gmehmet | 1:f60eafbf009a | 95 | q->num_item = 0; |
gmehmet | 1:f60eafbf009a | 96 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 97 | q->base = (char *)buf; |
gmehmet | 1:f60eafbf009a | 98 | q->wr = (char *)buf; |
gmehmet | 1:f60eafbf009a | 99 | q->rd = (char *)buf; |
gmehmet | 1:f60eafbf009a | 100 | q->item_size = item_size; |
gmehmet | 1:f60eafbf009a | 101 | q->buffer_size = buffer_size; |
gmehmet | 1:f60eafbf009a | 102 | q->max_buffer_size = buffer_size; |
gmehmet | 1:f60eafbf009a | 103 | q->name = NULL; |
gmehmet | 1:f60eafbf009a | 104 | |
gmehmet | 1:f60eafbf009a | 105 | #ifdef QUEUE_USAGE_STATS |
gmehmet | 1:f60eafbf009a | 106 | q->pop_cnt = 0; |
gmehmet | 1:f60eafbf009a | 107 | q->push_cnt = 0; |
gmehmet | 1:f60eafbf009a | 108 | q->stats_period_cnt = 100; // Default |
gmehmet | 1:f60eafbf009a | 109 | #endif |
gmehmet | 1:f60eafbf009a | 110 | |
gmehmet | 1:f60eafbf009a | 111 | return 0; |
gmehmet | 1:f60eafbf009a | 112 | } |
gmehmet | 1:f60eafbf009a | 113 | |
gmehmet | 1:f60eafbf009a | 114 | int queue_init_by_name(struct queue_t *q, void *buf, int item_size, int buffer_size, const char *name) |
gmehmet | 1:f60eafbf009a | 115 | { |
gmehmet | 1:f60eafbf009a | 116 | int ret = queue_init(q, buf, item_size, buffer_size); |
gmehmet | 1:f60eafbf009a | 117 | |
gmehmet | 1:f60eafbf009a | 118 | if (ret < 0) |
gmehmet | 1:f60eafbf009a | 119 | return ret; |
gmehmet | 1:f60eafbf009a | 120 | |
gmehmet | 1:f60eafbf009a | 121 | q->name = (char *)name; |
gmehmet | 1:f60eafbf009a | 122 | return 0; |
gmehmet | 1:f60eafbf009a | 123 | } |
gmehmet | 1:f60eafbf009a | 124 | |
gmehmet | 1:f60eafbf009a | 125 | |
gmehmet | 1:f60eafbf009a | 126 | void queue_destroy(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 127 | { |
gmehmet | 1:f60eafbf009a | 128 | /* TODO: This is placeholder function, double check the implementation */ |
gmehmet | 1:f60eafbf009a | 129 | free((void *)q->base); |
gmehmet | 1:f60eafbf009a | 130 | free((void *)q); |
gmehmet | 1:f60eafbf009a | 131 | } |
gmehmet | 1:f60eafbf009a | 132 | |
gmehmet | 1:f60eafbf009a | 133 | int enqueue(struct queue_t *q, void *data) |
gmehmet | 1:f60eafbf009a | 134 | { |
gmehmet | 1:f60eafbf009a | 135 | int ret = 0; |
gmehmet | 1:f60eafbf009a | 136 | |
gmehmet | 1:f60eafbf009a | 137 | if (!q || !data) |
gmehmet | 1:f60eafbf009a | 138 | return -EINVAL; // Invalid pointer |
gmehmet | 1:f60eafbf009a | 139 | |
gmehmet | 1:f60eafbf009a | 140 | if (q->wr == q->rd) |
gmehmet | 1:f60eafbf009a | 141 | ret = (q->num_item != 0) ? -2 : 0; // Is FIFO Full or Empty? |
gmehmet | 1:f60eafbf009a | 142 | |
gmehmet | 1:f60eafbf009a | 143 | if (q->wr >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 144 | q->wr = q->base; |
gmehmet | 1:f60eafbf009a | 145 | |
gmehmet | 1:f60eafbf009a | 146 | memcpy((void *)q->wr, data, q->item_size); |
gmehmet | 1:f60eafbf009a | 147 | q->wr = q->wr + q->item_size; |
gmehmet | 1:f60eafbf009a | 148 | q->num_item++; |
gmehmet | 1:f60eafbf009a | 149 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 150 | if (q->num_item > fifo_size) |
gmehmet | 1:f60eafbf009a | 151 | q->ovf_item = q->num_item - fifo_size; |
gmehmet | 1:f60eafbf009a | 152 | else |
gmehmet | 1:f60eafbf009a | 153 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 154 | return ret; |
gmehmet | 1:f60eafbf009a | 155 | } |
gmehmet | 1:f60eafbf009a | 156 | |
gmehmet | 1:f60eafbf009a | 157 | int enqueue_test(struct queue_t *q, void *data) |
gmehmet | 1:f60eafbf009a | 158 | { |
gmehmet | 1:f60eafbf009a | 159 | if (!q || !data) |
gmehmet | 1:f60eafbf009a | 160 | return -EINVAL; // Invalid pointer |
gmehmet | 1:f60eafbf009a | 161 | |
gmehmet | 1:f60eafbf009a | 162 | q->num_item++; |
gmehmet | 1:f60eafbf009a | 163 | return 0; |
gmehmet | 1:f60eafbf009a | 164 | } |
gmehmet | 1:f60eafbf009a | 165 | |
gmehmet | 1:f60eafbf009a | 166 | |
gmehmet | 1:f60eafbf009a | 167 | int dequeue_test(struct queue_t *q, void *data) |
gmehmet | 1:f60eafbf009a | 168 | { |
gmehmet | 1:f60eafbf009a | 169 | if (!q || !data) |
gmehmet | 1:f60eafbf009a | 170 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 171 | |
gmehmet | 1:f60eafbf009a | 172 | if (q->num_item > 0) |
gmehmet | 1:f60eafbf009a | 173 | q->num_item--; |
gmehmet | 1:f60eafbf009a | 174 | else |
gmehmet | 1:f60eafbf009a | 175 | return -2; |
gmehmet | 1:f60eafbf009a | 176 | return 0; |
gmehmet | 1:f60eafbf009a | 177 | } |
gmehmet | 1:f60eafbf009a | 178 | |
gmehmet | 1:f60eafbf009a | 179 | int dequeue(struct queue_t *q, void *data) |
gmehmet | 1:f60eafbf009a | 180 | { |
gmehmet | 1:f60eafbf009a | 181 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 182 | |
gmehmet | 1:f60eafbf009a | 183 | if (!q || !data) |
gmehmet | 1:f60eafbf009a | 184 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 185 | |
gmehmet | 1:f60eafbf009a | 186 | if (q->num_item <= 0) { |
gmehmet | 1:f60eafbf009a | 187 | return -2; |
gmehmet | 1:f60eafbf009a | 188 | } |
gmehmet | 1:f60eafbf009a | 189 | |
gmehmet | 1:f60eafbf009a | 190 | if (q->num_item > fifo_size) { |
gmehmet | 1:f60eafbf009a | 191 | uint32_t curr_rd_off = ((uint32_t)(q->rd - q->base) + q->num_item * q->item_size); |
gmehmet | 1:f60eafbf009a | 192 | q->rd = (q->base + (curr_rd_off % q->buffer_size)); |
gmehmet | 1:f60eafbf009a | 193 | q->num_item = fifo_size; // OVF number samples are already gone. |
gmehmet | 1:f60eafbf009a | 194 | |
gmehmet | 1:f60eafbf009a | 195 | #ifdef QUEUE_USAGE_STATS |
gmehmet | 1:f60eafbf009a | 196 | q->pop_cnt++; |
gmehmet | 1:f60eafbf009a | 197 | |
gmehmet | 1:f60eafbf009a | 198 | if ((q->pop_cnt % q->stats_period_cnt) == 0) { |
gmehmet | 1:f60eafbf009a | 199 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 200 | pr_info("%s:%d (%s) - %d samples lost, avail:%d \n",__func__, __LINE__, q->name, q->ovf_item, q->num_item); |
gmehmet | 1:f60eafbf009a | 201 | } else { |
gmehmet | 1:f60eafbf009a | 202 | pr_info("%s:%d - %d samples lost, avail:%d \n", __func__, __LINE__, q->ovf_item, q->num_item); |
gmehmet | 1:f60eafbf009a | 203 | } |
gmehmet | 1:f60eafbf009a | 204 | } |
gmehmet | 1:f60eafbf009a | 205 | #endif |
gmehmet | 1:f60eafbf009a | 206 | } |
gmehmet | 1:f60eafbf009a | 207 | |
gmehmet | 1:f60eafbf009a | 208 | if (q->rd >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 209 | q->rd = q->base; |
gmehmet | 1:f60eafbf009a | 210 | |
gmehmet | 1:f60eafbf009a | 211 | memcpy(data, (void *)q->rd, q->item_size); |
gmehmet | 1:f60eafbf009a | 212 | q->rd = q->rd + q->item_size; |
gmehmet | 1:f60eafbf009a | 213 | q->num_item--; |
gmehmet | 1:f60eafbf009a | 214 | if (q->num_item <= fifo_size) |
gmehmet | 1:f60eafbf009a | 215 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 216 | |
gmehmet | 1:f60eafbf009a | 217 | #if defined(QUEUE_DEBUG) |
gmehmet | 1:f60eafbf009a | 218 | do { |
gmehmet | 1:f60eafbf009a | 219 | static int cnt; |
gmehmet | 1:f60eafbf009a | 220 | |
gmehmet | 1:f60eafbf009a | 221 | if (cnt++ % 100 == 0) { |
gmehmet | 1:f60eafbf009a | 222 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 223 | pr_debug("%s - $ Fifo size: %d, usage: %d\n", q->name, fifo_size, q->num_item); |
gmehmet | 1:f60eafbf009a | 224 | } else { |
gmehmet | 1:f60eafbf009a | 225 | pr_debug("$ Fifo size: %d, usage: %d\n", fifo_size, q->num_item); |
gmehmet | 1:f60eafbf009a | 226 | } |
gmehmet | 1:f60eafbf009a | 227 | } |
gmehmet | 1:f60eafbf009a | 228 | } while(0); |
gmehmet | 1:f60eafbf009a | 229 | #endif |
gmehmet | 1:f60eafbf009a | 230 | |
gmehmet | 1:f60eafbf009a | 231 | return 0; |
gmehmet | 1:f60eafbf009a | 232 | } |
gmehmet | 1:f60eafbf009a | 233 | |
gmehmet | 1:f60eafbf009a | 234 | bool queue_is_full(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 235 | { |
gmehmet | 1:f60eafbf009a | 236 | if (!q) |
gmehmet | 1:f60eafbf009a | 237 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 238 | |
gmehmet | 1:f60eafbf009a | 239 | int num_items = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 240 | |
gmehmet | 1:f60eafbf009a | 241 | |
gmehmet | 1:f60eafbf009a | 242 | if (num_items > q->num_item) |
gmehmet | 1:f60eafbf009a | 243 | return false; |
gmehmet | 1:f60eafbf009a | 244 | return true; |
gmehmet | 1:f60eafbf009a | 245 | } |
gmehmet | 1:f60eafbf009a | 246 | |
gmehmet | 1:f60eafbf009a | 247 | int queue_usage(struct queue_t *q, int *total, int *nm_item) |
gmehmet | 1:f60eafbf009a | 248 | { |
gmehmet | 1:f60eafbf009a | 249 | if (!q) |
gmehmet | 1:f60eafbf009a | 250 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 251 | |
gmehmet | 1:f60eafbf009a | 252 | *total = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 253 | *nm_item = q->num_item; |
gmehmet | 1:f60eafbf009a | 254 | |
gmehmet | 1:f60eafbf009a | 255 | return 0; |
gmehmet | 1:f60eafbf009a | 256 | } |
gmehmet | 1:f60eafbf009a | 257 | |
gmehmet | 1:f60eafbf009a | 258 | int queue_pop(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 259 | { |
gmehmet | 1:f60eafbf009a | 260 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 261 | |
gmehmet | 1:f60eafbf009a | 262 | if (!q) |
gmehmet | 1:f60eafbf009a | 263 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 264 | |
gmehmet | 1:f60eafbf009a | 265 | if (q->num_item <= 0) { |
gmehmet | 1:f60eafbf009a | 266 | return -2; |
gmehmet | 1:f60eafbf009a | 267 | } |
gmehmet | 1:f60eafbf009a | 268 | |
gmehmet | 1:f60eafbf009a | 269 | if (q->num_item > fifo_size) { |
gmehmet | 1:f60eafbf009a | 270 | uint32_t curr_rd_off = ((uint32_t)(q->rd - q->base) + q->num_item * q->item_size); |
gmehmet | 1:f60eafbf009a | 271 | q->ovf_item = q->num_item - fifo_size; |
gmehmet | 1:f60eafbf009a | 272 | q->rd = q->base + (curr_rd_off % q->buffer_size); |
gmehmet | 1:f60eafbf009a | 273 | q->num_item = fifo_size; // OVF number samples are already gone. |
gmehmet | 1:f60eafbf009a | 274 | |
gmehmet | 1:f60eafbf009a | 275 | #ifdef QUEUE_USAGE_STATS |
gmehmet | 1:f60eafbf009a | 276 | q->push_cnt++; |
gmehmet | 1:f60eafbf009a | 277 | |
gmehmet | 1:f60eafbf009a | 278 | if ((q->push_cnt % q->stats_period_cnt) == 0) { |
gmehmet | 1:f60eafbf009a | 279 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 280 | pr_info("%s:%d (%s) - %d samples lost, avail:%d \n",__func__, __LINE__, q->name, q->ovf_item, q->num_item); |
gmehmet | 1:f60eafbf009a | 281 | } else { |
gmehmet | 1:f60eafbf009a | 282 | pr_info("%s:%d - %d samples lost, avail:%d \n", __func__, __LINE__, q->ovf_item, q->num_item); |
gmehmet | 1:f60eafbf009a | 283 | } |
gmehmet | 1:f60eafbf009a | 284 | } |
gmehmet | 1:f60eafbf009a | 285 | #endif |
gmehmet | 1:f60eafbf009a | 286 | } else |
gmehmet | 1:f60eafbf009a | 287 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 288 | |
gmehmet | 1:f60eafbf009a | 289 | if (q->rd >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 290 | q->rd = q->base; |
gmehmet | 1:f60eafbf009a | 291 | |
gmehmet | 1:f60eafbf009a | 292 | q->rd = q->rd + q->item_size; |
gmehmet | 1:f60eafbf009a | 293 | q->num_item--; |
gmehmet | 1:f60eafbf009a | 294 | |
gmehmet | 1:f60eafbf009a | 295 | #if defined(QUEUE_DEBUG) |
gmehmet | 1:f60eafbf009a | 296 | do { |
gmehmet | 1:f60eafbf009a | 297 | static int cnt; |
gmehmet | 1:f60eafbf009a | 298 | |
gmehmet | 1:f60eafbf009a | 299 | if (cnt++ % 100 == 0) { |
gmehmet | 1:f60eafbf009a | 300 | if (q->name) |
gmehmet | 1:f60eafbf009a | 301 | pr_debug("%s - $ Fifo size: %d, usage: %d\n", q->name, fifo_size, q->num_item); |
gmehmet | 1:f60eafbf009a | 302 | else |
gmehmet | 1:f60eafbf009a | 303 | pr_debug("$ Fifo size: %d, usage: %d\n", fifo_size, q->num_item); |
gmehmet | 1:f60eafbf009a | 304 | } |
gmehmet | 1:f60eafbf009a | 305 | } while(0); |
gmehmet | 1:f60eafbf009a | 306 | #endif |
gmehmet | 1:f60eafbf009a | 307 | |
gmehmet | 1:f60eafbf009a | 308 | return 0; |
gmehmet | 1:f60eafbf009a | 309 | } |
gmehmet | 1:f60eafbf009a | 310 | |
gmehmet | 1:f60eafbf009a | 311 | int queue_pop_n(struct queue_t *q, int n) |
gmehmet | 1:f60eafbf009a | 312 | { |
gmehmet | 1:f60eafbf009a | 313 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 314 | |
gmehmet | 1:f60eafbf009a | 315 | if (!q || n < 1) |
gmehmet | 1:f60eafbf009a | 316 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 317 | |
gmehmet | 1:f60eafbf009a | 318 | if (q->num_item < n) { |
gmehmet | 1:f60eafbf009a | 319 | return -2; |
gmehmet | 1:f60eafbf009a | 320 | } |
gmehmet | 1:f60eafbf009a | 321 | |
gmehmet | 1:f60eafbf009a | 322 | if (q->num_item > fifo_size) { |
gmehmet | 1:f60eafbf009a | 323 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 324 | pr_info("%s:%d ( %s ) - %d samples lost, avail:%d \n", |
gmehmet | 1:f60eafbf009a | 325 | __func__, __LINE__, q->name, q->num_item - fifo_size, fifo_size); |
gmehmet | 1:f60eafbf009a | 326 | } else { |
gmehmet | 1:f60eafbf009a | 327 | pr_info("%s:%d - %d samples lost, avail:%d \n", |
gmehmet | 1:f60eafbf009a | 328 | __func__, __LINE__, q->num_item - fifo_size, fifo_size); |
gmehmet | 1:f60eafbf009a | 329 | } |
gmehmet | 1:f60eafbf009a | 330 | q->num_item = fifo_size; // OVF number samples are already gone. |
gmehmet | 1:f60eafbf009a | 331 | n = fifo_size; |
gmehmet | 1:f60eafbf009a | 332 | uint32_t curr_rd_off = (uint32_t)(q->rd - q->base) + q->num_item * q->item_size; |
gmehmet | 1:f60eafbf009a | 333 | q->ovf_item = q->num_item - fifo_size; |
gmehmet | 1:f60eafbf009a | 334 | q->rd = q->base + curr_rd_off % q->buffer_size; |
gmehmet | 1:f60eafbf009a | 335 | } else { |
gmehmet | 1:f60eafbf009a | 336 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 337 | } |
gmehmet | 1:f60eafbf009a | 338 | |
gmehmet | 1:f60eafbf009a | 339 | if (q->rd >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 340 | q->rd = q->base; |
gmehmet | 1:f60eafbf009a | 341 | |
gmehmet | 1:f60eafbf009a | 342 | |
gmehmet | 1:f60eafbf009a | 343 | uint32_t rd_sz = q->item_size * n; |
gmehmet | 1:f60eafbf009a | 344 | uint32_t to_end = (uint32_t)(q->base + q->buffer_size - q->rd); |
gmehmet | 1:f60eafbf009a | 345 | if (to_end < rd_sz) { |
gmehmet | 1:f60eafbf009a | 346 | rd_sz -= to_end; |
gmehmet | 1:f60eafbf009a | 347 | q->rd = q->base; |
gmehmet | 1:f60eafbf009a | 348 | } |
gmehmet | 1:f60eafbf009a | 349 | |
gmehmet | 1:f60eafbf009a | 350 | q->rd = q->rd + rd_sz; |
gmehmet | 1:f60eafbf009a | 351 | q->num_item -= n; |
gmehmet | 1:f60eafbf009a | 352 | |
gmehmet | 1:f60eafbf009a | 353 | return 0; |
gmehmet | 1:f60eafbf009a | 354 | } |
gmehmet | 1:f60eafbf009a | 355 | |
gmehmet | 1:f60eafbf009a | 356 | int queue_front(struct queue_t *q, void *data) |
gmehmet | 1:f60eafbf009a | 357 | { |
gmehmet | 1:f60eafbf009a | 358 | #if 0 |
gmehmet | 1:f60eafbf009a | 359 | return queue_front_n(q, data, 1); |
gmehmet | 1:f60eafbf009a | 360 | #endif |
gmehmet | 1:f60eafbf009a | 361 | |
gmehmet | 1:f60eafbf009a | 362 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 363 | void *rd = 0; |
gmehmet | 1:f60eafbf009a | 364 | |
gmehmet | 1:f60eafbf009a | 365 | if (!q || !data) |
gmehmet | 1:f60eafbf009a | 366 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 367 | |
gmehmet | 1:f60eafbf009a | 368 | if (q->num_item <= 0) { |
gmehmet | 1:f60eafbf009a | 369 | return -2; |
gmehmet | 1:f60eafbf009a | 370 | } |
gmehmet | 1:f60eafbf009a | 371 | |
gmehmet | 1:f60eafbf009a | 372 | if (q->num_item > fifo_size) { |
gmehmet | 1:f60eafbf009a | 373 | uint32_t curr_rd_off = (uint32_t)(q->rd - q->base) + q->num_item * q->item_size; |
gmehmet | 1:f60eafbf009a | 374 | rd = q->base + (curr_rd_off % q->buffer_size); |
gmehmet | 1:f60eafbf009a | 375 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 376 | pr_info("%s:%d ( %s )- %d samples lost, avail:%d cap:%d \n", |
gmehmet | 1:f60eafbf009a | 377 | __func__, __LINE__, q->name, q->ovf_item, q->num_item, fifo_size); |
gmehmet | 1:f60eafbf009a | 378 | } else { |
gmehmet | 1:f60eafbf009a | 379 | pr_info("%s:%d - %d samples lost, avail:%d cap:%d \n", |
gmehmet | 1:f60eafbf009a | 380 | __func__, __LINE__, q->ovf_item, q->num_item, fifo_size); |
gmehmet | 1:f60eafbf009a | 381 | } |
gmehmet | 1:f60eafbf009a | 382 | } else { |
gmehmet | 1:f60eafbf009a | 383 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 384 | rd = q->rd; |
gmehmet | 1:f60eafbf009a | 385 | } |
gmehmet | 1:f60eafbf009a | 386 | |
gmehmet | 1:f60eafbf009a | 387 | if (q->rd >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 388 | rd = q->base; |
gmehmet | 1:f60eafbf009a | 389 | |
gmehmet | 1:f60eafbf009a | 390 | memcpy(data, (void *)rd, q->item_size); |
gmehmet | 1:f60eafbf009a | 391 | return 0; |
gmehmet | 1:f60eafbf009a | 392 | } |
gmehmet | 1:f60eafbf009a | 393 | |
gmehmet | 1:f60eafbf009a | 394 | int queue_front_n(struct queue_t *q, void *data, int n, int buf_sz) |
gmehmet | 1:f60eafbf009a | 395 | { |
gmehmet | 1:f60eafbf009a | 396 | int fifo_size = q->buffer_size / q->item_size; |
gmehmet | 1:f60eafbf009a | 397 | char *rd = 0; |
gmehmet | 1:f60eafbf009a | 398 | char *wr = (char *)data; |
gmehmet | 1:f60eafbf009a | 399 | |
gmehmet | 1:f60eafbf009a | 400 | if (!q || !data || n < 1) |
gmehmet | 1:f60eafbf009a | 401 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 402 | |
gmehmet | 1:f60eafbf009a | 403 | if (q->num_item < n) { |
gmehmet | 1:f60eafbf009a | 404 | return -2; |
gmehmet | 1:f60eafbf009a | 405 | } |
gmehmet | 1:f60eafbf009a | 406 | |
gmehmet | 1:f60eafbf009a | 407 | if (q->num_item > fifo_size) { |
gmehmet | 1:f60eafbf009a | 408 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 409 | pr_info("\n%s:%d ( %s ) - %d samples lost, avail:%d \n", |
gmehmet | 1:f60eafbf009a | 410 | __func__, __LINE__, q->name, q->num_item - fifo_size, fifo_size); |
gmehmet | 1:f60eafbf009a | 411 | } else { |
gmehmet | 1:f60eafbf009a | 412 | pr_info("\n%s:%d - %d samples lost, avail:%d \n", |
gmehmet | 1:f60eafbf009a | 413 | __func__, __LINE__, q->num_item - fifo_size, fifo_size); |
gmehmet | 1:f60eafbf009a | 414 | } |
gmehmet | 1:f60eafbf009a | 415 | q->num_item = fifo_size; |
gmehmet | 1:f60eafbf009a | 416 | n = fifo_size; |
gmehmet | 1:f60eafbf009a | 417 | uint32_t curr_rd_off = (uint32_t)(q->rd - q->base) + q->num_item * q->item_size; |
gmehmet | 1:f60eafbf009a | 418 | rd = q->base + (curr_rd_off % q->buffer_size); |
gmehmet | 1:f60eafbf009a | 419 | } else { |
gmehmet | 1:f60eafbf009a | 420 | q->ovf_item = 0; |
gmehmet | 1:f60eafbf009a | 421 | rd = q->rd; |
gmehmet | 1:f60eafbf009a | 422 | } |
gmehmet | 1:f60eafbf009a | 423 | |
gmehmet | 1:f60eafbf009a | 424 | if (q->rd >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 425 | rd = q->base; |
gmehmet | 1:f60eafbf009a | 426 | |
gmehmet | 1:f60eafbf009a | 427 | uint32_t rd_sz = q->item_size * n; |
gmehmet | 1:f60eafbf009a | 428 | |
gmehmet | 1:f60eafbf009a | 429 | if (buf_sz < rd_sz) { |
gmehmet | 1:f60eafbf009a | 430 | if (q->name) { |
gmehmet | 1:f60eafbf009a | 431 | pr_info("\n%s:%d ( %s ) - Insufficient buffer size: %d\n", |
gmehmet | 1:f60eafbf009a | 432 | __func__, __LINE__, q->name, buf_sz); |
gmehmet | 1:f60eafbf009a | 433 | } else { |
gmehmet | 1:f60eafbf009a | 434 | pr_info("\n%s:%d - Insufficient buffer size: %d\n", |
gmehmet | 1:f60eafbf009a | 435 | __func__, __LINE__, buf_sz); |
gmehmet | 1:f60eafbf009a | 436 | } |
gmehmet | 1:f60eafbf009a | 437 | return -EINVAL; |
gmehmet | 1:f60eafbf009a | 438 | } |
gmehmet | 1:f60eafbf009a | 439 | |
gmehmet | 1:f60eafbf009a | 440 | uint32_t to_end = (uint32_t)(q->base + q->buffer_size - q->rd); |
gmehmet | 1:f60eafbf009a | 441 | if (to_end < rd_sz) { |
gmehmet | 1:f60eafbf009a | 442 | memcpy(wr, rd, to_end); |
gmehmet | 1:f60eafbf009a | 443 | rd_sz -= to_end; |
gmehmet | 1:f60eafbf009a | 444 | rd = q->base; |
gmehmet | 1:f60eafbf009a | 445 | wr += to_end; |
gmehmet | 1:f60eafbf009a | 446 | memcpy(wr, rd, rd_sz); |
gmehmet | 1:f60eafbf009a | 447 | |
gmehmet | 1:f60eafbf009a | 448 | } else { |
gmehmet | 1:f60eafbf009a | 449 | memcpy(wr, rd, rd_sz); |
gmehmet | 1:f60eafbf009a | 450 | } |
gmehmet | 1:f60eafbf009a | 451 | |
gmehmet | 1:f60eafbf009a | 452 | return 0; |
gmehmet | 1:f60eafbf009a | 453 | } |
gmehmet | 1:f60eafbf009a | 454 | |
gmehmet | 1:f60eafbf009a | 455 | int enqueue_string(struct queue_t *q, char *data, int sz) |
gmehmet | 1:f60eafbf009a | 456 | { |
gmehmet | 1:f60eafbf009a | 457 | int ret = 0; |
gmehmet | 1:f60eafbf009a | 458 | int buf_index; |
gmehmet | 1:f60eafbf009a | 459 | char *wr_ptr; |
gmehmet | 1:f60eafbf009a | 460 | |
gmehmet | 1:f60eafbf009a | 461 | if (!q || !data || sz <= 0) |
gmehmet | 1:f60eafbf009a | 462 | return -EFAULT; // Invalid parameters |
gmehmet | 1:f60eafbf009a | 463 | |
gmehmet | 1:f60eafbf009a | 464 | if (q->wr == q->rd) |
gmehmet | 1:f60eafbf009a | 465 | ret = (q->num_item != 0) ? -2 : 0; // Is FIFO Full or Empty? |
gmehmet | 1:f60eafbf009a | 466 | |
gmehmet | 1:f60eafbf009a | 467 | if (q->wr >= (q->base + q->buffer_size)) |
gmehmet | 1:f60eafbf009a | 468 | q->wr = q->base; |
gmehmet | 1:f60eafbf009a | 469 | |
gmehmet | 1:f60eafbf009a | 470 | if ((q->num_item + sz) > q->buffer_size) { |
gmehmet | 1:f60eafbf009a | 471 | #if defined(QUEUE_DEBUG) |
gmehmet | 1:f60eafbf009a | 472 | { |
gmehmet | 1:f60eafbf009a | 473 | char buf[128]; |
gmehmet | 1:f60eafbf009a | 474 | int len; |
gmehmet | 1:f60eafbf009a | 475 | |
gmehmet | 1:f60eafbf009a | 476 | if (q->name) |
gmehmet | 1:f60eafbf009a | 477 | len = sprintf(buf, "\r\n**** %s - ( %s ) - Fifo is full. num_item: %d, sz: %d, buffer size: %d\r\n", |
gmehmet | 1:f60eafbf009a | 478 | __func__, q->name, q->num_item, sz, q->buffer_size); |
gmehmet | 1:f60eafbf009a | 479 | else |
gmehmet | 1:f60eafbf009a | 480 | len = sprintf(buf, "\r\n**** %s - Fifo is full. num_item: %d, sz: %d, buffer size: %d\r\n", |
gmehmet | 1:f60eafbf009a | 481 | __func__, q->num_item, sz, q->buffer_size); |
gmehmet | 1:f60eafbf009a | 482 | |
gmehmet | 1:f60eafbf009a | 483 | UART_Write(UART_PORT, (uint8_t*)buf, len); |
gmehmet | 1:f60eafbf009a | 484 | } |
gmehmet | 1:f60eafbf009a | 485 | #endif |
gmehmet | 1:f60eafbf009a | 486 | return -ENOMEM; |
gmehmet | 1:f60eafbf009a | 487 | } |
gmehmet | 1:f60eafbf009a | 488 | |
gmehmet | 1:f60eafbf009a | 489 | buf_index = (uint32_t)(q->wr - q->base); |
gmehmet | 1:f60eafbf009a | 490 | wr_ptr = q->base; |
gmehmet | 1:f60eafbf009a | 491 | q->num_item += sz; |
gmehmet | 1:f60eafbf009a | 492 | while(sz--) |
gmehmet | 1:f60eafbf009a | 493 | wr_ptr[buf_index++ % q->buffer_size] = *data++; |
gmehmet | 1:f60eafbf009a | 494 | |
gmehmet | 1:f60eafbf009a | 495 | q->wr = q->base + buf_index % q->buffer_size; |
gmehmet | 1:f60eafbf009a | 496 | return ret; |
gmehmet | 1:f60eafbf009a | 497 | } |
gmehmet | 1:f60eafbf009a | 498 | |
gmehmet | 1:f60eafbf009a | 499 | int dequeue_string(struct queue_t *q, char *buf, int buffer_size) |
gmehmet | 1:f60eafbf009a | 500 | { |
gmehmet | 1:f60eafbf009a | 501 | char *rd_ptr; |
gmehmet | 1:f60eafbf009a | 502 | int buf_index; |
gmehmet | 1:f60eafbf009a | 503 | int len; |
gmehmet | 1:f60eafbf009a | 504 | |
gmehmet | 1:f60eafbf009a | 505 | if (!q || !buf || buffer_size <= 0) |
gmehmet | 1:f60eafbf009a | 506 | return -EFAULT; |
gmehmet | 1:f60eafbf009a | 507 | |
gmehmet | 1:f60eafbf009a | 508 | if (q->num_item <= 0) { |
gmehmet | 1:f60eafbf009a | 509 | return -EPERM; |
gmehmet | 1:f60eafbf009a | 510 | } |
gmehmet | 1:f60eafbf009a | 511 | |
gmehmet | 1:f60eafbf009a | 512 | rd_ptr = (char *)q->base; |
gmehmet | 1:f60eafbf009a | 513 | buf_index = (uint32_t)(q->rd - q->base); |
gmehmet | 1:f60eafbf009a | 514 | len = q->num_item; |
gmehmet | 1:f60eafbf009a | 515 | |
gmehmet | 1:f60eafbf009a | 516 | while (buffer_size-- && q->num_item--) { |
gmehmet | 1:f60eafbf009a | 517 | char tmp = rd_ptr[buf_index % q->buffer_size]; |
gmehmet | 1:f60eafbf009a | 518 | rd_ptr[buf_index % q->buffer_size] = 0; // Remove this later on |
gmehmet | 1:f60eafbf009a | 519 | buf_index++; |
gmehmet | 1:f60eafbf009a | 520 | *buf++ = tmp; |
gmehmet | 1:f60eafbf009a | 521 | if (tmp == '\0') |
gmehmet | 1:f60eafbf009a | 522 | break; |
gmehmet | 1:f60eafbf009a | 523 | } |
gmehmet | 1:f60eafbf009a | 524 | |
gmehmet | 1:f60eafbf009a | 525 | if (q->num_item < 0) { |
gmehmet | 1:f60eafbf009a | 526 | /* Data corruption in FIFO */ |
gmehmet | 1:f60eafbf009a | 527 | q->num_item = 0; |
gmehmet | 1:f60eafbf009a | 528 | } else |
gmehmet | 1:f60eafbf009a | 529 | len -= q->num_item; |
gmehmet | 1:f60eafbf009a | 530 | |
gmehmet | 1:f60eafbf009a | 531 | q->rd = q->base + buf_index % q->buffer_size; |
gmehmet | 1:f60eafbf009a | 532 | |
gmehmet | 1:f60eafbf009a | 533 | return len; |
gmehmet | 1:f60eafbf009a | 534 | } |
gmehmet | 1:f60eafbf009a | 535 | |
gmehmet | 1:f60eafbf009a | 536 | int queue_str_len(struct queue_t *q) |
gmehmet | 1:f60eafbf009a | 537 | { |
gmehmet | 1:f60eafbf009a | 538 | char *rd_ptr; |
gmehmet | 1:f60eafbf009a | 539 | int buf_index; |
gmehmet | 1:f60eafbf009a | 540 | int len, i; |
gmehmet | 1:f60eafbf009a | 541 | |
gmehmet | 1:f60eafbf009a | 542 | if (!q) |
gmehmet | 1:f60eafbf009a | 543 | return -EFAULT; |
gmehmet | 1:f60eafbf009a | 544 | |
gmehmet | 1:f60eafbf009a | 545 | if (q->num_item <= 0) { |
gmehmet | 1:f60eafbf009a | 546 | return 0; |
gmehmet | 1:f60eafbf009a | 547 | } |
gmehmet | 1:f60eafbf009a | 548 | |
gmehmet | 1:f60eafbf009a | 549 | rd_ptr = q->base; |
gmehmet | 1:f60eafbf009a | 550 | buf_index = (uint32_t)(q->rd - q->base); |
gmehmet | 1:f60eafbf009a | 551 | i = q->num_item; |
gmehmet | 1:f60eafbf009a | 552 | len = 0; |
gmehmet | 1:f60eafbf009a | 553 | |
gmehmet | 1:f60eafbf009a | 554 | while (i--) { |
gmehmet | 1:f60eafbf009a | 555 | char tmp = rd_ptr[buf_index % q->buffer_size]; |
gmehmet | 1:f60eafbf009a | 556 | buf_index++; |
gmehmet | 1:f60eafbf009a | 557 | if (tmp == '\0') |
gmehmet | 1:f60eafbf009a | 558 | break; |
gmehmet | 1:f60eafbf009a | 559 | len++; |
gmehmet | 1:f60eafbf009a | 560 | } |
gmehmet | 1:f60eafbf009a | 561 | |
gmehmet | 1:f60eafbf009a | 562 | return len; |
gmehmet | 1:f60eafbf009a | 563 | } |
gmehmet | 1:f60eafbf009a | 564 | |
gmehmet | 1:f60eafbf009a | 565 | #if 0 |
gmehmet | 1:f60eafbf009a | 566 | void queue_test(void) |
gmehmet | 1:f60eafbf009a | 567 | { |
gmehmet | 1:f60eafbf009a | 568 | int ret; |
gmehmet | 1:f60eafbf009a | 569 | ppg_data_t ppg_test = { 0, }; |
gmehmet | 1:f60eafbf009a | 570 | ppg_data_t ppg_test_out = { 0, }; |
gmehmet | 1:f60eafbf009a | 571 | int i, j, ii, jj; |
gmehmet | 1:f60eafbf009a | 572 | static ppg_data_t ppg_data[10]; |
gmehmet | 1:f60eafbf009a | 573 | static queue_t queue; |
gmehmet | 1:f60eafbf009a | 574 | |
gmehmet | 1:f60eafbf009a | 575 | srand((unsigned)time(NULL)); |
gmehmet | 1:f60eafbf009a | 576 | ret = queue_init(&queue, &ppg_data, sizeof(ppg_data_t), sizeof(ppg_data)); |
gmehmet | 1:f60eafbf009a | 577 | while (1) { |
gmehmet | 1:f60eafbf009a | 578 | ii = rand() % 20; |
gmehmet | 1:f60eafbf009a | 579 | for (i = 0; i < ii; i++) { |
gmehmet | 1:f60eafbf009a | 580 | /* Test data */ |
gmehmet | 1:f60eafbf009a | 581 | ppg_test.timestamp++; |
gmehmet | 1:f60eafbf009a | 582 | ppg_test.ir++; |
gmehmet | 1:f60eafbf009a | 583 | ppg_test.red++; |
gmehmet | 1:f60eafbf009a | 584 | ppg_test.green++; |
gmehmet | 1:f60eafbf009a | 585 | /* Test functions */ |
gmehmet | 1:f60eafbf009a | 586 | ret = enqueue(&queue, &ppg_test); |
gmehmet | 1:f60eafbf009a | 587 | } |
gmehmet | 1:f60eafbf009a | 588 | jj = rand() % 20; |
gmehmet | 1:f60eafbf009a | 589 | for (j = 0; j < jj; j++) { |
gmehmet | 1:f60eafbf009a | 590 | ret = dequeue(&queue, &ppg_test_out); |
gmehmet | 1:f60eafbf009a | 591 | } |
gmehmet | 1:f60eafbf009a | 592 | } |
gmehmet | 1:f60eafbf009a | 593 | } |
gmehmet | 1:f60eafbf009a | 594 | #endif |
gmehmet | 1:f60eafbf009a | 595 | void queue_n_test(void) |
gmehmet | 1:f60eafbf009a | 596 | { |
gmehmet | 1:f60eafbf009a | 597 | struct queue_t q; |
gmehmet | 1:f60eafbf009a | 598 | uint8_t buf[5]; |
gmehmet | 1:f60eafbf009a | 599 | uint8_t peek_buf[5]; |
gmehmet | 1:f60eafbf009a | 600 | int error; |
gmehmet | 1:f60eafbf009a | 601 | int i; |
gmehmet | 1:f60eafbf009a | 602 | error = queue_init(&q, &buf[0], 1, sizeof(buf)); |
gmehmet | 1:f60eafbf009a | 603 | if (error) |
gmehmet | 1:f60eafbf009a | 604 | printf("queue_init error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 605 | |
gmehmet | 1:f60eafbf009a | 606 | uint8_t val = 0; |
gmehmet | 1:f60eafbf009a | 607 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 608 | val = 1; |
gmehmet | 1:f60eafbf009a | 609 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 610 | val = 2; |
gmehmet | 1:f60eafbf009a | 611 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 612 | val = 3; |
gmehmet | 1:f60eafbf009a | 613 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 614 | val = 4; |
gmehmet | 1:f60eafbf009a | 615 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 616 | |
gmehmet | 1:f60eafbf009a | 617 | printf("enqueued 0,1,2,3,4\r\n"); |
gmehmet | 1:f60eafbf009a | 618 | |
gmehmet | 1:f60eafbf009a | 619 | error = queue_front_n(&q, &peek_buf, 5, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 620 | if (error) { |
gmehmet | 1:f60eafbf009a | 621 | printf("queue_front_n n=5 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 622 | } else { |
gmehmet | 1:f60eafbf009a | 623 | printf("queue_front_n n=5: "); |
gmehmet | 1:f60eafbf009a | 624 | for (i = 0; i < 5; i++) { |
gmehmet | 1:f60eafbf009a | 625 | printf("%d ", peek_buf[i]); |
gmehmet | 1:f60eafbf009a | 626 | } |
gmehmet | 1:f60eafbf009a | 627 | printf("\r\n"); |
gmehmet | 1:f60eafbf009a | 628 | } |
gmehmet | 1:f60eafbf009a | 629 | |
gmehmet | 1:f60eafbf009a | 630 | error = queue_front_n(&q, &peek_buf, 6, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 631 | if (error) |
gmehmet | 1:f60eafbf009a | 632 | printf("queue_front_n n=6 error :)\r\n"); |
gmehmet | 1:f60eafbf009a | 633 | else |
gmehmet | 1:f60eafbf009a | 634 | printf("queue_front_n n=6 succeeded :(\r\n"); |
gmehmet | 1:f60eafbf009a | 635 | |
gmehmet | 1:f60eafbf009a | 636 | error = queue_pop_n(&q, 2); |
gmehmet | 1:f60eafbf009a | 637 | if (error) |
gmehmet | 1:f60eafbf009a | 638 | printf("queue_pop_n n=2 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 639 | else |
gmehmet | 1:f60eafbf009a | 640 | printf("queue_pop_n n=2 succeeded :)\r\n"); |
gmehmet | 1:f60eafbf009a | 641 | |
gmehmet | 1:f60eafbf009a | 642 | error = queue_front_n(&q, &peek_buf, 5, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 643 | if (error) |
gmehmet | 1:f60eafbf009a | 644 | printf("queue_front_n n=5 error :)\r\n"); |
gmehmet | 1:f60eafbf009a | 645 | |
gmehmet | 1:f60eafbf009a | 646 | error = queue_front_n(&q, &peek_buf, 3, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 647 | if (error) { |
gmehmet | 1:f60eafbf009a | 648 | printf("queue_front_n n=3 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 649 | } else { |
gmehmet | 1:f60eafbf009a | 650 | printf("queue_front_n n=3: "); |
gmehmet | 1:f60eafbf009a | 651 | for (i = 0; i < 3; i++) { |
gmehmet | 1:f60eafbf009a | 652 | printf("%d ", peek_buf[i]); |
gmehmet | 1:f60eafbf009a | 653 | } |
gmehmet | 1:f60eafbf009a | 654 | printf("\r\n"); |
gmehmet | 1:f60eafbf009a | 655 | } |
gmehmet | 1:f60eafbf009a | 656 | |
gmehmet | 1:f60eafbf009a | 657 | val = 0; |
gmehmet | 1:f60eafbf009a | 658 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 659 | val = 1; |
gmehmet | 1:f60eafbf009a | 660 | enqueue(&q, &val); |
gmehmet | 1:f60eafbf009a | 661 | |
gmehmet | 1:f60eafbf009a | 662 | printf("enqueued 0,1\r\n"); |
gmehmet | 1:f60eafbf009a | 663 | |
gmehmet | 1:f60eafbf009a | 664 | error = queue_front_n(&q, &peek_buf, 5, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 665 | if (error) { |
gmehmet | 1:f60eafbf009a | 666 | printf("queue_front_n n=5 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 667 | } else { |
gmehmet | 1:f60eafbf009a | 668 | printf("queue_front_n n=5: "); |
gmehmet | 1:f60eafbf009a | 669 | for (i = 0; i < 5; i++) { |
gmehmet | 1:f60eafbf009a | 670 | printf("%d ", peek_buf[i]); |
gmehmet | 1:f60eafbf009a | 671 | } |
gmehmet | 1:f60eafbf009a | 672 | printf("\r\n"); |
gmehmet | 1:f60eafbf009a | 673 | } |
gmehmet | 1:f60eafbf009a | 674 | |
gmehmet | 1:f60eafbf009a | 675 | error = queue_pop_n(&q, 4); |
gmehmet | 1:f60eafbf009a | 676 | if (error) |
gmehmet | 1:f60eafbf009a | 677 | printf("queue_pop_n n=4 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 678 | else |
gmehmet | 1:f60eafbf009a | 679 | printf("queue_pop_n n=4 succeeded :)\r\n"); |
gmehmet | 1:f60eafbf009a | 680 | |
gmehmet | 1:f60eafbf009a | 681 | error = queue_front_n(&q, &peek_buf, 1, sizeof(peek_buf)); |
gmehmet | 1:f60eafbf009a | 682 | if (error) { |
gmehmet | 1:f60eafbf009a | 683 | printf("queue_front_n n=1 error :(\r\n"); |
gmehmet | 1:f60eafbf009a | 684 | } else { |
gmehmet | 1:f60eafbf009a | 685 | printf("queue_front_n n=1: "); |
gmehmet | 1:f60eafbf009a | 686 | for (i = 0; i < 1; i++) { |
gmehmet | 1:f60eafbf009a | 687 | printf("%d ", peek_buf[i]); |
gmehmet | 1:f60eafbf009a | 688 | } |
gmehmet | 1:f60eafbf009a | 689 | printf("\r\n"); |
gmehmet | 1:f60eafbf009a | 690 | } |
gmehmet | 1:f60eafbf009a | 691 | } |