Skip to content

Commit 39a906d

Browse files
authored
Merge pull request #4 from dmadison/handbrake-change
Send handbrake position on change
2 parents 078c5e0 + f1e0067 commit 39a906d

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

examples/Handbrake/HandbrakeJoystick/HandbrakeJoystick.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Joystick_ Joystick(
4545
false, false, false, false, false, false, false, false); // no other axes
4646

4747
const int ADC_Max = 1023; // max value of the analog inputs, 10-bit on AVR boards
48+
const bool AlwaysSend = false; // override the position checks, *always* send data constantly
4849

4950

5051
void setup() {
@@ -53,13 +54,20 @@ void setup() {
5354
// if you have one, your calibration line should go here
5455

5556
Joystick.begin(false); // 'false' to disable auto-send
56-
5757
Joystick.setZAxisRange(0, ADC_Max);
58+
59+
updateJoystick(); // send initial state
5860
}
5961

6062
void loop() {
6163
handbrake.update();
6264

65+
if (handbrake.positionChanged() || AlwaysSend) {
66+
updateJoystick();
67+
}
68+
}
69+
70+
void updateJoystick() {
6371
int pos = handbrake.getPosition(0, ADC_Max);
6472
Joystick.setZAxis(pos);
6573

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ setCalibration KEYWORD2
7070

7171
getPosition KEYWORD2
7272
getPositionRaw KEYWORD2
73+
positionChanged KEYWORD2
7374

7475
hasPedal KEYWORD2
7576

@@ -104,6 +105,7 @@ serialCalibration KEYWORD2
104105

105106
getPosition KEYWORD2
106107
getPositionRaw KEYWORD2
108+
positionChanged KEYWORD2
107109

108110
setCalibration KEYWORD2
109111
serialCalibration KEYWORD2

src/SimRacing.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,15 +987,18 @@ LogitechShifter::LogitechShifter(uint8_t pinX, uint8_t pinY, uint8_t pinRev, uin
987987
//#########################################################
988988

989989
Handbrake::Handbrake(uint8_t pinAx, uint8_t detectPin)
990-
: analogAxis(pinAx), detector(detectPin)
990+
:
991+
analogAxis(pinAx),
992+
detector(detectPin),
993+
changed(false)
991994
{}
992995

993996
void Handbrake::begin() {
994997
update(); // set initial handbrake position
995998
}
996999

9971000
bool Handbrake::update() {
998-
bool changed = false;
1001+
changed = false;
9991002

10001003
detector.poll();
10011004
if (detector.getState() == DeviceConnection::Connected) {

src/SimRacing.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,13 @@ namespace SimRacing {
716716
*/
717717
int getPositionRaw() const;
718718

719+
/**
720+
* Checks whether the handbrake's position has changed since the last update.
721+
*
722+
* @return 'true' if the position has changed, 'false' otherwise
723+
*/
724+
bool positionChanged() const { return changed; }
725+
719726
/// @copydoc AnalogInput::setCalibration()
720727
void setCalibration(AnalogInput::Calibration newCal);
721728

@@ -728,6 +735,7 @@ namespace SimRacing {
728735
private:
729736
AnalogInput analogAxis; ///< axis data for the handbrake's position
730737
DeviceConnection detector; ///< detector instance for checking if the handbrake is connected
738+
bool changed; ///< whether the handbrake position has changed since the previous update
731739
};
732740

733741

0 commit comments

Comments
 (0)