Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_AdcBuffer.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Mar 1, 2018
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7#pragma once
8
9#include <cfg/cs_Config.h>
10#include <logging/cs_Logger.h>
12#include <util/cs_Error.h>
13
14#include <cstdint>
15#include <cstdlib>
16
17// The number of channels per buffer.
19
20// The number of samples for each channel in a buffer.
22
23// Total number of samples in a buffer.
25
26// The number of buffers.
28
35class AdcBuffer {
36private:
38 bool _allocated = false;
39
42
43public:
49 static AdcBuffer instance;
50 return instance;
51 }
52
57 if (_allocated) {
58 return ERR_SUCCESS;
59 }
60 for (adc_buffer_id_t i = 0; i < ADC_BUFFER_COUNT; ++i) {
61 adc_buffer_t* buf = (adc_buffer_t*)malloc(sizeof(adc_buffer_t));
62 if (buf == nullptr) {
63 LOGw("No space to allocate buf");
64 // TODO: free all buffers?
65 return ERR_NO_SPACE;
66 }
68 if (buf->samples == nullptr) {
69 LOGw("No space to allocate samples buf");
70 // TODO: free all buffers?
71 return ERR_NO_SPACE;
72 }
73 LOGd("Allocate buffer %i = %p", i, buf->samples);
74 _buf[i] = buf;
75 }
76 _allocated = true;
77 return ERR_SUCCESS;
78 }
79
84 // assert(buffer_id < getBufferCount(), "ADC has fewer buffers allocated");
85 return _buf[buffer_id];
86 }
87
88#ifdef HOST_TARGET
94 void setBuffer(adc_buffer_id_t buffer_id, adc_buffer_t* ptr) {
95 _buf[buffer_id] = ptr;
96 }
97#endif
98
102 static inline constexpr adc_sample_value_id_t getBufferLength() {
104 }
105
109 static inline constexpr adc_sample_value_id_t getChannelLength() {
111 }
112
116 static inline constexpr adc_channel_id_t getChannelCount() {
117 return ADC_CHANNEL_COUNT;
118 }
119
123 static inline constexpr adc_buffer_id_t getBufferCount() {
124 return ADC_BUFFER_COUNT;
125 }
126
138 adc_buffer_id_t buffer_id, adc_channel_id_t channel_id, adc_sample_value_id_t value_id) {
139 static_assert(std::is_unsigned<adc_sample_value_id_t>::value, "value id should be unsigned");
140 assert(value_id < getChannelLength(), "value id should be smaller than channel size");
141
142 adc_sample_value_id_t value_id_in_channel = value_id * getChannelCount() + channel_id;
143 adc_sample_value_t* buf = getBuffer(buffer_id)->samples;
144 adc_sample_value_t value = buf[value_id_in_channel];
145 return value;
146 }
147
157 adc_buffer_id_t buffer_id,
158 adc_channel_id_t channel_id,
159 adc_sample_value_id_t value_id,
160 adc_sample_value_t value) {
161 static_assert(std::is_unsigned<adc_sample_value_id_t>::value, "value id should be unsigned");
162 assert(value_id < getChannelLength(), "value id should be smaller than channel size");
163
164 adc_sample_value_id_t value_id_in_channel = value_id * getChannelCount() + channel_id;
165 adc_sample_value_t* buf = getBuffer(buffer_id)->samples;
166 buf[value_id_in_channel] = value;
167 }
168};
Class that keeps up the buffers used for ADC.
Definition: cs_AdcBuffer.h:35
static constexpr adc_sample_value_id_t getBufferLength()
Get total number of samples in a buffer.
Definition: cs_AdcBuffer.h:102
AdcBuffer()
Definition: cs_AdcBuffer.h:40
AdcBuffer(AdcBuffer const &)
Definition: cs_AdcBuffer.h:41
cs_ret_code_t init()
Allocate the buffers.
Definition: cs_AdcBuffer.h:56
adc_sample_value_t getValue(adc_buffer_id_t buffer_id, adc_channel_id_t channel_id, adc_sample_value_id_t value_id)
Get a particular value from a buffer.
Definition: cs_AdcBuffer.h:137
static AdcBuffer & getInstance()
Singleton implementation.
Definition: cs_AdcBuffer.h:48
static constexpr adc_buffer_id_t getBufferCount()
Get number of buffers.
Definition: cs_AdcBuffer.h:123
adc_buffer_t * getBuffer(adc_buffer_id_t buffer_id)
Get buffer with given id.
Definition: cs_AdcBuffer.h:83
void setValue(adc_buffer_id_t buffer_id, adc_channel_id_t channel_id, adc_sample_value_id_t value_id, adc_sample_value_t value)
For in-place filtering it is necessary to write a particular value into the buffer.
Definition: cs_AdcBuffer.h:156
bool _allocated
Definition: cs_AdcBuffer.h:38
adc_buffer_t * _buf[ADC_BUFFER_COUNT]
Definition: cs_AdcBuffer.h:37
static constexpr adc_sample_value_id_t getChannelLength()
Get number of samples for each channel in a buffer.
Definition: cs_AdcBuffer.h:109
static constexpr adc_channel_id_t getChannelCount()
Get number of channels per buffer.
Definition: cs_AdcBuffer.h:116
static const adc_buffer_id_t ADC_BUFFER_COUNT
Definition: cs_AdcBuffer.h:27
static const adc_channel_id_t ADC_CHANNEL_COUNT
Definition: cs_AdcBuffer.h:18
static const uint32_t ADC_BUFFER_SAMPLE_COUNT
Definition: cs_AdcBuffer.h:24
static const adc_sample_value_id_t ADC_CHANNEL_SAMPLE_COUNT
Definition: cs_AdcBuffer.h:21
#define CS_ADC_NUM_CHANNELS
Definition: cs_Config.h:147
#define CS_ADC_NUM_BUFFERS
Definition: cs_Config.h:151
#define CS_ADC_NUM_SAMPLES_PER_CHANNEL
Definition: cs_Config.h:148
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
@ ERR_NO_SPACE
Definition: cs_ErrorCodes.h:25
#define assert(expr, message)
Author: Crownstone Team Date: 21 Sep., 2013 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-...
Definition: cs_Error.h:22
#define LOGw(fmt,...)
Definition: cs_Logger.h:92
#define LOGd(fmt,...)
Definition: cs_Logger.h:90
uint8_t adc_buffer_id_t
Definition: cs_Typedefs.h:30
int16_t adc_sample_value_t
Definition: cs_Typedefs.h:34
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
uint8_t adc_channel_id_t
Definition: cs_Typedefs.h:31
uint16_t adc_sample_value_id_t
Definition: cs_Typedefs.h:33
Struct communicated from the ADC class when it's done sampling a buffer.
Definition: cs_PacketsInternal.h:346
adc_sample_value_t * samples
Pointer to the samples.
Definition: cs_PacketsInternal.h:369