スタートアップmpc起動スクリプト
$ cat /home/pi/radio/key_in.c  これをCCコンパイルしてできたa.outをkey_in.outにしておく。
                起動時にGPIO25がロー(0)の場合はすぐに終了するようにしてある。
                このため、PI基板のGPIO25にプルダウン用の抵抗を追加した。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
void close_gpio()
{
/* close */
   system("echo \"7\" > /sys/class/gpio/unexport");
   system("echo \"8\" > /sys/class/gpio/unexport");
   system("echo \"9\" > /sys/class/gpio/unexport");
   system("echo \"10\" > /sys/class/gpio/unexport");
   system("echo \"11\" > /sys/class/gpio/unexport");
   system("echo \"12\" > /sys/class/gpio/unexport");
}
int main(int argc, char *argv[])
{
  int xloop,i;
  int fd0;
  char in0[3], filename0[64];
  int  mpc_stat,down_stat;
#define ON_STAT 1
#define OFF_STAT 0
  mpc_stat= OFF_STAT;
  down_stat= OFF_STAT;
  /* initialize */
  system("echo \"7\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio7/direction");
  system("echo \"8\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio8/direction");
  system("echo \"9\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio9/direction");
  system("echo \"10\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio10/direction");
  system("echo \"1\" > /sys/class/gpio/gpio10/value");
  system("echo \"11\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio11/direction");
  system("echo \"0\" > /sys/class/gpio/gpio11/value");
  system("echo \"25\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio25/direction");
   /* When start up, check gpio25 state */
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0 )
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      if( in0[0] == '0')  /* This program finish */
      {
         close(fd0);
         close_gpio();
         exit(0);
      }
    }
    close(fd0);
do
/* for(xloop=0;xloop < 15;++xloop) */
{
    /* printf("xloop= %d\n",xloop); */ /* xloop++; */
    fd0=open("/sys/class/gpio/gpio7/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio7= %c \n", in0[0]); */
      if( in0[0]=='0')
      {
        switch(mpc_stat)
        {
           case  ON_STAT:
             system("mpc stop > /var/log/radio.log");
             mpc_stat=OFF_STAT;
             system("echo \"0\" > /sys/class/gpio/gpio11/value");
             break;
          case   OFF_STAT:
             system("mpc play > /var/log/radio.log");
             mpc_stat=ON_STAT;
             system("echo \"1\" > /sys/class/gpio/gpio11/value");
             break;
        }
      }
    }
    close(fd0);
   fd0=open("/sys/class/gpio/gpio8/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio8= %c \n", in0[0]); */
      if((in0[0]=='0') && (mpc_stat == ON_STAT))
      {
        system("mpc next > /var/log/radio.log");
      }
    }
    close(fd0);
     fd0=open("/sys/class/gpio/gpio9/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio9= %c \n", in0[0]); */
      if( (in0[0] == '0') && ( mpc_stat == ON_STAT))
      {
          system("mpc prev > /var/log/radio.log");
      }
    }
    close(fd0);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio25= %c \n", in0[0]); */
      if( in0[0] == '0') down_stat= ON_STAT;
    }
    close(fd0);
   /* wait half  second */
   usleep(500000);
 }  while(down_stat == OFF_STAT);  /* end of xloop */
   if( mpc_stat == ON_STAT)
   {
     mpc_stat=OFF_STAT;
     system("mpc stop");
     system("echo \"0\" > /sys/class/gpio/gpio11/value");
   }
    /* check down request two seconds more time */
    /*
    sleep(2);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
     */ 
      /* printf("gpio25= %c \n", in0[0]); */
     /*
      if((down_stat == ON_STAT) && (in0[0]=='0')) {}
      else { down_stat=OFF_STAT; }
    }
    close(fd0);
    */
   system("echo \"0\" > /sys/class/gpio/gpio10/value");
    /* close gpio */
    close_gpio();
   if(down_stat == ON_STAT)
   {  system("shutdown -h now "); }
}
プログラムを動かしてみて、ROOT権限がないとエラーとなって動かない。
$sudo /home/pi/radio/key_in.out &
ログファイルradio.log ができた後に、読み書きの設定を一般ユーザーも可能なように変更しておいた。
$sudo chmod 666 /var/log/radio.log
スタートアップのスクリプトを作成する。
$ cat /home/pi/bin/script_autorun
#!/bin/bash
# script to start an application
echo "start script-autorun..."
sudo /home/pi/radio/key_in.out &
このスクリプトを実行可能に設定する。
$  sudo chmod 755 script_autorun
起動時にこのスクリプトを起動するように /etc/rc.localに追加する。
$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
# for radio
/home/pi/bin/script_autorun      <====この行を追加した。
exit 0






