Bluetooth UART support for the Adafruit BluefruitLE SPI, for the University of York Engineering Stage 1 project

Committer:
ajp109
Date:
Fri Mar 12 14:35:25 2021 +0000
Revision:
3:bdfd15be7b82
Parent:
0:a80552d32b80
Remove readme

Who changed what in which revision?

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