@@ -86,45 +86,21 @@ Adafruit_FIFO::~Adafruit_FIFO()
8686 if (_buffer) rtos_free (_buffer);
8787}
8888
89- bool Adafruit_FIFO::_mutex_lock (bool isr)
90- {
91- (void ) isr;
92- return xSemaphoreTake (_mutex, portMAX_DELAY);
93- }
94-
95- bool Adafruit_FIFO::_mutex_unlock (bool isr)
96- {
97- (void ) isr;
98- return xSemaphoreGive (_mutex);
99- }
100-
101-
10289/* *****************************************************************************/
10390/* !
10491 @brief Clear the FIFO
10592*/
10693/* *****************************************************************************/
10794void Adafruit_FIFO::clear (void )
10895{
109- _mutex_lock (false );
96+ _mutex_lock ();
11097 _rd_idx = _wr_idx = _count = 0 ;
111- _mutex_unlock (false );
98+ _mutex_unlock ();
11299}
113100
114- /* *****************************************************************************/
115- /* !
116- @brief Write an item to the FIFO
117101
118- @param[in] item
119- Memory address of the item
120- */
121- /* *****************************************************************************/
122- uint16_t Adafruit_FIFO::write (void const * item)
102+ void Adafruit_FIFO::_push (void const * item)
123103{
124- if ( full () && !_overwritable ) return 0 ;
125-
126- _mutex_lock (false );
127-
128104 memcpy ( _buffer + (_wr_idx * _item_size),
129105 item,
130106 _item_size);
@@ -139,8 +115,25 @@ uint16_t Adafruit_FIFO::write(void const* item)
139115 {
140116 _count++;
141117 }
118+ }
142119
143- _mutex_unlock (false );
120+ /* *****************************************************************************/
121+ /* !
122+ @brief Write an item to the FIFO
123+
124+ @param[in] item
125+ Memory address of the item
126+ */
127+ /* *****************************************************************************/
128+ uint16_t Adafruit_FIFO::write (void const * item)
129+ {
130+ if ( full () && !_overwritable ) return 0 ;
131+
132+ _mutex_lock ();
133+
134+ _push (item);
135+
136+ _mutex_unlock ();
144137
145138 return 1 ;
146139}
@@ -157,20 +150,38 @@ uint16_t Adafruit_FIFO::write(void const* item)
157150 @return Number of written items
158151*/
159152/* *****************************************************************************/
160- uint16_t Adafruit_FIFO::write (void const * data, uint16_t n )
153+ uint16_t Adafruit_FIFO::write (void const * data, uint16_t count )
161154{
162- if ( n == 0 ) return 0 ;
155+ if ( count == 0 ) return 0 ;
156+
157+ _mutex_lock ();
163158
164- uint8_t * buf = (uint8_t *) data;
159+ // Not overwritable limit up to full
160+ if (!_overwritable) count = min (count, remaining ());
165161
166- uint16_t len = 0 ;
167- while ( (len < n) && write (buf) )
162+ uint8_t const * buf8 = (uint8_t const *) data;
163+ uint16_t n = 0 ;
164+
165+ while (n < count)
168166 {
169- len++;
170- buf += _item_size;
167+ _push (buf8);
168+
169+ n++;
170+ buf8 += _item_size;
171171 }
172172
173- return len;
173+ _mutex_unlock ();
174+
175+ return n;
176+ }
177+
178+ void Adafruit_FIFO::_pull (void * buffer)
179+ {
180+ memcpy (buffer,
181+ _buffer + (_rd_idx * _item_size),
182+ _item_size);
183+ _rd_idx = (_rd_idx + 1 ) % _depth;
184+ _count--;
174185}
175186
176187/* *****************************************************************************/
@@ -185,15 +196,11 @@ uint16_t Adafruit_FIFO::read(void* buffer)
185196{
186197 if ( empty () ) return 0 ;
187198
188- _mutex_lock (false );
199+ _mutex_lock ();
189200
190- memcpy (buffer,
191- _buffer + (_rd_idx * _item_size),
192- _item_size);
193- _rd_idx = (_rd_idx + 1 ) % _depth;
194- _count--;
201+ _pull (buffer);
195202
196- _mutex_unlock (false );
203+ _mutex_unlock ();
197204
198205 return 1 ;
199206}
@@ -210,42 +217,31 @@ uint16_t Adafruit_FIFO::read(void* buffer)
210217 @return Number of read items
211218*/
212219/* *****************************************************************************/
213- uint16_t Adafruit_FIFO::read (void * buffer, uint16_t n )
220+ uint16_t Adafruit_FIFO::read (void * buffer, uint16_t count )
214221{
215- if ( n == 0 ) return 0 ;
222+ if ( count == 0 ) return 0 ;
216223
217- uint8_t * buf = ( uint8_t *) buffer ;
224+ _mutex_lock () ;
218225
219- uint16_t len = 0 ;
220- while ( (len < n) && read (buf) )
221- {
222- len++;
223- buf += _item_size;
224- }
226+ /* Limit up to fifo's count */
227+ if ( count > _count ) count = _count;
225228
226- return len ;
227- }
229+ uint8_t * buf8 = ( uint8_t *) buffer ;
230+ uint16_t n = 0 ;
228231
229- /* **************************************************************************** */
230- /* !
231- @brief Read an item without removing it from the FIFO
232+ while (n < count)
233+ {
234+ _pull (buf8);
232235
233- @param[in] buffer
234- Memory address to store item
235- */
236- /* *****************************************************************************/
237- bool Adafruit_FIFO::peek (void * buffer)
238- {
239- if ( empty () ) return false ;
236+ n++;
237+ buf8 += _item_size;
238+ }
240239
241- memcpy (buffer,
242- _buffer + (_rd_idx * _item_size),
243- _item_size);
240+ _mutex_unlock ();
244241
245- return true ;
242+ return n ;
246243}
247244
248-
249245/* *****************************************************************************/
250246/* !
251247 @brief Read an item without removing it from the FIFO at the specific index
0 commit comments