Sweep a servo according to Proximity sensor measure
Dependencies: Servo X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1 by
Diff: main.cpp
- Revision:
- 32:724d2afb0ca2
- Parent:
- 31:1aaf6b8b066e
- Child:
- 33:b903a0ec8803
diff -r 1aaf6b8b066e -r 724d2afb0ca2 main.cpp --- a/main.cpp Tue Nov 10 10:55:34 2015 +0000 +++ b/main.cpp Thu Nov 12 11:55:28 2015 +0100 @@ -56,7 +56,7 @@ { char str[5]; - if(op_mode==range_continuous_interrupt) + if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling) { if(data_sensor_top.range_mm!=0xFFFFFFFF) { @@ -67,7 +67,7 @@ sprintf(str,"%s","----"); } } - else if(op_mode==als_continuous_interrupt) + else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling) { if(data_sensor_top.lux!=0xFFFFFFFF) { @@ -82,13 +82,29 @@ } /* On board red slider position check */ -OperatingMode CheckSlider(STMPE1600DigiIn measure_type) +enum OpModeIntPoll_t{ PollMeasure, IntMeasure }; + +OperatingMode CheckSlider(STMPE1600DigiIn measure_type, enum OpModeIntPoll_t OpMode) { - int measure=measure_type; - if(measure==RANGE) - return range_continuous_interrupt; - else if(measure==ALS) - return als_continuous_interrupt; +int measure=measure_type; + +OperatingMode ret; + switch (OpMode) { + case PollMeasure: + if(measure==RANGE) + ret = range_continuous_polling; + else if(measure==ALS) + ret = als_continuous_polling; + break; + + case IntMeasure: + if(measure==RANGE) + ret = range_continuous_interrupt; + else if(measure==ALS) + ret = als_continuous_interrupt; + break; + } + return ret; } /* Print on USB Serial the started OperatingMode */ @@ -111,7 +127,7 @@ /* Print on board 4 Digit display the app exiting BYE message */ #define DELAY 2000 // 2Sec -void Bye() +void DisplayMsg(const char * msg) { Timer timer; char str[5]; @@ -119,30 +135,29 @@ timer.start(); for(int i=0; i<DELAY; i=timer.read_ms()) { - sprintf(str,"%s","BYE"); + sprintf(str,"%s",msg); board->display->DisplayString(str, strlen(str)); } timer.stop(); } -int main() -{ - DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL); - board=X_NUCLEO_6180XA1::Instance(&device_i2c); - /* user button used to stop measurements in interrupt mode */ - InterruptIn stop_button(USER_BUTTON); + +void IntContinousALSorRangeMeasure (DevI2C device_i2c) { + // onboard red slider to switch Range/ALS measures STMPE1600DigiIn measure_type(device_i2c, GPIO_11); - int status; - + int status; //device_i2c.frequency(400000); //change i2c frequncy from 100kHz to 400kHz //pc.baud(115200); //change the printf baudrate if faster printf is needed - + + board=X_NUCLEO_6180XA1::Instance(&device_i2c); + DisplayMsg ("INT"); + status=board->InitBoard(); if(status) printf("Failed to init board!\n\r"); - stop_button.rise(&StopMeasureIRQ); - operating_mode=CheckSlider(measure_type); + + operating_mode=CheckSlider(measure_type, IntMeasure); status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); if(!status) { @@ -161,10 +176,11 @@ status=board->sensor_top->StopMeasurement(prev_operating_mode); if(!status) PrintStopMessage(prev_operating_mode); + int_stop_measure = false; printf("\nProgram stopped!\n\n\r"); break; } - operating_mode=CheckSlider(measure_type); + operating_mode=CheckSlider(measure_type, IntMeasure); if(operating_mode!=prev_operating_mode) { DisplayRefresh(prev_operating_mode); @@ -175,12 +191,80 @@ status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); if(!status) PrintStartMessage(operating_mode); + } else + DisplayRefresh(operating_mode); + } + } + DisplayMsg("BYE"); + delete board; +} + + +void PollContinousALSorRangeMeasure (DevI2C device_i2c) { + // onboard red slider to switch Range/ALS measures + STMPE1600DigiIn measure_type(device_i2c, GPIO_11); + int status; + + board=X_NUCLEO_6180XA1::Instance(&device_i2c); + DisplayMsg ("poll"); + status=board->InitBoard(); + if(status) + printf("Failed to init board!\n\r"); + operating_mode=CheckSlider(measure_type, PollMeasure); + status=board->sensor_top->StartMeasurement(operating_mode, NULL, NULL, NULL); + if(!status) + { + prev_operating_mode=operating_mode; + PrintStartMessage(operating_mode); + while(1) + { +// do // the loop waiting for the result could be avoided (if is acceptable to get not updated measures) +// { + status=board->sensor_top->GetMeasurement(operating_mode, &data_sensor_top); + DisplayRefresh(operating_mode); +// }while(status); + + if(int_stop_measure) + { + status=board->sensor_top->StopMeasurement(prev_operating_mode); + if(!status) + PrintStopMessage(prev_operating_mode); + int_stop_measure = false; + printf("\nProgram stopped!\n\n\r"); + break; + } + + operating_mode=CheckSlider(measure_type, PollMeasure); + if(operating_mode!=prev_operating_mode) + { + DisplayRefresh(prev_operating_mode); + status=board->sensor_top->StopMeasurement(prev_operating_mode); + if(!status) + PrintStopMessage(prev_operating_mode); + prev_operating_mode=operating_mode; + status=board->sensor_top->StartMeasurement(operating_mode, NULL, NULL, NULL); + if(!status) + PrintStartMessage(operating_mode); } else DisplayRefresh(operating_mode); - } + } } - Bye(); - delete board; + DisplayMsg("BYE"); + delete board; } + +int main() +{ + /* user button to stop measurements in progress */ + InterruptIn stop_button (USER_BUTTON); + stop_button.rise (&StopMeasureIRQ); + DevI2C device_i2c (VL6180X_I2C_SDA, VL6180X_I2C_SCL); + + IntContinousALSorRangeMeasure (device_i2c); // start Interrupt continous measures + + PollContinousALSorRangeMeasure (device_i2c); // start Polling continous measures + +} +