@@ -46,6 +46,115 @@ func kickCheck(chkicker chan bool) {
4646
4747func main () {
4848
49+ err := rpio .Open ()
50+ if err != nil {
51+ log .Fatal ("Error: " , err )
52+ }
53+
54+ //button が押されているか確認
55+ button1 := rpio .Pin (22 )
56+ button1 .Input ()
57+ button1 .PullUp ()
58+
59+ if button1 .Read ()^ 1 == rpio .High {
60+ log .Println ("Button1 is pressed. Start Robot Control Mode" )
61+ isControlByRobotMode = true
62+ }
63+
64+ rpio .Close ()
65+
66+ //Hostnameを取得する
67+ cmd := exec .Command ("hostname" )
68+ out , err := cmd .Output ()
69+ if err != nil {
70+ panic (err )
71+ }
72+ fmt .Println (string (out ))
73+
74+ //もし初期値のraspberrypiだったら
75+ if string (out ) == "raspberrypi\n " {
76+ //UNIX時間の下5桁を取得する
77+ unixtime := time .Now ().UnixNano ()
78+ log .Println ("Unixtime is " + fmt .Sprintf ("%d" , unixtime ))
79+ unixtime = unixtime % 100000
80+
81+ //Hostnameをracoon-XXXXXに変更する
82+ hostname := "racoon-" + fmt .Sprintf ("%05d" , unixtime )
83+
84+ log .Println ("Change Hostname To " + hostname )
85+ //Change Hostname
86+ //hostnamectl set-hostname raspberrypi コマンド実行
87+ cmd = exec .Command ("hostnamectl" , "set-hostname" , hostname )
88+ cmd .Run ()
89+
90+ //hostsの変更
91+ cmd = exec .Command ("sudo" , "sed" , "-i" , "/etc/hosts" , "-e" , "s/raspberrypi/" + hostname + "/g" , "/etc/hosts" )
92+ cmd .Run ()
93+
94+ //再起動
95+ log .Println ("=====Reboot=====" )
96+
97+ buzzer := rpio .Pin (12 )
98+ buzzer .Mode (rpio .Pwm )
99+ buzzer .Freq (1175 * 64 )
100+ buzzer .DutyCycle (16 , 32 )
101+ time .Sleep (500 * time .Millisecond )
102+ buzzer .DutyCycle (0 , 32 )
103+ buzzer .Freq (1396 * 64 )
104+ buzzer .DutyCycle (16 , 32 )
105+ time .Sleep (500 * time .Millisecond )
106+ buzzer .DutyCycle (0 , 32 )
107+ buzzer .Freq (1760 * 64 )
108+ buzzer .DutyCycle (16 , 32 )
109+ time .Sleep (500 * time .Millisecond )
110+ buzzer .DutyCycle (0 , 32 )
111+
112+ //reboot コマンド実行
113+ cmd = exec .Command ("reboot" )
114+ cmd .Run ()
115+
116+ }
117+
118+ if isControlByRobotMode {
119+ log .Println ("Robot Control Mode is ON" )
120+ out = []byte ("localuser\n " )
121+ }
122+
123+ if string (out ) == "localuser\n " {
124+ //ラズパイのGPIOのメモリを確保
125+ err := rpio .Open ()
126+ CheckError (err )
127+ buzzer := rpio .Pin (13 )
128+ buzzer .Mode (rpio .Pwm )
129+ buzzer .Freq (1175 * 64 )
130+ buzzer .DutyCycle (16 , 32 )
131+ time .Sleep (1000 * time .Millisecond )
132+ buzzer .DutyCycle (0 , 32 )
133+ time .Sleep (1000 * time .Millisecond )
134+
135+ button1 := rpio .Pin (22 )
136+ button1 .Input ()
137+ button1 .PullUp ()
138+
139+ if button1 .Read ()^ 1 == rpio .High {
140+ isControlByRobotMode = true
141+ log .Println ("Robot Control Mode is ON" )
142+ buzzer .Freq (1244 * 64 )
143+ buzzer .DutyCycle (16 , 32 )
144+ time .Sleep (100 * time .Millisecond )
145+ buzzer .DutyCycle (0 , 32 )
146+ time .Sleep (100 * time .Millisecond )
147+ buzzer .Freq (1244 * 64 )
148+ buzzer .DutyCycle (16 , 32 )
149+ time .Sleep (100 * time .Millisecond )
150+ buzzer .DutyCycle (0 , 32 )
151+ time .Sleep (100 * time .Millisecond )
152+
153+ } else {
154+ os .Exit (0 )
155+ }
156+ }
157+
49158 //自動アップデート
50159 go confirmAndSelfUpdate ()
51160 //GPIOの初期化
@@ -101,54 +210,6 @@ func main() {
101210 }
102211 }()
103212
104- //Hostnameを取得する
105- cmd := exec .Command ("hostname" )
106- out , err := cmd .Output ()
107- if err != nil {
108- panic (err )
109- }
110- fmt .Println (string (out ))
111-
112- //もし初期値のraspberrypiだったら
113- if string (out ) == "raspberrypi\n " {
114- //UNIX時間の下5桁を取得する
115- unixtime := time .Now ().UnixNano ()
116- log .Println ("Unixtime is " + fmt .Sprintf ("%d" , unixtime ))
117- unixtime = unixtime % 100000
118-
119- //Hostnameをracoon-XXXXXに変更する
120- hostname := "racoon-" + fmt .Sprintf ("%05d" , unixtime )
121-
122- log .Println ("Change Hostname To " + hostname )
123- //Change Hostname
124- //hostnamectl set-hostname raspberrypi コマンド実行
125- cmd = exec .Command ("hostnamectl" , "set-hostname" , hostname )
126- cmd .Run ()
127-
128- //再起動
129- log .Println ("=====Reboot=====" )
130-
131- buzzer := rpio .Pin (12 )
132- buzzer .Mode (rpio .Pwm )
133- buzzer .Freq (1175 * 64 )
134- buzzer .DutyCycle (16 , 32 )
135- time .Sleep (500 * time .Millisecond )
136- buzzer .DutyCycle (0 , 32 )
137- buzzer .Freq (1396 * 64 )
138- buzzer .DutyCycle (16 , 32 )
139- time .Sleep (500 * time .Millisecond )
140- buzzer .DutyCycle (0 , 32 )
141- buzzer .Freq (1760 * 64 )
142- buzzer .DutyCycle (16 , 32 )
143- time .Sleep (500 * time .Millisecond )
144- buzzer .DutyCycle (0 , 32 )
145-
146- //reboot コマンド実行
147- cmd = exec .Command ("reboot" )
148- cmd .Run ()
149-
150- }
151-
152213 //MyIDで指定したロボットIDを取得
153214 var MyID uint32 = uint32 (diptoid )
154215
0 commit comments