clientMap=new ConcurrentHashMap<>();
27 |
28 | /**
29 | * 实例化
30 | *
31 | * @param port
32 | * 监听的端口
33 | */
34 | public TcpServer(int port) {
35 | this.port = port;
36 | }
37 |
38 | /**
39 | * 启动服务器
40 | *
41 | * 如果启动失败,会回调{@code onServerStop()}
42 | */
43 | public void start() {
44 | runFlag = true;
45 | new Thread(this).start();
46 | }
47 |
48 | /**
49 | * 停止服务器
50 | *
51 | * 服务器停止后,会回调{@code onServerStop()}
52 | */
53 | public void stop() {
54 | runFlag = false;
55 | }
56 |
57 | /**
58 | * 监听端口,接受客户端连接(新线程中运行)
59 | */
60 | @Override
61 | public void run() {
62 | try {
63 | final ServerSocket server = new ServerSocket(port);
64 | while (runFlag) {
65 | try {
66 | final Socket socket = server.accept();
67 | startClient(socket);
68 | } catch (IOException e) {
69 | // 接受客户端连接出错
70 | e.printStackTrace();
71 | this.onConnectFailed();
72 | }
73 | }
74 | // 停止服务器,断开与每个客户端的连接
75 | try {
76 | for (SocketTransceiver client : clients) {
77 | client.stop();
78 | }
79 | clients.clear();
80 | server.close();
81 | } catch (Exception e) {
82 | e.printStackTrace();
83 | }
84 | } catch (IOException e) {
85 | // ServerSocket对象创建出错,服务器启动失败
86 | e.printStackTrace();
87 | }
88 | this.onServerStop();
89 | }
90 |
91 | /**
92 | * 启动客户端收发
93 | *
94 | * @param socket
95 | */
96 | private void startClient(final Socket socket) {
97 | SocketTransceiver client = new SocketTransceiver(socket) {
98 |
99 | @Override
100 | public void onReceive(InetAddress addr, byte s[],int len) {
101 | // if(!TcpServer.this.runFlag){
102 | // return;
103 | // }
104 | TcpServer.this.onReceive(this, s,len);
105 |
106 | }
107 |
108 | @Override
109 | public void onDisconnect(InetAddress addr) {
110 | clients.remove(this);
111 | TcpServer.this.onDisconnect(this);
112 | }
113 | };
114 | if(clientMap.containsKey(client.addr.getHostAddress())){
115 | clientMap.get(client.addr.getHostAddress()).stop();
116 |
117 | }else{
118 | clientMap.put(client.addr.getHostAddress(),client);
119 | }
120 | client.start();
121 | Log.e("ip123",client.addr.getHostAddress());
122 |
123 | // clientMap.put(client.addr.getHostAddress())
124 | clients.add(client);
125 | this.onConnect(client);
126 | }
127 |
128 | /**
129 | * 客户端:连接建立
130 | *
131 | * 注意:此回调是在新线程中执行的
132 | *
133 | * @param client
134 | * SocketTransceiver对象
135 | */
136 | public abstract void onConnect(SocketTransceiver client);
137 |
138 | /**
139 | * 客户端:连接建立失败
140 | *
141 | * 注意:此回调是在新线程中执行的
142 | */
143 | public abstract void onConnectFailed();
144 |
145 | /**
146 | * 客户端:收到字符串
147 | *
148 | * 注意:此回调是在新线程中执行的
149 | *
150 | * @param client
151 | * SocketTransceiver对象
152 | * @param s
153 | * 字符串
154 | */
155 | public abstract void onReceive(SocketTransceiver client, byte s[],int len);
156 |
157 | /**
158 | * 客户端:连接断开
159 | *
160 | * 注意:此回调是在新线程中执行的
161 | *
162 | * @param client
163 | * SocketTransceiver对象
164 | */
165 | public abstract void onDisconnect(SocketTransceiver client);
166 |
167 | /**
168 | * 服务器停止
169 | *
170 | * 注意:此回调是在新线程中执行的
171 | */
172 | public abstract void onServerStop();
173 | }
174 |
--------------------------------------------------------------------------------
/主显示端/src/bno055/bno055.cpp:
--------------------------------------------------------------------------------
1 | extern "C"
2 | {
3 | #include "bno055.h"
4 | #include "pa_HardwareIIC/pa_HardwareIIC.h"
5 | }
6 | #include "Arduino.h"
7 | #define pa_BNO055_chosenAddress (0x29)
8 |
9 | unsigned long timeStart = 0;
10 | unsigned long timeStop = 0;
11 | unsigned long timeinterval = 0;
12 | unsigned char startflag = 0;
13 | extern float bno055_InitZ;
14 |
15 | void pa_BNO055_writeIIC(uint8_t add, uint8_t reg, uint8_t data)
16 | {
17 | pa_IICSettingStruct a;
18 | pa_IIC_writeLen(add, reg, 1, &data, a);
19 | }
20 | uint8_t pa_BNO055_readIIC(uint8_t add, uint8_t reg)
21 | {
22 | pa_IICSettingStruct a;
23 | uint8_t data[1];
24 | pa_IIC_readLen(add, reg, 1, data, a);
25 | return data[0];
26 | }
27 | void pa_BNO055_init()
28 | {
29 |
30 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_SYS_TRIGGER_ADDR, 0x20);
31 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_SYS_TRIGGER_ADDR, 0x20);
32 | delay(700);
33 | // pa_IIC_init();
34 | unsigned char id = pa_BNO055_readIIC(pa_BNO055_chosenAddress, BNO055_CHIP_ID_ADDR); //bno055_readData(BNO055_CHIP_ID, &id, 1);//ok
35 | // if (id != BNO055_ID) {
36 | // delay(1000); // hold on for boot
37 | // id = pa_IIC_read8(pa_BNO055_chosenAddress,BNO055_CHIP_ID_ADDR);}
38 | if (id != BNO055_ID)
39 | {
40 | while (1)
41 | {
42 | }
43 | // pa_gotoErrorBlink();
44 | return; // still not? ok bail
45 | }
46 |
47 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_PAGE_ID_ADDR, 0); //bno055_setPage
48 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_SYS_TRIGGER_ADDR, 0x00); //bno055_writeData(BNO055_SYS_TRIGGER, 0x0);//ok
49 |
50 | // Select BNO055 config mode
51 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_OPR_MODE_ADDR, OPERATION_MODE_CONFIG); //bno055_setOperationModeConfig();//ok
52 | delay(19);
53 | delay(10);
54 |
55 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_OPR_MODE_ADDR, OPERATION_MODE_NDOF); //bno055_setOperationModeNDOF();
56 | delay(7);
57 | }
58 |
59 | bno055_vector_t pa_BNO055_getVector()
60 | {
61 | pa_BNO055_writeIIC(pa_BNO055_chosenAddress, BNO055_PAGE_ID_ADDR, 0); //bno055_setPage
62 | unsigned char buffer[8]; // Quaternion need 8 bytes
63 |
64 | // if (BNO055_EULER_H_LSB_ADDR == BNO055_QUATERNION_DATA_W_LSB_ADDR)
65 | // pa_IIC_read8(pa_BNO055_chosenAddress, BNO055_EULER_H_LSB_ADDR);
66 | // else
67 | {
68 | pa_IICSettingStruct a;
69 | pa_IIC_readLen(pa_BNO055_chosenAddress, BNO055_EULER_H_LSB_ADDR, sizeof(buffer), buffer, a);
70 | }
71 |
72 | double scale = 1;
73 |
74 | if (BNO055_EULER_H_LSB_ADDR == BNO055_MAG_DATA_X_LSB_ADDR)
75 | {
76 | scale = 16;
77 | }
78 | else if (BNO055_EULER_H_LSB_ADDR == BNO055_ACCEL_DATA_X_LSB_ADDR || BNO055_EULER_H_LSB_ADDR == BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR || BNO055_EULER_H_LSB_ADDR == BNO055_GRAVITY_DATA_X_LSB_ADDR)
79 | {
80 | scale = 100;
81 | }
82 | else if (BNO055_EULER_H_LSB_ADDR == BNO055_GYRO_DATA_X_LSB_ADDR)
83 | {
84 | scale = 16;
85 | }
86 | else if (BNO055_EULER_H_LSB_ADDR == BNO055_EULER_H_LSB_ADDR)
87 | {
88 | scale = 16;
89 | }
90 | else if (BNO055_EULER_H_LSB_ADDR == BNO055_QUATERNION_DATA_W_LSB_ADDR)
91 | {
92 | scale = (14);
93 | }
94 |
95 | bno055_vector_t xyz = {.w = 0, .x = 0, .y = 0, .z = 0};
96 | if (BNO055_EULER_H_LSB_ADDR == BNO055_QUATERNION_DATA_W_LSB_ADDR)
97 | {
98 | xyz.w = (signed short int)((buffer[1] << 8) | buffer[0]) / scale;
99 | xyz.x = (signed short int)((buffer[3] << 8) | buffer[2]) / scale;
100 | xyz.y = (signed short int)((buffer[5] << 8) | buffer[4]) / scale;
101 | xyz.z = (signed short int)((buffer[7] << 8) | buffer[6]) / scale;
102 | }
103 | else
104 | {
105 | xyz.x = (signed short int)((buffer[1] << 8) | buffer[0]) / scale;
106 | xyz.y = (signed short int)((buffer[3] << 8) | buffer[2]) / scale;
107 | xyz.z = (signed short int)((buffer[5] << 8) | buffer[4]) / scale;
108 | }
109 |
110 | return xyz;
111 | }
112 |
113 |
114 | int bno055_StepCount(bno055_vector_t myBNO055)
115 | {
116 | static int stepCount;
117 |
118 | if(abs(myBNO055.z-bno055_InitZ)>=20&&startflag==0)
119 | {
120 | timeStart = millis();
121 | startflag = 1;
122 | }
123 | if(abs(myBNO055.z - bno055_InitZ)<=5&&startflag==1)
124 | {
125 | timeStop = millis();
126 | startflag = 0;
127 | timeinterval = timeStop - timeStart;
128 | }
129 |
130 | if(timeinterval>=300)
131 | {
132 | timeinterval = 0;
133 | timeStop = 0;
134 | timeStart = 0;
135 | ++stepCount;
136 | }
137 |
138 | return stepCount*2;
139 | }
--------------------------------------------------------------------------------
/android/app/src/main/java/tv/higlobal/tcpdemo/SocketTransceiver.java:
--------------------------------------------------------------------------------
1 | package tv.higlobal.tcpdemo;
2 |
3 | import android.util.Log;
4 |
5 | import java.io.DataInputStream;
6 | import java.io.DataOutputStream;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.OutputStream;
10 | import java.net.InetAddress;
11 | import java.net.Socket;
12 |
13 | /**
14 | * Socket收发器 通过Socket发送数据,并使用新线程监听Socket接收到的数据
15 | *
16 | * @author jzj1993
17 | * @since 2015-2-22
18 | */
19 | public abstract class SocketTransceiver implements Runnable {
20 |
21 | protected Socket socket;
22 | protected InetAddress addr;
23 | protected DataInputStream in;
24 | protected DataOutputStream out;
25 | public byte UnhandledData[];
26 | private boolean runFlag;
27 |
28 | /**
29 | * 实例化
30 | *
31 | * @param socket
32 | * 已经建立连接的socket
33 | */
34 | public SocketTransceiver(Socket socket) {
35 | this.socket = socket;
36 | this.addr = socket.getInetAddress();
37 | }
38 |
39 | /**
40 | * 获取连接到的Socket地址
41 | *
42 | * @return InetAddress对象
43 | */
44 | public InetAddress getInetAddress() {
45 | return addr;
46 | }
47 |
48 | /**
49 | * 开启Socket收发
50 | *
51 | * 如果开启失败,会断开连接并回调{@code onDisconnect()}
52 | */
53 | public void start() {
54 | runFlag = true;
55 | new Thread(this).start();
56 | }
57 |
58 | /**
59 | * 断开连接(主动)
60 | *
61 | * 连接断开后,会回调{@code onDisconnect()}
62 | */
63 | public void stop() {
64 | // Log.e("ip123","stop");
65 |
66 | // runFlag = false;
67 | try {
68 | socket.shutdownInput();
69 | socket=null;
70 | if(in!=null){
71 | in.close();
72 | }
73 | if(out!=null){
74 | out.close();
75 | out=null;
76 | }
77 |
78 | } catch (Exception e) {
79 | e.printStackTrace();
80 | }
81 | }
82 |
83 | /**
84 | * 发送字符串
85 | *
86 | * @param s
87 | * 字符串
88 | * @return 发送成功返回true
89 | */
90 | OutputStream out1;
91 | public boolean send(byte a[]) {
92 | Log.e("hhhhh123","abcef");
93 | if(socket.isClosed()||!runFlag){
94 | return false;
95 | }
96 | if(out==null){
97 | try {
98 | out = new DataOutputStream(socket.getOutputStream());
99 | } catch (IOException e) {
100 | e.printStackTrace();
101 | }
102 | }
103 | if (out != null) {
104 | Log.e("hhhhh123","abc");
105 | try {
106 | out.write(a);
107 | out.flush();
108 | return true;
109 | } catch (Exception e) {
110 | e.printStackTrace();
111 | }
112 | }
113 | return false;
114 | }
115 |
116 | /**
117 | * 监听Socket接收的数据(新线程中运行)
118 | */
119 | @Override
120 | public void run() {
121 | // try {
122 | // in = new DataInputStream(this.socket.getInputStream());
123 | // out = new DataOutputStream(this.socket.getOutputStream());
124 | // } catch (IOException e) {
125 | // e.printStackTrace();
126 | // runFlag = false;
127 | // }
128 | InputStream inputStream;
129 | byte[] buf = new byte[1024];
130 | try {
131 | inputStream=this.socket.getInputStream();
132 | int len;
133 | while (runFlag) {
134 | if(inputStream==null){
135 | break;
136 | }
137 | if(inputStream.available()>0){
138 | if((len=inputStream.read(buf))==-1){
139 | break;
140 | }
141 | onReceive(addr,buf,len);
142 | }
143 | }
144 | } catch (IOException e) {
145 | e.printStackTrace();
146 | runFlag = false;
147 | }
148 |
149 | // 断开连接
150 | try {
151 | in.close();
152 | out.close();
153 | socket.close();
154 | in = null;
155 | out = null;
156 | socket = null;
157 | } catch (IOException e) {
158 | e.printStackTrace();
159 | }
160 | this.onDisconnect(addr);
161 | }
162 |
163 | /**
164 | * 接收到数据
165 | *
166 | * 注意:此回调是在新线程中执行的
167 | *
168 | * @param addr
169 | * 连接到的Socket地址
170 | * @param s
171 | * 收到的字符串
172 | */
173 | public abstract void onReceive(InetAddress addr, byte s[],int len);
174 |
175 | /**
176 | * 连接断开
177 | *
178 | * 注意:此回调是在新线程中执行的
179 | *
180 | * @param addr
181 | * 连接到的Socket地址
182 | */
183 | public abstract void onDisconnect(InetAddress addr);
184 | }
--------------------------------------------------------------------------------
/android/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/姿态手环/src/button2/Button2.cpp:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////
2 | /*
3 | Button2.cpp - Arduino Library to simplify working with buttons.
4 | Created by Lennart Hennigs.
5 | */
6 | /////////////////////////////////////////////////////////////////
7 |
8 | #include "Arduino.h"
9 | #include "Button2.h"
10 |
11 | /////////////////////////////////////////////////////////////////
12 |
13 | Button2::Button2(byte attachTo, byte buttonMode /* = INPUT_PULLUP */, boolean activeLow /* = true */, unsigned int debounceTimeout /* = DEBOUNCE_MS */) {
14 | pin = attachTo;
15 | setDebounceTime(debounceTimeout);
16 | pinMode(attachTo, buttonMode);
17 | pressed = activeLow ? HIGH : LOW;
18 | released = activeLow ? LOW : HIGH;
19 | state = pressed;
20 | }
21 |
22 | /////////////////////////////////////////////////////////////////
23 |
24 | bool Button2::operator==(Button2 &rhs) {
25 | return (this==&rhs);
26 | }
27 |
28 | /////////////////////////////////////////////////////////////////
29 |
30 | void Button2::setDebounceTime(unsigned int ms) {
31 | debounce_time_ms = ms;
32 | }
33 |
34 | /////////////////////////////////////////////////////////////////
35 |
36 | void Button2::setChangedHandler(CallbackFunction f) {
37 | change_cb = f;
38 | }
39 |
40 | /////////////////////////////////////////////////////////////////
41 |
42 | void Button2::setPressedHandler(CallbackFunction f) {
43 | pressed_cb = f;
44 | }
45 |
46 | /////////////////////////////////////////////////////////////////
47 |
48 | void Button2::setReleasedHandler(CallbackFunction f) {
49 | released_cb = f;
50 | }
51 |
52 | /////////////////////////////////////////////////////////////////
53 |
54 | void Button2::setClickHandler(CallbackFunction f) {
55 | click_cb = f;
56 | }
57 |
58 | /////////////////////////////////////////////////////////////////
59 |
60 | void Button2::setTapHandler(CallbackFunction f) {
61 | tap_cb = f;
62 | }
63 |
64 | /////////////////////////////////////////////////////////////////
65 |
66 | void Button2::setLongClickHandler(CallbackFunction f) {
67 | long_cb = f;
68 | }
69 |
70 | /////////////////////////////////////////////////////////////////
71 |
72 | void Button2::setDoubleClickHandler(CallbackFunction f) {
73 | double_cb = f;
74 | }
75 |
76 | /////////////////////////////////////////////////////////////////
77 |
78 | void Button2::setTripleClickHandler(CallbackFunction f) {
79 | triple_cb = f;
80 | }
81 |
82 | /////////////////////////////////////////////////////////////////
83 |
84 | unsigned int Button2::wasPressedFor() {
85 | return down_time_ms;
86 | }
87 |
88 | /////////////////////////////////////////////////////////////////
89 |
90 | boolean Button2::isPressed() {
91 | return (state == released);
92 | }
93 |
94 | /////////////////////////////////////////////////////////////////
95 |
96 | unsigned int Button2::getNumberOfClicks() {
97 | return click_count;
98 | }
99 |
100 | /////////////////////////////////////////////////////////////////
101 |
102 | unsigned int Button2::getClickType() {
103 | return last_click_type;
104 | }
105 |
106 | /////////////////////////////////////////////////////////////////
107 |
108 | void Button2::loop() {
109 | prev_state = state;
110 | state = digitalRead(pin);
111 |
112 | // is button pressed?
113 | if (prev_state == pressed && state == released) {
114 | down_ms = millis();
115 | pressed_triggered = false;
116 | click_count++;
117 | click_ms = down_ms;
118 |
119 | // is the button released?
120 | } else if (prev_state == released && state == pressed) {
121 | down_time_ms = millis() - down_ms;
122 | // is it beyond debounce time?
123 | if (down_time_ms >= debounce_time_ms) {
124 | // trigger release
125 | if (change_cb != NULL) change_cb (*this);
126 | if (released_cb != NULL) released_cb (*this);
127 | // trigger tap
128 | if (tap_cb != NULL) tap_cb (*this);
129 | // was it a longclick? (preceeds single / double / triple clicks)
130 | if (down_time_ms >= LONGCLICK_MS) {
131 | longclick_detected = true;
132 | }
133 | }
134 |
135 | // trigger pressed event (after debounce has passed)
136 | } else if (state == released && !pressed_triggered && (millis() - down_ms >= debounce_time_ms)) {
137 | if (change_cb != NULL) change_cb (*this);
138 | if (pressed_cb != NULL) pressed_cb (*this);
139 | pressed_triggered = true;
140 |
141 | // is the button pressed and the time has passed for multiple clicks?
142 | } else if (state == pressed && millis() - click_ms > DOUBLECLICK_MS) {
143 | // was there a longclick?
144 | if (longclick_detected) {
145 | // was it part of a combination?
146 | if (click_count == 1) {
147 | last_click_type = LONG_CLICK;
148 | if (long_cb != NULL) long_cb (*this);
149 | }
150 | longclick_detected = false;
151 | // determine the number of single clicks
152 | } else if (click_count > 0) {
153 | switch (click_count) {
154 | case 1:
155 | last_click_type = SINGLE_CLICK;
156 | if (click_cb != NULL) click_cb (*this);
157 | break;
158 | case 2:
159 | last_click_type = DOUBLE_CLICK;
160 | if (double_cb != NULL) double_cb (*this);
161 | break;
162 | case 3:
163 | last_click_type = TRIPLE_CLICK;
164 | if (triple_cb != NULL) triple_cb (*this);
165 | break;
166 | }
167 | }
168 | click_count = 0;
169 | click_ms = 0;
170 | }
171 | yield();
172 | }
173 |
174 | /////////////////////////////////////////////////////////////////
175 |
176 | void Button2::reset() {
177 | click_count = 0;
178 | last_click_type = 0;
179 | down_time_ms = 0;
180 | pressed_triggered = false;
181 | longclick_detected = false;
182 |
183 | pressed_cb = NULL;
184 | released_cb = NULL;
185 | change_cb = NULL;
186 | tap_cb = NULL;
187 | click_cb = NULL;
188 | long_cb = NULL;
189 | double_cb = NULL;
190 | triple_cb = NULL;
191 | }
192 |
193 | /////////////////////////////////////////////////////////////////
194 |
--------------------------------------------------------------------------------
/android/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/主显示端/src/ads_1292r/ads1292r.cpp:
--------------------------------------------------------------------------------
1 | //////////////////////////////////////////////////////////////////////////////////////////
2 | //
3 | // Arduino Library for ADS1292R Shield/Breakout
4 | //
5 | // Copyright (c) 2017 ProtoCentral
6 | //
7 | // This software is licensed under the MIT License(http://opensource.org/licenses/MIT).
8 | //
9 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
10 | // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
11 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
12 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
13 | // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 | //
15 | // Requires g4p_control graphing library for processing. Built on V4.1
16 | // Downloaded from Processing IDE Sketch->Import Library->Add Library->G4P Install
17 | //
18 | /////////////////////////////////////////////////////////////////////////////////////////
19 |
20 | #include
21 | #include
22 | #include
23 |
24 | char* ads1292r::ads1292_Read_Data()
25 | {
26 |
27 | static char SPI_Dummy_Buff[10];
28 |
29 | digitalWrite(ADS1292_CS_PIN, LOW);
30 |
31 | for (int i = 0; i < 9; ++i)
32 | {
33 | SPI_Dummy_Buff[i] = SPI.transfer(CONFIG_SPI_MASTER_DUMMY);
34 |
35 | }
36 |
37 | digitalWrite(ADS1292_CS_PIN, HIGH);
38 |
39 | return SPI_Dummy_Buff;
40 | }
41 |
42 | void ads1292r::ads1292_Init()
43 | {
44 | // start the SPI library:
45 | SPI.begin();
46 | SPI.setBitOrder(MSBFIRST);
47 | //CPOL = 0, CPHA = 1
48 | SPI.setDataMode(SPI_MODE1);
49 | // Selecting 1Mhz clock for SPI
50 | SPI.setClockDivider(SPI_CLOCK_DIV16);
51 |
52 | ads1292_Reset();
53 | delay(100);
54 | ads1292_Disable_Start();
55 | ads1292_Enable_Start();
56 |
57 | ads1292_Hard_Stop();
58 | ads1292_Start_Data_Conv_Command();
59 | ads1292_Soft_Stop();
60 | delay(50);
61 | ads1292_Stop_Read_Data_Continuous(); // SDATAC command
62 | delay(300);
63 |
64 | ads1292_Reg_Write(ADS1292_REG_CONFIG1, 0x00); //Set sampling rate to 125 SPS
65 | delay(10);
66 | ads1292_Reg_Write(ADS1292_REG_CONFIG2, 0b10100000); //Lead-off comp off, test signal disabled
67 | delay(10);
68 | ads1292_Reg_Write(ADS1292_REG_LOFF, 0b00010000); //Lead-off defaults
69 | delay(10);
70 | ads1292_Reg_Write(ADS1292_REG_CH1SET, 0b01000000); //Ch 1 enabled, gain 6, connected to electrode in
71 | delay(10);
72 | ads1292_Reg_Write(ADS1292_REG_CH2SET, 0b01100000); //Ch 2 enabled, gain 6, connected to electrode in
73 | delay(10);
74 | ads1292_Reg_Write(ADS1292_REG_RLDSENS, 0b00101100); //RLD settings: fmod/16, RLD enabled, RLD inputs from Ch2 only
75 | delay(10);
76 | ads1292_Reg_Write(ADS1292_REG_LOFFSENS, 0x00); //LOFF settings: all disabled
77 | delay(10);
78 | //Skip register 8, LOFF Settings default
79 | ads1292_Reg_Write(ADS1292_REG_RESP1, 0b11110010); //Respiration: MOD/DEMOD turned only, phase 0
80 | delay(10);
81 | ads1292_Reg_Write(ADS1292_REG_RESP2, 0b00000011); //Respiration: Calib OFF, respiration freq defaults
82 | delay(10);
83 | ads1292_Start_Read_Data_Continuous();
84 | delay(10);
85 | ads1292_Enable_Start();
86 | }
87 |
88 | void ads1292r::ads1292_Reset()
89 | {
90 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
91 | delay(100); // Wait 100 mSec
92 | digitalWrite(ADS1292_PWDN_PIN, LOW);
93 | delay(100);
94 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
95 | delay(100);
96 | }
97 |
98 | void ads1292r::ads1292_Disable_Start()
99 | {
100 | digitalWrite(ADS1292_START_PIN, LOW);
101 | delay(20);
102 | }
103 |
104 | void ads1292r::ads1292_Enable_Start()
105 | {
106 | digitalWrite(ADS1292_START_PIN, HIGH);
107 | delay(20);
108 | }
109 |
110 | void ads1292r::ads1292_Hard_Stop (void)
111 | {
112 | digitalWrite(ADS1292_START_PIN, LOW);
113 | delay(100);
114 | }
115 |
116 |
117 | void ads1292r::ads1292_Start_Data_Conv_Command (void)
118 | {
119 | ads1292_SPI_Command_Data(START); // Send 0x08 to the ADS1x9x
120 | }
121 |
122 | void ads1292r::ads1292_Soft_Stop (void)
123 | {
124 | ads1292_SPI_Command_Data(STOP); // Send 0x0A to the ADS1x9x
125 | }
126 |
127 | void ads1292r::ads1292_Start_Read_Data_Continuous (void)
128 | {
129 | ads1292_SPI_Command_Data(RDATAC); // Send 0x10 to the ADS1x9x
130 | }
131 |
132 | void ads1292r::ads1292_Stop_Read_Data_Continuous (void)
133 | {
134 | ads1292_SPI_Command_Data(SDATAC); // Send 0x11 to the ADS1x9x
135 | }
136 |
137 | void ads1292r::ads1292_SPI_Command_Data(unsigned char data_in)
138 | {
139 | byte data[1];
140 | //data[0] = data_in;
141 | digitalWrite(ADS1292_CS_PIN, LOW);
142 | delay(2);
143 | digitalWrite(ADS1292_CS_PIN, HIGH);
144 | delay(2);
145 | digitalWrite(ADS1292_CS_PIN, LOW);
146 | delay(2);
147 | SPI.transfer(data_in);
148 | delay(2);
149 | digitalWrite(ADS1292_CS_PIN, HIGH);
150 | }
151 |
152 | //Sends a write command to SCP1000
153 | void ads1292r::ads1292_Reg_Write (unsigned char READ_WRITE_ADDRESS, unsigned char DATA)
154 | {
155 | switch (READ_WRITE_ADDRESS)
156 | {
157 | case 1:
158 | DATA = DATA & 0x87;
159 | break;
160 | case 2:
161 | DATA = DATA & 0xFB;
162 | DATA |= 0x80;
163 | break;
164 | case 3:
165 | DATA = DATA & 0xFD;
166 | DATA |= 0x10;
167 | break;
168 | case 7:
169 | DATA = DATA & 0x3F;
170 | break;
171 | case 8:
172 | DATA = DATA & 0x5F;
173 | break;
174 | case 9:
175 | DATA |= 0x02;
176 | break;
177 | case 10:
178 | DATA = DATA & 0x87;
179 | DATA |= 0x01;
180 | break;
181 | case 11:
182 | DATA = DATA & 0x0F;
183 | break;
184 | default:
185 | break;
186 | }
187 |
188 | // now combine the register address and the command into one byte:
189 | byte dataToSend = READ_WRITE_ADDRESS | WREG;
190 |
191 | digitalWrite(ADS1292_CS_PIN, LOW);
192 | delay(2);
193 | digitalWrite(ADS1292_CS_PIN, HIGH);
194 | delay(2);
195 | // take the chip select low to select the device:
196 | digitalWrite(ADS1292_CS_PIN, LOW);
197 | delay(2);
198 | SPI.transfer(dataToSend); //Send register location
199 | SPI.transfer(0x00); //number of register to wr
200 | SPI.transfer(DATA); //Send value to record into register
201 |
202 | delay(2);
203 | // take the chip select high to de-select:
204 | digitalWrite(ADS1292_CS_PIN, HIGH);
205 | }
--------------------------------------------------------------------------------
/心率模块读取端/src/ads1292r/ads1292r.cpp:
--------------------------------------------------------------------------------
1 | //////////////////////////////////////////////////////////////////////////////////////////
2 | //
3 | // Arduino Library for ADS1292R Shield/Breakout
4 | //
5 | // Copyright (c) 2017 ProtoCentral
6 | //
7 | // This software is licensed under the MIT License(http://opensource.org/licenses/MIT).
8 | //
9 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
10 | // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
11 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
12 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
13 | // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 | //
15 | // Requires g4p_control graphing library for processing. Built on V4.1
16 | // Downloaded from Processing IDE Sketch->Import Library->Add Library->G4P Install
17 | //
18 | /////////////////////////////////////////////////////////////////////////////////////////
19 |
20 | #include
21 | #include
22 | #include
23 |
24 | char* ads1292r::ads1292_Read_Data()
25 | {
26 |
27 | static char SPI_Dummy_Buff[10];
28 |
29 | digitalWrite(ADS1292_CS_PIN, LOW);
30 |
31 | for (int i = 0; i < 9; ++i)
32 | {
33 | SPI_Dummy_Buff[i] = SPI.transfer(CONFIG_SPI_MASTER_DUMMY);
34 |
35 | }
36 |
37 | digitalWrite(ADS1292_CS_PIN, HIGH);
38 |
39 | return SPI_Dummy_Buff;
40 | }
41 |
42 | void ads1292r::ads1292_Init()
43 | {
44 | // start the SPI library:
45 | SPI.begin();
46 | SPI.setBitOrder(MSBFIRST);
47 | //CPOL = 0, CPHA = 1
48 | SPI.setDataMode(SPI_MODE1);
49 | // Selecting 1Mhz clock for SPI
50 | SPI.setClockDivider(SPI_CLOCK_DIV16);
51 |
52 | ads1292_Reset();
53 | delay(100);
54 | ads1292_Disable_Start();
55 | ads1292_Enable_Start();
56 |
57 | ads1292_Hard_Stop();
58 | ads1292_Start_Data_Conv_Command();
59 | ads1292_Soft_Stop();
60 | delay(50);
61 | ads1292_Stop_Read_Data_Continuous(); // SDATAC command
62 | delay(300);
63 |
64 | ads1292_Reg_Write(ADS1292_REG_CONFIG1, 0x00); //Set sampling rate to 125 SPS
65 | delay(10);
66 | ads1292_Reg_Write(ADS1292_REG_CONFIG2, 0b10100000); //Lead-off comp off, test signal disabled
67 | delay(10);
68 | ads1292_Reg_Write(ADS1292_REG_LOFF, 0b00010000); //Lead-off defaults
69 | delay(10);
70 | ads1292_Reg_Write(ADS1292_REG_CH1SET, 0b01000000); //Ch 1 enabled, gain 6, connected to electrode in
71 | delay(10);
72 | ads1292_Reg_Write(ADS1292_REG_CH2SET, 0b01100000); //Ch 2 enabled, gain 6, connected to electrode in
73 | delay(10);
74 | ads1292_Reg_Write(ADS1292_REG_RLDSENS, 0b00101100); //RLD settings: fmod/16, RLD enabled, RLD inputs from Ch2 only
75 | delay(10);
76 | ads1292_Reg_Write(ADS1292_REG_LOFFSENS, 0x00); //LOFF settings: all disabled
77 | delay(10);
78 | //Skip register 8, LOFF Settings default
79 | ads1292_Reg_Write(ADS1292_REG_RESP1, 0b11110010); //Respiration: MOD/DEMOD turned only, phase 0
80 | delay(10);
81 | ads1292_Reg_Write(ADS1292_REG_RESP2, 0b00000011); //Respiration: Calib OFF, respiration freq defaults
82 | delay(10);
83 | ads1292_Start_Read_Data_Continuous();
84 | delay(10);
85 | ads1292_Enable_Start();
86 | }
87 |
88 | void ads1292r::ads1292_Reset()
89 | {
90 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
91 | delay(100); // Wait 100 mSec
92 | digitalWrite(ADS1292_PWDN_PIN, LOW);
93 | delay(100);
94 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
95 | delay(100);
96 | }
97 |
98 | void ads1292r::ads1292_Disable_Start()
99 | {
100 | digitalWrite(ADS1292_START_PIN, LOW);
101 | delay(20);
102 | }
103 |
104 | void ads1292r::ads1292_Enable_Start()
105 | {
106 | digitalWrite(ADS1292_START_PIN, HIGH);
107 | delay(20);
108 | }
109 |
110 | void ads1292r::ads1292_Hard_Stop (void)
111 | {
112 | digitalWrite(ADS1292_START_PIN, LOW);
113 | delay(100);
114 | }
115 |
116 |
117 | void ads1292r::ads1292_Start_Data_Conv_Command (void)
118 | {
119 | ads1292_SPI_Command_Data(START); // Send 0x08 to the ADS1x9x
120 | }
121 |
122 | void ads1292r::ads1292_Soft_Stop (void)
123 | {
124 | ads1292_SPI_Command_Data(STOP); // Send 0x0A to the ADS1x9x
125 | }
126 |
127 | void ads1292r::ads1292_Start_Read_Data_Continuous (void)
128 | {
129 | ads1292_SPI_Command_Data(RDATAC); // Send 0x10 to the ADS1x9x
130 | }
131 |
132 | void ads1292r::ads1292_Stop_Read_Data_Continuous (void)
133 | {
134 | ads1292_SPI_Command_Data(SDATAC); // Send 0x11 to the ADS1x9x
135 | }
136 |
137 | void ads1292r::ads1292_SPI_Command_Data(unsigned char data_in)
138 | {
139 | byte data[1];
140 | //data[0] = data_in;
141 | digitalWrite(ADS1292_CS_PIN, LOW);
142 | delay(2);
143 | digitalWrite(ADS1292_CS_PIN, HIGH);
144 | delay(2);
145 | digitalWrite(ADS1292_CS_PIN, LOW);
146 | delay(2);
147 | SPI.transfer(data_in);
148 | delay(2);
149 | digitalWrite(ADS1292_CS_PIN, HIGH);
150 | }
151 |
152 | //Sends a write command to SCP1000
153 | void ads1292r::ads1292_Reg_Write (unsigned char READ_WRITE_ADDRESS, unsigned char DATA)
154 | {
155 | switch (READ_WRITE_ADDRESS)
156 | {
157 | case 1:
158 | DATA = DATA & 0x87;
159 | break;
160 | case 2:
161 | DATA = DATA & 0xFB;
162 | DATA |= 0x80;
163 | break;
164 | case 3:
165 | DATA = DATA & 0xFD;
166 | DATA |= 0x10;
167 | break;
168 | case 7:
169 | DATA = DATA & 0x3F;
170 | break;
171 | case 8:
172 | DATA = DATA & 0x5F;
173 | break;
174 | case 9:
175 | DATA |= 0x02;
176 | break;
177 | case 10:
178 | DATA = DATA & 0x87;
179 | DATA |= 0x01;
180 | break;
181 | case 11:
182 | DATA = DATA & 0x0F;
183 | break;
184 | default:
185 | break;
186 | }
187 |
188 | // now combine the register address and the command into one byte:
189 | byte dataToSend = READ_WRITE_ADDRESS | WREG;
190 |
191 | digitalWrite(ADS1292_CS_PIN, LOW);
192 | delay(2);
193 | digitalWrite(ADS1292_CS_PIN, HIGH);
194 | delay(2);
195 | // take the chip select low to select the device:
196 | digitalWrite(ADS1292_CS_PIN, LOW);
197 | delay(2);
198 | SPI.transfer(dataToSend); //Send register location
199 | SPI.transfer(0x00); //number of register to wr
200 | SPI.transfer(DATA); //Send value to record into register
201 |
202 | delay(2);
203 | // take the chip select high to de-select:
204 | digitalWrite(ADS1292_CS_PIN, HIGH);
205 | }
--------------------------------------------------------------------------------
/姿态手环/src/Ads1292/ads1292r.cpp:
--------------------------------------------------------------------------------
1 | //////////////////////////////////////////////////////////////////////////////////////////
2 | //
3 | // Arduino Library for ADS1292R Shield/Breakout
4 | //
5 | // Copyright (c) 2017 ProtoCentral
6 | //
7 | // This software is licensed under the MIT License(http://opensource.org/licenses/MIT).
8 | //
9 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
10 | // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
11 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
12 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
13 | // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 | //
15 | // Requires g4p_control graphing library for processing. Built on V4.1
16 | // Downloaded from Processing IDE Sketch->Import Library->Add Library->G4P Install
17 | //
18 | /////////////////////////////////////////////////////////////////////////////////////////
19 |
20 | #include
21 | #include "ads1292r.h"
22 | #include
23 |
24 | char *ads1292r::ads1292_Read_Data()
25 | {
26 |
27 | static char SPI_Dummy_Buff[10];
28 |
29 | digitalWrite(ADS1292_CS_PIN, LOW);
30 |
31 | for (int i = 0; i < 9; ++i)
32 | {
33 | SPI_Dummy_Buff[i] = SPI.transfer(CONFIG_SPI_MASTER_DUMMY);
34 | }
35 |
36 | digitalWrite(ADS1292_CS_PIN, HIGH);
37 |
38 | return SPI_Dummy_Buff;
39 | }
40 |
41 | void ads1292r::ads1292_Init()
42 | {
43 | pinMode(ADS1292_DRDY_PIN, INPUT); //6
44 | pinMode(ADS1292_CS_PIN, OUTPUT); //7
45 | pinMode(ADS1292_START_PIN, OUTPUT); //5
46 | pinMode(ADS1292_PWDN_PIN, OUTPUT); //4
47 | // start the SPI library:
48 | SPI.begin();
49 | SPI.setBitOrder(MSBFIRST);
50 | //CPOL = 0, CPHA = 1
51 | SPI.setDataMode(SPI_MODE1);
52 | // Selecting 1Mhz clock for SPI
53 | SPI.setClockDivider(SPI_CLOCK_DIV16);
54 |
55 | ads1292_Reset();
56 | delay(100);
57 | ads1292_Disable_Start();
58 | ads1292_Enable_Start();
59 |
60 | ads1292_Hard_Stop();
61 | ads1292_Start_Data_Conv_Command();
62 | ads1292_Soft_Stop();
63 | delay(50);
64 | ads1292_Stop_Read_Data_Continuous(); // SDATAC command
65 | delay(300);
66 |
67 | ads1292_Reg_Write(ADS1292_REG_CONFIG1, 0x00); //Set sampling rate to 125 SPS
68 | delay(10);
69 | ads1292_Reg_Write(ADS1292_REG_CONFIG2, 0b10100000); //Lead-off comp off, test signal disabled
70 | delay(10);
71 | ads1292_Reg_Write(ADS1292_REG_LOFF, 0b00010000); //Lead-off defaults
72 | delay(10);
73 | ads1292_Reg_Write(ADS1292_REG_CH1SET, 0b01000000); //Ch 1 enabled, gain 6, connected to electrode in
74 | delay(10);
75 | ads1292_Reg_Write(ADS1292_REG_CH2SET, 0b01100000); //Ch 2 enabled, gain 6, connected to electrode in
76 | delay(10);
77 | ads1292_Reg_Write(ADS1292_REG_RLDSENS, 0b00101100); //RLD settings: fmod/16, RLD enabled, RLD inputs from Ch2 only
78 | delay(10);
79 | ads1292_Reg_Write(ADS1292_REG_LOFFSENS, 0x00); //LOFF settings: all disabled
80 | delay(10);
81 | //Skip register 8, LOFF Settings default
82 | ads1292_Reg_Write(ADS1292_REG_RESP1, 0b11110010); //Respiration: MOD/DEMOD turned only, phase 0
83 | delay(10);
84 | ads1292_Reg_Write(ADS1292_REG_RESP2, 0b00000011); //Respiration: Calib OFF, respiration freq defaults
85 | delay(10);
86 | ads1292_Start_Read_Data_Continuous();
87 | delay(10);
88 | ads1292_Enable_Start();
89 | }
90 |
91 | void ads1292r::ads1292_Reset()
92 | {
93 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
94 | delay(100); // Wait 100 mSec
95 | digitalWrite(ADS1292_PWDN_PIN, LOW);
96 | delay(100);
97 | digitalWrite(ADS1292_PWDN_PIN, HIGH);
98 | delay(100);
99 | }
100 |
101 | void ads1292r::ads1292_Disable_Start()
102 | {
103 | digitalWrite(ADS1292_START_PIN, LOW);
104 | delay(20);
105 | }
106 |
107 | void ads1292r::ads1292_Enable_Start()
108 | {
109 | digitalWrite(ADS1292_START_PIN, HIGH);
110 | delay(20);
111 | }
112 |
113 | void ads1292r::ads1292_Hard_Stop(void)
114 | {
115 | digitalWrite(ADS1292_START_PIN, LOW);
116 | delay(100);
117 | }
118 |
119 | void ads1292r::ads1292_Start_Data_Conv_Command(void)
120 | {
121 | ads1292_SPI_Command_Data(START); // Send 0x08 to the ADS1x9x
122 | }
123 |
124 | void ads1292r::ads1292_Soft_Stop(void)
125 | {
126 | ads1292_SPI_Command_Data(STOP); // Send 0x0A to the ADS1x9x
127 | }
128 |
129 | void ads1292r::ads1292_Start_Read_Data_Continuous(void)
130 | {
131 | ads1292_SPI_Command_Data(RDATAC); // Send 0x10 to the ADS1x9x
132 | }
133 |
134 | void ads1292r::ads1292_Stop_Read_Data_Continuous(void)
135 | {
136 | ads1292_SPI_Command_Data(SDATAC); // Send 0x11 to the ADS1x9x
137 | }
138 |
139 | void ads1292r::ads1292_SPI_Command_Data(unsigned char data_in)
140 | {
141 | byte data[1];
142 | //data[0] = data_in;
143 | digitalWrite(ADS1292_CS_PIN, LOW);
144 | delay(2);
145 | digitalWrite(ADS1292_CS_PIN, HIGH);
146 | delay(2);
147 | digitalWrite(ADS1292_CS_PIN, LOW);
148 | delay(2);
149 | SPI.transfer(data_in);
150 | delay(2);
151 | digitalWrite(ADS1292_CS_PIN, HIGH);
152 | }
153 |
154 | //Sends a write command to SCP1000
155 | void ads1292r::ads1292_Reg_Write(unsigned char READ_WRITE_ADDRESS, unsigned char DATA)
156 | {
157 | switch (READ_WRITE_ADDRESS)
158 | {
159 | case 1:
160 | DATA = DATA & 0x87;
161 | break;
162 | case 2:
163 | DATA = DATA & 0xFB;
164 | DATA |= 0x80;
165 | break;
166 | case 3:
167 | DATA = DATA & 0xFD;
168 | DATA |= 0x10;
169 | break;
170 | case 7:
171 | DATA = DATA & 0x3F;
172 | break;
173 | case 8:
174 | DATA = DATA & 0x5F;
175 | break;
176 | case 9:
177 | DATA |= 0x02;
178 | break;
179 | case 10:
180 | DATA = DATA & 0x87;
181 | DATA |= 0x01;
182 | break;
183 | case 11:
184 | DATA = DATA & 0x0F;
185 | break;
186 | default:
187 | break;
188 | }
189 |
190 | // now combine the register address and the command into one byte:
191 | byte dataToSend = READ_WRITE_ADDRESS | WREG;
192 |
193 | digitalWrite(ADS1292_CS_PIN, LOW);
194 | delay(2);
195 | digitalWrite(ADS1292_CS_PIN, HIGH);
196 | delay(2);
197 | // take the chip select low to select the device:
198 | digitalWrite(ADS1292_CS_PIN, LOW);
199 | delay(2);
200 | SPI.transfer(dataToSend); //Send register location
201 | SPI.transfer(0x00); //number of register to wr
202 | SPI.transfer(DATA); //Send value to record into register
203 |
204 | delay(2);
205 | // take the chip select high to de-select:
206 | digitalWrite(ADS1292_CS_PIN, HIGH);
207 | }
--------------------------------------------------------------------------------
/姿态手环/src/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "bno080/SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
3 | // #include
4 | #include "Ads_112c04/Ads_112c04.h"
5 | #include "Ads1292/ads1292r.h"
6 | extern "C"
7 | {
8 | #include "bno055/bno055.h"
9 | #include "ssd1306/pa_oled.h"
10 | }
11 | #include
12 | //adc通道
13 | #define ANALOG_PIN_0 34
14 |
15 | #define TFT_CS 13
16 | #define TFT_DC 12
17 | #define TFT_RST 14
18 | #include "button2/Button2.h"
19 | char buffer[20];
20 |
21 | Button2 btn1(35);
22 | Button2 btn2(0);
23 | // Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
24 | Ads_112c04 &ads = Ads_112c04::instance;
25 | ads1292r ads1292;
26 |
27 | const char *ssid = "MyESP32AP";
28 | const char *password = "00000000";
29 |
30 | bno055_vector_t vec;
31 |
32 | WiFiClient client;
33 | const char *host = "192.168.43.1";
34 | const uint16_t port = 1234;
35 | void task(void *pvPar)
36 | {
37 | uint64_t lastMillis = millis();
38 | while (1)
39 | {
40 | if (millis() - lastMillis > 80)
41 | {
42 | lastMillis = millis();
43 | uint8_t sbuf[9];
44 | short a = vec.x * 16;
45 | short b = vec.y * 16;
46 | short c = vec.z * 16;
47 | short d = StepCount::stepCount;
48 | sbuf[0] = 'x';
49 | sbuf[1] = a >> 8;
50 | sbuf[2] = a;
51 | sbuf[3] = b >> 8;
52 | sbuf[4] = b;
53 | sbuf[5] = c >> 8;
54 | sbuf[6] = c;
55 | sbuf[7] = d >> 8;
56 | sbuf[8] = d;
57 | client.write(sbuf, 9);
58 | }
59 | // printf("I'm %s\r\n",(char *)pvPar);
60 | //使用此延时API可以将任务转入阻塞态,期间CPU继续运行其它任务
61 | vTaskDelay(10 / portTICK_PERIOD_MS);
62 | }
63 | }
64 | double adcValue = 0;
65 | void adctask(void *pvPar)
66 | {
67 | while (1)
68 | {
69 | adcValue = analogRead(ANALOG_PIN_0);
70 | adcValue = adcValue * 6.6 / 4096;
71 | delay(1000);
72 | }
73 | }
74 |
75 | void setup()
76 | {
77 | pinMode(13, OUTPUT);
78 | digitalWrite(13, HIGH);
79 |
80 | // pinMode(ANALOG_PIN_0, INPUT);
81 |
82 | Serial.begin(115200);
83 | Serial.println();
84 | Serial.println("BNO080 Read Example");
85 |
86 | btn1.setPressedHandler([](Button2 &b) {
87 | Serial.println("btn1");
88 | StepCount::counting = !StepCount::counting;
89 | });
90 | btn2.setPressedHandler([](Button2 &b) {
91 | Serial.println("btn2");
92 | });
93 | // // Serial.begin(115200);
94 | // // Serial.println();
95 | // // Serial.println("BNO080 Read Example");
96 | // // tft.begin();
97 | // // tft.setRotation(2);
98 | // // tft.fillScreen(ILI9341_BLUE);
99 |
100 | // ads1292.ads1292_Init();
101 | Wire.begin();
102 | Wire.setClock(400000);
103 | OLED_Init();
104 | OLED_Clear();
105 | OLED_ShowString(0, 0, "helloWorld", 12);
106 |
107 | pa_BNO055_init();
108 | // Wire.setClock(400000); //Increase I2C data rate to 400kHz
109 | // // pa_BNO055_init();
110 | // // myIMU.begin();
111 | StepCount::initFifo();
112 | // ads.init(Ads_112c04::AxState::DGND, Ads_112c04::AxState::DGND);
113 | // ads.configRegister0(Ads_112c04::Gain::GAIN_1);
114 | // delay(100);
115 | // ads.configRegister1(Ads_112c04::SpeedOfSample::SPS_1000, Ads_112c04::Mode::Mode_Normal, Ads_112c04::ConvMode::Continuous);
116 | // ads.startConv();
117 |
118 | // myIMU.enableStepCounter(500); //Send data update every 500ms
119 | // myIMU.enableGyro(50);
120 | WiFi.begin(ssid, password);
121 | while (WiFi.status() != WL_CONNECTED)
122 | {
123 | delay(500);
124 | Serial.print(".");
125 | }
126 | xTaskCreate(task, //任务函数
127 | "task1", //这个参数没有什么作用,仅作为任务的描述
128 | 2048, //任务栈的大小
129 | NULL, //传给任务函数的参数
130 | 2, //优先级,数字越大优先级越高
131 | NULL);
132 | // xTaskCreate(adctask, //任务函数
133 | // "task2", //这个参数没有什么作用,仅作为任务的描述
134 | // 2048, //任务栈的大小
135 | // NULL, //传给任务函数的参数
136 | // 3, //优先级,数字越大优先级越高
137 | // NULL);
138 | }
139 | char fmqflag=0;
140 | void loop()
141 | {
142 | while (WiFi.status() != WL_CONNECTED)
143 | {
144 | delay(500);
145 | OLED_ShowString(0, 1, "wifi not connected", 12);
146 | }
147 | int connect_cnt = 0;
148 | if (!client.connect(host, port))
149 | {
150 | while (!client.connect(host, port))
151 | {
152 | delay(500);
153 | Serial.print("*");
154 | OLED_ShowString(0, 1, "server not connected", 12);
155 | connect_cnt++;
156 | if (connect_cnt > 4)
157 | {
158 | return;
159 | }
160 | }
161 | // Serial.println("connection failed");
162 | return;
163 | }
164 | while (1)
165 | {
166 | if(fmqflag){
167 | fmqflag--;
168 | digitalWrite(13, LOW);
169 | }else{
170 | digitalWrite(13, HIGH);
171 | // delay(50);
172 | // digitalWrite(13, HIGH);
173 | }
174 | if (WiFi.status() != WL_CONNECTED)
175 | {
176 | //Serial.println("wifi lost");
177 | break;
178 | }
179 |
180 | btn1.loop();
181 | btn2.loop();
182 | // Serial.println("BNO080 Read Example");
183 | vec = pa_BNO055_getVector();
184 |
185 | int stepCount = 0;
186 | if (pa_BNO055_isLastDataValid())
187 | {
188 | stepCount = StepCount::bno055_StepCount(vec);
189 | }
190 | {
191 | char buff[30];
192 | snprintf(buff, 30, "x %.2f ", vec.x);
193 | OLED_ShowString(0, 0, buff, 12);
194 | }
195 | {
196 | char buff[30];
197 | snprintf(buff, 30, "y %.2f ", vec.y);
198 | OLED_ShowString(0, 1, buff, 12);
199 | }
200 | {
201 | char buff[30];
202 | snprintf(buff, 30, "z %.2f ", vec.z);
203 | OLED_ShowString(0, 2, buff, 12);
204 | }
205 |
206 | {
207 | // char buff[30];
208 | // snprintf(buff, 30, caddr_t"", vec.z);
209 | OLED_ShowString(0, 4, (char *)(StepCount::counting ? "counting" : "stopped "), 12);
210 | }
211 | {
212 | static int a = 0;
213 | char buff[30];
214 | a = stepCount / 10;
215 | StepCount::distance = (float)(stepCount) * 0.5;
216 | snprintf(buff, 30, "steps: %d ", stepCount);
217 | OLED_ShowString(0, 5, buff, 16);
218 | snprintf(buff, 30, "distance:%5.2fm", StepCount::distance);
219 | OLED_ShowString(0, 7, buff, 12);
220 | }
221 | {
222 |
223 | char buff[30];
224 | snprintf(buff, 30, "ADC:%.2f ", adcValue);
225 | OLED_ShowString(0, 3, buff, 12);
226 | }
227 | }
228 |
229 | // Serial.println(line);
230 | client.stop();
231 | OLED_Clear();
232 | // Serial.printf("%f %f %f",vec.x,vec.y,vec.z);
233 | }
--------------------------------------------------------------------------------
/姿态手环/src/Adafruit_ILI9341.h:
--------------------------------------------------------------------------------
1 | /*!
2 | * @file Adafruit_ILI9341.h
3 | *
4 | * This is the documentation for Adafruit's ILI9341 driver for the
5 | * Arduino platform.
6 | *
7 | * This library works with the Adafruit 2.8" Touch Shield V2 (SPI)
8 | * http://www.adafruit.com/products/1651
9 | * Adafruit 2.4" TFT LCD with Touchscreen Breakout w/MicroSD Socket - ILI9341
10 | * https://www.adafruit.com/product/2478
11 | * 2.8" TFT LCD with Touchscreen Breakout Board w/MicroSD Socket - ILI9341
12 | * https://www.adafruit.com/product/1770
13 | * 2.2" 18-bit color TFT LCD display with microSD card breakout - ILI9340
14 | * https://www.adafruit.com/product/1770
15 | * TFT FeatherWing - 2.4" 320x240 Touchscreen For All Feathers
16 | * https://www.adafruit.com/product/3315
17 | *
18 | * These displays use SPI to communicate, 4 or 5 pins are required
19 | * to interface (RST is optional).
20 | *
21 | * Adafruit invests time and resources providing this open source code,
22 | * please support Adafruit and open-source hardware by purchasing
23 | * products from Adafruit!
24 | *
25 | *
26 | * This library depends on
27 | * Adafruit_GFX being present on your system. Please make sure you have
28 | * installed the latest version before using this library.
29 | *
30 | * Written by Limor "ladyada" Fried for Adafruit Industries.
31 | *
32 | * BSD license, all text here must be included in any redistribution.
33 | *
34 | */
35 |
36 | #ifndef _ADAFRUIT_ILI9341H_
37 | #define _ADAFRUIT_ILI9341H_
38 |
39 | #include "Adafruit_GFX.h"
40 | #include "Arduino.h"
41 | #include "Print.h"
42 | #include
43 | #include
44 | #include
45 |
46 | #define ILI9341_TFTWIDTH 240 ///< ILI9341 max TFT width
47 | #define ILI9341_TFTHEIGHT 320 ///< ILI9341 max TFT height
48 |
49 | #define ILI9341_NOP 0x00 ///< No-op register
50 | #define ILI9341_SWRESET 0x01 ///< Software reset register
51 | #define ILI9341_RDDID 0x04 ///< Read display identification information
52 | #define ILI9341_RDDST 0x09 ///< Read Display Status
53 |
54 | #define ILI9341_SLPIN 0x10 ///< Enter Sleep Mode
55 | #define ILI9341_SLPOUT 0x11 ///< Sleep Out
56 | #define ILI9341_PTLON 0x12 ///< Partial Mode ON
57 | #define ILI9341_NORON 0x13 ///< Normal Display Mode ON
58 |
59 | #define ILI9341_RDMODE 0x0A ///< Read Display Power Mode
60 | #define ILI9341_RDMADCTL 0x0B ///< Read Display MADCTL
61 | #define ILI9341_RDPIXFMT 0x0C ///< Read Display Pixel Format
62 | #define ILI9341_RDIMGFMT 0x0D ///< Read Display Image Format
63 | #define ILI9341_RDSELFDIAG 0x0F ///< Read Display Self-Diagnostic Result
64 |
65 | #define ILI9341_INVOFF 0x20 ///< Display Inversion OFF
66 | #define ILI9341_INVON 0x21 ///< Display Inversion ON
67 | #define ILI9341_GAMMASET 0x26 ///< Gamma Set
68 | #define ILI9341_DISPOFF 0x28 ///< Display OFF
69 | #define ILI9341_DISPON 0x29 ///< Display ON
70 |
71 | #define ILI9341_CASET 0x2A ///< Column Address Set
72 | #define ILI9341_PASET 0x2B ///< Page Address Set
73 | #define ILI9341_RAMWR 0x2C ///< Memory Write
74 | #define ILI9341_RAMRD 0x2E ///< Memory Read
75 |
76 | #define ILI9341_PTLAR 0x30 ///< Partial Area
77 | #define ILI9341_VSCRDEF 0x33 ///< Vertical Scrolling Definition
78 | #define ILI9341_MADCTL 0x36 ///< Memory Access Control
79 | #define ILI9341_VSCRSADD 0x37 ///< Vertical Scrolling Start Address
80 | #define ILI9341_PIXFMT 0x3A ///< COLMOD: Pixel Format Set
81 |
82 | #define ILI9341_FRMCTR1 \
83 | 0xB1 ///< Frame Rate Control (In Normal Mode/Full Colors)
84 | #define ILI9341_FRMCTR2 0xB2 ///< Frame Rate Control (In Idle Mode/8 colors)
85 | #define ILI9341_FRMCTR3 \
86 | 0xB3 ///< Frame Rate control (In Partial Mode/Full Colors)
87 | #define ILI9341_INVCTR 0xB4 ///< Display Inversion Control
88 | #define ILI9341_DFUNCTR 0xB6 ///< Display Function Control
89 |
90 | #define ILI9341_PWCTR1 0xC0 ///< Power Control 1
91 | #define ILI9341_PWCTR2 0xC1 ///< Power Control 2
92 | #define ILI9341_PWCTR3 0xC2 ///< Power Control 3
93 | #define ILI9341_PWCTR4 0xC3 ///< Power Control 4
94 | #define ILI9341_PWCTR5 0xC4 ///< Power Control 5
95 | #define ILI9341_VMCTR1 0xC5 ///< VCOM Control 1
96 | #define ILI9341_VMCTR2 0xC7 ///< VCOM Control 2
97 |
98 | #define ILI9341_RDID1 0xDA ///< Read ID 1
99 | #define ILI9341_RDID2 0xDB ///< Read ID 2
100 | #define ILI9341_RDID3 0xDC ///< Read ID 3
101 | #define ILI9341_RDID4 0xDD ///< Read ID 4
102 |
103 | #define ILI9341_GMCTRP1 0xE0 ///< Positive Gamma Correction
104 | #define ILI9341_GMCTRN1 0xE1 ///< Negative Gamma Correction
105 | //#define ILI9341_PWCTR6 0xFC
106 |
107 | // Color definitions
108 | #define ILI9341_BLACK 0x0000 ///< 0, 0, 0
109 | #define ILI9341_NAVY 0x000F ///< 0, 0, 123
110 | #define ILI9341_DARKGREEN 0x03E0 ///< 0, 125, 0
111 | #define ILI9341_DARKCYAN 0x03EF ///< 0, 125, 123
112 | #define ILI9341_MAROON 0x7800 ///< 123, 0, 0
113 | #define ILI9341_PURPLE 0x780F ///< 123, 0, 123
114 | #define ILI9341_OLIVE 0x7BE0 ///< 123, 125, 0
115 | #define ILI9341_LIGHTGREY 0xC618 ///< 198, 195, 198
116 | #define ILI9341_DARKGREY 0x7BEF ///< 123, 125, 123
117 | #define ILI9341_BLUE 0x001F ///< 0, 0, 255
118 | #define ILI9341_GREEN 0x07E0 ///< 0, 255, 0
119 | #define ILI9341_CYAN 0x07FF ///< 0, 255, 255
120 | #define ILI9341_RED 0xF800 ///< 255, 0, 0
121 | #define ILI9341_MAGENTA 0xF81F ///< 255, 0, 255
122 | #define ILI9341_YELLOW 0xFFE0 ///< 255, 255, 0
123 | #define ILI9341_WHITE 0xFFFF ///< 255, 255, 255
124 | #define ILI9341_ORANGE 0xFD20 ///< 255, 165, 0
125 | #define ILI9341_GREENYELLOW 0xAFE5 ///< 173, 255, 41
126 | #define ILI9341_PINK 0xFC18 ///< 255, 130, 198
127 |
128 | /**************************************************************************/
129 | /*!
130 | @brief Class to manage hardware interface with ILI9341 chipset (also seems to
131 | work with ILI9340)
132 | */
133 | /**************************************************************************/
134 |
135 | class Adafruit_ILI9341 : public Adafruit_SPITFT {
136 | public:
137 | Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,
138 | int8_t _RST = -1, int8_t _MISO = -1);
139 | Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _RST = -1);
140 | #if !defined(ESP8266)
141 | Adafruit_ILI9341(SPIClass *spiClass, int8_t dc, int8_t cs = -1,
142 | int8_t rst = -1);
143 | #endif // end !ESP8266
144 | Adafruit_ILI9341(tftBusWidth busWidth, int8_t d0, int8_t wr, int8_t dc,
145 | int8_t cs = -1, int8_t rst = -1, int8_t rd = -1);
146 |
147 | void begin(uint32_t freq = 0);
148 | void setRotation(uint8_t r);
149 | void invertDisplay(bool i);
150 | void scrollTo(uint16_t y);
151 | void setScrollMargins(uint16_t top, uint16_t bottom);
152 |
153 | // Transaction API not used by GFX
154 | void setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
155 |
156 | uint8_t readcommand8(uint8_t reg, uint8_t index = 0);
157 | };
158 |
159 | #endif // _ADAFRUIT_ILI9341H_
160 |
--------------------------------------------------------------------------------