Ningkai Wu / Mbed 2 deprecated test_spi_ble

Dependencies:   mbed-rtos mbed

Committer:
wninghj
Date:
Thu Apr 05 21:26:10 2018 +0000
Revision:
0:39b7f3158ebe
TEAM12 DJ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wninghj 0:39b7f3158ebe 1 /**************************************************************************/
wninghj 0:39b7f3158ebe 2 /*!
wninghj 0:39b7f3158ebe 3 @file Adafruit_FIFO.cpp
wninghj 0:39b7f3158ebe 4 @author hathach
wninghj 0:39b7f3158ebe 5
wninghj 0:39b7f3158ebe 6 @section LICENSE
wninghj 0:39b7f3158ebe 7
wninghj 0:39b7f3158ebe 8 Software License Agreement (BSD License)
wninghj 0:39b7f3158ebe 9
wninghj 0:39b7f3158ebe 10 Copyright (c) 2015, Adafruit Industries (adafruit.com)
wninghj 0:39b7f3158ebe 11 All rights reserved.
wninghj 0:39b7f3158ebe 12
wninghj 0:39b7f3158ebe 13 Redistribution and use in source and binary forms, with or without
wninghj 0:39b7f3158ebe 14 modification, are permitted provided that the following conditions are met:
wninghj 0:39b7f3158ebe 15 1. Redistributions of source code must retain the above copyright
wninghj 0:39b7f3158ebe 16 notice, this list of conditions and the following disclaimer.
wninghj 0:39b7f3158ebe 17 2. Redistributions in binary form must reproduce the above copyright
wninghj 0:39b7f3158ebe 18 notice, this list of conditions and the following disclaimer in the
wninghj 0:39b7f3158ebe 19 documentation and/or other materials provided with the distribution.
wninghj 0:39b7f3158ebe 20 3. Neither the name of the copyright holders nor the
wninghj 0:39b7f3158ebe 21 names of its contributors may be used to endorse or promote products
wninghj 0:39b7f3158ebe 22 derived from this software without specific prior written permission.
wninghj 0:39b7f3158ebe 23
wninghj 0:39b7f3158ebe 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
wninghj 0:39b7f3158ebe 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
wninghj 0:39b7f3158ebe 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
wninghj 0:39b7f3158ebe 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
wninghj 0:39b7f3158ebe 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
wninghj 0:39b7f3158ebe 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
wninghj 0:39b7f3158ebe 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
wninghj 0:39b7f3158ebe 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
wninghj 0:39b7f3158ebe 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
wninghj 0:39b7f3158ebe 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
wninghj 0:39b7f3158ebe 34 */
wninghj 0:39b7f3158ebe 35 /**************************************************************************/
wninghj 0:39b7f3158ebe 36
wninghj 0:39b7f3158ebe 37 #include "Adafruit_FIFO.h"
wninghj 0:39b7f3158ebe 38 #include <string.h>
wninghj 0:39b7f3158ebe 39
wninghj 0:39b7f3158ebe 40 /******************************************************************************/
wninghj 0:39b7f3158ebe 41 /*!
wninghj 0:39b7f3158ebe 42 @brief Constructor
wninghj 0:39b7f3158ebe 43
wninghj 0:39b7f3158ebe 44 @param[in] buffer
wninghj 0:39b7f3158ebe 45 Memory location to store data
wninghj 0:39b7f3158ebe 46 @param[in] depth
wninghj 0:39b7f3158ebe 47 Maximum number of items can be hold in buffer
wninghj 0:39b7f3158ebe 48 @param[in] item_size
wninghj 0:39b7f3158ebe 49 Number of bytes of each item
wninghj 0:39b7f3158ebe 50 @param[in] overwrite
wninghj 0:39b7f3158ebe 51 Should the buffer is overwitten to the first item when it is full
wninghj 0:39b7f3158ebe 52 */
wninghj 0:39b7f3158ebe 53 /******************************************************************************/
wninghj 0:39b7f3158ebe 54 Adafruit_FIFO::Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite)
wninghj 0:39b7f3158ebe 55 {
wninghj 0:39b7f3158ebe 56 m_buffer = (uint8_t*) buffer;
wninghj 0:39b7f3158ebe 57 m_depth = depth;
wninghj 0:39b7f3158ebe 58 m_item_size = item_size;
wninghj 0:39b7f3158ebe 59 m_overwritable = overwrite;
wninghj 0:39b7f3158ebe 60
wninghj 0:39b7f3158ebe 61 m_count = m_wr_idx = m_rd_idx = 0;
wninghj 0:39b7f3158ebe 62 }
wninghj 0:39b7f3158ebe 63
wninghj 0:39b7f3158ebe 64 /******************************************************************************/
wninghj 0:39b7f3158ebe 65 /*!
wninghj 0:39b7f3158ebe 66 @brief Clear the FIFO
wninghj 0:39b7f3158ebe 67 */
wninghj 0:39b7f3158ebe 68 /******************************************************************************/
wninghj 0:39b7f3158ebe 69 void Adafruit_FIFO::clear(void)
wninghj 0:39b7f3158ebe 70 {
wninghj 0:39b7f3158ebe 71 m_rd_idx = m_wr_idx = m_count = 0;
wninghj 0:39b7f3158ebe 72 }
wninghj 0:39b7f3158ebe 73
wninghj 0:39b7f3158ebe 74 /******************************************************************************/
wninghj 0:39b7f3158ebe 75 /*!
wninghj 0:39b7f3158ebe 76 @brief Write an item to the FIFO
wninghj 0:39b7f3158ebe 77
wninghj 0:39b7f3158ebe 78 @param[in] item
wninghj 0:39b7f3158ebe 79 Memory address of the item
wninghj 0:39b7f3158ebe 80 */
wninghj 0:39b7f3158ebe 81 /******************************************************************************/
wninghj 0:39b7f3158ebe 82 bool Adafruit_FIFO::write(void const* item)
wninghj 0:39b7f3158ebe 83 {
wninghj 0:39b7f3158ebe 84 if ( full() && !m_overwritable ) return false;
wninghj 0:39b7f3158ebe 85
wninghj 0:39b7f3158ebe 86 memcpy( m_buffer + (m_wr_idx * m_item_size),
wninghj 0:39b7f3158ebe 87 item,
wninghj 0:39b7f3158ebe 88 m_item_size);
wninghj 0:39b7f3158ebe 89
wninghj 0:39b7f3158ebe 90 m_wr_idx = (m_wr_idx + 1) % m_depth;
wninghj 0:39b7f3158ebe 91
wninghj 0:39b7f3158ebe 92 if ( full() )
wninghj 0:39b7f3158ebe 93 {
wninghj 0:39b7f3158ebe 94 m_rd_idx = m_wr_idx; // keep the full state (rd == wr && len = size)
wninghj 0:39b7f3158ebe 95 }
wninghj 0:39b7f3158ebe 96 else
wninghj 0:39b7f3158ebe 97 {
wninghj 0:39b7f3158ebe 98 m_count++;
wninghj 0:39b7f3158ebe 99 }
wninghj 0:39b7f3158ebe 100
wninghj 0:39b7f3158ebe 101 return true;
wninghj 0:39b7f3158ebe 102 }
wninghj 0:39b7f3158ebe 103
wninghj 0:39b7f3158ebe 104 /******************************************************************************/
wninghj 0:39b7f3158ebe 105 /*!
wninghj 0:39b7f3158ebe 106 @brief Write array of items to the FIFO
wninghj 0:39b7f3158ebe 107
wninghj 0:39b7f3158ebe 108 @param[in] data
wninghj 0:39b7f3158ebe 109 Memory address of the item's array
wninghj 0:39b7f3158ebe 110 @param[in] n
wninghj 0:39b7f3158ebe 111 Number of items to write
wninghj 0:39b7f3158ebe 112
wninghj 0:39b7f3158ebe 113 @return Number of written items
wninghj 0:39b7f3158ebe 114 */
wninghj 0:39b7f3158ebe 115 /******************************************************************************/
wninghj 0:39b7f3158ebe 116 uint16_t Adafruit_FIFO::write_n(void const * data, uint16_t n)
wninghj 0:39b7f3158ebe 117 {
wninghj 0:39b7f3158ebe 118 if ( n == 0 ) return 0;
wninghj 0:39b7f3158ebe 119
wninghj 0:39b7f3158ebe 120 uint8_t* buf = (uint8_t*) data;
wninghj 0:39b7f3158ebe 121
wninghj 0:39b7f3158ebe 122 uint16_t len = 0;
wninghj 0:39b7f3158ebe 123 while( (len < n) && write(buf) )
wninghj 0:39b7f3158ebe 124 {
wninghj 0:39b7f3158ebe 125 len++;
wninghj 0:39b7f3158ebe 126 buf += m_item_size;
wninghj 0:39b7f3158ebe 127 }
wninghj 0:39b7f3158ebe 128
wninghj 0:39b7f3158ebe 129 return len;
wninghj 0:39b7f3158ebe 130 }
wninghj 0:39b7f3158ebe 131
wninghj 0:39b7f3158ebe 132 /******************************************************************************/
wninghj 0:39b7f3158ebe 133 /*!
wninghj 0:39b7f3158ebe 134 @brief Read an item from FIFO
wninghj 0:39b7f3158ebe 135
wninghj 0:39b7f3158ebe 136 @param[in] buffer
wninghj 0:39b7f3158ebe 137 Memory address to store item
wninghj 0:39b7f3158ebe 138 */
wninghj 0:39b7f3158ebe 139 /******************************************************************************/
wninghj 0:39b7f3158ebe 140 bool Adafruit_FIFO::read(void* buffer)
wninghj 0:39b7f3158ebe 141 {
wninghj 0:39b7f3158ebe 142 if( empty() ) return false;
wninghj 0:39b7f3158ebe 143
wninghj 0:39b7f3158ebe 144 memcpy(buffer,
wninghj 0:39b7f3158ebe 145 m_buffer + (m_rd_idx * m_item_size),
wninghj 0:39b7f3158ebe 146 m_item_size);
wninghj 0:39b7f3158ebe 147 m_rd_idx = (m_rd_idx + 1) % m_depth;
wninghj 0:39b7f3158ebe 148 m_count--;
wninghj 0:39b7f3158ebe 149
wninghj 0:39b7f3158ebe 150 return true;
wninghj 0:39b7f3158ebe 151 }
wninghj 0:39b7f3158ebe 152
wninghj 0:39b7f3158ebe 153 /******************************************************************************/
wninghj 0:39b7f3158ebe 154 /*!
wninghj 0:39b7f3158ebe 155 @brief Read multiple items to an array
wninghj 0:39b7f3158ebe 156
wninghj 0:39b7f3158ebe 157 @param[in] buffer
wninghj 0:39b7f3158ebe 158 Memory address of the item's array
wninghj 0:39b7f3158ebe 159 @param[in] n
wninghj 0:39b7f3158ebe 160 Number of items to read
wninghj 0:39b7f3158ebe 161
wninghj 0:39b7f3158ebe 162 @return Number of read items
wninghj 0:39b7f3158ebe 163 */
wninghj 0:39b7f3158ebe 164 /******************************************************************************/
wninghj 0:39b7f3158ebe 165
wninghj 0:39b7f3158ebe 166 uint16_t Adafruit_FIFO::read_n (void * buffer, uint16_t n)
wninghj 0:39b7f3158ebe 167 {
wninghj 0:39b7f3158ebe 168 if( n == 0 ) return 0;
wninghj 0:39b7f3158ebe 169
wninghj 0:39b7f3158ebe 170 uint8_t* buf = (uint8_t*) buffer;
wninghj 0:39b7f3158ebe 171
wninghj 0:39b7f3158ebe 172 uint16_t len = 0;
wninghj 0:39b7f3158ebe 173 while( (len < n) && read(buf) )
wninghj 0:39b7f3158ebe 174 {
wninghj 0:39b7f3158ebe 175 len++;
wninghj 0:39b7f3158ebe 176 buf += m_item_size;
wninghj 0:39b7f3158ebe 177 }
wninghj 0:39b7f3158ebe 178
wninghj 0:39b7f3158ebe 179 return len;
wninghj 0:39b7f3158ebe 180 }
wninghj 0:39b7f3158ebe 181
wninghj 0:39b7f3158ebe 182 /******************************************************************************/
wninghj 0:39b7f3158ebe 183 /*!
wninghj 0:39b7f3158ebe 184 @brief Read an item without removing it from the FIFO
wninghj 0:39b7f3158ebe 185
wninghj 0:39b7f3158ebe 186 @param[in] buffer
wninghj 0:39b7f3158ebe 187 Memory address to store item
wninghj 0:39b7f3158ebe 188 */
wninghj 0:39b7f3158ebe 189 /******************************************************************************/
wninghj 0:39b7f3158ebe 190 bool Adafruit_FIFO::peek(void* buffer)
wninghj 0:39b7f3158ebe 191 {
wninghj 0:39b7f3158ebe 192 if( empty() ) return false;
wninghj 0:39b7f3158ebe 193
wninghj 0:39b7f3158ebe 194 memcpy(buffer,
wninghj 0:39b7f3158ebe 195 m_buffer + (m_rd_idx * m_item_size),
wninghj 0:39b7f3158ebe 196 m_item_size);
wninghj 0:39b7f3158ebe 197
wninghj 0:39b7f3158ebe 198 return true;
wninghj 0:39b7f3158ebe 199 }
wninghj 0:39b7f3158ebe 200
wninghj 0:39b7f3158ebe 201
wninghj 0:39b7f3158ebe 202 /******************************************************************************/
wninghj 0:39b7f3158ebe 203 /*!
wninghj 0:39b7f3158ebe 204 @brief Read an item without removing it from the FIFO at the specific index
wninghj 0:39b7f3158ebe 205
wninghj 0:39b7f3158ebe 206 @param[in] position
wninghj 0:39b7f3158ebe 207 Position to read from in the FIFO buffer
wninghj 0:39b7f3158ebe 208
wninghj 0:39b7f3158ebe 209 @param[in] buffer
wninghj 0:39b7f3158ebe 210 Memory address to store item
wninghj 0:39b7f3158ebe 211 */
wninghj 0:39b7f3158ebe 212 /******************************************************************************/
wninghj 0:39b7f3158ebe 213 bool Adafruit_FIFO::peekAt(uint16_t position, void * p_buffer)
wninghj 0:39b7f3158ebe 214 {
wninghj 0:39b7f3158ebe 215 if( empty() || (position >= m_count) ) return false;
wninghj 0:39b7f3158ebe 216
wninghj 0:39b7f3158ebe 217 uint16_t index = (m_rd_idx + position) % m_depth; // rd_idx is position=0
wninghj 0:39b7f3158ebe 218 memcpy(p_buffer,
wninghj 0:39b7f3158ebe 219 m_buffer + (index * m_item_size),
wninghj 0:39b7f3158ebe 220 m_item_size);
wninghj 0:39b7f3158ebe 221
wninghj 0:39b7f3158ebe 222 return true;
wninghj 0:39b7f3158ebe 223 }
wninghj 0:39b7f3158ebe 224