WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
HandlesInternal.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include "wpi/hal/Types.h"
11
12/* General Handle Data Layout
13 * Bits 0-15: Handle Index
14 * Bits 16-23: 8 bit rolling reset detection
15 * Bits 24-30: Handle Type
16 * Bit 31: 1 if handle error, 0 if no error
17 *
18 * Other specialized handles will use different formats, however Bits 24-31 are
19 * always reserved for type and error handling.
20 */
21
22namespace wpi::hal {
23
24/**
25 * Base for all HAL Handles.
26 */
28 public:
31 HandleBase(const HandleBase&) = delete;
32 HandleBase& operator=(const HandleBase&) = delete;
33 virtual void ResetHandles();
34 static void ResetGlobalHandles();
35
36 protected:
37 int16_t m_version;
38};
39
40constexpr int16_t InvalidHandleIndex = -1;
41
42/**
43 * Enum of HAL handle types. Vendors/Teams should use Vendor (17).
44 */
77
78/**
79 * Get the handle index from a handle.
80 *
81 * @param handle the handle
82 * @return the index
83 */
84static inline int16_t getHandleIndex(HAL_Handle handle) {
85 // mask and return last 16 bits
86 return static_cast<int16_t>(handle & 0xffff);
87}
88
89/**
90 * Get the handle type from a handle.
91 *
92 * @param handle the handle
93 * @return the type
94 */
96 // mask first 8 bits and cast to enum
97 return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
98}
99
100/**
101 * Get if the handle is a specific type.
102 *
103 * @param handle the handle
104 * @param handleType the type to check
105 * @return true if the type is correct, otherwise false
106 */
107static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
108 return handleType == getHandleType(handle);
109}
110
111/**
112 * Get if the version of the handle is correct.
113 *
114 * Do not use on the roboRIO, used specifically for the sim to handle resets.
115 *
116 * @param handle the handle
117 * @param version the handle version to check
118 * @return true if the handle is the right version, otherwise false
119 */
120static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
121 return (((handle & 0xFF0000) >> 16) & version) == version;
122}
123
124/**
125 * Get if the handle is a correct type and version.
126 *
127 * Note the version is not checked on the roboRIO.
128 *
129 * @param handle the handle
130 * @param enumType the type to check
131 * @param version the handle version to check
132 * @return true if the handle is proper version and type, otherwise
133 * false.
134 */
135inline int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType,
136 int16_t version) {
137 if (!isHandleType(handle, enumType)) {
138 return InvalidHandleIndex;
139 }
140#if !defined(__FRC_SYSTEMCORE__)
141 if (!isHandleCorrectVersion(handle, version)) {
142 return InvalidHandleIndex;
143 }
144#endif
145 return getHandleIndex(handle);
146}
147
148/* specialized functions for Port handle
149 * Port Handle Data Layout
150 * Bits 0-7: Channel Number
151 * Bits 8-15: Module Number
152 * Bits 16-23: Unused
153 * Bits 24-30: Handle Type
154 * Bit 31: 1 if handle error, 0 if no error
155 */
156
157/**
158 * Create a handle for a specific index, type and version.
159 *
160 * Note the version is not checked on the roboRIO.
161 *
162 * @param index the index
163 * @param handleType the handle type
164 * @param version the handle version
165 * @return the created handle
166 */
168 int16_t version);
169} // namespace wpi::hal
@ index
Definition base.h:690
A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
Definition AddressableLED.hpp:31
Persistent alert to be sent to the driver station.
Definition Alert.hpp:36
Analog input class.
Definition AnalogInput.hpp:29
High level class for interfacing with CAN devices conforming to the standard CAN spec.
Definition CAN.hpp:24
Class for operating a compressor connected to a pneumatics module.
Definition Compressor.hpp:34
Class to read a duty cycle PWM input.
Definition DutyCycle.hpp:23
Class to read quad encoders.
Definition Encoder.hpp:31
Notifiers run a user-provided callback function on a separate thread.
Definition Notifier.hpp:30
Class implements the PWM generation in the FPGA.
Definition PWM.hpp:26
Driver for the RS-232 serial port on the roboRIO.
Definition SerialPort.hpp:27
Solenoid class for running high voltage Digital Output on a pneumatics module.
Definition Solenoid.hpp:26
static void ResetGlobalHandles()
HandleBase & operator=(const HandleBase &)=delete
HandleBase(const HandleBase &)=delete
virtual void ResetHandles()
int16_t m_version
Definition HandlesInternal.hpp:37
int32_t HAL_Handle
Definition Types.h:17
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition Types.hpp:9
HAL_HandleEnum
Enum of HAL handle types.
Definition HandlesInternal.hpp:45
@ REVPDH
Definition HandlesInternal.hpp:72
@ Interrupt
Definition HandlesInternal.hpp:50
@ CTREPCM
Definition HandlesInternal.hpp:70
@ CANStream
Definition HandlesInternal.hpp:74
@ DMA
Definition HandlesInternal.hpp:68
@ Relay
Definition HandlesInternal.hpp:54
@ AnalogGyro
Definition HandlesInternal.hpp:62
@ SimulationJni
Definition HandlesInternal.hpp:64
@ CTREPDP
Definition HandlesInternal.hpp:71
@ Port
Definition HandlesInternal.hpp:48
@ Counter
Definition HandlesInternal.hpp:57
@ DIO
Definition HandlesInternal.hpp:47
@ DigitalPWM
Definition HandlesInternal.hpp:56
@ REVPH
Definition HandlesInternal.hpp:73
@ AnalogOutput
Definition HandlesInternal.hpp:51
@ FPGAEncoder
Definition HandlesInternal.hpp:58
@ AnalogTrigger
Definition HandlesInternal.hpp:53
@ Undefined
Definition HandlesInternal.hpp:46
@ Vendor
Definition HandlesInternal.hpp:63
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition HandlesInternal.hpp:135
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
constexpr int16_t InvalidHandleIndex
Definition HandlesInternal.hpp:40
static bool isHandleCorrectVersion(HAL_Handle handle, int16_t version)
Get if the version of the handle is correct.
Definition HandlesInternal.hpp:120
static int16_t getHandleIndex(HAL_Handle handle)
Get the handle index from a handle.
Definition HandlesInternal.hpp:84
static HAL_HandleEnum getHandleType(HAL_Handle handle)
Get the handle type from a handle.
Definition HandlesInternal.hpp:95
static bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType)
Get if the handle is a specific type.
Definition HandlesInternal.hpp:107
constexpr int kHandleTypeHALBase
Definition Synchronization.hpp:26