1 package net.osdn.gokigen.cameratest.fuji;
3 import android.graphics.PointF;
4 import android.util.Log;
6 import androidx.annotation.NonNull;
8 import net.osdn.gokigen.cameratest.fuji.statuses.FujiStatusChecker;
9 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
10 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusRequest;
12 public class Connection implements IFujiStatusRequest
14 private final String TAG = toString();
15 private final MessageSequence sequence;
16 private final Communication comm;
17 private final FujiStatusChecker statusChecker;
19 public Connection(@NonNull ILiveViewImage imageViewer, @NonNull IFujiStatusNotify notify)
21 this.comm = new Communication(imageViewer);
22 this.sequence = new MessageSequence();
23 this.statusChecker = new FujiStatusChecker(this, notify);
26 public boolean start_connect(boolean isBothLiveView)
30 if (connect_to_camera(isBothLiveView))
32 ret = requestStatus();
36 statusChecker.start();
42 private boolean connect_to_camera(boolean isBothLiveView)
46 ReceivedDataHolder rx_bytes;
47 comm.connect_socket();
49 comm.send_to_camera(sequence.registration_message(), false);
50 rx_bytes = comm.receive_from_camera();
51 dump_bytes(0, rx_bytes);
55 if (rx_bytes.getData().length == 8)
57 byte[] receiveData = rx_bytes.getData();
58 if ((receiveData[0] == 0x05)&&(receiveData[1] == 0x00)&&(receiveData[2] == 0x00)&&(receiveData[3] == 0x00)&&
59 (receiveData[4] == 0x19)&&(receiveData[5] == 0x20)&&(receiveData[6] == 0x00)&&(receiveData[7] == 0x00))
68 comm.send_to_camera(sequence.start_message(), true);
69 rx_bytes = comm.receive_from_camera();
70 dump_bytes(1, rx_bytes);
73 // なんだろう?? (送信が必要なようだが)
74 comm.send_to_camera(sequence.start_message2(), true);
75 rx_bytes = comm.receive_from_camera();
76 dump_bytes(2, rx_bytes);
79 // two_part messageを発行 (その1)
80 comm.send_to_camera(sequence.start_message3_1(), true);
81 rx_bytes = comm.receive_from_camera();
82 dump_bytes(3, rx_bytes);
85 if (rx_bytes.getData().length <= 50)
87 // two_part messageを発行 (その2)
88 comm.send_to_camera(sequence.start_message3_2(), false);
89 rx_bytes = comm.receive_from_camera();
90 dump_bytes(4, rx_bytes);
95 comm.send_to_camera(sequence.start_message4(), true);
96 rx_bytes = comm.receive_from_camera();
97 dump_bytes(5, rx_bytes);
102 // two_part messageを発行 (その1)
103 comm.send_to_camera(sequence.start_message5_1(), true);
104 rx_bytes = comm.receive_from_camera();
105 dump_bytes(6, rx_bytes);
108 // two_part messageを発行 (その2)
109 comm.send_to_camera(sequence.start_message5_2(), false);
110 rx_bytes = comm.receive_from_camera();
111 dump_bytes(7, rx_bytes);
115 comm.send_to_camera(sequence.start_message6(), true);
116 rx_bytes = comm.receive_from_camera();
117 dump_bytes(8, rx_bytes);
121 comm.send_to_camera(sequence.start_message7(), true);
122 rx_bytes = comm.receive_from_camera();
123 dump_bytes(9, rx_bytes);
127 comm.send_to_camera(sequence.start_message8(), true);
128 rx_bytes = comm.receive_from_camera();
129 dump_bytes(10, rx_bytes);
134 comm.send_to_camera(sequence.camera_remote_message(), true);
136 // 応答OKの場合は、8バイト ({0x03, 0x00, 0x01, 0x20} + {0x10, 0x02, 0x00, 0x00} )が応答されるはず
137 rx_bytes = comm.receive_from_camera();
138 dump_bytes(11, rx_bytes);
142 // 別のポートもオープンして動作を行う。 (1500ms程度待つといけるみたいだ...)
145 comm.start_response();
147 Log.v(TAG, "connect_to_camera DONE.");
157 public void reset_to_camera()
161 comm.send_to_camera(sequence.reset_message(), true);
162 ReceivedDataHolder rx_bytes = comm.receive_from_camera();
163 dump_bytes(0, rx_bytes);
164 statusChecker.stop();
173 public void disconnect()
177 statusChecker.stop();
193 comm.stop_response();
201 comm.disconnect_socket();
210 public boolean requestStatus()
214 comm.send_to_camera(sequence.status_request_message(), true);
215 ReceivedDataHolder rx_bytes = comm.receive_from_camera();
216 if (rx_bytes.getData().length > 0)
219 statusChecker.statusReceived(rx_bytes);
221 dump_bytes(12, rx_bytes);
232 private void dump_bytes(int indexNumber, ReceivedDataHolder data)
235 StringBuffer message;
236 message = new StringBuffer();
237 for (byte item : data.getData())
240 message.append(String.format("%02x ", item));
243 Log.v(TAG, " RX [" + indexNumber + "] " + message);
245 message = new StringBuffer();
250 Log.v(TAG, " RX [" + indexNumber + "] " + message);
255 public boolean execute_focus_point(PointF point)
259 byte x = (byte) (0x000000ff & Math.round(point.x));
260 byte y = (byte) (0x000000ff & Math.round(point.y));
261 Log.v(TAG, "DRIVE AF (" + x + "," + y + ")");
263 comm.send_to_camera(sequence.execute_focus_lock(x, y), true);
265 ReceivedDataHolder rx_bytes = comm.receive_from_camera();
266 dump_bytes(16, rx_bytes);
275 public boolean execute_unlock_focus()
279 comm.send_to_camera(sequence.execute_focus_unlock(), true);
281 ReceivedDataHolder rx_bytes = comm.receive_from_camera();
282 dump_bytes(17, rx_bytes);
291 public boolean execute_shutter()
295 comm.send_to_camera(sequence.execute_shutter_message(), true);
297 ReceivedDataHolder rx_bytes = comm.receive_from_camera();
298 dump_bytes(14, rx_bytes);
307 public boolean updateProperty(int commandCode, int setValue)
309 ReceivedDataHolder rx_bytes;
311 byte high = (byte) ((0x0000ff00 & commandCode) >> 8);
312 byte low = (byte) (0x000000ff & commandCode);
314 byte data0 = (byte)((0xff000000 & setValue) >> 24);
315 byte data1 = (byte)((0x00ff0000 & setValue) >> 16);
316 byte data2 = (byte)((0x0000ff00 & setValue) >> 8);
317 byte data3 = (byte)((0x000000ff & setValue));
319 // two_part messageを発行 (その1)
320 comm.send_to_camera(sequence.update_property_1(high, low), true);
321 rx_bytes = comm.receive_from_camera();
322 dump_bytes(15, rx_bytes);
325 // two_part messageを発行 (その2)
326 comm.send_to_camera(sequence.update_property_2(data0, data1, data2, data3), false);
327 rx_bytes = comm.receive_from_camera();
328 dump_bytes(16, rx_bytes);