Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Committer:
gallonm
Date:
Mon Nov 02 15:45:36 2015 +0000
Revision:
23:9085260d2902
Parent:
22:bb440c864489
Child:
24:018a59122fe3
Updated DisplayRefresh function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gallonm 0:83c628a58feb 1 #include "mbed.h"
gallonm 8:4c05f7a5bb60 2 #include "x_nucleo_6180xa1.h"
gallonm 4:ccd62fd7e137 3 #include <string.h>
gallonm 4:ccd62fd7e137 4 #include <stdlib.h>
gallonm 4:ccd62fd7e137 5 #include <stdio.h>
gallonm 8:4c05f7a5bb60 6 #include <assert.h>
gallonm 8:4c05f7a5bb60 7
gallonm 20:b2e0b41a0e6b 8 /* This test application performs a range measurement and an als measurement.
gallonm 20:b2e0b41a0e6b 9 Display select slider allow to switch the measurement type; previuos
gallonm 20:b2e0b41a0e6b 10 measurement is stopped and a new measurement is started.
gallonm 20:b2e0b41a0e6b 11 User button allows to stop current measurement and the entire program.
gallonm 20:b2e0b41a0e6b 12 Reset button is used to restart the program. */
gallonm 20:b2e0b41a0e6b 13
gallonm 14:946e62f44f4f 14 /* Polling operating modes don`t require callback function that handles IRQ
gallonm 14:946e62f44f4f 15 Callback functions are used only for measure that require interrupt */
gallonm 14:946e62f44f4f 16
gallonm 20:b2e0b41a0e6b 17 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
gallonm 20:b2e0b41a0e6b 18 is not ready to be read from the corresponding register. So you need to wait
gallonm 20:b2e0b41a0e6b 19 for the result to be ready */
gallonm 20:b2e0b41a0e6b 20
gallonm 5:fa65d931bd96 21 #define VL6180X_I2C_SDA I2C_SDA
gallonm 5:fa65d931bd96 22 #define VL6180X_I2C_SCL I2C_SCL
gallonm 5:fa65d931bd96 23
gallonm 19:394c9a4a3046 24 #define RANGE 0
gallonm 19:394c9a4a3046 25 #define ALS 1
gallonm 10:5319abadb31e 26
gallonm 14:946e62f44f4f 27 /* timer and digital out pin used for debugging */
gallonm 14:946e62f44f4f 28 //Timer timer;
gallonm 14:946e62f44f4f 29 //int start, end;
gallonm 14:946e62f44f4f 30 //DigitalOut pin(PA_15);
gallonm 5:fa65d931bd96 31
gallonm 8:4c05f7a5bb60 32 DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
gallonm 9:1d0e839edee8 33 static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c);
gallonm 9:1d0e839edee8 34
gallonm 19:394c9a4a3046 35 MeasureData_t data_sensor_top;
gallonm 11:657dbe7bf245 36
gallonm 15:b94bc967fecd 37 /* flags that handle interrupt request */
gallonm 19:394c9a4a3046 38 bool flag_sensor_top=false, flag_stop_measure=false;
gallonm 15:b94bc967fecd 39
gallonm 19:394c9a4a3046 40 /* callback function of the sensor_top */
gallonm 15:b94bc967fecd 41 void SensorTopIRQ(void)
gallonm 9:1d0e839edee8 42 {
gallonm 11:657dbe7bf245 43 flag_sensor_top=true;
gallonm 14:946e62f44f4f 44 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
gallonm 9:1d0e839edee8 45 }
gallonm 9:1d0e839edee8 46
gallonm 19:394c9a4a3046 47 /* callback function of the user button to stop program */
gallonm 15:b94bc967fecd 48 void StopMeasureIRQ(void)
gallonm 15:b94bc967fecd 49 {
gallonm 15:b94bc967fecd 50 flag_stop_measure=true;
gallonm 15:b94bc967fecd 51 }
gallonm 14:946e62f44f4f 52
gallonm 19:394c9a4a3046 53 void DisplayRefresh(OperatingMode op_mode)
gallonm 19:394c9a4a3046 54 {
gallonm 19:394c9a4a3046 55 char str[4];
gallonm 19:394c9a4a3046 56
gallonm 19:394c9a4a3046 57 if(op_mode==range_continuous_interrupt)
gallonm 19:394c9a4a3046 58 {
gallonm 19:394c9a4a3046 59 if(data_sensor_top.range_mm!=0xFFFFFFFF)
gallonm 19:394c9a4a3046 60 {
gallonm 19:394c9a4a3046 61 sprintf(str,"%d",data_sensor_top.range_mm);
gallonm 19:394c9a4a3046 62 board->display->DisplayString(str, strlen(str));
gallonm 19:394c9a4a3046 63 }
gallonm 23:9085260d2902 64 /*else
gallonm 22:bb440c864489 65 {
gallonm 22:bb440c864489 66 sprintf(str,"%s","----");
gallonm 22:bb440c864489 67 board->display->DisplayString(str, strlen(str));
gallonm 23:9085260d2902 68 }*/
gallonm 19:394c9a4a3046 69 }
gallonm 19:394c9a4a3046 70 else if(op_mode==als_continuous_interrupt)
gallonm 19:394c9a4a3046 71 {
gallonm 19:394c9a4a3046 72 if(data_sensor_top.lux!=0xFFFFFFFF)
gallonm 19:394c9a4a3046 73 {
gallonm 19:394c9a4a3046 74 sprintf(str,"%d",data_sensor_top.lux);
gallonm 19:394c9a4a3046 75 board->display->DisplayString(str, strlen(str));
gallonm 19:394c9a4a3046 76 }
gallonm 23:9085260d2902 77 /*else
gallonm 22:bb440c864489 78 {
gallonm 22:bb440c864489 79 sprintf(str,"%s","----");
gallonm 22:bb440c864489 80 board->display->DisplayString(str, strlen(str));
gallonm 23:9085260d2902 81 }*/
gallonm 19:394c9a4a3046 82 }
gallonm 19:394c9a4a3046 83 }
gallonm 19:394c9a4a3046 84
gallonm 4:ccd62fd7e137 85 int main()
gallonm 5:fa65d931bd96 86 {
gallonm 19:394c9a4a3046 87 OperatingMode operating_mode, prev_operating_mode;
gallonm 19:394c9a4a3046 88 /* user button used to stop measurements in interrupt mode */
gallonm 19:394c9a4a3046 89 InterruptIn stop_button(USER_BUTTON);
gallonm 19:394c9a4a3046 90 STMPE1600DigiIn measure_type(device_i2c, GPIO_11);
gallonm 19:394c9a4a3046 91 int status, measure;
gallonm 19:394c9a4a3046 92 char str[4];
gallonm 19:394c9a4a3046 93
gallonm 19:394c9a4a3046 94 pc.baud(115200); //change the printf baudrate
gallonm 14:946e62f44f4f 95
gallonm 10:5319abadb31e 96 status=board->InitBoard();
gallonm 14:946e62f44f4f 97 if(status)
gallonm 20:b2e0b41a0e6b 98 printf("Failed to init the board!\n\r");
gallonm 19:394c9a4a3046 99
gallonm 15:b94bc967fecd 100 stop_button.rise(&StopMeasureIRQ);
gallonm 19:394c9a4a3046 101 measure=measure_type;
gallonm 19:394c9a4a3046 102 if(measure==RANGE)
gallonm 19:394c9a4a3046 103 operating_mode=range_continuous_interrupt;
gallonm 19:394c9a4a3046 104 else if(measure==ALS)
gallonm 19:394c9a4a3046 105 operating_mode=als_continuous_interrupt;
gallonm 20:b2e0b41a0e6b 106 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
gallonm 19:394c9a4a3046 107 prev_operating_mode=operating_mode;
gallonm 14:946e62f44f4f 108 if(!status)
gallonm 14:946e62f44f4f 109 {
gallonm 19:394c9a4a3046 110 printf("\nStart measurement\n\r");
gallonm 14:946e62f44f4f 111 while(1)
gallonm 14:946e62f44f4f 112 {
gallonm 14:946e62f44f4f 113 if(flag_sensor_top)
gallonm 14:946e62f44f4f 114 {
gallonm 14:946e62f44f4f 115 flag_sensor_top=false;
gallonm 19:394c9a4a3046 116 status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top);
gallonm 22:bb440c864489 117 DisplayRefresh(operating_mode);
gallonm 14:946e62f44f4f 118 }
gallonm 15:b94bc967fecd 119 if(flag_stop_measure)
gallonm 19:394c9a4a3046 120 {
gallonm 19:394c9a4a3046 121 status=board->sensor_top->StopMeasurement(prev_operating_mode);
gallonm 19:394c9a4a3046 122 if(status)
gallonm 19:394c9a4a3046 123 printf("Failed to stop measurement!\n\r");
gallonm 19:394c9a4a3046 124 else
gallonm 19:394c9a4a3046 125 printf("Measurement stopped!\n\r");
gallonm 19:394c9a4a3046 126 printf("\nProgram stopped!\n\n\r");
gallonm 19:394c9a4a3046 127 break;
gallonm 19:394c9a4a3046 128 }
gallonm 19:394c9a4a3046 129 measure=measure_type;
gallonm 19:394c9a4a3046 130 if(measure==RANGE)
gallonm 19:394c9a4a3046 131 operating_mode=range_continuous_interrupt;
gallonm 19:394c9a4a3046 132 else if(measure==ALS)
gallonm 19:394c9a4a3046 133 operating_mode=als_continuous_interrupt;
gallonm 19:394c9a4a3046 134 if(operating_mode!=prev_operating_mode)
gallonm 14:946e62f44f4f 135 {
gallonm 19:394c9a4a3046 136 DisplayRefresh(prev_operating_mode);
gallonm 19:394c9a4a3046 137 status=board->sensor_top->StopMeasurement(prev_operating_mode);
gallonm 19:394c9a4a3046 138 prev_operating_mode=operating_mode;
gallonm 19:394c9a4a3046 139 if(status)
gallonm 19:394c9a4a3046 140 printf("Failed to stop measurement!\n\r");
gallonm 19:394c9a4a3046 141 else
gallonm 19:394c9a4a3046 142 printf("Measurement stopped!\n\r");
gallonm 20:b2e0b41a0e6b 143 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
gallonm 19:394c9a4a3046 144 if(status)
gallonm 19:394c9a4a3046 145 {
gallonm 19:394c9a4a3046 146 printf("\nFailed to start measurement!\n\r");
gallonm 19:394c9a4a3046 147 break;
gallonm 19:394c9a4a3046 148 }
gallonm 19:394c9a4a3046 149 else
gallonm 19:394c9a4a3046 150 printf("\nStart measurement\n\r");
gallonm 19:394c9a4a3046 151 }
gallonm 19:394c9a4a3046 152 else
gallonm 19:394c9a4a3046 153 DisplayRefresh(operating_mode);
gallonm 14:946e62f44f4f 154 }
gallonm 14:946e62f44f4f 155 }
gallonm 14:946e62f44f4f 156 else
gallonm 19:394c9a4a3046 157 printf("\nFailed to start measurement!\n\r");
gallonm 20:b2e0b41a0e6b 158
gallonm 20:b2e0b41a0e6b 159 for(status=0;status<200;status++)
gallonm 20:b2e0b41a0e6b 160 {
gallonm 20:b2e0b41a0e6b 161 sprintf(str,"%s","CIAO");
gallonm 20:b2e0b41a0e6b 162 board->display->DisplayString(str, strlen(str));
gallonm 20:b2e0b41a0e6b 163 }
gallonm 10:5319abadb31e 164
gallonm 20:b2e0b41a0e6b 165 delete board;
gallonm 4:ccd62fd7e137 166 }
gallonm 5:fa65d931bd96 167