Magstripe Project: Ensure that empty tracks still print out a newline seperator so...
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 19 Jul 2009 07:30:37 +0000 (07:30 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 19 Jul 2009 07:30:37 +0000 (07:30 +0000)
Updates to PrinterHost demo to include some PCL test data plus fixes to the GetDeviceID routine.

Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c
Demos/Host/Incomplete/PrinterHost/PrinterHost.c
Demos/Host/Incomplete/PrinterHost/PrinterHost.h
Demos/Host/Incomplete/PrinterHost/makefile
Projects/Magstripe/Magstripe.c
Projects/Magstripe/Magstripe.h

index 0312891..46e534d 100644 (file)
@@ -32,7 +32,8 @@
 \r
 uint8_t Printer_GetDeviceID(Device_ID_String_t* DeviceIDString)\r
 {\r
-       uint8_t ErrorCode = HOST_SENDCONTROL_Successful;\r
+       uint8_t  ErrorCode = HOST_SENDCONTROL_Successful;\r
+       uint16_t DeviceIDStringLength;\r
 \r
        USB_ControlRequest = (USB_Request_Header_t)\r
                {\r
@@ -40,19 +41,24 @@ uint8_t Printer_GetDeviceID(Device_ID_String_t* DeviceIDString)
                        bRequest:      GET_DEVICE_ID,\r
                        wValue:        0,\r
                        wIndex:        0,\r
-                       wLength:       sizeof(DeviceIDString),\r
+                       wLength:       sizeof(DeviceIDString->Length),\r
                };\r
 \r
-       if ((ErrorCode == USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)\r
+       if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)\r
          return ErrorCode;\r
-         \r
-       DeviceIDString->Length = SwapEndian_16(DeviceIDString->Length);\r
        \r
+       DeviceIDStringLength = SwapEndian_16(DeviceIDString->Length);\r
+\r
        /* Protect against overflow for the null terminator if the string length is equal to or larger than the buffer */\r
-       if (DeviceIDString->Length >= sizeof(DeviceIDString->String))\r
-         DeviceIDString->Length = sizeof(DeviceIDString->String) - 1;\r
+       if (DeviceIDStringLength >= sizeof(DeviceIDString->String))\r
+         DeviceIDStringLength = sizeof(DeviceIDString->String) - 1;\r
+\r
+       USB_ControlRequest.wLength = DeviceIDStringLength;\r
+       \r
+       if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)\r
+         return ErrorCode;\r
        \r
-       DeviceIDString->String[DeviceIDString->Length] = 0x00;\r
+       DeviceIDString->String[DeviceIDStringLength] = 0x00;\r
        \r
        return HOST_SENDCONTROL_Successful;\r
 }\r
index 7b98542..5cdd063 100644 (file)
@@ -130,7 +130,7 @@ void USB_Printer_Host(void)
                                printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
                                \r
                                /* Indicate error via status LEDs */\r
-                               LEDs_SetAllLEDs(LEDS_LED1);\r
+                               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Wait until USB device disconnected */\r
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
@@ -144,7 +144,7 @@ void USB_Printer_Host(void)
                                printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
 \r
                                /* Indicate error via status LEDs */\r
-                               LEDs_SetAllLEDs(LEDS_LED1);\r
+                               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Wait until USB device disconnected */\r
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
@@ -154,23 +154,18 @@ void USB_Printer_Host(void)
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("Printer Enumerated.\r\n"));\r
-                       \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
-                       /* Indicate device busy via the status LEDs */\r
-                       LEDs_SetAllLEDs(LEDS_LED3 | LEDS_LED4);\r
-               \r
                        printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol);\r
                \r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
                        Device_ID_String_t DeviceIDString;\r
