GAP based TOF Demo

Dependencies:   BLE_API X_NUCLEO_6180XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Mon Aug 20 16:42:06 2018 +0000
Revision:
29:0f068d70c1a5
Parent:
28:86a594313434
made in Shanghai

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 28:86a594313434 1 // xn6180.cpp - X-NUCLEO 6180XA1 expansion board functionality
hux 28:86a594313434 2 //
hux 28:86a594313434 3 // This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
hux 28:86a594313434 4 // on the onboard embedded top sensor.
hux 28:86a594313434 5 // The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the
hux 28:86a594313434 6 // on board 4digits display.
hux 28:86a594313434 7 //
hux 28:86a594313434 8 // User Blue button allows to stop current measurement and the entire program releasing all the resources.
hux 28:86a594313434 9 // Reset button is used to restart the program.
hux 28:86a594313434 10 //
hux 28:86a594313434 11 // Polling operating modes don`t require callback function that handles IRQ
hux 28:86a594313434 12 // Callback IRQ functions are used only for measure that require interrupt
hux 28:86a594313434 13 //
hux 28:86a594313434 14 // GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
hux 28:86a594313434 15 // is not ready to be read from the corresponding register. So you need to wait
hux 28:86a594313434 16 // for the result to be ready
hux 28:86a594313434 17 //
hux 28:86a594313434 18
hux 28:86a594313434 19 #include "mbed.h"
hux 28:86a594313434 20 #include "shields/xn6180.h"
hux 28:86a594313434 21
hux 28:86a594313434 22 #define VL6180X_I2C_SDA D14
hux 28:86a594313434 23 #define VL6180X_I2C_SCL D15
hux 28:86a594313434 24
hux 28:86a594313434 25 #define RANGE 0
hux 28:86a594313434 26 #define ALS 1
hux 28:86a594313434 27
hux 28:86a594313434 28 #define DELAY 2000 // 2Sec
hux 28:86a594313434 29
hux 28:86a594313434 30 // static DisplayShield *pShield = 0; // used by IRQ callback to access data
hux 28:86a594313434 31
hux 28:86a594313434 32 void DisplayShield::init(const char *msg, int msec)
hux 28:86a594313434 33 {
hux 28:86a594313434 34 if (msg)
hux 28:86a594313434 35 display(msg,msec); // display "INI" for 1 second
hux 28:86a594313434 36
hux 28:86a594313434 37 // now init the 6180XA1 expansion board with default values
hux 28:86a594313434 38
hux 28:86a594313434 39 int err = pBoard->InitBoard();
hux 28:86a594313434 40 if (err)
hux 28:86a594313434 41 printf("Failed to init board!\n\r");
hux 28:86a594313434 42
hux 28:86a594313434 43 // read the red slider position for ALS/Range measure
hux 28:86a594313434 44
hux 28:86a594313434 45 slider(IntMeasure); // update curMode due to slider position
hux 28:86a594313434 46 clear(); // clear service handling request flag
hux 28:86a594313434 47 }
hux 28:86a594313434 48
hux 28:86a594313434 49
hux 28:86a594313434 50 DisplayShield::DisplayShield()
hux 28:86a594313434 51 {
hux 28:86a594313434 52 // first we need an I2C device
hux 28:86a594313434 53
hux 28:86a594313434 54 pDevice = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
hux 28:86a594313434 55
hux 28:86a594313434 56 // next we create the 6180XA1 expansion board singleton obj
hux 28:86a594313434 57 // after that we are already able to display the init message
hux 28:86a594313434 58
hux 28:86a594313434 59 pBoard = X_NUCLEO_6180XA1::Instance(pDevice, A3, A2, D13, D2);
hux 28:86a594313434 60
hux 28:86a594313434 61 init(); // initialize shield
hux 28:86a594313434 62 }
hux 28:86a594313434 63
hux 28:86a594313434 64 //==============================================================================
hux 28:86a594313434 65 // Displaying Messages or Values
hux 28:86a594313434 66 //==============================================================================
hux 28:86a594313434 67
hux 28:86a594313434 68
hux 28:86a594313434 69 void DisplayShield::display(const char * msg) // display max 4 digits
hux 28:86a594313434 70 {
hux 28:86a594313434 71 char buf[5];
hux 28:86a594313434 72 int nmax = sizeof(buf) - 1;
hux 28:86a594313434 73 int len = strlen(msg);
hux 28:86a594313434 74
hux 28:86a594313434 75 memset(buf,0,sizeof(buf)); // clear buffer, provide terminator
hux 28:86a594313434 76 for (int j=0; j < nmax; j++)
hux 28:86a594313434 77 { if (msg[j] == 0)
hux 28:86a594313434 78 break;
hux 28:86a594313434 79 buf[j] = msg[j];
hux 28:86a594313434 80 }
hux 28:86a594313434 81
hux 28:86a594313434 82 pBoard->display->DisplayString(buf, strlen(buf));
hux 28:86a594313434 83 }
hux 28:86a594313434 84
hux 28:86a594313434 85
hux 28:86a594313434 86 void DisplayShield::display(const char * msg, int msec) // display & wait
hux 28:86a594313434 87 {
hux 28:86a594313434 88 Timer timer;
hux 28:86a594313434 89
hux 28:86a594313434 90 timer.start();
hux 28:86a594313434 91 for(int i=0; i < msec; i = timer.read_ms())
hux 28:86a594313434 92 display(msg);
hux 28:86a594313434 93 timer.stop();
hux 28:86a594313434 94 }
hux 28:86a594313434 95
hux 28:86a594313434 96
hux 28:86a594313434 97 // On board 4 digit local display refresh
hux 28:86a594313434 98
hux 28:86a594313434 99 void DisplayShield::refresh(OpMode mode)
hux 28:86a594313434 100 {
hux 28:86a594313434 101 char str[5];
hux 28:86a594313434 102
hux 28:86a594313434 103 if(mode == range_continuous_interrupt || mode == range_continuous_polling)
hux 28:86a594313434 104 {
hux 28:86a594313434 105 if (data.range_mm != 0xFFFFFFFF)
hux 28:86a594313434 106 {
hux 28:86a594313434 107 sprintf(str,"%d",data.range_mm);
hux 28:86a594313434 108 }
hux 28:86a594313434 109 else
hux 28:86a594313434 110 {
hux 28:86a594313434 111 sprintf(str,"%s","----");
hux 28:86a594313434 112 }
hux 28:86a594313434 113 }
hux 28:86a594313434 114 else if(mode == als_continuous_interrupt || mode == als_continuous_polling)
hux 28:86a594313434 115 {
hux 28:86a594313434 116 if(data.lux != 0xFFFFFFFF)
hux 28:86a594313434 117 {
hux 28:86a594313434 118 sprintf(str,"%d",data.lux);
hux 28:86a594313434 119 }
hux 28:86a594313434 120 else
hux 28:86a594313434 121 {
hux 28:86a594313434 122 sprintf(str,"%s","----");
hux 28:86a594313434 123 }
hux 28:86a594313434 124 }
hux 28:86a594313434 125 pBoard->display->DisplayString(str, strlen(str));
hux 28:86a594313434 126 }
hux 28:86a594313434 127
hux 28:86a594313434 128 void DisplayShield::refresh() // refresh display in current mode
hux 28:86a594313434 129 {
hux 28:86a594313434 130 refresh(curMode);
hux 28:86a594313434 131 }
hux 28:86a594313434 132
hux 28:86a594313434 133
hux 28:86a594313434 134 //==============================================================================
hux 28:86a594313434 135 // IRQ Functionality
hux 28:86a594313434 136 //==============================================================================
hux 28:86a594313434 137
hux 28:86a594313434 138
hux 28:86a594313434 139 void DisplayShield::set() // set data read service request
hux 28:86a594313434 140 {
hux 28:86a594313434 141 flagService = true;
hux 28:86a594313434 142 }
hux 28:86a594313434 143
hux 28:86a594313434 144 void DisplayShield::clear() // clear data read service request
hux 28:86a594313434 145 {
hux 28:86a594313434 146 flagService = false;
hux 28:86a594313434 147 }
hux 28:86a594313434 148
hux 28:86a594313434 149 bool DisplayShield::request() // return data read service request
hux 28:86a594313434 150 {
hux 28:86a594313434 151 return flagService;
hux 28:86a594313434 152 }
hux 28:86a594313434 153 /*
hux 28:86a594313434 154 static void cbDone(void) // measurement done callback
hux 28:86a594313434 155 {
hux 28:86a594313434 156 if (pShield)
hux 28:86a594313434 157 {
hux 28:86a594313434 158 pShield->set(); // set service request
hux 28:86a594313434 159 pShield->disable(); // disable further interrupts
hux 28:86a594313434 160 pShield = 0; // free-up shield pointer, no more in use
hux 28:86a594313434 161 }
hux 28:86a594313434 162 }
hux 28:86a594313434 163 */
hux 28:86a594313434 164
hux 28:86a594313434 165 int DisplayShield::handle() // handle ISR and read the measurement
hux 28:86a594313434 166 {
hux 28:86a594313434 167 clear(); // clear data read service request
hux 28:86a594313434 168 return pBoard->sensor_top->HandleIRQ(curMode, &data);
hux 28:86a594313434 169 }
hux 28:86a594313434 170
hux 28:86a594313434 171
hux 28:86a594313434 172 void DisplayShield::disable() // disable interrupt measure detection
hux 28:86a594313434 173 {
hux 28:86a594313434 174 pBoard->sensor_top->DisableInterruptMeasureDetectionIRQ();
hux 28:86a594313434 175 }
hux 28:86a594313434 176
hux 28:86a594313434 177
hux 28:86a594313434 178 void DisplayShield::ready() // set data ready status
hux 28:86a594313434 179 {
hux 28:86a594313434 180 disable(); // disable interrupts
hux 28:86a594313434 181 set(); // set data service request flag
hux 28:86a594313434 182 }
hux 28:86a594313434 183
hux 28:86a594313434 184
hux 28:86a594313434 185 int DisplayShield::start(void (*callback)(void))
hux 28:86a594313434 186 {
hux 28:86a594313434 187 int err; // error code
hux 28:86a594313434 188
hux 28:86a594313434 189 // if (pShield != 0)
hux 28:86a594313434 190 // return 0x99; // pShield is already booked!
hux 28:86a594313434 191 //
hux 28:86a594313434 192 // pShield = this; // make access available to cbDone()
hux 28:86a594313434 193 err = pBoard->sensor_top->StartMeasurement(curMode, callback, NULL, NULL);
hux 28:86a594313434 194
hux 28:86a594313434 195 if (err == 0) // if no errors occured
hux 28:86a594313434 196 {
hux 28:86a594313434 197 prvMode = curMode;
hux 28:86a594313434 198 startMessage(); // then report about successful start
hux 28:86a594313434 199 }
hux 28:86a594313434 200
hux 28:86a594313434 201 return err;
hux 28:86a594313434 202 }
hux 28:86a594313434 203
hux 28:86a594313434 204
hux 28:86a594313434 205 int DisplayShield::stop()
hux 28:86a594313434 206 {
hux 28:86a594313434 207 int err; // error code
hux 28:86a594313434 208
hux 28:86a594313434 209 // pShield = 0; // free-up pShield pointer
hux 28:86a594313434 210 err = pBoard->sensor_top->StopMeasurement(prvMode); // stop measurement
hux 28:86a594313434 211
hux 28:86a594313434 212 if (err == 0) // if no errors occured
hux 28:86a594313434 213 stopMessage(); // then report about successful stop
hux 28:86a594313434 214
hux 28:86a594313434 215 return err; // return error code
hux 28:86a594313434 216 }
hux 28:86a594313434 217
hux 28:86a594313434 218
hux 28:86a594313434 219 //==============================================================================
hux 28:86a594313434 220 // Examine Red Slider Position
hux 28:86a594313434 221 //==============================================================================
hux 28:86a594313434 222
hux 28:86a594313434 223 bool DisplayShield::slider(enum OpModeIntPoll_t mode)
hux 28:86a594313434 224 {
hux 28:86a594313434 225 int measure= pBoard->RdSwitch();
hux 28:86a594313434 226
hux 28:86a594313434 227 switch (mode)
hux 28:86a594313434 228 {
hux 28:86a594313434 229 case PollMeasure:
hux 28:86a594313434 230 if (measure == RANGE)
hux 28:86a594313434 231 curMode = range_continuous_polling;
hux 28:86a594313434 232 else if(measure==ALS)
hux 28:86a594313434 233 curMode = als_continuous_polling;
hux 28:86a594313434 234 break;
hux 28:86a594313434 235
hux 28:86a594313434 236 case IntMeasure:
hux 28:86a594313434 237 if(measure == RANGE)
hux 28:86a594313434 238 curMode = range_continuous_interrupt;
hux 28:86a594313434 239 else if(measure == ALS)
hux 28:86a594313434 240 curMode = als_continuous_interrupt;
hux 28:86a594313434 241 break;
hux 28:86a594313434 242 }
hux 28:86a594313434 243
hux 28:86a594313434 244 return (curMode != prvMode); // slider position changed?
hux 28:86a594313434 245 }
hux 28:86a594313434 246
hux 28:86a594313434 247 bool DisplayShield::setup(OpMode mode)
hux 28:86a594313434 248 {
hux 28:86a594313434 249 switch (mode)
hux 28:86a594313434 250 {
hux 28:86a594313434 251 case range_continuous_polling:
hux 28:86a594313434 252 case als_continuous_polling:
hux 28:86a594313434 253 case range_continuous_interrupt:
hux 28:86a594313434 254 case als_continuous_interrupt:
hux 28:86a594313434 255 curMode = mode;
hux 28:86a594313434 256 prvMode = mode;
hux 28:86a594313434 257 return 0; // no errors
hux 28:86a594313434 258 }
hux 28:86a594313434 259 return 1; // return with error code 1
hux 28:86a594313434 260 }
hux 28:86a594313434 261
hux 28:86a594313434 262
hux 28:86a594313434 263 //==============================================================================
hux 28:86a594313434 264 // Trace Message Printing
hux 28:86a594313434 265 //==============================================================================
hux 28:86a594313434 266
hux 28:86a594313434 267 void DisplayShield::startMessage(OpMode mode)
hux 28:86a594313434 268 {
hux 28:86a594313434 269 if (mode == range_continuous_interrupt)
hux 28:86a594313434 270 printf("\nStarted range continuous interrupt measure\n\r");
hux 28:86a594313434 271 else if(prvMode == als_continuous_interrupt)
hux 28:86a594313434 272 printf("\nStarted als continuous interrupt measure\n\r");
hux 28:86a594313434 273 }
hux 28:86a594313434 274
hux 28:86a594313434 275 void DisplayShield::startMessage()
hux 28:86a594313434 276 {
hux 28:86a594313434 277 startMessage(curMode);
hux 28:86a594313434 278 }
hux 28:86a594313434 279
hux 28:86a594313434 280 void DisplayShield::stopMessage(OpMode mode)
hux 28:86a594313434 281 {
hux 28:86a594313434 282 if (mode == range_continuous_interrupt)
hux 28:86a594313434 283 printf("Stopped range continuous interrupt measure\n\r");
hux 28:86a594313434 284 else if(prvMode == als_continuous_interrupt)
hux 28:86a594313434 285 printf("Stopped als continuous interrupt measure\n\r");
hux 28:86a594313434 286 }
hux 28:86a594313434 287
hux 28:86a594313434 288 void DisplayShield::stopMessage()
hux 28:86a594313434 289 {
hux 28:86a594313434 290 stopMessage(prvMode);
hux 28:86a594313434 291 }
hux 28:86a594313434 292
hux 28:86a594313434 293 // eof