diff --git a/src/energenie/radio2.py b/src/energenie/radio2.py index aa896c7..7e2439c 100644 --- a/src/energenie/radio2.py +++ b/src/energenie/radio2.py @@ -19,6 +19,7 @@ LIBNAME = "radio_rpi.so" #LIBNAME = "drv/radio_mac.so" # testing +import time import ctypes from os import path mydir = path.dirname(path.abspath(__file__)) @@ -79,30 +80,51 @@ radio_transmitter_fn(m) -def transmit(payload, times=1): +def transmit(payload, outer_times=1, inner_times=8, outer_delay=0): """Transmit a single payload using the present modulation scheme""" #Note, this optionally does a mode change before and after #extern void radio_transmit(uint8_t* payload, uint8_t len, uint8_t repeats); + framelen = len(payload) - Frame = ctypes.c_ubyte * framelen - txframe = Frame(*payload) - times = ctypes.c_ubyte(times) - radio_transmit_fn(txframe, framelen, times) + if framelen < 1 or framelen > 255: + raise ValueError("frame len must be 1..255") + if outer_times < 1: + raise ValueError("outer_times must be >0") + if inner_times < 1 or inner_times > 255: + raise ValueError("tx times must be 0..255") + + framelen = len(payload) + Frame = ctypes.c_ubyte * framelen + txframe = Frame(*payload) + inner_times = ctypes.c_ubyte(inner_times) + + for i in range(outer_times): + radio_transmit_fn(txframe, framelen, inner_times) + if outer_delay != 0: + time.sleep(outer_delay) -def send_payload(payload, times=1): +def send_payload(payload, outer_times=1, inner_times=8, outer_delay=0): """Transmit a payload in present modulation scheme, repeated""" #Note, this does not do a mode change before or after, #and assumes the mode is already transmit #extern void radio_send_payload(uint8_t* payload, uint8_t len, uint8_t times); - #TODO: Should really validate parameters here, so that C code doesn't have to. - #see note at top of this file. framelen = len(payload) - Frame = ctypes.c_ubyte * framelen - txframe = Frame(*payload) - times = ctypes.c_ubyte(times) - radio_send_payload_fn(txframe, framelen, times) + if framelen < 1 or framelen > 255: + raise ValueError("frame len must be 1..255") + if outer_times < 1: + raise ValueError("outer_times must be >0") + if inner_times < 1 or inner_times > 255: + raise ValueError("tx times must be 0..255") + Frame = ctypes.c_ubyte * framelen + txframe = Frame(*payload) + inner_times = ctypes.c_ubyte(inner_times) + + for i in range(outer_times): + radio_send_payload_fn(txframe, framelen, inner_times) + if outer_delay != 0: + time.sleep(outer_delay) #def receiver(fsk=None, ook=None): diff --git a/src/legacy.py b/src/legacy.py index cd11ee0..edd8551 100644 --- a/src/legacy.py +++ b/src/legacy.py @@ -12,15 +12,21 @@ # moving over to the new, faster, C radio driver from energenie import radio2 as radio -# How many times to repeat messages +# How many times to send messages in the driver fast loop # Present version of driver limits to 15 # but this restriction will be lifted soon # 4800bps, burst transmit time at 15 repeats is 400mS # 1 payload takes 26ms # 75 payloads takes 2s -TIMES = 15 +INNER_TIMES = 8 -DELAY = 1 +# how many times to send messages in the API slow loop +# this is slower than using the driver, and will introduce +# inter-burst delays +OUTER_TIMES = 1 + +# delay in seconds between each application switch message +APP_DELAY = 1 #----- TEST APPLICATION ------------------------------------------------------- @@ -66,19 +72,19 @@ raw_input("press ENTER when LED is flashing") print("ON") - radio.transmit(ON_MSGS[switch_no], TIMES) - time.sleep(DELAY) + radio.transmit(ON_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) print("Device should now be programmed") print("Testing....") for i in range(4): - time.sleep(DELAY) + time.sleep(APP_DELAY) print("OFF") - radio.transmit(OFF_MSGS[switch_no], TIMES) - time.sleep(DELAY) + radio.transmit(OFF_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) print("ON") - radio.transmit(ON_MSGS[switch_no], TIMES) + radio.transmit(ON_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) print("Test completed") @@ -90,24 +96,24 @@ # switch_no 0 is ALL, then 1=1, 2=2, 3=3, 4=4 # ON print("switch %d ON" % switch_no) - radio.transmit(ON_MSGS[switch_no], TIMES) - time.sleep(DELAY) + radio.transmit(ON_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) # OFF print("switch %d OFF" % switch_no) - radio.transmit(OFF_MSGS[switch_no], TIMES) - time.sleep(DELAY) + radio.transmit(OFF_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) def switch1_loop(): """Repeatedly turn switch 1 ON then OFF""" while True: - print("Switch 1 ON") - radio.transmit(ON_MSGS[1], TIMES) - time.sleep(DELAY) + print("Switch all ON") + radio.transmit(ON_MSGS[0], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) - print("Switch 1 OFF") - radio.transmit(OFF_MSGS[1], TIMES) - time.sleep(DELAY) + print("Switch all OFF") + radio.transmit(OFF_MSGS[0], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) def pattern_test(): @@ -117,7 +123,7 @@ p = int(p, 16) msg = encoder.build_test_message(p) print("pattern %s payload %s" % (str(hex(p)), encoder.ashex(msg))) - radio.send_payload(msg, TIMES) + radio.send_payload(msg, OUTER_TIMES, INNER_TIMES) if __name__ == "__main__": @@ -130,9 +136,9 @@ try: #pattern_test() - legacy_learn_mode() - legacy_switch_loop() - #switch1_loop() + #legacy_learn_mode() + #legacy_switch_loop() + switch1_loop() finally: radio.finished()