Library to easily communicate with XBee modules.
Fork of XBeeLib by
FrameBuffer/FrameBuffer.cpp@3:8662ebe83570, 2015-05-18 (annotated)
- Committer:
- spastor
- Date:
- Mon May 18 13:16:55 2015 +0200
- Revision:
- 3:8662ebe83570
- Parent:
- 0:fcaad0dfa051
- Child:
- 4:629712865107
Automatic upload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
spastor | 0:fcaad0dfa051 | 1 | /** |
spastor | 0:fcaad0dfa051 | 2 | * Copyright (c) 2015 Digi International Inc., |
spastor | 0:fcaad0dfa051 | 3 | * All rights not expressly granted are reserved. |
spastor | 0:fcaad0dfa051 | 4 | * |
spastor | 0:fcaad0dfa051 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
spastor | 0:fcaad0dfa051 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
spastor | 0:fcaad0dfa051 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
spastor | 0:fcaad0dfa051 | 8 | * |
spastor | 0:fcaad0dfa051 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
spastor | 0:fcaad0dfa051 | 10 | * ======================================================================= |
spastor | 0:fcaad0dfa051 | 11 | */ |
spastor | 0:fcaad0dfa051 | 12 | |
spastor | 0:fcaad0dfa051 | 13 | #include "FrameBuffer.h" |
spastor | 0:fcaad0dfa051 | 14 | |
spastor | 0:fcaad0dfa051 | 15 | #if !(defined AVOID_DISABLE_IRQS) |
spastor | 0:fcaad0dfa051 | 16 | #define disable_irq() __disable_irq() |
spastor | 0:fcaad0dfa051 | 17 | #define enable_irq() __enable_irq() |
spastor | 0:fcaad0dfa051 | 18 | #else |
spastor | 0:fcaad0dfa051 | 19 | #define disable_irq() |
spastor | 0:fcaad0dfa051 | 20 | #define enable_irq() |
spastor | 0:fcaad0dfa051 | 21 | #endif |
spastor | 0:fcaad0dfa051 | 22 | |
spastor | 0:fcaad0dfa051 | 23 | FrameBuffer::FrameBuffer() : _head(0), _tail_app(0), _tail_syncr(0), _dropped_frames(0) |
spastor | 0:fcaad0dfa051 | 24 | { |
spastor | 0:fcaad0dfa051 | 25 | for (int i = 0; i < FRAME_BUFFER_SIZE; i++) { |
spastor | 0:fcaad0dfa051 | 26 | _frm_buf[i].frame = new ApiFrame(MAX_FRAME_PAYLOAD_LEN - 1); |
spastor | 0:fcaad0dfa051 | 27 | _frm_buf[i].status = FrameStatusFree; |
spastor | 0:fcaad0dfa051 | 28 | } |
spastor | 0:fcaad0dfa051 | 29 | } |
spastor | 0:fcaad0dfa051 | 30 | |
spastor | 0:fcaad0dfa051 | 31 | FrameBuffer::~FrameBuffer() |
spastor | 0:fcaad0dfa051 | 32 | { |
spastor | 0:fcaad0dfa051 | 33 | for (int i = 0; i < FRAME_BUFFER_SIZE; i++) { |
spastor | 0:fcaad0dfa051 | 34 | delete _frm_buf[i].frame; |
spastor | 0:fcaad0dfa051 | 35 | } |
spastor | 0:fcaad0dfa051 | 36 | } |
spastor | 0:fcaad0dfa051 | 37 | |
spastor | 0:fcaad0dfa051 | 38 | ApiFrame *FrameBuffer::get_next_free_frame(void) |
spastor | 0:fcaad0dfa051 | 39 | { |
spastor | 0:fcaad0dfa051 | 40 | uint8_t i = _head; |
spastor | 0:fcaad0dfa051 | 41 | ApiFrame *ret = NULL; |
spastor | 0:fcaad0dfa051 | 42 | |
spastor | 0:fcaad0dfa051 | 43 | do { |
spastor | 0:fcaad0dfa051 | 44 | if (_frm_buf[i].status == FrameStatusFree || _frm_buf[i].status == FrameStatusComplete) { |
spastor | 0:fcaad0dfa051 | 45 | if (_frm_buf[i].status == FrameStatusComplete) |
spastor | 0:fcaad0dfa051 | 46 | _dropped_frames++; |
spastor | 0:fcaad0dfa051 | 47 | _frm_buf[i].status = FrameStatusAssigned; |
spastor | 0:fcaad0dfa051 | 48 | ret = _frm_buf[i].frame; |
spastor | 0:fcaad0dfa051 | 49 | _head = ++i % FRAME_BUFFER_SIZE; |
spastor | 0:fcaad0dfa051 | 50 | break; |
spastor | 0:fcaad0dfa051 | 51 | } |
spastor | 0:fcaad0dfa051 | 52 | i++; |
spastor | 0:fcaad0dfa051 | 53 | i = i % FRAME_BUFFER_SIZE; |
spastor | 0:fcaad0dfa051 | 54 | } while (i != _head); |
spastor | 0:fcaad0dfa051 | 55 | |
spastor | 0:fcaad0dfa051 | 56 | return ret; |
spastor | 0:fcaad0dfa051 | 57 | } |
spastor | 0:fcaad0dfa051 | 58 | |
spastor | 0:fcaad0dfa051 | 59 | bool FrameBuffer::complete_frame(ApiFrame *frame) |
spastor | 0:fcaad0dfa051 | 60 | { |
spastor | 0:fcaad0dfa051 | 61 | bool ret = false; |
spastor | 0:fcaad0dfa051 | 62 | |
spastor | 0:fcaad0dfa051 | 63 | for (int i = 0; i < FRAME_BUFFER_SIZE; i++) { |
spastor | 0:fcaad0dfa051 | 64 | if (_frm_buf[i].frame == frame) { |
spastor | 0:fcaad0dfa051 | 65 | _frm_buf[i].status = FrameStatusComplete; |
spastor | 0:fcaad0dfa051 | 66 | ret = true; |
spastor | 0:fcaad0dfa051 | 67 | break; |
spastor | 0:fcaad0dfa051 | 68 | } |
spastor | 0:fcaad0dfa051 | 69 | } |
spastor | 0:fcaad0dfa051 | 70 | |
spastor | 0:fcaad0dfa051 | 71 | return ret; |
spastor | 0:fcaad0dfa051 | 72 | } |
spastor | 0:fcaad0dfa051 | 73 | |
spastor | 0:fcaad0dfa051 | 74 | ApiFrame *FrameBuffer::get_next_complete_frame(uint8_t* tail) |
spastor | 0:fcaad0dfa051 | 75 | { |
spastor | 0:fcaad0dfa051 | 76 | uint8_t i = *tail; |
spastor | 0:fcaad0dfa051 | 77 | ApiFrame *ret = NULL; |
spastor | 0:fcaad0dfa051 | 78 | |
spastor | 0:fcaad0dfa051 | 79 | do { |
spastor | 0:fcaad0dfa051 | 80 | disable_irq(); |
spastor | 0:fcaad0dfa051 | 81 | if (_frm_buf[i].status == FrameStatusComplete) { |
spastor | 0:fcaad0dfa051 | 82 | _frm_buf[i].status = FrameStatusAssigned; |
spastor | 0:fcaad0dfa051 | 83 | enable_irq(); |
spastor | 0:fcaad0dfa051 | 84 | ret = _frm_buf[i].frame; |
spastor | 0:fcaad0dfa051 | 85 | *tail = ++i % FRAME_BUFFER_SIZE; |
spastor | 0:fcaad0dfa051 | 86 | break; |
spastor | 0:fcaad0dfa051 | 87 | } |
spastor | 0:fcaad0dfa051 | 88 | enable_irq(); |
spastor | 0:fcaad0dfa051 | 89 | i++; |
spastor | 0:fcaad0dfa051 | 90 | i = i % FRAME_BUFFER_SIZE; |
spastor | 0:fcaad0dfa051 | 91 | } while (i != *tail); |
spastor | 0:fcaad0dfa051 | 92 | |
spastor | 0:fcaad0dfa051 | 93 | return ret; |
spastor | 0:fcaad0dfa051 | 94 | } |
spastor | 0:fcaad0dfa051 | 95 | |
spastor | 0:fcaad0dfa051 | 96 | ApiFrame *FrameBuffer::get_next_complete_frame_syncr(void) |
spastor | 0:fcaad0dfa051 | 97 | { |
spastor | 0:fcaad0dfa051 | 98 | return get_next_complete_frame(&_tail_syncr); |
spastor | 0:fcaad0dfa051 | 99 | } |
spastor | 0:fcaad0dfa051 | 100 | |
spastor | 0:fcaad0dfa051 | 101 | ApiFrame *FrameBuffer::get_next_complete_frame_app(void) |
spastor | 0:fcaad0dfa051 | 102 | { |
spastor | 0:fcaad0dfa051 | 103 | return get_next_complete_frame(&_tail_app); |
spastor | 0:fcaad0dfa051 | 104 | } |
spastor | 0:fcaad0dfa051 | 105 | |
spastor | 0:fcaad0dfa051 | 106 | bool FrameBuffer::free_frame(ApiFrame *frame) |
spastor | 0:fcaad0dfa051 | 107 | { |
spastor | 0:fcaad0dfa051 | 108 | bool ret = false; |
spastor | 0:fcaad0dfa051 | 109 | |
spastor | 0:fcaad0dfa051 | 110 | for (int i = 0; i < FRAME_BUFFER_SIZE; i++) { |
spastor | 0:fcaad0dfa051 | 111 | if (_frm_buf[i].frame == frame) { |
spastor | 0:fcaad0dfa051 | 112 | _frm_buf[i].status = FrameStatusFree; |
spastor | 0:fcaad0dfa051 | 113 | ret = true; |
spastor | 0:fcaad0dfa051 | 114 | break; |
spastor | 0:fcaad0dfa051 | 115 | } |
spastor | 0:fcaad0dfa051 | 116 | } |
spastor | 0:fcaad0dfa051 | 117 | |
spastor | 0:fcaad0dfa051 | 118 | return ret; |
spastor | 0:fcaad0dfa051 | 119 | } |
spastor | 0:fcaad0dfa051 | 120 | |
spastor | 0:fcaad0dfa051 | 121 | uint32_t FrameBuffer::get_dropped_frames_count(void) |
spastor | 0:fcaad0dfa051 | 122 | { |
spastor | 0:fcaad0dfa051 | 123 | const uint32_t dropped_frames = _dropped_frames; |
spastor | 0:fcaad0dfa051 | 124 | |
spastor | 0:fcaad0dfa051 | 125 | _dropped_frames = 0; |
spastor | 0:fcaad0dfa051 | 126 | |
spastor | 0:fcaad0dfa051 | 127 | return dropped_frames; |
spastor | 0:fcaad0dfa051 | 128 | } |