reduced message bytes for BLE

Dependencies:   BLE_API mbed nRF51822

Fork of Inp_Fiber_Logo_FOTA by inupathy

Revision:
6:35f536162183
Parent:
5:b3fe9050a777
Child:
7:4118d5a717bc
--- a/main.cpp	Tue May 30 07:54:02 2017 +0000
+++ b/main.cpp	Sat Jun 17 08:22:21 2017 +0000
@@ -1,3 +1,6 @@
+// 20170617 ストレス値を"R"(relax)から"S"(stress)に変更し、(1.0 - relaxRatio)を送るよう改変
+// 20170617 輝度調整の方法をrgbの合計値を基準にするように調整した。
+
 #include "mbed.h"
 #include "neopixel.h"
 #include "nrf_soc.h"
@@ -23,8 +26,11 @@
 #define MAXCOLORARRAY 1 //(MIN_INTERVAL / INTERVAL) // 色平均値計算用の配列サイズ (MIN_INTERVAL / 7)
 #define MAXCONARRAY 10
 #define MAXSTRESSARRAY 3
-#define DIM_LED 0.5 //0.2
-//#define DIM_LED 1.0
+
+#define BRIGHTNESS_MAX 150 // RGBLED1セットのpwm合計値の上限 ws2812b型のneopixelが6個の場合、150を超えた値にするとセンサーが誤作動する
+
+#define DIM_LED ((float)(BRIGHTNESS_MAX) / (float)(255))   // R, G, Bそれぞれのpwm値がBRIGHTNESS_MAXを超えない様にする制限
+
 
 // 心拍数の最低値、最高値
 // 人間の場合は60~150、標準80
@@ -49,7 +55,6 @@
 #define SHOW_HAPPY  2
 #define SHOW_CONCENTRATION    4
 
-int displayFlag = 0;
 
 BLE  ble;
 DFUService *dfuService; // ★OTA用
@@ -100,7 +105,7 @@
 float prevFilteredDistRatio = 0.0;
 
 #define HR_THRESHOLD 2
-int HRThreshold[] = {120, 1000}; 
+int HRThreshold[] = {100, 1000}; 
 
 
 // 気分判定フラグ
@@ -125,34 +130,26 @@
 unsigned char RainbowR[] = { 50,  50,  50, 127, 200, 255};
 unsigned char RainbowG[] = { 50, 127, 255, 127, 100,  50};
 unsigned char RainbowB[] = {255, 127,  50,  50,  50,  50};
-/*
-unsigned char HappyR[] = {255, 100, 100, 255, 100, 100};
-unsigned char HappyG[] = {100, 255, 100, 100, 255, 100};
-unsigned char HappyB[] = {100, 100, 255, 100, 100, 255};
-*/
-unsigned char HappyR[] = {255, 200, 100, 255, 200, 100};
-unsigned char HappyG[] = {100, 255, 200, 100, 255, 200};
-unsigned char HappyB[] = {100, 100, 255, 100, 100, 255};
 
-unsigned char RelaxR[] = {  0, 100, 255,   0, 100, 255};
+unsigned char HappyR[] = { 50,  50,  50, 127, 200, 255};
+unsigned char HappyG[] = { 50, 127, 255, 127, 100,  50};
+unsigned char HappyB[] = {255, 127,  50,  50,  50,  50};
+
+unsigned char RelaxR[] = {  0,   0,   0,   0,   0,   0};
 unsigned char RelaxG[] = {255, 255, 255, 255, 255, 255};
-unsigned char RelaxB[] = {  0, 100, 255,   0, 100, 255};
+unsigned char RelaxB[] = {  0,   0,   0,   0,   0,   0};
 
 unsigned char ExciteR[] = {255, 255, 255, 255, 255, 255};
 unsigned char ExciteG[] = {100, 100, 100, 100, 100, 100};
