![](/media/cache/group/MFR16-new.png.50x50_q85.jpg)
Sweep a servo according to Proximity sensor measure
Dependencies: Servo X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1 by
Diff: main.cpp
- Revision:
- 30:c8efec21544f
- Parent:
- 27:2afb9baf718f
- Child:
- 31:1aaf6b8b066e
--- a/main.cpp Fri Nov 06 12:47:24 2015 +0000 +++ b/main.cpp Mon Nov 09 16:53:24 2015 +0000 @@ -5,10 +5,12 @@ #include <stdio.h> #include <assert.h> -/* This test application performs a range measurement and an als measurement. - Display select slider allow to switch the measurement type; previuos - measurement is stopped and a new measurement is started. - User button allows to stop current measurement and the entire program. +/* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode + on the onboard embedded top sensor. + The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the + on bord 4digits display. + + User Blue button allows to stop current measurement and the entire program releasing all the resources. Reset button is used to restart the program. */ /* Polling operating modes don`t require callback function that handles IRQ @@ -34,21 +36,22 @@ OperatingMode operating_mode, prev_operating_mode; /* flags that handle interrupt request */ -bool flag_sensor_top=false, flag_stop_measure=false; +bool int_sensor_top=false, int_stop_measure=false; -/* callback function of the sensor_top */ +/* ISR callback function of the sensor_top */ void SensorTopIRQ(void) { - flag_sensor_top=true; + int_sensor_top=true; board->sensor_top->DisableInterruptMeasureDetectionIRQ(); } -/* callback function of the user button to stop program */ +/* ISR callback function of the user blue button to stop program */ void StopMeasureIRQ(void) { - flag_stop_measure=true; + int_stop_measure=true; } +/* On board 4 digit local display refresh */ void DisplayRefresh(OperatingMode op_mode) { char str[5]; @@ -58,12 +61,10 @@ if(data_sensor_top.range_mm!=0xFFFFFFFF) { sprintf(str,"%d",data_sensor_top.range_mm); - board->display->DisplayString(str, strlen(str)); } else { sprintf(str,"%s","----"); - board->display->DisplayString(str, strlen(str)); } } else if(op_mode==als_continuous_interrupt) @@ -71,25 +72,27 @@ if(data_sensor_top.lux!=0xFFFFFFFF) { sprintf(str,"%d",data_sensor_top.lux); - board->display->DisplayString(str, strlen(str)); } else { sprintf(str,"%s","----"); - board->display->DisplayString(str, strlen(str)); } - } + } + board->display->DisplayString(str, strlen(str)); } -OperatingMode CheckSlider(int measure) +/* On board red slider position check */ +OperatingMode CheckSlider(STMPE1600DigiIn measure_type) { + int measure=measure_type; if(measure==RANGE) return range_continuous_interrupt; else if(measure==ALS) return als_continuous_interrupt; } -void StartPrintMessage(OperatingMode op_mode) +/* Print on USB Serial the started OperatingMode */ +void PrintStartMessage(OperatingMode op_mode) { if(op_mode==range_continuous_interrupt) printf("\nStarted range continuous interrupt measure\n\r"); @@ -97,7 +100,8 @@ printf("\nStarted als continuous interrupt measure\n\r"); } -void StopPrintMessage(OperatingMode op_mode) +/* Print on USB Serial the stopped OperatingMode */ +void PrintStopMessage(OperatingMode op_mode) { if(op_mode==range_continuous_interrupt) printf("Stopped range continuous interrupt measure\n\r"); @@ -105,16 +109,20 @@ printf("Stopped als continuous interrupt measure\n\r"); } +/* Print on board 4 Digit display the app exiting BYE message */ +#define DELAY 2000 void Bye() { - int i; + Timer timer; char str[5]; - for(i=0;i<200;i++) + timer.start(); + for(int i=0; i<DELAY; i=timer.read_ms()) { sprintf(str,"%s","BYE"); board->display->DisplayString(str, strlen(str)); } + timer.stop(); } int main() @@ -124,7 +132,7 @@ /* user button used to stop measurements in interrupt mode */ InterruptIn stop_button(USER_BUTTON); STMPE1600DigiIn measure_type(device_i2c, GPIO_11); - int status, measure; + int status; //device_i2c.frequency(400000); //change i2c frequncy from 100kHz to 400kHz pc.baud(115200); //change the printf baudrate @@ -134,41 +142,39 @@ printf("Failed to init board!\n\r"); stop_button.rise(&StopMeasureIRQ); - measure=measure_type; - operating_mode=CheckSlider(measure); + operating_mode=CheckSlider(measure_type); status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); if(!status) { prev_operating_mode=operating_mode; - StartPrintMessage(operating_mode); + PrintStartMessage(operating_mode); while(1) { - if(flag_sensor_top) + if(int_sensor_top) { - flag_sensor_top=false; + int_sensor_top=false; status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); DisplayRefresh(operating_mode); } - if(flag_stop_measure) + if(int_stop_measure) { status=board->sensor_top->StopMeasurement(prev_operating_mode); if(!status) - StopPrintMessage(prev_operating_mode); + PrintStopMessage(prev_operating_mode); printf("\nProgram stopped!\n\n\r"); break; } - measure=measure_type; - operating_mode=CheckSlider(measure); + operating_mode=CheckSlider(measure_type); if(operating_mode!=prev_operating_mode) { DisplayRefresh(prev_operating_mode); status=board->sensor_top->StopMeasurement(prev_operating_mode); if(!status) - StopPrintMessage(prev_operating_mode); + PrintStopMessage(prev_operating_mode); prev_operating_mode=operating_mode; status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); if(!status) - StartPrintMessage(operating_mode); + PrintStartMessage(operating_mode); } else DisplayRefresh(operating_mode);