組み込みProgrammerのチラシの裏

Raspberry Pi Bluetooth Audio Receiver 2

| Comments

bluetooth-agentが起動していない時はペアリングすることが出来ないことが分かりました。 そのため、Raspberry piを電源入れてからいつまで待てばいいか分からないのでLEDを使って通知することにしました。

GPIO4を使用しているので、GNDとGPIO4にLEDを接続してください。

#!/usr/bin/env ruby

# File: bluetooth-agent-led.rb - last edit
# yoshitake 21-Aug-2015

class LedDriver
  GPIO_INDEX            = 4
  GPIO_ROOT             = "/sys/class/gpio"
  GPIO_EXPORT           = "#{GPIO_ROOT}/export"
  GPIO_TARGET           = "#{GPIO_ROOT}/gpio#{GPIO_INDEX}"
  GPIO_TARGET_DIRECTION = "#{GPIO_TARGET}/direction"
  GPIO_TARGET_VALUE     = "#{GPIO_TARGET}/value"

  def initialize
    open(GPIO_EXPORT, "w") do |io|
      io.print("#{GPIO_INDEX}")
    end
    # wait for GPIO_TARGET files.
    sleep(1)
    open(GPIO_TARGET_DIRECTION, "w") do |io|
      io.print("out")
    end
  end

  def on
    open(GPIO_TARGET_VALUE, "w") do |io|
      io.print("1")
    end
  end

  def off
    open(GPIO_TARGET_VALUE, "w") do |io|
      io.print("0")
    end
  end
end

led = LedDriver.new
loop do
  found = system("ps -C bluetooth-agent >/dev/null")
  found ? led.on : led.off
  sleep 1
end

# Log
# 21-Aug-2015 yoshitake Created.
#! /bin/sh
### BEGIN INIT INFO
# Provides: bluetooth-agent-led
# Required-Start: $remote_fs $syslog bluetooth-agent bluetooth pulseaudio
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
# /etc/init.d/bluetooth-agent-led
USER=root
HOME=/root
NAME=bluetooth-agent-led
PIDFILE=/var/run/$NAME.pid
export USER HOME
case "$1" in
        start)
                start-stop-daemon -S --background --make-pidfile --pidfile $PIDFILE -x /usr/bin/bluetooth-agent-led.rb -b --
                echo "bluetooth-agent-led started"
                ;;
        stop)
                echo "Stopping bluetooth-agent-led"
                start-stop-daemon -K --pidfile $PIDFILE
                ;;
        *)
                echo "Usage: /etc/init.d/bluetooth-agent-led {start|stop}"
                exit 1
                ;;
esac
exit 0
$ sudo chmod +x /usr/bin/bluetooth-agent-led.rb
$ sudo chmod +x /etc/init.d/bluetooth-agent-led
$ sudo update-rc.d bluetooth-agent-led defaults

LEDチカチカ daemon

#!/usr/bin/env ruby

# File: ledd - last edit
# yoshitake 21-Aug-2015

class LedDriver
  GPIO_INDEX            = 4
  GPIO_ROOT             = "/sys/class/gpio"
  GPIO_EXPORT           = "#{GPIO_ROOT}/export"
  GPIO_TARGET           = "#{GPIO_ROOT}/gpio#{GPIO_INDEX}"
  GPIO_TARGET_DIRECTION = "#{GPIO_TARGET}/direction"
  GPIO_TARGET_VALUE     = "#{GPIO_TARGET}/value"

  def initialize
    open(GPIO_EXPORT, "w") do |io|
      io.print("#{GPIO_INDEX}")
    end
    # wait for GPIO_TARGET files.
    sleep(1)
    open(GPIO_TARGET_DIRECTION, "w") do |io|
      io.print("out")
    end
  end

  def on
    open(GPIO_TARGET_VALUE, "w") do |io|
      io.print("1")
    end
  end

  def off
    open(GPIO_TARGET_VALUE, "w") do |io|
      io.print("0")
    end
  end
end

led = LedDriver.new
loop do
  led.on
  sleep 1
  led.off
  sleep 1
end

# Log
# 21-Aug-2015 yoshitake Created.
#! /bin/sh
### BEGIN INIT INFO
# Provides:          ledd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Yoshitake Hamano <oed0cow6oy5@gmail.com>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="LED daemon"
NAME=ledd
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --status --pidfile $PIDFILE && return 1
    start-stop-daemon -b -m --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    log_end_msg $?
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop}" >&2
    exit 3
    ;;
esac

:
$ sudo chmod +x /usr/sbin/ledd
$ sudo chmod +x /etc/init.d/ledd
$ sudo update-rc.d ledd defaults

Comments