]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/usb_hid.py
Merge pull request #941 from makermelissa/main
[Adafruit_Blinka-hackapet.git] / src / usb_hid.py
index 8286b558e7dac500fe7a7a01732821759da680da..791647b288f15dd17c3d99df947b5831daf77934 100644 (file)
@@ -19,7 +19,7 @@ import sys
 
 for module in ["dwc2", "libcomposite"]:
     if Path("/proc/modules").read_text(encoding="utf-8").find(module) == -1:
-        raise Exception(
+        raise Exception(  # pylint: disable=broad-exception-raised
             "%s module not present in your kernel. did you insmod it?" % module
         )
 this = sys.modules[__name__]
@@ -60,7 +60,7 @@ class Device:
         """
         report_id = report_id or self.report_ids[0]
         device_path = self.get_device_path(report_id)
-        with open(device_path, "rb+", encoding="utf-8") as fd:
+        with open(device_path, "rb+") as fd:
             if report_id > 0:
                 report = bytearray(report_id.to_bytes(1, "big")) + report
             fd.write(report)
@@ -82,7 +82,7 @@ class Device:
         Return `None` if nothing received.
         """
         device_path = self.get_device_path(report_id or self.report_ids[0])
-        with open(device_path, "rb+", encoding="utf-8") as fd:
+        with open(device_path, "rb+") as fd:
             os.set_blocking(fd.fileno(), False)
             report = fd.read(self.out_report_lengths[0])
             if report is not None:
@@ -261,7 +261,7 @@ Device.MOUSE = Device(
     ),
     usage_page=0x1,
     usage=0x02,
-    report_ids=[2],
+    report_ids=[0x02],
     in_report_lengths=[4],
     out_report_lengths=[0],
 )
@@ -450,7 +450,7 @@ Device.BOOT_MOUSE = Device(
     ),
     usage_page=0x1,
     usage=0x02,
-    report_ids=[1],
+    report_ids=[0],
     in_report_lengths=[4],
     out_report_lengths=[0],
 )
@@ -641,8 +641,8 @@ def enable(requested_devices: Sequence[Device], boot_device: int = 0) -> None:
     #     $ echo 120 > configs/c.1/MaxPower
     #     """
 
-    for i, device in enumerate(requested_devices):
-        config_root = "%s/configs/device.%s" % (this.gadget_root, i + 1)
+    for device in requested_devices:
+        config_root = "%s/configs/device.1" % this.gadget_root
         Path("%s/" % config_root).mkdir(parents=True, exist_ok=True)
         Path("%s/strings/0x409" % config_root).mkdir(parents=True, exist_ok=True)
         Path("%s/strings/0x409/configuration" % config_root).write_text(
@@ -702,7 +702,12 @@ def enable(requested_devices: Sequence[Device], boot_device: int = 0) -> None:
             # e.g.::
             #
             #     $ ln -s functions/ncm.usb0 configs/c.1  """
-            Path("%s/hid.usb%s" % (config_root, report_id)).symlink_to(function_root)
+            try:
+                Path("%s/hid.usb%s" % (config_root, report_id)).symlink_to(
+                    function_root
+                )
+            except FileNotFoundError:
+                pass
     # """ 5. Enabling the gadget
     # ----------------------
     # Such a gadget must be finally enabled so that the USB host can enumerate it.