-unsigned char ExciteB[] = { 75,  50,  25,  25,  50,  75};
-/*
-unsigned char StressR[] = {255, 255, 255, 255, 100, 100};
+unsigned char ExciteB[] = {  0,   0,   0,   0,   0,   0};
+
+unsigned char StressR[] = {100, 100, 100, 100, 100, 100};
 unsigned char StressG[] = {  0,   0,   0,   0,   0,   0};
-unsigned char StressB[] = {  0,   0, 100, 100, 255, 255};
-*/
-unsigned char StressR[] = {100, 100, 255, 255, 100, 100};
-unsigned char StressG[] = {  0,   0,   0,   0,   0,   0};
-unsigned char StressB[] = {255, 255, 100, 100, 255, 255};
+unsigned char StressB[] = {255, 255, 255, 255, 255, 255};
 
 unsigned char InterestR[] = {255, 255, 255, 255, 255, 255};
 unsigned char InterestG[] = {255, 255, 255, 255, 255, 255};
-unsigned char InterestB[] = {255, 100,   0,   0, 100, 255};
+unsigned char InterestB[] = {255, 255, 255, 255, 255, 255};
 
 float rainbowOffset = 0.0;
 
@@ -336,19 +333,14 @@
 
     float fBeatIndex = 0.0;
     
-//    displayFlag = SHOW_EXCITE;
-
-    displayFlag = SHOW_EXCITE | SHOW_HAPPY | SHOW_CONCENTRATION;
-    //displayFlag = SHOW_HAPPY ;
-
-//    mySerial.baud(9600);
-//    mySerial.format(8, Serial::None, 1);
-    
-//    PwrPin = 1;
-    
     static uint8_t rrIntervalH    = 0;
     static uint8_t rrIntervalL    = 1;
-//    static uint8_t bpm[8]         = {0x10, hrmCounter, 0, 0, 0, 0, rrIntervalH, rrIntervalL};
+
+    int r = 0;
+    int g = 0;
+    int b = 0;
+    int brightnessSum = 0;
+    int brightnessMax = 0;
 
     neopixel_strip_t neopixel1;
     neopixel_init(&neopixel1, P0_7, 1); 
@@ -384,17 +376,13 @@
     pixels[5] = &neopixel6;
     
     
-    int pixelIndex = 0;
+
     SetMoodColor();
-//    my_analogin_init();
-
         
     ble.init();
     ble.gap().onDisconnection(disconnectionCallback);
 
     
-//    ble.gap().onTimeout(ConnectTimeoutCallback);
-    
     UARTService uartService(ble);
     uartServicePtr = &uartService;
 
@@ -423,26 +411,38 @@
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(800); /* 1000ms */
     ble.gap().startAdvertising();
- 
     
     for( i = 0; i < NEOPIXEL_COUNT; i++)
