mattjlewis / diozero

Java Device I/O library that is portable across Single Board Computers and microcontrollers. Tested with Raspberry Pi, Odroid C2, BeagleBone Black, Next Thing CHIP, Asus Tinker Board and Arduinos / Pico. Supports GPIO, I2C, SPI as well as Serial communication. Also known to work with Udoo Quad.
https://www.diozero.com
MIT License
261 stars 59 forks source link

Can't close and re-open MFRC522 #188

Closed Ramblurr closed 7 months ago

Ramblurr commented 8 months ago

I'm building a modular application where the modules can be re/deactivated at runtime.

One of the modules wraps a MFRC522 device. Unlike other devices I'm wrapping (tested: Button, Led) when I open a MFRC522 instance, and properly device.close() it, then try to re-open it again.. I get the following error:

1. Caused by com.diozero.api.DeviceAlreadyOpenedException
   Device Native-GPIO-25 is already in use

GpioDeviceFactoryInterface.java:   81  com.diozero.internal.spi.GpioDeviceFactoryInterface/provisionDigitalOutputDevice
  DigitalOutputDevice.java:  158  com.diozero.api.DigitalOutputDevice/<init>
  DigitalOutputDevice.java:  142  com.diozero.api.DigitalOutputDevice/<init>
  DigitalOutputDevice.java:  129  com.diozero.api.DigitalOutputDevice/<init>
              MFRC522.java:  321  com.diozero.devices.MFRC522/<init>

(the stacktrace looks a little funny because I am calling diozero from Clojure)

EAGrahamJr commented 8 months ago

This seems like a pretty easy fix, so I took a shot at it. If you can check out the particular PR branch and use that to build locally, I think this fixes your issue.

Ramblurr commented 8 months ago

Thanks @EAGrahamJr ! I can confirm this patch fixes my issue. Now the MFRC522 can be opened/closed multiple times in the same process.

mattjlewis commented 8 months ago

Brilliant, thank you. Hope to incorporate in the latest release once I’ve tested the Pi5 changes.