Toggle output port without knowing prior state?

I’m trying to figure out how I can toggle an output port’s state without knowing what the state is prior to the toggle. For instance, I have a method that will run, instantiate an OutputPort and then I want to toggle the output pins value. But, there’s no way to know what state it’s currently in.

Is this possible? Will I need to instead create a global variable to hold all of my pin states, read the variable, and then toggle the outputport appropriately?


You can read the current state and use not operator to toggle


LED is an output port. Check the Begineers ebook for a complete example.

Thanks Rajesh! I wasn’t certain if the Read() method would return the current state of the output, or if it would just work when the port was set to input.

In that same vein, when I instantiate a new OutputPort, I have to give it a “initialState” property. Won’t this overwrite the current state? But, since the object isn’t instantiated, I can’t read its current state.

Until you instantiate the OutputPort it has no current state. So, the initialState is the current/first state of the port. Are you perhaps wanting to set it as an InputPort first to check for some other signal?

I have a method that when it runs, will toggle the output port direction. The OutputPort is instantiated when the method runs. Since the initialState has to be set when instantiated, it will overwrite the output state from the previous execution of the method.

Short of making the OutputPort a public global variable, is this possible?

I would recommend that you make all your ports global or your classes that instantiate them singletons… However, to do what you’re talking about you will first have to dispose of the first instantiation before you can reinstantiate that port again. So, there still is no previous state… :slight_smile: