SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
ConnectorX.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/I2C.h>
4#include <frc/Timer.h>
5#include <frc/smartdashboard/SmartDashboard.h>
6#include <frc/util/Color8Bit.h>
7#include <frc2/command/CommandPtr.h>
8#include <frc2/command/InstantCommand.h>
9#include <frc2/command/SubsystemBase.h>
10#include <hal/SimDevice.h>
11
12#include <chrono>
13#include <memory>
14#include <string>
15#include <thread>
16#include <vector>
17
20
21namespace ConnectorX {
22struct Message {
23 uint8_t data[61];
24 uint8_t len;
25 // Set to 0xFFFF to send to all
26 uint16_t teamNumber;
27};
28namespace Commands {
30 uint16_t count;
31 uint8_t brightness;
32};
33
35 int8_t valid;
36 uint16_t teamNumber;
37 // Send messages to 2 other teams
38 uint16_t initialTeams[2];
41};
42enum class CommandType {
43 // W
44 On = 0,
45 // W
46 Off = 1,
47 // W
48 Pattern = 2,
49 // W
50 ChangeColor = 3,
51 // R
53 // W
54 SetLedPort = 5,
55 // R
56 ReadAnalog = 6,
57 // W
58 DigitalSetup = 7,
59 // W
60 DigitalWrite = 8,
61 // R
62 DigitalRead = 9,
63 // W
64 SetConfig = 10,
65 // R
66 ReadConfig = 11,
67 // W
68 RadioSend = 12,
69 // R
71 // R
72 GetColor = 14,
73 // R
74 GetPort = 15,
75 // W
76 SetPatternZone = 16,
77 // W
78 SetNewZones = 17,
79 // W
80 SyncStates = 18,
81};
82
83struct CommandOn {};
84
85struct CommandOff {};
86
87// * Set delay to -1 to use default delay
89 uint8_t pattern;
90 uint8_t oneShot;
91 int16_t delay;
92};
93
95 uint8_t red;
96 uint8_t green;
97 uint8_t blue;
98};
99
101
103 uint8_t port;
104};
105
107 uint8_t port;
108};
109
111 uint8_t port;
122 uint8_t mode;
123};
124
126 uint8_t port;
127 uint8_t value;
128};
129
131 uint8_t port;
132};
133
137
139
143
145
147
149
151 uint16_t zoneIndex;
152 // if non-zero, will go from end pixel to start pixel
153 uint8_t reversed;
154};
155
156struct NewZone {
157 uint16_t offset;
158 uint16_t count;
159};
160
165
167 uint8_t zoneCount;
168 uint8_t zones[10];
169};
170
192
197
199 uint8_t done;
200};
201
203 uint16_t value;
204};
205
207 uint8_t value;
208};
209
213
217
219 uint32_t color;
220};
221
223 uint8_t port;
224};
225
235
240} // namespace Commands
241
242enum class PatternType {
243 None = 0,
244 SetAll = 1,
245 Blink = 2,
246 RGBFade = 3,
247 HackerMode = 4, // No worky
248 Breathe = 5,
249 SineRoll = 6,
250 Chase = 7,
251 AngryEyes = 8,
252 HappyEyes = 9,
253 BlinkingEyes = 10,
254 SurprisedEyes = 11,
255 Amogus = 12,
256 OwOEyes = 14,
257};
258
259enum class PinMode {
260 INPUT = 0,
261 OUTPUT = 1,
262 INPUT_PULLUP = 2,
263 INPUT_PULLDOWN = 3,
264 OUTPUT_2MA = 4,
265 OUTPUT_4MA = 5,
266 OUTPUT_8MA = 6,
267 OUTPUT_12MA = 7
268};
269
270enum class DigitalPort { D0 = 0, D1 = 1, D2 = 2 };
271
272enum class AnalogPort {};
273
274enum class LedPort { P0 = 0, P1 = 1 };
275
281 uint16_t offset;
282 uint16_t count;
284 frc::Color8Bit color;
286
288 offset = zone.offset;
289 count = zone.count;
290 reversed = false;
291 color = frc::Color8Bit(0, 0, 0);
293 }
294
295 std::string toString() {
296 return std::string("\tOffset: ") + std::to_string(offset) + "\n" +
297 std::string("\tCount: ") + std::to_string(count) + "\n" +
298 std::string("\tReversed: ") + std::to_string(reversed) + "\n" +
299 std::string("\tColor: ") + std::string(color.HexString().c_str()) +
300 "\n" + std::string("\tPattern: ") +
301 std::to_string(static_cast<uint8_t>(pattern)) + "\n";
302 }
303};
304
306 bool on;
308 std::vector<CachedZone> zones;
309};
310
312 uint8_t currentPort;
313 std::vector<CachedPort> ports;
314};
315
320class ConnectorXBoard : public frc2::SubsystemBase {
321public:
329 explicit ConnectorXBoard(uint8_t slaveAddress,
330 frc::I2C::Port port = frc::I2C::kMXP,
331 units::second_t connectorXDelay = 0.002_s);
332
339
345 inline Commands::CommandType lastCommand() const { return _lastCommand; }
346
353 inline PatternType lastPattern(LedPort port, uint8_t zoneIndex = 0) const {
354 return m_device.ports[static_cast<uint8_t>(port)].zones[zoneIndex].pattern;
355 }
356
364
370 void writeDigitalPin(DigitalPort port, bool value);
371
380
387 uint16_t readAnalogPin(AnalogPort port);
388
393 void setOn();
394
399 void setOff();
400
407 void setPattern(LedPort port, PatternType pattern, bool oneShot = false,
408 int16_t delay = -1, uint8_t zoneIndex = 0,
409 bool reversed = false);
410
415 void setColor(LedPort port, uint8_t red, uint8_t green, uint8_t blue,
416 uint8_t zoneIndex = 0);
417
421 void setColor(LedPort port, frc::Color8Bit color, uint8_t zoneIndex = 0) {
422 setColor(port, color.red, color.green, color.blue, zoneIndex);
423 }
424
430 void setColor(LedPort port, uint32_t color, uint8_t zoneIndex = 0) {
431 setColor(port, (color >> 16) & 255, (color >> 8) & 255, color & 255,
432 zoneIndex);
433 }
434
438 frc::Color8Bit getCurrentColor(LedPort port, uint8_t zoneIndex = 0) {
439 return m_device.ports[static_cast<uint8_t>(port)].zones[zoneIndex].color;
440 }
441
448
455
462
469
476
483 return static_cast<LedPort>(m_device.currentPort);
484 }
485
487 return m_device.ports[m_device.currentPort];
488 }
489
490 void setLedPort(LedPort port);
491
502 CachedZone &setCurrentZone(LedPort port, uint8_t zoneIndex = 0,
503 bool reversed = false, bool setReversed = false);
504
506 auto &port = getCurrentCachedPort();
507
508 return port.zones[port.currentZoneIndex];
509 }
510
514 void syncZones(LedPort port, const std::vector<uint8_t> &zones);
515
519 void createZones(LedPort port, std::vector<Commands::NewZone> &&newZones);
520
521private:
522 Commands::Response sendCommand(Commands::Command command,
523 bool expectResponse = false);
524
525 void delaySeconds(units::second_t delaySeconds) {
526 std::this_thread::sleep_for(std::chrono::milliseconds(2));
527 }
528
529 std::unique_ptr<frc::I2C> _i2c;
530 uint8_t _slaveAddress;
531 LedPort _currentLedPort = LedPort::P0;
532 units::second_t _delay;
533 CachedDevice m_device;
534 Commands::CommandType _lastCommand;
535 hal::SimDevice m_simDevice;
536 hal::SimInt m_simColorR, m_simColorG, m_simColorB;
537 hal::SimBoolean m_simOn;
538};
539} // namespace ConnectorX
Driver for use with the I2C, V2 iteration of Connector-X.
Definition ConnectorX.h:320
bool initialize()
Start communication with the controller.
void sendRadioMessage(Message message)
Send a message via the radio; set team to 0xFFFF to broadcast to all.
void syncZones(LedPort port, const std::vector< uint8_t > &zones)
Sync up to 10 zones to the same 0 state.
void createZones(LedPort port, std::vector< Commands::NewZone > &&newZones)
Create up to 10 new zones.
Commands::CommandType lastCommand() const
Get the last command sent.
Definition ConnectorX.h:345
void setLedPort(LedPort port)
ConnectorXBoard(uint8_t slaveAddress, frc::I2C::Port port=frc::I2C::kMXP, units::second_t connectorXDelay=0.002_s)
Construct a new Connector-X driver instance.
void setConfig(Commands::Configuration config)
Store the config in board's EEPROM.
CachedZone & getCurrentZone()
Definition ConnectorX.h:505
void configureDigitalPin(DigitalPort port, PinMode mode)
Configure a digital IO pin. Think Arduino pinMode()
void setPattern(LedPort port, PatternType pattern, bool oneShot=false, int16_t delay=-1, uint8_t zoneIndex=0, bool reversed=false)
Send the PATTERN command.
frc::Color8Bit getCurrentColor(LedPort port, uint8_t zoneIndex=0)
Get the current on-board Color, not the cached one.
Definition ConnectorX.h:438
void setColor(LedPort port, uint8_t red, uint8_t green, uint8_t blue, uint8_t zoneIndex=0)
Set the color; must also call a pattern to see it.
bool readDigitalPin(DigitalPort port)
Read state of digital IO pin.
Commands::Configuration readConfig()
Read the config stored in EEPROM.
LedPort getCurrentLedPort()
Get the current port.
Definition ConnectorX.h:482
bool getPatternDone(LedPort port)
Read if pattern is done running.
Message getLatestRadioMessage()
Read the last received message.
void setColor(LedPort port, frc::Color8Bit color, uint8_t zoneIndex=0)
Set the color using frc::Color8Bit.
Definition ConnectorX.h:421
uint16_t readAnalogPin(AnalogPort port)
Read the ADC value. Ranges from 0 - 3.3v; 12 bits of precision.
void writeDigitalPin(DigitalPort port, bool value)
Set a digital IO pin.
CachedZone & setCurrentZone(LedPort port, uint8_t zoneIndex=0, bool reversed=false, bool setReversed=false)
Set the current zone for running patterns.
void setColor(LedPort port, uint32_t color, uint8_t zoneIndex=0)
Set the color; must also call a pattern to see it.
Definition ConnectorX.h:430
PatternType lastPattern(LedPort port, uint8_t zoneIndex=0) const
Get the last pattern sent.
Definition ConnectorX.h:353
CachedPort & getCurrentCachedPort()
Definition ConnectorX.h:486
std::vector< CachedPort > ports
Definition ConnectorX.h:313
std::vector< CachedZone > zones
Definition ConnectorX.h:308
Stores the state of the Connector-X device locally.
Definition ConnectorX.h:280
CachedZone(Commands::NewZone zone)
Definition ConnectorX.h:287
std::string toString()
Definition ConnectorX.h:295
frc::Color8Bit color
Definition ConnectorX.h:284
uint8_t data[61]
Definition ConnectorX.h:23
CommandDigitalSetup commandDigitalSetup
Definition ConnectorX.h:179
CommandSetNewZones commandSetNewZones
Definition ConnectorX.h:189
CommandDigitalRead commandDigitalRead
Definition ConnectorX.h:181
CommandRadioGetLatestReceived commandRadioGetLatestReceived
Definition ConnectorX.h:185
CommandDigitalWrite commandDigitalWrite
Definition ConnectorX.h:180
CommandReadConfig commandReadConfig
Definition ConnectorX.h:183
CommandSetPatternZone commandSetPatternZone
Definition ConnectorX.h:188
CommandReadAnalog commandReadAnalog
Definition ConnectorX.h:178
CommandReadPatternDone commandReadPatternDone
Definition ConnectorX.h:176
CommandSyncZoneStates commandSyncZoneStates
Definition ConnectorX.h:190
CommandSetLedPort commandSetLedPort
Definition ConnectorX.h:177
ResponseReadAnalog responseReadAnalog
Definition ConnectorX.h:228
ResponseRadioLastReceived responseRadioLastReceived
Definition ConnectorX.h:230
ResponsePatternDone responsePatternDone
Definition ConnectorX.h:227
ResponseDigitalRead responseDigitalRead
Definition ConnectorX.h:229
ResponseReadConfiguration responseReadConfiguration
Definition ConnectorX.h:231
ResponseReadColor responseReadColor
Definition ConnectorX.h:232