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: Unused
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 constexpr int16_t InvalidHandleIndex = -1;
27 
28 enum class HAL_HandleEnum {
29  Undefined = 0,
30  DIO = 1,
31  Port = 2,
32  Notifier = 3,
33  Interrupt = 4,
34  AnalogOutput = 5,
35  AnalogInput = 6,
36  AnalogTrigger = 7,
37  Relay = 8,
38  PWM = 9,
39  DigitalPWM = 10,
40  Counter = 11,
41  FPGAEncoder = 12,
42  Encoder = 13,
43  Compressor = 14,
44  Solenoid = 15,
45  AnalogGyro = 16,
46  Vendor = 17
47 };
48 
49 static inline int16_t getHandleIndex(HAL_Handle handle) {
50  // mask and return last 16 bits
51  return static_cast<int16_t>(handle & 0xffff);
52 }
53 static inline HAL_HandleEnum getHandleType(HAL_Handle handle) {
54  // mask first 8 bits and cast to enum
55  return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
56 }
57 static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
58  return handleType == getHandleType(handle);
59 }
60 static inline int16_t getHandleTypedIndex(HAL_Handle handle,
61  HAL_HandleEnum enumType) {
62  if (!isHandleType(handle, enumType)) return InvalidHandleIndex;
63  return getHandleIndex(handle);
64 }
65 
66 /* specialized functions for Port handle
67  * Port Handle Data Layout
68  * Bits 0-7: Channel Number
69  * Bits 8-15: Module Number
70  * Bits 16-23: Unused
71  * Bits 24-30: Handle Type
72  * Bit 31: 1 if handle error, 0 if no error
73  */
74 
75 // using a 16 bit value so we can store 0-255 and still report error
76 static inline int16_t getPortHandleChannel(HAL_PortHandle handle) {
77  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
78  return static_cast<uint8_t>(handle & 0xff);
79 }
80 
81 // using a 16 bit value so we can store 0-255 and still report error
82 static inline int16_t getPortHandleModule(HAL_PortHandle handle) {
83  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
84  return static_cast<uint8_t>((handle >> 8) & 0xff);
85 }
86 
87 // using a 16 bit value so we can store 0-255 and still report error
88 static inline int16_t getPortHandleSPIEnable(HAL_PortHandle handle) {
89  if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
90  return static_cast<uint8_t>((handle >> 16) & 0xff);
91 }
92 
93 HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module);
94 
95 HAL_PortHandle createPortHandleForSPI(uint8_t channel);
96 
97 HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType);
98 } // namespace hal