Bluetooth UART support for the Adafruit BluefruitLE SPI, for the University of York Engineering Stage 1 project
utility/Adafruit_FIFO.h@0:a80552d32b80, 2021-02-06 (annotated)
- Committer:
- ajp109
- Date:
- Sat Feb 06 20:58:22 2021 +0000
- Revision:
- 0:a80552d32b80
Initial commit (not working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajp109 | 0:a80552d32b80 | 1 | /**************************************************************************/ |
ajp109 | 0:a80552d32b80 | 2 | /*! |
ajp109 | 0:a80552d32b80 | 3 | @file Adafruit_FIFO.h |
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 | #ifndef _Adafruit_FIFO_H_ |
ajp109 | 0:a80552d32b80 | 38 | #define _Adafruit_FIFO_H_ |
ajp109 | 0:a80552d32b80 | 39 | |
ajp109 | 0:a80552d32b80 | 40 | #include <stdint.h> |
ajp109 | 0:a80552d32b80 | 41 | #include <stdbool.h> |
ajp109 | 0:a80552d32b80 | 42 | |
ajp109 | 0:a80552d32b80 | 43 | class Adafruit_FIFO |
ajp109 | 0:a80552d32b80 | 44 | { |
ajp109 | 0:a80552d32b80 | 45 | private: |
ajp109 | 0:a80552d32b80 | 46 | uint8_t* m_buffer ; ///< buffer pointer |
ajp109 | 0:a80552d32b80 | 47 | uint16_t m_depth ; ///< max items |
ajp109 | 0:a80552d32b80 | 48 | uint8_t m_item_size ; ///< size of each item |
ajp109 | 0:a80552d32b80 | 49 | bool m_overwritable ; |
ajp109 | 0:a80552d32b80 | 50 | volatile uint16_t m_count ; ///< number of items in queue |
ajp109 | 0:a80552d32b80 | 51 | volatile uint16_t m_wr_idx ; ///< write pointer |
ajp109 | 0:a80552d32b80 | 52 | volatile uint16_t m_rd_idx ; ///< read pointer |
ajp109 | 0:a80552d32b80 | 53 | |
ajp109 | 0:a80552d32b80 | 54 | public: |
ajp109 | 0:a80552d32b80 | 55 | // Constructor |
ajp109 | 0:a80552d32b80 | 56 | Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite); |
ajp109 | 0:a80552d32b80 | 57 | |
ajp109 | 0:a80552d32b80 | 58 | void clear(void); |
ajp109 | 0:a80552d32b80 | 59 | bool peek(void* buffer); |
ajp109 | 0:a80552d32b80 | 60 | bool peekAt(uint16_t position, void * p_buffer); |
ajp109 | 0:a80552d32b80 | 61 | |
ajp109 | 0:a80552d32b80 | 62 | bool write(void const* item); |
ajp109 | 0:a80552d32b80 | 63 | uint16_t write_n(void const * data, uint16_t n); |
ajp109 | 0:a80552d32b80 | 64 | |
ajp109 | 0:a80552d32b80 | 65 | bool read(void* buffer); |
ajp109 | 0:a80552d32b80 | 66 | uint16_t read_n (void * buffer, uint16_t n); |
ajp109 | 0:a80552d32b80 | 67 | |
ajp109 | 0:a80552d32b80 | 68 | inline bool empty(void) { return m_count == 0; } |
ajp109 | 0:a80552d32b80 | 69 | inline bool full(void) { return m_count == m_depth; } |
ajp109 | 0:a80552d32b80 | 70 | inline uint16_t count(void) { return m_count; } |
ajp109 | 0:a80552d32b80 | 71 | inline uint16_t remaining(void) { return m_depth - m_count; } |
ajp109 | 0:a80552d32b80 | 72 | }; |
ajp109 | 0:a80552d32b80 | 73 | |
ajp109 | 0:a80552d32b80 | 74 | #endif /* _Adafruit_FIFO_H_ */ |