2016-05-31 23:45:47 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
#include "HAL/Types.h"
|
2016-05-31 23:45:47 -07:00
|
|
|
|
|
|
|
|
/* General Handle Data Layout
|
|
|
|
|
* Bits 0-15: Handle Index
|
|
|
|
|
* Bits 16-23: Unused
|
|
|
|
|
* Bits 24-30: Handle Type
|
|
|
|
|
* Bit 31: 1 if handle error, 0 if no error
|
|
|
|
|
*
|
|
|
|
|
* Other specialized handles will use different formats, however Bits 24-31 are
|
|
|
|
|
* always reserved for type and error handling.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace hal {
|
2016-06-05 07:29:47 -07:00
|
|
|
|
2016-06-17 20:21:25 -07:00
|
|
|
constexpr int16_t InvalidHandleIndex = -1;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
enum class HAL_HandleEnum {
|
2016-06-20 23:22:49 -07:00
|
|
|
Undefined = 0,
|
|
|
|
|
DIO = 1,
|
|
|
|
|
Port = 2,
|
|
|
|
|
Notifier = 3,
|
2016-06-27 11:32:40 -07:00
|
|
|
Interrupt = 4,
|
2016-06-27 21:32:30 -07:00
|
|
|
AnalogOutput = 5,
|
|
|
|
|
AnalogInput = 6,
|
2016-06-29 18:58:14 -07:00
|
|
|
AnalogTrigger = 7,
|
2016-06-30 21:39:09 -07:00
|
|
|
Relay = 8,
|
2016-06-30 23:43:00 -07:00
|
|
|
PWM = 9,
|
2016-07-01 00:29:08 -07:00
|
|
|
DigitalPWM = 10,
|
2016-07-02 08:22:44 -07:00
|
|
|
Counter = 11,
|
2016-07-03 15:22:22 -07:00
|
|
|
FPGAEncoder = 12,
|
|
|
|
|
Encoder = 13,
|
2016-07-02 09:24:54 -07:00
|
|
|
Compressor = 14,
|
2016-07-07 21:31:45 -07:00
|
|
|
Solenoid = 15,
|
|
|
|
|
AnalogGyro = 16
|
2016-06-20 23:22:49 -07:00
|
|
|
};
|
2016-06-05 07:29:47 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static inline int16_t getHandleIndex(HAL_Handle handle) {
|
2016-05-31 23:45:47 -07:00
|
|
|
// mask and return last 16 bits
|
2016-08-11 23:38:45 -07:00
|
|
|
return static_cast<int16_t>(handle & 0xffff);
|
2016-05-31 23:45:47 -07:00
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
static inline HAL_HandleEnum getHandleType(HAL_Handle handle) {
|
2016-05-31 23:45:47 -07:00
|
|
|
// mask first 8 bits and cast to enum
|
2016-08-24 21:39:16 -07:00
|
|
|
return static_cast<HAL_HandleEnum>((handle >> 24) & 0xff);
|
2016-05-31 23:45:47 -07:00
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
static inline bool isHandleType(HAL_Handle handle, HAL_HandleEnum handleType) {
|
2016-06-01 20:12:50 -07:00
|
|
|
return handleType == getHandleType(handle);
|
2016-05-31 23:45:47 -07:00
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
static inline int16_t getHandleTypedIndex(HAL_Handle handle,
|
|
|
|
|
HAL_HandleEnum enumType) {
|
2016-06-17 20:21:25 -07:00
|
|
|
if (!isHandleType(handle, enumType)) return InvalidHandleIndex;
|
2016-05-31 23:45:47 -07:00
|
|
|
return getHandleIndex(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-05 15:23:58 -07:00
|
|
|
/* specialized functions for Port handle
|
|
|
|
|
* Port Handle Data Layout
|
2016-08-12 13:45:28 -07:00
|
|
|
* Bits 0-7: Channel Number
|
2016-06-05 15:23:58 -07:00
|
|
|
* Bits 8-15: Module Number
|
|
|
|
|
* Bits 16-23: Unused
|
|
|
|
|
* Bits 24-30: Handle Type
|
|
|
|
|
* Bit 31: 1 if handle error, 0 if no error
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// using a 16 bit value so we can store 0-255 and still report error
|
2016-08-12 13:45:28 -07:00
|
|
|
static inline int16_t getPortHandleChannel(HAL_PortHandle handle) {
|
2016-07-09 00:24:26 -07:00
|
|
|
if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
|
2016-08-11 23:38:45 -07:00
|
|
|
return static_cast<uint8_t>(handle & 0xff);
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// using a 16 bit value so we can store 0-255 and still report error
|
2016-07-09 00:24:26 -07:00
|
|
|
static inline int16_t getPortHandleModule(HAL_PortHandle handle) {
|
|
|
|
|
if (!isHandleType(handle, HAL_HandleEnum::Port)) return InvalidHandleIndex;
|
2016-08-11 23:38:45 -07:00
|
|
|
return static_cast<uint8_t>((handle >> 8) & 0xff);
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module);
|
2016-06-05 15:23:58 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType);
|
2016-07-10 17:47:44 -07:00
|
|
|
} // namespace hal
|