Add locking support when accessing the GPIO pins #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have closed pull requests #8 and #9 in favour of this one as I have since realised that my initial locking attempts in those were not 100% correct. This one now locks out the GPIO pins correctly; the previous versions would have still allowed the pin settings to be clobbered when other Python scripts imported the Energenie module without even calling
change_plug_state
.Full description and reasoning behind this change
It may be the case that varying different Python scripts, targeting different socket groups, could be running on a Pi system at different times, or more likely, at near or precisely the same times. The radio controller can only target one socket group at a time, be that 0 (all sockets) or individual groups 1 to 4, so it is essential that during the GPIO pin setup period, followed by activation of the transmitter to send the radio signal, then de-activation, that no other Python scripts can clobber these actions. Effectively, the
change_plug_state
function should be exclusive to the caller until complete, preventing any other caller that wants to usechange_plug_state
from continuing until all actions are complete for the current caller.Other Changes
change_plug_state
function tobroadcast
.switch
function which takes a boolean value to switch ON (True
) or OFF (False
) the sockets. This involved re-working the socket group control codes into a 2 key (True
/False
) dictionary calledCODES
.GPIO.setup
andGPIO.output
to setup and assign the GPIO pin values by usingmap
instead, both when resetting these pins toFalse
and assigning the required pin values when targeting socket groups, usingenumerate([D3, D2, D1, D0])
to target indices 0, 1, 2 and 3 of each socket control code bit pattern defined in theCODES
dictionary. Usedenumerate(ALL[:4])
to actually achieve this, sinceALL = [D3, D2, D1, D0, MODSEL, ENABLE]
energenie.py
source file or can be passed into theswitch*
functions as an optional duration function parameter.reset
function which returns all GPIO pins to a false/low/zero state based on http://www.domoticz.com/wiki/Pi-Mote which states that "All 4 bits held zero (address and on/off) and held in that state for a period is a reset command to the encoder and forces it into a known state." I have added this because I have been experiencing issues where sometimes sockets do not switch states, even after several transmit attempts. I do not know if the transmitter is at fault or the sockets. In any case, I have introduced thisreset
function for resetting the GPIO pin values to a zero state both before and after each radio signal transmission invoked by thebroadcast
function.