#!/bin/sh

# start pigpiod daemon
pigpiod -t 0 -f -l -s 10

# wait for pigpiod to initialize - indicated by 'pigs t' exit code of zero

count=10 # approx time limit in seconds
while ! pigs t >/dev/null 2>&1 ; do
	if [ $((count--)) -le 0 ]; then
		printf "\npigpiod failed to initialize within time limit\n"
		exit 1
	fi
#	printf "\nWaiting for pigpiod to initialize\n"
	sleep 1
done
printf "\npigpiod is running\n"

# load uinput module - required to be able to send keystrokes
# then set the permission to group writable, so you don't need to run sbpd with root permissions
sudo modprobe uinput
sudo chmod g+w /dev/uinput

# The full list of Jivelite key commands can be found here:
# https://github.com/ralph-irving/tcz-lirc/blob/master/jivekeys.csv

# button 1 [PREV]					# button-section, defines the GPIO and key-commands
SW1=6							# GPIO (BCM, not Board)
SH1=KEY:KEY_Z					# key-command for SHORT press (here: preset 1)
LO1=KEY:KEY_E					# key-command for LONG press (here: preset 2)
LMS1=250 						# milliseconds for long press

# button 2 [FORW]
SW2=5
SH2=KEY:KEY_B
LO2=KEY:KEY_R
LMS2=250

# button 3 [PLAYNOW]
SW3=13
SH3=KEY:KEY_N
LO3=KEY:KEY_N
LMS3=250

# CMD="sbpd -v -f /home/tc/sbpd_commands.cfg \
CMD="sbpd -v \
b,$SW1,$SH1,2,0,$LO1,$LMS1 \
b,$SW2,$SH2,2,0,$LO2,$LMS2 \
b,$SW3,$SH3,2,0,$LO3,$LMS3 "


echo $CMD
$CMD > /dev/null 2>&1 &

