masaaki makabe / Mbed 2 deprecated BLE_API

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "exio.h"
00003 #include "BLE.h"
00004 #include "DFUService.h"
00005 #include "common.h"
00006 #include <stdlib.h>
00007 #include <arm_math.h>
00008 #include "CurrentTimeService.h"
00009 #include "MMA845x.h"
00010 #include "common.h"
00011 
00012 
00013 // BLE
00014 #define INTERVAL_500MSEC        (500UL)
00015 #define CONNTIMEOUT_3000MSEC    (3000UL)
00016 #define ADV_TIMEOUT             (0)
00017 #define DEVICE_NAME "Kitchen Scale"
00018 #define BLE_TXPOWER_4DBM        (4)
00019 
00020 // Device Information Service (DIS) (20 character limit)
00021 // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
00022 #define MANUFACTURER_NAME_STRING        "Hacarus" // Manufacturer Name String - shall represent the name of the manufacturer of the device.
00023 #define MODEL_NUMBER_STRING             "0001" // Model Number String - shall represent the model number that is assigned by the device vendor.
00024 #define SERIAL_NUMBER_STRING            "000780c0ffeef00d"  // Serial Number String - shall represent the serial number for a particular instance of the device.
00025 #define FIRMWARE_REVISION_STRING        "v1.00.009@rev0028" // Firmware Revision String - shall represent the firmware revision for the firmware within the device.
00026 
00027 // Weight Scale Service (Original)
00028 //#define UUID_WEIGHT_SCALE_SERVICE       (0x181D)
00029 
00030 #if defined(PCB_VER1) || defined(PCB_VER2)
00031 // Switch
00032 #define SW_THRESHOLD            (0.5)
00033 #define SW_SAMPLECOUNT          (3)
00034 #endif
00035 
00036 // Mode
00037 #define MODE_OFF    (0) // LED OFF
00038 #define MODE_START  (1) // LED OFF -> ON
00039 #define MODE_ON     (2) // LED ON
00040 #define MODE_END    (3) // LED ON -> OFF
00041 /*S-----------------------------------------------------------*/
00042 State_t _state = S_WATCH;
00043 Mode_t _mode = M_SCALE;
00044 /*E-----------------------------------------------------------*/
00045 
00046 // Led
00047 #define LED_INTERVAL_MSEC   (100)
00048 #define BRIGHTNESS_ADDVALUE (0.1)
00049 #define BRIGHTNESS_MINVALUE (0.0)
00050 #define BRIGHTNESS_MAXVALUE (1.0)
00051 
00052 // Properties
00053 //io io;
00054 /*S---------------------------------------------------------------*/
00055 //io io(P0_15, P0_13); // HX711's CLK & DAT
00056 exio io;
00057 DFUService *p_dfu;
00058 /*E---------------------------------------------------------------*/
00059 //io(P0_5, P0_4); // HX711's CLK & DAT for BLEnano debug
00060 uint32_t weight_data;
00061 float32_t weight = 0.0;
00062 uint32_t scale = 0;
00063 int update_counter = 0;
00064 #if defined(PCB_VER1) || defined(PCB_VER2)
00065 float sw_data[SW_SAMPLECOUNT];
00066 uint8_t sw_count = 0;
00067 #endif
00068 int led_mode = MODE_OFF;
00069 float led_brightness = BRIGHTNESS_MINVALUE;
00070 
00071 #ifdef UART_DEBUG
00072 #if defined(PCB_VER1) || defined(PCB_VER2)
00073 Serial pc(P0_9, P0_8);// TX=P0_9
00074 #else
00075 Serial pc(P0_9, P0_11);// TX=P0_9
00076 #endif
00077 #define UART_BAUD_RATE   (9600UL)
00078 #define DEBUG(...)              { pc.printf(__VA_ARGS__); }
00079 #else
00080 #define DEBUG(...) {}
00081 #endif
00082 
00083 Timer t;
00084 
00085 // BLE
00086 BLE ble;
00087 Gap::ConnectionParams_t connectionParams;
00088 
00089 /* Complete list of 16-bit Service IDs */
00090 uint16_t    uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE};
00091 
00092 /* Weight Scale Service */
00093 static const uint8_t UUID_HACARUS_WEIGHT_CHAR[] = {0x00, 0x00, 0x2A, 0x9D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
00094 GattCharacteristic  WeightMeasurement (UUID(UUID_HACARUS_WEIGHT_CHAR), (uint8_t *)&weight_data, sizeof(weight_data), sizeof(weight_data),
00095                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
00096 static const uint8_t UUID_HACARUS_SCALE_CHAR[] = {0x00, 0x00, 0x2A, 0x9E, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
00097 GattCharacteristic  WeightScale (UUID(UUID_HACARUS_SCALE_CHAR), (uint8_t *)&scale, sizeof(scale), sizeof(scale),
00098                                  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
00099 GattCharacteristic *Chars[] = {&WeightMeasurement,&WeightScale};
00100 static const uint8_t UUID_HACARUS_WEIGHT_SERVICE[] = {0x00, 0x00, 0x18, 0x1D, 0x00, 0x01, 0x00, 0x01, 0x00, 'H','a', 'c', 'a', 'r','u', 's'};
00101 GattService HWS = GattService(UUID(UUID_HACARUS_WEIGHT_SERVICE), Chars, sizeof(Chars) / sizeof(GattCharacteristic *));
00102 
00103 /* Device Information Service */
00104 GattCharacteristic ManuName(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)&MANUFACTURER_NAME_STRING, sizeof(MANUFACTURER_NAME_STRING) - 1, sizeof(MANUFACTURER_NAME_STRING) - 1,
00105                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00106 GattCharacteristic ModelNum(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)&MODEL_NUMBER_STRING, sizeof(MODEL_NUMBER_STRING) - 1, sizeof(MODEL_NUMBER_STRING) - 1,
00107                             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00108 GattCharacteristic SerialNum(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)&SERIAL_NUMBER_STRING, sizeof(SERIAL_NUMBER_STRING) - 1, sizeof(SERIAL_NUMBER_STRING) - 1,
00109                              GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00110 GattCharacteristic FWVersion(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)&FIRMWARE_REVISION_STRING, sizeof(FIRMWARE_REVISION_STRING) - 1, sizeof(FIRMWARE_REVISION_STRING) - 1,
00111                              GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00112 GattCharacteristic *DISChars[] = {&ManuName, &ModelNum, &SerialNum, &FWVersion};
00113 GattService        DIS(GattService::UUID_DEVICE_INFORMATION_SERVICE , DISChars, sizeof(DISChars) / sizeof(GattCharacteristic *));
00114 
00115 /*S--------------------------------------------------------------------*/
00116 CurrentTimeService *p_CurrentTimeService; // Current Time Service
00117 MMA845x *p_MMA845x; // 加速度センサー(本体)
00118 I2C i2c(P0_17, P0_18); // 加速度センサー(I2C)
00119 /*E--------------------------------------------------------------------*/
00120 
00121 #ifdef PCB_VER1
00122 bool check_joystick()
00123 {
00124     float sum_data = 0;
00125 
00126     sw_data[sw_count] = io.get_x();
00127 
00128     if(++sw_count >= SW_SAMPLECOUNT) {
00129         sw_count = 0;
00130     }
00131 
00132     for(int count = 0; count < SW_SAMPLECOUNT; count++) {
00133         sum_data += sw_data[count];
00134     }
00135 
00136     return ((sum_data / SW_SAMPLECOUNT) >= SW_THRESHOLD);
00137 }
00138 #endif
00139 
00140 uint32_t quick_ieee11073_from_float(float data)
00141 {
00142     uint8_t  exponent = 0xFE; //exponent is -2
00143     uint32_t mantissa = (uint32_t)(data*100);
00144 
00145     return ( ((uint32_t)exponent) << 24) | mantissa;
00146 }
00147 
00148 #if defined(PCB_VER1) || defined(PCB_VER2)
00149 void SWInit(void)
00150 {
00151     // SW Initialize
00152     for(int count = 0; count < SW_SAMPLECOUNT; count++) {
00153         sw_data[count] = 0;
00154     }
00155 }
00156 #endif
00157 
00158 void AppInit(void)
00159 {
00160 
00161 #if defined(PCB_VER1) || defined(PCB_VER2)
00162     SWInit();
00163 #endif
00164     io.analog_pow(1);
00165 
00166 #ifdef PCB_VER3
00167     // check XTALFREQ for TaiyoYuden module in PCB_VER3
00168     /*
00169             if(NRF_UICR->XTALFREQ == 0xFFFFFF00){
00170                 io.display_value = 3232;
00171             }else if(NRF_UICR->XTALFREQ == 0xFFFFFFFF){
00172                 io.display_value = 1616;
00173             }
00174     */
00175 #endif
00176 }
00177 
00178 /*
00179  * BLE CallBacks
00180  */
00181 void BLEConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
00182 {
00183     ble.updateConnectionParams(params->handle, params->connectionParams);
00184 #ifdef UART_DEBUG
00185     pc.printf("BLEConnectionCallback \r\n");
00186 #endif
00187 }
00188 
00189 void BLEDisconnectionCallback(const Gap::DisconnectionCallbackParams_t  *params)
00190 {
00191     if(led_mode == MODE_ON) {
00192         ble.startAdvertising();
00193     }
00194 #ifdef UART_DEBUG
00195     pc.printf("BLEDisconnectionCallback \r\n");
00196 #endif
00197 }
00198 
00199 void BLERadioNotificationCallback(bool radio_active)
00200 {
00201     if (radio_active == false) {
00202         if(led_mode == MODE_ON) {
00203         }
00204     }
00205 #ifdef UART_DEBUG
00206     pc.printf("BLERadioNotificationCallback \r\n");
00207 #endif
00208 }
00209 
00210 
00211 
00212 void BleInitialize(void)
00213 {
00214 
00215     uint8_t advertiseServiceID[16];
00216 
00217     // Initialize
00218     ble.init();
00219 
00220     // Event Set
00221     ble.onConnection(&BLEConnectionCallback);
00222     ble.onDisconnection(&BLEDisconnectionCallback);
00223     ble.onRadioNotification(&BLERadioNotificationCallback);
00224 
00225     ble.getPreferredConnectionParams(&connectionParams);
00226     connectionParams.maxConnectionInterval = INTERVAL_500MSEC;
00227     connectionParams.minConnectionInterval = INTERVAL_500MSEC;
00228     connectionParams.connectionSupervisionTimeout = CONNTIMEOUT_3000MSEC;
00229     connectionParams.slaveLatency = 2;
00230     ble.setPreferredConnectionParams(&connectionParams);
00231 
00232     ble.setTxPower(BLE_TXPOWER_4DBM);
00233 
00234     for(int i=0; i<16; i++) {
00235         advertiseServiceID[i] = UUID_HACARUS_WEIGHT_SERVICE[16 - 1 - i];
00236     }
00237 
00238     ble.accumulateAdvertisingPayload((GapAdvertisingData::Flags)(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED));
00239     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00240                                      (const uint8_t *)DEVICE_NAME,
00241                                      strlen(DEVICE_NAME));
00242     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00243     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00244                                      (const uint8_t *)advertiseServiceID, sizeof(advertiseServiceID));
00245 
00246     ble.setAdvertisingInterval(INTERVAL_500MSEC);
00247     ble.setAdvertisingTimeout(ADV_TIMEOUT); /* 0 is disable the advertising timeout. */
00248 
00249 /*S-----------------------------------------------------------------------*/
00250     /*CurrentTimeServiceの初期化とサービス追加*/
00251     ble_date_time_t dt; // 1970/1/1 00:00:00 以降の初期値
00252     dt.year = 2016;
00253     dt.month = 7;
00254     dt.day = 31;
00255     dt.hours = 23;
00256     dt.minutes = 45;
00257     dt.seconds = 0;
00258     p_CurrentTimeService = new CurrentTimeService(ble, dt); 
00259 /*E-----------------------------------------------------------------------*/        
00260     ble.addService(HWS);
00261     ble.addService(DIS);
00262 // DFUサービスがスタックに割り当たっていたのをヒープへ移動
00263    //DFUService dfu(ble);
00264     p_dfu = new DFUService(ble);
00265 }
00266 /*S---------------------------------------------------------------------*/
00267 /*キー押されて1sec経過すればコールバックされる*/
00268 int _key1sec = 0;
00269 bool _analog_skip = false;
00270 void KeyOn(void)
00271 {
00272     _key1sec = 1;
00273 }
00274 void AccelInit(void)
00275 {
00276     /*加速度センサー初期化*/
00277     p_MMA845x = new MMA845x(i2c, MMA845x::SA0_VSS );
00278     //p_MMA845x->attachZAxisPulse(&zTap);
00279     
00280     const int ADR = 0x3A;
00281     char data[2], cmd = 0x0D;
00282     i2c.write(ADR, &cmd, 1, true);
00283     i2c.read(ADR, &data[0], 1);  // data[0]: Who am I? 
00284     
00285     //p_MMA845x->enableMotionMode();
00286     //p_MMA845x->registerDump();
00287     p_MMA845x->enableDataReadyMode();
00288     
00289     io.attach(&KeyOn);
00290 }
00291 /*モード決定*/
00292 void GetMode(void)
00293 {
00294     // 横置きなら時計タイマーモード、縦置きなら計量モード
00295     //int x = p_MMA845x->getX();
00296     //int y = p_MMA845x->getY();
00297     //int z = p_MMA845x->getZ();
00298     float xx = p_MMA845x->getXX();    
00299     _mode = (xx < 0.3) ? M_SCALE : M_WATCHTIMER;
00300 #ifdef UART_DEBUG
00301     pc.printf("KATAMUKI xx=%f, mode=%s\r\n", xx, (_mode == M_SCALE) ? "SCALE" : "WATCHTIMER");
00302 #endif
00303 }
00304 /*STATE決定*/
00305 void GetState(int sw)
00306 {
00307     ble_date_time_t dt;
00308     // スイッチ長押しでタイマー、通常押しで時間表示
00309     if(sw == exio::LongPressed) {
00310         p_CurrentTimeService->setCounter(0); // タイマーカウンタリセット
00311         io.displaySeconds(0);
00312         _state = S_TIMER;
00313 #ifdef UART_DEBUG
00314         pc.printf("S_TIMER START\r\n");
00315 #endif
00316     } else if(sw == exio::Pressed){
00317         p_CurrentTimeService->setTm(60/*sec*/); /*表示タイムアウト*/
00318         p_CurrentTimeService->readDateTime(dt); // 時計取得
00319         io.displayHHMM(dt);
00320         _state = S_WATCH;
00321 #ifdef UART_DEBUG
00322         pc.printf("S_WATCH START\r\n");
00323 #endif
00324     }
00325 }
00326 int TMain(int sw)
00327 {
00328     /*下記は時計/タイマーモード*/
00329     ble_date_time_t dt;
00330     int t;
00331     switch(_state){
00332         case S_TIMER:
00333             /*タイマー表示の場合*/
00334             t = p_CurrentTimeService->getCounter();
00335             io.displaySeconds(t); // タイマー表示
00336             // スイッチ押された場合終了
00337             if(sw != exio::None){
00338 #ifdef UART_DEBUG
00339                 pc.printf("S_TIMER EXIT\r\n");
00340 #endif
00341                 return 1; // モード終了
00342             }
00343             break;
00344         case S_WATCH:
00345             /*時計表示の場合*/
00346             p_CurrentTimeService->readDateTime(dt); // 時計取得
00347             io.displayHHMM(dt); // 時計表示
00348             // 表示タイムアウトもしくはスイッチ押しの場合終了
00349             if(/*p_CurrentTimeService->getTm() <= 0 ||*/ sw != exio::None){ // タイムアウトorスイッチ押し
00350 #ifdef UART_DEBUG
00351                 pc.printf("S_WATCH EXIT\r\n");
00352 #endif
00353                 return 1; // モード終了
00354             }
00355            break;
00356         default:
00357             break;
00358     }
00359     return 0; // モード継続
00360 }
00361 /*E-----------------------------------------------------------------------*/
00362 
00363 //DigitalOut _reg_ps(P0_1, 1); // 1=normal, 0=power_save
00364 //DigitalOut _adc_rate(P0_6, 1); // 0=10Hz, 1=80Hz (HX711's RATE pin)
00365 
00366 int main()
00367 {
00368 /*S----------------------------------------------------*/
00369     int sw;
00370 /*E----------------------------------------------------*/
00371     float weight_s;
00372     int Navg = 5;
00373     int sample = 0;
00374 #ifdef UART_DEBUG
00375     pc.baud(UART_BAUD_RATE);
00376     pc.printf("%s(%d): Program Start\r\n", __FILE__, __LINE__);
00377     // for checking SPI configuration (SPI1 is used_)
00378 //    pc.printf("SPI->PSELSCK  = %x\r\n", NRF_SPI0->PSELSCK);  // will be 15 (P0_15)
00379 //    pc.printf("SPI->PSELMISO = %x\r\n", NRF_SPI0->PSELMISO); // will be 13 (P0_13)
00380 //    pc.printf("SPI->PSELMOSI = %x\r\n", NRF_SPI0->PSELMOSI); // will be 14 (P0_14): dummy
00381 //    pc.printf("SPI->PSELSCK  = %x\r\n", NRF_SPI1->PSELSCK);  // will be 15 (P0_15)
00382 //    pc.printf("SPI->PSELMISO = %x\r\n", NRF_SPI1->PSELMISO); // will be 13 (P0_13)
00383 //    pc.printf("SPI->PSELMOSI = %x\r\n", NRF_SPI1->PSELMOSI); // will be 14 (P0_14): dummy
00384 #endif
00385 #ifdef PCB_VER3
00386     // set XTAL=32MHz for TaiyoYuden's module
00387     //  is moved to mbed-src/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c
00388     DEBUG("UICR->XTALFREQ=%x\r\n", NRF_UICR->XTALFREQ); // this should be 0xffffff00, not 0xffffffff
00389 #endif
00390 
00391     BleInitialize();
00392 /*S-----------------------------------------------*/
00393     AccelInit();
00394 /*E-----------------------------------------------*/
00395     AppInit();
00396 
00397     led_mode = MODE_OFF;
00398 #ifdef DISPLAY_DEMO
00399     uint16_t d = 0;
00400     uint8_t demo_count = 0;
00401     led_mode = MODE_START; // for debug mode
00402 #endif
00403     for (;; ) {
00404         // 100msec waitForEvent
00405         t.reset();
00406         while(t.read_ms() < LED_INTERVAL_MSEC) {
00407             t.start();
00408             ble.waitForEvent();
00409             t.stop();
00410         }
00411         switch(led_mode) {
00412             case MODE_OFF:
00413                 io.analog_pow(0);
00414                 io.display(0);
00415 /*S--------------------------------------------------------------------*/
00416                 sw = io.get_switch();
00417                 io.switch_reset();
00418                 if(sw != exio::None) {/*通常押しもしくは長押し*/
00419                     _key1sec = 0;
00420                     io.CallFlagClear();
00421                     GetMode(); /*傾きからモード決定*/
00422                     if(_mode == M_WATCHTIMER){
00423                         GetState(sw); //WATCH or TIMER ?
00424                     }
00425 #ifdef UART_DEBUG
00426                     pc.printf("GO MODE_START\r\n");
00427 #endif
00428 /*E--------------------------------------------------------------------*/
00429                     led_mode = MODE_START;
00430                 }
00431 /*S---------------------------------------------------------------------------*/
00432                 else {
00433                     // キー押された状態で1sec経過した場合表示のみ開始、Timerモード開始は後ほど
00434                     if(_key1sec == 1){
00435                         _mode = M_WATCHTIMER;
00436                         _state = S_TIMER;                        
00437                         io.analog_pow(1);
00438                         io.power_save_mode(0);
00439                         _analog_skip = true;
00440                         io.displaySeconds(0);
00441                         //io.display_unitT();
00442                         _key1sec = 2;
00443                     } else if(_key1sec == 2){
00444                         led_brightness += BRIGHTNESS_ADDVALUE; // 徐々に明るく
00445                         io.display(led_brightness);
00446                         if(led_brightness >= BRIGHTNESS_MAXVALUE) _key1sec = 3;
00447                     } else {
00448                         io.display(led_brightness);
00449                     }
00450                 }
00451 /*E---------------------------------------------------------------------------*/
00452                 break;
00453             case MODE_START:
00454                 //if(_analog_skip) {_analog_skip=false; goto L090;}
00455                 io.analog_pow(1);
00456                 io.power_save_mode(0);
00457 /*S----------------------------------------------------------------*/
00458 //L090:
00459                 // タイマー時はここで毎回更新
00460                 //if(_mode == M_WATCHTIMER && _state == S_TIMER){
00461                 //    p_CurrentTimeService->setCounter(0); // タイマーカウンタリセット
00462                 //    io.displaySeconds(0);
00463                 /*} else*/ if(_mode == M_SCALE){
00464                     io.display_value = 0;
00465                 }
00466 /*E----------------------------------------------------------------*/
00467                 led_brightness += BRIGHTNESS_ADDVALUE;
00468                 io.display(led_brightness);
00469                 if(led_brightness >= BRIGHTNESS_MAXVALUE) {
00470 /*S-----------------------------------------------------------------------*/
00471                     if(_mode == M_WATCHTIMER){
00472                         ble.startAdvertising();
00473                         led_mode = MODE_ON;
00474                         break; /*時間/タイマーモードはここで終了*/
00475                     }
00476 /*E-----------------------------------------------------------------------*/
00477                     /*計量モードの初期処理*/
00478                     update_counter = 0;
00479                     io.calibrate_weight(); /*ここがゼロ補正*/
00480 #if defined(PCB_VER1) || defined(PCB_VER2)
00481                     SWInit();
00482 #endif
00483                     ble.startAdvertising();
00484                     led_mode = MODE_ON;
00485                     weight_s = 0.0;
00486                     sample = 0;
00487                 }
00488                 break;
00489             case MODE_ON:
00490 #ifdef UART_DEBUG
00491                 //pc.printf("%d %d %.2f\r\n", io.get_switch(), io.get_weight_raw(), io.get_weight());
00492 #endif
00493                 io.analog_pow(1);
00494 #ifdef DISPLAY_DEMO
00495                 demo_count++;
00496                 if (demo_count == 10) {
00497                     demo_count = 0;
00498                     io.display_value = d++; // increment display value for every 1s in demo mode
00499                     weight_data = quick_ieee11073_from_float(d);
00500                     ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
00501                                                   (uint8_t *)&weight_data,
00502                                                   sizeof(weight_data));
00503                 }
00504 #else
00505 /*S-------------------------------------------------------------------------*/
00506                 sw = io.get_switch();
00507                 io.switch_reset();
00508                 if(_mode == M_WATCHTIMER){
00509                     int ret = TMain(sw); // 時間/タイマー処理へ
00510                     if(ret == 1){ // モード終了
00511                         goto L010;
00512                     }
00513                     break; // 時間/タイマーモードはここで終了
00514                 }
00515                 /*下記は計量モード*/
00516                 if(sw == exio::Pressed) {
00517 L010:
00518 #ifdef UART_DEBUG
00519                     pc.printf("GO MODE_END\r\n");
00520 #endif
00521 /*E-------------------------------------------------------------------------*/
00522                     led_mode = MODE_END;
00523                     if(ble.getGapState().connected) {
00524                         ble.disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
00525                     } else {
00526                         ble.stopAdvertising();
00527                     }
00528                 } else {
00529                     scale++;
00530                     ble.updateCharacteristicValue(WeightScale.getValueAttribute().getHandle(),
00531                                                   (uint8_t *)&scale,
00532                                                   sizeof(scale));
00533 
00534                     weight_s += io.get_weight();
00535                     sample++;
00536                     if (sample == Navg) {
00537                         weight = weight_s / (float)Navg;
00538                         io.display_value = (uint16_t)weight;
00539                         weight_s = 0.0;
00540                         sample = 0;
00541 #ifdef UART_DEBUG
00542                         pc.printf("weight=%.1f\r\n", weight);
00543 #endif
00544                     }
00545 //                    pc.printf("weight=%.1f\r\n", weight);
00546 //                    io.display_value = 8888; // for LED soldering check
00547 #ifdef UART_DEBUG
00548 //                    pc.printf("%d\r\n", io._get_adc_raw(0));
00549 //                    pc.printf("weight=%f %d / %d\r\n", weight, io.display_value, io._adc0);
00550 #endif
00551                     if(++update_counter >= 5) {
00552                         weight_data = quick_ieee11073_from_float(weight);
00553                         ble.updateCharacteristicValue(WeightMeasurement.getValueAttribute().getHandle(),
00554                                                       (uint8_t *)&weight_data,
00555                                                       sizeof(weight_data));
00556                         update_counter = 0;
00557                     }
00558                 }
00559 #endif
00560                 break;
00561             case MODE_END:
00562                 led_brightness -= BRIGHTNESS_ADDVALUE;
00563                 io.display(led_brightness);
00564                 if(led_brightness <= BRIGHTNESS_MINVALUE) {
00565 #if defined(PCB_VER1) || defined(PCB_VER2)
00566                     SWInit();
00567 #endif
00568                     led_mode = MODE_OFF;
00569                     io.power_save_mode(1);
00570                 }
00571                 break;
00572         }
00573     }
00574 }