From 7184153e5dc68e134d64cb16a1096ba9f1957964 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 19 Mar 2009 15:01:57 +0000
Subject: [PATCH] Corrections to Keyboard and Mouse combined demos so that all
 modes (normal, interrupt, fully interrupt) work correctly.

---
 Demos/Keyboard/Descriptors.c |  4 ++--
 Demos/Keyboard/Keyboard.c    | 46 +++++++++++++++++-------------------
 Demos/Keyboard/Keyboard.h    |  2 +-
 Demos/Mouse/Mouse.c          | 23 ++++--------------
 Demos/Mouse/Mouse.h          |  2 +-
 5 files changed, 31 insertions(+), 46 deletions(-)

diff --git a/Demos/Keyboard/Descriptors.c b/Demos/Keyboard/Descriptors.c
index 545f9611a..1f383ab08 100644
--- a/Demos/Keyboard/Descriptors.c
+++ b/Demos/Keyboard/Descriptors.c
@@ -163,7 +163,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			EndpointAddress:        (ENDPOINT_DESCRIPTOR_DIR_IN | KEYBOARD_EPNUM),
 			Attributes:             EP_TYPE_INTERRUPT,
 			EndpointSize:           KEYBOARD_EPSIZE,
-			PollingIntervalMS:      0x02
+			PollingIntervalMS:      0x04
 		},
 
 	KeyboardLEDsEndpoint:
@@ -173,7 +173,7 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			EndpointAddress:        (ENDPOINT_DESCRIPTOR_DIR_OUT | KEYBOARD_LEDS_EPNUM),
 			Attributes:             EP_TYPE_INTERRUPT,
 			EndpointSize:           KEYBOARD_EPSIZE,
-			PollingIntervalMS:      0x02
+			PollingIntervalMS:      0x04
 		}
 };
 
diff --git a/Demos/Keyboard/Keyboard.c b/Demos/Keyboard/Keyboard.c
index f506070c0..a26f357a1 100644
--- a/Demos/Keyboard/Keyboard.c
+++ b/Demos/Keyboard/Keyboard.c
@@ -113,6 +113,11 @@ int main(void)
  */
 EVENT_HANDLER(USB_Connect)
 {
+	#if !defined(INTERRUPT_CONTROL_ENDPOINT)
+	/* Start USB management task */
+	Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
+	#endif
+
 	/* Indicate USB enumerating */
 	UpdateStatus(Status_USBEnumerating);
 
@@ -334,14 +339,10 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
 /** Fills the given HID report data structure with the next HID report to send to the host.
  *
  *  \param ReportData  Pointer to a HID report data structure to be filled
- *
- *  \return Boolean true if the new report differs from the last report, false otherwise
  */
-bool CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData)
+void CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData)
 {
-	static uint8_t PrevJoyStatus = 0;
-	uint8_t        JoyStatus_LCL        = Joystick_GetStatus();
-	bool           InputChanged         = false;
+	uint8_t JoyStatus_LCL = Joystick_GetStatus();
 
 	/* Clear the report contents */
 	memset(ReportData, 0, sizeof(USB_KeyboardReport_Data_t));
@@ -358,15 +359,6 @@ bool CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData)
 
 	if (JoyStatus_LCL & JOY_PRESS)
 	  ReportData->KeyCode[0] = 0x08; // E
-	  
-	/* Check if the new report is different to the previous report */
-	InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL);
-
-	/* Save the current joystick status for later comparison */
-	PrevJoyStatus = JoyStatus_LCL;
-
-	/* Return whether the new report is different to the previous report or not */
-	return InputChanged;
 }
 
 /** Processes a received LED report, and updates the board LEDs states to match.
@@ -394,19 +386,25 @@ void ProcessLEDReport(uint8_t LEDReport)
 static inline void SendNextReport(void)
 {
 	USB_KeyboardReport_Data_t KeyboardReportData;
-	bool                      SendReport;
+	bool                      SendReport = true;
 	
 	/* Create the next keyboard report for transmission to the host */
-	SendReport = CreateKeyboardReport(&KeyboardReportData);
+	CreateKeyboardReport(&KeyboardReportData);
 	
