View Single Post
MohammadAG's Avatar
Posts: 2,473 | Thanked: 12,265 times | Joined on Oct 2009 @ Jerusalem, PS/IL
#61
Indent your code!
Making debs is easy, hit me up with a PM if you have any problems
When you make a deb though you'll have to use sudo, so you should figure out why sudo gainroot doesn't work, prefix dbus and zenity lines with run-standalone.sh

Edit 2: who's # Copyright (c) 2010 OEM LTD (HE99116) #
Edit: indented code, I hate ugly scripts
Code:
#!/bin/sh

# Empty Frequencies Finder, find free/empty frequencies #
#                                                       #
#      Copyright (c) 2010 OEM LTD (HE99116)          #

send_request() 
{
  dbus-send --system --print-reply \
            --dest=de.pycage.FMRXEnabler \
            /de/pycage/FMRXEnabler\
            de.pycage.FMRXEnabler.request >/dev/null
}

find_sysfs_path() 
{
    F=`find /sys/class/i2c-adapter -name "fm_frequency"`
    echo `dirname ${F}`
}

power_on() 
{
    # switch on
    echo 1 >${SYSFS_PATH}/power_state
    # set DAC audio route 
    echo 16 >${SYSFS_PATH}/audio_route
    # set stereo
    echo 12 >${SYSFS_PATH}/dac_output
    # set preset tune mode
    echo 1 >${SYSFS_PATH}/fm_search_tune_mode
    # unmute
    echo 0 >${SYSFS_PATH}/mute
}

power_off() 
{
    echo "POWERED OFF" 
    # mute
    echo 1 >${SYSFS_PATH}/mute
    # switch off
    echo 0 >${SYSFS_PATH}/power_state
}

tune_in()
{
    FREQ=$1
    echo ${FREQ} >${SYSFS_PATH}/fm_frequency
}

change_frq()
{
    send_request
    SYSFS_PATH=`find_sysfs_path`
    power_on
    tune_in $cur_freq 
}


#Temporary file for saving empty frequencies
temp=`mktemp -t temp.XXXXXX`

#Print banner
ver="1.3.1"
zenity --info --text="E.F.F. (Emty Frequencies Finder)" --title="E.F.F-$ver"

#Vars
vars()
{
    max=`expr $max \* 100`
    min=`expr $min \* 100`
    range=`expr $max - $min`
    pers_var=`expr $range / 100`
    cur_freq=$min
    pers_cntr=0
    cnt_total=0
}

#Change testing frequency, calculate percentage of progress
change_tfrq()
{
    if [ $cur_freq -eq $max ] || [ $cur_freq -gt $max ]
        then 
            break
    fi
    pers_cntr=`expr $pers_cntr + 1`
    pers=`expr \( 100 \* $pers_cntr \) / $pers_var`
    cur_freq=`expr $cur_freq + 100`
    cnt=0
    change_frq
    if [ $guiyn -eq 0 ]
        then
            echo $pers
        else
            if [ $pers -lt 100 ]
                then
                    echo "Scanning...Found $cnt_total empty frequencies. $pers %"
                else
                    echo "Scanning...Found $cnt_total empty frequencies. 100 %"
            fi
    fi
    sleep 1
    if [ $guiyn -eq 0 ]
        then
            echo "# Scanning...Found $cnt_total empty frequencies"
    fi
}

#How much empty functions
hempty_a()
{
    if [ $cnt -eq 10 ]
        then
            sleep 1
    fi
}

hempty_b()
{
    if [ $cnt -eq 10 ] || [ $cnt -eq 45 ]
        then
            sleep 1
    fi
}

hempty_c()
{
    if [ $cnt -eq 5 ] || [ $cnt -eq 20 ] || [ $cnt -eq 60 ]
        then
            sleep 1
    fi
}

hempty_d()
{
    if [ $cnt -eq 5 ] || [ $cnt -eq 30 ] || [ $cnt -eq 60 ] || [ $cnt -eq 80 ]
        then
            sleep 1
    fi
}

#Zenity question to be root
quest_root()
{
    rans=$(zenity --entry --entry-text="root" --title="To run you must be root, if you agree type ok." --text="Press OK to continue or cancel to quit.")
    if [ $? -eq 0 ]
        then 
            eval $rans
        else
            zenity --error --text="You must be root!"
            exit 1
    fi
}

#Check if the frequency is empty 
check_frq()
{
    while [ 1 ]
        do
            #unmake the following from comment to is if its working
            #echo "# `cat /sys/class/i2c-adapter/i2c-3/3-0022/fm_frequency` `cat /sys/class/i2c-adapter/i2c-3/3-0022/fm_rssi`"
            #echo "# `echo $hempty $cnt`"
            if [ $cnt -eq `expr 60 * $hempty` ]
                then
                    cnt_total=`expr $cnt_total + 1`
                    msg=1
                    echo $cur_freq >> $temp
                    break
            fi
    case $hempty in
        1)
        hempty_a ;;
        2)
        hempty_b ;;
        3)
        hempty_c ;;
        4)
        hempty_d ;;
    esac
    if [ `cat /sys/class/i2c-adapter/i2c-3/3-0022/fm_rssi` = "-112" ]
        then
            cnt=`expr $cnt + 1`
        else
            break
    fi
    done
}

#Stop with zenity cancell button
zcancel()
{
    if [ $? -eq 1 ]
        then
            power_off
            exit 1
    fi
}

#Mesage on $temp, function
tmsg()
{
    if [ $guiyn -eq 1 ]
        then
            echo "You searched from $min to $max, with $hempty emty value. Found $cnt_total empty frequencies:" 
            cat $temp
        fi
}

#MAIN

if [ `cat /sys/devices/platform/gpio-switch/headphone/state` = "disconnected" ]
    then 
        zenity --error --title="ERROR" --text="Headphones are not pluged in!"
        exit 1
fi

if [ `id -u` = "0" ]
    then
        hempty=$(zenity --scale --min-value=1 --max-value=4 --value=1 --text="Note that higher value requires more scaning time!" --title="Search for: 1=Slightly-empty 2=Empty 3=Very-empty 4=Extremely-empty")
        if [ $? -eq 1 ]
            then
                zenity --error --title="ERROR" --text="You must select a value!Aborting..."
                exit 1
        fi

        min=$(zenity --scale --min-value=875 --max-value=1080 --value=875 --title="Search from:" --text="Select min value (ex. 991=99.1 , 1034=103.4)")
        zcancel
        max=$(zenity --scale --min-value=875 --max-value=1080 --value=875 --title="From $min to:" --text="Select max value (ex. 991=99.1 , 1034=103.4)")
        zcancel
        zenity --question --title="Do you want gui during scanning?" --text="If you type "YES" you will not be able to multitask." --ok-label="YES"

        if [ $? -eq 1 ]
            then
                guiyn=1
            else
                guiyn=0
        fi
        vars
        if [ $min -lt $max ]
            then
                if [ $guiyn -eq 0 ]
                    then
                        (while [ 1 ]
                        do
                        change_tfrq
                        check_frq
                        done
                        ) | zenity --progress --text="Scaning..." --auto-close
                        zcancel
                    else
                        while [ 1 ]
                        do
                        change_tfrq
                        check_frq
                        done
                fi
                tmsg
                zenity --text-info --filename=$temp
                power_off
            else
                zenity --error --text="Max value must be greater than min value!"
        fi 
    else
        zenity --error --text="You must be root!"
fi

Last edited by MohammadAG; 2010-09-29 at 14:52.