diff --git a/src/energenie/radio.py b/src/energenie/radio.py index fdc0b7e..4f4df02 100644 --- a/src/energenie/radio.py +++ b/src/energenie/radio.py @@ -279,6 +279,10 @@ def init(): spi.init_defaults() + print("######## RESET") + spi.reset() # send a hardware reset to ensure radio in clean state + print("######## END RESET") + trace("config FSK") HRF_config_FSK() HRF_clear_fifo() diff --git a/src/energenie/spi.py b/src/energenie/spi.py index 1ce3f42..b4ae190 100644 --- a/src/energenie/spi.py +++ b/src/energenie/spi.py @@ -5,6 +5,7 @@ LIBNAME = "spi_rpi.so" import ctypes +import time from os import path mydir = path.dirname(path.abspath(__file__)) @@ -18,27 +19,69 @@ spi_frame_fn = libspi["spi_frame"] spi_finished_fn = libspi["spi_finished"] +# Might put this in a separate rpi_gpio.so and dynamically link both +# from rpi_spi and from python, but have to be careful with sharing +# memmap. For now, we only use this to control the radio RESET, +# so let's leave this embedded inside the rpi_spi.so until we +# need any more visibility +#gpio_init_fn = libspi["gpio_init"] +#gpio_setin_fn = libspi["gpio_setin"] +gpio_setout_fn = libspi["gpio_setout"] +gpio_high_fn = libspi["gpio_high"] +gpio_low_fn = libspi["gpio_low"] +#gpio_write_fn = libspi["gpio_write"] +#gpio_read_fn = libspi["gpio_read"] + + def trace(msg): pass #print("spi:" + msg) - + + +# Note, this is radio specific, not SPI specific. hmm. +# fine for now to test the theory, but architecturally this is wrong. +# might have an optional reset line in spi_config, and an spi_reset() +# in spi.c that excercises it if required, as a more generic way +# to do this, as most SPI devices have a reset line. If value not +# defined, nothing happens. If value defined, it must have a +# polarity too, and spi.c will set it to an output inactive +# until reset is required. Also need to set up a reset active +# time and a 'after reset' guard time. That will cover most +# cases generically without having to surface the whole inner GPIO +# out to the python. + +def reset(): + trace("reset") + RESET = 25 # BCM GPIO number + reset = ctypes.c_int(RESET) + gpio_setout_fn(reset) + gpio_high_fn(reset) + time.sleep(0.1) + gpio_low_fn(reset) + time.sleep(0.1) + + def init_defaults(): trace("calling init_defaults") spi_init_defaults_fn() - + + def init(): trace("calling init") #TODO build a config structure #TODO pass in pointer to config structure #spi_init_fn() - + + def select(): trace("calling select") spi_select_fn() - + + def deselect(): trace("calling deselect") spi_deselect_fn() - + + def byte(tx): txbyte = ctypes.c_ubyte(tx) #trace("calling byte") @@ -59,8 +102,13 @@ for i in range(framelen): rxlist.append(rxframe[i]) return rxlist - + + def finished(): trace("calling finished") spi_finished_fn() + +# END + + diff --git a/src/monitor.py b/src/monitor.py index bc74cbd..6cca44b 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -88,6 +88,7 @@ sendSwitchTimer = Timer(5, 1) # every 5 seconds offset by initial 1 switch_state = 0 # OFF radio.receiver() + decoded = None while True: # See if there is a payload, and if there is, process it @@ -102,6 +103,10 @@ OpenHEMS.showMessage(decoded) updateDirectory(decoded) + #TODO: Should remember report time of each device, + #and reschedule command messages to avoid their transmit slot + #making it less likely to miss an incoming message due to + #the radio being in transmit mode # assume only 1 rec in a join, for now if decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN: @@ -114,6 +119,16 @@ radio.transmitter() radio.transmit(p) radio.receiver() + + if sendSwitchTimer.check() and decoded != None: + request = OpenHEMS.alterMessage(SWITCH_MESSAGE, + header_sensorid=decoded["header"]["sensorid"], + recs_0_value=switch_state) + p = OpenHEMS.encode(request) + radio.transmitter() + radio.transmit(p) + radio.receiver() + switch_state = (switch_state+1) % 2 # toggle if __name__ == "__main__":