diff --git a/src/cleanup.py b/src/cleanup.py new file mode 100644 index 0000000..2c430e6 --- /dev/null +++ b/src/cleanup.py @@ -0,0 +1,18 @@ +# cleanup.py 05/04/2016 D.J.Whale +# +# Put all used GPIO pins into an input state +# Useful to recover from a crash + +import RPi.GPIO as GPIO +GPIO.setmode(GPIO.BCM) + +GPIO.setup(27, GPIO.IN) # Green LED +GPIO.setup(22, GPIO.IN) # Red LED +GPIO.setup(7, GPIO.IN) # CS +GPIO.setup(8, GPIO.IN) # CS +GPIO.setup(11, GPIO.IN) # SCLK +GPIO.setup(10, GPIO.IN) # MOSI +GPIO.setup(9, GPIO.IN) # MISO +GPIO.setup(25, GPIO.IN) # RESET + +GPIO.cleanup() diff --git a/src/energenie/radio.py b/src/energenie/radio.py index 2e8c75f..db3a6ba 100644 --- a/src/energenie/radio.py +++ b/src/energenie/radio.py @@ -414,10 +414,12 @@ def transmit(payload): """Transmit a single payload using the present modulation scheme""" + spi.start_transaction() if not modulation_fsk: HRF_send_OOK_payload(payload) else: HRF_send_payload(payload) + spi.end_transaction() def receiver(fsk=None, ook=None): @@ -433,12 +435,18 @@ def isReceiveWaiting(): """Check to see if a payload is waiting in the receive buffer""" - return HRF_check_payload() + spi.start_transaction() + waiting = HRF_check_payload() + spi.end_transaction() + return waiting def receive(): """Receive a single payload from the buffer using the present modulation scheme""" - return HRF_receive_payload() + spi.start_transaction() + payload = HRF_receive_payload() + spi.end_transaction() + return payload def finished(): diff --git a/src/energenie/spi.py b/src/energenie/spi.py index b4ae190..74f3a10 100644 --- a/src/energenie/spi.py +++ b/src/energenie/spi.py @@ -10,6 +10,10 @@ from os import path mydir = path.dirname(path.abspath(__file__)) +RESET = 25 # BCM GPIO +LED_GREEN = 27 # BCM GPIO (not B rev1) +LED_RED = 22 # BCM GPIO + libspi = ctypes.cdll.LoadLibrary(mydir + "/" + LIBNAME) spi_init_defaults_fn = libspi["spi_init_defaults"] spi_init_fn = libspi["spi_init"] @@ -51,7 +55,7 @@ def reset(): trace("reset") - RESET = 25 # BCM GPIO number + reset = ctypes.c_int(RESET) gpio_setout_fn(reset) gpio_high_fn(reset) @@ -59,6 +63,14 @@ gpio_low_fn(reset) time.sleep(0.1) + # Put LEDs into known off state + led_red = ctypes.c_int(LED_RED) + led_green = ctypes.c_int(LED_GREEN) + gpio_setout_fn(led_red) + gpio_low_fn(led_red) + gpio_setout_fn(led_green) + gpio_low_fn(led_green) + def init_defaults(): trace("calling init_defaults") @@ -72,6 +84,20 @@ #spi_init_fn() +def start_transaction(): + """Start a transmit or receive, perhaps multiple bursts""" + # turn the GREEN LED on + led_green = ctypes.c_int(LED_GREEN) + gpio_high_fn(led_green) + + +def end_transaction(): + """End a transmit or receive, perhaps multiple listens""" + # turn the GREEN LED off + led_green = ctypes.c_int(LED_GREEN) + gpio_low_fn(led_green) + + def select(): trace("calling select") spi_select_fn()