-	/* Check if the idle period is set and has elapsed */
-	if (IdleCount && !(IdleMSRemaining))
+	/* Check if the idle period is set */
+	if (IdleCount)
 	{
-		/* Idle period elapsed, indicate that a report must be sent */
-		SendReport = true;
-		
-		/* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
-		IdleMSRemaining = (IdleCount << 2);
+		/* Check if idle period has elapsed */
+		if (!(IdleMSRemaining))
+		{
+			/* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
+			IdleMSRemaining = (IdleCount << 2);
+		}
+		else
+		{
+			/* Idle period not elapsed, indicate that a report must not be sent */
+			SendReport = false;			
+		}
 	}
 	
 	/* Select the Keyboard Report Endpoint */
diff --git a/Demos/Keyboard/Keyboard.h b/Demos/Keyboard/Keyboard.h
index 8b3147c2a..143ffc654 100644
--- a/Demos/Keyboard/Keyboard.h
+++ b/Demos/Keyboard/Keyboard.h
@@ -113,7 +113,7 @@
 		HANDLES_EVENT(USB_UnhandledControlPacket);
 		
 	/* Function Prototypes: */
-		bool CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData);
+		void CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData);
 		void ProcessLEDReport(uint8_t LEDReport);
 		static inline void SendNextReport(void);
 		static inline void ReceiveNextReport(void);
diff --git a/Demos/Mouse/Mouse.c b/Demos/Mouse/Mouse.c
index b61850abe..c4b7f55ad 100644
--- a/Demos/Mouse/Mouse.c
+++ b/Demos/Mouse/Mouse.c
@@ -113,9 +113,11 @@ int main(void)
  */
 EVENT_HANDLER(USB_Connect)
 {
+	#if !defined(INTERRUPT_CONTROL_ENDPOINT)
 	/* Start USB management task */
 	Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
-
+	#endif
+	
 	/* Indicate USB enumerating */
 	UpdateStatus(Status_USBEnumerating);
 
@@ -307,15 +309,10 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
 /** Fills the given HID report data structure with the next HID report to send to the host.
  *
  *  \param ReportData  Pointer to a HID report data structure to be filled
- *
- *  \return Boolean true if the new report differs from the last report, false otherwise
  */
-bool CreateMouseReport(USB_MouseReport_Data_t* ReportData)
+void CreateMouseReport(USB_MouseReport_Data_t* ReportData)
 {
-	static uint8_t PrevJoyStatus = 0;
-	static bool    PrevHWBStatus = false;
-	uint8_t        JoyStatus_LCL = Joystick_GetStatus();
-	bool           InputChanged  = false;
+	uint8_t JoyStatus_LCL = Joystick_GetStatus();
 	
 	/* Clear the report contents */
 	memset(ReportData, 0, sizeof(USB_MouseReport_Data_t));
@@ -335,16 +332,6 @@ bool CreateMouseReport(USB_MouseReport_Data_t* ReportData)
 	  
 	if (HWB_GetStatus())
 	  ReportData->Button |= (1 << 1);
-
-	/* Check if the new report is different to the previous report */
-	InputChanged = ((uint8_t)(PrevJoyStatus ^ JoyStatus_LCL) | (uint8_t)(HWB_GetStatus() ^ PrevHWBStatus));
-
-	/* Save the current joystick and HWB status for later comparison */
-	PrevJoyStatus = JoyStatus_LCL;
-	PrevHWBStatus = HWB_GetStatus();
-
-	/* Return whether the new report is different to the previous report or not */
-	return InputChanged;
 }
 
 /** Sends the next HID report to the host, via the keyboard data endpoint. */
diff --git a/Demos/Mouse/Mouse.h b/Demos/Mouse/Mouse.h
index 8723b7840..c9854a8f9 100644
--- a/Demos/Mouse/Mouse.h
+++ b/Demos/Mouse/Mouse.h
@@ -113,7 +113,7 @@
 		HANDLES_EVENT(USB_UnhandledControlPacket);
 
 	/* Function Prototypes: */
-		bool CreateMouseReport(USB_MouseReport_Data_t* ReportData);
+		void CreateMouseReport(USB_MouseReport_Data_t* ReportData);
 		void UpdateStatus(uint8_t CurrentStatus);
 
 #endif
-- 
GitLab