# 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

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?

UserRevisionLine numberNew 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 #include "mydsc_ring_buffer.h"
ghsalazar 0:71de2d089e41 18
ghsalazar 0:71de2d089e41 19 #include <stdlib.h>
ghsalazar 0:71de2d089e41 20
ghsalazar 0:71de2d089e41 21 int mydsc_ring_buffer_init(mydsc_ring_buffer_t *prb, uint32_t size)
ghsalazar 0:71de2d089e41 22 {
ghsalazar 0:71de2d089e41 23 int nmsg = -1;
ghsalazar 0:71de2d089e41 24 uint32_t array_size;
ghsalazar 0:71de2d089e41 25
ghsalazar 0:71de2d089e41 26 if (NULL != prb) {
ghsalazar 0:71de2d089e41 27 array_size = 1;
ghsalazar 0:71de2d089e41 28 while (array_size < size)
ghsalazar 0:71de2d089e41 29 array_size = array_size << 1;
ghsalazar 0:71de2d089e41 30 prb->max_position = array_size-1;
ghsalazar 0:71de2d089e41 31 prb->index = 0;
ghsalazar 0:71de2d089e41 32 prb->samples = (float*) malloc( array_size*sizeof(float) );
ghsalazar 0:71de2d089e41 33 nmsg = 0;
ghsalazar 0:71de2d089e41 34 }
ghsalazar 0:71de2d089e41 35 return nmsg;
ghsalazar 0:71de2d089e41 36 }
ghsalazar 0:71de2d089e41 37
ghsalazar 0:71de2d089e41 38 void mydsc_ring_buffer_push(mydsc_ring_buffer_t *ring, float input)
ghsalazar 0:71de2d089e41 39 {
ghsalazar 0:71de2d089e41 40 ring->index++;
ghsalazar 0:71de2d089e41 41 ring->index &= ring->max_position;
ghsalazar 0:71de2d089e41 42 ring->samples[ring->index] = input;
ghsalazar 0:71de2d089e41 43 }