# Card Reader Fix - Multi-USB Device Support ## Problem Description When a USB touchscreen was connected to the Raspberry Pi, the card reader authentication was not working. The system reported "no authentication was received" even though the card reader was physically connected on a different USB port. ### Root Cause The original `find_card_reader()` function used overly broad matching criteria: 1. It would select the **first** device with "keyboard" in its name 2. USB touchscreens often register as HID keyboard devices (for touch input) 3. The touchscreen would be detected first, blocking the actual card reader 4. No exclusion logic existed to filter out touch devices ## Solution The fix implements a **priority-based device selection** with **exclusion filters**: ### 1. Device Exclusion List Devices containing these keywords are now skipped: - `touch`, `touchscreen` - `mouse`, `mice` - `trackpad`, `touchpad` - `pen`, `stylus` - `video`, `button`, `lid` ### 2. Three-Priority Device Search **Priority 1: Explicit Card Readers** - Devices with "card", "reader", "rfid", or "hid" in their name - Must have keyboard capabilities (EV_KEY) - Excludes any device matching exclusion keywords **Priority 2: USB Keyboards** - Devices with both "usb" AND "keyboard" in their name - Card readers typically appear as "USB Keyboard" or similar - Excludes touch devices and other non-card peripherals **Priority 3: Fallback to Any Keyboard** - Any keyboard device not in the exclusion list - Used only if no card reader or USB keyboard is found ### 3. Enhanced Logging The system now logs: - All detected input devices at startup - Which devices are being skipped and why - Which device is ultimately selected as the card reader ## Testing ### Using the Test Script Run the enhanced test script to identify your card reader: ```bash cd /home/pi/Desktop/Kiwy-Signage/working_files python3 test_card_reader.py ``` The script will: 1. List all input devices with helpful indicators: - `** LIKELY CARD READER **` - devices with "card" or "reader" in name - `(Excluded: ...)` - devices that will be skipped - `(USB Keyboard - could be card reader)` - potential card readers 2. Auto-detect the card reader using the same logic as the main app 3. Allow manual selection by device number if auto-detection is wrong ### Example Output ``` === Available Input Devices === [0] /dev/input/event0 Name: USB Touchscreen Controller Phys: usb-0000:01:00.0-1.1/input0 Type: Keyboard/HID Input Device (Excluded: appears to be touch/mouse/other non-card device) [1] /dev/input/event1 Name: HID 08ff:0009 Phys: usb-0000:01:00.0-1.2/input0 Type: Keyboard/HID Input Device ** LIKELY CARD READER ** [2] /dev/input/event2 Name: Logitech USB Keyboard Phys: usb-0000:01:00.0-1.3/input0 Type: Keyboard/HID Input Device (USB Keyboard - could be card reader) ``` ### Verifying the Fix 1. **Check Logs**: When the main app starts, check the logs for device detection: ```bash tail -f /path/to/logfile ``` Look for messages like: ``` CardReader: Scanning input devices... CardReader: Skipping excluded device: USB Touchscreen Controller CardReader: Found card reader: HID 08ff:0009 at /dev/input/event1 ``` 2. **Test Card Swipe**: - Start the signage player - Click the edit button (pencil icon) - Swipe a card - Should successfully authenticate 3. **Multiple USB Devices**: Test with various USB configurations: - Touchscreen + card reader - Mouse + keyboard + card reader - Multiple USB hubs ## Configuration ### If Auto-Detection Fails If the automatic detection still selects the wrong device, you can: 1. **Check device names**: Run `test_card_reader.py` to see all devices 2. **Identify your card reader**: Note the exact name of your card reader 3. **Add custom exclusions**: If needed, add more keywords to the exclusion list 4. **Manual override**: Modify the priority logic to match your specific hardware ### Permissions Ensure the user running the app has permission to access input devices: ```bash # Add user to input group sudo usermod -a -G input $USER # Logout and login again for changes to take effect ``` ## Files Modified 1. **src/main.py** - Updated `CardReader.find_card_reader()` method - Added exclusion keyword list - Implemented priority-based search - Enhanced logging 2. **working_files/test_card_reader.py** - Updated `list_input_devices()` to show device classifications - Updated `test_card_reader()` to use same logic as main app - Added visual indicators for device types ## Compatibility This fix is backward compatible: - Works with single-device setups (no touchscreen) - Works with multiple USB devices - Fallback behavior unchanged for systems without card readers - No changes to card data format or server communication ## Future Enhancements Potential improvements for specific use cases: 1. **Configuration file**: Allow specifying device path or name pattern 2. **Device caching**: Remember the working device path to avoid re-scanning 3. **Hot-plug support**: Detect when card reader is plugged in after app starts 4. **Multi-reader support**: Support for multiple card readers simultaneously