Now you can download a copy of these docs so you can use them offline! Download now
HiTechnicColorSensor.cpp
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 "HiTechnicColorSensor.h"
8 #include "DigitalModule.h"
9 #include "I2C.h"
10 #include "NetworkCommunication/UsageReporting.h"
11 #include "networktables2/type/NumberArray.h"
12 #include "WPIErrors.h"
13 
14 const uint8_t HiTechnicColorSensor::kAddress;
15 const uint8_t HiTechnicColorSensor::kManufacturerBaseRegister;
16 const uint8_t HiTechnicColorSensor::kManufacturerSize;
17 const uint8_t HiTechnicColorSensor::kSensorTypeBaseRegister;
18 const uint8_t HiTechnicColorSensor::kSensorTypeSize;
19 const uint8_t HiTechnicColorSensor::kModeRegister;
20 const uint8_t HiTechnicColorSensor::kColorRegister;
21 const uint8_t HiTechnicColorSensor::kRedRegister;
22 const uint8_t HiTechnicColorSensor::kGreenRegister;
23 const uint8_t HiTechnicColorSensor::kBlueRegister;
24 const uint8_t HiTechnicColorSensor::kRawRedRegister;
25 const uint8_t HiTechnicColorSensor::kRawGreenRegister;
26 const uint8_t HiTechnicColorSensor::kRawBlueRegister;
27 
34  : m_i2c (NULL)
35 {
36  m_table = NULL;
37  DigitalModule *module = DigitalModule::GetInstance(moduleNumber);
38  m_mode = kActive;
39 
40  if (module)
41  {
42  m_i2c = module->GetI2C(kAddress);
43 
44  // Verify Sensor
45  const uint8_t kExpectedManufacturer[] = "HiTechnc";
46  const uint8_t kExpectedSensorType[] = "ColorPD ";
47  if ( ! m_i2c->VerifySensor(kManufacturerBaseRegister, kManufacturerSize, kExpectedManufacturer) )
48  {
49  wpi_setWPIError(CompassManufacturerError);
50  return;
51  }
52  if ( ! m_i2c->VerifySensor(kSensorTypeBaseRegister, kSensorTypeSize, kExpectedSensorType) )
53  {
54  wpi_setWPIError(CompassTypeError);
55  }
56 
57  nUsageReporting::report(nUsageReporting::kResourceType_HiTechnicColorSensor, moduleNumber - 1);
58  }
59 }
60 
65 {
66  delete m_i2c;
67  m_i2c = NULL;
68 }
69 
80 {
81  uint8_t color = 0;
82 
83  if(m_mode != kActive)
84  {
85  SetMode(kActive);
86  }
87  if (m_i2c)
88  {
89  m_i2c->Read(kColorRegister, sizeof(color), &color);
90  }
91  return color;
92 }
93 
106 {
107  uint8_t red = 0;
108 
109  if(m_mode != kActive)
110  {
111  SetMode(kActive);
112  }
113  if (m_i2c)
114  {
115  m_i2c->Read(kRedRegister, sizeof(red), &red);
116  }
117  return red;
118 }
119 
132 {
133  uint8_t green = 0;
134 
135  if(m_mode != kActive)
136  {
137  SetMode(kActive);
138  }
139  if (m_i2c)
140  {
141  m_i2c->Read(kGreenRegister, sizeof(green), &green);
142  }
143  return green;
144 }
145 
158 {
159  uint8_t blue = 0;
160 
161  if(m_mode != kActive)
162  {
163  SetMode(kActive);
164  }
165  if (m_i2c)
166  {
167  m_i2c->Read(kBlueRegister, sizeof(blue), &blue);
168  }
169  return blue;
170 }
171 
185 {
186  uint8_t colors[3] = {0,0,0};
187  RGB result;
188 
189  if(m_mode != kActive)
190  {
191  SetMode(kActive);
192  }
193  if(m_i2c)
194  {
195  m_i2c->Read(kRawRedRegister, sizeof(colors), (uint8_t*)&colors);
196  }
197 
198  result.red = colors[0];
199  result.green = colors[1];
200  result.blue = colors[2];
201 
202  return result;
203 }
204 
217 {
218  uint16_t rawRed = 0;
219 
220  if(m_mode == kActive)
221  {
222  SetMode(kRaw);
223  }
224  if (m_i2c)
225  {
226  m_i2c->Read(kRawRedRegister, sizeof(rawRed), (uint8_t *)&rawRed);
227  }
228  return rawRed;
229 }
230 
243 {
244  uint16_t rawGreen = 0;
245 
246  if(m_mode == kActive)
247  {
248  SetMode(kRaw);
249  }
250  if (m_i2c)
251  {
252  m_i2c->Read(kRawGreenRegister, sizeof(rawGreen), (uint8_t *)&rawGreen);
253  }
254  return rawGreen;
255 }
256 
269 {
270  uint16_t rawBlue = 0;
271 
272  if(m_mode == kActive)
273  {
274  SetMode(kRaw);
275  }
276  if (m_i2c)
277  {
278  m_i2c->Read(kRawBlueRegister, sizeof(rawBlue), (uint8_t *)&rawBlue);
279  }
280  return rawBlue;
281 }
282 
298 {
299  uint8_t colors[6] = {0,0,0,0,0,0};
300  RGB result;
301 
302  if(m_mode != kActive)
303  {
304  SetMode(kActive);
305  }
306  if(m_i2c)
307  {
308  m_i2c->Read(kRedRegister, sizeof(colors), (uint8_t*)&colors);
309  }
310 
311  result.red = (colors[0]<<8) + colors[1];
312  result.green = (colors[2]<<8) + colors[3];
313  result.blue = (colors[4]<<8) + colors[5];
314 
315  return result;
316 }
317 
327 void HiTechnicColorSensor::SetMode(tColorMode mode)
328 {
329  if(m_i2c)
330  {
331  m_i2c->Write(kModeRegister, (uint8_t)mode);
332  }
333 }
334 
335 /*
336  * Live Window code, only does anything if live window is activated.
337  */
338 std::string HiTechnicColorSensor::GetType()
339 {
340  return "Compass";
341 }
342 
347  m_table = subtable;
348  UpdateTable();
349 }
350 
355  if (m_table != NULL) {
356  m_table->PutNumber("Value", GetColor());
357  NumberArray* rgb = new NumberArray();
358  rgb->add(GetRed());
359  rgb->add(GetGreen());
360  rgb->add(GetBlue());
361  m_table->PutValue("RGB", *rgb);
362  delete rgb;
363  }
364 }
365 
370 {
371  return m_table;
372 }
373 
378 {
379 
380 }
381 
386 {
387 
388 }
virtual void PutNumber(std::string key, double value)=0
Definition: ITable.h:26
virtual void InitTable(ITable *subtable)
HiTechnicColorSensor(uint8_t moduleNumber)
void SetMode(tColorMode mode)
bool Read(uint8_t registerAddress, uint8_t count, uint8_t *data)
Definition: I2C.cpp:161
static DigitalModule * GetInstance(uint8_t moduleNumber)
virtual void PutValue(std::string key, ComplexData &value)=0
bool VerifySensor(uint8_t registerAddress, uint8_t count, const uint8_t *expected)
Definition: I2C.cpp:219
virtual ITable * GetTable()
I2C * GetI2C(uint32_t address)
bool Write(uint8_t registerAddress, uint8_t data)
Definition: I2C.cpp:140
virtual void StartLiveWindowMode()

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