Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Resolved buttons static checks (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeethaSujana committed Oct 5, 2020
1 parent 10554fb commit 25a4c0b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

#include <Pufferfish/Driver/Button/Button.h>

namespace Pufferfish {
namespace Driver {
namespace Button {
namespace Pufferfish::Driver::Button {

ButtonStatus Debouncer::transform(bool input, uint32_t current_time, bool &output) {
if (Pufferfish::Driver::Button::Debouncer::time_valid_check(
Expand Down Expand Up @@ -87,8 +85,8 @@ void EdgeDetector::transform(bool input, EdgeState &output) {

ButtonStatus Button::read_state(bool &debouned_output, EdgeState &switch_state_changed) {
bool input = button_input_.read();
uint32_t ms_time = time_.millis();

uint32_t ms_time = 0;
ms_time = Pufferfish::HAL::millis();
ButtonStatus status = debounce_.transform(input, ms_time, debouned_output);

/* Debounce is not success */
Expand All @@ -107,6 +105,4 @@ bool Debouncer::time_valid_check(uint32_t now_time, uint32_t last_time, uint32_t
return now_time - last_time < add_factor;
}

} // namespace Button
} // namespace Driver
} // namespace Pufferfish
} // namespace Pufferfish::Driver::Button
Original file line number Diff line number Diff line change
Expand Up @@ -18,86 +18,81 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "catch2/catch.hpp"
#include "Pufferfish/HAL/Mock/MockDigitalInput.h"
#include "Pufferfish/HAL/Mock/MockTime.h"
#include "Pufferfish/Driver/Button/Button.h"
#include <catch2/catch.hpp>

#include <Pufferfish/HAL/Mock/MockDigitalInput.h>
#include <Pufferfish/HAL/Mock/MockTime.h>
#include <Pufferfish/Driver/Button/Button.h>

namespace PF = Pufferfish;

SCENARIO("TESTSCENARIO 1 : Valid debounce time", "[Button]") {

GIVEN( "Switch input state and current time" ) {
GIVEN("Switch input state and current time") {
bool input = false;
bool output = false;
uint32_t index = 0;
uint32_t currentTime = 0;
PF::Driver::Button::ButtonStatus button_status;
PF::Driver::Button::Debouncer switchInput;
WHEN(" Switch input state change from low to high"){
PF::Driver::Button::Debouncer switch_input;
WHEN("Switch input state change from low to high") {
input = true;
THEN("Integration time is less than max time") {
for(currentTime = 0; currentTime < 100; currentTime++)
{
for (currentTime = 0; currentTime < 100; currentTime++) {
currentTime++;
input = true;
button_status = switchInput.transform(input, currentTime, output);
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
}
}
}
AND_WHEN("Switch input state change from low to high and Integration is equal to max time"){
AND_WHEN("Switch input state change from low to high and Integration is equal to max time") {
input = true;
currentTime = 0;
for(uint32_t currentTime = 0; currentTime < 100; currentTime++)
{
button_status = switchInput.transform(input, currentTime, output);
for (uint32_t currentTime = 0; currentTime < 100; currentTime++) {
button_status = switch_input.transform(input, currentTime, output);
}
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
THEN("Integration time is equal to or greater than max time") {
input = true;
button_status = switchInput.transform(input, currentTime, output);
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == true);
}
}
AND_WHEN("Switch input state change from low to high and Switch state changed from active high to low"){
AND_WHEN("Switch input state change from low to high and again changed from high to low") {
input = true;
for(uint32_t currentTime = 0; currentTime <= 100; currentTime++)
{
button_status = switchInput.transform(input, currentTime, output);
for (uint32_t currentTime = 0; currentTime <= 100; currentTime++) {
button_status = switch_input.transform(input, currentTime, output);
}
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == true);
THEN("Switch state changed from active high to low and Integration time is less than max integration time"){
THEN("Input changed from active high to low and max integration time is greater than Time") {
input = false;
for(uint32_t currentTime = 101; currentTime < 150; currentTime++)
{
button_status = switchInput.transform(input, currentTime, output);
for (uint32_t currentTime = 101; currentTime < 150; currentTime++) {
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == true);
}
}
}
AND_WHEN("Switch input state change from low to high and Switch state changed from active low to high"){
for(uint32_t currentTime = 0; currentTime < 150; currentTime++){
if(currentTime <= 100){
AND_WHEN("State change from low to high and again changed from active low to high") {
for (uint32_t currentTime = 0; currentTime < 150; currentTime++) {
if (currentTime <= 100) {
input = true;
button_status = switchInput.transform(input, currentTime, output);
button_status = switch_input.transform(input, currentTime, output);
} else {
input = false;
button_status = switchInput.transform(input, currentTime, output);
button_status = switch_input.transform(input, currentTime, output);
}
}
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == true);
THEN("Switch state changed from active high to low and Integration time is less than max integration time"){
THEN("Switch state changed from active high to low and Integration time is less than max Time") {
input = true;
for(uint32_t currentTime = 150; currentTime < 200; currentTime++)
{
button_status = switchInput.transform(input, currentTime, output);
for (uint32_t currentTime = 150; currentTime < 200; currentTime++) {
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == true);
}
Expand All @@ -107,63 +102,64 @@ SCENARIO("TESTSCENARIO 1 : Valid debounce time", "[Button]") {
}

SCENARIO("Switch is debouncing Continuously more than maximum limit", "[Button]") {

GIVEN( "Switch input state and current time" ) {
bool input = false;
bool output = false;
uint32_t currentTime = 0;
uint32_t j = 0;
PF::Driver::Button::Debouncer switchInput;
WHEN(" Switch input state change from low to high continuously "){
input = true;
currentTime = 0;
REQUIRE(switchInput.transform(input, currentTime, output) == PF::Driver::Button::ButtonStatus::not_ok);
REQUIRE(output == false);
for(uint32_t i = 1; i < 2000; i++){
currentTime++;
if (i < j+51 ) {
input = true;
REQUIRE(switchInput.transform(input, currentTime, output)== PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
}else if (i >= j+51 && i < j+100 ) {
input = false;
REQUIRE(switchInput.transform(input, currentTime, output)== PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
}
else if (i == j+100) {
j = i;
input = true;
REQUIRE(switchInput.transform(input, currentTime, output)== PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
}
GIVEN("Switch input state and current time") {
bool input = false;
bool output = false;
uint32_t currentTime = 0;
uint32_t j = 0;
PF::Driver::Button::Debouncer switch_input;
PF::Driver::Button::ButtonStatus button_status;
WHEN("Switch input state change from low to high continuously") {
input = true;
currentTime = 0;
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::not_ok);
REQUIRE(output == false);
for (uint32_t i = 1; i < 2000; i++) {
currentTime++;
if (i < (j + 51)) {
input = true;
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
} else if (i >= (j + 51) && i < (j + 100)) {
input = false;
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
} else if (i == (j + 100)) {
j = i;
input = true;
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(output == false);
}
THEN(" The current time is more than maximum debounce limit") {
}
THEN("The current time is more than maximum debounce limit") {
currentTime = 2000;
input = true;
REQUIRE(switchInput.transform(input, currentTime, output)== PF::Driver::Button::ButtonStatus::unknown);
button_status = switch_input.transform(input, currentTime, output);
REQUIRE(button_status == PF::Driver::Button::ButtonStatus::unknown);
REQUIRE(output == false);
}
}
}
}
}
}

SCENARIO("TESTSCENARIO 3 : Valid edge transition ", "[EdgeDetector::transform]") {

GIVEN( "Switch input state " ) {
GIVEN("Switch input state") {
bool input = false;
PF::Driver::Button::EdgeDetector edgeTransition;
PF::Driver::Button::EdgeState status;

WHEN("Input state changed from LOW to HIGH"){
WHEN("Input state changed from LOW to HIGH") {
input = false;
edgeTransition.transform(input, status);
THEN(""){
THEN("") {
input = true;
edgeTransition.transform(input, status);
REQUIRE(status == PF::Driver::Button::EdgeState::rising_edge);
}
}

WHEN("Input state changed from HIGH to LOW") {
input = true;
edgeTransition.transform(input, status);
Expand All @@ -173,17 +169,15 @@ SCENARIO("TESTSCENARIO 3 : Valid edge transition ", "[EdgeDetector::transform]")
REQUIRE(status == PF::Driver::Button::EdgeState::falling_edge);
}
}

WHEN("Input state changed from LOW to LOW") {
input = false;
edgeTransition.transform(input, status);
THEN("") {
input = false;
edgeTransition.transform(input, status);
REQUIRE(status == PF::Driver::Button::EdgeState::no_edge);
}
input = false;
edgeTransition.transform(input, status);
THEN("") {
input = false;
edgeTransition.transform(input, status);
REQUIRE(status == PF::Driver::Button::EdgeState::no_edge);
}
}

WHEN("Input state changed from HIGH to HIGH") {
input = false;
edgeTransition.transform(input, status);
Expand All @@ -192,45 +186,43 @@ SCENARIO("TESTSCENARIO 3 : Valid edge transition ", "[EdgeDetector::transform]")
edgeTransition.transform(input, status);
REQUIRE(status == PF::Driver::Button::EdgeState::no_edge);
}
}
}
}
}

SCENARIO("TESTSCENARIO 4 : Validate read_state ", "[Button]") {
GIVEN("Switch debounced output") {

PF::HAL::MockDigitalInput buttonInput;
PF::Driver::Button::Debouncer switchInput;
PF::Driver::Button::Debouncer switch_input;
PF::Driver::Button::ButtonStatus status;
PF::Driver::Button::EdgeDetector edgeTransition;
PF::HAL::MockTime timeInput;
bool debouned_output = false;
PF::Driver::Button::Button testButton(buttonInput, switchInput,timeInput);
PF::Driver::Button::Button testButton(buttonInput, switch_input, timeInput);
PF::Driver::Button::EdgeState PrevEdgeState = PF::Driver::Button::EdgeState::no_edge;
PF::Driver::Button::EdgeState testEdgeState = PF::Driver::Button::EdgeState::no_edge;
WHEN("Current time is less than the last sample time"){
WHEN("Current time is less than the last sample time") {
buttonInput.set_read(true);
timeInput.set_millis(0);
status = testButton.read_state(debouned_output, testEdgeState);
THEN("The output of the Edge transition same as previous"){
THEN("The output of the Edge transition same as previous") {
REQUIRE(status == PF::Driver::Button::ButtonStatus::not_ok);
REQUIRE(PrevEdgeState == testEdgeState);
}
}

WHEN("Current time is greater than the last sample time and switch debounced time is greater than the max_integrator_samples on Push"){
WHEN("Current time is greater the last sample time and debounced_time is greater than max_integrator_samples on push") {
buttonInput.set_read(false);
for(uint32_t i = 1; i < 101; i++){
for (uint32_t i = 1; i < 101; i++) {
buttonInput.set_read(true);
timeInput.set_millis(i);
status = testButton.read_state(debouned_output, testEdgeState);
}
THEN("The risigning edge state detected"){
THEN("The risigning edge state detected") {
REQUIRE(status == PF::Driver::Button::ButtonStatus::ok);
REQUIRE(testEdgeState == PF::Driver::Button::EdgeState::rising_edge);
}
}
WHEN(" Current time is greater than the last sample time and switch debounced time is greater than the max_integrator_samples on Release") {
WHEN("Current time is greater than the last sample Time and debounced time is greater than the max_integrator_samples on Release") {
buttonInput.set_read(false);
for (uint32_t i = 1; i < 101; i++) {
buttonInput.set_read(true);
Expand All @@ -252,6 +244,4 @@ SCENARIO("TESTSCENARIO 4 : Validate read_state ", "[Button]") {
}
}
}
}


}

0 comments on commit 25a4c0b

Please sign in to comment.