LCD表示のためのI2C通信のための設定
$ cat /etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
# blacklist i2c-bcm2708   <==コメントにする。
$ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev   <==追加する。
$ sudo apt-get install i2c-tools   I2C通信のためのツールのインストールをする。
Reading package lists... Done
$ cat /home/pi/radio/key_in.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/i2c-dev.h>
/* proto type declare for I2C and LCD display */
int i2c_portopen();
int i2c_write(int rs0, unsigned char db0, int fd0);
int i2c_portclose(int fd0);
int init_lcd(int fd0);
int test_lcd_puts(unsigned char *s1, unsigned char *s2, int fd0);
/* In Raspberry PI, I2C-0 0x00-0x77     */
/*                  I2C-1 0x78-         */
/* This LCD address 0x7c is bigger than 0x77 */
/* So, this LCD i2c map to I2C-1 0x3e   */
#define DEVICE_ADDR2 0x3e  /* 0x7c /* 0x0111 1100 */
int i2c_portclose(int fd0)
{
    close(fd0);
    return(0);
}
int i2c_portopen()
{
   int fd0;
   /* i2c port open */
   if( (fd0 = open("/dev/i2c-1", O_RDWR)) < 0)
   {
     return(-1);
   }
   /* set slave address */
   if(ioctl(fd0, I2C_SLAVE, DEVICE_ADDR2) < 0)
   {
     close(fd0);
     return(-1);
   }
   return(fd0);
}
int i2c_write1(int rs0, unsigned char db0, int fd0)
{
      int len;
      unsigned char send0[4];
      if( rs0 == 0) send0[0]= 0x00; /* rs=0 cmd */
      else         send0[0]= 0x40; /* rs=1 data */
      send0[1] = db0;
      len=2;
     if ( (write(fd0, send0, len)) != len)
     {
       return(-1);
     }
     return(0);
}
int test_lcd_puts(unsigned char *s1, unsigned char *s2, int fd0)
{
        int ack;
        ack=i2c_write1(0, 0x38, fd0);  /* //0x0011 1000); // function set // function set interface data 8bits, number line 2 */
        usleep(1000);
        ack=i2c_write1(0, 0x0c, fd0);   /* ///0x0000 1100); // Display On */
        usleep(1000);
        i2c_write1(0, 0x01, fd0);  /* ///0x00000001); // Clear Display   set ddram address */
        usleep(2000);
        /* display character on line 1 */
        while(*s1 != 0x00){
                ack=i2c_write1(1, *s1, fd0);    /* write ddram */
                s1++;
                usleep(1000);
        }
        /* move cursor to line 2
        ack=i2c_write1(0,0xc0, fd0);   /* ///0x11000000);       // ADDR=0x40  set ddram address */
        usleep(1000);
        /* two times access ! */
        ack=i2c_write1(0, 0xc0, fd0);
        usleep(1000);
        while(*s2 != 0x00){
               ack=i2c_write1(1, *s2, fd0);    /* write ddram */
               s2++;
               usleep(1000);
        }
        return 0;
}
int init_lcd(int fd0)
{
   int ack;
   /* lcd initize */
    ack=i2c_write1(0, 0x38, fd0);  /* function set interface data 8bits, number line 2, double height font */
    usleep(1000);
    ack=i2c_write1(0, 0x39, fd0);  /* function set Instruction table select With interface data 8bits, number line 2 */
    usleep(1000);
    ack=i2c_write1(0, 0x14, fd0);  /* interval osc 1/5bias, ajust internal OSC freq */
    usleep(1000);
    ack=i2c_write1(0, 0x70, fd0 );  /* | (contrast & 0x0f)); // contrast Low */
    usleep(1000);
    /* change contast from 0x5f to 0x5e */
    ack=i2c_write1(0, 0x5e, fd0);  /*  | ((contrast >> 4) & 0x03)); // Icon Display On, booster circuit on, contast High */
    usleep(1000);
    ack=i2c_write1(0, 0x6c, fd0);  /* 0x0110 1100); // follower control, on */
    usleep(300000);
    return(0);
}
void close_gpio()
{
/* close */
   system("echo \"7\" > /sys/class/gpio/unexport");
   system("echo \"8\" > /sys/class/gpio/unexport");
   system("echo \"9\" > /sys/class/gpio/unexport");
   system("echo \"10\" > /sys/class/gpio/unexport");
   system("echo \"11\" > /sys/class/gpio/unexport");
   system("echo \"12\" > /sys/class/gpio/unexport");
}
int read_log2(char *s1, char *s2)
/* size of s1 and s2 >=17 */
/* input only hazimeno 16 ko made,  LCD ga 1 Line 16 ko dakara */
{
    int l0,i;
    FILE *fp0;
#define LINE_MAX0 256
    char buf0[LINE_MAX0];
    s1[0]=0x00; s2[0]=0x00;
    if( (fp0 = fopen("/var/log/radio.log", "rt"))== NULL)
    {
     return(-1);
    }
    if( fgets(buf0, (LINE_MAX0-1), fp0) == NULL)
    {
      fclose(fp0);
      return(-1);
    }
    else
    {
       l0=strlen(buf0);
       if(l0>0) l0--;  /* delete last character LF ? */
       for(i=0;i < 16;++i)
       {
          if(i>=l0) break;
          if( buf0[i] != 0x00)
          {
            s1[i]=buf0[i];  s1[i+1]=0x00;
          }
       }
    }
    if( fgets(buf0, (LINE_MAX0-1), fp0) == NULL)
    {
      fclose(fp0);
      return(-1);
    }
    else
    {
       l0=strlen(buf0);
       if(l0>0) l0--;  /* delete last character LF ? */
       for(i=0;i < 16;++i)
       {
          if(i>=l0) break;
          if( buf0[i] != 0x00)
          {
            s2[i]=buf0[i];  s2[i+1]=0x00;
          }
       }
    }
    fclose(fp0);
    return(0);
}
int main(int argc, char *argv[])
{
  int xloop,i;
  int fd0, fd0i;
  int i2c_stat,disp_stat;
  char in0[3], filename0[64];
  int  mpc_stat,down_stat;
  char s1[20],s2[20];
#define ON_STAT 1
#define OFF_STAT 0
#define MPC_IDLE 0
#define MPC_PLAY 1
#define MPC_NEXT 2
#define MPC_PREV 3
#define MPC_STOP 4
#define MPC_OFF  5
#define X_SHUTDOWN 9
  disp_stat=MPC_IDLE;
  mpc_stat= OFF_STAT;
  down_stat= OFF_STAT;
  /* initialize */
  system("echo \"7\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio7/direction");
  system("echo \"8\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio8/direction");
  system("echo \"9\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio9/direction");
  system("echo \"10\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio10/direction");
  system("echo \"1\" > /sys/class/gpio/gpio10/value");
  system("echo \"11\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio11/direction");
  system("echo \"0\" > /sys/class/gpio/gpio11/value");
  system("echo \"25\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio25/direction");
   /* When start up, check gpio25 state */
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      if( in0[0] == '0')  /* This program finish */
      {
         close(fd0);
         close_gpio();
         exit(0);
      }
    }
    close(fd0);
    /* I2C initilze */
    fd0i=i2c_portopen();
    if( fd0i  < 0) { i2c_stat= OFF_STAT; }
    else
    {
      i2c_stat= ON_STAT;
      init_lcd(fd0i);
      test_lcd_puts("START..."," ",fd0i);
    }
