From: Carter Nelson Date: Fri, 11 Mar 2022 02:12:02 +0000 (-0800) Subject: Merge pull request #540 from ezio-melotti/close-mcp2221 X-Git-Tag: 7.1.2~1 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/1b0742ea4dd827c3f9217dab1dca35fbec100dce?ds=inline;hp=-c Merge pull request #540 from ezio-melotti/close-mcp2221 Close the mcp2221 device before exit. --- 1b0742ea4dd827c3f9217dab1dca35fbec100dce diff --combined src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py index 7b62c3f,665be7f..5ff027a --- a/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py +++ b/src/adafruit_blinka/microcontroller/mcp2221/mcp2221.py @@@ -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 @@@ -61,6 -62,14 +65,14 @@@ 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