OSDN Git Service

データが報告されてくるようになった。
[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 updateStatus(currentStatus : MyApplicationStatus.Status)
136     {
137         try
138         {
139             when (currentStatus) {
140                 MyApplicationStatus.Status.Initialized -> {
141                     Log.v(TAG, " INITIALIZED")
142                     thetaHardwareControl.controlLED(LedTarget.LED3, 1500, LedColor.GREEN)  // WIFIランプ
143                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)     // Liveランプ (OFF)
144                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)      // 赤ランプ
145                 }
146                 MyApplicationStatus.Status.Searching -> {
147                     Log.v(TAG, " SEARCHING")
148                     thetaHardwareControl.controlLED(LedTarget.LED3, 250, LedColor.GREEN)  // WIFIランプ
149                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
150                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
151                 }
152                 MyApplicationStatus.Status.Connected -> {
153                     Log.v(TAG, " CONNECTED")
154                     thetaHardwareControl.controlLED(LedTarget.LED3,  0, LedColor.GREEN)   // WIFIランプ
155                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
156                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)     // 赤ランプ
157                 }
158                 MyApplicationStatus.Status.Scanning -> {
159                     Log.v(TAG, " SCANNING")
160                     thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
161                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
162                     thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
163                 }
164                 MyApplicationStatus.Status.Syncing -> {
165                     Log.v(TAG, " SYNCING")
166                     thetaHardwareControl.controlLED(LedTarget.LED3, 0, LedColor.GREEN)    // WIFIランプ
167                     thetaHardwareControl.controlLED(LedTarget.LED6, 0, LedColor.BLUE)     // Liveランプ (ON)
168                     thetaHardwareControl.controlLED(LedTarget.LED7, 0, LedColor.RED)      // 赤ランプ
169                 }
170                 MyApplicationStatus.Status.FailedInitialize -> {
171                     Log.v(TAG, " FAILED INITIALIZE")
172                     thetaHardwareControl.controlLED(LedTarget.LED3, 250, LedColor.GREEN)  // WIFIランプ
173                     thetaHardwareControl.controlLED(LedTarget.LED6, -1, LedColor.BLUE)    // Liveランプ (OFF)
174                     thetaHardwareControl.controlLED(LedTarget.LED7, 250, LedColor.RED)    // 赤ランプ
175                 }
176                 else -> {
177                     Log.v(TAG, " UNDEFINED")
178                     thetaHardwareControl.controlLED(LedTarget.LED3, -1, LedColor.GREEN)  // WIFIランプ
179                     thetaHardwareControl.controlLED(LedTarget.LED4, -1, LedColor.BLUE)   // カメラランプ
180                     thetaHardwareControl.controlLED(LedTarget.LED5, -1, LedColor.BLUE)   // ムービーランプ
181                     thetaHardwareControl.controlLED(LedTarget.LED6,  0, LedColor.BLUE)   // Liveランプ (ON)
182                     thetaHardwareControl.controlLED(LedTarget.LED7, -1, LedColor.RED)    // 赤ランプ
183                 }
184             }
185         }
186         catch (e : Exception)
187         {
188             e.printStackTrace()
189         }
190     }
191
192     override fun onResume()
193     {
194         super.onResume()
195         if (isApConnected)
196         {
197             Log.v(TAG , " isApConnected : $isApConnected")
198         }
199         initializeBluetooth()
200     }
201
202     private fun initializeBluetooth()
203     {
204         try
205         {
206             val thread = Thread {
207                 try
208                 {
209                     val setupBluetooth = ThetaSetupBluetoothSPP("http://localhost:8080")
210                     setupBluetooth.setupBluetoothSPP(object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
211                     {
212                         Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr ($result)")
213
214                         if (result == 0)
215                         {
216                             // Bluetoothの初期化終了
217                             applicationStatus.status = MyApplicationStatus.Status.Initialized
218                             updateStatus(applicationStatus.status)
219                         }
220                     }})
221                 }
222                 catch (e: Exception)
223                 {
224                     e.printStackTrace()
225                 }
226             }
227             thread.start()
228         }
229         catch (e: Exception)
230         {
231             e.printStackTrace()
232         }
233     }
234
235     override fun foundBluetoothDevice(device: BluetoothDevice)
236     {
237         try
238         {
239             // Bluetoothデバイスが見つかった!
240             applicationStatus.status = MyApplicationStatus.Status.Connected
241             updateStatus(applicationStatus.status)
242         }
243         catch (e : Exception)
244         {
245             e.printStackTrace()
246         }
247     }
248
249     override fun notFindBluetoothDevice()
250     {
251         try
252         {
253             // Bluetoothデバイスが見つからなかった...
254             applicationStatus.status = MyApplicationStatus.Status.Initialized
255             updateStatus(applicationStatus.status)
256         }
257         catch (e : Exception)
258         {
259             e.printStackTrace()
260         }
261     }
262 }
263
264 //
265 // -----------------------------------------------------
266 //  LED1 : 電源ランプ
267 //  LED2 : カメラステータス ランプ(レンズとマイクの間)
268 //  LED3 : ワイヤレスマーク ランプ
269 //  LED4 : キャプチャーモード (カメラ)
270 //  LED5 : キャプチャーモード (ムービー)
271 //  LED6 : キャプチャーモード (LIVEストリーミング)
272 //  LED7 : ビデオ録画 ランプ
273 //  LED8 : メモリ警告ランプ
274 //
275 //  BTN1 : 電源ボタン
276 //  BTN2 : ワイヤレスボタン
277 //  BTN3 : モードボタン
278 //  SHUT : シャッターボタン
279 // -----------------------------------------------------
280 //
281 //  [制御可能なLED]
282 //    - LED3~LED8 : カラー : "blue", "green", "red", "cyan", "magenta", "yellow", "white"
283 //    - ブリンク間隔 : 1~2000 msec
284 //
285 //  [KeyCode]
286 //    - 27  : Shutter Button
287 //    - 130 : Mode Button
288 //    - 284 : Wireless Button
289 //    - 119 : Fn Button (Z1 Only)
290 //
291 //
292 //   http://localhost:8080/
293 //