forked from geekworm-com/x708
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-ubuntu.sh
180 lines (140 loc) · 4.42 KB
/
install-ubuntu.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#x708 Powering on /reboot /full shutdown through hardware
#!/bin/bash
#sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
SHUTDOWN=5
REBOOTPULSEMINIMUM=200
REBOOTPULSEMAXIMUM=600
echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
BOOT=12
echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
echo "X708 Shutting down..."
while [ 1 ]; do
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
if [ $shutdownSignal = 0 ]; then
/bin/sleep 0.2
else
pulseStart=$(date +%s%N | cut -b1-13)
while [ $shutdownSignal = 1 ]; do
/bin/sleep 0.02
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
echo "X708 Shutting down", SHUTDOWN, ", halting Rpi ..."
sudo poweroff
exit
fi
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
done
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then
echo "X708 Rebooting", SHUTDOWN, ", recycling Rpi ..."
sudo reboot
exit
fi
fi
done' > /etc/x708pwr.sh
sudo chmod +x /etc/x708pwr.sh
#sudo sed -i '$ i /etc/x708pwr.sh &' /etc/rc.local
#X708 full shutdown through Software
#!/bin/bash
#sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
BUTTON=13
echo "$BUTTON" > /sys/class/gpio/export;
echo "out" > /sys/class/gpio/gpio$BUTTON/direction
echo "1" > /sys/class/gpio/gpio$BUTTON/value
SLEEP=${1:-4}
re='^[0-9\.]+$'
if ! [[ $SLEEP =~ $re ]] ; then
echo "error: sleep time not a number" >&2; exit 1
fi
echo "X708 Shutting down..."
/bin/sleep $SLEEP
#restore GPIO 13
echo "0" > /sys/class/gpio/gpio$BUTTON/value
' > /usr/local/bin/x708softsd.sh
sudo chmod +x /usr/local/bin/x708softsd.sh
#X708 Battery voltage & precentage reading
#!/bin/bash
#sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
CUR_DIR=$(pwd)
echo '#!/usr/bin/env python3
import struct
import smbus
import sys
import time
def readVoltage(bus):
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
print ("******************")
print ("Voltage:%5.2fV" % readVoltage(bus))
print ("Battery:%5i%%" % readCapacity(bus))
if readCapacity(bus) == 100:
print ("Battery FULL")
if readCapacity(bus) < 20:
print ("Battery LOW")
print ("******************")
time.sleep(2)
' > ${CUR_DIR}/x708bat.py
sudo chmod +x ${CUR_DIR}/x708bat.py
#X708 AC Power loss / power adapter failture detection
#!/bin/bash
#sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python3
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
def my_callback(channel):
if GPIO.input(6): # if port 6 == 1
print ("---AC Power Loss OR Power Adapter Failure---")
else: # if port 6 != 1
print ("---AC Power OK,Power Adapter OK---")
GPIO.add_event_detect(6, GPIO.BOTH, callback=my_callback)
print ("1.Make sure your power adapter is connected")
print ("2.Disconnect and connect the power adapter to test")
print ("3.When power adapter disconnected, you will see: AC Power Loss or Power Adapter Failure")
print ("4.When power adapter reconnected, you will see: AC Power OK, Power Adapter OK")
input("Testing Started")
' > ${CUR_DIR}/x708pld.py
sudo chmod +x ${CUR_DIR}/x708pld.py
#####################################
echo '#!/bin/bash
#!/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
/etc/x708pwr.sh &
exit 0
' > /etc/rc.local
sudo chmod +x /etc/rc.local
# save these shell to x708.sh
SHELL_FILE=/etc/profile.d/x708.sh
if [ -e $SHELL_FILE ]; then
sudo rm $SHELL_FILE -f
fi
echo "alias x708off='sudo /usr/local/bin/x708softsd.sh'" > ${SHELL_FILE}