Chrispy
High-performance multichannel ADC sampling and audio recording.
Loading...
Searching...
No Matches
Adc.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include <stddef.h>
4#include <stdint.h>
5
6#include "Timer.h"
7
8namespace adc {
9
13enum struct BitResolution : uint8_t {
14 Eight = 8,
15 Ten = 10,
16};
17
19
23extern const size_t MAX_CHANNEL_COUNT;
24
28struct Channel {
32 uint8_t pin;
36 int8_t power;
41
50 explicit Channel(uint8_t _pin, int8_t _power, bool _active_high)
51 : pin(_pin), power(_power), active_high(_active_high) {}
52
59 inline int8_t mux_mask();
60};
61
66enum struct AutotriggerSource : uint8_t {
67 FreeRunning = 0b000,
68 AnalogComparator = 0b001,
69 ExternalIrq0 = 0b010,
70 TimCnt0CmpA = 0b011,
71 TimCnt0Ovf = 0b100,
72 TimCnt1CmpB = 0b101,
73 TimCnt1Ovf = 0b110,
74 TimCnt1Cap = 0b111,
75};
76
80bool init(uint8_t _nchannels, Channel* _channels, uint8_t* _buf, size_t _sz);
81
95int8_t start(BitResolution res, uint32_t sample_rate, size_t ch_window_sz = 1,
96 uint32_t warmup_ms = 100);
97
102uint32_t stop();
103
108uint32_t collected();
109
113void on();
114
118void off();
119
123void sleep();
124
142int8_t swap_buffer(uint8_t** buf, size_t& sz, size_t& ch_index);
143
150int8_t drain_buffer(uint8_t** buf, size_t& sz, size_t& ch_index);
151
152} // namespace adc
Definition Adc.cpp:8
void sleep()
Turn off ADC and enter sleep mode.
Definition Adc.cpp:286
int8_t drain_buffer(uint8_t **buf, size_t &sz, size_t &ch_index)
NOT SAFE TO USE WHEN THE ADC IS ENABLED!
Definition Adc.cpp:182
const size_t MAX_CHANNEL_COUNT
Number of channels supported by the ADC (0 - 15).
Definition Adc.cpp:34
BitResolution
Supported bit resolutions for ADC sampling.
Definition Adc.h:13
uint32_t collected()
Definition Adc.cpp:326
uint32_t stop()
Stops ADC sampling and performs cleanup on registers.
Definition Adc.cpp:328
void on()
Activate internal board's ADC.
Definition Adc.cpp:279
AutotriggerSource
Potential sources for ADC interrupts to be triggered by.
Definition Adc.h:66
size_t bytes_per_sample(BitResolution res)
Definition Adc.cpp:519
bool init(uint8_t nchannels, Channel *channels, uint8_t *buf, size_t sz)
Initialize ADC module with these parameters for sampling.
Definition Adc.cpp:266
int8_t swap_buffer(uint8_t **buf, size_t &sz, size_t &ch_index)
Retrieve a buffer from the module belonging to a specific channel, writing the size and channel index...
Definition Adc.cpp:215
void off()
Turn off ADC.
Definition Adc.cpp:284
int8_t start(BitResolution res, uint32_t sample_rate, size_t ch_window_sz, uint32_t warmup_ms)
Start ADC sampling at a certain rate with a given bit resolution.
Definition Adc.cpp:291
Single ADC channel (pin + metadata).
Definition Adc.h:28
uint8_t pin
Pin number corresponding to the channel being read.
Definition Adc.h:32
Channel(uint8_t _pin, int8_t _power, bool _active_high)
Channel constructor.
Definition Adc.h:50
int8_t mux_mask()
Gets bit pattern for mux mask to use with ADC for channel.
Definition Adc.cpp:396
bool active_high
Boolean for if the power is active high or active high,.
Definition Adc.h:40
int8_t power
Power pin.
Definition Adc.h:36