2014-06-07 17:37:51 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2014. 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 the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "WPILib.h"
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "TestBench.h"
|
|
|
|
|
|
2014-06-12 11:17:57 -04:00
|
|
|
static const double kDelayTime = 0.1;
|
|
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
/**
|
|
|
|
|
* A fixture with a digital input and a digital output physically wired
|
|
|
|
|
* together.
|
|
|
|
|
*/
|
|
|
|
|
class DIOLoopTest : public testing::Test {
|
|
|
|
|
protected:
|
|
|
|
|
DigitalInput *m_input;
|
|
|
|
|
DigitalOutput *m_output;
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
virtual void SetUp() {
|
|
|
|
|
m_input = new DigitalInput(TestBench::kLoop1InputChannel);
|
|
|
|
|
m_output = new DigitalOutput(TestBench::kLoop1OutputChannel);
|
|
|
|
|
}
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
virtual void TearDown() {
|
|
|
|
|
delete m_input;
|
|
|
|
|
delete m_output;
|
|
|
|
|
}
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
void Reset() {
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the DigitalInput and DigitalOutput classes by setting the output and
|
|
|
|
|
* reading the input.
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(DIOLoopTest, Loop) {
|
|
|
|
|
Reset();
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
m_output->Set(false);
|
2014-08-01 18:11:02 -04:00
|
|
|
Wait(kDelayTime);
|
2014-06-07 17:37:51 -04:00
|
|
|
EXPECT_FALSE(m_input->Get()) << "The digital output was turned off, but "
|
|
|
|
|
<< "the digital input is on.";
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
m_output->Set(true);
|
2014-08-01 18:11:02 -04:00
|
|
|
Wait(kDelayTime);
|
2014-06-07 17:37:51 -04:00
|
|
|
EXPECT_TRUE(m_input->Get()) << "The digital output was turned on, but "
|
|
|
|
|
<< "the digital input is off.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test a fake "counter" that uses the DIO loop as an input to make sure the
|
|
|
|
|
* Counter class works
|
|
|
|
|
*/
|
|
|
|
|
TEST_F(DIOLoopTest, FakeCounter) {
|
|
|
|
|
Reset();
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
Counter counter(m_input);
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
EXPECT_EQ(0, counter.Get()) << "Counter did not initialize to 0.";
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
/* Count 100 ticks. The counter value should be 100 after this loop. */
|
|
|
|
|
for(int i = 0; i < 100; i++) {
|
|
|
|
|
m_output->Set(true);
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
}
|
2014-08-01 18:11:02 -04:00
|
|
|
|
|
|
|
|
Wait(kDelayTime);
|
2014-06-12 11:17:57 -04:00
|
|
|
|
2014-06-07 17:37:51 -04:00
|
|
|
EXPECT_EQ(100, counter.Get()) << "Counter did not count up to 100.";
|
|
|
|
|
}
|
2014-08-01 18:11:02 -04:00
|
|
|
|
2014-08-04 12:19:31 -04:00
|
|
|
static void InterruptHandler(uint32_t interruptAssertedMask, void *param) {
|
2014-08-01 18:11:02 -04:00
|
|
|
*(int *)param = 12345;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(DIOLoopTest, AsynchronusInterruptWorks) {
|
|
|
|
|
int param = 0;
|
|
|
|
|
|
|
|
|
|
// Given an interrupt handler that sets an int to 12345
|
|
|
|
|
m_input->RequestInterrupts(InterruptHandler, ¶m);
|
|
|
|
|
m_input->EnableInterrupts();
|
|
|
|
|
|
|
|
|
|
// If the voltage rises
|
|
|
|
|
m_output->Set(false);
|
|
|
|
|
m_output->Set(true);
|
|
|
|
|
m_input->CancelInterrupts();
|
|
|
|
|
|
|
|
|
|
// Then the int should be 12345
|
|
|
|
|
Wait(kDelayTime);
|
|
|
|
|
EXPECT_EQ(12345, param) << "The interrupt did not run.";
|
|
|
|
|
}
|