#!/bin/bash
# console-config/src/lib-scripts/serial-rtd-out.sh
#
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Kelly Dunlop <kdunlop@guralp.com>
#  License: GPLv3
#
# Setup the parameters for a serial port configured for RTD output.



# display_rtd_out_sample_divisor
# sample_divisor (default 1.0)
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_rtd_out_sample_divisor() {
    if [ -z "$2" ]
    then
        default=$3
    else
        default=$2
    fi

    display_inputbox "$1" "Enter value to divide 24-bit samples into 16-bit range." \
           0 0 "$default"
}



# display_rtd_out_chan0
# chan0 (default "")
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_rtd_out_chan0() {
    if [ -z "$2" ]
    then
        default=$3
    else
        default=$2
    fi

    display_inputbox "$1 Enter the channel names here." "Data channel 0 name." \
           0 0 "$default"
}



# display_rtd_out_chan1
# chan1 (default "")
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_rtd_out_chan1() {
    if [ -z "$2" ]
    then
        default=$3
    else
        default=$2
    fi

    display_inputbox "$1 Enter the channel names here." "Data channel 1 name." \
           0 0 "$default"
}



# display_rtd_out_chan2
# chan2 (default "")
# $1 is the title text to display
# $2 is the current value
# $3 is the default value
display_rtd_out_chan2() {
    if [ -z "$2" ]
    then
        default=$3
    else
        default=$2
    fi

    display_inputbox "$1 Enter the channel names here." "Data channel 2 name." \
           0 0 "$default"
}



# This sets the default values in the config file independently of whether there were
# any changes to the user definable parameters.
# Think these should just be set now we always write a config file.....
check_and_update_defaults() {
    block_start="`cfget ${RTD_CFG_FILE} block_start_char`"
    if [ -z "${block_start}" ]
    then
        cfset ${RTD_CFG_FILE} block_start_char="0x0d"
    fi
    block_endian="`cfget ${RTD_CFG_FILE} block_endian`"
    if [ -z "${block_endian}" ]
    then
        cfset ${RTD_CFG_FILE} block_endian="big"
    fi
    block_coding="`cfget ${RTD_CFG_FILE} block_coding`"
    if [ -z "${block_coding}" ]
    then
        cfset ${RTD_CFG_FILE} block_coding="2s_complement"
    fi
    block_terminator="`cfget ${RTD_CFG_FILE} block_terminator`"
    if [ -z "${block_terminator}" ]
    then
        cfset ${RTD_CFG_FILE} block_terminator="block_end_char"
    fi
    block_terminator_char="`cfget ${RTD_CFG_FILE} block_terminator_char`"
    if [ -z "${block_terminator_char}" ]
    then
        cfset ${RTD_CFG_FILE} block_terminator_char="0x0a"
    fi

    # synthesise_sine_sps if a set of channels isn't set then set this.
    # This also needs to handle the case where synthesise_sine_sps was set
    # but now we've just set the chanX values so we need to unset it.
    value="`cfget ${RTD_CFG_FILE} chan0`"
    synthesise_sine_sps="`cfget ${RTD_CFG_FILE} synthesise_sine_sps`"
    if [ -z ${value} ]
    then
        if [ -z "${synthesise_sine_sps}" ]
        then
            cfset ${RTD_CFG_FILE} synthesise_sine_sps="40"
        fi
    else
        if [ -n "${synthesise_sine_sps}" ]
        then
            cfset ${RTD_CFG_FILE} synthesise_sine_sps=""
        fi
    fi
}



# Go through the list of things that the user can change in the RTD output config
# file.
#   $1 - portname
# If something is Cancelled or there is an error then this can exit.
serial_rtdout_parameters() {
    local port=$1
    local titletext

    RTD_CFG_FILE="${SERIAL_CFDIR}/rtd-out/${port}"

    # Set default values
    DEF_RTD_SAMPLE_DIVISOR="1.0"
    DEF_RTD_CHAN0=""
    DEF_RTD_CHAN1=""
    DEF_RTD_CHAN2=""

    # Get current values or set defaults if file doesn't already exist
    if [ -r $RTD_CFG_FILE ]
    then
        # get the current values for each item
        RTD_SAMPLE_DIVISOR="`cfget ${RTD_CFG_FILE} sample_divisor`"
        RTD_CHAN0="`cfget ${RTD_CFG_FILE} chan0`"
        RTD_CHAN1="`cfget ${RTD_CFG_FILE} chan1`"
        RTD_CHAN2="`cfget ${RTD_CFG_FILE} chan2`"
        RTD_SERIAL_BAUD="`cfget ${RTD_CFG_FILE} serial_baud`"
    else
        # set empty values for each item
        RTD_SAMPLE_DIVISOR=""
        RTD_CHAN0=""
        RTD_CHAN1=""
        RTD_CHAN2=""
        RTD_SERIAL_BAUD=""
    fi

    # Now go through the values giving user a choice
    titletext="${port} RTD out config"

    # sample_divisor (default 1.0)
    display_rtd_out_sample_divisor "${titletext}" "${RTD_SAMPLE_DIVISOR}" "${DEF_RTD_SAMPLE_DIVISOR}"
    NEW_RTD_SAMPLE_DIVISOR="`cat ${DIALOG_RESULTFILE}`"

    # chan0 (default "")
    display_rtd_out_chan0 "${titletext}" "${RTD_CHAN0}" "${DEF_RTD_CHAN0}"
    NEW_RTD_CHAN0="`cat ${DIALOG_RESULTFILE}`"

    # chan1 (default "")
    display_rtd_out_chan1 "${titletext}" "${RTD_CHAN1}" "${DEF_RTD_CHAN1}"
    NEW_RTD_CHAN1="`cat ${DIALOG_RESULTFILE}`"

    # chan2 (default "")
    display_rtd_out_chan2 "${titletext}" "${RTD_CHAN2}" "${DEF_RTD_CHAN2}"
    NEW_RTD_CHAN2="`cat ${DIALOG_RESULTFILE}`"

    cconf_create_config_file $RTD_CFG_FILE

    # Now we need to update the config
    cfset ${RTD_CFG_FILE} sample_divisor="${NEW_RTD_SAMPLE_DIVISOR}"
    cfset ${RTD_CFG_FILE} chan0="${NEW_RTD_CHAN0}"
    cfset ${RTD_CFG_FILE} chan1="${NEW_RTD_CHAN1}"
    cfset ${RTD_CFG_FILE} chan2="${NEW_RTD_CHAN2}"

    check_and_update_defaults

    # Now we've finished making changes reset the permission of the config file
    cconf_set_perms_and_group ${RTD_CFG_FILE} 664 "conf"
}



# vim: ts=4:sw=4:expandtab:syntax=sh
