Finding out if pin is in use

I’m working on a driver for a dual motor control board. This board has an enable line, I would like to be able to share the enable line between multiple driver board. There is no problem electrically with that but there is a configuration problem.

It would be best to be able to pass the pin used for the enable signal and have the driver figure out if that pin has already been configured as an OutputPort. If the pin is already an output port than the driver instance would only need to keep a reference to the OutputPort object.

So far I have not been able to find a way to do this.

I could always kludge it by creating a static ArrayList of the pins passed for use as Enable and the corresponding objects but that seems like poor style. I settled on creating a second constructor that takes an OuputPort object instead of just the Cpu.Pin. That is not ideal though as it requires the uses of the driver to know how the OutputPort should be configured (high or low).

If you try to “reuse” pin with another new OutputPort instance. Constructor of that new instance will throw an exception. You can can catch that exception and that would be you indication that output port for that pins has been already created.

yeah, but I still can’t get a reference to the existing Output port.

I’m going to add a static method in the driver class to generate the OutputPort. That way the user of the driver would only need to call the static method to generate or get a reference to the shared OutputPort object.

I would think it is a bad thing if the driver didn’t have exclusive control over the pin. Wouldn’t it cause conflicts of some kind?

I don’t know if there is a better way but my approach to this type of problem is to create a static class to be the HAL for the project. In there I have logical names for what the pins do and physical mappings in an Init(). That way multiple classes can share the same resources but in a controlled way.

I usually also use this same class (I call Device) to hold state variables. Also a simple way for everyone to know what can be used and what will be off limits at the time.