-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalInput.cpp
158 lines (138 loc) · 4.41 KB
/
DigitalInput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "DigitalInput.h"
#include "DigitalModule.h"
#include "Resource.h"
#include "Utility.h"
#include "WPIStatus.h"
static Resource *interrupts = NULL;
/**
* Create an instance of a DigitalInput.
* Creates a digital input given a slot and channel. Common creation routine
* for all constructors.
*/
void DigitalInput::InitDigitalInput(UINT32 slot, UINT32 channel)
{
Resource::CreateResourceObject(&interrupts, 8);
CheckDigitalChannel(channel);
CheckDigitalModule(slot);
m_channel = channel;
m_module = DigitalModule::GetInstance(slot);
m_module->AllocateDIO(channel, true);
}
/**
* Create an instance of a Digital Input class.
* Creates a digital input given a channel and uses the default module.
*/
DigitalInput::DigitalInput(UINT32 channel)
{
InitDigitalInput(GetDefaultDigitalModule(), channel);
}
/**
* Create an instance of a Digital Input class.
* Creates a digital input given an channel and module.
*/
DigitalInput::DigitalInput(UINT32 slot, UINT32 channel)
{
InitDigitalInput(slot, channel);
}
/**
* Free resources associated with the Digital Input class.
*/
DigitalInput::~DigitalInput()
{
if (m_manager != NULL)
{
delete m_manager;
delete m_interrupt;
interrupts->Free(m_interruptIndex);
}
m_module->FreeDIO(m_channel);
}
/*
* Get the value from a digital input channel.
* Retrieve the value of a single digital input channel from the FPGA.
*/
UINT32 DigitalInput::Get()
{
return m_module->GetDIO(m_channel);
}
/**
* @return The GPIO channel number that this object represents.
*/
UINT32 DigitalInput::GetChannel()
{
return m_channel;
}
/**
* @return The value to be written to the channel field of a routing mux.
*/
UINT32 DigitalInput::GetChannelForRouting()
{
return DigitalModule::RemapDigitalChannel(GetChannel() - 1);
}
/**
* @return The value to be written to the module field of a routing mux.
*/
UINT32 DigitalInput::GetModuleForRouting()
{
return DigitalModule::SlotToIndex(m_module->GetSlot());
}
/**
* @return The value to be written to the analog trigger field of a routing mux.
*/
bool DigitalInput::GetAnalogTriggerForRouting()
{
return false;
}
/**
* Request interrupts asynchronously on this digital input.
* @param handler The address of the interrupt handler function of type tInterruptHandler that
* will be called whenever there is an interrupt on the digitial input port.
* Request interrupts in synchronus mode where the user program interrupt handler will be
* called when an interrupt occurs.
* The default is interrupt on rising edges only.
*/
void DigitalInput::RequestInterrupts(tInterruptHandler handler, void *param)
{
m_interruptIndex = interrupts->Allocate();
//TODO: check for error on allocation
AllocateInterrupts(false);
m_interrupt->writeConfig_WaitForAck(false, &status);
m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &status);
m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &status);
m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &status);
SetUpSourceEdge(true, false);
m_manager->registerHandler(handler, param, &status);
wpi_assertCleanStatus(status);
}
/**
* Request interrupts synchronously on this digital input.
* Request interrupts in synchronus mode where the user program will have to explicitly
* wait for the interrupt to occur.
* The default is interrupt on rising edges only.
*/
void DigitalInput::RequestInterrupts()
{
m_interruptIndex = interrupts->Allocate();
//TODO: check for errors
AllocateInterrupts(true);
m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &status);
m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &status);
m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &status);
SetUpSourceEdge(true, false);
wpi_assertCleanStatus(status);
}
void DigitalInput::SetUpSourceEdge(bool risingEdge, bool fallingEdge)
{
wpi_assert(m_interrupt != NULL);
if (m_interrupt != NULL)
{
m_interrupt->writeConfig_RisingEdge(risingEdge, &status);
m_interrupt->writeConfig_FallingEdge(fallingEdge, &status);
}
wpi_assertCleanStatus(status);
}