WPILibC++ 2024.3.2
UnsafeDIO.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/mutex.h>
8
9#include "hal/ChipObject.h"
10#include "hal/Types.h"
11
12namespace hal {
13
14/**
15 * Proxy class for directly manipulating the DIO pins.
16 *
17 * This class is not copyable or movable, and should never be used
18 * outside of the UnsafeManipulateDIO callback.
19 */
21 DIOSetProxy(tDIO::tOutputEnable setOutputDirReg,
22 tDIO::tOutputEnable unsetOutputDirReg,
23 tDIO::tDO setOutputStateReg, tDIO::tDO unsetOutputStateReg,
24 tDIO* dio)
25 : m_setOutputDirReg{setOutputDirReg},
26 m_unsetOutputDirReg{unsetOutputDirReg},
27 m_setOutputStateReg{setOutputStateReg},
28 m_unsetOutputStateReg{unsetOutputStateReg},
29 m_dio{dio} {}
30
31 DIOSetProxy(const DIOSetProxy&) = delete;
35
36 void SetOutputMode(int32_t* status) {
37 m_dio->writeOutputEnable(m_setOutputDirReg, status);
38 }
39
40 void SetInputMode(int32_t* status) {
41 m_dio->writeOutputEnable(m_unsetOutputDirReg, status);
42 }
43
44 void SetOutputTrue(int32_t* status) {
45 m_dio->writeDO(m_setOutputStateReg, status);
46 }
47
48 void SetOutputFalse(int32_t* status) {
49 m_dio->writeDO(m_unsetOutputStateReg, status);
50 }
51
52 tDIO::tOutputEnable m_setOutputDirReg;
53 tDIO::tOutputEnable m_unsetOutputDirReg;
56 tDIO* m_dio;
57};
58namespace detail {
61int32_t ComputeDigitalMask(HAL_DigitalHandle handle, int32_t* status);
62} // namespace detail
63
64/**
65 * Unsafe digital output set function
66 * This function can be used to perform fast and deterministically set digital
67 * outputs. This function holds the DIO lock, so calling anything other then
68 * functions on the Proxy object passed as a parameter can deadlock your
69 * program.
70 *
71 * @param[in] handle the HAL digital handle of the pin to toggle.
72 * @param[out] status status check
73 * @param[in] func A functor taking a ref to a DIOSetProxy object.
74 */
75template <typename Functor>
76void UnsafeManipulateDIO(HAL_DigitalHandle handle, int32_t* status,
77 Functor func) {
78 auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
79 if (port == nullptr) {
80 *status = HAL_HANDLE_ERROR;
81 return;
82 }
84 tDIO* dSys = detail::UnsafeGetDigialSystem();
85 auto mask = detail::ComputeDigitalMask(handle, status);
86 if (*status != 0) {
87 return;
88 }
89 std::scoped_lock lock(dioMutex);
90
91 tDIO::tOutputEnable enableOE = dSys->readOutputEnable(status);
92 enableOE.value |= mask;
93 auto disableOE = enableOE;
94 disableOE.value &= ~mask;
95 tDIO::tDO enableDO = dSys->readDO(status);
96 enableDO.value |= mask;
97 auto disableDO = enableDO;
98 disableDO.value &= ~mask;
99
100 DIOSetProxy dioData{enableOE, disableOE, enableDO, disableDO, dSys};
101 func(dioData);
102}
103
104} // namespace hal
#define HAL_HANDLE_ERROR
Definition: Errors.h:92
HAL_Handle HAL_DigitalHandle
Definition: Types.h:31
detail namespace with internal helper functions
Definition: xchar.h:20
tDIO * UnsafeGetDigialSystem()
int32_t ComputeDigitalMask(HAL_DigitalHandle handle, int32_t *status)
wpi::mutex & UnsafeGetDIOMutex()
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: ChipObject.h:40
void UnsafeManipulateDIO(HAL_DigitalHandle handle, int32_t *status, Functor func)
Unsafe digital output set function This function can be used to perform fast and deterministically se...
Definition: UnsafeDIO.h:76
Proxy class for directly manipulating the DIO pins.
Definition: UnsafeDIO.h:20
DIOSetProxy(DIOSetProxy &&)=delete
void SetOutputFalse(int32_t *status)
Definition: UnsafeDIO.h:48
DIOSetProxy(tDIO::tOutputEnable setOutputDirReg, tDIO::tOutputEnable unsetOutputDirReg, tDIO::tDO setOutputStateReg, tDIO::tDO unsetOutputStateReg, tDIO *dio)
Definition: UnsafeDIO.h:21
void SetOutputTrue(int32_t *status)
Definition: UnsafeDIO.h:44
DIOSetProxy & operator=(DIOSetProxy &&)=delete
DIOSetProxy & operator=(const DIOSetProxy &)=delete
tDIO::tOutputEnable m_setOutputDirReg
Definition: UnsafeDIO.h:52
void SetInputMode(int32_t *status)
Definition: UnsafeDIO.h:40
void SetOutputMode(int32_t *status)
Definition: UnsafeDIO.h:36
tDIO * m_dio
Definition: UnsafeDIO.h:56
tDIO::tOutputEnable m_unsetOutputDirReg
Definition: UnsafeDIO.h:53
tDIO::tDO m_setOutputStateReg
Definition: UnsafeDIO.h:54
tDIO::tDO m_unsetOutputStateReg
Definition: UnsafeDIO.h:55
DIOSetProxy(const DIOSetProxy &)=delete
::std::mutex mutex
Definition: mutex.h:17