Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2011-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Buttons/Button.h" |
| 9 | |
| 10 | #include "Buttons/HeldButtonScheduler.h" |
| 11 | #include "Buttons/PressedButtonScheduler.h" |
| 12 | #include "Buttons/ReleasedButtonScheduler.h" |
| 13 | #include "Buttons/ToggleButtonScheduler.h" |
| 14 | #include "Buttons/CancelButtonScheduler.h" |
| 15 | |
| 16 | bool Trigger::Grab() { |
| 17 | if (Get()) |
| 18 | return true; |
| 19 | else if (m_table != nullptr) { |
| 20 | // if (m_table->isConnected())//TODO is connected on button? |
| 21 | return m_table->GetBoolean("pressed", false); |
| 22 | /*else |
| 23 | return false;*/ |
| 24 | } else |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | void Trigger::WhenActive(Command *command) { |
| 29 | auto pbs = new PressedButtonScheduler(Grab(), this, command); |
| 30 | pbs->Start(); |
| 31 | } |
| 32 | |
| 33 | void Trigger::WhileActive(Command *command) { |
| 34 | auto hbs = new HeldButtonScheduler(Grab(), this, command); |
| 35 | hbs->Start(); |
| 36 | } |
| 37 | |
| 38 | void Trigger::WhenInactive(Command *command) { |
| 39 | auto rbs = new ReleasedButtonScheduler(Grab(), this, command); |
| 40 | rbs->Start(); |
| 41 | } |
| 42 | |
| 43 | void Trigger::CancelWhenActive(Command *command) { |
| 44 | auto cbs = new CancelButtonScheduler(Grab(), this, command); |
| 45 | cbs->Start(); |
| 46 | } |
| 47 | |
| 48 | void Trigger::ToggleWhenActive(Command *command) { |
| 49 | auto tbs = new ToggleButtonScheduler(Grab(), this, command); |
| 50 | tbs->Start(); |
| 51 | } |
| 52 | |
| 53 | std::string Trigger::GetSmartDashboardType() const { return "Button"; } |
| 54 | |
| 55 | void Trigger::InitTable(std::shared_ptr<ITable> table) { |
| 56 | m_table = table; |
| 57 | if (m_table != nullptr) { |
| 58 | m_table->PutBoolean("pressed", Get()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::shared_ptr<ITable> Trigger::GetTable() const { return m_table; } |