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_interface.hpp
Go to the documentation of this file.
1/**
2 * @file meshx_io_interface.hpp
3 * @brief MeshX Abstract IO Interface
4 *
5 * This file defines the abstract IO interface for unified IO operations.
6 * The interface provides a function-based API for extensible GPIO, PWM, and custom IO.
7 *
8 * @author MeshX Team
9 * @date 2024
10 */
11
12#ifndef __MESHX_IO_INTERFACE_HPP
13#define __MESHX_IO_INTERFACE_HPP
14
15#include <cstdint>
16#include <vector>
17#include <memory>
18#include "meshx_c_header.h" // Follow C/C++ boundary pattern
19
20namespace meshx {
21
22/**
23 * @brief IO Function Types for function-based API
24 */
25enum class IoFunction {
26 SET_LEVEL = 0, /**< Set pin level function */
27 GET_LEVEL, /**< Get pin level function */
28 TOGGLE, /**< Toggle pin function */
29 SET_PWM_DUTY, /**< Set PWM duty cycle function */
30 SET_PWM_FREQUENCY, /**< Set PWM frequency function */
31 REGISTER_INTERRUPT, /**< Register interrupt function */
32 UNREGISTER_INTERRUPT, /**< Unregister interrupt function */
33 ENABLE_INTERRUPT, /**< Enable/disable interrupt function */
34 CUSTOM_FUNCTION, /**< Custom function (for extensibility) */
35 FUNCTION_MAX /**< Maximum function type */
36};
37
38/**
39 * @brief Abstract IO Interface
40 *
41 * This interface provides a unified API for all IO operations using
42 * a function-based approach for extensibility.
43 */
45public:
46 virtual ~MeshXIoInterface() = default;
47
48 /**
49 * @brief Execute IO function
50 *
51 * This is the core function-based API for all IO operations.
52 *
53 * @param function IO function to execute
54 * @param args Function arguments vector
55 * @return int Error code (0 = success, negative = error)
56 */
57 virtual int execute(IoFunction function, const std::vector<uint32_t>& args) = 0;
58
59 /**
60 * @brief Get logical pin number
61 *
62 * @return uint8_t Logical pin number (0-255)
63 */
64 virtual uint8_t getLogicalPin() const = 0;
65
66 /**
67 * @brief Get pin name
68 *
69 * @return const char* Pin name string
70 */
71 virtual const char* getName() const = 0;
72
73 /**
74 * @brief Check if function is supported
75 *
76 * @param function IO function to check
77 * @return true if function is supported
78 * @return false if function is not supported
79 */
80 virtual bool isFunctionSupported(IoFunction function) const = 0;
81
82 // Convenience methods (implemented in terms of execute())
83
84 /**
85 * @brief Set pin level (convenience method)
86 *
87 * @param level Pin level (0 = low, 1 = high)
88 * @return int Error code (0 = success, negative = error)
89 */
90 virtual int setLevel(uint8_t level) {
91 return execute(IoFunction::SET_LEVEL, {static_cast<uint32_t>(level)});
92 }
93
94 /**
95 * @brief Get pin level (convenience method)
96 *
97 * @param[out] level Pointer to store pin level
98 * @return int Error code (0 = success, negative = error)
99 */
100 virtual int getLevel(uint8_t* level) {
101 if (!level) return -1;
102 std::vector<uint32_t> args = {static_cast<uint32_t>(reinterpret_cast<uintptr_t>(level))};
103 return execute(IoFunction::GET_LEVEL, args);
104 }
105
106 /**
107 * @brief Toggle pin level (convenience method)
108 *
109 * @return int Error code (0 = success, negative = error)
110 */
111 virtual int toggle() {
112 return execute(IoFunction::TOGGLE, {});
113 }
114
115 /**
116 * @brief Set PWM duty cycle (convenience method)
117 *
118 * @param duty_cycle Duty cycle (0-100%)
119 * @return int Error code (0 = success, negative = error)
120 */
121 virtual int setPwmDutyCycle(uint8_t duty_cycle) {
122 return execute(IoFunction::SET_PWM_DUTY, {static_cast<uint32_t>(duty_cycle)});
123 }
124
125 /**
126 * @brief Set PWM frequency (convenience method)
127 *
128 * @param frequency Frequency in Hz
129 * @return int Error code (0 = success, negative = error)
130 */
131 virtual int setPwmFrequency(uint32_t frequency) {
132 return execute(IoFunction::SET_PWM_FREQUENCY, {frequency});
133 }
134
135 /**
136 * @brief Register interrupt (convenience method)
137 *
138 * @param callback Function pointer to callback
139 * @param user_data User data passed to callback
140 * @param trigger_type Interrupt trigger type
141 * @return int Error code (0 = success, negative = error)
142 */
143 virtual int registerInterrupt(void (*callback)(uint8_t, void*),
144 void* user_data,
145 uint8_t trigger_type) {
147 reinterpret_cast<uintptr_t>(callback),
148 reinterpret_cast<uintptr_t>(user_data),
149 static_cast<uint32_t>(trigger_type)
150 });
151 }
152
153 /**
154 * @brief Unregister interrupt (convenience method)
155 *
156 * @return int Error code (0 = success, negative = error)
157 */
158 virtual int unregisterInterrupt() {
160 }
161
162 /**
163 * @brief Enable/disable interrupt (convenience method)
164 *
165 * @param enable true to enable, false to disable
166 * @return int Error code (0 = success, negative = error)
167 */
168 virtual int enableInterrupt(bool enable) {
169 return execute(IoFunction::ENABLE_INTERRUPT, {static_cast<uint32_t>(enable ? 1 : 0)});
170 }
171};
172
173/**
174 * @brief IO Factory function type
175 */
176using IoFactoryFunction = std::unique_ptr<MeshXIoInterface>(*)(uint8_t logical_pin, const char* name);
177
178} // namespace meshx
179
180#endif /* __MESHX_IO_INTERFACE_HPP */
Abstract IO Interface.
Definition meshx_io_interface.hpp:44
virtual const char * getName() const =0
Get pin name.
virtual int unregisterInterrupt()
Unregister interrupt (convenience method).
Definition meshx_io_interface.hpp:158
virtual int registerInterrupt(void(*callback)(uint8_t, void *), void *user_data, uint8_t trigger_type)
Register interrupt (convenience method).
Definition meshx_io_interface.hpp:143
virtual uint8_t getLogicalPin() const =0
Get logical pin number.
virtual int setPwmDutyCycle(uint8_t duty_cycle)
Set PWM duty cycle (convenience method).
Definition meshx_io_interface.hpp:121
virtual ~MeshXIoInterface()=default
virtual int enableInterrupt(bool enable)
Enable/disable interrupt (convenience method).
Definition meshx_io_interface.hpp:168
virtual int toggle()
Toggle pin level (convenience method).
Definition meshx_io_interface.hpp:111
virtual int setLevel(uint8_t level)
Set pin level (convenience method).
Definition meshx_io_interface.hpp:90
virtual int execute(IoFunction function, const std::vector< uint32_t > &args)=0
Execute IO function.
virtual int setPwmFrequency(uint32_t frequency)
Set PWM frequency (convenience method).
Definition meshx_io_interface.hpp:131
virtual int getLevel(uint8_t *level)
Get pin level (convenience method).
Definition meshx_io_interface.hpp:100
virtual bool isFunctionSupported(IoFunction function) const =0
Check if function is supported.
This file includes all required header file encapslated under C env.
Definition meshx_io_factory.hpp:19
IoFunction
IO Function Types for function-based API.
Definition meshx_io_interface.hpp:25
@ REGISTER_INTERRUPT
Definition meshx_io_interface.hpp:31
@ FUNCTION_MAX
Definition meshx_io_interface.hpp:35
@ TOGGLE
Definition meshx_io_interface.hpp:28
@ UNREGISTER_INTERRUPT
Definition meshx_io_interface.hpp:32
@ SET_PWM_DUTY
Definition meshx_io_interface.hpp:29
@ SET_PWM_FREQUENCY
Definition meshx_io_interface.hpp:30
@ CUSTOM_FUNCTION
Definition meshx_io_interface.hpp:34
@ SET_LEVEL
Definition meshx_io_interface.hpp:26
@ GET_LEVEL
Definition meshx_io_interface.hpp:27
@ ENABLE_INTERRUPT
Definition meshx_io_interface.hpp:33
std::unique_ptr< MeshXIoInterface >(*)(uint8_t logical_pin, const char *name) IoFactoryFunction
IO Factory function type.
Definition meshx_io_interface.hpp:176