Arduino Sim Racing Library v1.1.5
Loading...
Searching...
No Matches
SimRacing::AnalogShifter Class Reference

Interface with shifters using two potentiometers for gear position. More...

#include <SimRacing.h>

Inheritance diagram for SimRacing::AnalogShifter:
SimRacing::Shifter SimRacing::Peripheral SimRacing::LogitechShifter

Classes

struct  GearPosition
 Simple struct to store X/Y coordinates for the calibration function. More...
 

Public Member Functions

 AnalogShifter (PinNum pinX, PinNum pinY, PinNum pinRev=UnusedPin, PinNum pinDetect=UnusedPin)
 Class constructor.
 
virtual void begin ()
 Initializes the hardware pins for reading the gear states.
 
virtual bool update ()
 Polls the hardware to update the current gear state.
 
long getPosition (Axis ax, long rMin=AnalogInput::Min, long rMax=AnalogInput::Max) const
 Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibration values.
 
int getPositionRaw (Axis ax) const
 Retrieves the buffered position for the analog axis.
 
bool getReverseButton () const
 Checks the current state of the "reverse" button at the bottom of the shift column.
 
void setCalibration (GearPosition neutral, GearPosition g1, GearPosition g2, GearPosition g3, GearPosition g4, GearPosition g5, GearPosition g6, float engagePoint=CalEngagementPoint, float releasePoint=CalReleasePoint, float edgeOffset=CalEdgeOffset)
 Calibrate the gear shifter for more accurate shifting.
 
void serialCalibration (Stream &iface=Serial)
 Runs an interactive calibration tool using the serial interface.
 
bool isConnected () const
 Check if the device is physically connected to the board.
 
int8_t getGear () const
 Returns the currently selected gear.
 
char getGearChar () const
 Returns a character that represents the current gear.
 
String getGearString () const
 Returns a String that represents the current gear.
 
bool gearChanged () const
 Checks whether the current gear has changed since the last update.
 
int8_t getGearMin ()
 Retrieves the minimum possible gear index.
 
int8_t getGearMax ()
 Retrieves the maximum possible gear index.
 

Static Public Member Functions

static char getGearChar (int gear)
 Returns a character that represents the given gear.
 
static String getGearString (int gear)
 Returns a String that represents the given gear.
 

Protected Attributes

const int8_t MinGear
 the lowest selectable gear
 
const int8_t MaxGear
 the highest selectable gear
 
int8_t currentGear
 index of the current gear
 
bool changed
 whether the gear has changed since the previous update
 

Detailed Description

Interface with shifters using two potentiometers for gear position.

Definition at line 547 of file SimRacing.h.

Constructor & Destructor Documentation

◆ AnalogShifter()

SimRacing::AnalogShifter::AnalogShifter ( PinNum pinX,
PinNum pinY,
PinNum pinRev = UnusedPin,
PinNum pinDetect = UnusedPin )

Class constructor.

Parameters
pinXthe analog input pin for the X axis
pinYthe analog input pin for the Y axis
pinRevthe digital input pin for the 'reverse' button
pinDetectthe digital pin for device detection (high is detected)

Definition at line 677 of file SimRacing.cpp.

Member Function Documentation

◆ begin()

void SimRacing::AnalogShifter::begin ( )
virtual

Initializes the hardware pins for reading the gear states.

Should be called in setup() before reading from the shifter.

Reimplemented from SimRacing::Peripheral.

Examples
ShiftJoystick.ino, and ShiftPrint.ino.

Definition at line 691 of file SimRacing.cpp.

◆ gearChanged()

bool SimRacing::Shifter::gearChanged ( ) const
inlineinherited

Checks whether the current gear has changed since the last update.

Returns
'true' if gear has changed, 'false' otherwise
Examples
ShiftJoystick.ino, and ShiftPrint.ino.

Definition at line 519 of file SimRacing.h.

◆ getGear()

int8_t SimRacing::Shifter::getGear ( ) const
inlineinherited

Returns the currently selected gear.

Will either be reverse (-1), neutral (0), or the current gear indexed at 1 (1st gear is 1, 2nd gear is 2, etc.).

Returns
current gear index
Examples
ShiftJoystick.ino, and ShiftPrint.ino.

Definition at line 475 of file SimRacing.h.

◆ getGearChar() [1/2]

char SimRacing::Shifter::getGearChar ( ) const
inherited

Returns a character that represents the current gear.

Returns
letter representing the current gear
See also
getGearChar(int)

Definition at line 624 of file SimRacing.cpp.

◆ getGearChar() [2/2]

char SimRacing::Shifter::getGearChar ( int gear)
staticinherited

Returns a character that represents the given gear.

'r' for reverse, 'n' for neutral, or the number of the current gear.

Parameters
gearthe gear index to get the representation for
Returns
letter representing the current gear

Definition at line 606 of file SimRacing.cpp.

◆ getGearMax()

int8_t SimRacing::Shifter::getGearMax ( )
inlineinherited

Retrieves the maximum possible gear index.

Returns
the highest gear index

Definition at line 533 of file SimRacing.h.

◆ getGearMin()

int8_t SimRacing::Shifter::getGearMin ( )
inlineinherited

Retrieves the minimum possible gear index.

Returns
the lowest gear index

