From eb060db71b629d72107e67a5057bf70f7d53307d Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Mon, 9 Aug 2010 12:04:52 +0000
Subject: [PATCH] Tighten up the code in the delta movement detection code in
 the MouseHostWithParser and JoystickHostWithParser demos. Make the
 ClassDriver JoystickHostWithParser demo use the HID_ALIGN_DATA() macro
 instead of manual shifting and casting.

---
 .../JoystickHostWithParser.c                     | 11 ++++-------
 .../MouseHostWithParser/MouseHostWithParser.c    |  9 +++------
 .../JoystickHostWithParser.c                     | 14 +++++---------
 .../MouseHostWithParser/MouseHostWithParser.c    | 16 ++++++----------
 4 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/Demos/Host/ClassDriver/JoystickHostWithParser/JoystickHostWithParser.c b/Demos/Host/ClassDriver/JoystickHostWithParser/JoystickHostWithParser.c
index f1b4cac7b..8420d05ab 100644
--- a/Demos/Host/ClassDriver/JoystickHostWithParser/JoystickHostWithParser.c
+++ b/Demos/Host/ClassDriver/JoystickHostWithParser/JoystickHostWithParser.c
@@ -148,16 +148,13 @@ int main(void)
 								  (ReportItem->Attributes.Usage.Usage == USAGE_Y))                 &&
 								 (ReportItem->ItemType                == REPORT_ITEM_TYPE_In))
 						{
-							int16_t DeltaMovement = (int16_t)(ReportItem->Value << (16 - ReportItem->Attributes.BitSize));
+							int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
 							
-							if (ReportItem->Attributes.Usage.Usage == USAGE_X)
+							if (DeltaMovement)
 							{
-								if (DeltaMovement)
+								if (ReportItem->Attributes.Usage.Usage == USAGE_X)
 								  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
-							}
-							else
-							{
-								if (DeltaMovement)
+								else
 								  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
 							}
 						}
diff --git a/Demos/Host/ClassDriver/MouseHostWithParser/MouseHostWithParser.c b/Demos/Host/ClassDriver/MouseHostWithParser/MouseHostWithParser.c
index 111bed48e..7482b01b0 100644
--- a/Demos/Host/ClassDriver/MouseHostWithParser/MouseHostWithParser.c
+++ b/Demos/Host/ClassDriver/MouseHostWithParser/MouseHostWithParser.c
@@ -159,14 +159,11 @@ int main(void)
 						{
 							int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
 							
-							if (ReportItem->Attributes.Usage.Usage == USAGE_X)
+							if (DeltaMovement)
 							{
-								if (DeltaMovement)
+								if (ReportItem->Attributes.Usage.Usage == USAGE_X)
 								  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
-							}
-							else
-							{
-								if (DeltaMovement)
+								else
 								  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
 							}
 						}
diff --git a/Demos/Host/LowLevel/JoystickHostWithParser/JoystickHostWithParser.c b/Demos/Host/LowLevel/JoystickHostWithParser/JoystickHostWithParser.c
index 91bd28829..e92c242d2 100644
--- a/Demos/Host/LowLevel/JoystickHostWithParser/JoystickHostWithParser.c
+++ b/Demos/Host/LowLevel/JoystickHostWithParser/JoystickHostWithParser.c
@@ -285,17 +285,13 @@ void ProcessJoystickReport(uint8_t* JoystickReport)
 			  
 			int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
 			
-			/* Determine if the report is for the X or Y delta movement */
-			if (ReportItem->Attributes.Usage.Usage == USAGE_X)
+			/* Check to see if a (non-zero) delta movement has been indicated */
+			if (DeltaMovement)
 			{
-				/* Turn on the appropriate LED according to direction if the delta is non-zero */
-				if (DeltaMovement)
+				/* Determine if the report is for the X or Y delta movement, light LEDs as appropriate */
+				if (ReportItem->Attributes.Usage.Usage == USAGE_X)
 				  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
-			}
-			else
-			{
-				/* Turn on the appropriate LED according to direction if the delta is non-zero */
-				if (DeltaMovement)
+				else
 				  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
 			}
 		}
diff --git a/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c b/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c
index 1b9c12aa9..38e4b2564 100644
--- a/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c
+++ b/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c
@@ -299,19 +299,15 @@ void ProcessMouseReport(uint8_t* MouseReport)
 			if (!(FoundData))
 			  continue;
 			  
-			int16_t DeltaMovement = (int16_t)(ReportItem->Value << (16 - ReportItem->Attributes.BitSize));
+			int16_t DeltaMovement = HID_ALIGN_DATA(ReportItem, int16_t);
 			
-			/* Determine if the report is for the X or Y delta movement */
-			if (ReportItem->Attributes.Usage.Usage == USAGE_X)
+			/* Check to see if a (non-zero) delta movement has been indicated */
+			if (DeltaMovement)
 			{
-				/* Turn on the appropriate LED according to direction if the delta is non-zero */
-				if (DeltaMovement)
+				/* Determine if the report is for the X or Y delta movement, light LEDs as appropriate */
+				if (ReportItem->Attributes.Usage.Usage == USAGE_X)
 				  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);
-			}
-			else
-			{
-				/* Turn on the appropriate LED according to direction if the delta is non-zero */
-				if (DeltaMovement)
+				else
 				  LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);
 			}
 		}
-- 
GitLab