VEXcode Doxygen
vex_controller.h
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) Innovation First 2017-2019, All rights reserved. */
4 /* */
5 /* Module: vex_controller.h */
6 /* Author: James Pearman */
7 /* Created: 10 July 2017 */
8 /* */
9 /* Revisions: */
10 /* V1.00 TBD - Initial release */
11 /* */
12 /*----------------------------------------------------------------------------*/
13 
14 #ifndef VEX_CONTROLLER_CLASS_H
15 #define VEX_CONTROLLER_CLASS_H
16 
17 #include <functional>
18 
19 /*-----------------------------------------------------------------------------*//*---------------------------------------------------------------------------*/
23 
24 namespace vex {
29  class controller {
30  private:
31  // copy if the controller id when this class was instantiated
32  controllerType _controllerId;
33  // internal device id for this controller
34  int32_t _index;
35 
36  int32_t value( V5_ControllerIndex channel );
37 
38  protected:
39  int32_t _getIndex();
40 
41  public:
42 
49  controller();
58  ~controller();
59 
60  bool installed();
61 
69  void rumble( const char *str );
70 
71  private:
72  // bit masks for events
73  enum class tEventType {
74  EVENT_5_UP_PRESSED = 0,
75  EVENT_5_UP_RELEASED = 1,
76  EVENT_5_DN_PRESSED = 2,
77  EVENT_5_DN_RELEASED = 3,
78 
79  EVENT_6_UP_PRESSED = 4,
80  EVENT_6_UP_RELEASED = 5,
81  EVENT_6_DN_PRESSED = 6,
82  EVENT_6_DN_RELEASED = 7,
83 
84  EVENT_7_UP_PRESSED = 8,
85  EVENT_7_UP_RELEASED = 9,
86  EVENT_7_DN_PRESSED = 10,
87  EVENT_7_DN_RELEASED = 11,
88 
89  EVENT_7_L_PRESSED = 12,
90  EVENT_7_L_RELEASED = 13,
91  EVENT_7_R_PRESSED = 14,
92  EVENT_7_R_RELEASED = 15,
93 
94  EVENT_8_UP_PRESSED = 16,
95  EVENT_8_UP_RELEASED = 17,
96  EVENT_8_DN_PRESSED = 18,
97  EVENT_8_DN_RELEASED = 19,
98 
99  EVENT_8_L_PRESSED = 20,
100  EVENT_8_L_RELEASED = 21,
101  EVENT_8_R_PRESSED = 22,
102  EVENT_8_R_RELEASED = 23,
103 
104  EVENT_A_CHANGED = 24,
105  EVENT_B_CHANGED = 25,
106  EVENT_C_CHANGED = 26,
107  EVENT_D_CHANGED = 27
108  };
109 
110  enum class tButtonType {
111  kButton5Up = 0,
112  kButton5Down,
113  kButton6Up,
114  kButton6Down,
115  kButton7Up,
116  kButton7Down,
117  kButton7Left,
118  kButton7Right,
119  kButton8Up,
120  kButton8Down,
121  kButton8Left,
122  kButton8Right,
123 
124  // Final V5 button names
125  kButtonL1 = 0,
126  kButtonL2,
127  kButtonR1,
128  kButtonR2,
129  kButtonUp,
130  kButtonDown,
131  kButtonLeft,
132  kButtonRight,
133  kButtonX,
134  kButtonB,
135  kButtonY,
136  kButtonA,
137 
138  kButtonUndefined
139  };
144  public:
145  class button {
146  private:
147  tButtonType _id;
148  controller *_parent;
149  tEventType _buttonToPressedEvent();
150  tEventType _buttonToReleasedEvent();
151 
152  public:
153  button() : _id( tButtonType::kButtonUndefined), _parent(NULL) {};
154  button( const tButtonType id, controller *parent ) : _id( id ), _parent(parent) {};
155  ~button() {};
163  void pressed( void (* callback)(void) );
164 
172  void released( void (* callback)(void) );
173 
181  bool pressing( void );
182 
183  mevent PRESSED = { (uint32_t)_parent->_getIndex(), (uint32_t)_buttonToPressedEvent() };
184  mevent RELEASED = { (uint32_t)_parent->_getIndex(), (uint32_t)_buttonToReleasedEvent() };
185  };
186 
187  private:
188  button button5Up = button( tButtonType::kButton5Up , this );
189  button button5Down = button( tButtonType::kButton5Down , this );
190  button button6Up = button( tButtonType::kButton6Up , this );
191  button button6Down = button( tButtonType::kButton6Down , this );
192  button button7Up = button( tButtonType::kButton7Up , this );
193  button button7Down = button( tButtonType::kButton7Down , this );
194  button button7Left = button( tButtonType::kButton7Left , this );
195  button button7Right = button( tButtonType::kButton7Right, this );
196  button button8Up = button( tButtonType::kButton8Up , this );
197  button button8Down = button( tButtonType::kButton8Down , this );
198  button button8Left = button( tButtonType::kButton8Left , this );
199  button button8Right = button( tButtonType::kButton8Right, this );
200 
201  public:
202  // Final V5 button names
209  button &ButtonL1 = button5Up;
216  button &ButtonL2 = button5Down;
223  button &ButtonR1 = button6Up;
230  button &ButtonR2 = button6Down;
237  button &ButtonUp = button7Up;
244  button &ButtonDown = button7Down;
251  button &ButtonLeft = button7Left;
258  button &ButtonRight = button7Right;
265  button &ButtonX = button8Up;
272  button &ButtonB = button8Down;
279  button &ButtonY = button8Left;
286  button &ButtonA = button8Right;
287 
288  private:
289  enum class tAxisType {
290  kAxisA = 0,
291  kAxisB,
292  kAxisC,
293  kAxisD,
294 
295  kAxisUndefined
296  };
297 
298 
299  public:
304  class axis {
305  private:
306  tAxisType _id;
307  controller *_parent;
308  tEventType _joystickToChangedEvent();
309 
310  public:
311  axis() : _id( tAxisType::kAxisUndefined), _parent(NULL) {};
312  axis( const tAxisType id, controller *parent ) : _id( id ), _parent(parent) {};
313  ~axis() {};
314 
322  void changed( void (* callback)(void) );
323 
331  int32_t value( void );
332 
342  int32_t position( percentUnits units = percentUnits::pct );
343 
344  mevent CHANGED = { (uint32_t)_parent->_getIndex(), (uint32_t)_joystickToChangedEvent() };
345  };
346 
347 
348  private:
349  axis AxisA = axis( tAxisType::kAxisA, this );
350  axis AxisB = axis( tAxisType::kAxisB, this );
351  axis AxisC = axis( tAxisType::kAxisC, this );
352  axis AxisD = axis( tAxisType::kAxisD, this );
353 
354  public:
355  // Final V5 joystick names
362  axis &Axis1 = AxisC;
369  axis &Axis2 = AxisD;
376  axis &Axis3 = AxisA;
383  axis &Axis4 = AxisB;
384 
389  class lcd {
390  private:
391  controller *_parent;
392  int32_t _row;
393  int32_t _maxrows;
394  int32_t _col;
395  int32_t _maxcols;
396 
397  char _textStr[32];
398 
399  controllerType getControllerId();
400 
401  public:
402  lcd();
403  lcd(controller *parent);
404  ~lcd() {};
405 
414  void setCursor( int32_t row, int32_t col );
415 
423  int32_t column();
424 
432  int32_t row();
433 
444  template <class T>
445  void print( T value ) {
446  if( std::is_same< T, int >::value )
447  print( "%d", (int)value );
448  else
449  if( std::is_same< T, double >::value )
450  print( "%.2f", (double)value );
451  else {
452  // primarily to handle modkit number
453  if( (int)value == value )
454  print( "%d", (int)value );
455  else
456  print( "%.2f", (double)value );
457  }
458  }
467  void print( const char *format, ... );
468  void print( char *format, ... );
469 
476  void clearScreen( void );
477 
485  void clearLine( int number );
486 
493  void clearLine( void );
494 
501  void newLine( void );
502  };
503 
504  // Controller has an instance of an lcd
511  lcd Screen = lcd(this);
512  // backwards compatibility
513 
514  lcd &LCD = Screen;
515  };
516 };
517 
518 #endif // VEX_CONTROLLER_CLASS_H
vex::controller::ButtonA
button & ButtonA
A button that represents the A button on the controller.
Definition: vex_controller.h:286
vex::triportType::button
A triport unit that is defined as a button.
vex::controller::axis::changed
void changed(void(*callback)(void))
Sets the function to be called when the joystick axis value changes.
vex::controller::button
Use the button class to get values from the controller's buttons.
Definition: vex_controller.h:145
vex::controller::Axis1
axis & Axis1
An axis of a joystick that represents axis 1 on the controller.
Definition: vex_controller.h:362
vex::controller::ButtonX
button & ButtonX
A button that represents the x button on the controller.
Definition: vex_controller.h:265
vex::controller::button::pressing
bool pressing(void)
Gets the status of a button.
vex::controller::rumble
void rumble(const char *str)
Rumbles the controller by a pattern defined by the parameter. Dots equal short, dashes equal long and...
vex::controller::lcd::clearScreen
void clearScreen(void)
Clears the controller's LCD screen.
vex::controller::lcd
Use this class to write to the controller's LCD screen.
Definition: vex_controller.h:389
vex
device class
Definition: vex_brain.h:26
vex::controller::axis::value
int32_t value(void)
Gets the value of the joystick axis on a scale from -127 to 127.
vex::controller::ButtonB
button & ButtonB
A button that represents the B button on the controller.
Definition: vex_controller.h:272
vex::controller::button::pressed
void pressed(void(*callback)(void))
Sets the function to be called when the button is pressed.
V5_ControllerIndex
enum _V5_ControllerIndex V5_ControllerIndex
V5 Controller definitions
vex::controller::controller
controller()
Creates a new controller object.
vex::controller::axis
Use the axis class to get values from one of the controller's joysticks.
Definition: vex_controller.h:304
vex::controller::ButtonDown
button & ButtonDown
A button that represents the down button on the controller.
Definition: vex_controller.h:244
vex::controller::button::released
void released(void(*callback)(void))
Sets the function to be called when the button is released.
vex::controller::lcd::clearLine
void clearLine(void)
Clears the current line.
vex::controller
Use the controller class to get values from the remote controller as well as write to the controller'...
Definition: vex_controller.h:29
vex::controller::lcd::setCursor
void setCursor(int32_t row, int32_t col)
Sets the cursor to the row and column number set in the parameters.
vex::percentUnits::pct
A percentage unit that represents a value from 0% to 100%.
vex::controller::lcd::column
int32_t column()
An integer that tracks the current cursor position's column, starting at 1.
vex::percentUnits
percentUnits
The measurement units for percentage values.
Definition: vex_units.h:26
vex::controller::lcd::newLine
void newLine(void)
Moves the cursor to the next line.
vex::controllerType
controllerType
The defined units for controller devices.
Definition: vex_units.h:250
vex::controller::ButtonY
button & ButtonY
A button that represents the Y button on the controller.
Definition: vex_controller.h:279
vex::controller::ButtonUp
button & ButtonUp
A button that represents the up button on the controller.
Definition: vex_controller.h:237
vex::controller::Axis3
axis & Axis3
An axis of a joystick that represents axis 3 on the controller.
Definition: vex_controller.h:376
vex::controller::ButtonR2
button & ButtonR2
A button that represents the R2 button on the controller.
Definition: vex_controller.h:230
vex::controller::Axis2
axis & Axis2
An axis of a joystick that represents axis 2 on the controller.
Definition: vex_controller.h:369
vex::controller::ButtonLeft
button & ButtonLeft
A button that represents the left button on the controller.
Definition: vex_controller.h:251
vex::controller::Axis4
axis & Axis4
An axis of a joystick that represents axis 4 on the controller.
Definition: vex_controller.h:383
vex::controller::lcd::row
int32_t row()
An integer that tracks the current cursor position's row, starting at 1.
vex::controller::ButtonL2
button & ButtonL2
A button that represents the L2 button on the controller.
Definition: vex_controller.h:216
vex::controller::ButtonRight
button & ButtonRight
A button that represents the right button on the controller.
Definition: vex_controller.h:258
vex::controller::ButtonR1
button & ButtonR1
A button that represents the R1 button on the controller.
Definition: vex_controller.h:223
vex::controller::lcd::print
void print(T value)
Prints a number, string, or Boolean.
Definition: vex_controller.h:445
vex::controller::ButtonL1
button & ButtonL1
A button that represents the L1 button on the controller.
Definition: vex_controller.h:209
vex::controller::axis::position
int32_t position(percentUnits units=percentUnits::pct)
Gets the position of the joystick axis on a scale from -100 to 100.
vex::controller::Screen
lcd Screen
Controller's instance of LCD.
Definition: vex_controller.h:511