# My Digital Signal Controller Library # The objective of MyDSC library is to implement controllers with help digital signal processors, digital signal controllers or mixed signals processors.
Dependents: mydsc_sampling_example mydsc_sampling_example mydsc-serial-example mydsc-nonblocking-example
include/mydsc_ring_buffer.h@4:90c3f1288d41, 2020-03-28 (annotated)
- Committer:
- ghsalazar
- Date:
- Sat Mar 28 16:09:15 2020 +0000
- Revision:
- 4:90c3f1288d41
- Parent:
- 0:71de2d089e41
Add error handling
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ghsalazar | 0:71de2d089e41 | 1 | /* My Digital Signal Controller library |
ghsalazar | 0:71de2d089e41 | 2 | * Copyright (c) 2019, Gastón H. SALAZAR-SILVA |
ghsalazar | 0:71de2d089e41 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ghsalazar | 0:71de2d089e41 | 4 | * |
ghsalazar | 0:71de2d089e41 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ghsalazar | 0:71de2d089e41 | 6 | * you may not use this file except in compliance with the License. |
ghsalazar | 0:71de2d089e41 | 7 | * |
ghsalazar | 0:71de2d089e41 | 8 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
ghsalazar | 0:71de2d089e41 | 9 | * |
ghsalazar | 0:71de2d089e41 | 10 | * Unless required by applicable law or agreed to in writing, software |
ghsalazar | 0:71de2d089e41 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
ghsalazar | 0:71de2d089e41 | 12 | * either express or implied. |
ghsalazar | 0:71de2d089e41 | 13 | * |
ghsalazar | 0:71de2d089e41 | 14 | * See the License for the specific language governing permissions and limitations under the License. |
ghsalazar | 0:71de2d089e41 | 15 | */ |
ghsalazar | 0:71de2d089e41 | 16 | |
ghsalazar | 0:71de2d089e41 | 17 | #ifndef MYDSC_RING_BUFFER_H |
ghsalazar | 0:71de2d089e41 | 18 | #define MYDSC_RING_BUFFER_H |
ghsalazar | 0:71de2d089e41 | 19 | |
ghsalazar | 0:71de2d089e41 | 20 | #include <stdint.h> |
ghsalazar | 0:71de2d089e41 | 21 | |
ghsalazar | 0:71de2d089e41 | 22 | #ifdef __cplusplus |
ghsalazar | 0:71de2d089e41 | 23 | extern "C" { |
ghsalazar | 0:71de2d089e41 | 24 | #endif |
ghsalazar | 0:71de2d089e41 | 25 | |
ghsalazar | 0:71de2d089e41 | 26 | /** |
ghsalazar | 0:71de2d089e41 | 27 | * @author Gastón H. SALAZAR-SILVA |
ghsalazar | 0:71de2d089e41 | 28 | * @date 10/29/2019 |
ghsalazar | 0:71de2d089e41 | 29 | * |
ghsalazar | 0:71de2d089e41 | 30 | * @brief This module. |
ghsalazar | 0:71de2d089e41 | 31 | */ |
ghsalazar | 0:71de2d089e41 | 32 | typedef struct mydsc_ring_buffer { |
ghsalazar | 0:71de2d089e41 | 33 | uint32_t max_position; /**< It is used as a mask for modular arithmetic. Its value should be 2^n - 1. */ |
ghsalazar | 0:71de2d089e41 | 34 | uint32_t index; /**< It is the actual position of the last element in the circular buffer. */ |
ghsalazar | 0:71de2d089e41 | 35 | float *samples; /**< The circular buffer is implemented as an array. this array is initializated with mydsc_ring_bufferinit. */ |
ghsalazar | 0:71de2d089e41 | 36 | } mydsc_ring_buffer_t; |
ghsalazar | 0:71de2d089e41 | 37 | |
ghsalazar | 0:71de2d089e41 | 38 | /** |
ghsalazar | 0:71de2d089e41 | 39 | * @brief Initialize a ring buffer. |
ghsalazar | 0:71de2d089e41 | 40 | * @param prb a pointer to a ring buffer. |
ghsalazar | 0:71de2d089e41 | 41 | * @param size the size of the samples array. Its value should be 2^n. |
ghsalazar | 0:71de2d089e41 | 42 | * @return 0 on success, -1 on error. |
ghsalazar | 0:71de2d089e41 | 43 | * |
ghsalazar | 0:71de2d089e41 | 44 | * @author Gastón H. SALAZAR-SILVA |
ghsalazar | 0:71de2d089e41 | 45 | * @date 10/29/2019 |
ghsalazar | 0:71de2d089e41 | 46 | */ |
ghsalazar | 0:71de2d089e41 | 47 | int mydsc_ring_buffer_init(mydsc_ring_buffer_t *prb, uint32_t size); |
ghsalazar | 0:71de2d089e41 | 48 | |
ghsalazar | 0:71de2d089e41 | 49 | /** |
ghsalazar | 0:71de2d089e41 | 50 | * @brief Push a value into a ring buffer. |
ghsalazar | 0:71de2d089e41 | 51 | * @param ring a pointer to a ring buffer. |
ghsalazar | 0:71de2d089e41 | 52 | * @param input a float to be pushed into the ring buffer. |
ghsalazar | 0:71de2d089e41 | 53 | * |
ghsalazar | 0:71de2d089e41 | 54 | * @author Gastón H. SALAZAR-SILVA |
ghsalazar | 0:71de2d089e41 | 55 | * @date 10/29/2019 |
ghsalazar | 0:71de2d089e41 | 56 | */ |
ghsalazar | 0:71de2d089e41 | 57 | void mydsc_ring_buffer_push(mydsc_ring_buffer_t *ring, float input); |
ghsalazar | 0:71de2d089e41 | 58 | |
ghsalazar | 0:71de2d089e41 | 59 | /** |
ghsalazar | 0:71de2d089e41 | 60 | * @brief Return a pointer to a especific sample. |
ghsalazar | 0:71de2d089e41 | 61 | * @param ring a pointer to a ring buffer. |
ghsalazar | 0:71de2d089e41 | 62 | * @param pos an integer to indicate which sample to point. A negative value is expexted to past samples. |
ghsalazar | 0:71de2d089e41 | 63 | * |
ghsalazar | 0:71de2d089e41 | 64 | * @author Gastón H. SALAZAR-SILVA |
ghsalazar | 0:71de2d089e41 | 65 | * @date 10/29/2019 |
ghsalazar | 0:71de2d089e41 | 66 | */ |
ghsalazar | 0:71de2d089e41 | 67 | static inline float* mydsc_ring_buffer_sample(mydsc_ring_buffer_t *ring, int32_t pos) |
ghsalazar | 0:71de2d089e41 | 68 | { |
ghsalazar | 0:71de2d089e41 | 69 | uint32_t n; |
ghsalazar | 0:71de2d089e41 | 70 | |
ghsalazar | 0:71de2d089e41 | 71 | n = ring->index + pos; |
ghsalazar | 0:71de2d089e41 | 72 | n &= ring->max_position; |
ghsalazar | 0:71de2d089e41 | 73 | return &ring->samples[n]; |
ghsalazar | 0:71de2d089e41 | 74 | } |
ghsalazar | 0:71de2d089e41 | 75 | |
ghsalazar | 0:71de2d089e41 | 76 | #ifdef __cplusplus |
ghsalazar | 0:71de2d089e41 | 77 | } |
ghsalazar | 0:71de2d089e41 | 78 | #endif |
ghsalazar | 0:71de2d089e41 | 79 | |
ghsalazar | 0:71de2d089e41 | 80 | #define $(rb,n) *mydsc_ring_buffersample( &rb, n) |
ghsalazar | 0:71de2d089e41 | 81 | #define $$(prb,n) *mydsc_ring_buffersample( prb, n) |
ghsalazar | 0:71de2d089e41 | 82 | |
ghsalazar | 0:71de2d089e41 | 83 | #endif // MYDSC_RING_BUFFER_H |