Uniot Core
0.8.1
Loading...
Searching...
No Matches
RegisterManager.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "CBORObject.h"
22#include "GpioRegister.h"
23#include "ObjectRegister.h"
24
25namespace uniot {
26
38 public:
43
48
52 void operator=(RegisterManager const &) = delete;
53
61 template <typename... Args>
62 void setDigitalInput(uint8_t first, Args... args) {
63 mGpioRegistry.setDigitalInput(first, args...);
64 }
65
73 template <typename... Args>
74 void setDigitalOutput(uint8_t first, Args... args) {
75 mGpioRegistry.setDigitalOutput(first, args...);
76 }
77
85 template <typename... Args>
86 void setAnalogInput(uint8_t first, Args... args) {
87 mGpioRegistry.setAnalogInput(first, args...);
88 }
89
97 template <typename... Args>
98 void setAnalogOutput(uint8_t first, Args... args) {
99 mGpioRegistry.setAnalogOutput(first, args...);
100 }
101
111 bool link(const String &name, RecordPtr link, uint32_t id = FOURCC(____)) {
112 return mObjectRegistry.link(name, link, id);
113 }
114
124 bool getGpio(const String &name, size_t index, uint8_t &outValue) const {
125 return mGpioRegistry.getRegisterValue(name, index, outValue);
126 }
127
136 template <typename T>
137 T *getObject(const String &name, size_t index) {
138 return mObjectRegistry.get<T>(name, index);
139 }
140
149 size_t getRegisterLength(const String &name) const {
150 auto length = mGpioRegistry.getRegisterLength(name);
151 if (!length) {
152 length = mObjectRegistry.getRegisterLength(name);
153 }
154 return length;
155 }
156
166 mGpioRegistry.iterateRegisters([&](const String &name, SharedPointer<Array<uint8_t>> values) {
167 obj.putArray(name.c_str()).append(values->size(), values->raw());
168 });
169 mObjectRegistry.iterateRegisters([&](const String &name, SharedPointer<Array<Pair<uint32_t, RecordPtr>>> values) {
170 auto arr = obj.putArray(name.c_str());
171 for (size_t i = 0; i < values->size(); i++) {
172 auto &pair = (*values)[i];
173 arr.append(pair.first);
174 }
175 });
176 }
177
178 private:
179 ObjectRegister mObjectRegistry;
180 GpioRegister mGpioRegistry;
181};
182
183} // namespace uniot
Definition Array.h:66
Array & append(int value)
Append an integer to the array.
Definition CBORObject.h:593
Definition CBORObject.h:40
Array putArray(int key)
Create or get an array at a specific integer key.
Definition CBORObject.h:121
Definition GpioRegister.h:38
Definition ObjectRegister.h:46
RegisterManager(RegisterManager const &)=delete
Copy constructor is deleted to prevent unexpected behavior.
void setDigitalOutput(uint8_t first, Args... args)
Sets one or more pins as digital outputs.
Definition RegisterManager.h:74
size_t getRegisterLength(const String &name) const
Gets the number of elements in the specified named register.
Definition RegisterManager.h:149
T * getObject(const String &name, size_t index)
Gets an object of the specified type from a named register.
Definition RegisterManager.h:137
void setAnalogInput(uint8_t first, Args... args)
Sets one or more pins as analog inputs.
Definition RegisterManager.h:86
void serializeRegisters(CBORObject &obj)
Serializes all registers (GPIO and Object) to a CBOR object.
Definition RegisterManager.h:165
RegisterManager()
Default constructor.
Definition RegisterManager.h:42
void setDigitalInput(uint8_t first, Args... args)
Sets one or more pins as digital inputs.
Definition RegisterManager.h:62
void operator=(RegisterManager const &)=delete
Assignment operator is deleted to prevent unexpected behavior.
void setAnalogOutput(uint8_t first, Args... args)
Sets one or more pins as analog outputs.
Definition RegisterManager.h:98
bool getGpio(const String &name, size_t index, uint8_t &outValue) const
Gets a GPIO value from a named register.
Definition RegisterManager.h:124
bool link(const String &name, RecordPtr link, uint32_t id=FOURCC(____))
Links an object to a named register.
Definition RegisterManager.h:111
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:160
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:169
#define FOURCC(name)
Creates a FourCC constant from a string literal.
Definition Common.h:107
ObjectRegisterRecord * RecordPtr
Type alias for ObjectRegisterRecord pointers for better readability.
Definition ObjectRegister.h:33
Contains all classes and functions related to the Uniot Core.