]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #540 from ezio-melotti/close-mcp2221
authorCarter Nelson <caternuson@gmail.com>
Fri, 11 Mar 2022 02:12:02 +0000 (18:12 -0800)
committerGitHub <noreply@github.com>
Fri, 11 Mar 2022 02:12:02 +0000 (18:12 -0800)
Close the mcp2221 device before exit.

1  2 
src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py

index 7b62c3f5c4931a4c9e0eb47bd0714fa966f0f16f,665be7f66faa4c9f4232452deb07b26bbe78ab9c..5ff027ab2803df4957508ee388cef9016c1f80e8
@@@ -1,10 -1,9 +1,12 @@@
 +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 +#
 +# SPDX-License-Identifier: MIT
  """Chip Definition for MCP2221"""
  
  import os
  import time
+ import atexit
  import hid
  
  # Here if you need it
@@@ -54,6 -53,8 +56,8 @@@ class MCP2221
      def __init__(self):
          self._hid = hid.device()
          self._hid.open(MCP2221.VID, MCP2221.PID)
+         # make sure the device gets closed before exit
+         atexit.register(self.close)
          if MCP2221_RESET_DELAY >= 0:
              self._reset()
          self._gp_config = [0x07] * 4  # "don't care" initial value
              self.gp_set_mode(pin, self.GP_GPIO)  # set to GPIO mode
              self.gpio_set_direction(pin, 1)  # set to INPUT
  
+     def close(self):
+         """Close the hid device. Does nothing if the device is not open."""
+         self._hid.close()
+     def __del__(self):
+         # try to close the device before destroying the instance
+         self.close()
      def _hid_xfer(self, report, response=True):
          """Perform HID Transfer"""
          # first byte is report ID, which =0 for MCP2221