OSDN Git Service

いったんコミット。
authorMRSa <mrsa@myad.jp>
Tue, 23 Feb 2021 00:45:49 +0000 (09:45 +0900)
committerMRSa <mrsa@myad.jp>
Tue, 23 Feb 2021 00:45:49 +0000 (09:45 +0900)
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/brainwave/BrainwaveDataHolder.kt
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/brainwave/IDetectSensingReceiver.kt [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaCaptureControl.kt [new file with mode: 0644]

index 33dec9c..0578d53 100644 (file)
@@ -13,16 +13,19 @@ import jp.osdn.gokigen.thetathoughtshutter.R.layout
 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult
 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
 import jp.osdn.gokigen.thetathoughtshutter.brainwave.BrainwaveDataHolder
+import jp.osdn.gokigen.thetathoughtshutter.brainwave.IDetectSensingReceiver
+import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaCaptureControl
 import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaHardwareControl
 import jp.osdn.gokigen.thetathoughtshutter.theta.ThetaSetupBluetoothSPP
 import jp.osdn.gokigen.thetathoughtshutter.theta.operation.IOperationCallback
 import java.lang.Exception
 
-class MainActivity : PluginActivity(), IBluetoothScanResult
+class MainActivity : PluginActivity(), IBluetoothScanResult, IDetectSensingReceiver
 {
     private val thetaHardwareControl = ThetaHardwareControl(this)
+    private val thetaCaptureControl = ThetaCaptureControl("http://localhost:8080")
     private val applicationStatus : MyApplicationStatus = MyApplicationStatus()
-    private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder(), this)
+    private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder(this), this)
 
     companion object
     {
@@ -44,24 +47,6 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
 
             override fun onKeyUp(keyCode: Int, event: KeyEvent?)
             {
-                if (keyCode == KeyReceiver.KEYCODE_CAMERA)   // Shutterボタン
-                {
-                    when (applicationStatus.status)
-                    {
-                        MyApplicationStatus.Status.Connected -> {
-                            // EEGからの情報を取得して撮影する
-                            applicationStatus.status = MyApplicationStatus.Status.Scanning
-                        }
-                        MyApplicationStatus.Status.Scanning -> {
-                            // スタンバイ状態に戻す
-                            applicationStatus.status = MyApplicationStatus.Status.Connected
-                        }
-                        else -> {
-                            // ダミー処理 (仮にEEG接続失敗のステータスにする)
-                            applicationStatus.status = MyApplicationStatus.Status.FailedInitialize
-                        }
-                    }
-                }
                 if (keyCode == KeyReceiver.KEYCODE_WLAN_ON_OFF) // Wirelessボタン
                 {
                     if (applicationStatus.status == MyApplicationStatus.Status.Initialized)
@@ -70,6 +55,7 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
                         connectToEEG()
                     }
                 }
+/*
                 if (keyCode == KeyReceiver.KEYCODE_MEDIA_RECORD) // Modeボタン
                 {
                     when (applicationStatus.status) {
@@ -91,11 +77,14 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
                         }
                     }
                 }
-/*
                 if (keyCode == KeyReceiver.KEYCODE_FUNCTION)   // Fnボタン (Z1のみ)
                 {
 
                 }
+                if (keyCode == KeyReceiver.KEYCODE_CAMERA)   // Shutterボタン
+                {
+
+                }
 */
                 updateStatus(applicationStatus.status)
             }
@@ -155,7 +144,6 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         applicationStatus.status = MyApplicationStatus.Status.Initialized
     }
 
-
     private fun updateStatus(currentStatus : MyApplicationStatus.Status)
     {
         try
@@ -220,6 +208,14 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         {
             Log.v(TAG , " isApConnected : $isApConnected")
         }
+        try
+        {
+            thetaCaptureControl.setupCaptureMode()
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
         initializeBluetooth()
     }
 
@@ -236,7 +232,6 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
         }
     }
 
-
     private fun initializeBluetooth()
     {
         try
@@ -297,6 +292,58 @@ class MainActivity : PluginActivity(), IBluetoothScanResult
             e.printStackTrace()
         }
     }
+
+    override fun startSensing()
+    {
+        // データの検出を開始する
+        Log.v(TAG, "  ===== START SENSING =====")
+        applicationStatus.status = MyApplicationStatus.Status.Scanning
+        updateStatus(applicationStatus.status)
+    }
+
+    override fun detectAttention()
+    {
+        Log.v(TAG, "  ===== DETECT ATTENTION =====")
+        applicationStatus.status = MyApplicationStatus.Status.Syncing
+        updateStatus(applicationStatus.status)
+    }
+
+    override fun detectMediation()
+    {
+        //Log.v(TAG, "  ===== DETECT MEDIATION =====")
+    }
+
+    override fun lostAttention()
+    {
+        Log.v(TAG, "  ===== LOST ATTENTION =====")
+        applicationStatus.status = MyApplicationStatus.Status.Scanning
+        updateStatus(applicationStatus.status)
+    }
+
+    override fun lostMediation()
+    {
+        //Log.v(TAG, "  ===== LOST MEDIATION =====")
+    }
+
+    override fun detectAttentionThreshold()
+    {
+        Log.v(TAG, "  ===== DETECT ATTENTION THRESHOLD =====")
+        try
+        {
+            // 静止画の撮影!
+            thetaCaptureControl.doCapture()
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
+    override fun detectMediationThreshold()
+    {
+        Log.v(TAG, "  ===== DETECT MEDIATION THRESHOLD =====")
+    }
+
 }
 
 //
index 5adfb76..c2484e8 100644 (file)
@@ -1,14 +1,17 @@
 package jp.osdn.gokigen.thetathoughtshutter.brainwave
 
 import android.util.Log
-import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
 import java.util.*
 
-class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
+class BrainwaveDataHolder(private val receiver: IDetectSensingReceiver? = null, maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
 {
     companion object
     {
         private val TAG = BrainwaveDataHolder::class.java.simpleName
+        private const val THRESHOLD_ATTENTION = 80
+        private const val THRESHOLD_MEDIATION = 90
+        private const val COUNT_CONTINUOUS_ATTENTION = 5
+        private const val COUNT_CONTINUOUS_MEDIATION = 5
     }
 
     private var valueBuffer: IntArray
@@ -16,6 +19,9 @@ class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
     private var maxBufferSize = 0
     private var currentPosition = 0
     private var bufferIsFull = false
+    private var attentionCount = 0
+    private var mediationCount = 0
+    private var dataReceived = false
 
     init
     {
@@ -58,6 +64,58 @@ class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
                 val attention = currentSummaryData.getAttention()
                 val mediation = currentSummaryData.getMediation()
                 Log.v(TAG, "  ATTENTION : $attention   MEDIATION : $mediation")
+                if ((!dataReceived)&&(attention > 0)&&(mediation > 0))
+                {
+                    // データの受信を開始した
+                    dataReceived = true
+                    receiver?.startSensing()
+                }
+
+                if (attention > THRESHOLD_ATTENTION)
+                {
+                    if (attentionCount == 0)
+                    {
+                        receiver?.detectAttention()
+                    }
+                    attentionCount++
+                    if (attentionCount == COUNT_CONTINUOUS_ATTENTION)
+                    {
+                        // 検出を通知する
+                        receiver?.detectAttentionThreshold()
+                    }
+                }
+                else
+                {
+                    // リミッターを下回ったので連続カウンタをクリアする
+                    if( attentionCount > 0)
+                    {
+                        receiver?.lostAttention()
+                    }
+                    attentionCount = 0
+                }
+
+                if (mediation > THRESHOLD_MEDIATION)
+                {
+                    if (mediationCount == 0)
+                    {
+                        receiver?.detectMediation()
+                    }
+                    mediationCount++
+                    if (mediationCount == COUNT_CONTINUOUS_MEDIATION)
+                    {
+                        // 検出を通知する
+                        receiver?.detectMediationThreshold()
+                    }
+                }
+                else
+                {
+                    // リミッターを下回ったので連続カウンタをクリアする
+                    if( mediationCount > 0)
+                    {
+                        receiver?.lostMediation()
+                    }
+                    mediationCount = 0
+                }
             }
             catch (e : Exception)
             {
diff --git a/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/brainwave/IDetectSensingReceiver.kt b/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/brainwave/IDetectSensingReceiver.kt
new file mode 100644 (file)
index 0000000..9bd5a02
--- /dev/null
@@ -0,0 +1,12 @@
+package jp.osdn.gokigen.thetathoughtshutter.brainwave
+
+interface IDetectSensingReceiver
+{
+    fun startSensing()
+    fun detectAttention()
+    fun lostAttention()
+    fun detectAttentionThreshold()
+    fun detectMediation()
+    fun lostMediation()
+    fun detectMediationThreshold()
+}
diff --git a/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaCaptureControl.kt b/app/src/main/java/jp/osdn/gokigen/thetathoughtshutter/theta/ThetaCaptureControl.kt
new file mode 100644 (file)
index 0000000..ac20e83
--- /dev/null
@@ -0,0 +1,49 @@
+package jp.osdn.gokigen.thetathoughtshutter.theta
+
+import android.util.Log
+import jp.osdn.gokigen.thetathoughtshutter.theta.operation.IOperationCallback
+import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaOptionSetControl
+import jp.osdn.gokigen.thetathoughtshutter.theta.operation.ThetaSingleShotControl
+import java.lang.Exception
+
+class ThetaCaptureControl(executeUrl : String = "http://192.168.1.1")
+{
+    private val setOption = ThetaOptionSetControl(executeUrl)
+    private val singleShot = ThetaSingleShotControl(executeUrl)
+    private var isCapturing = false
+
+    companion object
+    {
+        private val TAG = ThetaCaptureControl::class.java.simpleName
+    }
+
+    fun setupCaptureMode()
+    {
+        setOption.setOptions("\"captureMode\" : \"image\"", object : IOperationCallback {
+            override fun operationExecuted(result: Int, resultStr: String?)
+            {
+                if (resultStr != null)
+                {
+                    Log.v(TAG, "set to Image :  $resultStr ")
+                }
+            }
+        })
+    }
+
+    fun doCapture()
+    {
+        try
+        {
+            if (isCapturing)
+            {
+                Log.v(TAG, "ALREADY START CAPTURE SEQUENCE")
+                return
+            }
+            singleShot.singleShot()  // まじか、応答しない...
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+}
\ No newline at end of file