Skip to content

Commit dc2826a

Browse files
committed
Fixed issue with non-zero based reverse ranges
MHeironimus#25
1 parent 2af90bc commit dc2826a

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

Joystick/src/Joystick.cpp

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#define JOYSTICK_INCLUDE_BRAKE B00001000
4242
#define JOYSTICK_INCLUDE_STEERING B00010000
4343

44-
#define JOYSTICK_VALUE_RANGE_CHECK(value, l, g) if (((value) < min((l), (g))) || ((value) > max((l), (g)))) return
45-
4644
Joystick_::Joystick_(
4745
uint8_t hidReportId,
4846
uint8_t joystickType,
@@ -521,80 +519,58 @@ void Joystick_::releaseButton(uint8_t button)
521519

522520
void Joystick_::setXAxis(int16_t value)
523521
{
524-
JOYSTICK_VALUE_RANGE_CHECK(value, _xAxisMinimum, _xAxisMaximum);
525-
526522
_xAxis = value;
527523
if (_autoSendState) sendState();
528524
}
529525
void Joystick_::setYAxis(int16_t value)
530526
{
531-
JOYSTICK_VALUE_RANGE_CHECK(value, _yAxisMinimum, _yAxisMaximum);
532-
533527
_yAxis = value;
534528
if (_autoSendState) sendState();
535529
}
536530
void Joystick_::setZAxis(int16_t value)
537531
{
538-
JOYSTICK_VALUE_RANGE_CHECK(value, _zAxisMinimum, _zAxisMaximum);
539-
540532
_zAxis = value;
541533
if (_autoSendState) sendState();
542534
}
543535

544536
void Joystick_::setRxAxis(int16_t value)
545537
{
546-
JOYSTICK_VALUE_RANGE_CHECK(value, _rxAxisMinimum, _rxAxisMaximum);
547-
548538
_xAxisRotation = value;
549539
if (_autoSendState) sendState();
550540
}
551541
void Joystick_::setRyAxis(int16_t value)
552542
{
553-
JOYSTICK_VALUE_RANGE_CHECK(value, _ryAxisMinimum, _ryAxisMaximum);
554-
555543
_yAxisRotation = value;
556544
if (_autoSendState) sendState();
557545
}
558546
void Joystick_::setRzAxis(int16_t value)
559547
{
560-
JOYSTICK_VALUE_RANGE_CHECK(value, _rzAxisMinimum, _rzAxisMaximum);
561-
562548
_zAxisRotation = value;
563549
if (_autoSendState) sendState();
564550
}
565551

566552
void Joystick_::setRudder(int16_t value)
567553
{
568-
JOYSTICK_VALUE_RANGE_CHECK(value, _rudderMinimum, _rudderMaximum);
569-
570554
_rudder = value;
571555
if (_autoSendState) sendState();
572556
}
573557
void Joystick_::setThrottle(int16_t value)
574558
{
575-
JOYSTICK_VALUE_RANGE_CHECK(value, _throttleMinimum, _throttleMaximum);
576-
577559
_throttle = value;
578560
if (_autoSendState) sendState();
579561
}
580562
void Joystick_::setAccelerator(int16_t value)
581563
{
582-
JOYSTICK_VALUE_RANGE_CHECK(value, _acceleratorMinimum, _acceleratorMaximum);
583-
584564
_accelerator = value;
585565
if (_autoSendState) sendState();
586566
}
587567
void Joystick_::setBrake(int16_t value)
588568
{
589-
JOYSTICK_VALUE_RANGE_CHECK(value, _brakeMinimum, _brakeMaximum);
590-
591569
_brake = value;
592570
if (_autoSendState) sendState();
593571
}
594572
void Joystick_::setSteering(int16_t value)
595573
{
596-
JOYSTICK_VALUE_RANGE_CHECK(value, _steeringMinimum, _steeringMaximum);
597-
598574
_steering = value;
599575
if (_autoSendState) sendState();
600576
}
@@ -612,27 +588,32 @@ int Joystick_::buildAndSet16BitValue(bool includeValue, int16_t value, int16_t v
612588
int16_t convertedValue;
613589
uint8_t highByte;
614590
uint8_t lowByte;
591+
int16_t realMinimum = min(valueMinimum, valueMaximum);
592+
int16_t realMaximum = max(valueMinimum, valueMaximum);
615593

616-
if (includeValue == true) {
594+
if (includeValue == false) return 0;
617595

618-
if (valueMinimum > valueMaximum) {
619-
// Values go from a larger number to a smaller number (e.g. 1024 to 0)
620-
convertedValue = map(valueMinimum - value, valueMaximum, valueMinimum, actualMinimum, actualMaximum);
621-
} else {
622-
// Values go from a smaller number to a larger number (e.g. 0 to 1024)
623-
convertedValue = map(value, valueMinimum, valueMaximum, actualMinimum, actualMaximum);
624-
}
625-
626-
highByte = (uint8_t)(convertedValue >> 8);
627-
lowByte = (uint8_t)(convertedValue & 0x00FF);
628-
629-
dataLocation[0] = lowByte;
630-
dataLocation[1] = highByte;
631-
632-
return 2;
596+
if (value < realMinimum) {
597+
value = realMinimum;
598+
}
599+
if (value > realMaximum) {
600+
value = realMaximum;
633601
}
602+
603+
if (valueMinimum > valueMaximum) {
604+
// Values go from a larger number to a smaller number (e.g. 1024 to 0)
605+
value = realMaximum - value + realMinimum;
606+
}
607+
608+
convertedValue = map(value, realMinimum, realMaximum, actualMinimum, actualMaximum);
609+
610+
highByte = (uint8_t)(convertedValue >> 8);
611+
lowByte = (uint8_t)(convertedValue & 0x00FF);
612+
613+
dataLocation[0] = lowByte;
614+
dataLocation[1] = highByte;
634615

635-
return 0;
616+
return 2;
636617
}
637618

638619
int Joystick_::buildAndSetAxisValue(bool includeAxis, int16_t axisValue, int16_t axisMinimum, int16_t axisMaximum, uint8_t dataLocation[])

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ I have tested this library using the following Arduino IDE Versions:
205205
- 1.6.11
206206
- 1.6.12
207207
- 1.6.13
208+
- 1.8.0
208209

209210
I have tested this library with the following boards:
210211

@@ -220,4 +221,4 @@ works with the Arduino Due. I have also been told Version 1.x of the the Arduino
220221
- [Arduino UNO](https://www.arduino.cc/en/Main/ArduinoBoardUno) - NOT Supported - However, it might work with the [NicoHood/HoodLoader2](https://github.com/NicoHood/HoodLoader2) library, but I have not had a chance to try this out yet.
221222
- [Arduino MEGA](https://www.arduino.cc/en/Main/ArduinoBoardMega2560) - NOT Supported - However, it might work with the [NicoHood/HoodLoader2](https://github.com/NicoHood/HoodLoader2) library, but I have not had a chance to try this out yet.
222223

223-
(as of 2016-11-25)
224+
(as of 2016-12-24)

0 commit comments

Comments
 (0)