From b9c0427c36f8734c021e5b18d56f920c5b027f12 Mon Sep 17 00:00:00 2001 From: Mark Hedges Date: Sat, 6 Jun 2015 03:33:00 +0100 Subject: [PATCH 1/3] v1.1.0 --- energenie/__init__.py | 4 +- energenie/energenie.py | 109 +++++++++++++++++++++++++---------------- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/energenie/__init__.py b/energenie/__init__.py index 0bf53f6..88ff1b7 100644 --- a/energenie/__init__.py +++ b/energenie/__init__.py @@ -1,5 +1,5 @@ from __future__ import absolute_import -from .energenie import switch_on, switch_off +from .energenie import switch, switch_on, switch_off -__version__ = '1.0.1' +__version__ = '1.1.0' diff --git a/energenie/energenie.py b/energenie/energenie.py index 8eb3a0d..8e26e65 100644 --- a/energenie/energenie.py +++ b/energenie/energenie.py @@ -1,58 +1,83 @@ import RPi.GPIO as GPIO from time import sleep +from fcntl import flock, LOCK_EX -# The GPIO pins for the Energenie module -BIT1 = 17 -BIT2 = 22 -BIT3 = 23 -BIT4 = 27 -ON_OFF_KEY = 24 +# Set default transmission duration of radio signal in seconds. +# Setting to None or a value less than 0.25 will result in the code +# using the minimum sane value of 0.25 seconds which is the shortest +# possible time that one control frame can be broadcast in. +# +# Increasing this value may be beneficial if noise is anticipated +# in the radio spectrum or a large distance between the transmitter +# and sockets is expected because the transmitter will remain in a +# constant loop broadcasting the same control frame over and over +# again for the duration it is enabled. This can increase the chances +# of sockets receiving a valid recognisable control frame, which has +# not been corrupted, that can subsequently be acted upon. +# +DURATION = None + + +# Define GPIO pins for the Energenie module. +# +D0 = 17 +D1 = 22 +D2 = 23 +D3 = 27 +MODSEL = 24 ENABLE = 25 +# Assign all of the above into a master list. Place D3, D2, D1 and D0 +# first, in that order, for use with enumeration later on. +# +ALL = [D3, D2, D1, D0, MODSEL, ENABLE] + + +# Codes for switching ON (True) and OFF (False) the sockets. +# +# Group: ALL 1 2 3 4 +# D0-D3: 3210 3210 3210 3210 3210 +# +CODES = { + True: ['1011', '1111', '1110', '1101', '1100'], # ON + False: ['0011', '0111', '0110', '0101', '0100'], # OFF +} + + GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) -GPIO.setup(BIT1, GPIO.OUT) -GPIO.setup(BIT2, GPIO.OUT) -GPIO.setup(BIT3, GPIO.OUT) -GPIO.setup(BIT4, GPIO.OUT) - -GPIO.setup(ON_OFF_KEY, GPIO.OUT) -GPIO.setup(ENABLE, GPIO.OUT) - -GPIO.output(ON_OFF_KEY, False) -GPIO.output(ENABLE, False) - -GPIO.output(BIT1, False) -GPIO.output(BIT2, False) -GPIO.output(BIT3, False) -GPIO.output(BIT4, False) - -# Codes for switching on and off the sockets -# all 1 2 3 4 -ON = ['1011', '1111', '1110', '1101', '1100'] -OFF = ['0011', '0111', '0110', '0101', '0100'] - - -def change_plug_state(socket, on_or_off): - state = on_or_off[socket][3] == '1' - GPIO.output(BIT1, state) - state = on_or_off[socket][2] == '1' - GPIO.output(BIT2, state) - state = on_or_off[socket][1] == '1' - GPIO.output(BIT3, state) - state = on_or_off[socket][0] == '1' - GPIO.output(BIT4, state) + +def reset(): + map(lambda p: GPIO.setup(p, GPIO.OUT), ALL) + map(lambda p: GPIO.output(p, False), ALL) + sleep(0.1) + + +def broadcast(state, socket, duration): + duration = max(duration, 0.25) + lock = open('/run/GPIO.lock', 'a') + flock(lock, LOCK_EX) + reset() + map(lambda (i, p): GPIO.output(p, CODES[state][socket][i] == '1'), + enumerate(ALL[:4]) + ) sleep(0.1) GPIO.output(ENABLE, True) - sleep(0.25) + sleep(duration) GPIO.output(ENABLE, False) + reset() + lock.close() + + +def switch(state, socket=0, duration=DURATION): + broadcast(bool(state), socket, duration) -def switch_on(socket=0): - change_plug_state(socket, ON) +def switch_on(socket=0, duration=DURATION): + switch(True, socket, duration) -def switch_off(socket=0): - change_plug_state(socket, OFF) +def switch_off(socket=0, duration=DURATION): + switch(False, socket, duration) From 9a3cf0528a4590d86bf9e99c8e396af9c2e9c45b Mon Sep 17 00:00:00 2001 From: Mark Hedges Date: Sat, 9 Apr 2016 19:06:32 +0100 Subject: [PATCH 2/3] v1.1.1 --- energenie/energenie.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/energenie/energenie.py b/energenie/energenie.py index 8e26e65..e28c7a5 100644 --- a/energenie/energenie.py +++ b/energenie/energenie.py @@ -60,9 +60,13 @@ def broadcast(state, socket, duration): lock = open('/run/GPIO.lock', 'a') flock(lock, LOCK_EX) reset() - map(lambda (i, p): GPIO.output(p, CODES[state][socket][i] == '1'), - enumerate(ALL[:4]) - ) + try: + map(lambda (i, p): GPIO.output(p, CODES[state][socket][i] == '1'), + enumerate(ALL[:4]) + ) + except: + lock.close() + return sleep(0.1) GPIO.output(ENABLE, True) sleep(duration) From b32f99317681a942265dc2842fe02e5ee4fcd981 Mon Sep 17 00:00:00 2001 From: Mark Hedges Date: Mon, 11 Apr 2016 09:12:14 +0100 Subject: [PATCH 3/3] v1.1.1 --- energenie/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/energenie/__init__.py b/energenie/__init__.py index 88ff1b7..5fdecb9 100644 --- a/energenie/__init__.py +++ b/energenie/__init__.py @@ -2,4 +2,4 @@ from .energenie import switch, switch_on, switch_off -__version__ = '1.1.0' +__version__ = '1.1.1'