-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotaryDial.sh
executable file
·134 lines (117 loc) · 3.16 KB
/
rotaryDial.sh
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# rotaryDial:
# Read a rotary dial phone on the pi
#
# Wait for the handset to be lifted then monitor the ready pin
# should this change we need then count the number of pulses
# that occur before the ready pin returns to its original state.
# If the handset is hung up we abort and continue to monitor it.
#
# James Connor, November 2012
# input pin numbers
# GPIO 24
readyPin=5
# GPIO 23
pulsePin=4
# GPIO 26
handsetPin=6
# variables
DEBOUNCE=15
WAITING=0
LISTENNOPULSE=1
LISTENPULSE=2
# more variables
state=0
currentTime=$(date +%s%N)
lastStateChangeMillis=$(($currentTime/1000000))
# setup:
# Initialise the GPIO
#######################################################################
setup()
{
echo Setup
for i in $readyPin $pulsePin $handsetPin ; do gpio mode $i in ; done
}
# readAll:
# Read all input pins and display
#######################################################################
readAll()
{
echo "reading..."
for i in $readyPin $pulsePin $handsetPin ; do gpio read $i ; done
}
# waitHandset:
# Wait for the handset to be lifted. Because we have the GPIO
# pin pulled high, we wait for it to go low.
#######################################################################
waitHandset ()
{
echo -n "Waiting for handset ... "
while [ `gpio read $handsetPin` = 1 ]; do
sleep 0.1
done
echo "Handset lifted"
}
# changeState:
# change state to the given state, includes debounce.
# 0=waiting, 1=listen for no pulse, 2=listen pulse.
#######################################################################
changeState ()
{
currentTime=$(date +%s%N)
currentMillis=$(($currentTime/1000000))
# perhaps need to add in wrap around of millis
if [ $(($currentMillis-$lastStateChangeMillis)) > $DEBOUNCE ] ; then
state=$1
lastStateChangeMillis=$currentMillis
return 0
else
return 1
fi
}
# completeDial:
# Wait for the handset to be lifted. Because we have the GPIO
# pin pulled high, we wait for it to go low.
#######################################################################
completeDial ()
{
if changeState $WAITING ; then
echo $(($number-1))
fi
}
#######################################################################
# The main program
# Setup pins then monitor the handsetPin and go from there.
#######################################################################
setup
readAll
while true; do
waitHandset
# someone picked up the handset so lets start watching those pins
while [ `gpio read $handsetPin` = 0 ] ; do
case $state in
# WAITING
0 )
if [ `gpio read $readyPin` = 0 ] && changeState $LISTENNOPULSE ; then
number=0
fi
;;
# LISTENNOPULSE
1 )
if [ `gpio read $readyPin` = 1 ] ; then
completeDial
elif [ `gpio read $pulsePin` = 1 ] ; then
changeState $LISTENPULSE
fi
;;
# LISTENPULSE
2 )
if [ `gpio read $readyPin` = 1 ] ; then
completeDial
elif [ `gpio read $pulsePin` = 0 ] && changeState $LISTENNOPULSE ; then
number=$(($number+1))
fi
;;
esac
done
done