Now you can download a copy of these docs so you can use them offline! Download now
DigitalInput.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "DigitalInput.h" 00008 #include "DigitalModule.h" 00009 #include "Resource.h" 00010 #include "WPIErrors.h" 00011 00012 // TODO: This is not a good place for this... 00013 Resource *interruptsResource = NULL; 00014 00020 void DigitalInput::InitDigitalInput(UINT8 moduleNumber, UINT32 channel) 00021 { 00022 char buf[64]; 00023 Resource::CreateResourceObject(&interruptsResource, tInterrupt::kNumSystems); 00024 if (!CheckDigitalModule(moduleNumber)) 00025 { 00026 snprintf(buf, 64, "Digital Module %d", moduleNumber); 00027 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf); 00028 return; 00029 } 00030 if (!CheckDigitalChannel(channel)) 00031 { 00032 snprintf(buf, 64, "Digital Channel %d", channel); 00033 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf); 00034 return; 00035 } 00036 m_channel = channel; 00037 m_module = DigitalModule::GetInstance(moduleNumber); 00038 m_module->AllocateDIO(channel, true); 00039 } 00040 00047 DigitalInput::DigitalInput(UINT32 channel) 00048 { 00049 InitDigitalInput(GetDefaultDigitalModule(), channel); 00050 } 00051 00059 DigitalInput::DigitalInput(UINT8 moduleNumber, UINT32 channel) 00060 { 00061 InitDigitalInput(moduleNumber, channel); 00062 } 00063 00067 DigitalInput::~DigitalInput() 00068 { 00069 if (StatusIsFatal()) return; 00070 if (m_manager != NULL) 00071 { 00072 delete m_manager; 00073 delete m_interrupt; 00074 interruptsResource->Free(m_interruptIndex); 00075 } 00076 m_module->FreeDIO(m_channel); 00077 } 00078 00079 /* 00080 * Get the value from a digital input channel. 00081 * Retrieve the value of a single digital input channel from the FPGA. 00082 */ 00083 UINT32 DigitalInput::Get() 00084 { 00085 if (StatusIsFatal()) return 0; 00086 return m_module->GetDIO(m_channel); 00087 } 00088 00092 UINT32 DigitalInput::GetChannel() 00093 { 00094 return m_channel; 00095 } 00096 00100 UINT32 DigitalInput::GetChannelForRouting() 00101 { 00102 return DigitalModule::RemapDigitalChannel(GetChannel() - 1); 00103 } 00104 00108 UINT32 DigitalInput::GetModuleForRouting() 00109 { 00110 if (StatusIsFatal()) return 0; 00111 return m_module->GetNumber() - 1; 00112 } 00113 00117 bool DigitalInput::GetAnalogTriggerForRouting() 00118 { 00119 return false; 00120 } 00121 00130 void DigitalInput::RequestInterrupts(tInterruptHandler handler, void *param) 00131 { 00132 if (StatusIsFatal()) return; 00133 UINT32 index = interruptsResource->Allocate("Async Interrupt"); 00134 if (index == ~0ul) 00135 { 00136 CloneError(interruptsResource); 00137 return; 00138 } 00139 m_interruptIndex = index; 00140 00141 // Creates a manager too 00142 AllocateInterrupts(false); 00143 00144 tRioStatusCode localStatus = NiFpga_Status_Success; 00145 m_interrupt->writeConfig_WaitForAck(false, &localStatus); 00146 m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &localStatus); 00147 m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &localStatus); 00148 m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &localStatus); 00149 SetUpSourceEdge(true, false); 00150 00151 m_manager->registerHandler(handler, param, &localStatus); 00152 wpi_setError(localStatus); 00153 } 00154 00161 void DigitalInput::RequestInterrupts() 00162 { 00163 if (StatusIsFatal()) return; 00164 UINT32 index = interruptsResource->Allocate("Sync Interrupt"); 00165 if (index == ~0ul) 00166 { 00167 CloneError(interruptsResource); 00168 return; 00169 } 00170 m_interruptIndex = index; 00171 00172 AllocateInterrupts(true); 00173 00174 tRioStatusCode localStatus = NiFpga_Status_Success; 00175 m_interrupt->writeConfig_Source_AnalogTrigger(GetAnalogTriggerForRouting(), &localStatus); 00176 m_interrupt->writeConfig_Source_Channel(GetChannelForRouting(), &localStatus); 00177 m_interrupt->writeConfig_Source_Module(GetModuleForRouting(), &localStatus); 00178 SetUpSourceEdge(true, false); 00179 wpi_setError(localStatus); 00180 } 00181 00182 void DigitalInput::SetUpSourceEdge(bool risingEdge, bool fallingEdge) 00183 { 00184 if (StatusIsFatal()) return; 00185 if (m_interrupt == NULL) 00186 { 00187 wpi_setWPIErrorWithContext(NullParameter, "You must call RequestInterrupts before SetUpSourceEdge"); 00188 return; 00189 } 00190 tRioStatusCode localStatus = NiFpga_Status_Success; 00191 if (m_interrupt != NULL) 00192 { 00193 m_interrupt->writeConfig_RisingEdge(risingEdge, &localStatus); 00194 m_interrupt->writeConfig_FallingEdge(fallingEdge, &localStatus); 00195 } 00196 wpi_setError(localStatus); 00197 } 00198
Generated on Thu Jan 12 2012 22:35:19 for WPILibC++ by
1.7.1