Falcon
Loading...
Searching...
No Matches
dio.hpp
1// ---------------------------------------------------------------------
2// This file is part of falcon-core.
3//
4// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
5//
6// Falcon-server 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// Falcon-server 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 falcon-core. If not, see <http://www.gnu.org/licenses/>.
18// ---------------------------------------------------------------------
19
20#pragma once
21
22#include <cstdint>
23#include <stdexcept>
24#include <string>
25#include <vector>
26
27enum class DigitalOutputMode { NONE, HIGH, LOW, TOGGLE, PULSE };
28
29class DigitalDeviceError : public std::runtime_error {
30 public:
31 DigitalDeviceError(std::string msg) : runtime_error(msg) {}
32 DigitalDeviceError(std::string const command, std::string error)
33 : std::runtime_error("Error executing command " + command + " (error = " + error + ").") {}
34};
35
36class DigitalStateError : public std::runtime_error {
37 public:
38 DigitalStateError(std::string msg) : runtime_error(msg) {}
39};
40
41class DigitalState {
42 public:
43 DigitalState(uint32_t nchannels = 0) : state_(nchannels, false) {}
44
45 uint32_t nchannels() const;
46
47 typename std::vector<bool>::reference operator[](uint32_t channel);
48
49 bool state(uint32_t channel) const;
50 std::vector<bool>& state();
51 std::vector<bool> state(std::vector<uint32_t> channels) const;
52
53 void set_state(uint32_t channel, bool value);
54 void set_state(std::vector<uint32_t> channels, bool value);
55 void set_state(bool value);
56 void set_state(std::vector<bool> values);
57 void set_state(std::vector<uint32_t> channels, std::vector<bool> values);
58
59 void toggle_state(uint32_t channel);
60 void toggle_state(std::vector<uint32_t> channels);
61
62 std::string to_string(std::string high = "1", std::string low = "0",
63 std::string spacer = "") const;
64
65 protected:
66 std::vector<bool> state_;
67};
68
69class DigitalDevice {
70 public:
71 DigitalDevice(std::string type) : type_(type) {}
72 std::string type() const;
73 virtual std::string description() const;
74 virtual uint32_t nchannels() const = 0;
75 virtual DigitalState read_state() const = 0;
76 virtual void write_state(DigitalState& state) = 0;
77
78 protected:
79 std::string type_;
80};
81
82class DigitalOutputProtocol {
83 public:
84 DigitalOutputProtocol(uint32_t nchannels, unsigned int pulse_width,
85 DigitalOutputMode default_mode = DigitalOutputMode::NONE);
86 DigitalOutputMode mode(uint32_t channel) const { return mode_[channel]; }
87
88 void set_mode(uint32_t channel, DigitalOutputMode mode = DigitalOutputMode::NONE);
89 void set_mode(std::vector<uint32_t> channels, DigitalOutputMode mode = DigitalOutputMode::NONE);
90
91 unsigned int pulse_width() const;
92 void set_pulse_width(unsigned int value);
93
94 std::vector<uint32_t> find_channels(DigitalOutputMode mode = DigitalOutputMode::NONE);
95
96 void execute(DigitalDevice& device);
97
98 protected:
99 uint32_t nchannels_;
100 unsigned int pulse_width_;
101 std::vector<DigitalOutputMode> mode_;
102};
Definition dio.hpp:69
Definition dio.hpp:41