Programme de lecture du signal S-mètre

Sur la voie A1 du convertisseur arrive la tension du S-mètre (après adaptation de niveau par pont diviseur). Le script python suivant doit être lancé en continu et renseigne, sur changement de niveau de signal, le fichier de mesure. Ce dernier est un fichier TCL qui sera exploité par la logique du relais.

| smeter.py
#!/usr/bin/python
#
# MCP3204/MCP3208 sample program for Raspberry Pi
#
# how to setup /dev/spidev?.?
# $ sudo modprobe spi_bcm2708
#
# how to setup spidev
# $ sudo apt-get install python-dev python-pip
# $ sudo pip install spidev
#
import spidev
import time
from collections import deque
 
class MCP3208:
        def __init__(self, spi_channel=0):
                self.spi_channel = spi_channel
                self.conn = spidev.SpiDev(0, spi_channel)
                self.conn.max_speed_hz = 1000000 # 1MHz
 
        def __del__( self ):
                self.close
 
        def close(self):
                if self.conn != None:
                        self.conn.close
                        self.conn = None
 
        def bitstring(self, n):
                s = bin(n)[2:]
                return '0'*(8-len(s)) + s
 
        def read(self, adc_channel=0):
                # build command
                cmd = 128 # start bit
                cmd += 64 # single end / diff
                if adc_channel % 2 == 1:
                        cmd += 8
                if (adc_channel/2) % 2 == 1:
                        cmd += 16
                if (adc_channel/4) % 2 == 1:
                        cmd += 32
 
                # send & receive data
                reply_bytes = self.conn.xfer2([cmd, 0, 0, 0])
 
                #
                reply_bitstring = ''.join(self.bitstring(n) for n in reply_bytes)
                # print reply_bitstring
 
                # see also... http://akizukidenshi.com/download/MCP3204.pdf (page.20)
                reply = reply_bitstring[5:19]
                return int(reply, 2)
 
if __name__ == '__main__':
                spi = MCP3208(0)
 
                count = 0
                nb=4  #number of aquiring signal
                t = 0.5 #time between two acquiring in seconds. Can be a float value. If nb=3 and t= 2, the qualified value will be given after 6 seconds of continuous signal over 'level'
                level=1730 #under this value, signal is not considered
                stack = deque([],maxlen=nb)
 
                sig=-1
                sig_old= -1
                #while count <= 11:
                while True:
 
                        val = spi.read(0)
                        time.sleep(t)
                        #print "val brute = "
                        #print val
                        #print " stack = "
                        #print stack
                        if val> level :    # software Squelch
                                stack.append(val)
 
                        else :  # Reset counting
                                stack = deque([],maxlen=nb)
                                sig = -1
 
 
 
                        if len(stack)==nb :
                                v=0
                                for i in range(0,nb) :
                                        v+= stack [i]
                                        i = i+1
                                meanv= (v/nb)
                                #print len(stack)
                                #print stack
 
                                #print "meanv = "
                                #print meanv
                                if ((meanv <= stack[0]*1.05) and (meanv >= stack[0]*0.95)) :
                                #prend en compte uniquement si signal stable pour les n echantillons
                                        meanv = stack[0]
                                else :meanv= 0
 
                                if 0 <= meanv < 800 : sig=-1
                                #elif 650 <= meanv < 1950 : sig=0
                                elif 800 <= meanv < 920 : sig=1
                                elif 920 <= meanv < 1117 : sig=2
                                elif 1117 <= meanv < 1336 : sig=3
                                elif 1336 <= meanv < 1616 : sig=4
                                elif 1616 <= meanv < 1987 : sig=5
                                elif 1987 <= meanv < 2365 : sig=6
                                elif 2365 <= meanv < 2702 : sig=7
                                elif 2702 <= meanv < 3035 : sig=8
                                elif 3035 <= meanv < 3370 : sig=9
                                elif meanv>=3370 : sig=24
                                else : sig=-1
 
 
                        #print "Signal = %d" % sig
                        if sig != sig_old :
                                print "Changement de niveau de signal =  %d" % sig
                                file = open("/tmp/smeter.tcl", "w")
                                file.write( "set signal " + str(sig) + ";")
                                file.close()
                                sig_old=sig

Code de lecture du signal

Modifions à présent dans le fichier /usr/share/svxlink/events.d/local/Logic.tcl le son appelé dans la fonction send_rgr_sound. Nous avons crée 10 fichiers sonores différents (S1 à S9, et S9+).

proc send_rgr_sound {} {
  variable sql_rx_id;
  variable signal;
  #variable filename;
 
 
 
#lecture du signal
#   set output [exec python /etc/svxlink/smeter/smeter_1mes.py]
 
   if { [file exists /tmp/smeter.tcl]  } {
           source "/tmp/smeter.tcl"
           set son ""
                  if {$signal >=0} {
                        if {$signal>=10} {
                        set son "S9+" ; } else {
                        append son "S" $signal ;
                        }
                  playMsg "SVXCard/SMeter" $son;
                  }
           puts "Signal level on RX ID $sql_rx_id : $son";
        }
 
  playTone 440 200 100;
#CW::setPitch 600; # Sets the CW Tone to ~750 Hz
#CW::setAmplitude 100;
#CW::setCpm 125
#CW::play "k";
 
  playSilence 200;
 
  for {set i 0} {$i < $sql_rx_id} {incr i 1} {
    playTone 880 500 50;
    playSilence 50;
  }
  playSilence 100;
}