From c580696dc132777c58ee49da1accd0fbd27a78a5 Mon Sep 17 00:00:00 2001 From: "S. Seegel" Date: Tue, 24 Apr 2018 00:15:14 +0200 Subject: [PATCH] auto detect twin module --- emoncms.py | 10 +++++++++- fs20tx.py | 10 ++++++++-- lacrosse.py | 10 ++++++++-- rfm69.py | 44 +++++++++++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/emoncms.py b/emoncms.py index b1dff2b..5b611ad 100755 --- a/emoncms.py +++ b/emoncms.py @@ -1,6 +1,7 @@ #!/usr/bin/env python2.7 import rfm69 +from rfm69 import Rfm69 import sensors from sensors import rawsensor import time @@ -66,7 +67,14 @@ def LogSensor(data): return print "No match for ID" -rfm = rfm69.Rfm69() + +if Rfm69.Test(1): + rfm = Rfm69(1, 24) #when using the RaspyRFM twin +elif Rfm69.Test(0): + rfm = Rfm69() #when using a single single 868 MHz RaspyRFM +else: + print "No RFM69 module found!" + exit() rfm.SetParams( Freq = 868.300, #MHz center frequency diff --git a/fs20tx.py b/fs20tx.py index 52fe454..3cfe710 100755 --- a/fs20tx.py +++ b/fs20tx.py @@ -6,8 +6,14 @@ import sensors import sys import time -rfm = Rfm69() #when using a single single 868 MHz RaspyRFM -#rfm = Rfm69(1, 24) #when using the RaspyRFM twin +if Rfm69.Test(1): + rfm = Rfm69(1, 24) #when using the RaspyRFM twin +elif Rfm69.Test(0): + rfm = Rfm69() #when using a single single 868 MHz RaspyRFM +else: + print "No RFM69 module found!" + exit() + rfm.SetParams( Freq = 868.350, Datarate = 5.0, diff --git a/lacrosse.py b/lacrosse.py index e2aa2e9..ae9d5a6 100755 --- a/lacrosse.py +++ b/lacrosse.py @@ -7,8 +7,14 @@ from sensors import rawsensor import sys import time -rfm = Rfm69() - +if Rfm69.Test(1): + rfm = Rfm69(1, 24) #when using the RaspyRFM twin +elif Rfm69.Test(0): + rfm = Rfm69() #when using a single single 868 MHz RaspyRFM +else: + print "No RFM69 module found!" + exit() + rfm.SetParams( Freq = 868.312, #MHz center frequency Datarate = 9.579, #17.241, #kbit/s baudrate diff --git a/rfm69.py b/rfm69.py index c9544ab..ae0d824 100644 --- a/rfm69.py +++ b/rfm69.py @@ -99,29 +99,39 @@ MODE_TX = 3 MODE_RX = 4 class Rfm69: + @staticmethod + def Test(cs): + spi = spidev.SpiDev() + spi.open(0, cs) + #Testing presence of module + err = False + for i in range(0, 8): + spi.xfer2([(RegSyncValue1 + i) | 0x80, 0x55]) + test = spi.xfer2([(RegSyncValue1 + i), 0x00])[1] + if test != 0x55: + err = True + break + temp = spi.xfer2([(RegSyncValue1 + i) | 0x80, 0xAA]) + test = spi.xfer2([(RegSyncValue1 + i), 0x00])[1] + if test != 0xAA: + err = True + break + spi.close() + return not err + + def __init__(self, cs = 0, gpio_int = 25): + if not Rfm69.Test(cs): + print "ERROR! RFM69 not found" + return + self.__event = threading.Event() self.__spi = spidev.SpiDev() self.__spi.open(0, cs) self.__spi.max_speed_hz=int(5E6) self.__gpio_int = gpio_int - #Testing presence of module - err = False - for i in range(0, 8): - self.__WriteReg(RegSyncValue1 + i, 0x55) - if self.ReadReg(RegSyncValue1 + i) != 0x55: - err = True - break - self.__WriteReg(RegSyncValue1 + i, 0xAA) - if self.ReadReg(RegSyncValue1 + i) != 0xAA: - err = True - break - if err == True: - print "ERROR! RFM69 not found!" - return - - print "RFM69 found!" + print "RFM69 found on CS", cs GPIO.setmode(GPIO.BCM) GPIO.setup(gpio_int, GPIO.IN) GPIO.add_event_detect(gpio_int, GPIO.RISING, callback=self.__RfmIrq) @@ -196,7 +206,7 @@ class Rfm69: print("INIT COMPLETE") def __RfmIrq(self, ch): - self.__event.set(); + self.__event.set() def __WriteReg(self, reg, val): temp = self.__spi.xfer2([(reg & 0x7F) | 0x80, val & 0xFF])