blob: ef19ffba9a25b40813a82c44f4f58069f8d4e233 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "Solenoid.h"
9
10#include <sstream>
11
12#include "LiveWindow/LiveWindow.h"
13#include "WPIErrors.h"
14#include "simulation/simTime.h"
15
16using namespace frc;
17
18/**
19 * Constructor.
20 *
21 * @param channel The channel on the solenoid module to control (1..8).
22 */
23Solenoid::Solenoid(int channel) : Solenoid(1, channel) {}
24
25/**
26 * Constructor.
27 *
28 * @param moduleNumber The solenoid module (1 or 2).
29 * @param channel The channel on the solenoid module to control (1..8).
30 */
31Solenoid::Solenoid(int moduleNumber, int channel) {
32 std::stringstream ss;
33 ss << "pneumatic/" << moduleNumber << "/" << channel;
34 m_impl = new SimContinuousOutput(ss.str());
35
36 LiveWindow::GetInstance()->AddActuator("Solenoid", moduleNumber, channel,
37 this);
38}
39
40Solenoid::~Solenoid() {
41 if (m_table != nullptr) m_table->RemoveTableListener(this);
42}
43
44/**
45 * Set the value of a solenoid.
46 *
47 * @param on Turn the solenoid output off or on.
48 */
49void Solenoid::Set(bool on) {
50 m_on = on;
51 m_impl->Set(on ? 1 : -1);
52}
53
54/**
55 * Read the current value of the solenoid.
56 *
57 * @return The current value of the solenoid.
58 */
59bool Solenoid::Get() const { return m_on; }
60
61void Solenoid::ValueChanged(ITable* source, llvm::StringRef key,
62 std::shared_ptr<nt::Value> value, bool isNew) {
63 if (!value->IsBoolean()) return;
64 Set(value->GetBoolean());
65}
66
67void Solenoid::UpdateTable() {
68 if (m_table != nullptr) {
69 m_table->PutBoolean("Value", Get());
70 }
71}
72
73void Solenoid::StartLiveWindowMode() {
74 Set(false);
75 if (m_table != nullptr) {
76 m_table->AddTableListener("Value", this, true);
77 }
78}
79
80void Solenoid::StopLiveWindowMode() {
81 Set(false);
82 if (m_table != nullptr) {
83 m_table->RemoveTableListener(this);
84 }
85}
86
87std::string Solenoid::GetSmartDashboardType() const { return "Solenoid"; }
88
89void Solenoid::InitTable(std::shared_ptr<ITable> subTable) {
90 m_table = subTable;
91 UpdateTable();
92}
93
94std::shared_ptr<ITable> Solenoid::GetTable() const { return m_table; }