OSDN Git Service

WPZ2で、撮影画像・動画画像のサイズを変更、表示できるようにする。
[gokigen/mangle.git] / app / src / main / java / jp / osdn / gokigen / gokigenassets / camera / vendor / pixpro / wrapper / status / PixproStatusChecker.kt
1 package jp.osdn.gokigen.gokigenassets.camera.vendor.pixpro.wrapper.status
2
3 import android.graphics.Color
4 import android.util.Log
5 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusUpdateNotify
6 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatus
7 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusWatcher
8 import jp.osdn.gokigen.gokigenassets.camera.vendor.pixpro.wrapper.command.IPixproCommandPublisher
9 import jp.osdn.gokigen.gokigenassets.camera.vendor.pixpro.wrapper.command.messages.IPixproCommandCallback
10 import jp.osdn.gokigen.gokigenassets.camera.vendor.pixpro.wrapper.command.messages.specific.PixproStatusRequest
11 import jp.osdn.gokigen.gokigenassets.liveview.message.IMessageDrawer
12 import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleLogDumper
13 import java.lang.Exception
14
15 class PixproStatusChecker : IPixproCommandCallback, ICameraStatusWatcher, ICameraStatus
16 {
17     private val statusHolder = PixproStatusHolder()
18     private var whileFetching = false
19     private var notifier: ICameraStatusUpdateNotify? = null
20     private var pollDuration: Int = EVENT_POLL_QUEUE_MS
21     private lateinit var commandPublisher : IPixproCommandPublisher
22
23     fun setCommandPublisher(commandPublisher : IPixproCommandPublisher)
24     {
25         this.commandPublisher = commandPublisher
26         statusHolder.setCommandPublisher(commandPublisher)
27     }
28
29     override fun getStatusList(key: String): List<String?>
30     {
31         try
32         {
33             return (statusHolder.getAvailableItemList(key))
34         }
35         catch (e: Exception)
36         {
37             e.printStackTrace()
38         }
39         return (ArrayList())
40     }
41
42     override fun getStatus(key: String): String
43     {
44         try
45         {
46             return (statusHolder.getItemStatus(key))
47         }
48         catch (e: Exception)
49         {
50             e.printStackTrace()
51         }
52         return ("")
53     }
54
55     override fun getStatusColor(key: String): Int
56     {
57         try
58         {
59             return (statusHolder.getItemStatusColor(key))
60         }
61         catch (e: Exception)
62         {
63             e.printStackTrace()
64         }
65         return (Color.WHITE)
66     }
67
68     override fun setStatus(key: String, value: String)
69     {
70         Log.v(TAG, "setStatus($key, $value)")
71         try
72         {
73             return (statusHolder.setItemStatus(key, value))
74         }
75         catch (e: Exception)
76         {
77             e.printStackTrace()
78         }
79     }
80
81     override fun startStatusWatch(indicator: IMessageDrawer?, notifier: ICameraStatusUpdateNotify?)
82     {
83         if (whileFetching)
84         {
85             Log.v(TAG, "startStatusWatch() already starting.")
86             return
87         }
88         try
89         {
90             this.notifier = notifier
91             whileFetching = true
92
93             val thread = Thread {
94                 while (whileFetching)
95                 {
96                     try
97                     {
98                         Thread.sleep(pollDuration.toLong())
99
100                         Log.v(TAG, " === POLL EVENT === ")
101                         if (::commandPublisher.isInitialized)
102                         {
103                             commandPublisher.enqueueCommand(PixproStatusRequest(this))
104                         }
105                     }
106                     catch (e: Exception)
107                     {
108                         e.printStackTrace()
109                     }
110                 }
111             }
112             try
113             {
114                 thread.start()
115             }
116             catch (e: Exception)
117             {
118                 e.printStackTrace()
119             }
120         }
121         catch (e: Exception)
122         {
123             e.printStackTrace()
124         }
125     }
126
127     override fun stopStatusWatch()
128     {
129         Log.v(TAG, "stopStatusWatch()")
130         whileFetching = false
131         notifier = null
132     }
133
134     override fun receivedMessage(id: Int, rx_body: ByteArray?)
135     {
136         if (isDumpLog)
137         {
138             Log.v(TAG, " RECEIVED EVENT : ${rx_body?.size} bytes.")
139             SimpleLogDumper.dumpBytes("EVT[${rx_body?.size}]", rx_body)
140         }
141         try
142         {
143             val length = rx_body?.size ?: 0
144             if (length == 2744)
145             {
146                 // TAKE MODE
147                 val takeMode = when (val take0 = rx_body?.get(16 * 136 + 8))
148                 {
149                     0x01.toByte() -> "P"
150                     0x04.toByte() -> "M"
151                     0x06.toByte() -> "ASCN"
152                     0x07.toByte() -> "Video"
153                     0x0c.toByte() -> "Cont."
154                     else -> "($take0)"
155                 }
156                 statusHolder.updateValue(ICameraStatus.TAKE_MODE, takeMode)
157
158                 // FLASH Mode
159                 val flashMode = when (val flash0 = rx_body?.get(16 * 127 + 8))
160                 {
161                     0x01.toByte() -> "OFF"
162                     0x02.toByte() -> "AUTO"
163                     0x04.toByte() -> "ON"
164                     else -> "($flash0)"
165                 }
166                 statusHolder.updateValue(ICameraStatus.TORCH_MODE, flashMode)
167
168                 // WHITE BALANCE
169                 val whiteBalance = when (val wb0 = rx_body?.get(16 * 125 + 8))
170                 {
171                     0x01.toByte() -> "AUTO"
172                     0x02.toByte() -> "Daylight"
173                     0x04.toByte() -> "Cloudy"
174                     0x10.toByte() -> "Fluorescent"
175                     0x20.toByte() -> "Fluorescent CWF"
176                     0x80.toByte() -> "Incandescent"
177                     else -> "($wb0)"
178                 }
179                 statusHolder.updateValue(ICameraStatus.WHITE_BALANCE, whiteBalance)
180
181                 // ISO SENSITIVITY
182                 val isoSensitivity = when (val iso0 = rx_body?.get(16 * 70 + 0))
183                 {
184                     0x00.toByte() -> "Auto"
185                     0x01.toByte() -> "100"
186                     0x02.toByte() -> "200"
187                     0x03.toByte() -> "400"
188                     0x04.toByte() -> "800"
189                     0x05.toByte() -> "1600"
190                     0x06.toByte() -> "3200"
191                     else -> "($iso0)"
192                 }
193                 statusHolder.updateValue(ICameraStatus.ISO_SENSITIVITY, isoSensitivity)
194
195                 // EXPOSURE Compensation
196                 val exposureCompensation = when (val exprev0 = rx_body?.get(16 * 61 + 0))
197                 {
198                     0x00.toByte() -> "-3.0"
199                     0x01.toByte() -> "-2.7"
200                     0x02.toByte() -> "-2.3"
201                     0x03.toByte() -> "-2.0"
202                     0x04.toByte() -> "-1.7"
203                     0x05.toByte() -> "-1.3"
204                     0x06.toByte() -> "-1.0"
205                     0x07.toByte() -> "-0.7"
206                     0x08.toByte() -> "-0.3"
207                     0x09.toByte() -> "0.0"
208                     0x0a.toByte() -> "+0.3"
209                     0x0b.toByte() -> "+0.7"
210                     0x0c.toByte() -> "+1.0"
211                     0x0d.toByte() -> "+1.3"
212                     0x0e.toByte() -> "+1.7"
213                     0x0f.toByte() -> "+2.0"
214                     0x10.toByte() -> "+2.3"
215                     0x11.toByte() -> "+2.7"
216                     0x12.toByte() -> "+3.0"
217
218                     else -> "($exprev0)"
219                 }
220                 statusHolder.updateValue(ICameraStatus.EXPREV, exposureCompensation)
221
222                 // Shutter Speed
223                 val shutterSpeed = when (val sv = rx_body?.get(16 * 75 + 8))
224                 {
225                     0x01.toByte() -> "1/2000"
226                     0x02.toByte() -> "1/1600"
227                     0x03.toByte() -> "1/1200"
228                     0x04.toByte() -> "1/1000"
229                     0x05.toByte() -> "1/800"
230                     0x06.toByte() -> "1/600"
231                     0x07.toByte() -> "1/500"
232                     0x08.toByte() -> "1/400"
233                     0x09.toByte() -> "1/320"
234                     0x0a.toByte() -> "1/250"
235                     0x0b.toByte() -> "1/200"
236                     0x0c.toByte() -> "1/160"
237                     0x0d.toByte() -> "1/125"
238                     0x0e.toByte() -> "1/100"
239                     0x0f.toByte() -> "1/80"
240                     0x10.toByte() -> "1/60"
241                     0x11.toByte() -> "1/50"
242                     0x12.toByte() -> "1/40"
243                     0x13.toByte() -> "1/30"
244                     0x14.toByte() -> "1/25"
245                     0x15.toByte() -> "1/20"
246                     0x16.toByte() -> "1/15"
247                     0x17.toByte() -> "1/13"
248                     0x18.toByte() -> "1/10"
249                     0x19.toByte() -> "1/8"
250                     0x1a.toByte() -> "1/6"
251                     0x1b.toByte() -> "1/5"
252                     0x1c.toByte() -> "1/4"
253                     0x1d.toByte() -> "1/3"
254                     0x1e.toByte() -> "1/2.5"
255                     0x1f.toByte() -> "1/2"
256                     0x20.toByte() -> "1/1.6"
257                     0x21.toByte() -> "1/1.3"
258                     0x22.toByte() -> "1s"
259                     0x23.toByte() -> "1.3s"
260                     0x24.toByte() -> "1.5s"
261                     0x25.toByte() -> "2s"
262                     0x26.toByte() -> "2.5s"
263                     0x27.toByte() -> "3s"
264                     0x28.toByte() -> "4s"
265                     0x29.toByte() -> "5s"
266                     0x2a.toByte() -> "6s"
267                     0x2b.toByte() -> "8s"
268                     0x2c.toByte() -> "10s"
269                     0x2d.toByte() -> "13s"
270                     0x2e.toByte() -> "15s"
271                     0x2f.toByte() -> "20s"
272                     0x30.toByte() -> "25s"
273                     0x31.toByte() -> "30s"
274                     0x00.toByte() -> ""
275                     else -> "($sv)"
276                 }
277                 statusHolder.updateValue(ICameraStatus.SHUTTER_SPEED, shutterSpeed)
278
279                 // IMAGE SIZE
280                 val imageSize = when (val picSize = (rx_body?.get(16 * 129 + 8) ?: 0).toInt() + (rx_body?.get(16 * 129 + 9) ?: 0) * 256 + ((rx_body?.get(16 * 129 + 10) ?: 0) * 65536) + ((rx_body?.get(16 * 129 + 11) ?: 0) * 16777216))
281                 {
282                     0x00010000 -> "4608x3456"
283                     0x00004000 -> "4608x3072"
284                     0x00001000 -> "4608x2592"
285                     0x00000400 -> "3648x2736"
286                     0x00000020 -> "2592x1944"
287                     0x00000008 -> "2048x1536"
288                     0x00000004 -> "1920x1080"
289                     0x00000001 ->  "640x480"
290                     else -> "($picSize)"
291                 }
292                 statusHolder.updateValue(ICameraStatus.IMAGE_SIZE, imageSize)
293
294                 // MOVIE SIZE
295                 val movieSize = when (val videoSize = (rx_body?.get(16 * 132 + 0) ?: 0).toInt() + (rx_body?.get(16 * 132 + 1) ?: 0) * 256 + ((rx_body?.get(16 * 132 + 2) ?: 0) * 65536) + ((rx_body?.get(16 * 132 + 3) ?: 0) * 16777216))
296                 {
297                     0x00400000 -> "1920×1080 30p"
298                     0x00080000 -> "1280x720 60p"
299                     0x00040000 -> "1280x720 30p"
300                     0x00000100 -> "640x480 120p"
301                     0x00000040 -> "640x480 30p"
302                     else -> "[$videoSize]"
303                 }
304                 statusHolder.updateValue(ICameraStatus.MOVIE_SIZE, movieSize)
305
306             }
307         }
308         catch (e: Exception)
309         {
310             e.printStackTrace()
311         }
312     }
313
314     companion object
315     {
316         private val TAG = PixproStatusChecker::class.java.simpleName
317         private const val EVENT_POLL_QUEUE_MS = 550  // 550ms
318         private const val isDumpLog = false
319     }
320
321 }