#!/bin/sh
#############################################################
# TODO: check old and new address before change?
# TODO: check result after change, query old and new address?
#############################################################
path="${0%/*}"
. "$path/inc/gwhw.inc"
. "$path/inc/dali.inc"

needRoot
daliI2COK

usage() {
	die 'gwhw dali change-short [short/group/br] [new short/clear/remove]'
}

ADDR="${1}"
shift
NADDR="${1}"
shift

[ -z "${ADDR}"  ] && usage
[ -z "${NADDR}" ] && usage
#############################################################
# It does not make sense to change short adress in group or droadcast, but we allow it anyway
ADDR="$(addrToHex "$ADDR")"
case "${NADDR}" in
	clear|remove|mask|0xff|255)
		NADDR=0xff
		;;
	*)
		[ '1' = "$(( NADDR>63 ))" ] && NADDR=63
		# Address must be in the form of 0b0AAAAAA1
		NADDR="$(( (${NADDR}<<1) +1 ))"
esac

# Change short address
daliTX3B 0x00 0xa3 "${NADDR}" # Store value in DTR
waitTX
daliTX3B 0x01 "${ADDR}" 0x80 # send DTR as short address