-                       if (Printer_GetDeviceID(&DeviceIDString) != HOST_SENDCONTROL_Successful)\r
+                       if ((ErrorCode = Printer_GetDeviceID(&DeviceIDString)) != HOST_SENDCONTROL_Successful)\r
                        {\r
+                               puts_P(PSTR("Control Error (Get DeviceID).\r\n"));\r
+                               printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+\r
                                /* Indicate error via status LEDs */\r
-                               LEDs_SetAllLEDs(LEDS_LED1);\r
+                               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Wait until USB device disconnected */\r
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
@@ -178,9 +173,42 @@ void USB_Printer_Host(void)
                        }\r
 \r
                        printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString.String);\r
+\r
+                       puts_P(PSTR("Printer Enumerated.\r\n"));\r
+                                       \r
+                       USB_HostState = HOST_STATE_Ready;\r
+                       break;\r
+               case HOST_STATE_Ready:\r
+                       /* Indicate device busy via the status LEDs */\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
+               \r
+            //--------------------------------------------------------------\r
+                       #define TEST_TEXT_PAGE "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X"\r
+\r
+                       Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE);\r
+            Pipe_Unfreeze();\r
+                       \r
+                       puts_P(PSTR("Waiting for Printer to Become Ready...\r\n"));\r
+                       \r
+                       while (!(Pipe_IsReadWriteAllowed()));\r
+\r
+                       uint8_t strSize = sizeof(TEST_TEXT_PAGE)-1;\r
+\r
+                       printf_P(PSTR("Printer Write Allowed, sending complete page (%d bytes)...\r\n"), strSize);\r
+                               \r
+                       Pipe_Write_Stream_LE(TEST_TEXT_PAGE, strSize);\r
+            Pipe_ClearOUT();\r
+\r
+                       puts_P(PSTR("Page sent to printer.\r\n"));\r
+\r
+                       while (!(Pipe_IsReadWriteAllowed()));\r
+            Pipe_Freeze();                             \r
+\r
+                       puts_P(PSTR("Pipe Frozen.\r\n"));\r
+            //--------------------------------------------------------------\r
                \r
                        /* Indicate device no longer busy */\r
-                       LEDs_SetAllLEDs(LEDS_LED4);\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 \r
                        USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                        break;\r
index 191cd7f..46427b3 100644 (file)
@@ -60,6 +60,9 @@
                /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */\r
                #define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)\r
        \r
+               /** LED mask for the library LED driver, to indicate that the USB interface is busy. */\r
+               #define LEDMASK_USB_BUSY         (LEDS_LED2)\r
+\r
        /* External Variables: */\r
                extern uint8_t PrinterProtocol;\r
        \r
index a56d9f2..ab2eaab 100644 (file)
@@ -60,7 +60,7 @@
 \r
 \r
 # MCU name\r
-MCU = at90usb1287\r
+MCU = at90usb647\r
 \r
 \r
 # Target board (see library "Board Types" documentation, USER or blank for projects not requiring\r
index 1151735..087e510 100644 (file)
 /** Bit buffers to hold the read bits for each of the three magnetic card tracks before they are transmitted\r
  *  to the host as keyboard presses.\r
  */\r
-BitBuffer_t TrackDataBuffers[3];\r
+BitBuffer_t TrackDataBuffers[TOTAL_TRACKS];\r
+\r
+/** Pointer to the current track buffer being sent to the host. */\r
+BitBuffer_t* CurrentTrackBuffer = &TrackDataBuffers[TOTAL_TRACKS];\r
 \r
 /** LUFA HID Class driver interface configuration and state information. This structure is\r
  *  passed to all HID Class driver functions, so that multiple instances of the same class\r
@@ -71,7 +74,7 @@ int main(void)
 {\r
        SetupHardware();\r
        \r
-       for (uint8_t Buffer = 0; Buffer < 3; Buffer++)\r
+       for (uint8_t Buffer = 0; Buffer < TOTAL_TRACKS; Buffer++)\r
          BitBuffer_Init(&TrackDataBuffers[Buffer]);\r
 \r
        for (;;)\r
@@ -124,13 +127,13 @@ void ReadMagstripeData(void)
 \r
        while (Magstripe_LCL & MAG_CARDPRESENT)\r
        {\r
-               for (uint8_t Track = 0; Track < 3; Track++)\r
+               for (uint8_t Track = 0; Track < TOTAL_TRACKS; Track++)\r
                {\r
                        bool DataPinLevel      = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);\r
                        bool ClockPinLevel     = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);\r
                        bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);\r
                \r
-                       /* Sample on rising clock edges */\r
+                       /* Sample data on rising clock edges from the card reader */\r
                        if (ClockPinLevel && ClockLevelChanged)\r
                          BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);\r
                }\r
