slitaz-arm view rpi/tazberry @ rev 214

tazbian: typo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu May 05 17:03:59 2016 +0200 (2016-05-05)
parents e481a1b067df
children
line source
1 #!/bin/sh
2 #
3 # TazBerry - SliTaz Raspberry Pi Config Tool
4 #
5 # Copyright (C) 2014 SliTaz ARM - BSD License
6 # Author: Christophe Lincoln <pankso@slitaz.org>
7 #
8 . /lib/libtaz.sh
9 check_root
11 title="{ TazBerry }"
12 config="/boot/config.txt"
13 tmpdir="/tmp/tazberry"
14 tmp="$tmpdir/$$"
15 height="20"
16 width="72"
18 # Mirror URL
19 rpi_mirror="http://mirror.slitaz.org/arm/rpi/"
21 # Use a tmp directory
22 mkdir -p ${tmpdir}
24 # Small built-in help
25 help() {
26 cat << EOT
28 $(boldify "$(gettext 'Usage:')") $(basename $0) [command|rpi_function]
30 $(boldify "$(gettext 'Commands:')")
31 oclock $(gettext 'Configure overclocking')
32 linux-up $(gettext 'Update SliTaz R-Pi Linux kernel')
33 cam-setup $(gettext 'Configure your Pi to use the Camera')
34 ls-functions $(gettext 'List TazBerry functions')
35 rpi_ $(gettext 'Execute any tazberry_function')
36 testsuite $(gettext 'Run TazBerry testsuite')
38 EOT
39 }
41 #
42 # RPi functions
43 #
45 # This could move to /usr/bin/slitaz-??? or something since it's not
46 # ARM specific
47 rpi_stats() {
48 ip=$(ifconfig | fgrep -A 1 "encap:Ethernet" | fgrep "inet" | cut -d ":" -f 2)
49 mem_total=$(free -m | fgrep "Mem:" | awk '{print $2}')
50 mem_used=$(free -m | fgrep "Mem:" | awk '{print $3}')
51 mem_used_pct=$(( ( ${mem_used} * 100) / ${mem_total} ))
52 cat << EOT
53 $(gettext 'Kernel') : $(uname -snrm)
54 $(gettext 'Uptime') : $(uptime | awk '{print $3}' | sed s"/:/h /" | sed s"/,/min/")
55 $(gettext 'Network IP') : $(echo $ip | awk '{print $1}')
56 $(gettext 'CPU heat') : $(awk '{printf "%3.1f C\n", $1/1000}' /sys/class/thermal/thermal_zone0/temp)
57 $(gettext 'Processes') : $(ps | wc -l)
58 $(gettext 'Memory usage') : ${mem_used_pct}%
59 EOT
60 }
62 rpi_turbo() {
63 if ! fgrep -q 'force_turbo=1' ${config}; then
64 gettext "Enabling Raspberry Pi force turbo..."
65 echo "force_turbo=1" >> ${config}; status
66 else
67 gettext "Disabling Raspberry Pi force turbo..."
68 sed -i '/force_turbo=1/'d ${config}; status
69 fi
70 }
72 # RPi Overclocking
73 rpi_oclock() {
74 cat << EOT
75 none 700MHz ARM 250MHz core 400MHz SDRAM 0 overvolt
76 modest 800MHz ARM 300MHz core 400MHz SDRAM 0 overvolt
77 medium 900MHz ARM 333MHz core 450MHz SDRAM 2 overvolt
78 high 950MHz ARM 450MHz core 450MHz SDRAM 6 overvolt
79 turbo 1000MHz ARM 500MHz core 500MHz SDRAM 6 overvolt
80 EOT
81 }
83 # Update custom Raspberry Pi Kernel
84 linux_update() {
85 cache="/var/cache/tazpkg"
86 current="/var/lib/tazpkg/installed/linux"
87 {
88 echo "XXX" && echo 20
89 echo -e "\n$(gettext 'Raspberry Pi kernel update')"
90 echo "XXX" && sleep 1
91 mkdir -p ${cache}
92 rm -f ${cache}/linux-version.txt
94 # Last version
95 echo "XXX" && echo 40
96 echo -e "\n$(gettext 'Fetching latest Kernel version string...')"
97 echo "XXX" && sleep 1
98 if busybox wget -q -s ${rpi_mirror}/packages/last-linux.txt; then
99 wget -q ${rpi_mirror}/packages/last-linux.txt \
100 -O ${cache}/linux-version.txt || exit 1
101 else
102 echo "XXX" && echo 100
103 echo -e "\n$(gettext 'Mirror is unreachable')"
104 echo "XXX" && sleep 1
105 exit 1
106 fi
107 kvers=$(cat $cache/linux-version.txt)
108 pkg="linux-${kvers}-arm.tazpkg"
110 echo "XXX" && echo 50
111 echo -e "\n$(gettext 'Last Kernel version:') $kvers"
112 echo "XXX" && sleep 2
113 rm -rf ${cache}/linux-${kvers}*
115 # Download
116 echo "XXX" && echo 60
117 echo -e "\n$(gettext 'Fetching latest Linux package...')"
118 echo "XXX"
119 wget -q ${rpi_mirror}/packages/${pkg} -O ${cache}/${pkg} || exit 1
121 # Install
122 echo "XXX" && echo 80
123 echo -e "\n$(gettext 'Installing:') ${pkg}"
124 echo "XXX"
125 cd ${cache}
126 spk-add ${pkg} --forced >/dev/null 2>&1 || exit 1
128 echo "XXX" && echo 100
129 echo -e "\n$(gettext 'Cleaning installing files...')"
130 echo "XXX" && sleep 2
131 rm -f ${cache}/linux-version.txt
133 } | dialog --title "{ Kernel Update }" \
134 --gauge "" 8 ${width} 0
135 # Reboot ?
136 reboot_box \
137 "$(gettext 'You must reboot to use your new Linux Kernel.')"
138 }
140 # Unset overclocking
141 unset_oclock() {
142 debug "unset_oclock"
143 sed -i \
144 -e '/# Overclocking.*/'d \
145 -e '/arm_freq=/'d \
146 -e '/core_freq=/'d \
147 -e '/sdram_freq=/'d \
148 -e '/over_voltage=/'d ${config}
149 # Remove if 3 empty lines follows
150 sed -i '1N;N;/^\n\n$/d;P;D' ${config}
151 }
153 # Set overclocking: set_oclock [mode]
154 set_oclock() {
155 debug "set_oclock $1"
156 case "$1" in
157 none)
158 arm_freq=700
159 core_freq=250
160 sdram_freq=400
161 over_voltage=0 ;;
162 modest)
163 arm_freq=800
164 core_freq=300
165 sdram_freq=400
166 over_voltage=0 ;;
167 medium)
168 arm_freq=900
169 core_freq=333
170 sdram_freq=450
171 over_voltage=2 ;;
172 high)
173 arm_freq=950
174 core_freq=450
175 sdram_freq=450
176 over_voltage=6 ;;
177 turbo)
178 arm_freq=1000
179 core_freq=500
180 sdram_freq=500
181 over_voltage=6 ;;
182 esac
183 unset_oclock
184 cat >> ${config} << EOT
185 # Overclocking Settings
186 arm_freq=$arm_freq
187 core_freq=$core_freq
188 sdram_freq=$sdram_freq
189 over_voltage=$over_voltage
191 EOT
192 }
194 tazberry_back() {
195 gettext "Press ENTER to go back to TazBerry"; read
196 }
198 #
199 # GUI Functions
200 #
202 # Usage: msg_box "title" "message"
203 msg_box() {
204 dialog --title "{ $1 }" --cr-wrap \
205 --ok-label "TazBerry" --msgbox "\n$2" ${height} ${width}
206 }
208 # Usage: text_box "file"
209 text_box() {
210 local file="$1"
211 if [ -f "$file" ]; then
212 dialog --cr-wrap \
213 --title "{ $(gettext 'Viewing file:') $file }" \
214 --extra-button \
215 --extra-label "Edit file" \
216 --ok-label "TazBerry" \
217 --textbox ${file} ${height} ${width}
218 else
219 dialog --title "$title" \
220 --msgbox "\nERROR: $file does not exist" 10 ${width}
221 fi
222 # Handle options
223 case "$?" in
224 3) editor ${file} ;;
225 esac
226 }
228 # Usage: tail_box "file"
229 tail_box() {
230 dialog --title "{ $(gettext 'Watching file:') $2 }" \
231 --tailbox "$1" ${height} ${width}
232 }
234 # Usage: reboot_box "Message..."
235 reboot_box() {
236 dialog --title "$title" --yesno "\n${2}\n\n
237 Do You wish to reboot now ?" \
238 ${height} ${width}
239 [ "$?" == 0 ] && reboot
240 }
242 # Packages info and spk-up button
243 pkgs_box() {
244 out="$tmpdir/spk-info.txt"
245 spk info --output=raw > ${out}
246 dialog --cr-wrap \
247 --title "{ $(gettext 'Packages') }" \
248 --extra-button \
249 --extra-label "$(gettext 'Upgrade')" \
250 --ok-label "TazBerry" \
251 --textbox "$out" ${height} ${width}
252 # Handle options
253 case "$?" in
254 3) spk-up && tazberry_back ;;
255 esac
256 }
258 # Overclocking info TODO: button to setup
259 oclock_box() {
260 mode=$(fgrep arm_freq ${config} | cut -d '=' -f 2)
261 case "$mode" in
262 700) mode="none" ;;
263 800) mode="modest" ;;
264 900) mode="medium" ;;
265 950) mode="high" ;;
266 1000) mode="turbo" ;;
267 esac
268 dialog \
269 --clear --colors \
270 --extra-button \
271 --title "{ Overclocking }" \
272 --ok-label "TazBerry" \
273 --extra-label "Set Overclock" \
274 --cancel-label "Unset Oclock" \
275 --menu "\nSet Raspberry Pi overclocking, current setting: \Zb\Z2$mode" \
276 ${height} ${width} 14 \
277 "none" "700MHz ARM 250MHz core 400MHz SDRAM 0 overvolt" \
278 "modest" "800MHz ARM 300MHz core 400MHz SDRAM 0 overvolt" \
279 "medium" "900MHz ARM 333MHz core 450MHz SDRAM 2 overvolt" \
280 "high" "950MHz ARM 450MHz core 450MHz SDRAM 6 overvolt" \
281 "turbo" "1000MHz ARM 500MHz core 500MHz SDRAM 6 overvolt" 2>${tmp}
282 retval="$?"
283 mode=$(cat $tmp)
284 case "$retval" in
285 0) continue ;;
286 1) unset_oclock ;;
287 3) set_oclock "$mode" ;;
288 255) rm -rf ${tmpdir} && exit 0 ;;
289 esac
290 }
292 gpu_mem_box() {
293 dialog \
294 --clear \
295 --title "$title" \
296 --ok-label "Set mem" \
297 --cancel-label "TazBerry" \
298 --menu "\nSet the memory allocated to the GPU (in MB)" \
299 ${height} ${width} 14 \
300 "16" "For server, NAS, base system without X" \
301 "32" "For very small TFT and TV display" \
302 "64" "Default value, fine for low latency desktop" \
303 "128" "For a faster desktop, using a video player" \
304 "256" "For media center, GL/3D applications" 2>${tmp}
305 mem=$(cat $tmp)
306 sed -i \
307 -e s"/#gpu_mem=.*/gpu_mem=$mem/" \
308 -e s"/gpu_mem=.*/gpu_mem=$mem/" /boot/config.txt
309 }
311 picam_setup() {
312 db="/var/lib/tazpkg/installed"
313 gpu_mem=128
314 pct="0"
315 {
316 # Needed packages
317 for pkg in raspberrypi-vc raspberrypi-boot-x
318 do
319 echo "XXX" && echo $((${pct} + 20))
320 echo -e "\nInstalling: $pkg"
321 echo "XXX"
322 [ -d "$db/$pkg" ] || spk-add ${pkg} >/dev/null 2>&1
323 sleep 1
324 done
326 echo "XXX" && echo 60
327 echo -e "\nSetting up: ${config}"
328 echo "XXX" && sleep 1
329 # 0 to disable camera, 1 to enable it
330 if ! grep -q "start_x=1" ${config}; then
331 cat >> ${config} << EOT
333 # Camera settings
334 start_x=1
336 EOT
337 fi
338 # In case it was commented ot disabled
339 sed -i s'/#start_x=.*/start_x=1/' ${config}
340 sed -i s'/start_x=.*/start_x=1/' ${config}
342 # PiCam needs 128 MB of GPU memory
343 echo "XXX" && echo 80
344 echo -e "\nSetting GPU memory to: ${gpu_mem}"
345 echo "XXX" && sleep 1
347 sed -i "s/#gpu_mem=.*/gpu_mem=${gpu_mem}/" ${config}
348 sed -i "s/gpu_mem=.*/gpu_mem=${gpu_mem}/" ${config}
350 echo "XXX" && echo 100
351 echo -e "\nPiCam config done..."
352 echo "XXX" && sleep 1
354 } | dialog --title "{ PiCam Setup }" \
355 --gauge "" 8 ${width} 0
357 # Reboot ?
358 reboot_box \
359 "$(gettext 'SliTaz is configured for the PiCam but you need a reboot to use it.')"
360 }
362 # Breakout bords!
363 boards_box() {
364 dialog \
365 --clear \
366 --title "$title" \
367 --ok-label "Exec" \
368 --cancel-label "TazBerry" \
369 --menu "\nSetup or use a breakout board" ${height} ${width} 14 \
370 "camera" "$(gettext 'Configure your Pi to use a Camera')" \
371 "piglow" "$(gettext 'Setup your Pi to use PiGlow')" \
372 "raspicomm" "$(gettext 'Initialize RaspiComm board')" \
373 "pitft" "$(gettext 'Setup PiTFT touchscreen')" 2>${tmp}
375 # Handle options
376 case "$?" in
377 1|255) continue ;;
378 esac
380 # Handle actions
381 action=$(cat $tmp)
382 case "$action" in
383 camera)
384 picam_setup ;;
385 piglow)
386 for pkg in python-rpi-pyglow wiringpi-piglow; do
387 spk-add ${pkg}
388 done
389 newline
390 echo -n "Do you wish to try the leds [y/N] ? " ; read anser
391 if [ "$anser" == "y" ]; then
392 echo "Enter a value between: 5-255" && pyglow test
393 fi ;;
394 raspicomm)
395 raspicomm init; read -t 4 ;;
396 pitft)
397 pitft-setup
398 echo -n "Press ENTER to continue..."; read ;;
399 esac
400 }
402 # Main Dialog menu
403 main_box() {
404 dialog \
405 --clear \
406 --title "$title" \
407 --ok-label "Exec" \
408 --cancel-label "Quit" \
409 --menu "" ${height} ${width} 14 \
410 "rpi-stats" "$(gettext 'Show some RPi system stats')" \
411 "gpu-mem" "$(gettext 'Set memory split between ARM and GPU')" \
412 "boards" "$(gettext 'Boards settings and tools (Camera, PyGlow)')" \
413 "act-led" "$(gettext 'Turn on/off RPi onboard ACT green led')" \
414 "oclock" "$(gettext 'Overclocking configuration')" \
415 "rpi-turbo" "$(gettext 'Enable or disable RPi turbo on next boot')" \
416 "boot-cmdline" "$(gettext 'View boot args /boot/cmdline.txt')" \
417 "boot-config" "$(gettext 'View config file /boot/config.txt')" \
418 "linux-up" "$(gettext 'Update SliTaz Raspberry Pi Linux Kernel')" \
419 "packages" "$(gettext 'SliTaz packages manager') (spk)" \
420 "config" "$(gettext 'System config (lang, keyboard, wifi)')" \
421 "reboot" "$(gettext 'Reboot SliTaz system')" \
422 "halt" "$(gettext 'Halt the Raspberry Pi')" \
423 "quit" "$(gettext 'Exit TazBerry tool')" 2>${tmp}
425 # Handle options
426 case "$?" in
427 1|255) rm -rf ${tmpdir} && exit 0 ;;
428 esac
430 # Handle actions
431 action=$(cat $tmp)
432 case "$action" in
433 rpi-stats)
434 msg_box "RPi Stats" "$(rpi_stats)" ;;
435 gpu-mem)
436 gpu_mem_box ;;
437 boards)
438 boards_box ;;
439 act-led)
440 pileds act ;;
441 oclock)
442 oclock_box ;;
443 rpi-turbo)
444 rpi_turbo && newline
445 tazberry_back ;;
446 boot-cmdline)
447 text_box /boot/cmdline.txt ;;
448 boot-config)
449 text_box ${config} ;;
450 linux-up)
451 linux_update
452 tazberry_back ;;
453 packages)
454 spk-dialog ;;
455 config)
456 slitaz-config ;;
457 reboot)
458 reboot ;;
459 halt)
460 poweroff ;;
461 quit)
462 rm -rf ${tmpdir} && exit 0 ;;
463 esac
464 }
466 # Progress bar testsuite
467 gauge_box() {
468 { for i in $(seq 1 100); do
469 echo $i
470 sleep 0.01
471 done
472 echo 100; } | dialog --title "{ TazBerry }" \
473 --gauge "Work in progress..." 6 ${width} 0
474 }
476 #
477 # Handle commands
478 #
480 case "$1" in
481 oclock)
482 oclock_box ;;
484 linux-up)
485 linux_update ;;
487 cam-setup)
488 picam_setup ;;
490 ls-functions)
491 newline
492 boldify "$(gettext 'TazBerry functions list')"
493 separator
494 grep "^rpi_*" $0 | awk '{print " ", $1}'
495 newline ;;
497 testsuite)
498 gauge_box
499 msg_box "Testsuite" "Test message"
500 rpi_stats
501 newline ;;
503 rpi_*)
504 # Execute functions
505 $@ ;;
507 "")
508 # No args: display Ncurses dialog
509 while true; do
510 main_box
511 done ;;
513 *|*help|-h)
514 # Display help
515 help ;;
516 esac
518 # Clean exit
519 rm -rf ${tmpdir}
520 exit 0