blob: 08189573bc82eb2ad564ab37919c886cdc40e75d [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "InterruptableSensorBase.h"
8#include "Utility.h"
9
10InterruptableSensorBase::InterruptableSensorBase()
11{
12 m_manager = NULL;
13 m_interrupt = NULL;
14}
15
16InterruptableSensorBase::~InterruptableSensorBase()
17{
18
19}
20
21void InterruptableSensorBase::AllocateInterrupts(bool watcher)
22{
23 wpi_assert(m_interrupt == NULL);
24 wpi_assert(m_manager == NULL);
25 // Expects the calling leaf class to allocate an interrupt index.
26 tRioStatusCode localStatus = NiFpga_Status_Success;
27 m_interrupt = tInterrupt::create(m_interruptIndex, &localStatus);
28 m_interrupt->writeConfig_WaitForAck(false, &localStatus);
29 m_manager = new tInterruptManager(1 << m_interruptIndex, watcher, &localStatus);
30 wpi_setError(localStatus);
31}
32
33/**
34 * Cancel interrupts on this device.
35 * This deallocates all the chipobject structures and disables any interrupts.
36 */
37void InterruptableSensorBase::CancelInterrupts()
38{
39 wpi_assert(m_manager != NULL);
40 wpi_assert(m_interrupt != NULL);
41 delete m_interrupt;
42 delete m_manager;
43 m_interrupt = NULL;
44 m_manager = NULL;
45}
46
47/**
48 * In synchronous mode, wait for the defined interrupt to occur.
49 * @param timeout Timeout in seconds
50 */
51void InterruptableSensorBase::WaitForInterrupt(float timeout)
52{
53 wpi_assert(m_manager != NULL);
54 wpi_assert(m_interrupt != NULL);
55 tRioStatusCode localStatus = NiFpga_Status_Success;
56 m_manager->watch((INT32)(timeout * 1e3), &localStatus);
57 wpi_setError(localStatus);
58}
59
60/**
61 * Enable interrupts to occur on this input.
62 * Interrupts are disabled when the RequestInterrupt call is made. This gives time to do the
63 * setup of the other options before starting to field interrupts.
64 */
65void InterruptableSensorBase::EnableInterrupts()
66{
67 wpi_assert(m_manager != NULL);
68 wpi_assert(m_interrupt != NULL);
69 tRioStatusCode localStatus = NiFpga_Status_Success;
70 m_manager->enable(&localStatus);
71 wpi_setError(localStatus);
72}
73
74/**
75 * Disable Interrupts without without deallocating structures.
76 */
77void InterruptableSensorBase::DisableInterrupts()
78{
79 wpi_assert(m_manager != NULL);
80 wpi_assert(m_interrupt != NULL);
81 tRioStatusCode localStatus = NiFpga_Status_Success;
82 m_manager->disable(&localStatus);
83 wpi_setError(localStatus);
84}
85
86/**
87 * Return the timestamp for the interrupt that occurred most recently.
88 * This is in the same time domain as GetClock().
89 * @return Timestamp in seconds since boot.
90 */
91double InterruptableSensorBase::ReadInterruptTimestamp()
92{
93 wpi_assert(m_interrupt != NULL);
94 tRioStatusCode localStatus = NiFpga_Status_Success;
95 UINT32 timestamp = m_interrupt->readTimeStamp(&localStatus);
96 wpi_setError(localStatus);
97 return timestamp * 1e-6;
98}