do
/* for(xloop=0;xloop < 15;++xloop) */
{
    /* printf("xloop= %d\n",xloop); */ /* xloop++; */
    fd0=open("/sys/class/gpio/gpio7/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio7= %c \n", in0[0]); */
      if( in0[0]=='0')
      {
        switch(mpc_stat)
        {
           case  ON_STAT:
             system("mpc stop > /var/log/radio.log");
             mpc_stat=OFF_STAT; disp_stat=MPC_STOP;
             system("echo \"0\" > /sys/class/gpio/gpio11/value");
             break;
          case   OFF_STAT:
             system("mpc play > /var/log/radio.log");
             mpc_stat=ON_STAT;  disp_stat=MPC_PLAY;
             system("echo \"1\" > /sys/class/gpio/gpio11/value");
             break;
        }
      }
    }
    close(fd0);
   fd0=open("/sys/class/gpio/gpio8/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio8= %c \n", in0[0]); */
      if((in0[0]=='0') && (mpc_stat == ON_STAT))
      {
        disp_stat=MPC_NEXT;
        system("mpc next > /var/log/radio.log");
      }
    }
    close(fd0);
     fd0=open("/sys/class/gpio/gpio9/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio9= %c \n", in0[0]); */
      if( (in0[0] == '0') && ( mpc_stat == ON_STAT))
      {
          disp_stat=MPC_PREV;
          system("mpc prev > /var/log/radio.log");
      }
    }
    close(fd0);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio25= %c \n", in0[0]); */
      if( in0[0] == '0') {  down_stat= ON_STAT;  disp_stat=X_SHUTDOWN; }
    }
    close(fd0);
   /* wait half  second */
   usleep(500000);
   /* display control */
   if(i2c_stat == ON_STAT)
   {
    switch( disp_stat )
    {
     case MPC_PLAY:
     case MPC_NEXT:
     case MPC_PREV:
          read_log2(s1,s2);
          test_lcd_puts(s1,s2,fd0i);
          disp_stat=MPC_IDLE;
          break;
     case MPC_STOP:
          test_lcd_puts("MPC STOP"," ",fd0i);
          disp_stat=MPC_IDLE;
          break;
     case X_SHUTDOWN:
          test_lcd_puts("SHUTDOWN..."," ",fd0i);
          disp_stat=MPC_IDLE;
          break;
    }
   }
 }  while(down_stat == OFF_STAT);  /* end of xloop */
   if( mpc_stat == ON_STAT)
   {
     mpc_stat=OFF_STAT;
     system("mpc stop > /var/log/radio.log");
     system("echo \"0\" > /sys/class/gpio/gpio11/value");
   }
    /* check down request two seconds more time */
    /*
    sleep(2);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      */
      /* printf("gpio25= %c \n", in0[0]); */
      /*
      if((down_stat == ON_STAT) && (in0[0]=='0')) {}
      else { down_stat=OFF_STAT; }
    }
    close(fd0);
    */
   system("echo \"0\" > /sys/class/gpio/gpio10/value");
    /* close gpio */
    close_gpio();
    /* close i2c */
    if(i2c_stat == ON_STAT)
    {  
       sleep(1);
       test_lcd_puts(" "," ",fd0i);
       i2c_portclose(fd0i);
    }
   if(down_stat == ON_STAT)
   {  system("shutdown -h now "); }
}