@@ -138,6 +141,8 @@ void ReadMagstripeData(void)
                Magstripe_Prev = Magstripe_LCL;\r
                Magstripe_LCL  = Magstripe_GetStatus();\r
        }\r
+       \r
+       CurrentTrackBuffer = &TrackDataBuffers[0];\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
@@ -169,41 +174,28 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  */\r
 uint16_t CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, void* ReportData)\r
 {\r
+       USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;\r
+\r
        static bool IsKeyReleaseReport;\r
-       static bool IsNewlineReport;\r
 \r
-       USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;\r
-       BitBuffer_t*               Buffer         = NULL;\r
-       \r
        /* Key reports must be interleaved with key release reports, or repeated keys will be ignored */\r
-       IsKeyReleaseReport = !IsKeyReleaseReport;       \r
+       IsKeyReleaseReport = !IsKeyReleaseReport;\r
 \r
-       if (IsKeyReleaseReport)\r
+       if ((IsKeyReleaseReport) || (CurrentTrackBuffer == &TrackDataBuffers[TOTAL_TRACKS]))\r
        {\r
+               /* No more data to send, or key release report between key presses */\r
                KeyboardReport->KeyCode = KEY_NONE;\r
        }\r
-       else if (IsNewlineReport)\r
+       else if (!(CurrentTrackBuffer->Elements))\r
        {\r
-               IsNewlineReport = false;\r
+               /* End of current track, send an enter press and change to the next track's buffer */\r
                KeyboardReport->KeyCode = KEY_ENTER;\r
+               CurrentTrackBuffer++;\r
        }\r
        else\r
        {\r
-               /* Read out tracks in ascending order - when each track buffer is empty, progress to next buffer */\r
-               if (TrackDataBuffers[0].Elements)\r
-                 Buffer = &TrackDataBuffers[0];\r
-               else if (TrackDataBuffers[1].Elements)\r
-                 Buffer = &TrackDataBuffers[1];                        \r
-               else if (TrackDataBuffers[2].Elements)\r
-                 Buffer = &TrackDataBuffers[2];\r
-               else\r
-                 return 0;\r
-\r
-               KeyboardReport->KeyCode = BitBuffer_GetNextBit(Buffer) ? KEY_1 : KEY_0;\r
-               \r
-               /* If current track buffer now empty, next report must be a newline to seperate track data */\r
-               if (!(Buffer->Elements))\r
-                 IsNewlineReport = true;\r
+               /* Still data in the current track; convert next bit to a 1 or 0 keypress */\r
+               KeyboardReport->KeyCode = BitBuffer_GetNextBit(CurrentTrackBuffer) ? KEY_1 : KEY_0;\r
        }\r
        \r
        return sizeof(USB_KeyboardReport_Data_t);\r
index 93e593e..4e0aac9 100644 (file)
@@ -51,6 +51,9 @@
                #include <LUFA/Drivers/USB/Class/HID.h>\r
 \r
        /* Macros: */\r
+               /** Total number of tracks which can be read from the card, between 1 and 3. */\r
+               #define TOTAL_TRACKS       3\r
+       \r
                /** HID keyboard keycode to indicate that no is currently pressed. */\r
                #define KEY_NONE           0\r
        \r