Skip to content

Commit cbcc514

Browse files
authored
Merge pull request MHeironimus#27 from MHeironimus/master
Update version-2.0 branch with issue-25 changes
2 parents e0965a8 + d9c382d commit cbcc514

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

Joystick/examples/JoystickTest/JoystickTest.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ void testHatSwitch(unsigned int currentStep)
157157
void testThrottleRudder(unsigned int value)
158158
{
159159
Joystick.setThrottle(value);
160-
Joystick.setRudder(255 - value);
160+
Joystick.setRudder(value);
161161
}
162162

163163
void testXYZAxisRotation(unsigned int degree)
164164
{
165165
Joystick.setRxAxis(degree);
166-
Joystick.setRyAxis(360 - degree);
166+
Joystick.setRyAxis(degree);
167167
Joystick.setRzAxis(degree * 2);
168168
}
169169

@@ -174,10 +174,10 @@ void setup() {
174174
Joystick.setYAxisRange(-127, 127);
175175
Joystick.setZAxisRange(-127, 127);
176176
Joystick.setRxAxisRange(0, 360);
177-
Joystick.setRyAxisRange(0, 360);
177+
Joystick.setRyAxisRange(360, 0);
178178
Joystick.setRzAxisRange(0, 720);
179179
Joystick.setThrottleRange(0, 255);
180-
Joystick.setRudderRange(0, 255);
180+
Joystick.setRudderRange(255, 0);
181181

182182
if (testAutoSendMode)
183183
{

Joystick/src/Joystick.cpp

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -519,80 +519,58 @@ void Joystick_::releaseButton(uint8_t button)
519519

520520
void Joystick_::setXAxis(int16_t value)
521521
{
522-
if ((value < _xAxisMinimum) || (value > _xAxisMaximum)) return;
523-
524522
_xAxis = value;
525523
if (_autoSendState) sendState();
526524
}
527525
void Joystick_::setYAxis(int16_t value)
528526
{
529-
if ((value < _yAxisMinimum) || (value > _yAxisMaximum)) return;
530-
531527
_yAxis = value;
532528
if (_autoSendState) sendState();
533529
}
534530
void Joystick_::setZAxis(int16_t value)
535531
{
536-
if ((value < _zAxisMinimum) || (value > _zAxisMaximum)) return;
537-
538532
_zAxis = value;
539533
if (_autoSendState) sendState();
540534
}
541535

542536
void Joystick_::setRxAxis(int16_t value)
543537
{
544-
if ((value < _rxAxisMinimum) || (value > _rxAxisMaximum)) return;
545-
546538
_xAxisRotation = value;
547539
if (_autoSendState) sendState();
548540
}
549541
void Joystick_::setRyAxis(int16_t value)
550542
{
551-
if ((value < _ryAxisMinimum) || (value > _ryAxisMaximum)) return;
552-
553543
_yAxisRotation = value;
554544
if (_autoSendState) sendState();
555545
}
556546
void Joystick_::setRzAxis(int16_t value)
557547
{
558-
if ((value < _rzAxisMinimum) || (value > _rzAxisMaximum)) return;
559-
560548
_zAxisRotation = value;
561549
if (_autoSendState) sendState();
562550
}
563551

564552
void Joystick_::setRudder(int16_t value)
565553
{
566-
if ((value < _rudderMinimum) || (value > _rudderMaximum)) return;
567-
568554
_rudder = value;
569555
if (_autoSendState) sendState();
570556
}
571557
void Joystick_::setThrottle(int16_t value)
572558
{
573-
if ((value < _throttleMinimum) || (value > _throttleMaximum)) return;
574-
575559
_throttle = value;
576560
if (_autoSendState) sendState();
577561
}
578562
void Joystick_::setAccelerator(int16_t value)
579563
{
580-
if ((value < _acceleratorMinimum) || (value > _acceleratorMaximum)) return;
581-
582564
_accelerator = value;
583565
if (_autoSendState) sendState();
584566
}
585567
void Joystick_::setBrake(int16_t value)
586568
{
587-
if ((value < _brakeMinimum) || (value > _brakeMaximum)) return;
588-
589569
_brake = value;
590570
if (_autoSendState) sendState();
591571
}
592572
void Joystick_::setSteering(int16_t value)
593573
{
594-
if ((value < _steeringMinimum) || (value > _steeringMaximum)) return;
595-
596574
_steering = value;
597575
if (_autoSendState) sendState();
598576
}
@@ -610,20 +588,32 @@ int Joystick_::buildAndSet16BitValue(bool includeValue, int16_t value, int16_t v
610588
int16_t convertedValue;
611589
uint8_t highByte;
612590
uint8_t lowByte;
591+
int16_t realMinimum = min(valueMinimum, valueMaximum);
592+
int16_t realMaximum = max(valueMinimum, valueMaximum);
613593

614-
if (includeValue == true) {
615-
convertedValue = map(value, valueMinimum, valueMaximum, actualMinimum, actualMaximum);
616-
617-
highByte = (uint8_t)(convertedValue >> 8);
618-
lowByte = (uint8_t)(convertedValue & 0x00FF);
619-
620-
dataLocation[0] = lowByte;
621-
dataLocation[1] = highByte;
622-
623-
return 2;
594+
if (includeValue == false) return 0;
595+
596+
if (value < realMinimum) {
597+
value = realMinimum;
598+
}
599+
if (value > realMaximum) {
600+
value = realMaximum;
624601
}
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;
625615

626-
return 0;
616+
return 2;
627617
}
628618

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

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Arduino Joystick Library
2-
#### Version 2.0.1
2+
#### Version 2.0.2
33
This library can be used with Arduino IDE 1.6.6 (or above) to add one or more joysticks (or gamepads) to the list of HID devices an [Arduino Leonardo](https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) or [Arduino Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro) (or any Arduino clone that is based on the ATmega32u4) can support. This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).
44

55
## Features
@@ -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)