WPILibC++  2018.4.1-1223-g36000dd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
HandlesInternal.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include "hal/Types.h"
13 
14 /* General Handle Data Layout
15  * Bits 0-15: Handle Index
16  * Bits 16-23: 8 bit rolling reset detection
17  * Bits 24-30: Handle Type
18  * Bit 31: 1 if handle error, 0 if no error
19  *
20  * Other specialized handles will use different formats, however Bits 24-31 are
21  * always reserved for type and error handling.
22  */
23 
24 namespace hal {
25 
29 class HandleBase {
30  public:
31  HandleBase();
32  ~HandleBase();
33  HandleBase(const HandleBase&) = delete;
34  HandleBase& operator=(const HandleBase&) = delete;
35  virtual void ResetHandles();
36  static void ResetGlobalHandles();
37 
38  protected:
39  int16_t m_version;
40 };
41 
42 constexpr int16_t InvalidHandleIndex = -1;
43 
47 enum class HAL_HandleEnum {
48  Undefined = 0,
49  DIO = 1,
50  Port = 2,
51  Notifier = 3,
52  Interrupt = 4,
53  AnalogOutput = 5,
54  AnalogInput = 6,
55  AnalogTrigger = 7,
56  Relay = 8,
57  PWM = 9,
58  DigitalPWM = 10,
59  Counter = 11,
60  FPGAEncoder = 12,
61  Encoder = 13,
62  Compressor = 14,
63  Solenoid = 15,
64  AnalogGyro = 16,
65  Vendor = 17,
66  SimulationJni = 18,
67  CAN = 19,
68 };
69 
76 static inline int16_t getHandleIndex(HAL_Handle handle) {
77  // mask and return last 16 bits
78  return static_cast<int16_t>(handle & 0xffff);
79 }
80 
87 static inline HAL_HandleEnum getHandleType(HAL_Handle handle) {
88  // mask first 8 bits and cast to enum
89  return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
90 }
91 
99 static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
100  return handleType == getHandleType(handle);
101 }
102 
112 static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
113  return (((handle & 0xFF0000) >> 16) & version) == version;
114 }
115 
127 static inline int16_t getHandleTypedIndex(HAL_Handle handle,
128  HAL_HandleEnum enumType,
129  int16_t version) {
130  if (!isHandleType(handle, enumType)) return InvalidHandleIndex;
131 #if !defined(__FRC_ROBORIO__)
132  if (!isHandleCorrectVersion(handle, version)) return InvalidHandleIndex;
133 #endif
134  return getHandleIndex(handle);
135 }
136 
137 /* specialized functions for Port handle
138  * Port Handle Data Layout
139  * Bits 0-7: Channel Number
140  * Bits 8-15: Module Number
141  * Bits 16-23: Unused
142  * Bits 24-30: Handle Type
143  * Bit 31: 1 if handle error, 0 if no error
144  */
145 
146 // using a 16 bit value so we can store 0-255 and still report error
153 static inline int16_t getPortHandleChannel(HAL_PortHandle handle) {
154  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
155  return static_cast<uint8_t>(handle & 0xff);
156 }
157 
158 // using a 16 bit value so we can store 0-255 and still report error
165 static inline int16_t getPortHandleModule(HAL_PortHandle handle) {
166  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
167  return static_cast<uint8_t>((handle >> 8) & 0xff);
168 }
169 
170 // using a 16 bit value so we can store 0-255 and still report error
177 static inline int16_t getPortHandleSPIEnable(HAL_PortHandle handle) {
178  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
179  return static_cast<uint8_t>((handle >> 16) & 0xff);
180 }
181 
189 HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module);
190 
197 HAL_PortHandle createPortHandleForSPI(uint8_t channel);
198 
209 HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType,
210  int16_t version);
211 } // namespace hal
HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module)
Create a port handle.
HAL_HandleEnum
Enum of HAL handle types.
Definition: HandlesInternal.h:47
Base for all HAL Handles.
Definition: HandlesInternal.h:29
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: SimDataValue.h:19
HAL_PortHandle createPortHandleForSPI(uint8_t channel)
Create a port handle for SPI.
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.