-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.sh
executable file
·1140 lines (1019 loc) · 27.3 KB
/
example.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
################################################################################
#
# Demo script for Advantech Embedded Controller
#
# Copyright (c) Advantech Co., Ltd. All Rights Reserved
#
################################################################################
################################################################################
###### Global Value ######
################################################################################
ADV_DEBUG_INFO="0" # Please change to "0", it only use for debug info print.
################################################################################
###### Global Functions ######
################################################################################
function show_title
{
clear
echo "**********************************************"
echo "** EIOIS200 Example **"
echo "**********************************************"
echo ""
}
# ternary simulates the ternary operator likes:
# C++ : A = B == C ? D : E ;
# to be: $A = `ternary "[ $B == $C ]" ? "$D" : "$E"`
# |<--- $1 --->| $2 $3 $4 $5
function ternary {
if eval "$1" ; then
echo -e "$3"
else
echo -e "$5"
fi
}
################################################################################
###### 1) WDT Sub1 Menu ######
################################################################################
function WdtSub1Menu
{
local SYSFS=/sys/class/watchdog
local wdt
local sel
local i
local delay
# Need wdctl
wdctl > /dev/null 2>&1
if [ $? -ne 0 ]; then
return `echo -p "please install wdctl"`
fi
# find the device name of watchdog#
for i in $(ls -v $SYSFS); do
local dev=`wdctl /dev/${i} 2> /dev/null | grep eiois200`
if [ "$dev" != "" ]; then
wdt=${i}
fi
done
if [ "$wdt" == "" ]; then
return `read -p"Can't find eiois200 watchdog"`
fi
while :
do
local Timeleft=`cat /sys/class/watchdog/$wdt/timeleft`
# Show Menu
sync
show_title
echo "Watchdog"
if [ "$Timeleft" != "0" ]; then
echo -e "\033[41m\033[30m\033[05mSystem reboots in ${Timeleft}(s)\033[0m"
else
echo -e
fi
echo -e "0) Back to Main menu(Stop watchdog)"
echo -e "1) set timeout value:" `cat /sys/class/watchdog/$wdt/timeout`
echo -e "2) Start / Trigger"
echo -e "3) Stop\n"
read -t1 -n1 -p "Enter your choice: " sel
echo -e
# Processes
case $sel in
"0")
echo V > /dev/${wdt}
return 1
;;
"1")
read -p"Reset time (unit: second): " delay
wdctl -s $delay /dev/$wdt
;;
"2")
echo -n 0 > /dev/$wdt
read -p"Watchdog started..." -t1 -n1
;;
"3")
echo -n V > /dev/${wdt}
read -p"Watchdog stopped..." -t1 -n1
;;
*)
continue
;;
esac
done
}
################################################################################
###### 2) HWM Sub1 Menu ######
################################################################################
function HwmSub1Menu
{
local SYSFS=/sys/class/hwmon
local dev
local sel
local file
local hwm_dev=""
local label
# find the hwmon#
for dev in $(ls -v $SYSFS)
do
if [ -e $SYSFS/$dev/name ] &&
[ `cat $SYSFS/$dev/name` == "eiois200_hwmon" ]; then
hwm_dev=$SYSFS/$dev
break
fi
done
if [ $hwm_dev == ""]; then
return `read -p"Can't find eiois200 hwmon device..."`
fi
show_title
while :
do
# Show all hwmon items
echo -e "\033[4;0H"
echo -e "Hardware Monitor"
echo -e "\nVoltage(mV): "
for file in $hwm_dev/in*input; do
if [ -f "$file" ]; then
label=`cat ${file%input}label`
val=`cat $file`
printf "\t%s\t: %d.%02d \n" $label $((val/1000)) $(((val/10) % 100))
fi
done
echo -e "\nTemperature(degree Celsius): "
for file in $hwm_dev/temp*input; do
if [ -f "$file" ]; then
label=`cat ${file%input}label`
val=`cat $file`
printf "\t%s\t: %d.%d \n" $label $((val/1000)) $(((val%1000)/100))
fi
done
echo -e "\nFan speed(rpm): "
for file in $hwm_dev/fan*input; do
if [ -f "$file" ]; then
echo -e "\t`cat ${file%input}label`\t: `cat $file` "
fi
done
echo -e
read -t0.016 -n1 -p "Press any key to leave..."
if [ $? -eq 0 ]; then
return 0
fi
done
}
################################################################################
###### 3) Smart Fan Sub1 Menu ######
################################################################################
function SmartFanSub1Menu
{
local SYSFS=/sys/class/thermal
local names=()
local i=0
local dev
# Search smart fan
for dev in $(ls -v $SYSFS); do
if [ -e $SYSFS/$dev/type ] &&
[ `cat $SYSFS/$dev/type` == "eiois200_fan" ]; then
names[${#names[@]}]=$dev
fi
done
if [ "${#names[@]}" == "0" ]; then
return `read -p"Can't find Smart Fan..."`
fi
while : ; do
local tz_dev=$SYSFS/${names[${i}]}
local mode=`cat $tz_dev/fan_mode`
local temp=`cat $tz_dev/temp`
local dev_lst=()
local sel
local val
# Show menu
for ((j=0 ; j < ${#names[@]} ; j=j+1)); do
dev_lst+=`ternary "[ $j == $i ]" ? "[${names[j]}]" : " ${names[j]} "`
done
show_title
echo -e "Smart Fan Controller"
echo -e
echo -e "0) Back to Main menu"
echo -e " ) Temperature :" $((temp/1000)).$(((temp/100)%10))
echo -e "1) Select Smart Fan : $dev_lst"
echo -e "2) Toggle Fan mode :" $mode
echo -e "3) Manual PWM :" `cat $tz_dev/PWM`
echo -e "4) Low Stop Limit :" $((`cat $tz_dev/trip_point_2_temp` / 1000))
echo -e "5) Low Limit :" $((`cat $tz_dev/trip_point_1_temp` / 1000))
echo -e "6) High Limit :" $((`cat $tz_dev/trip_point_0_temp` / 1000))
echo -e "7) Max PWM :" `cat $tz_dev/cdev0/max_state`
echo -e "8) Min PWM :" `cat $tz_dev/cdev1/max_state`
echo -e
read -t0.1 -n1 -p"Enter your choice: " sel
echo -e
# Processes
case $sel in
"0")
return 0;;
"1")
i=$(((i + 1) % ${#names[@]}));;
"2")
# Switch fan operation mode
case $mode in
"Stop") echo Full > $tz_dev/fan_mode;;
"Full") echo Manual > $tz_dev/fan_mode;;
"Manual") echo Auto > $tz_dev/fan_mode;;
"Auto") echo Stop > $tz_dev/fan_mode;;
esac
;;
"3")
read -p"Enter PWM (0 ~ 100 %): " val
echo "$val" > "$tz_dev/PWM"
;;
"4")
read -p"Low Stop Limit (0 ~ 255 milli-Celsius): " val
echo ${val}000 > $tz_dev/trip_point_2_temp
;;
"5")
read -p"Low Limit (0 ~ 255 milli-Celsius): " val
echo ${val}000 > $tz_dev/trip_point_1_temp
;;
"6")
read -p"High Stop Limit (0 ~ 255 milli-Celsius): " val
echo ${val}000 > $tz_dev/trip_point_0_temp
;;
"7")
read -p"Max PWM (0 ~ 100 %): " val
echo ${val} > $tz_dev/cdev0/set_max_state
rmmod eiois200_fan
modprobe eiois200_fan
;;
"8")
read -p"Min PWM (0 ~ 100 %): " val
echo ${val} > $tz_dev/cdev1/set_max_state
rmmod eiois200_fan
modprobe eiois200_fan
;;
esac
done
}
################################################################################
###### x) Thermal Sub1 Menu ######
################################################################################
function ThermalSub1Menu
{
local ACTS=("Shutdown" "Power OFF" "Throttle")
local SYSFS=/sys/class/thermal
local names=()
local act=0
local i=0
local dev
# Search thermal protect
for dev in $(ls -v $SYSFS); do
if [ -e $SYSFS/$dev/type ] &&
[ $(cat "$SYSFS/$dev/type") == "eiois200_thermal" ]; then
names[${#names[@]}]=$dev
fi
done
if [ "${#names[@]}" == "0" ]; then
return `read -p"Can't find thermal protect..."`
fi
while :
do
local fs=${SYSFS}/${names[$i]}
local trigger_fs=${fs}/trip_point_${act}_temp
local state_fs=${fs}/cdev${act}/enable
local trigger=`cat $trigger_fs`
local state=`cat $state_fs`
local temp=`cat $fs/temp`
# Show menu
show_title
echo -e "Thermal Protection"
echo -e
echo -e "0) Back to Main menu"
echo -e "1) Protection zone :" ${names[$i]}
echo -e " ) Sensor :" `cat ${fs}/name`
echo -e " ) Temperature :" $((temp / 10)).$((temp % 10))
echo -e " ) Type :" `cat $fs/trip_point_${act}_type`
echo -e "2) Event Type :" ${ACTS[$act]}
echo -e "3) Trigger Temperature :" $((trigger / 10)).$((trigger % 10))
echo -e "4) State :" $state
echo -e
read -t1 -n1 -p"Select the item you want to set: " sel
echo -e
# Processes
case $sel in
"0")
return 0
;;
"1")
# Switch to next device
i=$(( (i + 1) % ${#names[@]} ))
;;
"2")
# Switch to next action
act=$(( (act + 1) % ${#ACTS[@]} ))
;;
"3")
# Temp unit is 0.1 degree-Celsius
echo -e "Trigger Temperature (0 ~ 1250 Centi-Celsius): \c"
read trigger
echo $trigger > $trigger_fs
;;
"4")
if [ $state == enabled ]; then
echo disabled > $state_fs
else
echo enabled > $state_fs
fi
esac
done
}
################################################################################
###### 4) GPIO Sub1 Menu ######
################################################################################
function GpioSub1Menu
{
local SYSFS=/sys/class/gpio
local shift=0
local base=0
local val
local sel
local dir
local state
# Search GPIO
for base in $(ls -v $SYSFS); do
if [ -e $SYSFS/$base/label ] &&
[ `cat $SYSFS/$base/label` == "gpio_eiois200" ]; then
# get rid of prefix gpiochip
ngpio=` cat $SYSFS/${base}/ngpio `
base=$(echo $base | sed s/[a-z]//g)
break
fi
done
if [[ $base -eq 0 ]]; then
return `read -p"GPIO device not found..."`
fi
while : ; do
local num=$((${base}+${shift}))
local path=${SYSFS}/gpio${num}
echo ${num} > ${SYSFS}/export
dir=`cat ${path}/direction`
state=`cat ${path}/value`
echo ${num} > ${SYSFS}/unexport
# Show menu
show_title
printf "GPIO\n\n"
printf "0) Back to Main menu\n"
printf "1) GPIO Pin : %d (GPIO%d)\n" $shift $(($base + $shift))
printf "2) Direction : $dir\n"
printf "3) Level : $state\n\n"
read -t1 -n1 -p "Select the item you want to set: " sel
echo -e
# Processes
case $sel in
"0")
return 0
;;
"1")
printf "Pin Number (0 ~ %d):" $(($ngpio-1))
read val
if [ $val -lt $ngpio ] && [ $val -ge 0 ] ; then
shift=$(($val))
fi
;;
"2")
dir=`ternary "[ \"$dir\" == \"in\" ]" ? "out" : "in"`
echo $num > ${SYSFS}/export
echo $dir > ${path}/direction
echo $num > ${SYSFS}/unexport
;;
"3")
state=$((1 - state))
echo $num > ${SYSFS}/export
echo $state > ${path}/value
echo $num > ${SYSFS}/unexport
;;
*)
;;
esac
done
}
################################################################################
###### 5) Backlight Sub1 Menu ######
################################################################################
function BlSub1Menu
{
local SYSFS=/sys/class/backlight
local PARAM=/sys/module/eiois200_bl/parameters
local i
local val
local bl_name
# find the Backlight
for i in $(ls -v $SYSFS | grep eiois200_bl); do
bl_name=$i
break ;
done
if [ "$bl_name" == "" ]; then
return `read -p"Can't find eiois200 bl"`
fi
while : ; do
local dir=${SYSFS}/${bl_name}
local bri=`cat ${dir}/brightness`
local max=`cat ${dir}/max_brightness`
local bri_invert=`cat ${PARAM}/bri_invert`
local bri_freq=`cat ${PARAM}/bri_freq`
local bl_power=`cat ${dir}/bl_power`
local bl_power_invert=`cat ${PARAM}/bl_power_invert`
# Show menu
show_title
printf "Backlight: $bl_name\n\n"
printf "0) Back to Main menu\n"
printf "1) Brightness value : %d (0 to %d)\n" $bri $max
printf "2) Brightness frequency\t: $bri_freq Hz\n"
printf "3) Brightness invert : %s\n" `ternary "[[ \"$((bri_invert&1))\" == \"1\" ]]" ? On : Off`
printf "4) Power : %s\n" `ternary "[[ \"$bl_power\" == \"1\" ]]" ? On : Off`
printf "5) Power invert\t : %s\n" `ternary "[[ \"$bl_power_invert\" == \"1\" ]]" ? On : Off`
printf "\n"
read -n1 -p"Enter your choice: " sel
echo -e
# Processes
case $sel in
"0")
return 0
;;
"1")
printf "Input PWM value between 0 to %d: " $max
read val
echo $val > ${dir}/brightness
;;
"2")
read -p"Input PWM freqeuency(Hz): " val
if [ $val -ge 10 ]; then
rmmod eiois200_bl
modprobe eiois200_bl bri_freq=${val}
sleep 0.2
fi
;;
"3")
rmmod eiois200_bl
modprobe eiois200_bl bri_invert=$((1 - (bri_invert&1)))
sleep 0.2
;;
"4")
echo $((1 - bl_power)) > ${dir}/bl_power
;;
"5")
rmmod eiois200_bl
modprobe eiois200_bl bl_power_invert=$((1 - bl_power_invert))
sleep 0.2
;;
*)
;;
esac
done
}
################################################################################
###### 6-2) SMBus Sub2 Probe: ######
################################################################################
function SMBusSub2Probe
{
echo -e
echo "Address of existed devices (Hex):" \
`i2cdetect -y $1 | sed '1 d' | cut -c5-51 | tr '\n' ' '| sed 's/-- //g' | sed 's/ //g'`
echo -e
read -n1 -t2 -p"Press ENTER to continue..."
}
################################################################################
###### 6-3) SMBus Sub2 Read: ######
################################################################################
function SMBusSub2Read
{
local host=$1
local expr=""
local addr=0
local cmd=0
local len=1
local sel
local val
while : ; do
#Show menu
show_title
case $len in
0) protocol="Block Read";;
1) protocol="Receive Byte";;
2) protocol="Read Byte";;
3) protocol="Read Word";;
*) protocol="Block Read";;
esac
printf "SMBus Read Data: i2c-$1\n\n"
printf "0) Back to smbus menu\n"
printf "1) Protocol : $protocol\n"
printf "2) Address : %d (0x%X)\n" $addr $addr
printf "3) Command : %d (0x%X)\n" $cmd $cmd
printf "4) Run\n\n"
read -n1 -p"Enter your choice: " sel
echo -e
# Processes
case $sel in
"0")
return 0
;;
"1")
# Different length of protocal
len=$(( (len + 1) % 4))
;;
"2")
read -p"Address: " val;
if [ -n "$val" ]; then
addr=$val
fi
;;
"3")
read -p"Command: " val;
if [ -n "$val" ]; then
cmd=$val
fi
;;
"4")
# Different expression by protocal
case $len in
0) expr="i2cget -y -f -a ${host} $addr $cmd s";;
1) expr="i2cget -y -f -a ${host} $addr ";;
2) expr="i2cget -y -f -a ${host} $addr $cmd b";;
3) expr="i2cget -y -f -a ${host} $addr $cmd w";;
esac
# Execute expression
echo -e "\n${expr}"
eval "$expr"
read -n1 -t2 -p"Read transfer `ternary "[[ $? -eq 0 ]]" ? "Success\n" : "Fail\n"` ..."
;;
esac
done
}
################################################################################
###### 6-4) SMBus Sub2 Write: ######
################################################################################
function SMBusSub2Write
{
local host=$1
local addr=0
local data=()
local expr
local sel
while : ; do
# Show menu
show_title
case ${#data[@]} in
0) protocol="Quick Write";;
1) protocol="Send Byte";;
2) protocol="Write Byte";;
3) protocol="Write Word";;
*) protocol="Block Write";;
esac
printf "SMBus Write Data: i2c-$1\n\n"
printf "0) Back to SMBus menu\n"
printf " ) Protocol: $protocol\n"
printf "1) Address : %d (0x%02X) (7-bit)\n" $addr $addr
echo -e " ) Command : ${data[0]}"
echo -e " ) length : ${#data[@]}"
echo -e "2) Data : ${data[@]}"
echo -e "3) Run\n"
read -n1 -p"Enter your choice: " sel
echo -e "\n"
# Processes
case $sel in
"0")
return 0
;;
"1")
read -p"Address: " addr
;;
"2")
read -p"Input a series of HEX value (like 0x01 0x02 0x03 ...): " \
-a data
;;
"3")
# Different expression by length
case ${#data[@]} in
0)
expr="i2cdetect -y -a -q ${host} $addr $addr"
echo -e "\n" $expr "\n"
val=`$expr`
read -p"Write transfer $(ternary "[[ \"\$val\" == *\"--\"* ]]" ? "fail\n" : "Success\n") ..."
continue
;;
1) expr="i2cset -y -f -a ${host} $addr ${data[@]}";;
2) expr="i2cset -y -f -a ${host} $addr ${data[@]}";;
3) expr="i2cset -y -f -a ${host} $addr ${data[0]} $((data[1] * 0x100 + data[2])) w";;
*) expr="i2cset -y -f -a ${host} $addr ${data[@]} s";;
esac
# Execute expression
echo -e "${expr}"
eval "${expr}"
echo ""
read -n1 -t2 -p"Write transfer `ternary "[[ $? -eq 0 ]]" ? "Success\n" : "Fail\n"` ... "
;;
*)
;;
esac
done
}
################################################################################
###### 6) SMBus Sub1 Menu ######
################################################################################
function SMBusSub1Menu
{
local SYSFS=/sys/bus/i2c/devices
local count=0
local cur=0
local hosts=()
local i
i2cdetect -l > /dev/null 2>&1
if [ $? -ne 0 ]; then
return `read -p "please install i2c-tools"`
fi
# find the SMBus# and save to array
for i in $(ls -v $SYSFS); do
if [ -d $SYSFS/$i ] &&
[ "$(cat $SYSFS/$i/name | grep "eiois200")" != "" ]; then
hosts[count++]=$(echo $i | sed 's/i2c-//g')
fi
done
if [ $count -eq 0 ]; then
read -p"No SMBus adapters... "
return 1
fi
while : ; do
local sel=
local host=${hosts[$cur]}
# Show menu
for ((i=0 ; i < $count ; i=i+1)); do
sel+=$(ternary "[[ $i -eq $cur ]]" ? "[i2c-${hosts[$i]}]" : " i2c-${hosts[$i]} ")
done
show_title
echo -e "SMBus"
echo -e
echo -e "0) Back to Main menu"
echo -e "1) Select SMBus host: $sel"
echo -e " ) Internal name: `cat $SYSFS/i2c-${hosts[$cur]}/name | sed 's/eiois200 //g'`"
echo -e "2) Probe"
echo -e "3) Read"
echo -e "4) Write\n"
read -n1 -p "Enter your choice: " sel
echo -e
# Switch to sub-menu
case $sel in
"0")
return 0
;;
"1")
cur=$(( (cur + 1) % count))
;;
"2")
SMBusSub2Probe $host
;;
"3")
SMBusSub2Read $host
;;
"4")
SMBusSub2Write $host
;;
*)
;;
esac
done
}
################################################################################
###### 7-2) I2C Sub2 Probe: ######
################################################################################
function I2CSub2Probe
{
echo -e "\nAddress of existed devices (Hex):" \
`i2cdetect -y $1 | sed '1 d' | cut -c5-51 | tr '\n' ' '| sed 's/-- //g' | sed 's/ //g'`
echo -e
read -n1 -t2 -p"Press ENTER to continue..." probe_value
}
################################################################################
###### 7-3) I2C Sub2 Read: ######
################################################################################
function I2CSub2Read
{
local host=$1
local addr=0
local cmd=0
local len=0
local val
local sel
while : ; do
# Menu
show_title
printf "I2C Read Data: i2c-$host\n\n"
printf "0) Back to I2C menu\n"
printf "1) Address : %d (0x%X) ( (7-bit)\n" $addr $addr
printf "2) Command : %d (0x%X) (Byte-type)\n" $cmd $cmd
printf "3) Length : %d (0x%X)\n" $len $len
printf "4) Run\n\n"
read -n1 -p"Enter your choice: " sel
echo -e "\n"
# Processes
case $sel in
"0")
return 0
;;
"1")
read -p"Enter 7-bit Address: " val
if [ -n "$val" ]; then
addr=$val
fi
;;
"2")
read -p"Byte Command : " val
if [ -n "$val" ]; then
cmd=$val
fi
;;
"3")
read -p"Read Length: " val
if [ -n "$val" ]; then
len=$val
fi
;;
"4")
echo -e "i2ctransfer -f -a -y $host w1@$addr $cmd r$len"
i2ctransfer -f -a -y $host w1@$addr $cmd r$len
read -n1 -t2 -p "I2C Read transfer `ternary "[ $? -eq 0 ]" ? "Success" : "Fail"`..."
;;
*)
;;
esac
done
}
################################################################################
###### 7-4) I2C Sub2 Write: ######
################################################################################
function I2CSub2Write
{
local host=$1
local addr=0
local data=()
while : ; do
local val
local sel
# Show Menu
show_title
printf "I2C Write Data: i2c-$host \n\n"
printf "0) Back to I2C menu\n"
printf "1) Address : %d (0x%X) (7-bit)\n" $addr $addr
printf " ) Command : ${data[0]}\n"
printf " ) Length : ${#data[@]}\n"
echo -e "2) Write Data : ${data[@]}"
printf "3) Run\n\n"
read -n1 -p"Enter your choice: " sel;
echo -e "\n"
# Processes
case $sel in
"0")
return 1
;;
"1")
read -p"7-bit Address: " addr
;;
"2")
read -p"Input a series of HEX value (Like 0x01 0x02 0x03 ...): " -a data
;;
"3")
echo i2ctransfer -f -a -y $host w$((${#data[@]}))@$addr ${data[@]}
i2ctransfer -f -a -y $host w$((${#data[@]}))@$addr ${data[@]}
read -n1 -t2 -p "I2C Write transfer `ternary "[ $? -eq 0 ]" ? "Success" : "Fail"`..."
;;
esac
done
}
################################################################################
###### 7-4) I2C Sub2 Write Read combine: ######
################################################################################
function I2CSub2WriteRead
{
local host=$1
local addr=0
local len=0
local data=()
local sel
local val
local expr=""
while : ; do
# Show menu
show_title
printf "I2C Write Read Combine: i2c-$host\n\n"
printf "0) Back to I2C menu\n"
printf "1) Address : %d (0x%X) (7-bit)\n" $addr $addr
printf " ) Command : ${data[0]}\n"
printf " ) Write Length : %d (0x%X)\n" ${#data[@]} ${#data[@]}
echo "2) Write Data : ${data[@]} "
printf "3) Read Length : $len\n"
printf "4) Run\n\n"
read -n1 -p"Enter your choice: " sel
echo -e "\n"
# Processes
case $sel in
"0")
return 1
;;
"1")
echo -e "7-bit Address : \c"
read val
if [ -n "$val" ]; then
addr=$val
fi
;;
"2")
echo -e p"Input a series of HEX value (Like 0x01 0x02 0x03 ...): \c"
read -a data
;;
"3")
echo -e "Read Length: \c"
read val
if [ -n "$val" ]; then
len=$val
fi
;;
"4")
# Different expression by length
if [ ${#data[@]} -eq 0 ]; then
expr="i2ctransfer -f -a -y $host r${len}@$addr"
elif [ $len -eq 0 ]; then
expr="i2ctransfer -f -a -y $host w${#data[@]}@$addr ${data[@]}"
else
expr="i2ctransfer -f -a -y $host w${#data[@]}@$addr ${data[@]} r$len"
fi
# Execute expression
echo -e "$expr"
eval "$expr"
read -n1 -t2 -p "I2C Write Read Combined transfer `ternary "[ $? -eq 0 ]" ? "Success" : "Fail"`..."
;;
*)
;;
esac
done
}
################################################################################
###### 7) I2C Sub1 Menu ######
################################################################################
function I2CSub1Menu
{
local SYSFS=/sys/bus/i2c/devices
local PARAM=/sys/module/i2c_eiois200/parameters
local i=0
local hosts=()
local val
i2cdetect -l > /dev/null 2>&1
if [ $? -ne 0 ]; then
return `read -p"Please install i2c-tools"`
fi
# find the I2C# and save to array
for I2CN in $(ls -v $SYSFS); do
if [ -d $SYSFS/$I2CN ] &&
[ "$(cat $SYSFS/$I2CN/name | grep "eiois200")" != "" ]; then
hosts[${#hosts[@]}]=$(echo $I2CN | sed 's/i2c-//g')
fi
done
if [[ ${#hosts[@]} -eq 0 ]]; then
return `read -p"No I2C adapter..."`
fi
while : ; do
local name=$(cat $SYSFS/i2c-${hosts[$i]}/name | sed 's/eiois200-//g')
local i2c_freq=`cat ${PARAM}/${name: -4}_freq`
local lst=
local sel=0
# Shoe menu
for ((j=0 ; j < ${#hosts[@]} ; j++)); do
lst+=`ternary "[ $i == $j ]" ? "[i2c${hosts[j]}]" : " i2c${hosts[j]} "`
done
show_title
echo -e "I2C"
echo -e
echo -e "0) Back to Main menu"
echo -e "1) Select host: $lst"
echo -e " ) Internal name: eiois200-$name"
echo -e "2) Probe"