reduced message bytes for BLE

Dependencies:   BLE_API mbed nRF51822

Fork of Inp_Fiber_Logo_FOTA by inupathy

Committer:
ayaando
Date:
Tue Jul 04 00:36:43 2017 +0000
Revision:
8:1ad3ab5148bc
Parent:
7:4118d5a717bc
Child:
9:4363af5affba
BRIGHTNESS_MAX?100???; ?????????????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ayaando 8:1ad3ab5148bc 1 // 20170703 BRIGHTNESS_MAXを100に変更
ayaando 8:1ad3ab5148bc 2 // シリアル通信のプロトコルを、文字列→バイト列に変更
ayaando 7:4118d5a717bc 3 // 20170619 DeviceInformationのSerialNumberにIND+Macアドレスを適用し、DEVICENAMEはINUPATHYとした
jojisdogakane 6:35f536162183 4 // 20170617 ストレス値を"R"(relax)から"S"(stress)に変更し、(1.0 - relaxRatio)を送るよう改変
jojisdogakane 6:35f536162183 5 // 20170617 輝度調整の方法をrgbの合計値を基準にするように調整した。
jojisdogakane 6:35f536162183 6
jojisdogakane 0:2f6112d75752 7 #include "mbed.h"
jojisdogakane 0:2f6112d75752 8 #include "neopixel.h"
jojisdogakane 0:2f6112d75752 9 #include "nrf_soc.h"
jojisdogakane 0:2f6112d75752 10 #include "common.h"
jojisdogakane 0:2f6112d75752 11 #include "ble/BLE.h"
jojisdogakane 0:2f6112d75752 12 #include "ble/services/HeartRateService.h"
jojisdogakane 0:2f6112d75752 13 #include "ble/services/BatteryService.h"
jojisdogakane 0:2f6112d75752 14 #include "ble/services/DeviceInformationService.h"
jojisdogakane 0:2f6112d75752 15 #include "UARTService.h"
ayaando 1:f28bdfaf3005 16 #include "ble_gap.h"
ayaando 2:221df6d014f5 17 #include "ble/services/DFUService.h" // ★OTA用
ayaando 2:221df6d014f5 18
jojisdogakane 0:2f6112d75752 19 #define RAINBOW_RATIO 1
jojisdogakane 0:2f6112d75752 20 #define RCFILTER 0.9
jojisdogakane 0:2f6112d75752 21 #define RCFILTER2 0.9
jojisdogakane 4:a0150e0f285b 22 #define NEOPIXEL_COUNT 6
jojisdogakane 0:2f6112d75752 23 #define MIN_LIGHT 0
jojisdogakane 0:2f6112d75752 24 #define PNNX_FACTOR 0.1 // 0.06
jojisdogakane 4:a0150e0f285b 25 #define MAXARRAY 15 //15 // 心拍平均値計算用の配列サイズ //20
jojisdogakane 4:a0150e0f285b 26 #define MAXHRVARRAY 10 //30 // 脈間ゆらぎ平均値計算用の配列サイズ
jojisdogakane 0:2f6112d75752 27 #define MIN_INTERVAL 200
jojisdogakane 0:2f6112d75752 28 #define INTERVAL 10 // Loopの間隔 通常時推奨1 通信時推奨20
jojisdogakane 0:2f6112d75752 29 #define MAXCOLORARRAY 1 //(MIN_INTERVAL / INTERVAL) // 色平均値計算用の配列サイズ (MIN_INTERVAL / 7)
jojisdogakane 0:2f6112d75752 30 #define MAXCONARRAY 10
jojisdogakane 0:2f6112d75752 31 #define MAXSTRESSARRAY 3
jojisdogakane 6:35f536162183 32
ayaando 8:1ad3ab5148bc 33 #define BRIGHTNESS_MAX 100 // RGBLED1セットのpwm合計値の上限 ws2812b型のneopixelが6個の場合、150を超えた値にするとセンサーが誤作動する
jojisdogakane 6:35f536162183 34
jojisdogakane 6:35f536162183 35 #define DIM_LED ((float)(BRIGHTNESS_MAX) / (float)(255)) // R, G, Bそれぞれのpwm値がBRIGHTNESS_MAXを超えない様にする制限
jojisdogakane 6:35f536162183 36
jojisdogakane 0:2f6112d75752 37
jojisdogakane 0:2f6112d75752 38 // 心拍数の最低値、最高値
jojisdogakane 0:2f6112d75752 39 // 人間の場合は60~150、標準80
jojisdogakane 0:2f6112d75752 40 // 犬の場合は40~200、標準60
jojisdogakane 0:2f6112d75752 41 #define HR_MIN 40
jojisdogakane 0:2f6112d75752 42 #define HR_MAX 150
jojisdogakane 0:2f6112d75752 43
jojisdogakane 0:2f6112d75752 44 #define RELAX_RATIO_MIN 0.6
jojisdogakane 0:2f6112d75752 45
jojisdogakane 0:2f6112d75752 46 // ラジアンを度に変換する.
jojisdogakane 0:2f6112d75752 47 #define RadianToDegree(radian) ((180 / 3.14159) * (radian))
jojisdogakane 0:2f6112d75752 48
jojisdogakane 0:2f6112d75752 49 // 度をラジアンに変換する.
jojisdogakane 0:2f6112d75752 50 #define DegreeToRadian(degree) ((3.14159 / 180) * (degree))
jojisdogakane 0:2f6112d75752 51
jojisdogakane 0:2f6112d75752 52 // 色の識別値設定(ピンではない)
jojisdogakane 0:2f6112d75752 53 #define RED 0
jojisdogakane 0:2f6112d75752 54 #define BLUE 1
jojisdogakane 0:2f6112d75752 55 #define GREEN 2
jojisdogakane 0:2f6112d75752 56
jojisdogakane 0:2f6112d75752 57 #define SHOW_EXCITE 1
jojisdogakane 0:2f6112d75752 58 #define SHOW_HAPPY 2
jojisdogakane 0:2f6112d75752 59 #define SHOW_CONCENTRATION 4
jojisdogakane 0:2f6112d75752 60
jojisdogakane 0:2f6112d75752 61
jojisdogakane 0:2f6112d75752 62 BLE ble;
ayaando 2:221df6d014f5 63 DFUService *dfuService; // ★OTA用
jojisdogakane 0:2f6112d75752 64
jojisdogakane 0:2f6112d75752 65 AnalogIn HRSigIn(P0_4);
jojisdogakane 0:2f6112d75752 66
jojisdogakane 0:2f6112d75752 67
ayaando 7:4118d5a717bc 68 const static char DEVICE_NAME[] = "INUPATHY";
jojisdogakane 0:2f6112d75752 69 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
jojisdogakane 0:2f6112d75752 70 GattService::UUID_DEVICE_INFORMATION_SERVICE};
jojisdogakane 0:2f6112d75752 71 static volatile bool triggerSensorPolling = true;
jojisdogakane 0:2f6112d75752 72
jojisdogakane 4:a0150e0f285b 73 short ColorArrayR[NEOPIXEL_COUNT];
jojisdogakane 4:a0150e0f285b 74 short ColorArrayG[NEOPIXEL_COUNT];
jojisdogakane 4:a0150e0f285b 75 short ColorArrayB[NEOPIXEL_COUNT];
jojisdogakane 0:2f6112d75752 76
jojisdogakane 4:a0150e0f285b 77 short TargetColorArrayR[NEOPIXEL_COUNT];
jojisdogakane 4:a0150e0f285b 78 short TargetColorArrayG[NEOPIXEL_COUNT];
jojisdogakane 4:a0150e0f285b 79 short TargetColorArrayB[NEOPIXEL_COUNT];
jojisdogakane 0:2f6112d75752 80
jojisdogakane 0:2f6112d75752 81
jojisdogakane 4:a0150e0f285b 82 #define NEOPIXEL_COMMANDLEN ((NEOPIXEL_COUNT * 3) + 1 + 1)
jojisdogakane 0:2f6112d75752 83
jojisdogakane 0:2f6112d75752 84
jojisdogakane 0:2f6112d75752 85 unsigned long pixelStartCounter;
jojisdogakane 0:2f6112d75752 86
jojisdogakane 0:2f6112d75752 87 int mainCurrentPixel = 0;
jojisdogakane 0:2f6112d75752 88 int mainPrevPixel = 0;
jojisdogakane 0:2f6112d75752 89 int mainPixelLightCount;
jojisdogakane 0:2f6112d75752 90 int mainPixelStep = 0;
jojisdogakane 0:2f6112d75752 91 int mainStepsRemain = 0;
jojisdogakane 0:2f6112d75752 92
jojisdogakane 0:2f6112d75752 93 unsigned long currentmillis = 0;
jojisdogakane 0:2f6112d75752 94 unsigned long prevmillis = 0;
jojisdogakane 0:2f6112d75752 95 unsigned long intervalmillis = 0;
jojisdogakane 0:2f6112d75752 96 unsigned long prevIntervalmillis = 0;
jojisdogakane 0:2f6112d75752 97 unsigned long intervalmillisArray[MAXARRAY] = {1000};
jojisdogakane 4:a0150e0f285b 98 unsigned long intervalmillisHRVArray[MAXHRVARRAY] = {1000};
jojisdogakane 0:2f6112d75752 99 float conArray[MAXCONARRAY] = {0.5};
jojisdogakane 0:2f6112d75752 100
jojisdogakane 0:2f6112d75752 101 //float stressArray[MAXSTRESSARRAY] = {0.5};
jojisdogakane 0:2f6112d75752 102
jojisdogakane 0:2f6112d75752 103 float prevFilteredAngle = 0.5;
jojisdogakane 0:2f6112d75752 104 float happyRatio = 0.0;
jojisdogakane 4:a0150e0f285b 105 float happyRatioNow = 0.0;
jojisdogakane 0:2f6112d75752 106 float prevRelaxRatio = 1.0;
jojisdogakane 0:2f6112d75752 107 float prevFilteredDistRatio = 0.0;
jojisdogakane 4:a0150e0f285b 108
jojisdogakane 4:a0150e0f285b 109 #define HR_THRESHOLD 2
jojisdogakane 6:35f536162183 110 int HRThreshold[] = {100, 1000};
jojisdogakane 4:a0150e0f285b 111
jojisdogakane 0:2f6112d75752 112
jojisdogakane 4:a0150e0f285b 113 // 気分判定フラグ
jojisdogakane 4:a0150e0f285b 114 #define IS_RELAX 0
jojisdogakane 4:a0150e0f285b 115 #define IS_EXCITED 1
jojisdogakane 4:a0150e0f285b 116 #define IS_HAPPY 2
jojisdogakane 4:a0150e0f285b 117 #define IS_INTERESTED 3
jojisdogakane 4:a0150e0f285b 118 #define IS_STRESSED 4 // IS_STRESSEDはMOODの個数としても扱うので最後の番号にすること
jojisdogakane 4:a0150e0f285b 119 #define MOOD_COUNT IS_STRESSED + 1
jojisdogakane 0:2f6112d75752 120
jojisdogakane 4:a0150e0f285b 121 // 気分判定係数
jojisdogakane 4:a0150e0f285b 122 #define CONMARGIN 0.85
jojisdogakane 4:a0150e0f285b 123 #define RELAXMARGIN 0.2
jojisdogakane 4:a0150e0f285b 124 #define HAPPYMARGIN 0.225
jojisdogakane 0:2f6112d75752 125
jojisdogakane 4:a0150e0f285b 126 // 気分判定フラグを索引にする
jojisdogakane 4:a0150e0f285b 127 int MoodColorR[NEOPIXEL_COUNT][MOOD_COUNT];
jojisdogakane 4:a0150e0f285b 128 int MoodColorG[NEOPIXEL_COUNT][MOOD_COUNT];
jojisdogakane 4:a0150e0f285b 129 int MoodColorB[NEOPIXEL_COUNT][MOOD_COUNT];
jojisdogakane 4:a0150e0f285b 130
jojisdogakane 0:2f6112d75752 131
jojisdogakane 4:a0150e0f285b 132 unsigned char RainbowR[] = { 50, 50, 50, 127, 200, 255};
jojisdogakane 4:a0150e0f285b 133 unsigned char RainbowG[] = { 50, 127, 255, 127, 100, 50};
jojisdogakane 4:a0150e0f285b 134 unsigned char RainbowB[] = {255, 127, 50, 50, 50, 50};
jojisdogakane 4:a0150e0f285b 135
jojisdogakane 6:35f536162183 136 unsigned char HappyR[] = { 50, 50, 50, 127, 200, 255};
jojisdogakane 6:35f536162183 137 unsigned char HappyG[] = { 50, 127, 255, 127, 100, 50};
jojisdogakane 6:35f536162183 138 unsigned char HappyB[] = {255, 127, 50, 50, 50, 50};
jojisdogakane 6:35f536162183 139
jojisdogakane 6:35f536162183 140 unsigned char RelaxR[] = { 0, 0, 0, 0, 0, 0};
jojisdogakane 4:a0150e0f285b 141 unsigned char RelaxG[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 6:35f536162183 142 unsigned char RelaxB[] = { 0, 0, 0, 0, 0, 0};
jojisdogakane 0:2f6112d75752 143
jojisdogakane 4:a0150e0f285b 144 unsigned char ExciteR[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 4:a0150e0f285b 145 unsigned char ExciteG[] = {100, 100, 100, 100, 100, 100};
jojisdogakane 6:35f536162183 146 unsigned char ExciteB[] = { 0, 0, 0, 0, 0, 0};
jojisdogakane 6:35f536162183 147
jojisdogakane 6:35f536162183 148 unsigned char StressR[] = {100, 100, 100, 100, 100, 100};
jojisdogakane 4:a0150e0f285b 149 unsigned char StressG[] = { 0, 0, 0, 0, 0, 0};
jojisdogakane 6:35f536162183 150 unsigned char StressB[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 4:a0150e0f285b 151
jojisdogakane 4:a0150e0f285b 152 unsigned char InterestR[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 4:a0150e0f285b 153 unsigned char InterestG[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 6:35f536162183 154 unsigned char InterestB[] = {255, 255, 255, 255, 255, 255};
jojisdogakane 0:2f6112d75752 155
jojisdogakane 0:2f6112d75752 156 float rainbowOffset = 0.0;
jojisdogakane 0:2f6112d75752 157
jojisdogakane 0:2f6112d75752 158 bool bConcentration;
jojisdogakane 0:2f6112d75752 159 unsigned long loopCounter = 0;
jojisdogakane 0:2f6112d75752 160 int lightPos = 1;
jojisdogakane 0:2f6112d75752 161 int prevLightPos = 1;
jojisdogakane 0:2f6112d75752 162
jojisdogakane 0:2f6112d75752 163 double relaxRatio = 0.0;
jojisdogakane 0:2f6112d75752 164 float conRatio = 0.5;
jojisdogakane 0:2f6112d75752 165 float conRatioNow = 0.5;
jojisdogakane 0:2f6112d75752 166 double HR_ratio = 0.0;
jojisdogakane 0:2f6112d75752 167
jojisdogakane 0:2f6112d75752 168 unsigned long IBI;
jojisdogakane 0:2f6112d75752 169
jojisdogakane 0:2f6112d75752 170 int x3, y3;
jojisdogakane 0:2f6112d75752 171
jojisdogakane 0:2f6112d75752 172 int mainPixelCounter;
jojisdogakane 0:2f6112d75752 173
jojisdogakane 0:2f6112d75752 174 int prevInterval;
jojisdogakane 0:2f6112d75752 175 int prevprevInterval;
jojisdogakane 0:2f6112d75752 176
jojisdogakane 0:2f6112d75752 177 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
jojisdogakane 0:2f6112d75752 178 {
jojisdogakane 0:2f6112d75752 179 ble.gap().startAdvertising(); // restart advertising
jojisdogakane 0:2f6112d75752 180 }
jojisdogakane 0:2f6112d75752 181 /*
jojisdogakane 0:2f6112d75752 182 void ConnectTimeoutCallback(Gap::EventCallback_t timeoutCallback)
jojisdogakane 0:2f6112d75752 183 {
jojisdogakane 0:2f6112d75752 184 ble.gap().startAdvertising(); // restart advertising
jojisdogakane 0:2f6112d75752 185 }
jojisdogakane 0:2f6112d75752 186 */
jojisdogakane 0:2f6112d75752 187
jojisdogakane 0:2f6112d75752 188 uint8_t BPM = 0;
jojisdogakane 0:2f6112d75752 189 float sensorValue = 0;
jojisdogakane 0:2f6112d75752 190 Timer t;
jojisdogakane 0:2f6112d75752 191 Timer tRand;
jojisdogakane 0:2f6112d75752 192
jojisdogakane 0:2f6112d75752 193 float redVal = 0;
jojisdogakane 0:2f6112d75752 194 float blueVal = 255;
jojisdogakane 0:2f6112d75752 195 float greenVal = 0;
jojisdogakane 0:2f6112d75752 196
jojisdogakane 0:2f6112d75752 197 Ticker neopixelTick;
jojisdogakane 0:2f6112d75752 198
jojisdogakane 0:2f6112d75752 199 UARTService *uartServicePtr;
jojisdogakane 0:2f6112d75752 200
jojisdogakane 0:2f6112d75752 201 void neopixelTick_Handler() {
jojisdogakane 0:2f6112d75752 202 return;
jojisdogakane 0:2f6112d75752 203 }
jojisdogakane 0:2f6112d75752 204
jojisdogakane 4:a0150e0f285b 205 void SetMoodColor() {
jojisdogakane 4:a0150e0f285b 206 int i = 0;
jojisdogakane 4:a0150e0f285b 207
jojisdogakane 4:a0150e0f285b 208 for(i = 0; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 209 MoodColorR[i][IS_RELAX] = RelaxR[i];
jojisdogakane 4:a0150e0f285b 210 MoodColorG[i][IS_RELAX] = RelaxG[i];
jojisdogakane 4:a0150e0f285b 211 MoodColorB[i][IS_RELAX] = RelaxB[i];
jojisdogakane 4:a0150e0f285b 212 }
jojisdogakane 4:a0150e0f285b 213 for(i = 0; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 214 MoodColorR[i][IS_EXCITED] = ExciteR[i];
jojisdogakane 4:a0150e0f285b 215 MoodColorG[i][IS_EXCITED] = ExciteG[i];
jojisdogakane 4:a0150e0f285b 216 MoodColorB[i][IS_EXCITED] = ExciteB[i];
jojisdogakane 4:a0150e0f285b 217 }
jojisdogakane 4:a0150e0f285b 218
jojisdogakane 4:a0150e0f285b 219 for(i = 0; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 220 MoodColorR[i][IS_HAPPY] = HappyR[i];
jojisdogakane 4:a0150e0f285b 221 MoodColorG[i][IS_HAPPY] = HappyG[i];
jojisdogakane 4:a0150e0f285b 222 MoodColorB[i][IS_HAPPY] = HappyB[i];
jojisdogakane 4:a0150e0f285b 223 }
jojisdogakane 4:a0150e0f285b 224 for(i = 0; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 225 MoodColorR[i][IS_INTERESTED] = InterestR[i];
jojisdogakane 4:a0150e0f285b 226 MoodColorG[i][IS_INTERESTED] = InterestG[i];
jojisdogakane 4:a0150e0f285b 227 MoodColorB[i][IS_INTERESTED] = InterestB[i];
jojisdogakane 4:a0150e0f285b 228 }
jojisdogakane 4:a0150e0f285b 229 for(i = 0; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 230 MoodColorR[i][IS_STRESSED] = StressR[i];
jojisdogakane 4:a0150e0f285b 231 MoodColorG[i][IS_STRESSED] = StressG[i];
jojisdogakane 4:a0150e0f285b 232 MoodColorB[i][IS_STRESSED] = StressB[i];
jojisdogakane 4:a0150e0f285b 233 }
jojisdogakane 0:2f6112d75752 234 }
jojisdogakane 0:2f6112d75752 235
jojisdogakane 4:a0150e0f285b 236 // 現在の気分の判定
jojisdogakane 4:a0150e0f285b 237 int getMood(int bpm, double relaxRatio, double happyRatio, double conRatio) {
jojisdogakane 4:a0150e0f285b 238 // 第一優先:集中
jojisdogakane 4:a0150e0f285b 239 // 集中係数が0.95以上なら集中と判定
jojisdogakane 4:a0150e0f285b 240 if(conRatio >= CONMARGIN)
jojisdogakane 4:a0150e0f285b 241 {
jojisdogakane 4:a0150e0f285b 242 return IS_INTERESTED;
jojisdogakane 4:a0150e0f285b 243 }
jojisdogakane 0:2f6112d75752 244
jojisdogakane 4:a0150e0f285b 245 // 第二優先:Stress
jojisdogakane 4:a0150e0f285b 246 // リラックス係数が0.2以下
jojisdogakane 4:a0150e0f285b 247 if(relaxRatio <= RELAXMARGIN) {
jojisdogakane 4:a0150e0f285b 248 return IS_STRESSED;
jojisdogakane 4:a0150e0f285b 249 }
jojisdogakane 0:2f6112d75752 250
jojisdogakane 4:a0150e0f285b 251 // 第三優先:Happy
jojisdogakane 4:a0150e0f285b 252 // Happy係数が0.2以上
jojisdogakane 4:a0150e0f285b 253 if(happyRatio >= HAPPYMARGIN && relaxRatio > 0.5) {
jojisdogakane 4:a0150e0f285b 254 return IS_HAPPY;
jojisdogakane 4:a0150e0f285b 255 }
jojisdogakane 0:2f6112d75752 256
jojisdogakane 4:a0150e0f285b 257 // それ以外は興奮度合い
jojisdogakane 4:a0150e0f285b 258 // 心拍数の閾値判定
jojisdogakane 4:a0150e0f285b 259 int i = 0;
jojisdogakane 4:a0150e0f285b 260 for(i = 0; i < HR_THRESHOLD; i++) {
jojisdogakane 4:a0150e0f285b 261 if (BPM <= HRThreshold[i]) {
jojisdogakane 0:2f6112d75752 262 break;
jojisdogakane 0:2f6112d75752 263 }
jojisdogakane 0:2f6112d75752 264 }
jojisdogakane 4:a0150e0f285b 265 switch(i) {
jojisdogakane 4:a0150e0f285b 266 // 第1領域:NORMAL
jojisdogakane 4:a0150e0f285b 267 case 0:
jojisdogakane 4:a0150e0f285b 268 return IS_RELAX;
jojisdogakane 4:a0150e0f285b 269 // 第2領域:EXCITED1
jojisdogakane 4:a0150e0f285b 270 case 1:
jojisdogakane 4:a0150e0f285b 271 return IS_EXCITED;
jojisdogakane 0:2f6112d75752 272 }
jojisdogakane 0:2f6112d75752 273
jojisdogakane 4:a0150e0f285b 274 return IS_RELAX;
jojisdogakane 4:a0150e0f285b 275 }
jojisdogakane 0:2f6112d75752 276
jojisdogakane 4:a0150e0f285b 277 //Serial mySerial(USBTX, USBRX);
jojisdogakane 4:a0150e0f285b 278 //Serial mySerial(P0_9, P0_11);
jojisdogakane 0:2f6112d75752 279
ayaando 7:4118d5a717bc 280 // MACアドレスを取得してBLEのSerialNumberに組み込む
ayaando 7:4118d5a717bc 281 void setBleSerialNumber(char *serialNumber)
ayaando 1:f28bdfaf3005 282 {
ayaando 1:f28bdfaf3005 283 int i;
ayaando 1:f28bdfaf3005 284
ayaando 1:f28bdfaf3005 285 ble_gap_addr_t mac_address;
ayaando 1:f28bdfaf3005 286
ayaando 1:f28bdfaf3005 287 if(sd_ble_gap_address_get(&mac_address) != NRF_SUCCESS){
ayaando 1:f28bdfaf3005 288 return;
ayaando 1:f28bdfaf3005 289 }
ayaando 1:f28bdfaf3005 290
ayaando 1:f28bdfaf3005 291 // ADDR_LENは6
ayaando 1:f28bdfaf3005 292 for(i=BLE_GAP_ADDR_LEN-1; i>=0; i--){
ayaando 7:4118d5a717bc 293 sprintf(serialNumber, "%s%02x", serialNumber, (mac_address.addr)[i]);
ayaando 1:f28bdfaf3005 294 }
ayaando 7:4118d5a717bc 295 sprintf(serialNumber, "%s%s", serialNumber, "\0");
ayaando 1:f28bdfaf3005 296
ayaando 7:4118d5a717bc 297 }
jojisdogakane 0:2f6112d75752 298
jojisdogakane 0:2f6112d75752 299 int main(void)
jojisdogakane 0:2f6112d75752 300 {
ayaando 8:1ad3ab5148bc 301 /*
jojisdogakane 0:2f6112d75752 302 char msgIBI[100];
jojisdogakane 0:2f6112d75752 303 int nMsgIBILen = 0;
jojisdogakane 0:2f6112d75752 304 char msgHappy[10];
jojisdogakane 0:2f6112d75752 305 int nMsgHappyLen = 0;
jojisdogakane 0:2f6112d75752 306 char msgConcentration[10];
jojisdogakane 0:2f6112d75752 307 int nMsgConcentrationLen = 0;
jojisdogakane 0:2f6112d75752 308 char msgRelax[10];
jojisdogakane 0:2f6112d75752 309 int nMsgRelaxLen = 0;
ayaando 8:1ad3ab5148bc 310 */
ayaando 8:1ad3ab5148bc 311 byte_t msgHeader[3] = {0x49,0x4e,0x50};
jojisdogakane 0:2f6112d75752 312
ayaando 8:1ad3ab5148bc 313 byte_t msg[19];
ayaando 8:1ad3ab5148bc 314 // int nMsgLen = 0;
jojisdogakane 0:2f6112d75752 315
jojisdogakane 0:2f6112d75752 316 float Brightness = 1.0;
jojisdogakane 0:2f6112d75752 317 bool brighten = true;
jojisdogakane 0:2f6112d75752 318 float brightenSpeedMs = 1000.0;
jojisdogakane 0:2f6112d75752 319 float colorSpeedMs = 1000.0;
jojisdogakane 0:2f6112d75752 320 float conRatioShow = 0.5;
jojisdogakane 0:2f6112d75752 321
jojisdogakane 0:2f6112d75752 322 int i;
jojisdogakane 0:2f6112d75752 323 unsigned long lightPrevMillis = 0;
jojisdogakane 0:2f6112d75752 324
jojisdogakane 0:2f6112d75752 325 int currentBPM;
jojisdogakane 0:2f6112d75752 326
jojisdogakane 0:2f6112d75752 327 float fBeatIndex = 0.0;
jojisdogakane 0:2f6112d75752 328
jojisdogakane 0:2f6112d75752 329 static uint8_t rrIntervalH = 0;
jojisdogakane 0:2f6112d75752 330 static uint8_t rrIntervalL = 1;
jojisdogakane 6:35f536162183 331
jojisdogakane 6:35f536162183 332 int r = 0;
jojisdogakane 6:35f536162183 333 int g = 0;
jojisdogakane 6:35f536162183 334 int b = 0;
jojisdogakane 6:35f536162183 335 int brightnessSum = 0;
jojisdogakane 6:35f536162183 336 int brightnessMax = 0;
jojisdogakane 0:2f6112d75752 337
jojisdogakane 0:2f6112d75752 338 neopixel_strip_t neopixel1;
jojisdogakane 0:2f6112d75752 339 neopixel_init(&neopixel1, P0_7, 1);
jojisdogakane 0:2f6112d75752 340 neopixel_clear(&neopixel1);
jojisdogakane 0:2f6112d75752 341
jojisdogakane 0:2f6112d75752 342 neopixel_strip_t neopixel2;
jojisdogakane 0:2f6112d75752 343 neopixel_init(&neopixel2, P0_8, 1);
jojisdogakane 0:2f6112d75752 344 neopixel_clear(&neopixel2);
jojisdogakane 0:2f6112d75752 345
jojisdogakane 0:2f6112d75752 346 neopixel_strip_t neopixel3;
jojisdogakane 0:2f6112d75752 347 neopixel_init(&neopixel3, P0_10, 1);
jojisdogakane 0:2f6112d75752 348 neopixel_clear(&neopixel3);
jojisdogakane 0:2f6112d75752 349
jojisdogakane 0:2f6112d75752 350 neopixel_strip_t neopixel4;
jojisdogakane 0:2f6112d75752 351 neopixel_init(&neopixel4, P0_9, 1);
jojisdogakane 0:2f6112d75752 352 neopixel_clear(&neopixel4);
jojisdogakane 0:2f6112d75752 353
jojisdogakane 0:2f6112d75752 354 neopixel_strip_t neopixel5;
jojisdogakane 0:2f6112d75752 355 neopixel_init(&neopixel5, P0_15, 1);
jojisdogakane 0:2f6112d75752 356 neopixel_clear(&neopixel5);
jojisdogakane 0:2f6112d75752 357
jojisdogakane 0:2f6112d75752 358 neopixel_strip_t neopixel6;
jojisdogakane 0:2f6112d75752 359 neopixel_init(&neopixel6, P0_11, 1);
jojisdogakane 0:2f6112d75752 360 neopixel_clear(&neopixel6);
jojisdogakane 0:2f6112d75752 361
jojisdogakane 0:2f6112d75752 362 neopixel_strip_t *pixels[6];
jojisdogakane 0:2f6112d75752 363
jojisdogakane 0:2f6112d75752 364 pixels[0] = &neopixel1;
jojisdogakane 0:2f6112d75752 365 pixels[1] = &neopixel2;
jojisdogakane 0:2f6112d75752 366 pixels[2] = &neopixel3;
jojisdogakane 0:2f6112d75752 367 pixels[3] = &neopixel4;
jojisdogakane 0:2f6112d75752 368 pixels[4] = &neopixel5;
jojisdogakane 0:2f6112d75752 369 pixels[5] = &neopixel6;
jojisdogakane 0:2f6112d75752 370
jojisdogakane 4:a0150e0f285b 371
jojisdogakane 6:35f536162183 372
jojisdogakane 4:a0150e0f285b 373 SetMoodColor();
jojisdogakane 4:a0150e0f285b 374
jojisdogakane 0:2f6112d75752 375 ble.init();
jojisdogakane 0:2f6112d75752 376 ble.gap().onDisconnection(disconnectionCallback);
jojisdogakane 0:2f6112d75752 377
jojisdogakane 4:a0150e0f285b 378
jojisdogakane 0:2f6112d75752 379 UARTService uartService(ble);
jojisdogakane 0:2f6112d75752 380 uartServicePtr = &uartService;
jojisdogakane 0:2f6112d75752 381
ayaando 2:221df6d014f5 382 // ★OTA用
ayaando 2:221df6d014f5 383 dfuService = new DFUService(ble, NULL);
jojisdogakane 4:a0150e0f285b 384
jojisdogakane 0:2f6112d75752 385 /* Setup primary service. */
jojisdogakane 0:2f6112d75752 386 HeartRateService hrServiceBPM(ble, BPM, HeartRateService::LOCATION_CHEST);
jojisdogakane 0:2f6112d75752 387 // HeartRateService hrServiceIBI(ble, IBI, HeartRateService::LOCATION_FINGER);
jojisdogakane 0:2f6112d75752 388
jojisdogakane 0:2f6112d75752 389 // BatteryService batteryService(ble, 100);
jojisdogakane 0:2f6112d75752 390
jojisdogakane 0:2f6112d75752 391 /* Setup auxiliary service. */
ayaando 2:221df6d014f5 392 //DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
ayaando 7:4118d5a717bc 393
ayaando 7:4118d5a717bc 394 char serialNumber[16] = "IND";
ayaando 7:4118d5a717bc 395 // SerialNumberStringにMACアドレスを組み込む
ayaando 7:4118d5a717bc 396 setBleSerialNumber(serialNumber);
ayaando 7:4118d5a717bc 397
ayaando 2:221df6d014f5 398 // ★
ayaando 7:4118d5a717bc 399 DeviceInformationService deviceInfo(ble, "INUPATHY01", "0.0.1", serialNumber, "0.0.1", "0.0.1", "0.0.1");
ayaando 1:f28bdfaf3005 400
jojisdogakane 0:2f6112d75752 401 /* Setup advertising. */
jojisdogakane 0:2f6112d75752 402 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
jojisdogakane 0:2f6112d75752 403 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
jojisdogakane 0:2f6112d75752 404 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
jojisdogakane 0:2f6112d75752 405 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
jojisdogakane 0:2f6112d75752 406 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jojisdogakane 0:2f6112d75752 407 ble.gap().setAdvertisingInterval(800); /* 1000ms */
jojisdogakane 0:2f6112d75752 408 ble.gap().startAdvertising();
jojisdogakane 0:2f6112d75752 409
jojisdogakane 4:a0150e0f285b 410 for( i = 0; i < NEOPIXEL_COUNT; i++)
jojisdogakane 6:35f536162183 411 {
jojisdogakane 6:35f536162183 412 // 起動時の色パターンを設定
jojisdogakane 6:35f536162183 413 ColorArrayR[i] = RainbowR[i];
jojisdogakane 6:35f536162183 414 ColorArrayG[i] = RainbowG[i];
jojisdogakane 6:35f536162183 415 ColorArrayB[i] = RainbowB[i];
jojisdogakane 6:35f536162183 416
jojisdogakane 4:a0150e0f285b 417 TargetColorArrayR[i] = ColorArrayR[i];
jojisdogakane 4:a0150e0f285b 418 TargetColorArrayG[i] = ColorArrayG[i];
jojisdogakane 4:a0150e0f285b 419 TargetColorArrayB[i] = ColorArrayB[i];
jojisdogakane 6:35f536162183 420
jojisdogakane 6:35f536162183 421 // 輝度調整
jojisdogakane 6:35f536162183 422 r = ColorArrayR[i] * DIM_LED;
jojisdogakane 6:35f536162183 423 g = ColorArrayG[i] * DIM_LED;
jojisdogakane 6:35f536162183 424 b = ColorArrayB[i] * DIM_LED;
jojisdogakane 6:35f536162183 425
jojisdogakane 6:35f536162183 426 brightnessSum = r + g + b;
jojisdogakane 6:35f536162183 427
jojisdogakane 6:35f536162183 428 // 輝度の合計値が上限を超えた場合は補正する
jojisdogakane 6:35f536162183 429 if(brightnessSum > BRIGHTNESS_MAX) {
jojisdogakane 6:35f536162183 430 r = (int)(r * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 6:35f536162183 431 g = (int)(g * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 6:35f536162183 432 b = (int)(b * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 6:35f536162183 433 }
jojisdogakane 6:35f536162183 434
jojisdogakane 0:2f6112d75752 435 neopixel_set_color(
jojisdogakane 0:2f6112d75752 436 pixels[i],
jojisdogakane 0:2f6112d75752 437 0,
jojisdogakane 6:35f536162183 438 short(r),
jojisdogakane 6:35f536162183 439 short(g),
jojisdogakane 6:35f536162183 440 short(b));
jojisdogakane 0:2f6112d75752 441 neopixel_show(pixels[i]);
jojisdogakane 0:2f6112d75752 442 }
jojisdogakane 4:a0150e0f285b 443
jojisdogakane 0:2f6112d75752 444 t.start();
jojisdogakane 0:2f6112d75752 445
jojisdogakane 0:2f6112d75752 446 prevmillis = t.read_ms();
jojisdogakane 0:2f6112d75752 447
jojisdogakane 0:2f6112d75752 448 bool bPulseEvent = false;
jojisdogakane 0:2f6112d75752 449
jojisdogakane 0:2f6112d75752 450 float degPoint = 0;
jojisdogakane 0:2f6112d75752 451 float degRatio = 0;
jojisdogakane 0:2f6112d75752 452
jojisdogakane 0:2f6112d75752 453 bool bSwingR = false;
jojisdogakane 0:2f6112d75752 454
jojisdogakane 0:2f6112d75752 455 float swingIndex = 0.0;
jojisdogakane 0:2f6112d75752 456
jojisdogakane 0:2f6112d75752 457 float midPoint = 0.5;
jojisdogakane 0:2f6112d75752 458
jojisdogakane 0:2f6112d75752 459 // neopixelTick.attach(neopixelTick_Handler, 0.005);
jojisdogakane 0:2f6112d75752 460
jojisdogakane 0:2f6112d75752 461 while (1) {
jojisdogakane 0:2f6112d75752 462
jojisdogakane 0:2f6112d75752 463 // batteryService.updateBatteryLevel( (uint8_t)read_battery_level());
jojisdogakane 0:2f6112d75752 464
jojisdogakane 0:2f6112d75752 465 float currentValue = HRSigIn;
jojisdogakane 0:2f6112d75752 466
jojisdogakane 0:2f6112d75752 467 bPulseEvent = false;
jojisdogakane 0:2f6112d75752 468
jojisdogakane 4:a0150e0f285b 469 if ((currentValue <= 0.3) && (sensorValue > 0.3))
jojisdogakane 0:2f6112d75752 470 {
jojisdogakane 0:2f6112d75752 471 bPulseEvent = true;
jojisdogakane 0:2f6112d75752 472 }
jojisdogakane 0:2f6112d75752 473
jojisdogakane 0:2f6112d75752 474 int sens = 1023 * currentValue;
jojisdogakane 0:2f6112d75752 475
jojisdogakane 0:2f6112d75752 476 // printf("S%d", sens);
jojisdogakane 0:2f6112d75752 477 // printf("S%d\n", sens);
jojisdogakane 0:2f6112d75752 478
jojisdogakane 0:2f6112d75752 479 sensorValue = currentValue;
jojisdogakane 0:2f6112d75752 480
jojisdogakane 0:2f6112d75752 481 if(bPulseEvent) {
jojisdogakane 0:2f6112d75752 482 fBeatIndex = 0.0;
jojisdogakane 0:2f6112d75752 483 // batteryService.updateBatteryLevel( (uint8_t)read_battery_level());
jojisdogakane 0:2f6112d75752 484
jojisdogakane 0:2f6112d75752 485 // 現在の時刻を取得
jojisdogakane 0:2f6112d75752 486 // t.stop(); // stop, startは時間計測の結果を有意に短くしてしまうので使わない
jojisdogakane 0:2f6112d75752 487 currentmillis = t.read_ms();
jojisdogakane 0:2f6112d75752 488 // t.start();
jojisdogakane 0:2f6112d75752 489 // 現在時刻から過去時刻を引き、間隔を求める
jojisdogakane 0:2f6112d75752 490 intervalmillis = currentmillis - prevmillis;
jojisdogakane 0:2f6112d75752 491
jojisdogakane 0:2f6112d75752 492 // 心拍の間の間隔
jojisdogakane 0:2f6112d75752 493 IBI = intervalmillis;
jojisdogakane 0:2f6112d75752 494
jojisdogakane 0:2f6112d75752 495 // 現在の時刻を過去時刻として記録
jojisdogakane 0:2f6112d75752 496 prevmillis = currentmillis;
jojisdogakane 0:2f6112d75752 497
jojisdogakane 0:2f6112d75752 498 // 平均値算出用配列に値を詰める
jojisdogakane 0:2f6112d75752 499 double averageIntervalMillis;
jojisdogakane 0:2f6112d75752 500
jojisdogakane 0:2f6112d75752 501 for(i = 0; i < MAXARRAY ; i++)
jojisdogakane 0:2f6112d75752 502 {
jojisdogakane 0:2f6112d75752 503 // 最後尾以外の場合
jojisdogakane 0:2f6112d75752 504 if(i != MAXARRAY - 1)
jojisdogakane 0:2f6112d75752 505 {
jojisdogakane 0:2f6112d75752 506 // 次の要素の値をコピーする
jojisdogakane 0:2f6112d75752 507 intervalmillisArray[i] = intervalmillisArray[i + 1];
jojisdogakane 0:2f6112d75752 508 }
jojisdogakane 0:2f6112d75752 509 // 最後尾の場合
jojisdogakane 0:2f6112d75752 510 else {
jojisdogakane 0:2f6112d75752 511 // 最新値を詰める
jojisdogakane 0:2f6112d75752 512 intervalmillisArray[i] = intervalmillis;
jojisdogakane 0:2f6112d75752 513 }
jojisdogakane 0:2f6112d75752 514 }
jojisdogakane 0:2f6112d75752 515 // 先頭が0の間は何もしない
jojisdogakane 0:2f6112d75752 516 unsigned long sumIntervalMillis = 0;
jojisdogakane 0:2f6112d75752 517 unsigned long sumIntervalMillisV = 0;
jojisdogakane 0:2f6112d75752 518 if (intervalmillisArray[0] != 0)
jojisdogakane 0:2f6112d75752 519 {
jojisdogakane 0:2f6112d75752 520 // 平均値算出
jojisdogakane 0:2f6112d75752 521 averageIntervalMillis = 0;
jojisdogakane 0:2f6112d75752 522 for(i = 0; i < MAXARRAY ; i++)
jojisdogakane 0:2f6112d75752 523 {
jojisdogakane 0:2f6112d75752 524 sumIntervalMillis += intervalmillisArray[i];
jojisdogakane 0:2f6112d75752 525 sumIntervalMillisV += intervalmillisArray[i] * intervalmillisArray[i];
jojisdogakane 0:2f6112d75752 526 }
jojisdogakane 0:2f6112d75752 527 double squ;
jojisdogakane 0:2f6112d75752 528 double var;
jojisdogakane 0:2f6112d75752 529 double stdDev;
jojisdogakane 0:2f6112d75752 530
jojisdogakane 0:2f6112d75752 531 averageIntervalMillis = (double)sumIntervalMillis / (double)MAXARRAY;
jojisdogakane 0:2f6112d75752 532
jojisdogakane 0:2f6112d75752 533 squ = sumIntervalMillisV - (double)(sumIntervalMillis * sumIntervalMillis) / (double)MAXARRAY;
jojisdogakane 0:2f6112d75752 534 var = squ / (double)(MAXARRAY - 1);
jojisdogakane 0:2f6112d75752 535 stdDev = sqrt(var);
jojisdogakane 0:2f6112d75752 536
jojisdogakane 0:2f6112d75752 537 BPM = min32_of(60000 / averageIntervalMillis, 255);
jojisdogakane 0:2f6112d75752 538 // if(intervalmillis < averageIntervalMillis - stdDev) { // || intervalmillis > averageIntervalMillis + stdDev) {
jojisdogakane 0:2f6112d75752 539
jojisdogakane 0:2f6112d75752 540 currentBPM = min32_of(60000 / intervalmillis, 255);
jojisdogakane 0:2f6112d75752 541 /* }
jojisdogakane 0:2f6112d75752 542 else {
jojisdogakane 0:2f6112d75752 543 currentBPM = BPM;
jojisdogakane 0:2f6112d75752 544 } */
jojisdogakane 0:2f6112d75752 545 }
jojisdogakane 0:2f6112d75752 546 /*
jojisdogakane 0:2f6112d75752 547 int IBI1024 = IBI * (1024/1000);
jojisdogakane 0:2f6112d75752 548
jojisdogakane 0:2f6112d75752 549 bpm[1] = BPM;
jojisdogakane 0:2f6112d75752 550 bpm[6] = (IBI1024 & 0xFF00) >> 8;
jojisdogakane 0:2f6112d75752 551 bpm[7] = (IBI1024 & 0xFF);
jojisdogakane 0:2f6112d75752 552 */
jojisdogakane 0:2f6112d75752 553 // update bpm
jojisdogakane 0:2f6112d75752 554 if(ble.gap().getState().connected == 1)
jojisdogakane 0:2f6112d75752 555 {
jojisdogakane 0:2f6112d75752 556 hrServiceBPM.updateHeartRate(BPM);
jojisdogakane 0:2f6112d75752 557 // hrServiceIBI.updateHeartRate(IBI);
jojisdogakane 0:2f6112d75752 558 }
jojisdogakane 0:2f6112d75752 559
jojisdogakane 0:2f6112d75752 560 // printf("B%d\n", BPM);
jojisdogakane 0:2f6112d75752 561 // printf("Q%d\n", IBI);
jojisdogakane 0:2f6112d75752 562
jojisdogakane 0:2f6112d75752 563 // nMsgLen = sprintf(msg,"B%d\0", BPM);
jojisdogakane 0:2f6112d75752 564 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
jojisdogakane 0:2f6112d75752 565
ayaando 8:1ad3ab5148bc 566 // nMsgIBILen = sprintf(msgIBI,"Q%d\0", IBI);
jojisdogakane 0:2f6112d75752 567 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
jojisdogakane 0:2f6112d75752 568
jojisdogakane 0:2f6112d75752 569 // 平均値算出用配列に値を詰める
jojisdogakane 0:2f6112d75752 570 for(i = 0; i < MAXHRVARRAY ; i++)
jojisdogakane 0:2f6112d75752 571 {
jojisdogakane 0:2f6112d75752 572 // 最後尾以外の場合
jojisdogakane 0:2f6112d75752 573 if(i != MAXHRVARRAY - 1)
jojisdogakane 0:2f6112d75752 574 {
jojisdogakane 0:2f6112d75752 575 // 次の要素の値をコピーする
jojisdogakane 0:2f6112d75752 576 intervalmillisHRVArray[i] = intervalmillisHRVArray[i + 1];
jojisdogakane 0:2f6112d75752 577 }
jojisdogakane 0:2f6112d75752 578 // 最後尾の場合
jojisdogakane 0:2f6112d75752 579 else {
jojisdogakane 0:2f6112d75752 580 // 最新値を詰める
jojisdogakane 0:2f6112d75752 581 intervalmillisHRVArray[i] = intervalmillis;
jojisdogakane 0:2f6112d75752 582 }
jojisdogakane 0:2f6112d75752 583 }
jojisdogakane 0:2f6112d75752 584
jojisdogakane 0:2f6112d75752 585 // 平均値算出
jojisdogakane 0:2f6112d75752 586 int nNNInterval;
jojisdogakane 0:2f6112d75752 587 unsigned long ulIntervalSum = 0;
jojisdogakane 0:2f6112d75752 588 int nSampleCount = 0;
jojisdogakane 0:2f6112d75752 589 for(i = 0; i < MAXHRVARRAY ; i++)
jojisdogakane 0:2f6112d75752 590 {
jojisdogakane 0:2f6112d75752 591 if(intervalmillisHRVArray[i] > 0)
jojisdogakane 0:2f6112d75752 592 {
jojisdogakane 0:2f6112d75752 593 ulIntervalSum += intervalmillisHRVArray[i];
jojisdogakane 0:2f6112d75752 594 nSampleCount++;
jojisdogakane 0:2f6112d75752 595 }
jojisdogakane 0:2f6112d75752 596 }
jojisdogakane 0:2f6112d75752 597
jojisdogakane 0:2f6112d75752 598 float IntervalAverage = 0.0;
jojisdogakane 0:2f6112d75752 599 if(nSampleCount > 0){
jojisdogakane 0:2f6112d75752 600 IntervalAverage = (float)ulIntervalSum / (float)nSampleCount;
jojisdogakane 0:2f6112d75752 601 }
jojisdogakane 0:2f6112d75752 602
jojisdogakane 0:2f6112d75752 603 nNNInterval = (int)((float)IntervalAverage * PNNX_FACTOR);
jojisdogakane 0:2f6112d75752 604
jojisdogakane 0:2f6112d75752 605
jojisdogakane 0:2f6112d75752 606 bool bContinue = false;
jojisdogakane 0:2f6112d75752 607 int nNNxCount = 0;
jojisdogakane 0:2f6112d75752 608 for(i = 0; i < MAXHRVARRAY ; i++)
jojisdogakane 0:2f6112d75752 609 {
jojisdogakane 0:2f6112d75752 610 // if not end
jojisdogakane 0:2f6112d75752 611 if(i < MAXHRVARRAY - 1) {
jojisdogakane 0:2f6112d75752 612 if(abs((int)(intervalmillisHRVArray[i] - intervalmillisHRVArray[i + 1])) >= nNNInterval)
jojisdogakane 0:2f6112d75752 613 {
jojisdogakane 0:2f6112d75752 614 if(bContinue == false)
jojisdogakane 0:2f6112d75752 615 {
jojisdogakane 0:2f6112d75752 616 nNNxCount++;
jojisdogakane 0:2f6112d75752 617 }
jojisdogakane 0:2f6112d75752 618 nNNxCount++;
jojisdogakane 0:2f6112d75752 619 bContinue = true;
jojisdogakane 0:2f6112d75752 620 }
jojisdogakane 0:2f6112d75752 621 else {
jojisdogakane 0:2f6112d75752 622 bContinue = false;
jojisdogakane 0:2f6112d75752 623 }
jojisdogakane 0:2f6112d75752 624 }
jojisdogakane 0:2f6112d75752 625 }
jojisdogakane 0:2f6112d75752 626
jojisdogakane 0:2f6112d75752 627 float diffRatio = (float)nNNxCount / (float)nSampleCount;
jojisdogakane 0:2f6112d75752 628
jojisdogakane 0:2f6112d75752 629 if(diffRatio > 1)
jojisdogakane 0:2f6112d75752 630 {
jojisdogakane 0:2f6112d75752 631 diffRatio = 1;
jojisdogakane 0:2f6112d75752 632 }
jojisdogakane 0:2f6112d75752 633
jojisdogakane 0:2f6112d75752 634 // リラックスの係数を算出する
jojisdogakane 0:2f6112d75752 635 relaxRatio = diffRatio;
jojisdogakane 0:2f6112d75752 636
ayaando 8:1ad3ab5148bc 637 // nMsgRelaxLen = sprintf(msgRelax,"S%0.2f\0", (1-relaxRatio));
jojisdogakane 0:2f6112d75752 638
jojisdogakane 0:2f6112d75752 639 // nMsgLen = sprintf(msg,"R%0.2f\0", relaxRatio);
jojisdogakane 0:2f6112d75752 640 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
jojisdogakane 0:2f6112d75752 641
jojisdogakane 0:2f6112d75752 642 // 現在値から最低値を引く
jojisdogakane 0:2f6112d75752 643 // relaxRatio = relaxRatio - RELAX_RATIO_MIN;
jojisdogakane 0:2f6112d75752 644
jojisdogakane 0:2f6112d75752 645 // 1.0 / 最低値 × 現在値
jojisdogakane 0:2f6112d75752 646 // relaxRatio = relaxRatio * (1.0 / (1.0 - RELAX_RATIO_MIN));
jojisdogakane 0:2f6112d75752 647
jojisdogakane 0:2f6112d75752 648 int x1, y1, x2, y2;
jojisdogakane 0:2f6112d75752 649 int tempx1, tempy1, tempx2, tempy2, tempx3, tempy3;
jojisdogakane 0:2f6112d75752 650 int longx, shortx, longy, shorty;
jojisdogakane 0:2f6112d75752 651 long xx1, xx2, yy1, yy2;
jojisdogakane 0:2f6112d75752 652
jojisdogakane 0:2f6112d75752 653 int movex, movey;
jojisdogakane 0:2f6112d75752 654
jojisdogakane 0:2f6112d75752 655 double dist1, dist2;
jojisdogakane 0:2f6112d75752 656 double conRatioRatio;
jojisdogakane 0:2f6112d75752 657
jojisdogakane 0:2f6112d75752 658 /*
jojisdogakane 0:2f6112d75752 659 // 最後尾の3データから二つのx, y地点を作る。
jojisdogakane 0:2f6112d75752 660 x1 = intervalmillisArray[nSampleCount - 1];
jojisdogakane 0:2f6112d75752 661 y1 = intervalmillisArray[nSampleCount - 2];
jojisdogakane 0:2f6112d75752 662 x2 = intervalmillisArray[nSampleCount - 2];
jojisdogakane 0:2f6112d75752 663 y2 = intervalmillisArray[nSampleCount - 3];
jojisdogakane 0:2f6112d75752 664 */
jojisdogakane 0:2f6112d75752 665
jojisdogakane 0:2f6112d75752 666 x1 = intervalmillis;
jojisdogakane 0:2f6112d75752 667 y1 = prevInterval;
jojisdogakane 0:2f6112d75752 668 x2 = prevInterval;
jojisdogakane 0:2f6112d75752 669 y2 = prevprevInterval;
jojisdogakane 0:2f6112d75752 670
jojisdogakane 0:2f6112d75752 671 // 2地点を結ぶ直線の長さを算出する。
jojisdogakane 0:2f6112d75752 672 if(x1 > x2)
jojisdogakane 0:2f6112d75752 673 {
jojisdogakane 0:2f6112d75752 674 longx = x1;
jojisdogakane 0:2f6112d75752 675 shortx = x2;
jojisdogakane 0:2f6112d75752 676 }
jojisdogakane 0:2f6112d75752 677 else {
jojisdogakane 0:2f6112d75752 678 longx = x2;
jojisdogakane 0:2f6112d75752 679 shortx = x1;
jojisdogakane 0:2f6112d75752 680 }
jojisdogakane 0:2f6112d75752 681
jojisdogakane 0:2f6112d75752 682 if(y1 > y2)
jojisdogakane 0:2f6112d75752 683 {
jojisdogakane 0:2f6112d75752 684 longy = y1;
jojisdogakane 0:2f6112d75752 685 shorty = y2;
jojisdogakane 0:2f6112d75752 686 }
jojisdogakane 0:2f6112d75752 687 else {
jojisdogakane 0:2f6112d75752 688 longy = y2;
jojisdogakane 0:2f6112d75752 689 shorty = y1;
jojisdogakane 0:2f6112d75752 690 }
jojisdogakane 0:2f6112d75752 691
jojisdogakane 0:2f6112d75752 692 xx1 = longx - shortx;
jojisdogakane 0:2f6112d75752 693 yy1 = longy - shorty;
jojisdogakane 0:2f6112d75752 694
jojisdogakane 0:2f6112d75752 695 dist1 = sqrt((double)((xx1 * xx1) + (yy1 * yy1)));
jojisdogakane 0:2f6112d75752 696
jojisdogakane 0:2f6112d75752 697 dist2 = longx + longy;
jojisdogakane 0:2f6112d75752 698 float distRatio = dist1 / dist2;
jojisdogakane 0:2f6112d75752 699
jojisdogakane 0:2f6112d75752 700 conRatioNow = 1 - distRatio;
jojisdogakane 0:2f6112d75752 701
jojisdogakane 0:2f6112d75752 702 // 平均値算出用配列に値を詰める
jojisdogakane 0:2f6112d75752 703 float averageConRatio = 0.5;
jojisdogakane 0:2f6112d75752 704 for(i = 0; i < MAXCONARRAY ; i++)
jojisdogakane 0:2f6112d75752 705 {
jojisdogakane 0:2f6112d75752 706 // 最後尾以外の場合
jojisdogakane 0:2f6112d75752 707 if(i != MAXCONARRAY - 1)
jojisdogakane 0:2f6112d75752 708 {
jojisdogakane 0:2f6112d75752 709 // 次の要素の値をコピーする
jojisdogakane 0:2f6112d75752 710 conArray[i] = conArray[i + 1];
jojisdogakane 0:2f6112d75752 711 }
jojisdogakane 0:2f6112d75752 712 // 最後尾の場合
jojisdogakane 0:2f6112d75752 713 else {
jojisdogakane 0:2f6112d75752 714 // 最新値を詰める
jojisdogakane 0:2f6112d75752 715 conArray[i] = distRatio;
jojisdogakane 0:2f6112d75752 716 }
jojisdogakane 0:2f6112d75752 717 }
jojisdogakane 0:2f6112d75752 718 // 先頭が0の間は何もしない
jojisdogakane 0:2f6112d75752 719 if (conArray[0] != 0)
jojisdogakane 0:2f6112d75752 720 {
jojisdogakane 0:2f6112d75752 721 // 平均値算出
jojisdogakane 0:2f6112d75752 722 averageConRatio = 0;
jojisdogakane 0:2f6112d75752 723 for(i = 0; i < MAXCONARRAY ; i++)
jojisdogakane 0:2f6112d75752 724 {
jojisdogakane 0:2f6112d75752 725 averageConRatio += conArray[i];
jojisdogakane 0:2f6112d75752 726 }
jojisdogakane 0:2f6112d75752 727 averageConRatio = averageConRatio / (double)MAXCONARRAY;
jojisdogakane 0:2f6112d75752 728 }
jojisdogakane 0:2f6112d75752 729 /*
jojisdogakane 0:2f6112d75752 730 float averageStressRatio = 0.5;
jojisdogakane 0:2f6112d75752 731 for(i = 0; i < MAXSTRESSARRAY ; i++)
jojisdogakane 0:2f6112d75752 732 {
jojisdogakane 0:2f6112d75752 733 // 最後尾以外の場合
jojisdogakane 0:2f6112d75752 734 if(i != MAXSTRESSARRAY - 1)
jojisdogakane 0:2f6112d75752 735 {
jojisdogakane 0:2f6112d75752 736 // 次の要素の値をコピーする
jojisdogakane 0:2f6112d75752 737 stressArray[i] = stressArray[i + 1];
jojisdogakane 0:2f6112d75752 738 }
jojisdogakane 0:2f6112d75752 739 // 最後尾の場合
jojisdogakane 0:2f6112d75752 740 else {
jojisdogakane 0:2f6112d75752 741 // 最新値を詰める
jojisdogakane 0:2f6112d75752 742 if(x1 > y1) {
jojisdogakane 0:2f6112d75752 743 stressArray[i] = y1 / x1;
jojisdogakane 0:2f6112d75752 744 }
jojisdogakane 0:2f6112d75752 745 else {
jojisdogakane 0:2f6112d75752 746 stressArray[i] = x1 / y1;
jojisdogakane 0:2f6112d75752 747 }
jojisdogakane 0:2f6112d75752 748 }
jojisdogakane 0:2f6112d75752 749 }
jojisdogakane 0:2f6112d75752 750 // 先頭が0の間は何もしない
jojisdogakane 0:2f6112d75752 751 if (stressArray[0] != 0)
jojisdogakane 0:2f6112d75752 752 {
jojisdogakane 0:2f6112d75752 753 // 平均値算出
jojisdogakane 0:2f6112d75752 754 averageStressRatio = 0;
jojisdogakane 0:2f6112d75752 755 for(i = 0; i < MAXSTRESSARRAY ; i++)
jojisdogakane 0:2f6112d75752 756 {
jojisdogakane 0:2f6112d75752 757 averageStressRatio += stressArray[i];
jojisdogakane 0:2f6112d75752 758 }
jojisdogakane 0:2f6112d75752 759 averageStressRatio = averageStressRatio / (double)MAXSTRESSARRAY;
jojisdogakane 0:2f6112d75752 760 }
jojisdogakane 0:2f6112d75752 761 */
jojisdogakane 0:2f6112d75752 762
jojisdogakane 0:2f6112d75752 763 /* float rcFilter = RCFILTER2;
jojisdogakane 0:2f6112d75752 764 float filteredDistRatio = 0.0;
jojisdogakane 0:2f6112d75752 765 float a;
jojisdogakane 0:2f6112d75752 766 if(prevFilteredDistRatio != 0) {
jojisdogakane 0:2f6112d75752 767 a = rcFilter * prevFilteredDistRatio;
jojisdogakane 0:2f6112d75752 768 filteredDistRatio = a + (1 - rcFilter) * distRatio;
jojisdogakane 0:2f6112d75752 769 }
jojisdogakane 0:2f6112d75752 770 else {
jojisdogakane 0:2f6112d75752 771 filteredDistRatio = (1 - rcFilter) * distRatio;
jojisdogakane 0:2f6112d75752 772 }
jojisdogakane 0:2f6112d75752 773 prevFilteredDistRatio = filteredDistRatio ;
jojisdogakane 0:2f6112d75752 774
jojisdogakane 0:2f6112d75752 775 conRatio = filteredDistRatio ;
jojisdogakane 0:2f6112d75752 776 */
jojisdogakane 0:2f6112d75752 777 conRatio = averageConRatio;
jojisdogakane 0:2f6112d75752 778 conRatio = 1 - conRatio;
jojisdogakane 0:2f6112d75752 779
ayaando 8:1ad3ab5148bc 780 // nMsgConcentrationLen = sprintf(msgConcentration,"C%0.2f\0", conRatio);
jojisdogakane 0:2f6112d75752 781
jojisdogakane 0:2f6112d75752 782 // nMsgLen = sprintf(msg,"C%0.2f\0", conRatio);
jojisdogakane 0:2f6112d75752 783 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
jojisdogakane 0:2f6112d75752 784
jojisdogakane 0:2f6112d75752 785
jojisdogakane 0:2f6112d75752 786 prevprevInterval = prevInterval;
jojisdogakane 0:2f6112d75752 787 prevInterval = intervalmillis;
jojisdogakane 0:2f6112d75752 788
jojisdogakane 0:2f6112d75752 789 // (x2, y2)が原点となるように座標を変換する
jojisdogakane 0:2f6112d75752 790 movex = x2 * -1;
jojisdogakane 0:2f6112d75752 791 movey = y2 * -1;
jojisdogakane 0:2f6112d75752 792
jojisdogakane 0:2f6112d75752 793 tempx1 = x1 - movex;
jojisdogakane 0:2f6112d75752 794 tempy1 = y1 - movey;
jojisdogakane 0:2f6112d75752 795 tempx2 = x2 - movex;
jojisdogakane 0:2f6112d75752 796 tempy2 = y2 - movey;
jojisdogakane 0:2f6112d75752 797 tempx3 = x3 - movex;
jojisdogakane 0:2f6112d75752 798 tempy3 = y3 - movey;
jojisdogakane 0:2f6112d75752 799
jojisdogakane 0:2f6112d75752 800 // (x2, y2)を中心とした円を考える。
jojisdogakane 0:2f6112d75752 801
jojisdogakane 0:2f6112d75752 802 // (x3, y3)を(x2, y2)を中心に180°回転する。→(rotx3, roty3)
jojisdogakane 0:2f6112d75752 803 double theta = DegreeToRadian(180);
jojisdogakane 0:2f6112d75752 804
jojisdogakane 0:2f6112d75752 805 double rotx3 = (tempx3) * cos(theta) - (tempy3) * sin(theta);
jojisdogakane 0:2f6112d75752 806 double roty3 = (tempx3) * sin(theta) + (tempy3) * cos(theta);
jojisdogakane 0:2f6112d75752 807
jojisdogakane 0:2f6112d75752 808 // ベクトル1とベクトル3の内積を求める
jojisdogakane 0:2f6112d75752 809 double vecInt = tempx1 * rotx3 + tempy1 * roty3;
jojisdogakane 0:2f6112d75752 810
jojisdogakane 0:2f6112d75752 811 // ベクトル1とベクトル3の外積を求める
jojisdogakane 0:2f6112d75752 812 double vecExt = tempx1 * roty3 - tempy1 * rotx3;
jojisdogakane 0:2f6112d75752 813
jojisdogakane 0:2f6112d75752 814 // ベクトル1とベクトル3の角度を求める
jojisdogakane 0:2f6112d75752 815 theta = atan2(vecExt, vecInt);
jojisdogakane 0:2f6112d75752 816
jojisdogakane 0:2f6112d75752 817 double deg = RadianToDegree(theta);
jojisdogakane 0:2f6112d75752 818
jojisdogakane 0:2f6112d75752 819 midPoint = degRatio;
jojisdogakane 0:2f6112d75752 820
jojisdogakane 0:2f6112d75752 821 if( deg <= 11.25 && deg >= -11.25) {
jojisdogakane 0:2f6112d75752 822 degPoint = 0.5;
jojisdogakane 0:2f6112d75752 823 }
jojisdogakane 0:2f6112d75752 824 else if(deg >= 11.25 && deg <= 33.75) {
jojisdogakane 0:2f6112d75752 825 degPoint = 0.375;
jojisdogakane 0:2f6112d75752 826 }
jojisdogakane 0:2f6112d75752 827 else if(deg >= 33.75 && deg <= 56.25) {
jojisdogakane 0:2f6112d75752 828 degPoint = 0.25;
jojisdogakane 0:2f6112d75752 829 }
jojisdogakane 0:2f6112d75752 830 else if(deg >= 56.25 && deg <= 78.75) {
jojisdogakane 0:2f6112d75752 831 degPoint = 0.125;
jojisdogakane 0:2f6112d75752 832 }
jojisdogakane 0:2f6112d75752 833 else if(deg >= 78.75 && deg <= 101.25) {
jojisdogakane 0:2f6112d75752 834 degPoint = 0;
jojisdogakane 0:2f6112d75752 835 }
jojisdogakane 0:2f6112d75752 836 else if(deg >= 101.25 && deg <= 123.75) {
jojisdogakane 0:2f6112d75752 837 degPoint = 0.125;
jojisdogakane 0:2f6112d75752 838 }
jojisdogakane 0:2f6112d75752 839 else if(deg >= 123.75 && deg <= 146.25) {
jojisdogakane 0:2f6112d75752 840 degPoint = 0.25;
jojisdogakane 0:2f6112d75752 841 }
jojisdogakane 0:2f6112d75752 842 else if(deg >= 146.25 && deg <= 168.75) {
jojisdogakane 0:2f6112d75752 843 degPoint = 0.375;
jojisdogakane 0:2f6112d75752 844 }
jojisdogakane 0:2f6112d75752 845 else if(deg >= 168.75 && deg <= 180 || deg <= 0 && deg >= -11.25) {
jojisdogakane 0:2f6112d75752 846 degPoint = 0.5;
jojisdogakane 0:2f6112d75752 847 }
jojisdogakane 0:2f6112d75752 848 else if(deg <= -11.25 && deg >= -33.75) {
jojisdogakane 0:2f6112d75752 849 degPoint = 0.625;
jojisdogakane 0:2f6112d75752 850 }
jojisdogakane 0:2f6112d75752 851 else if(deg <= -33.75 && deg >= -56.25) {
jojisdogakane 0:2f6112d75752 852 degPoint = 0.75;
jojisdogakane 0:2f6112d75752 853 }
jojisdogakane 0:2f6112d75752 854 else if(deg <= -56.25 && deg >= -78.75) {
jojisdogakane 0:2f6112d75752 855 degPoint = 0.875;
jojisdogakane 0:2f6112d75752 856 }
jojisdogakane 0:2f6112d75752 857 else if(deg <= -78.75 && deg >= -101.25) {
jojisdogakane 0:2f6112d75752 858 degPoint = 1;
jojisdogakane 0:2f6112d75752 859 }
jojisdogakane 0:2f6112d75752 860 else if(deg <= -101.25 && deg >= -123.75) {
jojisdogakane 0:2f6112d75752 861 degPoint = 0.875;
jojisdogakane 0:2f6112d75752 862 }
jojisdogakane 0:2f6112d75752 863 else if(deg <= -123.75 && deg >= -146.25) {
jojisdogakane 0:2f6112d75752 864 degPoint = 0.75;
jojisdogakane 0:2f6112d75752 865 }
jojisdogakane 0:2f6112d75752 866 else if(deg <= -146.25 && deg >= -168.75) {
jojisdogakane 0:2f6112d75752 867 degPoint = 0.625;
jojisdogakane 0:2f6112d75752 868 }
jojisdogakane 0:2f6112d75752 869 else {
jojisdogakane 0:2f6112d75752 870 degPoint = 0;
jojisdogakane 0:2f6112d75752 871 }
jojisdogakane 0:2f6112d75752 872
jojisdogakane 4:a0150e0f285b 873 happyRatioNow = degPoint;
jojisdogakane 4:a0150e0f285b 874
jojisdogakane 0:2f6112d75752 875 degRatio = (deg + 180.0) / 360.0;
jojisdogakane 0:2f6112d75752 876
jojisdogakane 0:2f6112d75752 877 float rcFilter = RCFILTER;
jojisdogakane 0:2f6112d75752 878 float filteredAngle = rcFilter * prevFilteredAngle + (1 - rcFilter) * degPoint;
jojisdogakane 0:2f6112d75752 879 prevFilteredAngle = filteredAngle ;
jojisdogakane 0:2f6112d75752 880
jojisdogakane 0:2f6112d75752 881 happyRatio = filteredAngle - 0.5;
jojisdogakane 0:2f6112d75752 882
jojisdogakane 0:2f6112d75752 883 if(happyRatio <= 0){
jojisdogakane 0:2f6112d75752 884 happyRatio = abs(happyRatio);
jojisdogakane 0:2f6112d75752 885 }
jojisdogakane 0:2f6112d75752 886 else {
jojisdogakane 0:2f6112d75752 887 happyRatio = 0;
jojisdogakane 0:2f6112d75752 888 }
jojisdogakane 0:2f6112d75752 889
jojisdogakane 0:2f6112d75752 890 // happyRatio = averageStressRatio ;// @@@
jojisdogakane 0:2f6112d75752 891
ayaando 8:1ad3ab5148bc 892 // nMsgHappyLen = sprintf(msgHappy,"H%0.2f\0", happyRatio);
jojisdogakane 0:2f6112d75752 893
jojisdogakane 0:2f6112d75752 894 // nMsgLen = sprintf(msg,"H%0.2f\0", happyRatio);
jojisdogakane 0:2f6112d75752 895 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
jojisdogakane 0:2f6112d75752 896
jojisdogakane 4:a0150e0f285b 897 // happyRatio = happyRatio * happyRatio * happyRatio;
jojisdogakane 0:2f6112d75752 898
jojisdogakane 4:a0150e0f285b 899 // happyRatio *= 10;
jojisdogakane 0:2f6112d75752 900
jojisdogakane 0:2f6112d75752 901 x3 = x2;
jojisdogakane 0:2f6112d75752 902 y3 = y2;
jojisdogakane 0:2f6112d75752 903
jojisdogakane 0:2f6112d75752 904 int HR_range = 0;
jojisdogakane 0:2f6112d75752 905
jojisdogakane 0:2f6112d75752 906 int HR_tempval = currentBPM;
jojisdogakane 0:2f6112d75752 907
jojisdogakane 0:2f6112d75752 908
jojisdogakane 0:2f6112d75752 909 // 心拍の係数を算出する
jojisdogakane 0:2f6112d75752 910
jojisdogakane 0:2f6112d75752 911 // 最大心拍数以上→最大心拍数に
jojisdogakane 0:2f6112d75752 912 if( HR_MAX < HR_tempval)
jojisdogakane 0:2f6112d75752 913 {
jojisdogakane 0:2f6112d75752 914 HR_tempval = HR_MAX;
jojisdogakane 0:2f6112d75752 915 }
jojisdogakane 0:2f6112d75752 916
jojisdogakane 0:2f6112d75752 917 // 最少心拍数以下→最少心拍数に
jojisdogakane 0:2f6112d75752 918 if( HR_MIN > HR_tempval)
jojisdogakane 0:2f6112d75752 919 {
jojisdogakane 0:2f6112d75752 920 HR_tempval = HR_MIN;
jojisdogakane 0:2f6112d75752 921 }
jojisdogakane 0:2f6112d75752 922
jojisdogakane 0:2f6112d75752 923 // 心拍の幅を計算
jojisdogakane 0:2f6112d75752 924 HR_range = HR_MAX - HR_MIN;
jojisdogakane 0:2f6112d75752 925
jojisdogakane 0:2f6112d75752 926 // 現在値-最少心拍数
jojisdogakane 0:2f6112d75752 927 HR_tempval = HR_tempval - HR_MIN;
jojisdogakane 0:2f6112d75752 928
jojisdogakane 0:2f6112d75752 929 // 心拍の係数を算出。0 ~ 1.0
jojisdogakane 0:2f6112d75752 930 HR_ratio = (double)HR_tempval / (double)HR_range;
jojisdogakane 0:2f6112d75752 931
jojisdogakane 0:2f6112d75752 932 if(conRatioNow >= 0.95 /* && averageAngle >= 0.7*/)
jojisdogakane 0:2f6112d75752 933 {
jojisdogakane 0:2f6112d75752 934 bConcentration = true;
jojisdogakane 0:2f6112d75752 935 }
jojisdogakane 0:2f6112d75752 936 else {
jojisdogakane 0:2f6112d75752 937 bConcentration = false;
jojisdogakane 0:2f6112d75752 938 }
jojisdogakane 0:2f6112d75752 939 prevRelaxRatio = relaxRatio;
jojisdogakane 0:2f6112d75752 940
jojisdogakane 0:2f6112d75752 941 int hval = 1023 * happyRatio;
jojisdogakane 0:2f6112d75752 942 int rval = 1023 * relaxRatio;
jojisdogakane 0:2f6112d75752 943 int cval = 1023 * conRatio;
jojisdogakane 0:2f6112d75752 944
jojisdogakane 0:2f6112d75752 945 // printf("H%d\n", hval);
jojisdogakane 0:2f6112d75752 946 // printf("R%d\n", rval);
jojisdogakane 0:2f6112d75752 947 // printf("C%d\n", cval);
jojisdogakane 0:2f6112d75752 948
ayaando 8:1ad3ab5148bc 949 // sprintf(msg,"%s%s%s%s\0", msgIBI, msgHappy, msgRelax, msgConcentration);
ayaando 8:1ad3ab5148bc 950
ayaando 8:1ad3ab5148bc 951 float relaxRatioFloat = float(1-relaxRatio);
ayaando 8:1ad3ab5148bc 952
ayaando 8:1ad3ab5148bc 953 memcpy(msg, msgHeader, sizeof(msgHeader));
ayaando 8:1ad3ab5148bc 954 memcpy(&(msg[ 3]), &IBI , sizeof(IBI) );
ayaando 8:1ad3ab5148bc 955 memcpy(&(msg[ 7]), &happyRatio , sizeof(happyRatio));
ayaando 8:1ad3ab5148bc 956 memcpy(&(msg[11]), &relaxRatioFloat, sizeof(relaxRatioFloat));
ayaando 8:1ad3ab5148bc 957 memcpy(&(msg[15]), &conRatio , sizeof(conRatio) );
jojisdogakane 0:2f6112d75752 958 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),
ayaando 8:1ad3ab5148bc 959 (uint8_t*)msg, sizeof(msg));
ayaando 8:1ad3ab5148bc 960 // (uint8_t*)msg, nMsgIBILen + nMsgHappyLen + nMsgRelaxLen + nMsgConcentrationLen);
jojisdogakane 0:2f6112d75752 961 // (uint8_t*)msg, nMsgIBILen + 1 + nMsgHappyLen + 1 + nMsgRelaxLen + 1 + nMsgConcentrationLen);
jojisdogakane 0:2f6112d75752 962
jojisdogakane 0:2f6112d75752 963 // determine color range
jojisdogakane 0:2f6112d75752 964
jojisdogakane 0:2f6112d75752 965 conRatio = conRatio - 0.5;
jojisdogakane 0:2f6112d75752 966
jojisdogakane 0:2f6112d75752 967 if(conRatio < 0) {
jojisdogakane 0:2f6112d75752 968 conRatio = 0;
jojisdogakane 0:2f6112d75752 969 }
jojisdogakane 0:2f6112d75752 970
jojisdogakane 0:2f6112d75752 971 conRatio = conRatio * 2;
jojisdogakane 0:2f6112d75752 972
jojisdogakane 0:2f6112d75752 973 // happyRatio = happyRatio * 2;
jojisdogakane 0:2f6112d75752 974 // happyRatio = happyRatio * 2;
jojisdogakane 0:2f6112d75752 975 if(happyRatio > 1) {
jojisdogakane 0:2f6112d75752 976 happyRatio = 1;
jojisdogakane 0:2f6112d75752 977 }
jojisdogakane 4:a0150e0f285b 978 /*
jojisdogakane 0:2f6112d75752 979 if(BPM >= HR_MIN) {
jojisdogakane 0:2f6112d75752 980 happyRatio /= ((float)(BPM * 2) / 255.0) ;
jojisdogakane 0:2f6112d75752 981 }
jojisdogakane 4:a0150e0f285b 982 */
jojisdogakane 0:2f6112d75752 983 }
jojisdogakane 0:2f6112d75752 984
jojisdogakane 4:a0150e0f285b 985 if(BPM > 0 && bPulseEvent) {
jojisdogakane 4:a0150e0f285b 986 int NewMood = getMood(BPM, relaxRatio, happyRatio, conRatio) ;
jojisdogakane 4:a0150e0f285b 987
jojisdogakane 4:a0150e0f285b 988 for(i = 0 ; i < NEOPIXEL_COUNT; i++) {
jojisdogakane 4:a0150e0f285b 989 TargetColorArrayR[i] = MoodColorR[i][NewMood];
jojisdogakane 4:a0150e0f285b 990 TargetColorArrayG[i] = MoodColorG[i][NewMood];
jojisdogakane 4:a0150e0f285b 991 TargetColorArrayB[i] = MoodColorB[i][NewMood];
jojisdogakane 0:2f6112d75752 992 }
jojisdogakane 4:a0150e0f285b 993 }
jojisdogakane 0:2f6112d75752 994
jojisdogakane 6:35f536162183 995 for(i = NEOPIXEL_COUNT - 1; i >= 0; i--) {
jojisdogakane 6:35f536162183 996 if(ColorArrayR[i] < TargetColorArrayR[i]) {
jojisdogakane 6:35f536162183 997 ColorArrayR[i]++;
jojisdogakane 6:35f536162183 998 }
jojisdogakane 6:35f536162183 999 else if(ColorArrayR[i] > TargetColorArrayR[i]) {
jojisdogakane 6:35f536162183 1000 ColorArrayR[i]--;
jojisdogakane 4:a0150e0f285b 1001 }
jojisdogakane 6:35f536162183 1002 if(ColorArrayG[i] < TargetColorArrayG[i]) {
jojisdogakane 6:35f536162183 1003 ColorArrayG[i]++;
jojisdogakane 4:a0150e0f285b 1004 }
jojisdogakane 6:35f536162183 1005 else if(ColorArrayG[i] > TargetColorArrayG[i]) {
jojisdogakane 6:35f536162183 1006 ColorArrayG[i]--;
jojisdogakane 6:35f536162183 1007 }
jojisdogakane 6:35f536162183 1008 if(ColorArrayB[i] < TargetColorArrayB[i]) {
jojisdogakane 6:35f536162183 1009 ColorArrayB[i]++;
jojisdogakane 4:a0150e0f285b 1010 }
jojisdogakane 6:35f536162183 1011 else if(ColorArrayB[i] > TargetColorArrayB[i]) {
jojisdogakane 6:35f536162183 1012 ColorArrayB[i]--;
jojisdogakane 4:a0150e0f285b 1013 }
jojisdogakane 6:35f536162183 1014
jojisdogakane 6:35f536162183 1015 // 輝度調整
jojisdogakane 6:35f536162183 1016 r = ColorArrayR[i] * DIM_LED;
jojisdogakane 6:35f536162183 1017 g = ColorArrayG[i] * DIM_LED;
jojisdogakane 6:35f536162183 1018 b = ColorArrayB[i] * DIM_LED;
jojisdogakane 6:35f536162183 1019
jojisdogakane 6:35f536162183 1020 brightnessSum = r + g + b;
jojisdogakane 6:35f536162183 1021
jojisdogakane 6:35f536162183 1022 // 輝度の合計値が上限を超えた場合は補正する
jojisdogakane 6:35f536162183 1023 if(brightnessSum > BRIGHTNESS_MAX) {
jojisdogakane 6:35f536162183 1024 r = (int)(r * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 6:35f536162183 1025 g = (int)(g * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 6:35f536162183 1026 b = (int)(b * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
jojisdogakane 0:2f6112d75752 1027 }
jojisdogakane 0:2f6112d75752 1028
jojisdogakane 4:a0150e0f285b 1029 neopixel_set_color(
jojisdogakane 6:35f536162183 1030 pixels[i],
jojisdogakane 4:a0150e0f285b 1031 0,
jojisdogakane 6:35f536162183 1032 short(r),
jojisdogakane 6:35f536162183 1033 short(g),
jojisdogakane 6:35f536162183 1034 short(b));
jojisdogakane 6:35f536162183 1035 neopixel_show(pixels[i]);
jojisdogakane 0:2f6112d75752 1036 }
jojisdogakane 4:a0150e0f285b 1037 //wait_ms(1);
jojisdogakane 6:35f536162183 1038 wait_ms(10); // 500usまで下げてもセンサー共振は起こらないことを確認
jojisdogakane 0:2f6112d75752 1039 }
jojisdogakane 0:2f6112d75752 1040 }