VMX-pi C++ HAL Library for Raspberry Pi
VMX-pi Robotics Controller & Vision/Motion Processor
VMXIO.h
1 /* ============================================
2 VMX-pi HAL source code is placed under the MIT license
3 Copyright (c) 2017 Kauai Labs
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18 THE SOFTWARE.
19 ===============================================
20 */
21 
22 #ifndef VMXIO_H_
23 #define VMXIO_H_
24 
25 #include "VMXResourceConfig.h"
26 #include "VMXErrors.h"
27 #include "VMXTime.h"
28 #include <unordered_set>
29 #include <list>
30 
31 class PIGPIOClient;
32 class IOCXClient;
33 class MISCClient;
34 class VMXChannelManager;
35 class VMXResourceManager;
36 
37 class VMXIO_PIGPIOInterruptSink;
38 
41 class VMXIO {
42 
43  friend class VMXPi;
44 
45  PIGPIOClient& pigpio;
46  IOCXClient& iocx;
47  MISCClient& misc;
48  VMXChannelManager& chan_mgr;
49  VMXResourceManager& res_mgr;
50  VMXTime& time;
51  VMXIO_PIGPIOInterruptSink *p_int_sink;
52 
53  bool DisconnectAnalogInputInterrupt(uint8_t analog_trigger_num); /* ????? */
54 
55  VMXIO(PIGPIOClient& pigpio, IOCXClient& iocx, MISCClient& misc, VMXChannelManager& chan_mgr, VMXResourceManager& res_mgr, VMXTime& time_ref);
56  void ReleaseResources();
57  virtual ~VMXIO();
58 
59 public:
60 
61  /*** RESOURCE AND CHANNEL ENUMERATION */
62  uint8_t GetNumResourcesByType(VMXResourceType resource_type);
63  uint8_t GetNumChannelsByCapability(VMXChannelCapability channel_capability);
64  uint8_t GetNumChannelsByType(VMXChannelType channel_type, VMXChannelIndex& first_channel_index);
65  bool GetChannelCapabilities(VMXChannelIndex channel_index, VMXChannelType& channel_type, VMXChannelCapability& capability_bits);
66  bool ChannelSupportsCapability(VMXChannelIndex channel_index, VMXChannelCapability capability);
67 
68  /*** RESOURCE HANDLE ACQUISITION */
69  bool GetResourceHandle(VMXResourceType resource_type, VMXResourceIndex res_index, VMXResourceHandle& resource_handle, VMXErrorCode *errcode = 0);
70  bool GetResourcesCompatibleWithChannelAndCapability(VMXChannelIndex channel_index, VMXChannelCapability capability, std::list<VMXResourceHandle>& compatible_res_handles);
71  bool GetUnallocatedResourcesCompatibleWithChannelAndCapability(VMXChannelIndex channel_index, VMXChannelCapability capability, std::list<VMXResourceHandle>& unallocated_compatible_res_handles);
72  bool GetChannelsCompatibleWithResource(VMXResourceHandle resource_handle, VMXChannelIndex& first_channel_index, uint8_t& num_channels);
73 
74  /*** RESOURCE ALLOCATION ***/
75 
76  bool IsResourceAllocated(VMXResourceHandle resource, bool& allocated, bool& is_shared, VMXErrorCode *errcode = 0);
77  bool AllocateResource(VMXResourceHandle resource, VMXErrorCode* errcode = 0);
78  bool DeallocateResource(VMXResourceHandle resource, VMXErrorCode *errcode = 0);
79  bool DeallocateAllResources(VMXErrorCode *last_errorcode = 0);
80 
81  /*** RESOURCE-CHANNEL ROUTING ***/
82 
83  bool RouteChannelToResource(VMXChannelIndex channel, VMXResourceHandle resource, VMXErrorCode* errcode = 0);
84  bool UnrouteChannelFromResource(VMXChannelIndex channel, VMXResourceHandle resource, VMXErrorCode *errcode = 0);
85  bool UnrouteAllChannelsFromResource(VMXResourceHandle resource, VMXErrorCode *errcode = 0);
86 
87  /*** RESOURCE CONFIGURATION (see VMXResourceConfig.h for various configuration classes) ***/
88 
89  bool SetResourceConfig(VMXResourceHandle resource, const VMXResourceConfig* p_config, VMXErrorCode *errcode = 0);
90  bool GetResourceConfig(VMXResourceHandle resource, VMXResourceConfig*& p_config, VMXErrorCode *errcode = 0);
91  bool GetResourceDefaultConfig(VMXResourceHandle resource, VMXResourceConfig*& p_config, VMXErrorCode *errcode = 0);
92 
93  /*** RESOURCE ACTIVATION ***/
94 
95  bool IsResourceActive(VMXResourceHandle, bool &active, VMXErrorCode* errcode = 0);
96  bool ActivateResource(VMXResourceHandle resource, VMXErrorCode* errcode = 0);
97  bool DeactivateResource(VMXResourceHandle resource, VMXErrorCode* errcode = 0);
98 
99  /*** ACTIVATION HELPERS ***/
100  bool ActivateSinglechannelResource(VMXChannelIndex channel_index, VMXChannelCapability channel_capability,
101  VMXResourceHandle& res_handle, const VMXResourceConfig *res_cfg = 0, VMXErrorCode *errcode = 0);
102  bool ActivateMultichannelResource(uint8_t num_channels, VMXChannelIndex *p_channel_indexes, VMXChannelCapability *p_channel_capabilities,
103  VMXResourceHandle& res_handle, const VMXResourceConfig *res_cfg = 0, VMXErrorCode *errcode = 0);
104 
105  /*** RESOURCE ACTIONS ***/
106 
107  /* DIO Resources */
108  bool DIO_Get(VMXResourceHandle dio_res_handle, bool& high, VMXErrorCode *errcode = 0);
109  bool DIO_Set(VMXResourceHandle dio_res_handle, bool high, VMXErrorCode *errcode = 0);
110 
111  /* PWMGenerator */
113  const uint8_t MIN_PWM_GENERATOR_DUTY_CYCLE = 0;
115  const uint8_t MAX_PWM_GENERATOR_DUTY_CYCLE = 255;
116  bool PWMGenerator_SetDutyCycle(VMXResourceHandle pwmgen_res_handle, VMXResourcePortIndex port_index, uint8_t duty_cycle, VMXErrorCode *errcode = 0);
117 
118  /* PWMCapture */
119  /* Returns current PWM Capture count of 1us ticks in the current duty cycle. */
120  bool PWMCapture_GetCount(VMXResourceHandle pwmcap_res_handle, int32_t& count, VMXErrorCode *errcode = 0);
121 
122  /* Encoder */
123  /* Returns current integrated count of encoder ticks (at the current resolution) */
124  bool Encoder_GetCount(VMXResourceHandle encoder_res_handle, int32_t& count, VMXErrorCode *errcode = 0);
126  typedef enum { EncoderForward, EncoderReverse } EncoderDirection;
127  bool Encoder_GetDirection(VMXResourceHandle encoder_res_handle, EncoderDirection& direction, VMXErrorCode *errcode = 0);
128  bool Encoder_Reset(VMXResourceHandle encoder_res_handle, VMXErrorCode *errcode = 0);
129 
130  /* Accumulator */
131  /* NOTE: The resolution of Accumulator values is dependent upon the current number of bits */
132  /* 0 bits: 12-bit resolution, 1 bit: 13-bit resolution, etc. */
133  /* See the AccumulatorConfig for more information on modifying these bits */
134 
135  bool Accumulator_GetOversampleValue(VMXResourceHandle accum_res_handle, uint32_t& oversample_value, VMXErrorCode *errcode = 0);
136  bool Accumulator_GetAverageValue(VMXResourceHandle accum_res_handle, uint32_t& average_value, VMXErrorCode *errcode = 0);
137  bool Accumulator_GetInstantaneousValue(VMXResourceHandle accum_res_handle, uint32_t& average_value, VMXErrorCode *errcode = 0);
138  bool Accumulator_GetFullScaleVoltage(float& full_scale_voltage, VMXErrorCode *errcode = 0);
139  bool Accumulator_GetAverageVoltage(VMXResourceHandle accum_res_handle, float& average_value, VMXErrorCode *errcode = 0);
140 
141  /* Analog Trigger */
143  typedef enum { BelowThreshold, AboveThreshold, InWindow } AnalogTriggerState;
144  bool AnalogTrigger_GetState(VMXResourceHandle antrig_res_handle, AnalogTriggerState& state, VMXErrorCode *errcode = 0);
145 
146  /* Interrupt */
147 
148  /* UART */
149  bool UART_Write(VMXResourceHandle uart_res_handle, uint8_t *p_data, uint16_t size, VMXErrorCode *errcode = 0);
150  bool UART_Read(VMXResourceHandle uart_res_handle, uint8_t *p_data, uint16_t max_size, uint16_t& actual_size_read, VMXErrorCode *errcode = 0);
151  bool UART_GetBytesAvailable(VMXResourceHandle uart_es_handle, uint16_t& size, VMXErrorCode *errcode = 0);
152 
153  /* SPI */
154  bool SPI_Write(VMXResourceHandle spi_res_handle, uint8_t *p_send_data, uint16_t size, VMXErrorCode *errcode = 0);
155  bool SPI_Read(VMXResourceHandle spi_res_handle, uint8_t *p_rcv_data, uint16_t size, VMXErrorCode *errcode = 0);
156  bool SPI_Transaction(VMXResourceHandle spi_res_handle, uint8_t *p_send_data, uint8_t *p_rcv_data, uint16_t size, VMXErrorCode *errcode = 0);
157 
158  /* I2C */
159  bool I2C_Write(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress, uint8_t* dataToSend, int32_t sendSize, VMXErrorCode *errcode = 0);
160  bool I2C_Read(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress, uint8_t* buffer, int32_t count, VMXErrorCode *errcode = 0);
161  bool I2C_Transaction(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress,
162  uint8_t* dataToSend, uint16_t sendSize,
163  uint8_t* dataReceived, uint16_t receiveSize, VMXErrorCode *errcode = 0);
164 };
165 
166 #endif /* VMXIO_H_ */
bool ActivateSinglechannelResource(VMXChannelIndex channel_index, VMXChannelCapability channel_capability, VMXResourceHandle &res_handle, const VMXResourceConfig *res_cfg=0, VMXErrorCode *errcode=0)
Attempts to activate the specified VMXResourceHandle, by first allocating the resource, then routing the specified VMX Channel to it, then applying the specified configuration, and then finally performing resource activation.
Definition: VMXIO.cpp:1244
bool AnalogTrigger_GetState(VMXResourceHandle antrig_res_handle, AnalogTriggerState &state, VMXErrorCode *errcode=0)
Returns the current state from a VMX AnalogTrigger Resource.
Definition: VMXIO.cpp:1809
Top-level Library Class providing access to all VMX-pi functionality.
Definition: VMXPi.h:37
EncoderDirection
Enumeration of Encoder Directions.
Definition: VMXIO.h:126
bool GetChannelsCompatibleWithResource(VMXResourceHandle resource_handle, VMXChannelIndex &first_channel_index, uint8_t &num_channels)
Returns a list of VMXChannelIndexes which are compatible with the specified VMXResourceHandle.
Definition: VMXIO.cpp:255
bool ChannelSupportsCapability(VMXChannelIndex channel_index, VMXChannelCapability capability)
Indicates whether the specified VMXChannelIndex supports the specified VMXChannelCapability.
Definition: VMXIO.cpp:333
bool UART_Read(VMXResourceHandle uart_res_handle, uint8_t *p_data, uint16_t max_size, uint16_t &actual_size_read, VMXErrorCode *errcode=0)
Reads data from the specified VMX UART Resource.
Definition: VMXIO.cpp:1878
bool DeallocateResource(VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to deallocate the specified VMXResourceHandle.
Definition: VMXIO.cpp:443
uint8_t GetNumChannelsByType(VMXChannelType channel_type, VMXChannelIndex &first_channel_index)
Returns the number of VMX Channels of the specified VMXChannelType.
Definition: VMXIO.cpp:208
uint16_t VMXResourceHandle
Handle to a particular VMX Resource.
Definition: VMXResource.h:61
bool IsResourceAllocated(VMXResourceHandle resource, bool &allocated, bool &is_shared, VMXErrorCode *errcode=0)
Indicates whether the specified VMXResourceHandle is currently allocated, and whether it is a shared ...
Definition: VMXIO.cpp:355
bool Encoder_GetCount(VMXResourceHandle encoder_res_handle, int32_t &count, VMXErrorCode *errcode=0)
Returns the current Encoder count value from a VMX Encoder Resource.
Definition: VMXIO.cpp:1522
Base structure representing VMXResourceType-specific configuration data that must be set to a valid d...
Definition: VMXResourceConfig.h:33
AnalogTriggerState
Enumeration of Analog Trigger States.
Definition: VMXIO.h:143
bool SPI_Read(VMXResourceHandle spi_res_handle, uint8_t *p_rcv_data, uint16_t size, VMXErrorCode *errcode=0)
Reads data from the specified VMX SPI Resource.
Definition: VMXIO.cpp:1958
uint8_t GetNumChannelsByCapability(VMXChannelCapability channel_capability)
Returns the number of VMX Channels of the specified VMXChannelCapability.
Definition: VMXIO.cpp:196
bool I2C_Transaction(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress, uint8_t *dataToSend, uint16_t sendSize, uint8_t *dataReceived, uint16_t receiveSize, VMXErrorCode *errcode=0)
Simultaneously Writes/Reads data to/from the specified VMX I2C Resource.
Definition: VMXIO.cpp:2073
uint8_t GetNumResourcesByType(VMXResourceType resource_type)
Returns the number of VMX Resources of the specified VMXResourceType.
Definition: VMXIO.cpp:185
bool GetResourceHandle(VMXResourceType resource_type, VMXResourceIndex res_index, VMXResourceHandle &resource_handle, VMXErrorCode *errcode=0)
Returns the VMXResourceHandle of the VMX Resource identified by the specified VMXResourceType and VMX...
Definition: VMXIO.cpp:225
const uint8_t MAX_PWM_GENERATOR_DUTY_CYCLE
Maximum PWM Generator Duty Cycle value.
Definition: VMXIO.h:115
bool GetResourcesCompatibleWithChannelAndCapability(VMXChannelIndex channel_index, VMXChannelCapability capability, std::list< VMXResourceHandle > &compatible_res_handles)
Returns a list of VMXResourceHandles which are compatible with the specified VMXChannelIndex and VMXC...
Definition: VMXIO.cpp:239
bool Accumulator_GetOversampleValue(VMXResourceHandle accum_res_handle, uint32_t &oversample_value, VMXErrorCode *errcode=0)
Returns the current oversample value from a VMX Analog Accumulator Resource.
Definition: VMXIO.cpp:1703
bool RouteChannelToResource(VMXChannelIndex channel, VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to route the specified VMXChannelIndex to the allocated VMXResourceHandle Note: If the resou...
Definition: VMXIO.cpp:521
bool GetResourceDefaultConfig(VMXResourceHandle resource, VMXResourceConfig *&p_config, VMXErrorCode *errcode=0)
Gets the default configuration of the specified VMXResourceHandle.
Definition: VMXIO.cpp:868
bool DIO_Set(VMXResourceHandle dio_res_handle, bool high, VMXErrorCode *errcode=0)
Sets the current signal state of a VMX DigitalIO Resource in Digital Output mode. ...
Definition: VMXIO.cpp:1668
bool Accumulator_GetAverageValue(VMXResourceHandle accum_res_handle, uint32_t &average_value, VMXErrorCode *errcode=0)
Returns the current averaged value from a VMX Analog Accumulator Resource.
Definition: VMXIO.cpp:1731
bool I2C_Write(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress, uint8_t *dataToSend, int32_t sendSize, VMXErrorCode *errcode=0)
Writes the specified data to the specified VMX I2C Resource.
Definition: VMXIO.cpp:2014
uint8_t VMXResourceIndex
Zero-based index of a particular VMX Resource, relative to a specific VMXResourceType.
Definition: VMXResource.h:59
bool SPI_Write(VMXResourceHandle spi_res_handle, uint8_t *p_send_data, uint16_t size, VMXErrorCode *errcode=0)
Writes the specified data to the specified VMX SPI Resource.
Definition: VMXIO.cpp:1931
bool GetResourceConfig(VMXResourceHandle resource, VMXResourceConfig *&p_config, VMXErrorCode *errcode=0)
Gets the configuration of the specified VMXResourceHandle.
Definition: VMXIO.cpp:841
bool ActivateMultichannelResource(uint8_t num_channels, VMXChannelIndex *p_channel_indexes, VMXChannelCapability *p_channel_capabilities, VMXResourceHandle &res_handle, const VMXResourceConfig *res_cfg=0, VMXErrorCode *errcode=0)
Attempts to activate the specified VMXResourceHandle, by first allocating the resource, then routing the specified VMX Channels to it, then applying the specified configuration, and then finally performing resource activation.
Definition: VMXIO.cpp:1270
bool AllocateResource(VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to allocate the specified VMXResourceHandle.
Definition: VMXIO.cpp:399
bool GetChannelCapabilities(VMXChannelIndex channel_index, VMXChannelType &channel_type, VMXChannelCapability &capability_bits)
Returns the VMXChannelType and VMXChannelCapability for the specified VMXChannelIndex.
Definition: VMXIO.cpp:295
bool I2C_Read(VMXResourceHandle i2c_res_handle, uint8_t deviceAddress, uint8_t *buffer, int32_t count, VMXErrorCode *errcode=0)
Reads data from the specified VMX I2C Resource.
Definition: VMXIO.cpp:2041
VMXResourceType
Enumerates the various types of VMXResources.
Definition: VMXResource.h:33
uint8_t VMXChannelIndex
Type representing the 0-based index of a VMX Channel.
Definition: VMXChannel.h:105
bool UART_Write(VMXResourceHandle uart_res_handle, uint8_t *p_data, uint16_t size, VMXErrorCode *errcode=0)
Writes the specified data to the specified VMX UART Resource.
Definition: VMXIO.cpp:1848
bool GetUnallocatedResourcesCompatibleWithChannelAndCapability(VMXChannelIndex channel_index, VMXChannelCapability capability, std::list< VMXResourceHandle > &unallocated_compatible_res_handles)
Returns a list of unallocated VMXResourceHandles which are compatible with the specified VMXChannelIn...
Definition: VMXIO.cpp:270
const uint8_t MIN_PWM_GENERATOR_DUTY_CYCLE
Minimum PWM Generator Duty Cycle value.
Definition: VMXIO.h:113
VMXTime time
The VMXTime object providing access to VMX-pi Time functionality.
Definition: VMXPi.h:46
bool UnrouteChannelFromResource(VMXChannelIndex channel, VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to unroute the specified VMXChannelIndex from the allocated VMXResourceHandle.
Definition: VMXIO.cpp:679
The VMXTime class provides time management functions, including system timestamps, VMX Real-time Clock access, timer notifications and current thread time-delay.
Definition: VMXTime.h:38
bool IsResourceActive(VMXResourceHandle, bool &active, VMXErrorCode *errcode=0)
Indicates whether the specified VMXResourceHandle is currently active.
Definition: VMXIO.cpp:378
bool Encoder_GetDirection(VMXResourceHandle encoder_res_handle, EncoderDirection &direction, VMXErrorCode *errcode=0)
Returns the current EncoderDirection value from a VMX Encoder Resource.
Definition: VMXIO.cpp:1545
bool Encoder_Reset(VMXResourceHandle encoder_res_handle, VMXErrorCode *errcode=0)
Sets the VMX Encoder Resource&#39;s count value to 0.
Definition: VMXIO.cpp:1574
The VMXIO class provides access to VMX Analog/Digital IO functions, including VMX Channel and VMX Res...
Definition: VMXIO.h:41
bool DeallocateAllResources(VMXErrorCode *last_errorcode=0)
Attempts to deallocate all currently-allocated VMXResourceHandles.
Definition: VMXIO.cpp:494
bool Accumulator_GetAverageVoltage(VMXResourceHandle accum_res_handle, float &average_value, VMXErrorCode *errcode=0)
Returns the current averaged value in voltage units from a VMX Analog Accumulator Resource...
Definition: VMXIO.cpp:1789
bool Accumulator_GetInstantaneousValue(VMXResourceHandle accum_res_handle, uint32_t &average_value, VMXErrorCode *errcode=0)
Returns the current instantaneous (non-averaged) value from a VMX Analog Accumulator Resource...
Definition: VMXIO.cpp:1757
bool SetResourceConfig(VMXResourceHandle resource, const VMXResourceConfig *p_config, VMXErrorCode *errcode=0)
Sets the configuration to the specified VMXResourceHandle.
Definition: VMXIO.cpp:814
bool UART_GetBytesAvailable(VMXResourceHandle uart_es_handle, uint16_t &size, VMXErrorCode *errcode=0)
Returns the number of data bytes which have been received from VMX UART Resource. ...
Definition: VMXIO.cpp:1904
bool Accumulator_GetFullScaleVoltage(float &full_scale_voltage, VMXErrorCode *errcode=0)
Returns the current VMX Analog Input full-scale voltage level.
Definition: VMXIO.cpp:1771
bool ActivateResource(VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to activate the specified VMXResourceHandle NOTE: VMX Resources can be activated after they ...
Definition: VMXIO.cpp:901
bool UnrouteAllChannelsFromResource(VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to unroute all VMX Channels from the allocated VMXResourceHandle.
Definition: VMXIO.cpp:778
bool DeactivateResource(VMXResourceHandle resource, VMXErrorCode *errcode=0)
Attempts to deactivate the specified VMXResourceHandle.
Definition: VMXIO.cpp:1360
VMXChannelCapability
Enumeration of VMX Channel Capabilities.
Definition: VMXChannel.h:64
VMXChannelType
Enumeration of VMX Channel types.
Definition: VMXChannel.h:42
bool DIO_Get(VMXResourceHandle dio_res_handle, bool &high, VMXErrorCode *errcode=0)
Returns the current signal state from a VMX DigitalIO Resource.
Definition: VMXIO.cpp:1636
bool SPI_Transaction(VMXResourceHandle spi_res_handle, uint8_t *p_send_data, uint8_t *p_rcv_data, uint16_t size, VMXErrorCode *errcode=0)
Simultaneously Writes/Reads data to/from the specified VMX SPI Resource.
Definition: VMXIO.cpp:1986
uint8_t VMXResourcePortIndex
Zero-based index of a particular VMX Resource Port of a VMX Resource.
Definition: VMXResource.h:63
bool PWMGenerator_SetDutyCycle(VMXResourceHandle pwmgen_res_handle, VMXResourcePortIndex port_index, uint8_t duty_cycle, VMXErrorCode *errcode=0)
Sets the VMX PWM Generator Resource&#39;s Duty Cycle to the specified value.
Definition: VMXIO.cpp:1601