MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_io_factory.hpp
Go to the documentation of this file.
1/**
2 * @file meshx_io_factory.hpp
3 * @brief MeshX IO Factory
4 *
5 * This file defines the IO factory for creating IO instances based on configuration.
6 *
7 * @author MeshX Team
8 * @date 2024
9 */
10
11#ifndef __MESHX_IO_FACTORY_HPP
12#define __MESHX_IO_FACTORY_HPP
13
14#include <memory>
15#include <unordered_map>
16#include <string>
18
19namespace meshx {
20
21/**
22 * @brief IO Type enumeration
23 */
24enum class IoType {
25 GPIO, /**< General Purpose Input/Output */
26 PWM, /**< Pulse Width Modulation */
27 ADC, /**< Analog to Digital Converter (future) */
28 DAC, /**< Digital to Analog Converter (future) */
29 CUSTOM /**< Custom IO type */
30};
31
32/**
33 * @brief IO Configuration Structure
34 */
35struct IoConfig {
36 uint8_t logical_pin; /**< Logical pin number (0-255) */
37 IoType type; /**< IO type */
38 std::string name; /**< Pin name */
39
40 // Type-specific configuration
41 union {
42 struct {
43 uint8_t mode; /**< GPIO mode */
44 uint8_t pull; /**< Pull resistor setting */
45 uint8_t drive; /**< Drive strength */
46 uint8_t initial_level;/**< Initial output level */
47 bool signal_inversion;/**< Signal inversion */
49
50 struct {
51 uint32_t frequency; /**< PWM frequency */
52 uint8_t duty_cycle; /**< Initial duty cycle */
53 uint8_t resolution; /**< PWM resolution */
54 uint8_t channel; /**< Hardware channel */
55 } pwm;
56
57 struct {
58 uint16_t custom_id; /**< Custom function ID */
59 uint32_t custom_data; /**< Custom function data */
62};
63
64/**
65 * @brief IO Factory Class
66 *
67 * This factory creates IO instances based on configuration.
68 */
70public:
71 /**
72 * @brief Get singleton instance
73 *
74 * @return MeshXIoFactory& Reference to singleton instance
75 */
77
78 /**
79 * @brief Register IO type factory function
80 *
81 * @param type IO type
82 * @param factory_func Factory function
83 * @return true if registration successful
84 * @return false if registration failed (type already registered)
85 */
86 bool registerType(IoType type, IoFactoryFunction factory_func);
87
88 /**
89 * @brief Create IO instance
90 *
91 * @param config IO configuration
92 * @return std::unique_ptr<MeshXIoInterface> IO instance pointer
93 */
94 std::unique_ptr<MeshXIoInterface> create(const IoConfig& config);
95
96 /**
97 * @brief Create IO instance from YAML configuration
98 *
99 * @param yaml_config YAML configuration node
100 * @return std::unique_ptr<MeshXIoInterface> IO instance pointer
101 */
102 std::unique_ptr<MeshXIoInterface> createFromYaml(const void* yaml_config);
103
104 /**
105 * @brief Check if IO type is supported
106 *
107 * @param type IO type to check
108 * @return true if type is supported
109 * @return false if type is not supported
110 */
111 bool isTypeSupported(IoType type) const;
112
113private:
114 MeshXIoFactory() = default;
115 ~MeshXIoFactory() = default;
116
117 // Delete copy constructor and assignment operator
120
121 // Factory function registry
122 std::unordered_map<IoType, IoFactoryFunction> factory_registry_;
123};
124
125} // namespace meshx
126
127#endif /* __MESHX_IO_FACTORY_HPP */
bool registerType(IoType type, IoFactoryFunction factory_func)
Register IO type factory function.
Definition meshx_io_factory.cpp:48
static MeshXIoFactory & getInstance()
Get singleton instance.
Definition meshx_io_factory.cpp:26
MeshXIoFactory & operator=(const MeshXIoFactory &)=delete
std::unique_ptr< MeshXIoInterface > createFromYaml(const void *yaml_config)
Create IO instance from YAML configuration.
Definition meshx_io_factory.cpp:87
bool isTypeSupported(IoType type) const
Check if IO type is supported.
Definition meshx_io_factory.cpp:101
MeshXIoFactory(const MeshXIoFactory &)=delete
std::unique_ptr< MeshXIoInterface > create(const IoConfig &config)
Create IO instance.
Definition meshx_io_factory.cpp:69
std::unordered_map< IoType, IoFactoryFunction > factory_registry_
Definition meshx_io_factory.hpp:122
MeshX Abstract IO Interface.
Definition meshx_io_factory.hpp:19
IoType
IO Type enumeration.
Definition meshx_io_factory.hpp:24
@ DAC
Definition meshx_io_factory.hpp:28
@ CUSTOM
Definition meshx_io_factory.hpp:29
@ ADC
Definition meshx_io_factory.hpp:27
@ PWM
Definition meshx_io_factory.hpp:26
@ GPIO
Definition meshx_io_factory.hpp:25
std::unique_ptr< MeshXIoInterface >(*)(uint8_t logical_pin, const char *name) IoFactoryFunction
IO Factory function type.
Definition meshx_io_interface.hpp:176
IO Configuration Structure.
Definition meshx_io_factory.hpp:35
bool signal_inversion
Definition meshx_io_factory.hpp:47
uint8_t pull
Definition meshx_io_factory.hpp:44
uint32_t custom_data
Definition meshx_io_factory.hpp:59
uint32_t frequency
Definition meshx_io_factory.hpp:51
uint8_t resolution
Definition meshx_io_factory.hpp:53
struct meshx::IoConfig::@322131243157366012127327120072350016325131204373::@367141106155340206106161007235363020301330311260 gpio
uint8_t logical_pin
Definition meshx_io_factory.hpp:36
uint8_t duty_cycle
Definition meshx_io_factory.hpp:52
uint8_t mode
Definition meshx_io_factory.hpp:43
struct meshx::IoConfig::@322131243157366012127327120072350016325131204373::@177030051017162327370351266300313027016365020016 custom
uint16_t custom_id
Definition meshx_io_factory.hpp:58
struct meshx::IoConfig::@322131243157366012127327120072350016325131204373::@240320275032304141243012114035354247040336106337 pwm
IoType type
Definition meshx_io_factory.hpp:37
union meshx::IoConfig::@322131243157366012127327120072350016325131204373 config
uint8_t drive
Definition meshx_io_factory.hpp:45
uint8_t initial_level
Definition meshx_io_factory.hpp:46
std::string name
Definition meshx_io_factory.hpp:38
uint8_t channel
Definition meshx_io_factory.hpp:54