-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsample.c
114 lines (101 loc) · 3.53 KB
/
sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* sample.c
* Sample ring buffer and sample ring buffer function definitions.
*
* Copyright 2018, 2019 Matt Rounds
*
* This file is part of ExplorerLink.
*
* ExplorerLink is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* ExplorerLink is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* ExplorerLink. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include "channel.h"
#include "sample.h"
#include "ring_buffer.h"
#define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))
/* The number of bytes used for sample metadata (rate, length, timestamp) */
#define SAMPLE_METADATA_BYTES 10
/* Just an array of the pointers to sample rate buffers, for iteration */
SampleRateBuffer_t *pxSampleRateBuffers[] = {
&xSampleBuffer1Hz,
&xSampleBuffer10Hz,
&xSampleBuffer100Hz
};
/* Sample buffer definitions */
volatile uint8_t puc1HzData[SAMPLE_BUFFER_SIZE];
SampleRateBuffer_t xSampleBuffer1Hz = {
.xData = {
.pucData = puc1HzData,
.ulSize = SAMPLE_BUFFER_SIZE,
.ulReadIndex = 0,
.ulWriteIndex = 0
},
.usSampleRateHz = RATE_1HZ,
};
volatile uint8_t puc10HzData[SAMPLE_BUFFER_SIZE];
SampleRateBuffer_t xSampleBuffer10Hz = {
.xData = {
.pucData = puc10HzData,
.ulSize = SAMPLE_BUFFER_SIZE,
.ulReadIndex = 0,
.ulWriteIndex = 0
},
.usSampleRateHz = RATE_10HZ,
};
volatile uint8_t puc100HzData[SAMPLE_BUFFER_SIZE];
SampleRateBuffer_t xSampleBuffer100Hz = {
.xData = {
.pucData = puc100HzData,
.ulSize = SAMPLE_BUFFER_SIZE,
.ulReadIndex = 0,
.ulWriteIndex = 0
},
.usSampleRateHz = RATE_100HZ,
};
/*
* Get the number of sample buffers.
*/
uint8_t ucSampleGetBufferCount(void) {
return ARRAY_LENGTH(pxSampleRateBuffers);
}
/*
* Checks the sample buffers and returns the period of the one with the
* highest frequency, in milliseconds.
*/
float ulSampleGetMinPeriodMS(void) {
uint8_t ucNumBuffers = ARRAY_LENGTH(pxSampleRateBuffers);
uint32_t i;
uint32_t ulLast = 0;
uint32_t ulCur;
for (i = 0; i < ucNumBuffers; i++) {
ulCur = (uint32_t)(pxSampleRateBuffers[i]->usSampleRateHz);
if (ulCur > ulLast) {
ulLast = ulCur;
}
}
return (uint32_t)(1000 / ulCur);
}
/*
* Use the channel API to store the size of each sample with its buffer.
*/
void vInitSampleRateBuffers(void) {
uint32_t ucNumBuffers = ARRAY_LENGTH(pxSampleRateBuffers);
uint32_t i;
for (i = 0; i < ucNumBuffers; i++) {
pxSampleRateBuffers[i]->ulSampleSize = ulChannelGetByteCountForRate(
(SampleRateHz_t)(pxSampleRateBuffers[i]->usSampleRateHz)) +
SAMPLE_METADATA_BYTES;
}
}