pepopifaced

pepopifaced is for daemon contorl rasberry_pi to GPIO

pepopifacedとはrasberry_pi + PiFaceのdaemonで、Remote-Handのコアプログラムです。

定周期で入出力16ビットHIGH/LOW状態、IRkitIPアドレス、IRkitデータ、temperデータをjsonデータ作成します

入力8ビットデータの変化(event)LOW→HIGH、HIGH→LOWを捕らえ予め登録しているscriptを起動します。

o 2013.9.17 ファーストリリース

o 2014.12.21 pepopifaceバージョンUPに伴う変更

o 2017.7.29 jsonデータ作成を別daemonへ移動させ、出来るだけevent処理の高速化、温湿度とAI値のスライス判定機能追加

  1. #!/bin/bash
  2. # licence GPLv2 ; this scripts designed by Isamu.Yamauchi 2015.2.20 update 2017.7.29
  3. # Scan piface & CPU Temp ,USB Temper, I2C Hum&Temp for rasberry pi.
  4. # pepopifaced
  5. DIOCTL="/usr/local/bin/pepopiface 8"
  6. DIOCTL1ST="/usr/local/bin/pepopiface"
  7. DIR=/www/remote-hand/tmp
  8. DIORD=$DIR/.di_read_data
  9. tDIORD=$DIR/.di_read_data.tmp
  10. DIOWD=$DIR/.do_write_data
  11. tDIOWD=$DIR/.do_write_data.tmp
  12. DIR=/www/remote-hand/tmp
  13. TCOSWD=$DIR/.tocos_write_data
  14. TCOSRD=$DIR/.tocos_read_data
  15. ALIAS_DI=$DIR/.alias_di
  16. DIREADJSON=/www/remote-hand/.di_read_data.json
  17. USR_BIN=/usr/bin/dio
  18. EXEC_CMD=/usr/local/bin/pepodiodexec
  19. error(){
  20. [ -e $tDIORD ] && rm -f $tDIORD
  21. [ -e $DIORD ] && rm -f $DIORD
  22. [ -e $DIOWD ] && rm -f $DIOWD
  23. exit 0
  24. }
  25. exec_cmd() {
  26. # gpio read & High or Low exec command
  27. # exec_cmd ch low or high
  28. # echo ${USR_BIN}${1}${2} >/dev/tty1
  29. if [ -e ${USR_BIN}${1}${2} ];then
  30. $EXEC_CMD ${USR_BIN}${1}${2} >/dev/null 2>&1
  31. fi
  32. }
  33. trap error SIGTERM SIGHUP SIGKILL SIGINT
  34. [ -e $tDIORD ] && rm -f $tDIORD
  35. [ -e $DIORD ] && rm -f $DIORD
  36. [ -e $DIOWD ] && rm -f $DIOWD
  37. [ -e $tDIOWD ] && rm -f $tDIOWD
  38. $DIOCTL1ST >/dev/null 2>&1
  39. for n in 0 1 2 3 4 5 6 7 8 9 10; do
  40. old_di[$n]="-1" ; new_di[$n]="-1"
  41. done
  42. while true;do
  43. unset piface_ip di do
  44. for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31; do old[$i]="-1" ;done
  45. [ -e $ALIAS_DI ] && . $ALIAS_DI
  46. if [ -z $piface_ip ];then
  47. LOOPTIME=1000
  48. # gpio read data initialize
  49. RD="-1"
  50. RD=`$DIOCTL 2>&1`
  51. if [ "$RD" != "-1" ];then
  52. for i in 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768; do
  53. case "$i" in
  54. 1) tRD="$(($((0x$RD & 1)) / 1))" ;j=0 ;;
  55. 2) tRD="$(($((0x$RD & 2)) / 2))" ;j=1 ;;
  56. 4) tRD="$(($((0x$RD & 4)) / 4))" ;j=2 ;;
  57. 8) tRD="$(($((0x$RD & 8)) / 8))" ;j=3 ;;
  58. 16) tRD="$(($((0x$RD & 16)) / 16))" ;j=4 ;;
  59. 32) tRD="$(($((0x$RD & 32)) / 32))" ;j=5 ;;
  60. 64) tRD="$(($((0x$RD & 64)) / 64))" ;j=6 ;;
  61. 128) tRD="$(($((0x$RD & 128)) / 128))" ;j=7 ;;
  62. 256) tRD="$(($((0x$RD & 256)) / 256))" ;j=8 ;;
  63. 512) tRD="$(($((0x$RD & 512)) / 512))" ;j=9 ;;
  64. 1024) tRD="$(($((0x$RD & 1024)) / 1024))" ;j=10 ;;
  65. 2048) tRD="$(($((0x$RD & 2048)) / 2048))" ;j=11 ;;
  66. 4096) tRD="$(($((0x$RD & 4096)) / 4096))" ;j=12 ;;
  67. 8192) tRD="$(($((0x$RD & 8192)) / 8192))" ;j=13 ;;
  68. 16384) tRD="$(($((0x$RD & 16384)) / 16384))" ;j=14 ;;
  69. 32768) tRD="$(($((0x$RD & 32768)) / 32768))" ;j=15 ;;
  70. esac
  71. old[$j]=$tRD
  72. done
  73. else
  74. rm -f $DIORD $DIOWD
  75. fi
  76. else
  77. LOOPTIME=5000
  78. if [ -e $DIORD -a -e $DIOWD ];then
  79. . $DIORD
  80. . $DIOWD
  81. for n in 0 1 2 3 4 5 6 7; do
  82. [ ! -z ${di[$n]} ] && old[$n]=${di[$n]}
  83. [ ! -z ${do[$n]} ] && old[$n]=${do[$n]}
  84. done
  85. fi
  86. fi
  87. #Tocos di set
  88. if [ -e ${TCOSRD} ];then
  89. unset TI1 TI2 TI3 TI4 AI1 AI2 AI3 AI4
  90. . ${TCOSRD}
  91. [ ! -z "$TI1" ] && old[16]=$TI1 || old[16]="-1"
  92. [ ! -z "$TI2" ] && old[17]=$TI2 || old[17]="-1"
  93. [ ! -z "$TI3" ] && old[18]=$TI3 || old[18]="-1"
  94. [ ! -z "$TI4" ] && old[19]=$TI4 || old[19]="-1"
  95. [ ! -z "$AI1" ] && old[20]=$AI1 || old[20]="-1"
  96. [ ! -z "$AI2" ] && old[21]=$AI2 || old[21]="-1"
  97. [ ! -z "$AI3" ] && old[22]=$AI3 || old[22]="-1"
  98. [ ! -z "$AI4" ] && old[23]=$AI4 || old[23]="-1"
  99. fi
  100. #i2c Temperature,Humidity & CPU Temperature & USB Temperature convert to digital set
  101. if [ -e ${DIREADJSON} ];then
  102. CPU_TEMP=`cat ${DIREADJSON} |awk '/cpu_temp/{gsub(/"|cpu_temp|℃|:/,"",$0);split($1,J,".");printf J[1]}'`
  103. if [ ! -z "$CPU_TEMP" -a ! -z "${slice_ai[16]}" ];then
  104. [ $CPU_TEMP -lt ${slice_ai[16]} ] && old[24]=0 || old[24]=1
  105. fi
  106. USB_TEMP=`cat ${DIREADJSON} |awk '/temper/{gsub(/"/,"",$0);gsub(/,/," ",$0);gsub(/temper,:|℃/,"",$0);split($0,I," ");split(I[2],J,".");printf J[1]}'`
  107. if [ ! -z "$USB_TEMP" -a ! -z "${slice_ai[17]}" ];then
  108. [ $USB_TEMP -lt ${slice_ai[17]} ] && old[25]=0 || old[25]=1
  109. fi
  110. I2C_TEMP=`cat ${DIREADJSON} |awk '/i2ctemp/{gsub(/"/,"",$0)gsub(/,/," ",$0);gsub(/i2ctemp,:|℃|%/,"",$0);split($0,I," ");split(I[2],J,".");printf J[1]}'`
  111. if [ ! -z "$I2C_TEMP" -a ! -z "${slice_ai[18]}" ];then
  112. [ $I2C_TEMP -lt ${slice_ai[18]} ] && old[26]=0 || old[26]=1
  113. fi
  114. I2C_HUM=`cat ${DIREADJSON} |awk '/i2ctemp/{gsub(/"/,"",$0)gsub(/,/," ",$0);gsub(/i2ctemp,:|℃|%/,"",$0);split($0,I," ");split(I[3],J,".");printf J[1]}'`
  115. if [ ! -z "$I2C_HUM" -a ! -z "${slice_ai[19]}" ];then
  116. [ $I2C_HUM -lt ${slice_ai[19]} ] && old[27]=0 || old[27]=1
  117. fi
  118. fi
  119. #Tocos do set
  120. if [ -e ${TCOSWD} ];then
  121. unset TO1 TO2 TO3 TO4
  122. . ${TCOSWD}
  123. [ ! -z "$TO1" ] && old[28]=$TO1 || old[28]="-1"
  124. [ ! -z "$TO2" ] && old[29]=$TO2 || old[29]="-1"
  125. [ ! -z "$TO3" ] && old[30]=$TO3 || old[30]="-1"
  126. [ ! -z "$TO4" ] && old[31]=$TO4 || old[31]="-1"
  127. fi
  128. di[0]=${old[0]}
  129. di[1]=${old[1]}
  130. di[2]=${old[2]}
  131. di[3]=${old[3]}
  132. di[4]=${old[4]}
  133. di[5]=${old[5]}
  134. di[6]=${old[6]}
  135. di[7]=${old[7]}
  136. di[8]=${old[16]}
  137. di[9]=${old[17]}
  138. di[10]=${old[18]}
  139. for n in 0 1 2 3 4 5 6 7 8 9 10; do
  140. new_di[$n]=${di[$n]}
  141. if [ "${new_di[$n]}" = "-1" -o "${old_di[$n]}" = "-1" ];then
  142. old_di[$n]=${new_di[$n]}
  143. else
  144. if [ "${old_di[$n]}" != "${new_di[$n]}" ];then
  145. if [ "${old_di[$n]}" = "1" ];then
  146. old_di[$n]=${new_di[$n]}
  147. exec_cmd $n low
  148. j=$(($n + 11))
  149. exec_cmd $j low
  150. elif [ "${old_di[$n]}" = "0" ];then
  151. old_di[$n]=${new_di[$n]}
  152. exec_cmd $n high
  153. j=$(($n + 11))
  154. exec_cmd $j high
  155. fi
  156. fi
  157. fi
  158. done
  159. cat>$tDIORD<<END
  160. di[0]=${old[0]}
  161. di[1]=${old[1]}
  162. di[2]=${old[2]}
  163. di[3]=${old[3]}
  164. di[4]=${old[4]}
  165. di[5]=${old[5]}
  166. di[6]=${old[6]}
  167. di[7]=${old[7]}
  168. di[8]=${old[16]}
  169. di[9]=${old[17]}
  170. di[10]=${old[18]}
  171. di[11]=${old[19]}
  172. di[12]=${old[20]}
  173. di[13]=${old[21]}
  174. di[14]=${old[22]}
  175. di[15]=${old[23]}
  176. di[16]=${old[24]}
  177. di[17]=${old[25]}
  178. di[18]=${old[26]}
  179. di[19]=${old[27]}
  180. END
  181. cat>$tDIOWD<<END
  182. do[0]=${old[8]}
  183. do[1]=${old[9]}
  184. do[2]=${old[10]}
  185. do[3]=${old[11]}
  186. do[4]=${old[12]}
  187. do[5]=${old[13]}
  188. do[6]=${old[14]}
  189. do[7]=${old[15]}
  190. do[8]=${old[28]}
  191. do[9]=${old[29]}
  192. do[10]=${old[30]}
  193. do[11]=${old[31]}
  194. END
  195. mv $tDIORD $DIORD ; mv $tDIOWD $DIOWD
  196. chown www-data.www-data $DIORD ; chown www-data.www-data $DIOWD
  197. msleep $LOOPTIME
  198. done