Electra Bearcat 101

Bearcat 101 Frequency Calculator
    
#!/bin/bash 

# Bearcat 101 Frequency Calculator

import sys
import os

def getfrequency(f):
    Q = 0
    M = 0
    N = 0
    freq = 150.0
    frange = '00'
    # get frequency input
    try:
        freq = float(f)
        if freq >= 30.0 and freq <= 50.0:
            N = 10340
            Q = 200        
            frange = '11'
        elif freq >= 144.0 and freq <= 174.0:
            N = 36940
            Q = 200        
            frange = '01'
        elif freq >= 416.0 and freq <= 512.0:
            N = 40964
            Q = 80
            frange = '10'
        else:
            print('input error!')
            print('out of range!')
            print('')
            sys.exit(-1)
        M = N - round(freq * Q)
         # must be 13 bits long
        B = bin(int(M))[2:].zfill(13)
        B_list = []
        B_list.append(B)
        B = "".join(B_list)
    except ValueError:
        print('invalid input')
        sys.exit()
    return B, frange, f
    
def getdelay(delay):
    # get delay input
    delay = delay.lower()
    if delay != 'y' and delay != 'n':
        print('no answer given for delay')
        print('assuming delay = no')
        print('')
        delay = 'n'
    return delay

def calculate(B, frange, delay, f):
    sw_list = []
    sw_list.append(B)
    sw_list.append(frange)

    if delay == 'y':
        sw_list.append('1')
    else:
        sw_list.append('0')

    str_sw = "".join(sw_list)
    return str_sw

def printoutput(str_sw, f, delay):
    print('')
    print("frequency: ", f, "Mhz")
    print("delay on:", delay)
    print('')
    print('Switch settings')
    print('')
    print(str_sw[:4], " ", str_sw[4:8], " ", str_sw[8:12], " ", str_sw[12:16])
    print('')
    print("length: ",len(str_sw))
    print('')

def writeoutput(position, str_sw, f, delay, outfile):
    writestring = ''
    f = "{0:.4f}".format(float(f))
    writestring = str(position) + '\t\t' + f + '\t' + str_sw[:4] + " " + str_sw[4:8] + " " + str_sw[8:12] + " " + str_sw[12:16] + '\n'
    outfile.write(writestring)

if __name__ == '__main__':
    if len(sys.argv) == 4:
        #batch mode
        #inputs inputlist, outputlist
        infilename = sys.argv[1]
        outfilename = sys.argv[2]
        delay = sys.argv[3]
        if os.path.exists(infilename):
            infile = open(infilename, 'rt')
        else:
            print('error infile ' + infilename + " does not exist")
            sys.exit(1)
        outfile = open(outfilename, 'wt')
        outfile.write('channel\tfrequency\tswitches\n')
        count = 1
        for line in infile:
            B, frange, f = getfrequency(line.rstrip('\n\r'))
            delay = delay.lower()
            str_sw = calculate(B, frange, delay, f)
            writeoutput(count, str_sw, f, delay, outfile)
            count += 1
        infile.close()
        outfile.close()
        print('processed to ' + outfilename)
        sys.exit(0)
    elif len(sys.argv) > 1:
        #invalid input
        print('usage: inputfile outputfile delay')
        sys.exit(1)
    else:
        #interactive mode
        print('')
        print('Bearcat 101 Scanner Programmer')
        print('')
        lexit = 0
        answer = 'y'
        while (lexit != 1):
            f = ''
            if sys.version_info.major == 3:
                f = input('frequency (Mhz): ')
            elif sys.version_info.major == 2:
                try:
                    f = raw_input('frequency (MHz): ')
                except NameError:
                    pass
            else:
                print ('unknown python version!')
                sys.exit(1)
            B, frange, f = getfrequency(f)
            delay = ''
            if sys.version_info.major == 3:
                delay = input('delay (y/n): ')
            elif sys.version_info.major == 2:
                try:
                    delay = raw_input('delay (y/n): ')
                except NameError:
                    pass
            else:
                print ('unknown python version!')
                sys.exit(1)                 
            delay = getdelay(delay)
            str_sw = calculate(B, frange, delay, f)
            printoutput(str_sw, f, delay)
            answer = ''
            if sys.version_info.major == 3:
                answer = input('again? (y/n): ')
            elif sys.version_info.major == 2:
                try:
                    answer = raw_input('again? (y/n): ')
                except NameError:
                    pass
            else:
                print ('unknown python version!')
                sys.exit(1)
            answer = answer.lower()
            if answer == 'n':
                lexit = 1
            else:
                print('')
        print('')
        print('done...')
        print('')
    
  

Bearcat 101 Related LINKS