@@ -655,6 +655,37 @@ void Joystick_::setHatSwitch(int8_t hatSwitchIndex, int16_t value)
655
655
if (_autoSendState) sendState ();
656
656
}
657
657
658
+ int Joystick_::buildAndSet16BitValue (bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, int16_t actualMinimum, int16_t actualMaximum, uint8_t dataLocation[])
659
+ {
660
+ int16_t convertedValue;
661
+ uint8_t highByte;
662
+ uint8_t lowByte;
663
+
664
+ if (includeValue == true ) {
665
+ convertedValue = map (value, valueMinimum, valueMaximum, actualMinimum, actualMaximum);
666
+
667
+ highByte = (uint8_t )(convertedValue >> 8 );
668
+ lowByte = (uint8_t )(convertedValue & 0x00FF );
669
+
670
+ dataLocation[0 ] = lowByte;
671
+ dataLocation[1 ] = highByte;
672
+
673
+ return 2 ;
674
+ }
675
+
676
+ return 0 ;
677
+ }
678
+
679
+ int Joystick_::buildAndSetAxisValue (bool includeAxis, int16_t axisValue, int16_t axisMinimum, int16_t axisMaximum, uint8_t dataLocation[])
680
+ {
681
+ return buildAndSet16BitValue (includeAxis, axisValue, axisMinimum, axisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM, dataLocation);
682
+ }
683
+
684
+ int Joystick_::buildAndSetSimulationValue (bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, uint8_t dataLocation[])
685
+ {
686
+ return buildAndSet16BitValue (includeValue, value, valueMinimum, valueMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM, dataLocation);
687
+ }
688
+
658
689
void Joystick_::sendState ()
659
690
{
660
691
uint8_t data[_hidReportSize];
@@ -689,108 +720,19 @@ void Joystick_::sendState()
689
720
} // Hat Switches
690
721
691
722
// Set Axis Values
692
- int16_t convertedValue;
693
- uint8_t highByte;
694
- uint8_t lowByte;
695
- if (_includeXAxis == true ) {
696
- convertedValue = map (_xAxis, _xAxisMinimum, _xAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
697
-
698
- highByte = (uint8_t )(convertedValue >> 8 );
699
- lowByte = (uint8_t )(convertedValue & 0x00FF );
700
-
701
- data[index++] = lowByte;
702
- data[index++] = highByte;
703
- }
704
- if (_includeYAxis == true ) {
705
- convertedValue = map (_yAxis, _yAxisMinimum, _yAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
706
-
707
- highByte = (uint8_t )(convertedValue >> 8 );
708
- lowByte = (uint8_t )(convertedValue & 0x00FF );
709
-
710
- data[index++] = lowByte;
711
- data[index++] = highByte;
712
- }
713
- if (_includeZAxis == true ) {
714
- convertedValue = map (_zAxis, _zAxisMinimum, _zAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
715
-
716
- highByte = (uint8_t )(convertedValue >> 8 );
717
- lowByte = (uint8_t )(convertedValue & 0x00FF );
718
-
719
- data[index++] = lowByte;
720
- data[index++] = highByte;
721
- }
722
- if (_includeRxAxis == true ) {
723
- convertedValue = map (_xAxisRotation, _rxAxisMinimum, _rxAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
724
-
725
- highByte = (uint8_t )(convertedValue >> 8 );
726
- lowByte = (uint8_t )(convertedValue & 0x00FF );
727
-
728
- data[index++] = lowByte;
729
- data[index++] = highByte;
730
- }
731
- if (_includeRyAxis == true ) {
732
- convertedValue = map (_yAxisRotation, _ryAxisMinimum, _ryAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
733
-
734
- highByte = (uint8_t )(convertedValue >> 8 );
735
- lowByte = (uint8_t )(convertedValue & 0x00FF );
736
-
737
- data[index++] = lowByte;
738
- data[index++] = highByte;
739
- }
740
- if (_includeRzAxis == true ) {
741
- convertedValue = map (_zAxisRotation, _rzAxisMinimum, _rzAxisMaximum, JOYSTICK_AXIS_MINIMUM, JOYSTICK_AXIS_MAXIMUM);
742
-
743
- highByte = (uint8_t )(convertedValue >> 8 );
744
- lowByte = (uint8_t )(convertedValue & 0x00FF );
745
-
746
- data[index++] = lowByte;
747
- data[index++] = highByte;
748
- }
749
- if (_includeRudder == true ) {
750
- convertedValue = map (_rudder, _rudderMinimum, _rudderMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM);
751
-
752
- highByte = (uint8_t )(convertedValue >> 8 );
753
- lowByte = (uint8_t )(convertedValue & 0x00FF );
754
-
755
- data[index++] = lowByte;
756
- data[index++] = highByte;
757
- }
758
- if (_includeThrottle == true ) {
759
- convertedValue = map (_throttle, _throttleMinimum, _throttleMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM);
760
-
761
- highByte = (uint8_t )(convertedValue >> 8 );
762
- lowByte = (uint8_t )(convertedValue & 0x00FF );
763
-
764
- data[index++] = lowByte;
765
- data[index++] = highByte;
766
- }
767
- if (_includeAccelerator == true ) {
768
- convertedValue = map (_accelerator, _acceleratorMinimum, _acceleratorMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM);
769
-
770
- highByte = (uint8_t )(convertedValue >> 8 );
771
- lowByte = (uint8_t )(convertedValue & 0x00FF );
772
-
773
- data[index++] = lowByte;
774
- data[index++] = highByte;
775
- }
776
- if (_includeBrake == true ) {
777
- convertedValue = map (_brake, _brakeMinimum, _brakeMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM);
778
-
779
- highByte = (uint8_t )(convertedValue >> 8 );
780
- lowByte = (uint8_t )(convertedValue & 0x00FF );
781
-
782
- data[index++] = lowByte;
783
- data[index++] = highByte;
784
- }
785
- if (_includeSteering == true ) {
786
- convertedValue = map (_steering, _steeringMinimum, _steeringMaximum, JOYSTICK_SIMULATOR_MINIMUM, JOYSTICK_SIMULATOR_MAXIMUM);
787
-
788
- highByte = (uint8_t )(convertedValue >> 8 );
789
- lowByte = (uint8_t )(convertedValue & 0x00FF );
790
-
791
- data[index++] = lowByte;
792
- data[index++] = highByte;
793
- }
723
+ index += buildAndSetAxisValue (_includeXAxis, _xAxis, _xAxisMinimum, _xAxisMaximum, &(data[index]));
724
+ index += buildAndSetAxisValue (_includeYAxis, _yAxis, _yAxisMinimum, _yAxisMaximum, &(data[index]));
725
+ index += buildAndSetAxisValue (_includeZAxis, _zAxis, _zAxisMinimum, _zAxisMaximum, &(data[index]));
726
+ index += buildAndSetAxisValue (_includeRxAxis, _xAxisRotation, _rxAxisMinimum, _rxAxisMaximum, &(data[index]));
727
+ index += buildAndSetAxisValue (_includeRyAxis, _yAxisRotation, _ryAxisMinimum, _ryAxisMaximum, &(data[index]));
728
+ index += buildAndSetAxisValue (_includeRzAxis, _zAxisRotation, _rzAxisMinimum, _rzAxisMaximum, &(data[index]));
729
+
730
+ // Set Simulation Values
731
+ index += buildAndSetSimulationValue (_includeRudder, _rudder, _rudderMinimum, _rudderMaximum, &(data[index]));
732
+ index += buildAndSetSimulationValue (_includeThrottle, _throttle, _throttleMinimum, _throttleMaximum, &(data[index]));
733
+ index += buildAndSetSimulationValue (_includeAccelerator, _accelerator, _acceleratorMinimum, _acceleratorMaximum, &(data[index]));
734
+ index += buildAndSetSimulationValue (_includeBrake, _brake, _brakeMinimum, _brakeMaximum, &(data[index]));
735
+ index += buildAndSetSimulationValue (_includeSteering, _steering, _steeringMinimum, _steeringMaximum, &(data[index]));
794
736
795
737
DynamicHID ().SendReport (_hidReportId, data, _hidReportSize);
796
738
}
0 commit comments