Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 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 | #include "HAL/handles/HandlesInternal.h" |
| 9 | |
| 10 | namespace hal { |
| 11 | HAL_PortHandle createPortHandle(uint8_t channel, uint8_t module) { |
| 12 | // set last 8 bits, then shift to first 8 bits |
| 13 | HAL_PortHandle handle = static_cast<HAL_PortHandle>(HAL_HandleEnum::Port); |
| 14 | handle = handle << 24; |
| 15 | // shift module and add to 3rd set of 8 bits |
| 16 | int32_t temp = module; |
| 17 | temp = (temp << 8) & 0xff00; |
| 18 | handle += temp; |
| 19 | // add channel to last 8 bits |
| 20 | handle += channel; |
| 21 | return handle; |
| 22 | } |
| 23 | |
| 24 | HAL_PortHandle createPortHandleForSPI(uint8_t channel) { |
| 25 | // set last 8 bits, then shift to first 8 bits |
| 26 | HAL_PortHandle handle = static_cast<HAL_PortHandle>(HAL_HandleEnum::Port); |
| 27 | handle = handle << 16; |
| 28 | // set second set up bits to 1 |
| 29 | int32_t temp = 1; |
| 30 | temp = (temp << 8) & 0xff00; |
| 31 | handle += temp; |
| 32 | // shift to last set of bits |
| 33 | handle = handle << 8; |
| 34 | // add channel to last 8 bits |
| 35 | handle += channel; |
| 36 | return handle; |
| 37 | } |
| 38 | |
| 39 | HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType) { |
| 40 | if (index < 0) return HAL_kInvalidHandle; |
| 41 | uint8_t hType = static_cast<uint8_t>(handleType); |
| 42 | if (hType == 0 || hType > 127) return HAL_kInvalidHandle; |
| 43 | // set last 8 bits, then shift to first 8 bits |
| 44 | HAL_Handle handle = hType; |
| 45 | handle = handle << 24; |
| 46 | // add index to set last 16 bits |
| 47 | handle += index; |
| 48 | return handle; |
| 49 | } |
| 50 | } // namespace hal |