diff --git a/src/energenie/drv/build_mac b/src/energenie/drv/build_mac index 3949287..e96ac74 100755 --- a/src/energenie/drv/build_mac +++ b/src/energenie/drv/build_mac @@ -29,9 +29,9 @@ # build hrf69_test -## gcc hrf69_test.c hrf69.c spis_rpi.c gpio_rpi.c -## mv a.out hrf69_test -## chmod u+x hrf69_test +##gcc hrf69_test.c hrf69.c spis_rpi.c gpio_rpi.c +##mv a.out hrf69_test +##chmod u+x hrf69_test # build radio_test @@ -42,13 +42,13 @@ # build spi .so library on Raspberry Pi ##gcc -Wall -shared -o spi_rpi.so -fPIC spis_rpi.c gpio_rpi.c -## nm -D spi_rpi.so +##nm -D spi_rpi.so ##cp spi_rpi.so .. # radio spi .so library on Mac gcc -Wall -shared -o radio_mac.so -fPIC radio.c hrfm69.c spis.c gpio_sim.c delay_posix.c -nm -g radio_mac.so -## cp radio_mac.so .. +##nm -g radio_mac.so +##cp radio_mac.so .. # END diff --git a/src/energenie/drv/gpio.h b/src/energenie/drv/gpio.h index 2b261fe..cfe319b 100644 --- a/src/energenie/drv/gpio.h +++ b/src/energenie/drv/gpio.h @@ -8,6 +8,8 @@ #include "system.h" +extern const uint8_t gpio_sim; /* 0=> not simulated */ + /***** FUNCTION PROTOTYPES *****/ void gpio_init(void); diff --git a/src/energenie/drv/gpio_rpi.c b/src/energenie/drv/gpio_rpi.c index 8896682..f57a9a5 100644 --- a/src/energenie/drv/gpio_rpi.c +++ b/src/energenie/drv/gpio_rpi.c @@ -33,6 +33,8 @@ static volatile unsigned *gpio; +const uint8_t gpio_sim=0; /* 0=> not simulated */ + /****** MACROS *****/ diff --git a/src/energenie/drv/gpio_sim.c b/src/energenie/drv/gpio_sim.c index f7a2871..741d2cc 100644 --- a/src/energenie/drv/gpio_sim.c +++ b/src/energenie/drv/gpio_sim.c @@ -13,7 +13,7 @@ /***** CONFIGURATION *****/ #define GPIO_MAX 20 -#define GPIO_DEBUG +//#define GPIO_DEBUG //#define GPIO_LOOPBACK @@ -24,6 +24,7 @@ static uint8_t gpio_out[GPIO_MAX] = {0}; static uint8_t gpio_in[GPIO_MAX] = {0}; +const uint8_t gpio_sim=1; /* 1=> is simulated */ void gpio_init() { diff --git a/src/energenie/drv/hrfm69.c b/src/energenie/drv/hrfm69.c index 27f1757..6960e45 100644 --- a/src/energenie/drv/hrfm69.c +++ b/src/energenie/drv/hrfm69.c @@ -3,10 +3,13 @@ * Hope RF RFM69 radio controller low level register interface. */ +#include + #include "system.h" #include "hrfm69.h" #include "spi.h" #include "trace.h" +#include "gpio.h" /*---------------------------------------------------------------------------*/ @@ -93,9 +96,15 @@ void HRF_pollreg(uint8_t addr, uint8_t mask, uint8_t value) { + if (gpio_sim) + { + TRACE_OUTS("gpio simulated, bailing early to prevent lockup\n"); + return; + } + while (! HRF_checkreg(addr, mask, value)) { - // busy wait + // busy wait (TODO:with no timeout & error recovery?) } } @@ -105,6 +114,7 @@ void HRF_clear_fifo(void) { + //TODO: max fifolen is 66, should bail after that to prevent lockup while ((HRF_readreg(HRF_ADDR_IRQFLAGS2) & HRF_MASK_FIFONOTEMPTY) == HRF_MASK_FIFONOTEMPTY) { HRF_readreg(HRF_ADDR_FIFO); diff --git a/src/energenie/drv/radio.c b/src/energenie/drv/radio.c index e870262..bd9be21 100644 --- a/src/energenie/drv/radio.c +++ b/src/energenie/drv/radio.c @@ -1,6 +1,8 @@ /* radio.c 12/04/2016 D.J.Whale * - * An interface to the Energenie Raspberry Pi Radio. + * An interface to the Energenie Raspberry Pi Radio board ENER314-RT-VER01 + * + * https://energenie4u.co.uk/index.phpcatalogue/product/ENER314-RT */ @@ -17,6 +19,9 @@ /***** CONFIGURATION *****/ +#define EXPECTED_RADIOVER 36 + + // Energenie specific radio config values //#define RADIO_VAL_SYNCVALUE1FSK 0x2D // 1st byte of Sync word //#define RADIO_VAL_SYNCVALUE2FSK 0xD4 // 2nd byte of Sync word @@ -32,6 +37,7 @@ /* GPIO assignments for Raspberry Pi using BCM numbering */ #define RESET 25 +// GREEN used for RX, RED used for TX #define LED_GREEN 27 // (not B rev1) #define LED_RED 22 @@ -46,7 +52,6 @@ /***** LOCAL FUNCTION PROTOTYPES *****/ - static void _change_mode(uint8_t mode); static void _wait_ready(void); static void _wait_txready(void); @@ -76,6 +81,7 @@ // [ADDR_FIFOTHRESH, VAL_FIFOTHRESH1], # Condition to start packet transmission: at least one byte in FIFO // [ADDR_OPMODE, MODE_RECEIVER] # Operating mode to Receiver // ] +//#define CONFIG_FSK_COUNT (sizeof(config_FSK)/sizeof(HRF_CONFIG_REC)) static HRF_CONFIG_REC config_OOK[] = { @@ -90,38 +96,28 @@ {HRF_ADDR_BITRATELSB, 0x0B}, // bitrate:4800b/s {HRF_ADDR_PREAMBLELSB, 0}, // preamble size LSB {HRF_ADDR_SYNCCONFIG, HRF_VAL_SYNCCONFIG0}, // Size of sync word (disabled) - {HRF_ADDR_PACKETCONFIG1, 0x00}, // Fixed length, no Manchester coding + {HRF_ADDR_PACKETCONFIG1, 0x80}, // Tx Variable length, no Manchester coding + {HRF_ADDR_PAYLOADLEN, 0} // no payload length + }; #define CONFIG_OOK_COUNT (sizeof(config_OOK)/sizeof(HRF_CONFIG_REC)) /***** MODULE STATE *****/ -// mode = None -// modulation_fsk = None +typedef uint8_t RADIO_MODE; // Stores HRF_MODE_xxx + +typedef struct +{ + RADIO_MODULATION modu; + RADIO_MODE mode; +} RADIO_DATA; + +RADIO_DATA radio_data; /***** PRIVATE ***************************************************************/ - -/*---------------------------------------------------------------------------*/ -// Change the operating mode of the HRF radi - -static void _change_mode(uint8_t mode) -{ - HRF_writereg(HRF_ADDR_OPMODE, mode); -} - - -/*---------------------------------------------------------------------------*/ -// Wait for HRF to be ready after last command - -static void _wait_ready(void) -{ - HRF_pollreg(HRF_ADDR_IRQFLAGS1, HRF_MASK_MODEREADY, HRF_MASK_MODEREADY); -} - - /*---------------------------------------------------------------------------*/ // Load a table of configuration values into HRF registers @@ -136,10 +132,44 @@ /*---------------------------------------------------------------------------*/ +// Change the operating mode of the HRF radio + +static void _change_mode(uint8_t mode) +{ + HRF_writereg(HRF_ADDR_OPMODE, mode); + _wait_ready(); + gpio_low(LED_GREEN); // TX OFF + gpio_low(LED_RED); // RX OFF + + if (mode == HRF_MODE_TRANSMITTER) + { + _wait_txready(); + gpio_high(LED_RED); // TX ON + } + else if (mode == HRF_MODE_RECEIVER) + { + gpio_high(LED_GREEN); // RX ON + } + radio_data.mode = mode; +} + + +/*---------------------------------------------------------------------------*/ +// Wait for HRF to be ready after last command + +static void _wait_ready(void) +{ + TRACE_OUTS("_wait_ready\n"); + HRF_pollreg(HRF_ADDR_IRQFLAGS1, HRF_MASK_MODEREADY, HRF_MASK_MODEREADY); +} + + +/*---------------------------------------------------------------------------*/ // Wait for the HRF to be ready, and ready for tx, after last command static void _wait_txready(void) { + TRACE_OUTS("_wait_txready\n"); HRF_pollreg(HRF_ADDR_IRQFLAGS1, HRF_MASK_MODEREADY|HRF_MASK_TXREADY, HRF_MASK_MODEREADY|HRF_MASK_TXREADY); } @@ -174,14 +204,6 @@ /*---------------------------------------------------------------------------*/ -uint8_t radio_get_ver(void) -{ - return HRF_readreg(HRF_ADDR_VERSION); -} - - -/*---------------------------------------------------------------------------*/ - void radio_init(void) { TRACE_OUTS("radio_init\n"); @@ -203,16 +225,28 @@ uint8_t rv = radio_get_ver(); TRACE_OUTN(rv); TRACE_NL(); - if (rv != 36) + if (rv < EXPECTED_RADIOVER) //TODO: make this ASSERT() { - TRACE_FAIL("unexpected radio ver, not 36(dec)\n"); + TRACE_OUTS("warning:unexpected radio ver EXPECTED_RADIOVER) + { + TRACE_OUTS("warning:unexpected radio ver>exp\n"); } - TRACE_OUTS("standby mode\n"); - _change_mode(HRF_MODE_STANDBY); - HRF_pollreg(HRF_ADDR_IRQFLAGS1, HRF_MASK_MODEREADY, HRF_MASK_MODEREADY); + radio_standby(); } + +/*---------------------------------------------------------------------------*/ + +uint8_t radio_get_ver(void) +{ + return HRF_readreg(HRF_ADDR_VERSION); +} + + /*---------------------------------------------------------------------------*/ void radio_modulation(RADIO_MODULATION mod) @@ -220,32 +254,17 @@ if (mod == RADIO_MODULATION_OOK) { _config(config_OOK, CONFIG_OOK_COUNT); + radio_data.modu = mod; } - else + //else if (mod == RADIO_MODULATION_FSK) + //{ + // _config(config_FSK, CONFIG_FSK_COUNT); + // radio_data.modu = mod; + //} + else //TODO: make this ASSERT() { - TRACE_FAIL("Unknown modulation requested\n"); + TRACE_FAIL("Unknown modulation\n"); } - -// def modulation(fsk=None, ook=None): -// """Switch modulation, if needed""" -// global modulation_fsk -// -// # Handle sensible module defaults for earlier versions of user code -// if fsk == None and ook == None: -// # Force FSK mode -// fsk = True -// -// if fsk != None and fsk: -// if modulation_fsk == None or modulation_fsk == False: -// trace("switch to FSK") -// HRF_config(config_FSK) -// modulation_fsk = True -// -// elif ook != None and ook: -// if modulation_fsk == None or modulation_fsk == True: -// trace("switch to OOK") -// HRF_config(config_OOK) -// modulation_fsk = False } @@ -254,26 +273,56 @@ void radio_transmitter(RADIO_MODULATION mod) { TRACE_OUTS("radio_transmitter\n"); + radio_modulation(mod); _change_mode(HRF_MODE_TRANSMITTER); - _wait_txready(); - //radio_data.modulation = mod; } /*---------------------------------------------------------------------------*/ -void radio_transmit(uint8_t* payload, uint8_t len, uint8_t repeats) +void radio_receiver(RADIO_MODULATION mod) +{ + TRACE_OUTS("radio_receiver\n"); + + radio_modulation(mod); + _change_mode(HRF_MODE_RECEIVER); +} + + +/*---------------------------------------------------------------------------*/ + +void radio_standby(void) +{ + TRACE_OUTS("radio_standby\n"); + _change_mode(HRF_MODE_STANDBY); +} + + +/*---------------------------------------------------------------------------*/ + +void radio_transmit(uint8_t* payload, uint8_t len, uint8_t times) { TRACE_OUTS("radio_transmit\n"); - radio_transmitter(RADIO_MODULATION_OOK); //TODO use present state - radio_send_payload(payload, len, repeats); - radio_standby(); + + uint8_t prevmode = radio_data.mode; + if (radio_data.mode != HRF_MODE_TRANSMITTER) + { + _change_mode(HRF_MODE_TRANSMITTER); + } + + radio_send_payload(payload, len, times); + + if (radio_data.mode != prevmode) + { + _change_mode(prevmode); + } } /*---------------------------------------------------------------------------*/ // Send a payload of data +<<<<<<< HEAD //TODO: Rewrite this to use FIFOLEV and FIFOEMPTY with payloadlen=0 //rather than PACKETSENT, as it will allow any number of repeats. // @@ -295,10 +344,44 @@ wait for fifoempty interrupt flag to be set (wait 1 byte * bps to ensure last byte transmitted) */ +======= +>>>>>>> long-tx -void radio_send_payload(uint8_t* payload, uint8_t len, uint8_t repeats) +/* DESIGN FOR DUTY CYCLE PROTECTION REQUIREMENT (write this later) + * + * At OOK 4800bps, 1 bit is 20uS, 1 byte is 1.6ms, 16 bytes is 26.6ms + * 15 times (old design limit) is 400ms + * 255 times (new design limit) is 6.8s + + * See page 3 of this app note: http://www.ti.com/lit/an/swra090/swra090.pdf + * + * Transmitter duty cycle + * The transmitter duty cycle is defined as the ratio of the maximum ”on” time, relative to a onehour period. + * If message acknowledgement is required, the additional ”on” time shall be included. Advisory limits are: + * + * Duty cycle Maximum “on” time [sec] Minimum “off” time [sec] + * 0.1 % 0.72 0.72 + * 1 % 3.6 1.8 + * 10 % 36 3.6 + */ + +/* DESIGN FOR >255 payload len (write this later) + + will need to set fifolevel as a proportion of payload len + and load that proportion. + i.e. inside the payload repeat loop + load the fifo up in non integral payload portions + also, txstart condition would need to start before whole payload loaded in FIFO + that is probably ok, but fifolev is more to do with fill rate and transmit rate, + and less to do with the actual payload length. + Note that FIFO empties at a rate proportional to the bitrate, + and also adding on manchester coding will slow the emptying rate. + */ + + +void radio_send_payload(uint8_t* payload, uint8_t len, uint8_t times) { - TRACE_OUTS("send_payload\n"); + TRACE_OUTS("radio_send_payload\n"); // Note, when PA starts up, radio inserts a 01 at start before any user data // we might need to pad away from this by sending a sync of many zero bits @@ -309,48 +392,31 @@ // and not on every FIFO load. int i; - uint8_t irqflags1; - uint8_t irqflags2; + + /* VALIDATE: Check input parameters are in range */ + if (times == 0 || len == 0) //TODO: make this an ASSERT() + { + TRACE_FAIL("zero times or payloadlen\n"); + } + if (len > 32) //TODO: make this an ASSERT() + { + TRACE_FAIL("payload length>32\n"); + } /* CONFIGURE: Setup the radio for transmit of the correct payload length */ TRACE_OUTS("config\n"); - if ((unsigned int)repeats * (unsigned int)len > 255) - { - // This is a temporary situation until the new 'indefinite transmit' - // scheme is implemented using fifolevel only, and ignoring packetsent. - TRACE_FAIL("repeats*payloadlen > 255, can't configure\n"); - } - - // the full packet/burst consists of repeated payloads - // packetsent will trigger when this number of bytes have been transmitted - HRF_writereg(HRF_ADDR_PAYLOADLEN, len * repeats); - // but the FIFO is filled in 1 message (4+10+2=16 byte) sections + // Start transmitting when a full payload is loaded. So for '15': // level triggers when it 'strictly exceeds' level (i.e. 16 bytes starts tx, // and <=15 bytes triggers fifolevel irqflag to be cleared) + // We already know from earlier that payloadlen<=32 (which fits into half a FIFO) HRF_writereg(HRF_ADDR_FIFOTHRESH, len-1); - /* Bring into transmitter mode and ramp up the PA */ - TRACE_OUTS("transmitter mode\n"); - _change_mode(HRF_MODE_TRANSMITTER); - - TRACE_OUTS("wait for modeready,txready in irqflags1\n"); - HRF_pollreg(HRF_ADDR_IRQFLAGS1, HRF_MASK_MODEREADY|HRF_MASK_TXREADY, HRF_MASK_MODEREADY|HRF_MASK_TXREADY); - - irqflags1 = HRF_readreg(HRF_ADDR_IRQFLAGS1); - irqflags2 = HRF_readreg(HRF_ADDR_IRQFLAGS2); - TRACE_OUTS("irqflags1,2="); - TRACE_OUTN(irqflags1); - TRACE_OUTC(','); - TRACE_OUTN(irqflags2); - TRACE_NL(); - - - /* TRANSMIT: Transmit a number of bursts back to back */ - TRACE_OUTS("tx repeats in a single burst\n"); + /* TRANSMIT: Transmit a number of payloads back to back */ + TRACE_OUTS("tx multiple payloads in a single burst\n"); // send a number of payload repeats for the whole packet burst - for (i=0; i #include -#define TRACE_OUTS(s) printf("%s", s) -#define TRACE_OUTN(n) printf("%d", (unsigned int)n) -#define TRACE_OUTC(c) putc(c, stdout) -#define TRACE_NL() TRACE_OUTC('\n') +#define TRACE +#define TRACE_OUTS(S) do{printf("%s", S);fflush(stdout);} while (0) +#define TRACE_OUTN(N) do{printf("%d", (unsigned int)N);fflush(stdout);} while (0) +#define TRACE_OUTC(C) putc(C, stdout) +#define TRACE_NL() do{TRACE_OUTC('\n');fflush(stdout);} while (0) +#define TRACE_FAIL(msg) do{fprintf(stderr, "%s", msg);exit(-1);} while (0) -#define TRACE_FAIL(msg) do { \ - fprintf(stderr, "%s", msg); \ - exit(-1); \ -} while (0) +#else // no trace defined +#define TRACE_OUTS(S) +#define TRACE_OUTN(N) +#define TRACE_OUTC(C) +#define TRACE_NL() +#define TRACE_FAIL(M) +#endif #endif /***** END OF FILE *****/ diff --git a/src/energenie/radio2.py b/src/energenie/radio2.py index 8d3d0b0..7e2439c 100644 --- a/src/energenie/radio2.py +++ b/src/energenie/radio2.py @@ -2,24 +2,24 @@ # # New version of the radio driver, with most of the fast stuff pushed into C. # -# This is a temporary test only, eventually when OOK and FSK are reimplemented -# and re-tested, this module will replace the radio.py/spi.py and spi_rpi.so files. +# NOTE 1: This is partially tested, and only used for OOK transmit at the moment. +# FSK transmit and receive is inside radio.py, as the underlying radio.c code +# does not yet support FSK mode. -#NOTE 1: THIS IS A WORK IN PROGRESS - DO NOT USE -#It has the same interface as radio.py (intentionally) -#so that when it is finished, we can just exchange it and all should work -#as before, but faster and more reliably. +# NOTE 2: Also there is an idea to do a python wrapper, build the C code +# for an Arduino and wrap it with a simple serial message handler. +# This would then make it possible to use the Energenie Radio on a Mac/PC/Linux +# machine but by still using the same higher level Python code. +# All you would need is a different radio.py that marshalled data to and from +# the Arduino via pyserial. -#NOTE 2: Also there is an idea to do a python wrapper, build the C code -#for an Arduino and wrap it with a simple serial message handler. -#This would then make it possible to use the Energenie Radio on a Mac/PC/Linux -#machine but by still using the same higher level Python code. -#All you would need is a different radio.py that marshalled data to and from -#the Arduino via pyserial. +#TODO: Should really add parameter validation here, so that C code doesn't have to. +#although it will be faster in C (C could be made optional, like an assert?) LIBNAME = "radio_rpi.so" -#LIBNAME = "radio_mac.so" # testing +#LIBNAME = "drv/radio_mac.so" # testing +import time import ctypes from os import path mydir = path.dirname(path.abspath(__file__)) @@ -80,28 +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 does a mode change before and after + #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) - repeats = ctypes.c_ubyte(times) - radio_transmit_fn(txframe, framelen, repeats) + 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 repeats); + #extern void radio_send_payload(uint8_t* payload, uint8_t len, uint8_t times); framelen = len(payload) - Frame = ctypes.c_ubyte * framelen - txframe = Frame(*payload) - repeats = ctypes.c_ubyte(times) - radio_send_payload_fn(txframe, framelen, repeats) + 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/energenie/radio2_test.py b/src/energenie/radio2_test.py index a393d25..1a13ceb 100644 --- a/src/energenie/radio2_test.py +++ b/src/energenie/radio2_test.py @@ -9,12 +9,11 @@ import time # How many times to repeat the OOK payload. -# NOTE: At the moment this is limited to 16*8<256 in the HRF module -# but that restriction will soon be removed. - - -REPEATS = 8 - +# 4800bps*8*16=26ms per payload +# 75 payloads is 2 seconds +# 255 payloads is 6.8 seconds +TIMES = 75 +DELAY = 0.5 # The 'radio' module knows nothing about the Energenie (HS1527) bit encoding, # so this test code manually encodes the bits. @@ -48,19 +47,20 @@ """Repeatedly test switch 1 ON then OFF""" radio.init() - + # init() defaults to standby() try: radio.modulation(ook=True) while True: print("Switch 1 ON") - radio.send_payload(enc_1on, REPEATS) - radio.standby() - time.sleep(1) + radio.transmit(enc_1on, TIMES) + # auto returns to standby + if DELAY!=0: time.sleep(DELAY) print("Switch 1 OFF") - radio.send_payload(enc_1off, REPEATS) - radio.standby() - time.sleep(1) + radio.transmit(enc_1off, TIMES) + # auto returns to standby + if DELAY!=0: time.sleep(DELAY) + finally: radio.finished() @@ -68,4 +68,4 @@ if __name__ == "__main__": radio_test_ook() -# END \ No newline at end of file +# END diff --git a/src/energenie/radio_rpi.so b/src/energenie/radio_rpi.so index 3d4902a..0129587 100755 --- a/src/energenie/radio_rpi.so +++ b/src/energenie/radio_rpi.so Binary files differ diff --git a/src/legacy.py b/src/legacy.py index 04daa9c..e504201 100644 --- a/src/legacy.py +++ b/src/legacy.py @@ -12,18 +12,27 @@ # 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 -REPEATS = 15 # 4800bps, burst transmit time at 15 repeats is 400mS -# 1 payload takes 26mS +# 1 payload takes 26ms +# 75 payloads takes 2s +INNER_TIMES = 16 + +# 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 ------------------------------------------------------- # Prebuild all possible message up front, to make switching code faster -HOUSE_ADDRESS = None # default +HOUSE_ADDRESS = 0xA0170 ALL_ON = encoder.build_switch_msg(True, house_address=HOUSE_ADDRESS) ONE_ON = encoder.build_switch_msg(True, device_address=1, house_address=HOUSE_ADDRESS) @@ -63,19 +72,19 @@ raw_input("press ENTER when LED is flashing") print("ON") - radio.send_payload(ON_MSGS[switch_no], REPEATS) - time.sleep(1) + 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(1) + time.sleep(APP_DELAY) print("OFF") - radio.send_payload(OFF_MSGS[switch_no], REPEATS) - time.sleep(1) + radio.transmit(OFF_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) print("ON") - radio.send_payload(ON_MSGS[switch_no], REPEATS) + radio.transmit(ON_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) print("Test completed") @@ -87,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.send_payload(ON_MSGS[switch_no], REPEATS) - time.sleep(2) + radio.transmit(ON_MSGS[switch_no], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) # OFF print("switch %d OFF" % switch_no) - radio.send_payload(OFF_MSGS[switch_no], REPEATS) - time.sleep(2) + 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.send_payload(ON_MSGS[1], REPEATS) - time.sleep(1) + radio.transmit(ON_MSGS[1], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) print("Switch 1 OFF") - radio.send_payload(OFF_MSGS[1], REPEATS) - time.sleep(1) + radio.transmit(OFF_MSGS[1], OUTER_TIMES, INNER_TIMES) + time.sleep(APP_DELAY) def pattern_test(): @@ -114,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, REPEATS) + radio.send_payload(msg, OUTER_TIMES, INNER_TIMES) if __name__ == "__main__": @@ -127,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()