![](/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
main.cpp@19:394c9a4a3046, 2015-10-30 (annotated)
- Committer:
- gallonm
- Date:
- Fri Oct 30 11:59:52 2015 +0100
- Revision:
- 19:394c9a4a3046
- Parent:
- 18:6312be3b32d9
- Child:
- 20:b2e0b41a0e6b
Introduced a test application that performs a range mesurement and an als measurement
in continuous interrupt mode.
Display select slider allow to switch the measurement type.
User button stop the program.
Reset button star the program.
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:946e62f44f4f | 8 | /* Polling operating modes don`t require callback function that handles IRQ |
gallonm | 14:946e62f44f4f | 9 | Callback functions are used only for measure that require interrupt */ |
gallonm | 14:946e62f44f4f | 10 | |
gallonm | 5:fa65d931bd96 | 11 | #define VL6180X_I2C_SDA I2C_SDA |
gallonm | 5:fa65d931bd96 | 12 | #define VL6180X_I2C_SCL I2C_SCL |
gallonm | 5:fa65d931bd96 | 13 | |
gallonm | 19:394c9a4a3046 | 14 | #define RANGE 0 |
gallonm | 19:394c9a4a3046 | 15 | #define ALS 1 |
gallonm | 10:5319abadb31e | 16 | |
gallonm | 14:946e62f44f4f | 17 | /* timer and digital out pin used for debugging */ |
gallonm | 14:946e62f44f4f | 18 | //Timer timer; |
gallonm | 14:946e62f44f4f | 19 | //int start, end; |
gallonm | 14:946e62f44f4f | 20 | //DigitalOut pin(PA_15); |
gallonm | 5:fa65d931bd96 | 21 | |
gallonm | 8:4c05f7a5bb60 | 22 | DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
gallonm | 9:1d0e839edee8 | 23 | static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c); |
gallonm | 9:1d0e839edee8 | 24 | |
gallonm | 19:394c9a4a3046 | 25 | MeasureData_t data_sensor_top; |
gallonm | 11:657dbe7bf245 | 26 | |
gallonm | 15:b94bc967fecd | 27 | /* flags that handle interrupt request */ |
gallonm | 19:394c9a4a3046 | 28 | bool flag_sensor_top=false, flag_stop_measure=false; |
gallonm | 15:b94bc967fecd | 29 | |
gallonm | 19:394c9a4a3046 | 30 | /* callback function of the sensor_top */ |
gallonm | 15:b94bc967fecd | 31 | void SensorTopIRQ(void) |
gallonm | 9:1d0e839edee8 | 32 | { |
gallonm | 11:657dbe7bf245 | 33 | flag_sensor_top=true; |
gallonm | 14:946e62f44f4f | 34 | board->sensor_top->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 9:1d0e839edee8 | 35 | } |
gallonm | 9:1d0e839edee8 | 36 | |
gallonm | 19:394c9a4a3046 | 37 | /* callback function of the user button to stop program */ |
gallonm | 15:b94bc967fecd | 38 | void StopMeasureIRQ(void) |
gallonm | 15:b94bc967fecd | 39 | { |
gallonm | 15:b94bc967fecd | 40 | flag_stop_measure=true; |
gallonm | 15:b94bc967fecd | 41 | } |
gallonm | 14:946e62f44f4f | 42 | |
gallonm | 19:394c9a4a3046 | 43 | void DisplayRefresh(OperatingMode op_mode) |
gallonm | 19:394c9a4a3046 | 44 | { |
gallonm | 19:394c9a4a3046 | 45 | char str[4]; |
gallonm | 19:394c9a4a3046 | 46 | |
gallonm | 19:394c9a4a3046 | 47 | if(op_mode==range_continuous_interrupt) |
gallonm | 19:394c9a4a3046 | 48 | { |
gallonm | 19:394c9a4a3046 | 49 | if(data_sensor_top.range_mm!=0xFFFFFFFF) |
gallonm | 19:394c9a4a3046 | 50 | { |
gallonm | 19:394c9a4a3046 | 51 | sprintf(str,"%d",data_sensor_top.range_mm); |
gallonm | 19:394c9a4a3046 | 52 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 53 | } |
gallonm | 19:394c9a4a3046 | 54 | } |
gallonm | 19:394c9a4a3046 | 55 | else if(op_mode==als_continuous_interrupt) |
gallonm | 19:394c9a4a3046 | 56 | { |
gallonm | 19:394c9a4a3046 | 57 | if(data_sensor_top.lux!=0xFFFFFFFF) |
gallonm | 19:394c9a4a3046 | 58 | { |
gallonm | 19:394c9a4a3046 | 59 | sprintf(str,"%d",data_sensor_top.lux); |
gallonm | 19:394c9a4a3046 | 60 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 61 | } |
gallonm | 19:394c9a4a3046 | 62 | } |
gallonm | 19:394c9a4a3046 | 63 | } |
gallonm | 19:394c9a4a3046 | 64 | |
gallonm | 4:ccd62fd7e137 | 65 | int main() |
gallonm | 5:fa65d931bd96 | 66 | { |
gallonm | 19:394c9a4a3046 | 67 | OperatingMode operating_mode, prev_operating_mode; |
gallonm | 19:394c9a4a3046 | 68 | /* user button used to stop measurements in interrupt mode */ |
gallonm | 19:394c9a4a3046 | 69 | InterruptIn stop_button(USER_BUTTON); |
gallonm | 19:394c9a4a3046 | 70 | STMPE1600DigiIn measure_type(device_i2c, GPIO_11); |
gallonm | 19:394c9a4a3046 | 71 | int status, measure; |
gallonm | 19:394c9a4a3046 | 72 | char str[4]; |
gallonm | 19:394c9a4a3046 | 73 | |
gallonm | 19:394c9a4a3046 | 74 | pc.baud(115200); //change the printf baudrate |
gallonm | 14:946e62f44f4f | 75 | |
gallonm | 10:5319abadb31e | 76 | status=board->InitBoard(); |
gallonm | 14:946e62f44f4f | 77 | if(status) |
gallonm | 15:b94bc967fecd | 78 | printf("Failed to init the board!\n\r"); |
gallonm | 19:394c9a4a3046 | 79 | |
gallonm | 15:b94bc967fecd | 80 | stop_button.rise(&StopMeasureIRQ); |
gallonm | 19:394c9a4a3046 | 81 | measure=measure_type; |
gallonm | 19:394c9a4a3046 | 82 | if(measure==RANGE) |
gallonm | 19:394c9a4a3046 | 83 | operating_mode=range_continuous_interrupt; |
gallonm | 19:394c9a4a3046 | 84 | else if(measure==ALS) |
gallonm | 19:394c9a4a3046 | 85 | operating_mode=als_continuous_interrupt; |
gallonm | 19:394c9a4a3046 | 86 | status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, &data_sensor_top, NULL, NULL); |
gallonm | 19:394c9a4a3046 | 87 | prev_operating_mode=operating_mode; |
gallonm | 14:946e62f44f4f | 88 | if(!status) |
gallonm | 14:946e62f44f4f | 89 | { |
gallonm | 19:394c9a4a3046 | 90 | printf("\nStart measurement\n\r"); |
gallonm | 14:946e62f44f4f | 91 | while(1) |
gallonm | 14:946e62f44f4f | 92 | { |
gallonm | 14:946e62f44f4f | 93 | if(flag_sensor_top) |
gallonm | 14:946e62f44f4f | 94 | { |
gallonm | 14:946e62f44f4f | 95 | flag_sensor_top=false; |
gallonm | 19:394c9a4a3046 | 96 | status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); |
gallonm | 14:946e62f44f4f | 97 | if(!status) |
gallonm | 19:394c9a4a3046 | 98 | { |
gallonm | 19:394c9a4a3046 | 99 | if(operating_mode==range_continuous_interrupt) |
gallonm | 19:394c9a4a3046 | 100 | sprintf(str,"%d",data_sensor_top.range_mm); |
gallonm | 19:394c9a4a3046 | 101 | else if(operating_mode==als_continuous_interrupt) |
gallonm | 19:394c9a4a3046 | 102 | sprintf(str,"%d",data_sensor_top.lux); |
gallonm | 19:394c9a4a3046 | 103 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 104 | } |
gallonm | 19:394c9a4a3046 | 105 | /*else |
gallonm | 19:394c9a4a3046 | 106 | { |
gallonm | 19:394c9a4a3046 | 107 | sprintf(str,"%s","----"); |
gallonm | 19:394c9a4a3046 | 108 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 109 | }*/ |
gallonm | 14:946e62f44f4f | 110 | } |
gallonm | 15:b94bc967fecd | 111 | if(flag_stop_measure) |
gallonm | 19:394c9a4a3046 | 112 | { |
gallonm | 19:394c9a4a3046 | 113 | status=board->sensor_top->StopMeasurement(prev_operating_mode); |
gallonm | 19:394c9a4a3046 | 114 | if(status) |
gallonm | 19:394c9a4a3046 | 115 | printf("Failed to stop measurement!\n\r"); |
gallonm | 19:394c9a4a3046 | 116 | else |
gallonm | 19:394c9a4a3046 | 117 | printf("Measurement stopped!\n\r"); |
gallonm | 19:394c9a4a3046 | 118 | printf("\nProgram stopped!\n\n\r"); |
gallonm | 19:394c9a4a3046 | 119 | break; |
gallonm | 19:394c9a4a3046 | 120 | } |
gallonm | 19:394c9a4a3046 | 121 | measure=measure_type; |
gallonm | 19:394c9a4a3046 | 122 | if(measure==RANGE) |
gallonm | 19:394c9a4a3046 | 123 | operating_mode=range_continuous_interrupt; |
gallonm | 19:394c9a4a3046 | 124 | else if(measure==ALS) |
gallonm | 19:394c9a4a3046 | 125 | operating_mode=als_continuous_interrupt; |
gallonm | 19:394c9a4a3046 | 126 | if(operating_mode!=prev_operating_mode) |
gallonm | 14:946e62f44f4f | 127 | { |
gallonm | 19:394c9a4a3046 | 128 | DisplayRefresh(prev_operating_mode); |
gallonm | 19:394c9a4a3046 | 129 | status=board->sensor_top->StopMeasurement(prev_operating_mode); |
gallonm | 19:394c9a4a3046 | 130 | prev_operating_mode=operating_mode; |
gallonm | 19:394c9a4a3046 | 131 | if(status) |
gallonm | 19:394c9a4a3046 | 132 | printf("Failed to stop measurement!\n\r"); |
gallonm | 19:394c9a4a3046 | 133 | else |
gallonm | 19:394c9a4a3046 | 134 | printf("Measurement stopped!\n\r"); |
gallonm | 19:394c9a4a3046 | 135 | status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, &data_sensor_top, NULL, NULL); |
gallonm | 19:394c9a4a3046 | 136 | if(status) |
gallonm | 19:394c9a4a3046 | 137 | { |
gallonm | 19:394c9a4a3046 | 138 | printf("\nFailed to start measurement!\n\r"); |
gallonm | 19:394c9a4a3046 | 139 | break; |
gallonm | 19:394c9a4a3046 | 140 | } |
gallonm | 19:394c9a4a3046 | 141 | else |
gallonm | 19:394c9a4a3046 | 142 | printf("\nStart measurement\n\r"); |
gallonm | 19:394c9a4a3046 | 143 | } |
gallonm | 19:394c9a4a3046 | 144 | else |
gallonm | 19:394c9a4a3046 | 145 | DisplayRefresh(operating_mode); |
gallonm | 14:946e62f44f4f | 146 | } |
gallonm | 14:946e62f44f4f | 147 | } |
gallonm | 14:946e62f44f4f | 148 | else |
gallonm | 19:394c9a4a3046 | 149 | printf("\nFailed to start measurement!\n\r"); |
gallonm | 10:5319abadb31e | 150 | |
gallonm | 19:394c9a4a3046 | 151 | delete board; |
gallonm | 4:ccd62fd7e137 | 152 | } |
gallonm | 5:fa65d931bd96 | 153 | |
gallonm | 5:fa65d931bd96 | 154 |