Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locking support when accessing the GPIO pins #10

Closed
wants to merge 3 commits into from
Closed

Add locking support when accessing the GPIO pins #10

wants to merge 3 commits into from

Conversation

ParkaMark
Copy link

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 use change_plug_state from continuing until all actions are complete for the current caller.

Other Changes

  • Modified files are now verified as pep8 compliant.
  • Renamed variables to correspond with naming convention used in technical documentation https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf
  • Renamed change_plug_state function to broadcast.
  • New 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 called CODES.
  • Collapsed the repetitiveness of calling GPIO.setup and GPIO.output to setup and assign the GPIO pin values by using map instead, both when resetting these pins to False and assigning the required pin values when targeting socket groups, using enumerate([D3, D2, D1, D0]) to target indices 0, 1, 2 and 3 of each socket control code bit pattern defined in the CODES dictionary. Used enumerate(ALL[:4]) to actually achieve this, since ALL = [D3, D2, D1, D0, MODSEL, ENABLE]
  • The radio signal's transmit duration can now be set to a custom value (see the comment within the code as to why this may be useful). It can either be set to some default value by changing it directly within the energenie.py source file or can be passed into the switch* functions as an optional duration function parameter.
  • Introduced a 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 this reset function for resetting the GPIO pin values to a zero state both before and after each radio signal transmission invoked by the broadcast function.

@bennuttall
Copy link

Library deprecated. See #15.

@bennuttall bennuttall closed this Jun 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants