WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
HandlesInternal.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2016-2017. 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 
26 class HandleBase {
27  public:
28  HandleBase();
29  ~HandleBase();
30  HandleBase(const HandleBase&) = delete;
31  HandleBase& operator=(const HandleBase&) = delete;
32  virtual void ResetHandles();
33  static void ResetGlobalHandles();
34 
35  protected:
36  int16_t m_version;
37 };
38 
39 constexpr int16_t InvalidHandleIndex = -1;
40 
41 enum class HAL_HandleEnum {
42  Undefined = 0,
43  DIO = 1,
44  Port = 2,
45  Notifier = 3,
46  Interrupt = 4,
47  AnalogOutput = 5,
48  AnalogInput = 6,
49  AnalogTrigger = 7,
50  Relay = 8,
51  PWM = 9,
52  DigitalPWM = 10,
53  Counter = 11,
54  FPGAEncoder = 12,
55  Encoder = 13,
56  Compressor = 14,
57  Solenoid = 15,
58  AnalogGyro = 16,
59  Vendor = 17
60 };
61 
62 static inline int16_t getHandleIndex(HAL_Handle handle) {
63  // mask and return last 16 bits
64  return static_cast<int16_t>(handle & 0xffff);
65 }
66 static inline HAL_HandleEnum getHandleType(HAL_Handle handle) {
67  // mask first 8 bits and cast to enum
68  return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
69 }
70 static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
71  return handleType == getHandleType(handle);
72 }
73 static inline bool isHandleCorrectVersion(HAL_Handle handle, int16_t version) {
74  return (((handle & 0xFF0000) >> 16) & version) == version;
75 }
76 static inline int16_t getHandleTypedIndex(HAL_Handle handle,
77  HAL_HandleEnum enumType,
78  int16_t version) {
79  if (!isHandleType(handle, enumType)) return InvalidHandleIndex;
80 #if !defined(CONFIG_ATHENA)
81  if (!isHandleCorrectVersion(handle, version)) return InvalidHandleIndex;
82 #endif
83  return getHandleIndex(handle);
84 }
85 
86 /* specialized functions for Port handle
87  * Port Handle Data Layout
88  * Bits 0-7: Channel Number
89  * Bits 8-15: Module Number
90  * Bits 16-23: Unused
91  * Bits 24-30: Handle Type
92  * Bit 31: 1 if handle error, 0 if no error
93  */
94 
95 // using a 16 bit value so we can store 0-255 and still report error
96 static inline int16_t getPortHandleChannel(HAL_PortHandle handle) {
97  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
98  return static_cast<uint8_t>(handle & 0xff);
99 }
100 
101 // using a 16 bit value so we can store 0-255 and still report error
102 static inline int16_t getPortHandleModule(HAL_PortHandle handle) {
103  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
104  return static_cast<uint8_t>((handle >> 8) & 0xff);
105 }
106 
107 // using a 16 bit value so we can store 0-255 and still report error
108 static inline int16_t getPortHandleSPIEnable(HAL_PortHandle handle) {
109  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
110  return static_cast<uint8_t>((handle >> 16) & 0xff);
111 }
112 
113 HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module);
114 
115 HAL_PortHandle createPortHandleForSPI(uint8_t channel);
116 
117 HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType,
118  int16_t version);
119 } // namespace hal
Definition: HandlesInternal.h:26