以下は、オフタイマー機能を付けたもの。 mpc stop中に、prev(-10分)とnext(+10分)を使ってshutdownまでの時間を設定する。
メインループの回数をカウントしているだけなので、時間の精度はアバウトである。
キースイッチを0.5秒間隔で読み込んでいるので、キーの操作感が今一である。
$ cat /home/pi/radio/key_in.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/i2c-dev.h>
/* proto type declare for I2C and LCD display */
int i2c_portopen();
int i2c_write(int rs0, unsigned char db0, int fd0);
int i2c_portclose(int fd0);
int init_lcd(int fd0);
int test_lcd_puts(unsigned char *s1, unsigned char *s2, int fd0);
/* #define USE_PRINTF 1 */
/* In Raspberry PI, I2C-0 0x00-0x77     */
/*                  I2C-1 0x78-         */
/* This LCD address 0x7c is bigger than 0x77 */
/* So, this LCD i2c map to I2C-1 0x3e   */
#define DEVICE_ADDR2 0x3e  /* 0x7c /* 0x0111 1100 */
int i2c_portclose(int fd0)
{
    close(fd0);
    return(0);
}
int i2c_portopen()
{
   int fd0;
   /* i2c port open */
   if( (fd0 = open("/dev/i2c-1", O_RDWR))  < 0)
   {
#ifdef USE_PRINTF
     printf("error: cannot open i2c-0 \n");
#endif
     return(-1);
   }
   /* set slave address */
   if(ioctl(fd0, I2C_SLAVE, DEVICE_ADDR2)  < 0)
   {
#ifdef USE_PRINTF
     printf("error: cannot access slave \n");
#endif
     close(fd0);
     return(-1);
   }
   return(fd0);
}
int i2c_write1(int rs0, unsigned char db0, int fd0)
{
      int len;
      unsigned char send0[4];
      if( rs0 == 0) send0[0]= 0x00; /* rs=0 cmd */
      else         send0[0]= 0x40; /* rs=1 data */
      send0[1] = db0;
      len=2;
     if ( (write(fd0, send0, len)) != len)
     {
#ifdef USE_PRINTF
       printf("error: i2C_write1\n");
#endif
       return(-1);
     }
     return(0);
}
int test_lcd_puts(unsigned char *s1, unsigned char *s2, int fd0)
{
        int ack;
        ack=i2c_write1(0, 0x38, fd0);  /* //0x0011 1000); // function set // function set interface data 8bits, number line 2 */
        usleep(1000);
        ack=i2c_write1(0, 0x0c, fd0);   /* ///0x0000 1100); // Display On */
        usleep(1000);
        i2c_write1(0, 0x01, fd0);  /* ///0x00000001); // Clear Display   set ddram address */
        usleep(2000);
        /* display character on line 1 */
        while(*s1 != 0x00){
                ack=i2c_write1(1, *s1, fd0);    /* write ddram */
                s1++;
                usleep(1000);
        }
        /* move cursor to line 2
        ack=i2c_write1(0,0xc0, fd0);   /* ///0x11000000);       // ADDR=0x40  set ddram address */
        usleep(1000);
        /* two times access ! */
        ack=i2c_write1(0, 0xc0, fd0);
        usleep(1000);
        while(*s2 != 0x00){
               ack=i2c_write1(1, *s2, fd0);    /* write ddram */
               s2++;
               usleep(1000);
        }
        return 0;
}
int init_lcd(int fd0)
{
   int ack;
   /* lcd initize */
    ack=i2c_write1(0, 0x38, fd0);  /* function set interface data 8bits, number line 2, double height font */
    usleep(1000);
    ack=i2c_write1(0, 0x39, fd0);  /* function set Instruction table select With interface data 8bits, number line 2 */
    usleep(1000);
    ack=i2c_write1(0, 0x14, fd0);  /* interval osc 1/5bias, ajust internal OSC freq */
    usleep(1000);
    ack=i2c_write1(0, 0x70, fd0 );  /* | (contrast & 0x0f)); // contrast Low */
    usleep(1000);
    /* change contast from 0x5f to 0x5e */
    ack=i2c_write1(0, 0x5e, fd0);  /*  | ((contrast >> 4) & 0x03)); // Icon Display On, booster circuit on, contast High */
    usleep(1000);
    ack=i2c_write1(0, 0x6c, fd0);  /* 0x0110 1100); // follower control, on */
    usleep(300000);
    return(0);
}
void close_gpio()
{
/* close */
   system("echo \"7\" > /sys/class/gpio/unexport");
   system("echo \"8\" > /sys/class/gpio/unexport");
   system("echo \"9\" > /sys/class/gpio/unexport");
   system("echo \"10\" > /sys/class/gpio/unexport");
   system("echo \"11\" > /sys/class/gpio/unexport");
   system("echo \"12\" > /sys/class/gpio/unexport");
}
int read_log2(char *s1, char *s2)
/* size of s1 and s2 >=17 */
/* input only hazimeno 16 ko made,  LCD ga 1 Line 16 ko dakara */
{
    int l0,i;
    FILE *fp0;
#define LINE_MAX0 256
    char buf0[LINE_MAX0];
    s1[0]=0x00; s2[0]=0x00;
    if( (fp0 = fopen("/var/log/radio.log", "rt"))== NULL)
    {
     return(-1);
    }
    if( fgets(buf0, (LINE_MAX0-1), fp0) == NULL)
    {
      fclose(fp0);
      return(-1);
    }
    else
    {
       l0=strlen(buf0);
       if(l0>0) l0--;  /* delete last character LF ? */
       for(i=0;i < 16;++i)
       {
          if(i>=l0) break;
          if( buf0[i] != 0x00)
          {
            s1[i]=buf0[i];  s1[i+1]=0x00;
          }
       }
    }
    if( fgets(buf0, (LINE_MAX0-1), fp0) == NULL)
    {
      fclose(fp0);
      return(-1);
    }
    else
    {
       l0=strlen(buf0);
       if(l0>0) l0--;  /* delete last character LF ? */
       for(i=0;i < 16;++i)
       {
          if(i>=l0) break;
          if( buf0[i] != 0x00)
          {
            s2[i]=buf0[i];  s2[i+1]=0x00;
          }
       }
    }
    fclose(fp0);
    return(0);
}
int main(int argc, char *argv[])
{
  int xloop,i;
  int fd0, fd0i;
  int i2c_stat,disp_stat;
  char in0[3], filename0[64];
  int  mpc_stat,down_stat;
  char s1[20],s2[20];
  int time0;
#define ON_STAT 1
#define OFF_STAT 0
#define MPC_IDLE 0
#define MPC_PLAY 1
#define MPC_NEXT 2
#define MPC_PREV 3
#define MPC_STOP 4
#define MPC_OFF  5
#define X_SHUTDOWN 9
  disp_stat=MPC_IDLE;
  mpc_stat= OFF_STAT;
  down_stat= OFF_STAT;
#define TIME_STEP 1200  /* (1/0.5) x 60second  x 10min */
  time0=0;
  /* initialize */
  system("echo \"7\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio7/direction");
  system("echo \"8\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio8/direction");
  system("echo \"9\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio9/direction");
  system("echo \"10\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio10/direction");
  system("echo \"1\" > /sys/class/gpio/gpio10/value");
  system("echo \"11\" > /sys/class/gpio/export");
  system("echo \"out\" > /sys/class/gpio/gpio11/direction");
  system("echo \"0\" > /sys/class/gpio/gpio11/value");
  system("echo \"25\" > /sys/class/gpio/export");
  system("echo \"in\" > /sys/class/gpio/gpio25/direction");
   /* When start up, check gpio25 state */
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      if( in0[0] == '0')  /* This program finish */
      {
         close(fd0);
         close_gpio();
         exit(0);
      }
    }
    close(fd0);
    /* I2C initilze */
    fd0i=i2c_portopen();
    if( fd0i  < 0) { i2c_stat= OFF_STAT; }
    else
    {
      i2c_stat= ON_STAT;
      init_lcd(fd0i);
      test_lcd_puts("START..."," ",fd0i);
    }
