Fix demos based on the device mode HID class driver, as well as the driver itself...
[pub/USBasp.git] / Projects / Benito / Benito.c
index 720a224..26c5664 100644 (file)
@@ -45,6 +45,9 @@ volatile uint8_t TxPulseMSRemaining    = 0;
 /** Counter for the number of milliseconds remaining for the RX activity LED pulse being generated. */\r
 volatile uint8_t RxPulseMSRemaining    = 0;\r
 \r
+/** Counter for the number of milliseconds remaining for the enumeration LED ping-pong being generated. */\r
+volatile uint8_t PingPongMSRemaining   = 0;\r
+\r
 /** LUFA CDC Class driver interface configuration and state information. This structure is\r
  *  passed to all CDC Class driver functions, so that multiple instances of the same class\r
  *  within a device can be differentiated from one another.\r
@@ -64,11 +67,6 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
                                .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
                                .NotificationEndpointSize   = CDC_NOTIFICATION_EPSIZE,\r
                        },\r
-               \r
-               .State =\r
-                       {\r
-                               // Leave all state values to their defaults\r
-                       }\r
        };\r
 \r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
@@ -78,12 +76,10 @@ int main(void)
 {\r
        SetupHardware();\r
 \r
-       LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
-\r
        for (;;)\r
        {\r
                /* Echo bytes from the host to the target via the hardware USART */\r
-               if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))\r
+               if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) > 0)\r
                {\r
                        Serial_TxByte(CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));\r
 \r
@@ -103,10 +99,20 @@ int main(void)
                /* Check if the millisecond timer has elapsed */\r
                if (TIFR0 & (1 << OCF0A))\r
                {\r
+                       /* Check if the LEDs should be ping-ponging (during enumeration) */\r
+                       if (PingPongMSRemaining && !(--PingPongMSRemaining))\r
+                       {\r
+                               LEDs_ToggleLEDs(LEDMASK_BUSY);\r
+                               PingPongMSRemaining = PING_PONG_LED_PULSE_MS;\r
+                       }\r
+               \r
                        /* Check if the reset pulse period has elapsed, if so tristate the target reset line */\r
                        if (ResetPulseMSRemaining && !(--ResetPulseMSRemaining))\r
-                         AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;\r
-\r
+                       {\r
+                               LEDs_TurnOffLEDs(LEDMASK_BUSY);\r
+                               AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;\r
+                       }\r
+                       \r
                        /* Turn off TX LED(s) once the TX pulse period has elapsed */\r
                        if (TxPulseMSRemaining && !(--TxPulseMSRemaining))\r
                          LEDs_TurnOffLEDs(LEDMASK_TX);\r
@@ -151,22 +157,25 @@ void SetupHardware(void)
 /** Event handler for the library USB Connection event. */\r
 void EVENT_USB_Connect(void)\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
+       PingPongMSRemaining = PING_PONG_LED_PULSE_MS;\r
+       LEDs_SetAllLEDs(LEDMASK_TX);\r
 }\r
 \r
 /** Event handler for the library USB Disconnection event. */\r
 void EVENT_USB_Disconnect(void)\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
+       PingPongMSRemaining = 0;\r
+       LEDs_TurnOffLEDs(LEDMASK_BUSY);\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
 void EVENT_USB_ConfigurationChanged(void)\r
 {\r
-       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+       PingPongMSRemaining = 0;\r
+       LEDs_TurnOffLEDs(LEDMASK_BUSY);\r
 \r
        if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
-         LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+         LEDs_TurnOnLEDs(LEDMASK_ERROR);\r
 }\r
 \r
 /** Event handler for the library USB Unhandled Control Packet event. */\r
@@ -224,6 +233,8 @@ void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const C
        /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */\r
        if (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)\r
        {\r
+               LEDs_TurnOnLEDs(LEDMASK_BUSY);\r
+       \r
                AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;\r
                ResetPulseMSRemaining = AVR_RESET_PULSE_MS;\r
        }\r