Definition at line 526 of file SimRacing.h.

◆ getGearString() [1/2]

String SimRacing::Shifter::getGearString ( ) const
inherited

Returns a String that represents the current gear.

Returns
String representing the current gear
See also
getGearString(int)

Definition at line 665 of file SimRacing.cpp.

◆ getGearString() [2/2]

String SimRacing::Shifter::getGearString ( int gear)
staticinherited

Returns a String that represents the given gear.

"reverse" for reverse, "neutral" for neutral, and then "1st", "2nd", "3rd", and so on.

Parameters
gearthe gear index to get the representation for
Returns
String representing the current gear
Examples
ShiftPrint.ino.

Definition at line 628 of file SimRacing.cpp.

◆ getPosition()

long SimRacing::AnalogShifter::getPosition ( Axis ax,
long rMin = AnalogInput::Min,
long rMax = AnalogInput::Max ) const

Retrieves the buffered position for the analog axis, rescaled to a nominal range using the calibration values.

By default this is rescaled to a 10-bit value, matching the range used by the AVR analog to digital converter (ADC).

Parameters
rMinthe minimum output value for the rescaling function
rMaxthe maximum output value for the rescaling function
Returns
the axis position, buffered and rescaled
Parameters
axthe axis to get the position of
Examples
ShiftJoystick.ino.

Definition at line 786 of file SimRacing.cpp.

◆ getPositionRaw()

int SimRacing::AnalogShifter::getPositionRaw ( Axis ax) const

Retrieves the buffered position for the analog axis.

Returns
the axis position, buffered
Parameters
axthe axis to get the position of
Examples
ShiftPrint.ino.

Definition at line 791 of file SimRacing.cpp.

◆ getReverseButton()

bool SimRacing::AnalogShifter::getReverseButton ( ) const

Checks the current state of the "reverse" button at the bottom of the shift column.

This button is pressed (HIGH) when the shifter is in reverse gear, LOW otherwise.

Returns
current state of the "reverse" button
Examples
ShiftJoystick.ino.

Definition at line 796 of file SimRacing.cpp.

◆ isConnected()

bool SimRacing::AnalogShifter::isConnected ( ) const
inlinevirtual

Check if the device is physically connected to the board.

That means it is both present and detected long enough to be considered 'stable'.

Returns
'true' if the device is connected, 'false' otherwise

Reimplemented from SimRacing::Peripheral.

Definition at line 641 of file SimRacing.h.

◆ serialCalibration()

void SimRacing::AnalogShifter::serialCalibration ( Stream & iface = Serial)

Runs an interactive calibration tool using the serial interface.

Parameters
ifacethe serial interface to send and receive prompts. Defaults to Serial (CDC USB on most boards).
Examples
ShiftPrint.ino.

Definition at line 886 of file SimRacing.cpp.

◆ setCalibration()

void SimRacing::AnalogShifter::setCalibration ( GearPosition neutral,
GearPosition g1,
GearPosition g2,
GearPosition g3,
GearPosition g4,
GearPosition g5,
GearPosition g6,
float engagePoint = CalEngagementPoint,
float releasePoint = CalReleasePoint,
float edgeOffset = CalEdgeOffset )

Calibrate the gear shifter for more accurate shifting.

Note that this uses a large number of GearPosition arguments rather than an array because it allows for the use of aggregate initialization.

This way users can copy a single line to set calibration, rather than declaring an array of GearPosition elements and then passing that by pointer to this function.

Parameters
neutralthe X/Y position of the shifter in neutral
g1the X/Y position of the shifter in 1st gear
g2the X/Y position of the shifter in 2nd gear
g3the X/Y position of the shifter in 3rd gear
g4the X/Y position of the shifter in 4th gear
g5the X/Y position of the shifter in 5th gear
g6the X/Y position of the shifter in 6th gear
engagePointdistance from neutral on Y to register a gear as being engaged (as a percentage of distance from neutral to Y max, 0-1)
releasePointdistance from neutral on Y to go back into neutral from an engaged gear (as a percentage of distance from neutral to Y max, 0-1)
edgeOffsetdistance from neutral on X to select the side gears rather than the center gears (as a percentage of distance from neutral to X max, 0-1)

Definition at line 805 of file SimRacing.cpp.

◆ update()

bool SimRacing::AnalogShifter::update ( )
virtual

Polls the hardware to update the current gear state.

Returns
'true' if the gear has changed, 'false' otherwise

Implements SimRacing::Peripheral.

Examples
ShiftJoystick.ino, and ShiftPrint.ino.

Definition at line 698 of file SimRacing.cpp.

Member Data Documentation

◆ changed

bool SimRacing::Shifter::changed
protectedinherited

whether the gear has changed since the previous update

Definition at line 540 of file SimRacing.h.

◆ currentGear

int8_t SimRacing::Shifter::currentGear
protectedinherited

index of the current gear

Definition at line 539 of file SimRacing.h.

◆ MaxGear

const int8_t SimRacing::Shifter::MaxGear
protectedinherited

the highest selectable gear

Definition at line 537 of file SimRacing.h.

◆ MinGear

const int8_t SimRacing::Shifter::MinGear
protectedinherited

the lowest selectable gear

Definition at line 536 of file SimRacing.h.


The documentation for this class was generated from the following files: