OSDN Git Service

5adfb76f5f90636171891e33de153befdab12559
[gokigen/ThetaThoughtShutter.git] / app / src / main / java / jp / osdn / gokigen / thetathoughtshutter / brainwave / BrainwaveDataHolder.kt
1 package jp.osdn.gokigen.thetathoughtshutter.brainwave
2
3 import android.util.Log
4 import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
5 import java.util.*
6
7 class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
8 {
9     companion object
10     {
11         private val TAG = BrainwaveDataHolder::class.java.simpleName
12     }
13
14     private var valueBuffer: IntArray
15     private var currentSummaryData = BrainwaveSummaryData()
16     private var maxBufferSize = 0
17     private var currentPosition = 0
18     private var bufferIsFull = false
19
20     init
21     {
22         this.maxBufferSize = maxBufferSize
23         valueBuffer = IntArray(maxBufferSize)
24     }
25
26     override fun receivedRawData(value: Int)
27     {
28         //Log.v(TAG, " receivedRawData() : $value");
29         try
30         {
31             valueBuffer[currentPosition] = value
32             currentPosition++
33             if (currentPosition == maxBufferSize)
34             {
35                 currentPosition = 0
36                 bufferIsFull = true
37             }
38         }
39         catch (e: Exception)
40         {
41             e.printStackTrace()
42         }
43     }
44
45     override fun receivedSummaryData(data: ByteArray?)
46     {
47         if (data != null)
48         {
49             // Log.v(TAG, " receivedSummaryData() : ${data.size} bytes.")
50             if (!currentSummaryData.update(data))
51             {
52                 // parse failure...
53                 Log.v(TAG, " FAIL : PARSE EEG SUMMARY DATA (" + data.size + ")")
54                 return
55             }
56             try
57             {
58                 val attention = currentSummaryData.getAttention()
59                 val mediation = currentSummaryData.getMediation()
60                 Log.v(TAG, "  ATTENTION : $attention   MEDIATION : $mediation")
61             }
62             catch (e : Exception)
63             {
64                 e.printStackTrace()
65             }
66         }
67     }
68
69     fun getSummaryData(): BrainwaveSummaryData
70     {
71         return currentSummaryData
72     }
73
74     fun getValues(size: Int): IntArray?
75     {
76         var replyData: IntArray? = null
77         try {
78             var endPosition = currentPosition - 1
79             if (currentPosition > size) {
80                 return Arrays.copyOfRange(valueBuffer, endPosition - size, endPosition)
81             }
82             if (!bufferIsFull) {
83                 return Arrays.copyOfRange(valueBuffer, 0, endPosition)
84             }
85             if (currentPosition == 0) {
86                 endPosition = maxBufferSize - 1
87                 return Arrays.copyOfRange(valueBuffer, endPosition - size, endPosition)
88             }
89             val remainSize = size - (currentPosition - 1)
90             val size0: IntArray = Arrays.copyOfRange(valueBuffer, 0, currentPosition - 1)
91             val size1: IntArray = Arrays.copyOfRange(
92                 valueBuffer,
93                 maxBufferSize - 1 - remainSize, maxBufferSize - 1
94             )
95             replyData = IntArray(size)
96             System.arraycopy(size1, 0, replyData, 0, size1.size)
97             System.arraycopy(size0, 0, replyData, size1.size, size0.size)
98         } catch (e: Exception) {
99             e.printStackTrace()
100         }
101         return replyData
102     }
103
104 }