初始代码包下载:链接:https://pan.baidu.com/s/1nEb5kmk8PabkFmK3bO8Fww
提取码:r5pu
解压后用eclipse打开该项目
在src中建立名字为lib的package,
讲这三个.jar文件复制到lib下
导入jar文件方法如下:
大致的目录结构如下:
然后修改brainkeyboard.java代码为:
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;
import neurosky.ThinkGearSocket;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
public class brainkeyboard extends PApplet {
/*可读取的参数 int delta, int theta, int low_alpha, int high_alpha,
int low_beta, int high_beta, int low_gamma, int mid_gamma
*/
ThinkGearSocket neuroSocket;
static int attention = 0;
static int mytheta=0;
static int mydelta=0;
static int mylow_alpha=0;
static int myhigh_alpha=0;
static int mylow_beta=0,myhigh_beta=0;
int blinkSt = 0;
int blink = 0;
int k=0;
/*------------------------------------*/
String[] letter = {"A","B","C","D","E","F","G","H","I","J","K","L"," "," ","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","-","-"};
/*------------------------------------*/
String input = " ";
String lockinput=" ";
int t=0;
PFont font;
PImage bg;
int i=30,j=110;
/*------------------------------------*/
public void setup()
{
size (550, 300);
ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
try
{
neuroSocket.start();
}
catch (ConnectException e) {
e.printStackTrace();
}
fill(0);
font = createFont("Courier",48);
textFont(font);
bg=loadImage("1.jpg");
}
/*------------------------------------*/
public void draw() {
background(120);
fill(255, 255, 0);
textSize(12);
text("Attention: " + attention,100, 10);
strokeJoin(ROUND);
stroke(50,100,150);
strokeWeight(3);
fill(255);
rect(30,30,500,50);
// ----------------------------------------
if (blinkSt>0)
{
if(letter[k]=="-"){
input = input.substring(0,input.length()-1);
}
else{
input = input+letter[k];
lockinput = input;
fill(0);
textSize(18);
text(input, 40, 48, 195, 30);
textSize(48);
}
}
//-----------------------------------------------------
fill(255, 255, 0);
textSize(12);
text("Blink: " + blinkSt, 10, 10);
blinkSt=0;
fill(0);
textSize(18);
text(lockinput, 40, 48, 500, 30);
textSize(48);
//------------------------------------------------------
image(bg,30,110);
noFill();
stroke(255);
rect(i,j,50,50);
// 自己加的两行
// print( "i:"+i +" " + "j:"+ j + " " + "k:"+ k + " " +"Blinkst:" + blinkSt);
// ---------------------------------------------
if(attention > 40)
{
t=50;
println(" Attention: " + attention);
// attention=0;
}
if(t ==50){
if(i<450){
i=i+50;
}
else{
i=30;
if(j<200)
{
j=j+50;
}
else{
j=110;
}
}
// -------------------------------------
if(k<29)
{
k =k+1;
}
else
{k=0;}
t=0;
}
t++;
}
/*------------------------------------*/
public void blinkEvent(int blinkStrength)
{
blinkSt = blinkStrength;
// blink = 1;
}
/*------------------------------------*/
public void attentionEvent(int attentionLevel)
{
attention = attentionLevel;
}
public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma)
{
mytheta=theta;
mydelta=delta;
myhigh_alpha=high_alpha;
mylow_alpha=low_alpha;
mylow_beta=low_beta;
myhigh_beta=high_beta;
}
/*------------------------------------*/
public void stop() {
neuroSocket.stop();
super.stop();
}
//定时
static Timer timer =new Timer(true);
// 注意,javax.swing包中也有一个Timer类,如果import中用到swing包,要注意名字的冲突。
static TimerTask task = new TimerTask() {
public void run() {
//每次需要执行的代码放到这里面。
System.out.println("专注度:"+attention+" theta:"+mytheta+" mydelta:"+mydelta
+" mylow_alpha:"+mylow_alpha+" myhigh_alpha:"+myhigh_alpha+" mylow_beta:"+mylow_beta+" myhigh_beta:"+myhigh_beta);
System.out.println( System.currentTimeMillis());
try {
PreparedStatement ps=conn.prepareStatement("insert into biao values(?,?,?,?,?,?,?)");
ps.setInt(1, attention );
ps.setInt(2, mytheta);
ps.setInt(3, mydelta);
ps.setInt(4, mylow_alpha);
ps.setInt(5, myhigh_alpha);
ps.setInt(6,mylow_beta );
ps.setInt(7, myhigh_beta);
ps.execute();
System.out.println("zhou");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//"jdbc:mysql://localhost:3306/RUNOOB";
static final String DB_URL =
"jdbc:mysql://localhost:3306/zhou?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "123456";
static Connection conn = null;
static public void main(String args[]) {
try{
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}
timer.schedule(task,10000,1000);
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "brainkeyboard" });
}
}
功能:读取脑波耳机中的α、β、attention等数据值存放到mysql数据库,1秒钟更新一次
数据如下:
ThinkGear.jar源代码如下:
/* | |
* | |
* This provides a simple socket connector to the NeuroSky MindWave ThinkGear connector. | |
* For more info visit http://crea.tion.to/processing/thinkgear-java-socket | |
* | |
* No warranty or any stuffs like that. | |
* | |
* Have fun! | |
* Andreas Borg | |
* borg@elevated.to | |
* | |
* | |
* (c) 2010 | |
* | |
* This library is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU Lesser General Public | |
* License as published by the Free Software Foundation; either | |
* version 2.1 of the License, or (at your option) any later version. | |
* | |
* This library is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General | |
* Public License along with this library; if not, write to the | |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
* Boston, MA 02111-1307 USA | |
* | |
* @author Andreas Borg, borg@elevated.to | |
* @modified June, 2011 | |
* @version 1.0 | |
* | |
* | |
* This library is following the same design as the one developed by Jorge C. S. Cardoso for the MindSet device. | |
* The MindWave device can communicate to a socket over JSON instead of the serial port. That makes it easier and tidier | |
* to talk between the device and Java. For instructions on how to use the callback listeners please refer to | |
* | |
* http://jorgecardoso.eu/processing/MindSetProcessing/ | |
* | |
* | |
* Data is passed back to the application via the following callback methods: | |
* | |
* | |
* public void attentionEvent(int attentionLevel) | |
* Returns the current attention level [0, 100]. | |
* Values in [1, 20] are considered strongly lowered. | |
* Values in [20, 40] are considered reduced levels. | |
* Values in [40, 60] are considered neutral. | |
* Values in [60, 80] are considered slightly elevated. | |
* Values in [80, 100] are considered elevated. | |
* | |
* public void meditationEvent(int meditationLevel) | |
* Returns the current meditation level [0, 100]. | |
* The interpretation of the values is the same as for the attentionLevel. | |
* | |
* | |
* public void poorSignalEvent(int signalLevel) | |
* Returns the signal level [0, 200]. The greater the value, the more noise is detected in the signal. | |
* 200 is a special value that means that the ThinkGear contacts are not touching the skin. | |
* | |
* | |
* public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) </code><br> | |
* Returns the EEG data. The values have no units. | |
* | |
* | |
* | |
* public void rawEvent(int []) | |
* Returns the the current 512 raw signal samples [-32768, 32767]. | |
* | |
* | |
*/ | |
package neurosky; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Method; | |
import java.net.ConnectException; | |
import java.net.Socket; | |
import java.net.SocketException; | |
import java.net.UnknownHostException; | |
import java.util.Iterator; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import processing.core.PApplet; | |
public class ThinkGearSocket implements Runnable{ | |
public PApplet parent; | |
public Socket neuroSocket; | |
public OutputStream outStream; | |
public InputStream inStream; | |
public BufferedReader stdIn; | |
private Method attentionEventMethod = null; | |
private Method meditationEventMethod = null; | |
private Method poorSignalEventMethod = null; | |
private Method blinkEventMethod = null; | |
private Method eegEventMethod = null; | |
private Method rawEventMethod = null; | |
public String appName=""; | |
public String appKey=""; | |
private Thread t; | |
private int raw[] = new int[512]; | |
private int index = 0; | |
public final static String VERSION = "1.0"; | |
private boolean running = true; | |
public ThinkGearSocket(PApplet _parent, String _appName,String _appKey){ | |
this(_parent); | |
appName = _appName;//these were mentioned in the documentation as required, but test prove they are not. | |
appKey = _appKey; | |
} | |
public ThinkGearSocket(PApplet _parent){ | |
parent = _parent; | |
try { | |
attentionEventMethod = | |
parent.getClass().getMethod("attentionEvent", new Class[] { | |
int.class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("attentionEvent() method not defined. "); | |
} | |
try { | |
meditationEventMethod = | |
parent.getClass().getMethod("meditationEvent", new Class[] { | |
int.class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("meditationEvent() method not defined. "); | |
} | |
try { | |
poorSignalEventMethod = | |
parent.getClass().getMethod("poorSignalEvent", new Class[] { | |
int.class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("poorSignalEvent() method not defined. "); | |
} | |
try { | |
blinkEventMethod = | |
parent.getClass().getMethod("blinkEvent", new Class[] { | |
int.class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("blinkEvent() method not defined. "); | |
} | |
try { | |
eegEventMethod = | |
parent.getClass().getMethod("eegEvent", new Class[] { | |
int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("eegEvent() method not defined. "); | |
} | |
try { | |
rawEventMethod = | |
parent.getClass().getMethod("rawEvent", new Class[] { | |
int[].class | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("rawEvent() method not defined. "); | |
} | |
} | |
public boolean isRunning(){ | |
return running; | |
} | |
/** | |
* return the version of the library. | |
* | |
* @return String | |
*/ | |
public static String version() { | |
return VERSION; | |
} | |
public void start() throws ConnectException{ | |
try { | |
neuroSocket = new Socket("127.0.0.1",13854); | |
} catch (ConnectException e) { | |
//e.printStackTrace(); | |
System.out.println("Oi plonker! Is ThinkkGear running?"); | |
running = false; | |
throw e; | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try { | |
inStream = neuroSocket.getInputStream(); | |
outStream = neuroSocket.getOutputStream(); | |
stdIn = new BufferedReader(new InputStreamReader(neuroSocket.getInputStream())); | |
running = true; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if(appName !="" && appKey !=""){ | |
JSONObject appAuth = new JSONObject(); | |
try { | |
appAuth.put("appName", appName); | |
} catch (JSONException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
try { | |
appAuth.put("appKey", appKey); | |
} catch (JSONException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
//throws some error | |
sendMessage(appAuth.toString()); | |
System.out.println("appAuth"+appAuth); | |
} | |
JSONObject format = new JSONObject(); | |
try { | |
format.put("enableRawOutput", true); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
System.out.println("raw error"); | |
e.printStackTrace(); | |
} | |
try { | |
format.put("format", "Json"); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
System.out.println("Json error"); | |
e.printStackTrace(); | |
} | |
//System.out.println("format "+format); | |
sendMessage(format.toString()); | |
t = new Thread(this); | |
t.start(); | |
} | |
@SuppressWarnings("deprecation") | |
public void stop(){ | |
if(running){ | |
t.interrupt(); | |
try { | |
neuroSocket.close(); | |
inStream.close(); | |
outStream.close(); | |
stdIn.close(); | |
stdIn = null; | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
//System.out.println("Socket close issue"); | |
} | |
} | |
running = false; | |
} | |
public void sendMessage(String msg){ | |
PrintWriter out = new PrintWriter(outStream, true); | |
//System.out.println("sendmsg"); | |
out.println(msg); | |
} | |
@Override | |
public void run() { | |
if(running && neuroSocket.isConnected()){ | |
String userInput; | |
try { | |
while ((userInput = stdIn.readLine()) != null) { | |
String[] packets = userInput.split("/\r/"); | |
for(int s=0;s<packets.length;s++){ | |
if(((String) packets[s]).indexOf("{")>-1){ | |
JSONObject obj = new JSONObject((String) packets[s]); | |
parsePacket(obj); | |
} | |
//String name = obj.get("name").toString(); | |
} | |
} | |
} | |
catch(SocketException e){ | |
//System.out.println("For some reason stdIn throws error even if closed"); | |
//maybe it takes a cycle to close properly? | |
//e.printStackTrace(); | |
} | |
catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
//e.printStackTrace(); | |
} | |
parent.delay(50); | |
}else{ | |
running = false; | |
} | |
} | |
private void triggerAttentionEvent(int attentionLevel) { | |
if (attentionEventMethod != null) { | |
try { | |
attentionEventMethod.invoke(parent, new Object[] { | |
attentionLevel | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling attentionEvent() because of an error."); | |
e.printStackTrace(); | |
attentionEventMethod = null; | |
} | |
} | |
} | |
private void triggerMeditationEvent(int meditationLevel) { | |
if (meditationEventMethod != null) { | |
try { | |
meditationEventMethod.invoke(parent, new Object[] { | |
meditationLevel | |
} | |
); | |
//println("Attention: " + attention); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling meditationEvent() because of an error."); | |
e.printStackTrace(); | |
meditationEventMethod = null; | |
} | |
} | |
} | |
private void triggerPoorSignalEvent(int poorSignalLevel) { | |
if (poorSignalEventMethod != null) { | |
try { | |
poorSignalEventMethod.invoke(parent, new Object[] { | |
poorSignalLevel | |
} | |
); | |
//println("Attention: " + attention); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling meditationEvent() because of an error."); | |
e.printStackTrace(); | |
poorSignalEventMethod = null; | |
} | |
} | |
} | |
private void triggerBlinkEvent(int blinkStrength) { | |
if (blinkEventMethod != null) { | |
try { | |
blinkEventMethod.invoke(parent, new Object[] { | |
blinkStrength | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling blinkEvent() because of an error."); | |
e.printStackTrace(); | |
blinkEventMethod = null; | |
} | |
} | |
} | |
private void triggerEEGEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) { | |
if (eegEventMethod != null) { | |
try { | |
eegEventMethod.invoke(parent, new Object[] { | |
delta, theta, low_alpha, high_alpha, low_beta, high_beta, low_gamma, mid_gamma | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling eegEvent() because of an error."); | |
e.printStackTrace(); | |
eegEventMethod = null; | |
} | |
} | |
} | |
private void triggerRawEvent(int []values) { | |
if (rawEventMethod != null) { | |
try { | |
rawEventMethod.invoke(parent, new Object[] { | |
values | |
} | |
); | |
} | |
catch (Exception e) { | |
System.err.println("Disabling rawEvent() because of an error."); | |
e.printStackTrace(); | |
rawEventMethod = null; | |
} | |
} | |
} | |
private void parsePacket(JSONObject data){ | |
Iterator itr = data.keys(); | |
while(itr.hasNext()) { | |
Object e = itr.next(); | |
String key = e.toString(); | |
try{ | |
if(key.matches("poorSignalLevel")){ | |
triggerPoorSignalEvent(data.getInt(e.toString())); | |
} | |
if(key.matches("rawEeg")){ | |
int rawValue = (Integer) data.get("rawEeg"); | |
raw[index] = rawValue; | |
index++; | |
if (index == 512) { | |
index = 0; | |
int rawCopy[] = new int[512]; | |
parent.arrayCopy(raw, rawCopy); | |
triggerRawEvent(rawCopy); | |
} | |
} | |
if(key.matches("blinkStrength")){ | |
triggerBlinkEvent(data.getInt(e.toString())); | |
} | |
if(key.matches("eSense")){ | |
JSONObject esense = data.getJSONObject("eSense"); | |
triggerAttentionEvent(esense.getInt("attention")); | |
triggerMeditationEvent(esense.getInt("meditation")); | |
} | |
if(key.matches("eegPower")){ | |
JSONObject eegPower = data.getJSONObject("eegPower"); | |
triggerEEGEvent(eegPower.getInt("delta"), eegPower.getInt("theta"), eegPower.getInt("lowAlpha"), eegPower.getInt("highAlpha"),eegPower.getInt("lowBeta"), eegPower.getInt("highBeta"),eegPower.getInt("lowGamma"), eegPower.getInt("highGamma")); | |
//System.out.println(key); | |
} | |
} | |
catch(Exception ex){ | |
ex.printStackTrace(); | |
} | |
} | |
// | |
} | |
} |