Now you can download a copy of these docs so you can use them offline! Download now
LiveWindow.cpp
1 #include "LiveWindow/LiveWindow.h"
2 #include "networktables/NetworkTable.h"
3 #include <algorithm>
4 #include <sstream>
5 
6 LiveWindow* LiveWindow::m_instance = NULL;
7 
14 {
15  if (m_instance == NULL)
16  {
17  m_instance = new LiveWindow();
18  }
19  return m_instance;
20 }
21 
27 {
28  m_enabled = false;
29  m_liveWindowTable = NetworkTable::GetTable("LiveWindow");
30  m_statusTable = m_liveWindowTable->GetSubTable("~STATUS~");
31  m_scheduler = Scheduler::GetInstance();
32 }
33 
38 void LiveWindow::SetEnabled(bool enabled)
39 {
40  if (m_enabled == enabled)
41  return;
42  if (enabled)
43  {
44  printf("Starting live window mode\n");
45  if (m_firstTime)
46  {
47  InitializeLiveWindowComponents();
48  m_firstTime = false;
49  }
50  m_scheduler->SetEnabled(false);
51  m_scheduler->RemoveAll();
52  for (std::map<LiveWindowSendable *, LiveWindowComponent>::iterator it =
53  m_components.begin(); it != m_components.end(); ++it)
54  {
55  it->first->StartLiveWindowMode();
56  }
57  }
58  else
59  {
60  printf("Ending LiveWindow mode\n");
61  for (std::map<LiveWindowSendable *, LiveWindowComponent>::iterator it =
62  m_components.begin(); it != m_components.end(); ++it)
63  {
64  it->first->StopLiveWindowMode();
65  }
66  m_scheduler->SetEnabled(true);
67  }
68  m_enabled = enabled;
69  m_statusTable->PutBoolean("LW Enabled", m_enabled);
70 }
71 
72 LiveWindow::~LiveWindow()
73 {
74 }
75 
82 void LiveWindow::AddSensor(const char *subsystem, const char *name,
83  LiveWindowSendable *component)
84 {
85  m_components[component].subsystem = subsystem;
86  m_components[component].name = name;
87  m_components[component].isSensor = true;
88 }
89 
96 void LiveWindow::AddActuator(const char *subsystem, const char *name,
97  LiveWindowSendable *component)
98 {
99  m_components[component].subsystem = subsystem;
100  m_components[component].name = name;
101  m_components[component].isSensor = false;
102 }
103 
107 void LiveWindow::AddSensor(std::string type, int module, int channel, LiveWindowSendable *component)
108 {
109  std::ostringstream oss;
110  oss << type << "[" << module << "," << channel << "]";
111  std::string types(oss.str());
112  char* cc = new char[types.size() + 1];
113  types.copy(cc, types.size());
114  cc[types.size()]='\0';
115  AddSensor("Ungrouped", cc, component);
116  if (std::find(m_sensors.begin(), m_sensors.end(), component) == m_sensors.end())
117  m_sensors.push_back(component);
118 }
119 
123 void LiveWindow::AddActuator(std::string type, int module, int channel, LiveWindowSendable *component)
124 {
125  std::ostringstream oss;
126  oss << type << "[" << module << "," << channel << "]";
127  std::string types(oss.str());
128  char* cc = new char[types.size() + 1];
129  types.copy(cc, types.size());
130  cc[types.size()]='\0';
131  AddActuator("Ungrouped", cc, component);
132 }
133 
139 void LiveWindow::UpdateValues()
140 {
141  for (unsigned int i = 0; i < m_sensors.size(); i++)
142  {
143  m_sensors[i]->UpdateTable();
144  }
145 }
146 
152 {
153  if (m_enabled)
154  {
155  UpdateValues();
156  }
157 }
158 
166 void LiveWindow::InitializeLiveWindowComponents()
167 {
168  for (std::map<LiveWindowSendable *, LiveWindowComponent>::iterator it =
169  m_components.begin(); it != m_components.end(); ++it)
170  {
171  LiveWindowSendable *component = it->first;
172  LiveWindowComponent c = it->second;
173  std::string subsystem = c.subsystem;
174  std::string name = c.name;
175  m_liveWindowTable->GetSubTable(subsystem)->PutString("~TYPE~",
176  "LW Subsystem");
177  ITable *table = m_liveWindowTable->GetSubTable(subsystem)->GetSubTable(
178  name);
179  table->PutString("~TYPE~", component->GetSmartDashboardType());
180  table->PutString("Name", name);
181  table->PutString("Subsystem", subsystem);
182  component->InitTable(table);
183  if (c.isSensor)
184  {
185  m_sensors.push_back(component);
186  }
187  }
188 }
189 
void AddActuator(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:96
void SetEnabled(bool enabled)
Definition: LiveWindow.cpp:38
static NetworkTable * GetTable(std::string key)
Definition: ITable.h:26
void Run()
Definition: LiveWindow.cpp:151
virtual ITable * GetSubTable(std::string key)=0
void AddSensor(const char *subsystem, const char *name, LiveWindowSendable *component)
Definition: LiveWindow.cpp:82
virtual void InitTable(ITable *subtable)=0
virtual void PutBoolean(std::string key, bool value)=0
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
virtual std::string GetSmartDashboardType()=0
static Scheduler * GetInstance()
Definition: Scheduler.cpp:47
virtual void PutString(std::string key, std::string value)=0

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6