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 X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp
00001 // S16_Blue_ToF - Blue button and Time-of-Flight Sensor Service over GATT 00002 00003 #include "bricks/bricks.h" 00004 #include "shields/shields.h" 00005 00006 typedef DisplayShield Dish; 00007 00008 static Dish *pShield; 00009 Blinker blink; 00010 00011 //============================================================================== 00012 // GATT Database Setup 00013 //============================================================================== 00014 00015 // Detection Service 00016 00017 Service detection(0xA010,"Detection"); // Detection Service 00018 Characteristic<Bool> chrPresence(detection,0xA011, "n", "Presence"); 00019 00020 // Debug Service 00021 00022 Service debug(0xA030,"Debug"); // Debug Service 00023 Characteristic<Bool> chrTest (debug, 0xA031, "w", "Test"); 00024 00025 static void cbWritten(Blob &o) // handle updates 00026 { 00027 Bool value; 00028 00029 if (updated(o,chrTest)) // has chrTest been updated? 00030 { 00031 get(o,chrTest,value); // get value of chrTest 00032 set(o,chrPresence,value); // and store this value to chrPresence 00033 } 00034 } 00035 00036 void services(Blob &o) 00037 { 00038 enroll(o,detection); // enroll detection service 00039 enroll(o,debug); // enroll debug service 00040 00041 onWritten(o,cbWritten); // setup 'data written' callback 00042 } 00043 00044 //============================================================================== 00045 // Button Functionality 00046 //============================================================================== 00047 00048 typedef enum state { OFF, ON, IDLE } State; 00049 00050 InterruptIn button(USER_BUTTON); // declare blue user button 00051 State state = IDLE; 00052 00053 static void cbRise(void) 00054 { 00055 Blob o; // declare a blob (BLE OBject) 00056 Bool value = 0; 00057 00058 if (o.hasInitialized()) 00059 { 00060 blink.off();; 00061 state = OFF; 00062 set(o,chrPresence,value); // and store this value to chrPresence 00063 } 00064 } 00065 00066 static void cbFall(void) 00067 { 00068 Blob o; // declare a blob (BLE OBject) 00069 Bool value = 1; 00070 00071 if (o.hasInitialized()) 00072 { 00073 blink.on(); 00074 state = ON; 00075 set(o,chrPresence,value); // and store this value to chrPresence 00076 } 00077 } 00078 00079 void setupButton() // enroll button functionality 00080 { 00081 button.rise(&cbRise); // interrupt callback setup 00082 button.fall(&cbFall); // interrupt callback setup 00083 } 00084 00085 //============================================================================== 00086 // Setup BLE Functionality 00087 //============================================================================== 00088 00089 void cbError(Blob &o) // Error Reporting Callback 00090 { 00091 blink.error(); // 'error' blink sequence 00092 } 00093 00094 void cbConnect(Blob &o) // Connection Callback 00095 { 00096 blink.connected(); // 'error' blink sequence 00097 } 00098 00099 void cbDisconnect(Blob &o) // Disconnection Callback 00100 { 00101 advertise(o); // start advertising on client disconnect 00102 blink.advertise(); // 'advertise' blink sequence 00103 } 00104 00105 void cbInitComplete(Blob &o) // Immediately After Initializing BLE 00106 { 00107 services(o); // enroll all services & setup callbacks 00108 00109 onConnect(o,cbConnect); // setup connection callback 00110 onDisconnect(o,cbDisconnect); // setup disconnection callback 00111 00112 device(o,"S16#2.0 Blue TOF"); 00113 name(o,"Blue TOF"); 00114 00115 advertise(o,"C:ng",100); // start advertising @ 100 msec interval 00116 blink.advertise(); // 'advertise' blink sequence 00117 } 00118 00119 //============================================================================== 00120 // Simple Loop - Cannot Change Mode 00121 //============================================================================== 00122 00123 void distanceMeasurement(Dish &d) 00124 { 00125 uint32_t dist; 00126 00127 pShield->pBoard->sensor_top->GetDistance(&dist); 00128 00129 if (dist == 0xFF) 00130 dist = 0xFFFFFFFF; 00131 00132 d.data.range_mm = dist; 00133 } 00134 00135 00136 void refreshDisplay(Dish &d, int msec) 00137 { 00138 Timer timer; 00139 00140 timer.start(); 00141 for (int i=0; i < msec; i = timer.read_ms()) 00142 d.refresh(); // refresh display 00143 timer.stop(); 00144 } 00145 00146 00147 void loop(Blob &o, Dish &d) 00148 { 00149 static const char *off = " "; 00150 Bool state = false; 00151 00152 for (;;) 00153 { 00154 distanceMeasurement(d); 00155 if (d.data.range_mm != 0xFFFFFFFF) 00156 { if (state == false) 00157 { blink.on(); // switch LED on 00158 state = true; 00159 set(o,chrPresence,state); 00160 } 00161 } 00162 else 00163 { if (state == true) 00164 { if (isConnected(o)) 00165 blink.connected(off); // switch LED off 00166 else 00167 blink.advertise(off); // switch LED off 00168 state = false; 00169 set(o,chrPresence,state); 00170 } 00171 } 00172 00173 if (d.red()) // red slider switched to 'RANGE'? 00174 refreshDisplay(d,250); 00175 sleep(o); // 'sleep(o,10)' causes connect issues 00176 } 00177 } 00178 00179 //============================================================================== 00180 // Main Program 00181 //============================================================================== 00182 00183 int main(void) 00184 { 00185 Blob o; 00186 verbose(o); // enable all trace messages 00187 trace(o,1,"\nS16_Blue_ToF\n"); 00188 blink.idle(); 00189 00190 pShield = new Dish("Init"); // init shield 00191 pShield->setup(InterruptTOF); // setup fot ToF measurement only 00192 00193 init(o,cbInitComplete); // init BLE system 00194 setupButton(); // enroll button functionality 00195 00196 loop(o,*pShield); // run measurement loop 00197 } 00198
Generated on Sat Jul 16 2022 07:45:39 by
1.7.2
