libyggdrasil  v1.0.0
dac.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <cmath>
31 
32 namespace bsp::mid::drv {
33 
43  template<auto Context, u8 Index, u32 Offset, u32 MaxValue>
44  struct DACChannel {
45  DACChannel(const DACChannel&) = delete;
46  auto operator=(const DACChannel&) = delete;
47 
48  constexpr static auto ReferenceVoltage = 3.3;
49 
55  static bool init() {
56  DACChannel::s_device = open(("/sys/bus/iio/devices/iio:device" + std::to_string(Context) + "/out_voltage" + std::to_string(Index) + "_raw").c_str(), O_RDWR);
57  return DACChannel::s_device != -1;
58  }
59 
65  static bool deinit() {
66  close(DACChannel::s_device);
67  return true;
68  }
69 
75  auto operator=(float value) const noexcept {
76  u32 rawValue = std::max<float>(value * MaxValue - Offset, 0.0F);
77 
78  std::string data = std::to_string(rawValue);
79  lseek(DACChannel::s_device, 0, 0);
80  if (write(DACChannel::s_device, data.data(), data.size()) <= 0)
81  return;
82  lseek(DACChannel::s_device, 0, 0);
83  }
84 
90  operator float() const noexcept {
91  std::string data(0xFF, 0x00);
92 
93  if (read(DACChannel::s_device, data.data(), data.size()) < 0)
94  return 0.0F;
95  lseek(DACChannel::s_device, 0, 0);
96 
97  return float(std::max<float>(std::stof(data) + Offset, 0)) / MaxValue;
98  }
99 
100  private:
101  DACChannel() = default;
102 
106  template<auto, template<auto, u8, u32, u32> typename>
107  friend struct bsp::drv::DAConverter;
108 
109  static inline int s_device = -1;
110  };
111 
112 }
bsp::mid::drv::DACChannel::init
static bool init()
Init function.
Definition: dac.hpp:55
dac.hpp
Frontend for the DAC abstraction.
bsp::mid::drv::DACChannel::ReferenceVoltage
constexpr static auto ReferenceVoltage
Definition: dac.hpp:48
bsp::mid::drv::DACChannel
DAC Channel implementation for Asgard.
Definition: dac.hpp:44
bsp::mid::drv::DACChannel::deinit
static bool deinit()
Deinit function.
Definition: dac.hpp:65
u32
uint32_t u32
Definition: types.h:38
bsp::mid::drv::DACChannel::operator=
auto operator=(const DACChannel &)=delete
bsp::mid::drv::DACChannel::DACChannel
DACChannel(const DACChannel &)=delete
bsp::mid::drv::DACChannel::operator=
auto operator=(float value) const noexcept
Set the current DAC value.
Definition: dac.hpp:75
bsp::mid::drv
Definition: adc.hpp:32
bsp::drv::DAConverter
Base class for DAC abstraction.
Definition: dac.hpp:40