semf
analogsensor.h
Go to the documentation of this file.
1
10#ifndef SEMF_INPUT_SENSOR_ANALOGSENSOR_H_
11#define SEMF_INPUT_SENSOR_ANALOGSENSOR_H_
12
13#include <semf/input/analogin.h>
17
18namespace semf
19{
20template <typename T, size_t N_CHANNELS = 1>
32{
33public:
40 AnalogSensor(AnalogIn& analogIn, T buffer[], size_t bufferSize);
41 explicit AnalogSensor(const AnalogSensor& other) = delete;
42 virtual ~AnalogSensor() = default;
43
44 void update() override;
45 LastInBuffer<T>& buffer(size_t index) override;
46
47private:
49 void onDataAvailable();
50
52 AnalogIn& m_analogIn;
54 LastInDmaBuffer<T> m_buffers[N_CHANNELS];
56 size_t m_currentChannelIndex = 0;
58 SEMF_SLOT(m_onDataAvailableSlot, AnalogSensor, *this, onDataAvailable);
59};
60
61template <typename T, size_t N_CHANNELS>
62AnalogSensor<T, N_CHANNELS>::AnalogSensor(AnalogIn& analogIn, T buffer[], size_t bufferSize)
63: m_analogIn(analogIn)
64{
65 m_analogIn.dataAvailable.connect(m_onDataAvailableSlot);
66 for (size_t i = 0; i < N_CHANNELS; i++)
67 {
68 m_buffers[i].setBuffer(buffer, bufferSize);
69 m_buffers[i].setStride(N_CHANNELS);
70 m_buffers[i].setPos(i);
71 }
72}
73
74template <typename T, size_t N_CHANNELS>
76{
77 m_analogIn.start();
78}
79
80template <typename T, size_t N_CHANNELS>
82{
83 return m_buffers[index];
84}
85
86template <typename T, size_t N_CHANNELS>
88{
89 m_buffers[m_currentChannelIndex].put(static_cast<T>(m_analogIn.value()));
90 HardwareSensor<T>::dataAvailable(m_currentChannelIndex);
91 m_currentChannelIndex++;
92 m_currentChannelIndex %= N_CHANNELS;
93}
94} /* namespace semf */
95#endif /* SEMF_INPUT_SENSOR_ANALOGSENSOR_H_ */
Interface for using ADC (Analog to Digital Conversion) hardware in interrupt mode.
Definition: analogin.h:27
Signal dataAvailable
Definition: analogin.h:46
Provides data from an AnalogIn to one to more Sensor objects.
Definition: analogsensor.h:32
void update() override
Definition: analogsensor.h:75
LastInBuffer< T > & buffer(size_t index) override
Returns the buffer, where the measurement data is stored in.
Definition: analogsensor.h:81
virtual ~AnalogSensor()=default
AnalogSensor(const AnalogSensor &other)=delete
AnalogSensor(AnalogIn &analogIn, T buffer[], size_t bufferSize)
Constructor.
Definition: analogsensor.h:62
Base class for sensor hardware like analog sensor. By calling update(), a measurement cycle gets trig...
Signal< size_t > dataAvailable
The LastInBuffer class implements a kind of circular buffer.
Definition: lastinbuffer.h:40
The LastInDmaBuffer class implements a kind of circular buffer.
void connect(SlotBase< Arguments... > &slot)
Connect a method to the signal.
Definition: signal.h:94