OSDN Git Service

設定変える準備を搭載。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / Connection.java
1 package net.osdn.gokigen.cameratest.fuji;
2
3 import android.app.Activity;
4 import android.graphics.PointF;
5 import android.util.Log;
6
7 import androidx.annotation.NonNull;
8
9 import net.osdn.gokigen.cameratest.fuji.statuses.FujiStatusChecker;
10 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
11 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusRequest;
12
13 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraProperties.FILM_SIMULATION;
14 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraProperties.IMAGE_ASPECT;
15 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.FILM_SIMULATION_MAX;
16 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.FILM_SIMULATION_MIN;
17 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.IMAGE_ASPECT_MAX;
18 import static net.osdn.gokigen.cameratest.fuji.statuses.IFujiCameraPropertyValues.IMAGE_ASPECT_MIN;
19
20 public class Connection implements IFujiStatusRequest
21 {
22     private final String TAG = toString();
23     private final MessageSequence sequence;
24     private final Communication comm;
25     private final FujiStatusChecker statusChecker;
26
27     public Connection(@NonNull Activity activity, @NonNull ILiveViewImage imageViewer, @NonNull IFujiStatusNotify notify)
28     {
29         this.comm = new Communication(activity, imageViewer);
30         this.sequence = new MessageSequence();
31         this.statusChecker = new FujiStatusChecker(this, notify);
32     }
33
34     public boolean start_connect(boolean isBothLiveView)
35     {
36         boolean ret = false;
37
38         if (connect_to_camera(isBothLiveView))
39         {
40             ret = requestStatus();
41             if (ret)
42             {
43                 // 定期監視の開始
44                 statusChecker.start();
45             }
46         }
47         return (ret);
48     }
49
50     private boolean connect_to_camera(boolean isBothLiveView)
51     {
52         try
53         {
54             ReceivedDataHolder rx_bytes;
55             comm.connect_socket();
56
57             comm.send_to_camera(sequence.registration_message(), false);
58             rx_bytes = comm.receive_from_camera();
59             dump_bytes(0, rx_bytes);
60             Thread.sleep(50);
61
62             // 応答エラーかどうかをチェックする
63             if (rx_bytes.getData().length == 8)
64             {
65                 byte[] receiveData = rx_bytes.getData();
66                 if ((receiveData[0] == 0x05)&&(receiveData[1] == 0x00)&&(receiveData[2] == 0x00)&&(receiveData[3] == 0x00)&&
67                     (receiveData[4] == 0x19)&&(receiveData[5] == 0x20)&&(receiveData[6] == 0x00)&&(receiveData[7] == 0x00))
68                 {
69                     // 応答エラー...
70                     return (false);
71                 }
72                 return (false);
73             }
74
75             // start_messageを送信
76             comm.send_to_camera(sequence.start_message(), true);
77             rx_bytes = comm.receive_from_camera();
78             dump_bytes(1, rx_bytes);
79             Thread.sleep(50);
80
81             //  なんだろう?? (送信が必要なようだが)
82             comm.send_to_camera(sequence.start_message2(), true);
83             rx_bytes = comm.receive_from_camera();
84             dump_bytes(2, rx_bytes);
85             Thread.sleep(50);
86
87             // two_part messageを発行 (その1)
88             comm.send_to_camera(sequence.start_message3_1(), true);
89             rx_bytes = comm.receive_from_camera();
90             dump_bytes(3, rx_bytes);
91             Thread.sleep(50);
92
93             if (rx_bytes.getData().length <= 50)
94             {
95                 // two_part messageを発行 (その2)
96                 comm.send_to_camera(sequence.start_message3_2(), false);
97                 rx_bytes = comm.receive_from_camera();
98                 dump_bytes(4, rx_bytes);
99                 Thread.sleep(50);
100             }
101
102             // remote mode
103             comm.send_to_camera(sequence.start_message4(), true);
104             rx_bytes = comm.receive_from_camera();
105             dump_bytes(5, rx_bytes);
106             Thread.sleep(50);
107
108             if (!isBothLiveView)
109             {
110                 // two_part messageを発行 (その1)
111                 comm.send_to_camera(sequence.start_message5_1(), true);
112                 rx_bytes = comm.receive_from_camera();
113                 dump_bytes(6, rx_bytes);
114                 Thread.sleep(50);
115
116                 // two_part messageを発行 (その2)
117                 comm.send_to_camera(sequence.start_message5_2(), false);
118                 rx_bytes = comm.receive_from_camera();
119                 dump_bytes(7, rx_bytes);
120                 Thread.sleep(50);
121
122                 // ????
123                 comm.send_to_camera(sequence.start_message6(), true);
124                 rx_bytes = comm.receive_from_camera();
125                 dump_bytes(8, rx_bytes);
126                 Thread.sleep(50);
127
128                 // ????
129                 comm.send_to_camera(sequence.start_message7(), true);
130                 rx_bytes = comm.receive_from_camera();
131                 dump_bytes(9, rx_bytes);
132                 Thread.sleep(50);
133
134                 // ????
135                 comm.send_to_camera(sequence.start_message8(), true);
136                 rx_bytes = comm.receive_from_camera();
137                 dump_bytes(10, rx_bytes);
138                 Thread.sleep(50);
139             }
140
141             // リモート制御の開始!
142             comm.send_to_camera(sequence.camera_remote_message(), true);
143
144             // 応答OKの場合は、8バイト ({0x03, 0x00, 0x01, 0x20} + {0x10, 0x02, 0x00, 0x00} )が応答されるはず
145             rx_bytes = comm.receive_from_camera();
146             dump_bytes(11, rx_bytes);
147             Thread.sleep(150);
148
149
150             // 別のポートもオープンして動作を行う。 (1500ms程度待つといけるみたいだ...)
151             Thread.sleep(2000);
152             comm.start_stream();
153             comm.start_response();
154
155             Log.v(TAG, "connect_to_camera DONE.");
156             return (true);
157         }
158         catch (Exception e)
159         {
160             e.printStackTrace();
161         }
162         return (false);
163     }
164
165     public void reset_to_camera()
166     {
167         try
168         {
169             comm.send_to_camera(sequence.reset_message(), true);
170             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
171             dump_bytes(0, rx_bytes);
172             statusChecker.stop();
173             Thread.sleep(150);
174         }
175         catch (Exception e)
176         {
177             e.printStackTrace();
178         }
179     }
180
181     public void disconnect()
182     {
183         try
184         {
185             statusChecker.stop();
186         }
187         catch (Exception e)
188         {
189             e.printStackTrace();
190         }
191         try
192         {
193             comm.stop_stream();
194         }
195         catch (Exception e)
196         {
197             e.printStackTrace();
198         }
199         try
200         {
201             comm.stop_response();
202         }
203         catch (Exception e)
204         {
205             e.printStackTrace();
206         }
207         try
208         {
209             comm.disconnect_socket();
210         }
211         catch (Exception e)
212         {
213             e.printStackTrace();
214         }
215     }
216
217     @Override
218     public boolean requestStatus()
219     {
220         try
221         {
222             comm.send_to_camera(sequence.status_request_message(), true);
223             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
224             if (rx_bytes.getData().length > 0)
225             {
226                 // 受信したステータス情報を渡す
227                 statusChecker.statusReceived(rx_bytes);
228             }
229             dump_bytes(12, rx_bytes);
230
231             return (true);
232         }
233         catch (Exception e)
234         {
235             e.printStackTrace();
236         }
237         return (false);
238     }
239
240     private void dump_bytes(int indexNumber, ReceivedDataHolder data)
241     {
242         int index = 0;
243         StringBuffer message;
244         message = new StringBuffer();
245         for (byte item : data.getData())
246         {
247             index++;
248             message.append(String.format("%02x ", item));
249             if (index >= 8)
250             {
251                 Log.v(TAG, " RX [" + indexNumber + "] " + message);
252                 index = 0;
253                 message = new StringBuffer();
254             }
255         }
256         if (index != 0)
257         {
258             Log.v(TAG, " RX [" + indexNumber + "] "  + message);
259         }
260         System.gc();
261     }
262
263     public boolean execute_focus_point(PointF point)
264     {
265         try
266         {
267             byte x = (byte) (0x000000ff & Math.round(point.x));
268             byte y = (byte) (0x000000ff & Math.round(point.y));
269             Log.v(TAG, "DRIVE AF (" + x + "," + y + ")");
270
271             comm.send_to_camera(sequence.execute_focus_lock(x, y), true);
272
273             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
274             dump_bytes(16, rx_bytes);
275         }
276         catch (Exception e)
277         {
278             e.printStackTrace();
279         }
280         return (false);
281     }
282
283     public boolean execute_change_film_simulation()
284     {
285         try
286         {
287             // 現在の値を入手
288             int currentValue = statusChecker.getValue(FILM_SIMULATION);
289             currentValue++;
290             if (currentValue > FILM_SIMULATION_MAX)
291             {
292                 currentValue = FILM_SIMULATION_MIN;
293             }
294             return (updateProperty(FILM_SIMULATION, currentValue));
295         }
296         catch (Exception e)
297         {
298             e.printStackTrace();
299         }
300         return (false);
301     }
302
303     public boolean execute_change_image_aspect()
304     {
305         try
306         {
307             // 現在の値を入手
308             int currentValue = statusChecker.getValue(IMAGE_ASPECT);
309             currentValue++;
310             if (currentValue > IMAGE_ASPECT_MAX)
311             {
312                 currentValue = IMAGE_ASPECT_MIN;
313             }
314             return (updateProperty(IMAGE_ASPECT, currentValue));
315         }
316         catch (Exception e)
317         {
318             e.printStackTrace();
319         }
320         return (false);
321     }
322
323
324     public boolean execute_unlock_focus()
325     {
326         try
327         {
328             comm.send_to_camera(sequence.execute_focus_unlock(), true);
329
330             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
331             dump_bytes(17, rx_bytes);
332         }
333         catch (Exception e)
334         {
335             e.printStackTrace();
336         }
337         return (false);
338     }
339
340     public boolean execute_shutter()
341     {
342         try
343         {
344             comm.send_to_camera(sequence.execute_shutter_message(), true);
345
346             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
347             dump_bytes(14, rx_bytes);
348         }
349         catch (Exception e)
350         {
351             e.printStackTrace();
352         }
353         return (false);
354     }
355
356     public boolean updateProperty(int commandCode, int setValue)
357     {
358         ReceivedDataHolder rx_bytes;
359         try {
360             byte high = (byte) ((0x0000ff00 & commandCode) >> 8);
361             byte low = (byte) (0x000000ff & commandCode);
362
363             byte data0 = (byte)((0xff000000 & setValue) >> 24);
364             byte data1 = (byte)((0x00ff0000 & setValue) >> 16);
365             byte data2 = (byte)((0x0000ff00 & setValue) >> 8);
366             byte data3 = (byte)((0x000000ff & setValue));
367
368             // two_part messageを発行 (その1)
369             comm.send_to_camera(sequence.update_property_1(high, low), true);
370             rx_bytes = comm.receive_from_camera();
371             dump_bytes(15, rx_bytes);
372             Thread.sleep(50);
373
374             // two_part messageを発行 (その2)
375             comm.send_to_camera(sequence.update_property_2(data0, data1, data2, data3), false);
376             rx_bytes = comm.receive_from_camera();
377             dump_bytes(16, rx_bytes);
378         }
379         catch (Exception e)
380         {
381             e.printStackTrace();
382         }
383         return (false);
384     }
385 }