Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 "DoubleSolenoid.h" |
| 8 | #include "WPIErrors.h" |
| 9 | #include <string.h> |
| 10 | #include "LiveWindow/LiveWindow.h" |
| 11 | |
| 12 | /** |
| 13 | * Constructor. |
| 14 | * |
| 15 | * @param forwardChannel The forward channel on the module to control. |
| 16 | * @param reverseChannel The reverse channel on the module to control. |
| 17 | */ |
| 18 | DoubleSolenoid::DoubleSolenoid(uint32_t forwardChannel, uint32_t reverseChannel) |
| 19 | : DoubleSolenoid(1, forwardChannel, reverseChannel) {} |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | * |
| 24 | * @param moduleNumber The solenoid module (1 or 2). |
| 25 | * @param forwardChannel The forward channel on the module to control. |
| 26 | * @param reverseChannel The reverse channel on the module to control. |
| 27 | */ |
| 28 | DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel, uint32_t reverseChannel) |
| 29 | { |
| 30 | m_reversed = false; |
| 31 | if (reverseChannel < forwardChannel) { // Swap ports to get the right address |
| 32 | int channel = reverseChannel; |
| 33 | reverseChannel = forwardChannel; |
| 34 | forwardChannel = channel; |
| 35 | m_reversed = true; |
| 36 | } |
| 37 | char buffer[50]; |
| 38 | int n = sprintf(buffer, "pneumatic/%d/%d/%d/%d", moduleNumber, |
| 39 | forwardChannel, moduleNumber, reverseChannel); |
| 40 | m_impl = new SimContinuousOutput(buffer); |
| 41 | |
| 42 | LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", moduleNumber, |
| 43 | forwardChannel, this); |
| 44 | } |
| 45 | |
| 46 | DoubleSolenoid::~DoubleSolenoid() { |
| 47 | if (m_table != nullptr) m_table->RemoveTableListener(this); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Set the value of a solenoid. |
| 52 | * |
| 53 | * @param value Move the solenoid to forward, reverse, or don't move it. |
| 54 | */ |
| 55 | void DoubleSolenoid::Set(Value value) |
| 56 | { |
| 57 | m_value = value; |
| 58 | switch(value) |
| 59 | { |
| 60 | case kOff: |
| 61 | m_impl->Set(0); |
| 62 | break; |
| 63 | case kForward: |
| 64 | m_impl->Set(m_reversed ? -1 : 1); |
| 65 | break; |
| 66 | case kReverse: |
| 67 | m_impl->Set(m_reversed ? 1 : -1); |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Read the current value of the solenoid. |
| 74 | * |
| 75 | * @return The current value of the solenoid. |
| 76 | */ |
| 77 | DoubleSolenoid::Value DoubleSolenoid::Get() const |
| 78 | { |
| 79 | return m_value; |
| 80 | } |
| 81 | |
| 82 | void DoubleSolenoid::ValueChanged(ITable *source, llvm::StringRef key, |
| 83 | std::shared_ptr<nt::Value> value, |
| 84 | bool isNew) { |
| 85 | if (!value->IsString()) return; |
| 86 | Value lvalue = kOff; |
| 87 | if (value->GetString() == "Forward") |
| 88 | lvalue = kForward; |
| 89 | else if (value->GetString() == "Reverse") |
| 90 | lvalue = kReverse; |
| 91 | Set(lvalue); |
| 92 | } |
| 93 | |
| 94 | void DoubleSolenoid::UpdateTable() { |
| 95 | if (m_table != nullptr) { |
| 96 | m_table->PutString("Value", (Get() == kForward ? "Forward" : (Get() == kReverse ? "Reverse" : "Off"))); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void DoubleSolenoid::StartLiveWindowMode() { |
| 101 | Set(kOff); |
| 102 | if (m_table != nullptr) { |
| 103 | m_table->AddTableListener("Value", this, true); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void DoubleSolenoid::StopLiveWindowMode() { |
| 108 | Set(kOff); |
| 109 | if (m_table != nullptr) { |
| 110 | m_table->RemoveTableListener(this); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | std::string DoubleSolenoid::GetSmartDashboardType() const { |
| 115 | return "Double Solenoid"; |
| 116 | } |
| 117 | |
| 118 | void DoubleSolenoid::InitTable(std::shared_ptr<ITable> subTable) { |
| 119 | m_table = subTable; |
| 120 | UpdateTable(); |
| 121 | } |
| 122 | |
| 123 | std::shared_ptr<ITable> DoubleSolenoid::GetTable() const { |
| 124 | return m_table; |
| 125 | } |