SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
dac.hpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS control library
3 *
4 * Copyright (C) 2022-2024, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
22#pragma once
23
24#include <cstdint>
25#include <etl/algorithm.h>
26
28{
39class dac
40{
41public:
48 dac(uint16_t resolution, float vref)
49 : m_res(resolution), m_vref(vref), m_volts(0.0f)
50 {
51 }
52
61 virtual void
62 start() = 0;
63
69 virtual void
70 stop() = 0;
71
77 virtual float
78 voltage() const
79 {
80 return m_volts;
81 }
82
92 virtual void
93 set_voltage(float volts)
94 {
95 m_volts = etl::clamp(volts, 0.0f, m_vref);
96 uint32_t x = m_volts / (vref() / (1UL << m_res));
97 write(x);
98 }
99
105 virtual float
106 vref() const
107 {
108 return m_vref;
109 }
110
111protected:
112 virtual void
113 write(uint32_t x) = 0;
114
115private:
116 const uint16_t m_res;
117 const float m_vref;
118 float m_volts;
119};
120
121} // namespace satnogs::comms::lib::bsp
dac(uint16_t resolution, float vref)
Construct a new DAC object.
Definition dac.hpp:48
virtual float voltage() const
Retrieves the latest output voltage of the DAC.
Definition dac.hpp:78
virtual void stop()=0
A virtual method to stop the DAC.
virtual float vref() const
Retrieves the reference voltage of the DAC.
Definition dac.hpp:106
virtual void start()=0
A virtual method to start the DAC.
virtual void set_voltage(float volts)
Sets the output voltage of the DAC.
Definition dac.hpp:93
virtual void write(uint32_t x)=0