Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API LIS3DH mbed nRF51822 BMC050 nRF51_LowPwr nRF51_Vdd
Fork of BLE_EddystoneBeacon_Service by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /* 00018 * /////// Works well on Switch Science mbed TY51822r3 /////// 00019 * Modified by Kenji Arai 00020 * http://www.page.sannet.ne.jp/kenjia/index.html 00021 * http://mbed.org/users/kenjiArai/ 00022 * 00023 * Started: Feburary 1st, 2016 00024 * Revised: June 8th, 2016 00025 * 00026 * Original: 00027 * nRF51822_SimpleControls 00028 * https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_EddystoneBeacon_Service/ 00029 * Tested Controller Device: 00030 * iPhone6 Physical Web (PhyWeb) By Viet Hoa Dinh 00031 * https://itunes.apple.com/us/app/physical-web/id927653608?mt=8 00032 */ 00033 00034 /* 00035 * STEP1 00036 * If you just got the board, plese set following difinition and not connect all of pins. 00037 #define USE_ACC 0 00038 #define CHK_ACC 0 00039 #define USE_DEVICE_SERIAL 0 // in nRF51_disable_peripheral.h 00040 #define ACC_DEVIC x // No effect 00041 * STEP2 00042 * If you equiped the Acc sensor, please check ACC data using folloing definition. 00043 #define USE_ACC 1 00044 #define CHK_ACC 1 00045 #define USE_DEVICE_SERIAL 1 // in nRF51_disable_peripheral.h 00046 #define ACC_DEVIC 0 or 1 // depends on your sensor device 00047 * STEP3 00048 * This is a final setup without no debug information. 00049 #define USE_ACC 1 00050 #define CHK_ACC 0 00051 #define USE_DEVICE_SERIAL 0 // in nRF51_disable_peripheral.h 00052 #define ACC_DEVIC 0 or 1 // depends on your sensor device 00053 * 00054 */ 00055 00056 #define ACC_DEVIC 0 // 0=LIS3DH, 1=BMC050 00057 00058 // Include --------------------------------------------------------------------------------------- 00059 #include "mbed.h" 00060 #include "BLE.h" 00061 #include "EddystoneService.h" 00062 #if ACC_DEVIC 00063 #include "BMC050.h" 00064 #else 00065 #include "LIS3DH.h" 00066 #endif // ACC_DEVIC 00067 #include "nRF51_Vdd.h" 00068 #include "nRF51_lowpwr.h" 00069 00070 // Definition ------------------------------------------------------------------------------------ 00071 #define USE_ACC 0 // if you equipped ACC sensor, please set 1 00072 #define CHK_ACC 0 // Ckeck ACC device itself 00073 00074 #define POWER_LEVEL 0 // 0,1,2 00075 00076 // Before using this function, please specify your program are used following functions or not. 00077 #define USE_DEVICE_STDIO_MESSAGES 0 // printf 00078 #define USE_DEVICE_SERIAL 0 // Serial or DEBUG & etc. 00079 #define USE_DEVICE_I2C 0 // Sensors with I2C, LCD, EEPROM, Driver chips & etc. 00080 #define USE_DEVICE_SPI 0 // Sensors with SOI, LCD, EEPROM, Driver chips & etc. 00081 #define USE_DEVICE_SPISLAVE 0 // Communication with master vis SPI 00082 #define USE_DEVICE_PWMOUT 0 // PWM duty output, Serve & etc. 00083 #define USE_DEVICE_ANALOGIN 0 // Analog adc 00084 00085 #if USE_DEVICE_STDIO_MESSAGES 00086 #define DEBUG(...) { printf(__VA_ARGS__); } 00087 #else 00088 #define DEBUG(...) 00089 #endif 00090 00091 #if USE_DEVICE_SERIAL 00092 #define BAUD(x) pc.baud(x) 00093 #define GETC(x) pc.getc(x) 00094 #define PUTC(x) pc.putc(x) 00095 #define PRINTF(...) { pc.printf(__VA_ARGS__); } 00096 #define READABLE(x) pc.readable(x) 00097 #define ATTACH(x,y) pc.attach(x, y); 00098 #else 00099 #define BAUD(x) 00100 #define GETC(x) 'c' 00101 #define PUTC(x) 00102 #define PRINTF(...) 00103 #define READABLE(x) 00104 #define ATTACH(x,y) 00105 #endif 00106 00107 // RAM ------------------------------------------------------------------------------------------- 00108 EddystoneService *eddyServicePtr; 00109 Gap::Address_t my_mac; 00110 00111 // ROM / Constant data --------------------------------------------------------------------------- 00112 #if (USE_ACC == 1) && (ACC_DEVIC == 1) 00113 const BMC050ACC_TypeDef acc_parameter = { 00114 BMC050_A_G_CHIP_ADDR, // I2C Address 00115 BMC050_FS_2G, // G-range slection 00116 BMC050_BW_250, // Bandwidth 00117 }; 00118 00119 const BMC050MAG_TypeDef mag_parameter = { 00120 BMC050_MAG_NOT_USED_ADDR,// Not use mag sensor 00121 BMC050_DUMMY // dummy 00122 }; 00123 #endif 00124 00125 const nRF51_LOWPWR_TypeDef lowpwr_table = 00126 { 00127 #if USE_DEVICE_STDIO_MESSAGES 00128 true, 00129 #else 00130 false, 00131 #endif 00132 #if USE_DEVICE_SERIAL 00133 true, 00134 #else 00135 false, 00136 #endif 00137 #if USE_DEVICE_I2C 00138 true, 00139 #else 00140 false, 00141 #endif 00142 #if USE_DEVICE_SPI 00143 true, 00144 #else 00145 false, 00146 #endif 00147 #if USE_DEVICE_SPISLAVE 00148 true, 00149 #else 00150 false, 00151 #endif 00152 #if USE_DEVICE_PWMOUT 00153 true, 00154 #else 00155 false, 00156 #endif 00157 #if USE_DEVICE_ANALOGIN 00158 true 00159 #else 00160 false 00161 #endif 00162 }; 00163 00164 // Function prototypes --------------------------------------------------------------------------- 00165 int8_t check_dice(void); 00166 uint16_t update_vdd(uint16_t x); 00167 uint16_t update_temp(uint16_t x); 00168 void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext); 00169 void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext); 00170 00171 // Object ---------------------------------------------------------------------------------------- 00172 DigitalOut CHG_LED(LED1); 00173 #if USE_DEVICE_SERIAL 00174 Serial pc(USBTX, USBRX); 00175 #endif // USE_DEVICE_SERIAL 00176 nRF51_Vdd vdd(3.6f, 1.8f, ONLY4VDD); 00177 #if USE_ACC == 1 00178 I2C i2c(P0_3, P0_4); // SDA, SCL 00179 #if ACC_DEVIC 00180 BMC050 acc(i2c, &acc_parameter, &mag_parameter); 00181 #else 00182 LIS3DH acc(i2c,LIS3DH_G_CHIP_ADDR,LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); 00183 #endif // ACC_DEVIC 00184 #endif // USE_ACC == 1 00185 00186 //------------------------------------------------------------------------------------------------- 00187 // Control Program 00188 //------------------------------------------------------------------------------------------------- 00189 void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext){ 00190 BLE &ble = initContext->ble; 00191 ble_error_t error = initContext->error; 00192 00193 if (error != BLE_ERROR_NONE) { 00194 onBleInitError(initContext); 00195 return; 00196 } 00197 // Set UID and TLM frame data 00198 const UIDNamespaceID_t uidNamespaceID = 00199 {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; 00200 const UIDInstanceID_t uidInstanceID = 00201 {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; 00202 uint8_t tlmVersion = 0x00; 00203 00204 Gap::AddressType_t my_mac_type; 00205 ble.gap().getAddress(&my_mac_type, my_mac); 00206 PRINTF( 00207 " my_MAC %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n", 00208 my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0], 00209 (my_mac_type == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random" 00210 ); 00211 PRINTF( 00212 " mac_board_? = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x};\r\n", 00213 my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5] 00214 ); 00215 // Initialize a EddystoneBeaconConfig service 00216 // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m 00217 #if POWER_LEVEL == 0 00218 static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; 00219 #elif POWER_LEVEL == 1 00220 static const PowerLevels_t defaultAdvPowerLevels = {-87, -73, -61, -53}; 00221 #else 00222 static const PowerLevels_t defaultAdvPowerLevels = {-107, -93, -81, -73}; 00223 #endif // POWER_LEVEL 00224 // Values for radio power levels, provided by manufacturer. 00225 #if POWER_LEVEL == 0 00226 static const PowerLevels_t radioPowerLevels = {-30, -16, -4, 4}; 00227 #elif POWER_LEVEL == 1 00228 static const PowerLevels_t radioPowerLevels = {-60, -46, -34, -26}; 00229 #else 00230 static const PowerLevels_t radioPowerLevels = {-90, -76, -64, -56}; 00231 #endif // POWER_LEVEL 00232 eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels); 00233 // created short cut web addres by http://bitly.oshiire.org/ 00234 00235 switch (check_dice()){ 00236 case 1: 00237 eddyServicePtr->setURLData("http://bit.ly/1oJh91B"); // Switch sience(mbed) 00238 break; 00239 case 2: 00240 eddyServicePtr->setURLData("http://bit.ly/1oJhP7g"); // switch sience(HP) 00241 break; 00242 case 3: 00243 eddyServicePtr->setURLData("http://bit.ly/1VvuCVr"); // Taiyo Yuden BLE 00244 break; 00245 case 4: 00246 eddyServicePtr->setURLData("http://bit.ly/1Vvtp0l"); // Taiyo Yuden 00247 break; 00248 case 5: 00249 eddyServicePtr->setURLData("http://bit.ly/1Vvt51J"); // JH1PJL(mbed) 00250 break; 00251 case 6: 00252 eddyServicePtr->setURLData("http://bit.ly/1VvteT0"); // JH1PJL(HP) 00253 break; 00254 case 0: 00255 default: 00256 eddyServicePtr->setURLData("http://mbed.org"); 00257 break; 00258 } 00259 eddyServicePtr->setUIDData(&uidNamespaceID, &uidInstanceID); 00260 eddyServicePtr->setTLMData(tlmVersion); 00261 eddyServicePtr->onTLMBatteryVoltageUpdate(&update_vdd); 00262 eddyServicePtr->onTLMBeaconTemperatureUpdate(&update_temp); 00263 eddyServicePtr->startBeaconService(5, 5, 5); // Start Eddystone in config mode 00264 } 00265 00266 #if (CHK_ACC == 0) 00267 int main(void){ 00268 uint8_t old_dice; 00269 00270 CHG_LED = 1; 00271 LowPwr set_lowpwr(&lowpwr_table); 00272 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00273 ble.init(bleInitComplete); 00274 while (ble.hasInitialized() == false){;} 00275 old_dice = check_dice(); // set initial value 00276 wait(0.5); 00277 CHG_LED = 0; 00278 while (true) { 00279 ble.waitForEvent(); 00280 if (old_dice != check_dice()){ 00281 SCB->AIRCR = 0x05fa0004; // System RESET!![ 00282 // Not come here (Just in case) 00283 deepsleep(); 00284 while(true){ wait(100);} 00285 } 00286 } 00287 } 00288 #else // (CHK_ACC == 0) 00289 #if (USE_ACC == 0) 00290 #error "Please set USE_ACC = 1) 00291 #else // (USE_ACC == 0) 00292 int main(void){ 00293 float fa[3]; 00294 00295 #if (ACC_DEVIC == 1) 00296 while (true){ 00297 if (acc.read_id_acc() == I_AM_BMC050_ACC){ 00298 PRINTF("I'm BMC050\r\n"); 00299 acc.read_data_acc(fa); 00300 break; 00301 } else { 00302 PRINTF("I'm NOT BMC050\r\n"); 00303 } 00304 wait(1.0); 00305 } 00306 #endif // (ACC_DEVIC == 1) 00307 LowPwr set_lowpwr(&lowpwr_table); 00308 while (true) { 00309 #if ACC_DEVIC 00310 acc.read_data_acc(fa); 00311 #else // ACC_DEVIC 00312 acc.read_data(fa); 00313 #endif // ACC_DEVIC 00314 PRINTF("acc:x=%+4.2f,y=%+4.2f,z=%+4.2f\r\n",fa[0],fa[1],fa[2]); 00315 wait(0.5); 00316 } 00317 } 00318 #endif // (USE_ACC == 0) 00319 #endif // (CHK_ACC == 0) 00320 00321 #if (USE_ACC == 1) 00322 int8_t check_dice(void){ 00323 float fa[3]; // Acc 0:X, 1:Y, 2:Z 00324 00325 #if ACC_DEVIC 00326 acc.read_data_acc(fa); 00327 #else // ACC_DEVIC 00328 acc.read_data(fa); 00329 #endif // ACC_DEVIC 00330 //PRINTF("acc:%4.3f\r\n", fa[0]); 00331 PRINTF("acc:x=%+4.2f,y=%+4.2f,z=%+4.2f\r\n",fa[0],fa[1],fa[2]); 00332 if (fa[0] > 6.0f){ return 2;} 00333 if (fa[0] < -6.0f){ return 5;} 00334 if (fa[1] > 6.0f){ return 4;} 00335 if (fa[1] < -6.0f){ return 3;} 00336 if (fa[2] > 6.0f){ return 1;} 00337 if (fa[2] < -6.0f){ return 6;} 00338 return 0; 00339 } 00340 #else // (USE_ACC == 1) 00341 int8_t check_dice(void){ 00342 return 1; 00343 } 00344 #endif // (USE_ACC == 1) 00345 00346 void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext){ 00347 // Initialization error handling goes here... 00348 (void) initContext; 00349 } 00350 00351 // Update Vdd data 00352 uint16_t update_vdd(uint16_t x){ 00353 #if USE_DEVICE_SERIAL 00354 float v; 00355 00356 v = vdd.read_real_value(); 00357 PRINTF("Vdd:%f[V]\r\n", v); 00358 return (uint16_t)(v * 1000); 00359 #else //USE_DEVICE_SERIAL 00360 return (uint16_t)(vdd.read_real_value() * 1000); 00361 #endif // USE_DEVICE_SERIAL 00362 } 00363 00364 // Update Temperature data 00365 uint16_t update_temp(uint16_t x){ 00366 float t; 00367 uint16_t temp; 00368 int32_t p_temp; 00369 00370 NRF_TEMP->TASKS_START = 1; 00371 while (NRF_TEMP->EVENTS_DATARDY == 0){;} 00372 NRF_TEMP->EVENTS_DATARDY = 0; 00373 if ((NRF_TEMP->TEMP & 0x00000200) != 0){ 00374 p_temp = (NRF_TEMP->TEMP | 0xFFFFFC00); 00375 } else { 00376 p_temp = NRF_TEMP->TEMP; 00377 } 00378 NRF_TEMP->TASKS_STOP = 1; /** Stop the temperature measurement. */ 00379 t = float(p_temp) / 4; // Original = float(p_temp)/4.0f - 16.0f; 00380 #if USE_DEVICE_SERIAL 00381 PRINTF("Chip temp: %+4.1f[degC]\r\n", t); 00382 #endif // USE_DEVICE_SERIAL 00383 if ( t >= 0){ 00384 temp = (uint16_t)(t * 256); 00385 } else { 00386 temp = (uint16_t)(t * -256); 00387 temp = 0x10000 - temp; 00388 } 00389 return temp; 00390 }
Generated on Wed Jul 13 2022 04:37:27 by