-    {
-//        ColorArrayR[i] = RainbowR[i];
-//        ColorArrayG[i] = RainbowG[i];
-//        ColorArrayB[i] = RainbowB[i];
-        ColorArrayR[i] = StressR[i];
-        ColorArrayG[i] = StressG[i];
-        ColorArrayB[i] = StressB[i];
+    {   
+        // 起動時の色パターンを設定
+        ColorArrayR[i] = RainbowR[i];
+        ColorArrayG[i] = RainbowG[i];
+        ColorArrayB[i] = RainbowB[i];
+     
         TargetColorArrayR[i] = ColorArrayR[i];
         TargetColorArrayG[i] = ColorArrayG[i];
         TargetColorArrayB[i] = ColorArrayB[i];
-    
+        
+        // 輝度調整
+        r = ColorArrayR[i] * DIM_LED;
+        g = ColorArrayG[i] * DIM_LED;
+        b = ColorArrayB[i] * DIM_LED;
+     
+        brightnessSum = r + g + b;
+        
+        // 輝度の合計値が上限を超えた場合は補正する
+        if(brightnessSum > BRIGHTNESS_MAX) {
+            r = (int)(r * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
+            g = (int)(g * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
+            b = (int)(b * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
+        }
+        
         neopixel_set_color(
             pixels[i],
             0,
-            short(float(ColorArrayR[i] * DIM_LED)),
-            short(float(ColorArrayG[i] * DIM_LED)),
-            short(float(ColorArrayB[i] * DIM_LED)));
+            short(r),
+            short(g),
+            short(b));
         neopixel_show(pixels[i]);
     }
  
@@ -639,7 +639,7 @@
             // リラックスの係数を算出する
             relaxRatio = diffRatio;
 
-            nMsgRelaxLen = sprintf(msgRelax,"R%0.2f\0", relaxRatio); 
+            nMsgRelaxLen = sprintf(msgRelax,"S%0.2f\0", (1-relaxRatio)); 
             
 //            nMsgLen = sprintf(msg,"R%0.2f\0", relaxRatio); 
 //            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)msg, nMsgLen);
@@ -988,46 +988,49 @@
             }
         }
         
-//        for(i = NEOPIXEL_COUNT - 1; i >= 0; i--) {
-            if(ColorArrayR[pixelIndex] < TargetColorArrayR[pixelIndex]) {
-                ColorArrayR[pixelIndex]++;
+        for(i = NEOPIXEL_COUNT - 1; i >= 0; i--) {
+            if(ColorArrayR[i] < TargetColorArrayR[i]) {
+                ColorArrayR[i]++;
+            }
+            else if(ColorArrayR[i] > TargetColorArrayR[i]) {
+                ColorArrayR[i]--;
             }
-            else if(ColorArrayR[pixelIndex] > TargetColorArrayR[pixelIndex]) {
-                ColorArrayR[pixelIndex]--;
+            if(ColorArrayG[i] < TargetColorArrayG[i]) {
+                ColorArrayG[i]++;
             }
-/*            if(ColorArrayR[pixelIndex] < 200) {
-                ColorArrayR[pixelIndex]++;
+            else if(ColorArrayG[i] > TargetColorArrayG[i]) {
+                ColorArrayG[i]--;
+            }
+            if(ColorArrayB[i] < TargetColorArrayB[i]) {
+                ColorArrayB[i]++;
             }
-            else if(ColorArrayR[pixelIndex] > 200) {
-                ColorArrayR[pixelIndex]--;
-            }*/
-              if(ColorArrayG[pixelIndex] < TargetColorArrayG[pixelIndex]) {
-                ColorArrayG[pixelIndex]++;
+            else if(ColorArrayB[i] > TargetColorArrayB[i]) {
+                ColorArrayB[i]--;
             }
-            else if(ColorArrayG[pixelIndex] > TargetColorArrayG[pixelIndex]) {
-                ColorArrayG[pixelIndex]--;
-            }
-            if(ColorArrayB[pixelIndex] < TargetColorArrayB[pixelIndex]) {
-                ColorArrayB[pixelIndex]++;
-            }
-            else if(ColorArrayB[pixelIndex] > TargetColorArrayB[pixelIndex]) {
-                ColorArrayB[pixelIndex]--;
+            
+            // 輝度調整
+            r = ColorArrayR[i] * DIM_LED;
+            g = ColorArrayG[i] * DIM_LED;
+            b = ColorArrayB[i] * DIM_LED;
+         
+            brightnessSum = r + g + b;
+            
+            // 輝度の合計値が上限を超えた場合は補正する
+            if(brightnessSum > BRIGHTNESS_MAX) {
+                r = (int)(r * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
+                g = (int)(g * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
+                b = (int)(b * ((float)BRIGHTNESS_MAX / (float)brightnessSum));
             }
             
             neopixel_set_color(
-                pixels[pixelIndex],
+                pixels[i],
                 0,
-                short(float(ColorArrayR[pixelIndex]) * DIM_LED),
-                short(float(ColorArrayG[pixelIndex]) * DIM_LED),
-                short(float(ColorArrayB[pixelIndex]) * DIM_LED));
-            neopixel_show(pixels[pixelIndex]);
-//        }
-        pixelIndex++;
-        if(pixelIndex >= NEOPIXEL_COUNT)
-        {
-            pixelIndex = 0;
+                short(r),
+                short(g),
+                short(b));
+            neopixel_show(pixels[i]);
         }
         //wait_ms(1);
-        wait_us(500);
+        wait_ms(10); // 500usまで下げてもセンサー共振は起こらないことを確認
     }
 }