OSDN Git Service

33dec9cd37290324253b5f61658fb03d0bf4327f
[gokigen/ThetaThoughtShutter.git] / app / src / main / java / jp / osdn / gokigen / thetathoughtshutter / MainActivity.kt
1 package jp.osdn.gokigen.thetathoughtshutter
2
3 import android.bluetooth.BluetoothDevice
4 import android.os.Bundle
5 import android.util.Log
6 import android.view.KeyEvent
7 import com.theta360.pluginlibrary.activity.PluginActivity
8 import com.theta360.pluginlibrary.callback.KeyCallback
9 import com.theta360.pluginlibrary.receiver.KeyReceiver
10 import com.theta360.pluginlibrary.values.LedColor
11 import com.theta360.pluginlibrary.values.LedTarget
12 import jp.osdn.gokigen.thetathoughtshutter.R.layout
13 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult
14 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
15 import jp.osdn.gokigen.thetathoughtshutter.brainwave.BrainwaveDataHolder
16 import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaHardwareControl
17 import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaSetupBluetoothSPP
18 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.IOperationCallback
19 import java.lang.Exception
20
21 class MainActivity : PluginActivity(), IBluetoothScanResult
22 {
23     private val thetaHardwareControl = ThetaHardwareControl(this)
24     private val applicationStatus : MyApplicationStatus = MyApplicationStatus()
25     private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder(), this)
26
27     companion object
28     {
29         private val TAG = MainActivity::class.java.simpleName
30     }
31
32     override fun onCreate(savedInstanceState: Bundle?)
33     {
34         super.onCreate(savedInstanceState)
35         setContentView(layout.activity_main)
36
37         setAutoClose(true)
38
39         setKeyCallback(object : KeyCallback {
40             override fun onKeyDown(keyCode: Int, event: KeyEvent?)
41             {
42
43             }
44
45             override fun onKeyUp(keyCode: Int, event: KeyEvent?)
46             {
47                 if (keyCode == KeyReceiver.KEYCODE_CAMERA)   // Shutterボタン
48                 {
49                     when (applicationStatus.status)
50                     {
51                         MyApplicationStatus.Status.Connected -> {
52                             // EEGからの情報を取得して撮影する
53                             applicationStatus.status = MyApplicationStatus.Status.Scanning
54                         }
55                         MyApplicationStatus.Status.Scanning -> {
56                             // スタンバイ状態に戻す
57                             applicationStatus.status = MyApplicationStatus.Status.Connected
58                         }
59                         else -> {
60                             // ダミー処理 (仮にEEG接続失敗のステータスにする)
61                             applicationStatus.status = MyApplicationStatus.Status.FailedInitialize
62                         }
63                     }
64                 }
65                 if (keyCode == KeyReceiver.KEYCODE_WLAN_ON_OFF) // Wirelessボタン
66                 {
67                     if (applicationStatus.status == MyApplicationStatus.Status.Initialized)
68                     {
69                         // Bluetooth SPPで EEGに接続する
70                         connectToEEG()
71                     }
72                 }
73                 if (keyCode == KeyReceiver.KEYCODE_MEDIA_RECORD) // Modeボタン
74                 {
75                     when (applicationStatus.status) {
76                         MyApplicationStatus.Status.Searching -> {
77                             // ダミー処理 (EEG接続完了)
78                             applicationStatus.status = MyApplicationStatus.Status.Connected
79                         }
80                         MyApplicationStatus.Status.Scanning -> {
81                             // ダミー処理 (高まっている状態)
82                             applicationStatus.status = MyApplicationStatus.Status.Syncing
83                         }
84                         MyApplicationStatus.Status.Syncing -> {
85                             // ダミー処理 (高まるのを待っている状態)
86                             applicationStatus.status = MyApplicationStatus.Status.Scanning
87                         }
88                         else -> {
89                             // ダミー処理 (初期化完了)
90                             applicationStatus.status = MyApplicationStatus.Status.Initialized
91                         }
92                     }
93                 }
94 /*
95                 if (keyCode == KeyReceiver.KEYCODE_FUNCTION)   // Fnボタン (Z1のみ)
96                 {
97
98                 }
99 */
100                 updateStatus(applicationStatus.status)
101             }
102
103             override fun onKeyLongPress(keyCode: Int, event: KeyEvent?)
104             {
105
106             }
107         })
108         updateStatus(applicationStatus.status)
109     }
110
111     // Bluetooth SPPで EEGに接続する
112     private fun connectToEEG()
113     {
114         try
115         {
116             val thread = Thread {
117                 try
118                 {
119                     bluetoothConnection.connect("MindWave Mobile")
120                 }
121                 catch (e: Exception)
122                 {
123                     e.printStackTrace()
124                 }
125             }
126             thread.start()
127         }
128         catch (e: Exception)
129         {
130             e.printStackTrace()
131         }
132         applicationStatus.status = MyApplicationStatus.Status.Searching
133     }
134
135     private fun disconnectToEEG()
136     {
137         try
138         {
139             val thread = Thread {
140                 try
141                 {
142                     bluetoothConnection.disconnect()
143                 }
144                 catch (e: Exception)
145                 {
146                     e.printStackTrace()
147                 }
148             }
149             thread.start()
150         }
151         catch (e: Exception)
152         {
153             e.printStackTrace()
154         }
155         applicationStatus.status = MyApplicationStatus.Status.Initialized
156     }
157
158
159     private fun updateStatus(currentStatus : MyApplicationStatus.Status)
160     {
161         try
162         {
163             when (currentStatus) {
164                 MyApplicationStatus.Status.Initialized -> {
165                     Log.v(TAG, " INITIALIZED")
166                     thetaHardwareControl.controlLED(LedTarget.LED3, 1500, LedColor.GREEN)  // WIFIランプ
167                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)     // Liveランプ (OFF)
168                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)      // 赤ランプ
169                 }
170                 MyApplicationStatus.Status.Searching -> {
171                     Log.v(TAG, " SEARCHING")
172                     thetaHardwareControl.controlLED(LedTarget.LED3, 250, LedColor.GREEN)  // WIFIランプ
173                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
174                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
175                 }
176                 MyApplicationStatus.Status.Connected -> {
177                     Log.v(TAG, " CONNECTED")
178                     thetaHardwareControl.controlLED(LedTarget.LED3,  0, LedColor.GREEN)   // WIFIランプ
179                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
180                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
181                 }
182                 MyApplicationStatus.Status.Scanning -> {
183                     Log.v(TAG, " SCANNING")
184                     thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
185                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
186                     thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
187                 }
188                 MyApplicationStatus.Status.Syncing -> {
189                     Log.v(TAG, " SYNCING")
190                     thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
191                     thetaHardwareControl.controlLED(LedTarget.LED6, 0, LedColor.BLUE)     // Liveランプ (ON)
192                     thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
193                 }
194                 MyApplicationStatus.Status.FailedInitialize -> {
195                     Log.v(TAG, " FAILED INITIALIZE")
196                     thetaHardwareControl.controlLED(LedTarget.LED3, 250, LedColor.GREEN)  // WIFIランプ
197                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
198                     thetaHardwareControl.controlLED(LedTarget.LED7, 250, LedColor.RED)    // 赤ランプ
199                 }
200                 else -> {
201                     Log.v(TAG, " UNDEFINED")
202                     thetaHardwareControl.controlLED(LedTarget.LED3, -1, LedColor.GREEN)  // WIFIランプ
203                     thetaHardwareControl.controlLED(LedTarget.LED4, -1, LedColor.BLUE)   // カメラランプ
204                     thetaHardwareControl.controlLED(LedTarget.LED5, -1, LedColor.BLUE)   // ムービーランプ
205                     thetaHardwareControl.controlLED(LedTarget.LED6,  0, LedColor.BLUE)   // Liveランプ (ON)
206                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)    // 赤ランプ
207                 }
208             }
209         }
210         catch (e : Exception)
211         {
212             e.printStackTrace()
213         }
214     }
215
216     override fun onResume()
217     {
218         super.onResume()
219         if (isApConnected)
220         {
221             Log.v(TAG , " isApConnected : $isApConnected")
222         }
223         initializeBluetooth()
224     }
225
226     override fun onPause()
227     {
228         super.onPause()
229         try
230         {
231             disconnectToEEG()
232         }
233         catch (e : Exception)
234         {
235             e.printStackTrace()
236         }
237     }
238
239
240     private fun initializeBluetooth()
241     {
242         try
243         {
244             val thread = Thread {
245                 try
246                 {
247                     val setupBluetooth = ThetaSetupBluetoothSPP("http://localhost:8080")
248                     setupBluetooth.setupBluetoothSPP(object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
249                     {
250                         Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr ($result)")
251
252                         if (result == 0)
253                         {
254                             // Bluetoothの初期化終了
255                             applicationStatus.status = MyApplicationStatus.Status.Initialized
256                             updateStatus(applicationStatus.status)
257                         }
258                     }})
259                 }
260                 catch (e: Exception)
261                 {
262                     e.printStackTrace()
263                 }
264             }
265             thread.start()
266         }
267         catch (e: Exception)
268         {
269             e.printStackTrace()
270         }
271     }
272
273     override fun foundBluetoothDevice(device: BluetoothDevice)
274     {
275         try
276         {
277             // Bluetoothデバイスが見つかった!
278             applicationStatus.status = MyApplicationStatus.Status.Connected
279             updateStatus(applicationStatus.status)
280         }
281         catch (e : Exception)
282         {
283             e.printStackTrace()
284         }
285     }
286
287     override fun notFindBluetoothDevice()
288     {
289         try
290         {
291             // Bluetoothデバイスが見つからなかった...
292             applicationStatus.status = MyApplicationStatus.Status.Initialized
293             updateStatus(applicationStatus.status)
294         }
295         catch (e : Exception)
296         {
297             e.printStackTrace()
298         }
299     }
300 }
301
302 //
303 // -----------------------------------------------------
304 //  LED1 : 電源ランプ
305 //  LED2 : カメラステータス ランプ(レンズとマイクの間)
306 //  LED3 : ワイヤレスマーク ランプ
307 //  LED4 : キャプチャーモード (カメラ)
308 //  LED5 : キャプチャーモード (ムービー)
309 //  LED6 : キャプチャーモード (LIVEストリーミング)
310 //  LED7 : ビデオ録画 ランプ
311 //  LED8 : メモリ警告ランプ
312 //
313 //  BTN1 : 電源ボタン
314 //  BTN2 : ワイヤレスボタン
315 //  BTN3 : モードボタン
316 //  SHUT : シャッターボタン
317 // -----------------------------------------------------
318 //
319 //  [制御可能なLED]
320 //    - LED3~LED8 : カラー : "blue", "green", "red", "cyan", "magenta", "yellow", "white"
321 //    - ブリンク間隔 : 1~2000 msec
322 //
323 //  [KeyCode]
324 //    - 27  : Shutter Button
325 //    - 130 : Mode Button
326 //    - 284 : Wireless Button
327 //    - 119 : Fn Button (Z1 Only)
328 //
329 //
330 //   http://localhost:8080/
331 //