#!/bin/sh


cd /root

hciconfig hci0 up

#hcitool -i hci0 scan

bluetoothd

sleep 3

# headset
hciconfig hci0 class 280404
#hciconfig hci0 name 'sif headset'
hciconfig hci0 name 'hark headset'

# scan
hciconfig hci0 piscan

# voice path to HCI
#hcitool -i hci0 cmd 3f 1d 00 >& /dev/null

# voice path to PCM
hcitool -i hci0 cmd 3f 1d 01 >& /dev/null

# PCM master
#hcitool -i hci0 cmd 3f 07 02 >& /dev/null

# PCM slave
hcitool -i hci0 cmd 3f 07 00 >& /dev/null

# daemon動作 16K->8K ダウンサンプリングあり
#./bt -d -r

# daemon動作 16K->8K ダウンサンプリングあり 常時音声出力
./bt -d -r -a 

# daemon動作 16K->8K ダウンサンプリングあり 音声認識起動
#./bt -d -r -w

#echo 'bluetooth start...'

## Gadget RASP-MX Enable

# Tablet Mic Mode
#modprobe g_raspmx mic_mode=0

# Cascade Mic Mode
#modprobe g_raspmx mic_mode=1


## USB(HOST Side) Reset
echo 0 > /sys/bus/usb/devices/usb1/authorized
usleep 200000	# 200ms
echo 1 > /sys/bus/usb/devices/usb1/authorized
usleep 200000	# 200ms

## Multi CPU Enable
echo 1 > /sys/devices/system/cpu/cpu1/online
usleep 200000	# 200ms
echo 1 > /sys/devices/system/cpu/cpu2/online
usleep 200000	# 200ms
echo 1 > /sys/devices/system/cpu/cpu3/online
usleep 200000	# 200ms

## LED Control
# CPU_LED0 (RED)
echo 102 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio102/direction
echo 1 > /sys/class/gpio/gpio102/value
hciconfig | grep "DOWN" > /dev/null
if [ $? -eq 0 ] ; then
echo 0 > /sys/class/gpio/gpio102/value
fi
usleep 200000	# 200ms
# CPU_LED0 (GREEN)
echo 103 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio103/direction
echo 0 > /sys/class/gpio/gpio103/value
usleep 200000	# 200ms

## Demo
#if [ -e /hark/hark-wav.conf ]; then
#    /hark/hark --daemon --config /hark/hark-wav.conf --stanby
#fi

## Demo kurage + UART
mkdir -p /tmp/raw

# mount SD
if [ -e /dev/mmcblk0p2 ]; then
    mount /dev/mmcblk0p2 /tmp/raw
elif [ -e /dev/mmcblk1p2 ]; then
    mount /dev/mmcblk1p2 /tmp/raw
else
    echo "2nd partition not found, /tmp/raw is ram disk."
fi

# start hark
if [ -e /hark/hark-kurage0828.conf ]; then
    hark --daemon --config /hark/hark-kurage0828.conf
fi