do
/* for(xloop=0;xloop < 15;++xloop) */
{
    /* printf("xloop= %d\n",xloop); */ /* xloop++; */
    fd0=open("/sys/class/gpio/gpio7/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio7= %c \n", in0[0]); */
      if( in0[0]=='0')
      {
        switch(mpc_stat)
        {
           case  ON_STAT:
             system("mpc stop > /var/log/radio.log");
             mpc_stat=OFF_STAT; disp_stat=MPC_STOP;
             system("echo \"0\" > /sys/class/gpio/gpio11/value");
             break;
          case   OFF_STAT:
             system("mpc play > /var/log/radio.log");
             mpc_stat=ON_STAT;  disp_stat=MPC_PLAY;
             system("echo \"1\" > /sys/class/gpio/gpio11/value");
             break;
        }
      }
    }
    close(fd0);
   fd0=open("/sys/class/gpio/gpio8/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio8= %c \n", in0[0]); */
      if((in0[0]=='0') && (mpc_stat == ON_STAT))
      {
        disp_stat=MPC_NEXT;
        system("mpc next > /var/log/radio.log");
      }
      else if( (in0[0]=='0') && (mpc_stat == OFF_STAT))
      {
         time0 -= TIME_STEP;
         if( time0  < 0) time0=0;
         disp_stat=MPC_STOP;
      }
    }
    close(fd0);
     fd0=open("/sys/class/gpio/gpio9/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio9= %c \n", in0[0]); */
      if( (in0[0] == '0') && ( mpc_stat == ON_STAT))
      {
          disp_stat=MPC_PREV;
          system("mpc prev > /var/log/radio.log");
      }
      else if( (in0[0] == '0') && ( mpc_stat == OFF_STAT))
      {
          time0 += TIME_STEP;
          disp_stat=MPC_STOP;
      }
    }
    close(fd0);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      /* printf("gpio25= %c \n", in0[0]); */
      if( in0[0] == '0') {  down_stat= ON_STAT;  disp_stat=X_SHUTDOWN; }
    }
    close(fd0);
   /* wait half  second */
   usleep(500000);
   /* count down timer */
   if( time0 > 0)  --time0;
   if( time0 == 1) /* When time0 is 1, then enter shutdown */
   {
      down_stat= ON_STAT;
      disp_stat= X_SHUTDOWN;
   }
   /* display control */
   if(i2c_stat == ON_STAT)
   {
    switch( disp_stat )
    {
     case MPC_PLAY:
     case MPC_NEXT:
     case MPC_PREV:
          read_log2(s1,s2);
          test_lcd_puts(s1,s2,fd0i);
          disp_stat=MPC_IDLE;
          break;
     case MPC_STOP:
          if( time0 > 0 )
          {
             sprintf(s2,"OFF TIMER %d", ((time0 / (2 * 60))+1));
             test_lcd_puts("MPC STOP",s2,fd0i);
          }
          sleep(1);
          test_lcd_puts("MPC STOP"," ",fd0i);
          disp_stat=MPC_IDLE;
          break;
     case X_SHUTDOWN:
          test_lcd_puts("SHUTDOWN..."," ",fd0i);
          disp_stat=MPC_IDLE;
          break;
    }
   }
 }  while(down_stat == OFF_STAT);  /* end of xloop */
   if( mpc_stat == ON_STAT)
   {
     mpc_stat=OFF_STAT;
     system("mpc stop > /var/log/radio.log");
     system("echo \"0\" > /sys/class/gpio/gpio11/value");
   }
    /* check down request two seconds more time */
    /*
    sleep(2);
    fd0=open("/sys/class/gpio/gpio25/value",O_RDWR);
    if( fd0 < 0)
    { close_gpio(); exit(0);}
    else
    {
      read(fd0,in0,2);
      */
      /* printf("gpio25= %c \n", in0[0]); */
      /*
      if((down_stat == ON_STAT) && (in0[0]=='0')) {}
      else { down_stat=OFF_STAT; }
    }
    close(fd0);
    */
   system("echo \"0\" > /sys/class/gpio/gpio10/value");
    /* close gpio */
    close_gpio();
    /* close i2c */
    if(i2c_stat == ON_STAT)
    {
       sleep(1);
       test_lcd_puts(" "," ", fd0i);
       i2c_portclose(fd0i);
    }
   if(down_stat == ON_STAT)
   {  system("shutdown -h now "); }
}