Sweep a servo according to Proximity sensor measure
Dependencies: Servo X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1 by
main.cpp@16:fbf61b7c65ba, 2015-10-28 (annotated)
- Committer:
- gallonm
- Date:
- Wed Oct 28 13:09:32 2015 +0000
- Revision:
- 16:fbf61b7c65ba
- Parent:
- 15:b94bc967fecd
- Child:
- 17:68515d0667af
Updated main.cpp
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 | 14:946e62f44f4f | 3 | #include "HmiBBGesture.h" |
gallonm | 4:ccd62fd7e137 | 4 | #include <string.h> |
gallonm | 4:ccd62fd7e137 | 5 | #include <stdlib.h> |
gallonm | 4:ccd62fd7e137 | 6 | #include <stdio.h> |
gallonm | 8:4c05f7a5bb60 | 7 | #include <assert.h> |
gallonm | 8:4c05f7a5bb60 | 8 | |
gallonm | 14:946e62f44f4f | 9 | /* Polling operating modes don`t require callback function that handles IRQ |
gallonm | 14:946e62f44f4f | 10 | Callback functions are used only for measure that require interrupt */ |
gallonm | 14:946e62f44f4f | 11 | |
gallonm | 5:fa65d931bd96 | 12 | #define VL6180X_I2C_SDA I2C_SDA |
gallonm | 5:fa65d931bd96 | 13 | #define VL6180X_I2C_SCL I2C_SCL |
gallonm | 5:fa65d931bd96 | 14 | |
gallonm | 10:5319abadb31e | 15 | //#define RANGE_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 16 | //#define ALS_SINGLE_SHOT_POLLING |
gallonm | 10:5319abadb31e | 17 | //#define RANGE_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 18 | //#define ALS_CONTINUOUS_POLLING |
gallonm | 10:5319abadb31e | 19 | //#define RANGE_CONTINUOUS_INTERRUPT |
gallonm | 14:946e62f44f4f | 20 | //#define ALS_CONTINUOUS_INTERRUPT |
gallonm | 11:657dbe7bf245 | 21 | //#define INTERLEAVED_MODE_INTERRUPT |
gallonm | 10:5319abadb31e | 22 | //#define RANGE_CONTINUOUS_POLLING_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 23 | //#define RANGE_CONTINUOUS_POLLING_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 24 | //#define RANGE_CONTINUOUS_POLLING_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 25 | //#define ALS_CONTINUOUS_POLLING_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 26 | //#define ALS_CONTINUOUS_POLLING_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 27 | //#define ALS_CONTINUOUS_POLLING_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 28 | //#define RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 29 | //#define RANGE_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 30 | //#define RANGE_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW |
gallonm | 10:5319abadb31e | 31 | //#define ALS_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 10:5319abadb31e | 32 | //#define ALS_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD |
gallonm | 10:5319abadb31e | 33 | //#define ALS_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW |
gallonm | 14:946e62f44f4f | 34 | #define HAND_GESTURE |
gallonm | 10:5319abadb31e | 35 | |
gallonm | 14:946e62f44f4f | 36 | /* timer and digital out pin used for debugging */ |
gallonm | 14:946e62f44f4f | 37 | //Timer timer; |
gallonm | 14:946e62f44f4f | 38 | //int start, end; |
gallonm | 14:946e62f44f4f | 39 | //DigitalOut pin(PA_15); |
gallonm | 5:fa65d931bd96 | 40 | |
gallonm | 8:4c05f7a5bb60 | 41 | DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
gallonm | 9:1d0e839edee8 | 42 | static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c); |
gallonm | 9:1d0e839edee8 | 43 | |
gallonm | 14:946e62f44f4f | 44 | HmiBBGesture gesture_reco; |
gallonm | 9:1d0e839edee8 | 45 | |
gallonm | 14:946e62f44f4f | 46 | MeasureData_t data_sensor_top, data_sensor_bottom, data_sensor_left, data_sensor_right; |
gallonm | 11:657dbe7bf245 | 47 | |
gallonm | 15:b94bc967fecd | 48 | /* flags that handle interrupt request */ |
gallonm | 15:b94bc967fecd | 49 | bool flag_sensor_top=false, flag_sensor_bottom=false, flag_sensor_left=false, flag_sensor_right=false, flag_stop_measure=false; |
gallonm | 15:b94bc967fecd | 50 | |
gallonm | 15:b94bc967fecd | 51 | /* user button used to stop measurements in interrupt mode */ |
gallonm | 15:b94bc967fecd | 52 | InterruptIn stop_button(USER_BUTTON); |
gallonm | 9:1d0e839edee8 | 53 | |
gallonm | 9:1d0e839edee8 | 54 | /* callback functions of the sensors */ |
gallonm | 15:b94bc967fecd | 55 | void SensorTopIRQ(void) |
gallonm | 9:1d0e839edee8 | 56 | { |
gallonm | 11:657dbe7bf245 | 57 | flag_sensor_top=true; |
gallonm | 14:946e62f44f4f | 58 | board->sensor_top->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 9:1d0e839edee8 | 59 | } |
gallonm | 9:1d0e839edee8 | 60 | |
gallonm | 15:b94bc967fecd | 61 | void SensorBottomIRQ(void) |
gallonm | 11:657dbe7bf245 | 62 | { |
gallonm | 11:657dbe7bf245 | 63 | flag_sensor_bottom=true; |
gallonm | 14:946e62f44f4f | 64 | board->sensor_bottom->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 11:657dbe7bf245 | 65 | } |
gallonm | 11:657dbe7bf245 | 66 | |
gallonm | 15:b94bc967fecd | 67 | void SensorLeftIRQ(void) |
gallonm | 11:657dbe7bf245 | 68 | { |
gallonm | 11:657dbe7bf245 | 69 | flag_sensor_left=true; |
gallonm | 14:946e62f44f4f | 70 | board->sensor_left->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 11:657dbe7bf245 | 71 | } |
gallonm | 11:657dbe7bf245 | 72 | |
gallonm | 15:b94bc967fecd | 73 | void SensorRightIRQ(void) |
gallonm | 11:657dbe7bf245 | 74 | { |
gallonm | 11:657dbe7bf245 | 75 | flag_sensor_right=true; |
gallonm | 14:946e62f44f4f | 76 | board->sensor_right->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 11:657dbe7bf245 | 77 | } |
gallonm | 9:1d0e839edee8 | 78 | |
gallonm | 15:b94bc967fecd | 79 | void StopMeasureIRQ(void) |
gallonm | 15:b94bc967fecd | 80 | { |
gallonm | 15:b94bc967fecd | 81 | flag_stop_measure=true; |
gallonm | 15:b94bc967fecd | 82 | } |
gallonm | 14:946e62f44f4f | 83 | |
gallonm | 4:ccd62fd7e137 | 84 | int main() |
gallonm | 5:fa65d931bd96 | 85 | { |
gallonm | 14:946e62f44f4f | 86 | int status, status_t, status_b, status_l, status_r; |
gallonm | 14:946e62f44f4f | 87 | |
gallonm | 15:b94bc967fecd | 88 | //device_i2c.frequency(400000); //change i2c frequncy from 100kHz to 400kHz |
gallonm | 14:946e62f44f4f | 89 | pc.baud(115200); //change baudrate of the printf |
gallonm | 14:946e62f44f4f | 90 | |
gallonm | 10:5319abadb31e | 91 | status=board->InitBoard(); |
gallonm | 14:946e62f44f4f | 92 | if(status) |
gallonm | 15:b94bc967fecd | 93 | printf("Failed to init the board!\n\r"); |
gallonm | 10:5319abadb31e | 94 | |
gallonm | 10:5319abadb31e | 95 | #ifdef RANGE_SINGLE_SHOT_POLLING |
gallonm | 14:946e62f44f4f | 96 | status_t=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 97 | if(!status_t) |
gallonm | 14:946e62f44f4f | 98 | printf("Top Range: %dmm\n\r",data_sensor_top.range_mm); |
gallonm | 14:946e62f44f4f | 99 | else if(status==INVALID_PARAMS) |
gallonm | 14:946e62f44f4f | 100 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 101 | else |
gallonm | 14:946e62f44f4f | 102 | printf("Invalid range value!\n\r"); |
gallonm | 14:946e62f44f4f | 103 | status_t=board->sensor_top->StopMeasurement(range_single_shot_polling); |
gallonm | 14:946e62f44f4f | 104 | if(status_t) |
gallonm | 14:946e62f44f4f | 105 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 106 | #endif |
gallonm | 9:1d0e839edee8 | 107 | |
gallonm | 10:5319abadb31e | 108 | #ifdef ALS_SINGLE_SHOT_POLLING |
gallonm | 14:946e62f44f4f | 109 | status_t=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 110 | if(!status_t) |
gallonm | 15:b94bc967fecd | 111 | printf("Top Light: %d lux\n\r",data_sensor_top.lux); |
gallonm | 14:946e62f44f4f | 112 | else if(status==INVALID_PARAMS) |
gallonm | 14:946e62f44f4f | 113 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 114 | else |
gallonm | 14:946e62f44f4f | 115 | printf("Invalid light value!\n\r"); |
gallonm | 14:946e62f44f4f | 116 | status_t=board->sensor_top->StopMeasurement(als_single_shot_polling); |
gallonm | 14:946e62f44f4f | 117 | if(status_t) |
gallonm | 14:946e62f44f4f | 118 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 119 | #endif |
gallonm | 10:5319abadb31e | 120 | |
gallonm | 10:5319abadb31e | 121 | #ifdef RANGE_CONTINUOUS_POLLING |
gallonm | 14:946e62f44f4f | 122 | status_t=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 123 | if(!status_t) |
gallonm | 14:946e62f44f4f | 124 | { |
gallonm | 14:946e62f44f4f | 125 | int i; |
gallonm | 14:946e62f44f4f | 126 | for(i=0;i<10;i++) |
gallonm | 14:946e62f44f4f | 127 | { |
gallonm | 14:946e62f44f4f | 128 | status_t=board->sensor_top->GetMeasurement(range_continuous_polling, &data_sensor_top); |
gallonm | 11:657dbe7bf245 | 129 | if(!status_t) |
gallonm | 14:946e62f44f4f | 130 | printf("Top Range measure %d: %dmm\n\r",i+1,data_sensor_top.range_mm); |
gallonm | 14:946e62f44f4f | 131 | else |
gallonm | 14:946e62f44f4f | 132 | printf("Invalid range value!\n\r"); |
gallonm | 14:946e62f44f4f | 133 | } |
gallonm | 10:5319abadb31e | 134 | } |
gallonm | 14:946e62f44f4f | 135 | else |
gallonm | 14:946e62f44f4f | 136 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 137 | status_t=board->sensor_top->StopMeasurement(range_continuous_polling); |
gallonm | 14:946e62f44f4f | 138 | if(status_t) |
gallonm | 14:946e62f44f4f | 139 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 140 | #endif |
gallonm | 10:5319abadb31e | 141 | |
gallonm | 10:5319abadb31e | 142 | #ifdef ALS_CONTINUOUS_POLLING |
gallonm | 14:946e62f44f4f | 143 | status_t=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 144 | if(!status_t) |
gallonm | 14:946e62f44f4f | 145 | { |
gallonm | 14:946e62f44f4f | 146 | int i; |
gallonm | 14:946e62f44f4f | 147 | for(i=0;i<10;i++) |
gallonm | 14:946e62f44f4f | 148 | { |
gallonm | 14:946e62f44f4f | 149 | status=board->sensor_top->GetMeasurement(als_continuous_polling, &data_sensor_top); |
gallonm | 11:657dbe7bf245 | 150 | if(!status_t) |
gallonm | 15:b94bc967fecd | 151 | printf("Top Light measure %d: %d lux\n\r",i,data_sensor_top.lux); |
gallonm | 14:946e62f44f4f | 152 | else |
gallonm | 14:946e62f44f4f | 153 | printf("Invalid light value!\n\r"); |
gallonm | 14:946e62f44f4f | 154 | } |
gallonm | 9:1d0e839edee8 | 155 | } |
gallonm | 14:946e62f44f4f | 156 | else |
gallonm | 14:946e62f44f4f | 157 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 158 | status_t=board->sensor_top->StopMeasurement(als_continuous_polling); |
gallonm | 14:946e62f44f4f | 159 | if(status_t) |
gallonm | 14:946e62f44f4f | 160 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 161 | #endif |
gallonm | 11:657dbe7bf245 | 162 | |
gallonm | 11:657dbe7bf245 | 163 | #ifdef RANGE_CONTINUOUS_INTERRUPT |
gallonm | 15:b94bc967fecd | 164 | stop_button.rise(&StopMeasureIRQ); |
gallonm | 14:946e62f44f4f | 165 | status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 166 | if(!status) |
gallonm | 14:946e62f44f4f | 167 | { |
gallonm | 14:946e62f44f4f | 168 | while(1) |
gallonm | 14:946e62f44f4f | 169 | { |
gallonm | 14:946e62f44f4f | 170 | if(flag_sensor_top) |
gallonm | 14:946e62f44f4f | 171 | { |
gallonm | 14:946e62f44f4f | 172 | flag_sensor_top=false; |
gallonm | 14:946e62f44f4f | 173 | status=board->sensor_top->HandleIRQ(range_continuous_interrupt, &data_sensor_top); |
gallonm | 14:946e62f44f4f | 174 | if(!status) |
gallonm | 14:946e62f44f4f | 175 | printf("Range: %dmm\n\r",data_sensor_top.range_mm); |
gallonm | 14:946e62f44f4f | 176 | else |
gallonm | 14:946e62f44f4f | 177 | printf("Invalid range value!\n\r"); |
gallonm | 14:946e62f44f4f | 178 | } |
gallonm | 15:b94bc967fecd | 179 | if(flag_stop_measure) |
gallonm | 15:b94bc967fecd | 180 | break; |
gallonm | 14:946e62f44f4f | 181 | } |
gallonm | 14:946e62f44f4f | 182 | } |
gallonm | 14:946e62f44f4f | 183 | else |
gallonm | 14:946e62f44f4f | 184 | printf("Failed to start measurement!\n\r"); |
gallonm | 15:b94bc967fecd | 185 | status=board->sensor_top->StopMeasurement(range_continuous_interrupt); |
gallonm | 14:946e62f44f4f | 186 | if(status) |
gallonm | 14:946e62f44f4f | 187 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 188 | #endif |
gallonm | 10:5319abadb31e | 189 | |
gallonm | 10:5319abadb31e | 190 | #ifdef ALS_CONTINUOUS_INTERRUPT |
gallonm | 15:b94bc967fecd | 191 | stop_button.rise(&StopMeasureIRQ); |
gallonm | 14:946e62f44f4f | 192 | status_t=board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL); |
gallonm | 11:657dbe7bf245 | 193 | if(!status_t) |
gallonm | 14:946e62f44f4f | 194 | { |
gallonm | 14:946e62f44f4f | 195 | while(1) |
gallonm | 14:946e62f44f4f | 196 | { |
gallonm | 14:946e62f44f4f | 197 | if(flag_sensor_top) |
gallonm | 14:946e62f44f4f | 198 | { |
gallonm | 14:946e62f44f4f | 199 | flag_sensor_top=false; |
gallonm | 14:946e62f44f4f | 200 | status_t=board->sensor_top->HandleIRQ(als_continuous_interrupt, &data_sensor_top); |
gallonm | 14:946e62f44f4f | 201 | if(!status_t) |
gallonm | 15:b94bc967fecd | 202 | printf("Top Light: %d lux\n\r",data_sensor_top.lux); |
gallonm | 14:946e62f44f4f | 203 | else |
gallonm | 14:946e62f44f4f | 204 | printf("Invalid light value!\n\r"); |
gallonm | 10:5319abadb31e | 205 | } |
gallonm | 15:b94bc967fecd | 206 | if(flag_stop_measure) |
gallonm | 15:b94bc967fecd | 207 | break; |
gallonm | 14:946e62f44f4f | 208 | } |
gallonm | 14:946e62f44f4f | 209 | } |
gallonm | 14:946e62f44f4f | 210 | else |
gallonm | 14:946e62f44f4f | 211 | printf("Failed to stop measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 212 | status_t=board->sensor_top->StopMeasurement(als_continuous_interrupt); |
gallonm | 14:946e62f44f4f | 213 | if(status_t) |
gallonm | 14:946e62f44f4f | 214 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 215 | #endif |
gallonm | 9:1d0e839edee8 | 216 | |
gallonm | 15:b94bc967fecd | 217 | #ifdef INTERLEAVED_MODE_INTERRUPT |
gallonm | 15:b94bc967fecd | 218 | stop_button.rise(&StopMeasureIRQ); |
gallonm | 14:946e62f44f4f | 219 | status_t=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 220 | if(!status_t) |
gallonm | 14:946e62f44f4f | 221 | { |
gallonm | 14:946e62f44f4f | 222 | while(1) |
gallonm | 14:946e62f44f4f | 223 | { |
gallonm | 14:946e62f44f4f | 224 | if(flag_sensor_top) |
gallonm | 14:946e62f44f4f | 225 | { |
gallonm | 14:946e62f44f4f | 226 | flag_sensor_top=false; |
gallonm | 14:946e62f44f4f | 227 | status_t=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &data_sensor_top); |
gallonm | 14:946e62f44f4f | 228 | if(!status_t) |
gallonm | 15:b94bc967fecd | 229 | printf("Top Range: %dmm\tTop Light: %d lux\n\r",data_sensor_top.range_mm, data_sensor_top.lux); |
gallonm | 14:946e62f44f4f | 230 | else |
gallonm | 14:946e62f44f4f | 231 | printf("Invalid range or light value!\n\r"); |
gallonm | 9:1d0e839edee8 | 232 | } |
gallonm | 15:b94bc967fecd | 233 | if(flag_stop_measure) |
gallonm | 15:b94bc967fecd | 234 | break; |
gallonm | 14:946e62f44f4f | 235 | } |
gallonm | 14:946e62f44f4f | 236 | } |
gallonm | 14:946e62f44f4f | 237 | else |
gallonm | 14:946e62f44f4f | 238 | printf("Failed to stop measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 239 | status_t=board->sensor_top->StopMeasurement(interleaved_mode_interrupt); |
gallonm | 14:946e62f44f4f | 240 | if(status_t) |
gallonm | 14:946e62f44f4f | 241 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 242 | #endif |
gallonm | 10:5319abadb31e | 243 | |
gallonm | 10:5319abadb31e | 244 | #ifdef RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD |
gallonm | 15:b94bc967fecd | 245 | stop_button.rise(&StopMeasureIRQ); |
gallonm | 14:946e62f44f4f | 246 | status_t=board->sensor_top->StartMeasurement(range_continuous_interrupt_low_threshold, SensorTopIRQ, &data_sensor_top, 80, NULL); |
gallonm | 14:946e62f44f4f | 247 | if(!status_t) |
gallonm | 14:946e62f44f4f | 248 | { |
gallonm | 14:946e62f44f4f | 249 | while(1) |
gallonm | 14:946e62f44f4f | 250 | { |
gallonm | 14:946e62f44f4f | 251 | if(flag_sensor_top) |
gallonm | 14:946e62f44f4f | 252 | { |
gallonm | 14:946e62f44f4f | 253 | flag_sensor_top=false; |
gallonm | 14:946e62f44f4f | 254 | status_t=board->sensor_top->HandleIRQ(range_continuous_interrupt_low_threshold, &data_sensor_top); |
gallonm | 14:946e62f44f4f | 255 | if(!status_t) |
gallonm | 14:946e62f44f4f | 256 | printf("Range int low threshold: %dmm\n\r",data_sensor_top.range_mm); |
gallonm | 14:946e62f44f4f | 257 | else |
gallonm | 14:946e62f44f4f | 258 | printf("Invalid range value!\n\r"); |
gallonm | 14:946e62f44f4f | 259 | } |
gallonm | 15:b94bc967fecd | 260 | if(flag_stop_measure) |
gallonm | 15:b94bc967fecd | 261 | break; |
gallonm | 14:946e62f44f4f | 262 | } |
gallonm | 14:946e62f44f4f | 263 | } |
gallonm | 14:946e62f44f4f | 264 | else |
gallonm | 14:946e62f44f4f | 265 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 266 | status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold); |
gallonm | 14:946e62f44f4f | 267 | if(status_t) |
gallonm | 14:946e62f44f4f | 268 | printf("Failed to stop measurement!\n\r"); |
gallonm | 10:5319abadb31e | 269 | #endif |
gallonm | 5:fa65d931bd96 | 270 | |
gallonm | 15:b94bc967fecd | 271 | /* Hand Gesture test application */ |
gallonm | 14:946e62f44f4f | 272 | #ifdef HAND_GESTURE |
gallonm | 15:b94bc967fecd | 273 | #define ALS_DELAY 5000 |
gallonm | 14:946e62f44f4f | 274 | int gest_code, prev_gest_code, gest_duration; |
gallonm | 14:946e62f44f4f | 275 | char str[5]; |
gallonm | 14:946e62f44f4f | 276 | char valLeft[] = {'E'}; |
gallonm | 14:946e62f44f4f | 277 | char valRight[] = {'3'}; |
gallonm | 15:b94bc967fecd | 278 | char valTap[] = {'0'}; |
gallonm | 15:b94bc967fecd | 279 | unsigned int timeout = 10000; |
gallonm | 15:b94bc967fecd | 280 | |
gallonm | 14:946e62f44f4f | 281 | status_l=board->sensor_left->StartMeasurement(range_continuous_interrupt, SensorLeftIRQ, &data_sensor_left, NULL, NULL); |
gallonm | 14:946e62f44f4f | 282 | status_r=board->sensor_right->StartMeasurement(range_continuous_interrupt, SensorRightIRQ, &data_sensor_right, NULL, NULL); |
gallonm | 15:b94bc967fecd | 283 | status_t=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &data_sensor_top, NULL, NULL); |
gallonm | 14:946e62f44f4f | 284 | gesture_reco.Init(150, 100, 200, 60, 500); |
gallonm | 16:fbf61b7c65ba | 285 | if((!status_l)&&(!status_r)&&(!status_t)) |
gallonm | 14:946e62f44f4f | 286 | { |
gallonm | 14:946e62f44f4f | 287 | while(1) |
gallonm | 14:946e62f44f4f | 288 | { |
gallonm | 14:946e62f44f4f | 289 | if(flag_sensor_left) |
gallonm | 14:946e62f44f4f | 290 | { |
gallonm | 14:946e62f44f4f | 291 | flag_sensor_left=false; |
gallonm | 14:946e62f44f4f | 292 | status_l=board->sensor_left->HandleIRQ(range_continuous_interrupt, &data_sensor_left); |
gallonm | 14:946e62f44f4f | 293 | } |
gallonm | 14:946e62f44f4f | 294 | if(flag_sensor_right) |
gallonm | 14:946e62f44f4f | 295 | { |
gallonm | 14:946e62f44f4f | 296 | flag_sensor_right=false; |
gallonm | 14:946e62f44f4f | 297 | status_r=board->sensor_right->HandleIRQ(range_continuous_interrupt, &data_sensor_right); |
gallonm | 14:946e62f44f4f | 298 | } |
gallonm | 14:946e62f44f4f | 299 | if(data_sensor_left.range_mm>400) |
gallonm | 14:946e62f44f4f | 300 | data_sensor_left.range_mm=400; |
gallonm | 14:946e62f44f4f | 301 | if(data_sensor_right.range_mm>400) |
gallonm | 14:946e62f44f4f | 302 | data_sensor_right.range_mm=400; |
gallonm | 14:946e62f44f4f | 303 | gest_code=gesture_reco.Update(data_sensor_right.range_mm, data_sensor_left.range_mm, &gest_duration); |
gallonm | 15:b94bc967fecd | 304 | if(timeout==ALS_DELAY && gest_code==-1) |
gallonm | 15:b94bc967fecd | 305 | { |
gallonm | 15:b94bc967fecd | 306 | status=board->sensor_top->GetMeasurement(als_continuous_polling, &data_sensor_top); |
gallonm | 15:b94bc967fecd | 307 | if(!status_t) |
gallonm | 15:b94bc967fecd | 308 | printf("Light measure: %d lux\n\r",data_sensor_top.lux); |
gallonm | 15:b94bc967fecd | 309 | else |
gallonm | 15:b94bc967fecd | 310 | printf("Invalid light value!\n\r"); |
gallonm | 15:b94bc967fecd | 311 | } |
gallonm | 14:946e62f44f4f | 312 | switch(gest_code) |
gallonm | 14:946e62f44f4f | 313 | { |
gallonm | 14:946e62f44f4f | 314 | case -1: |
gallonm | 15:b94bc967fecd | 315 | timeout++; |
gallonm | 15:b94bc967fecd | 316 | if(timeout>=ALS_DELAY) |
gallonm | 15:b94bc967fecd | 317 | timeout=ALS_DELAY; |
gallonm | 14:946e62f44f4f | 318 | break; |
gallonm | 14:946e62f44f4f | 319 | case 0: //tap and start timer |
gallonm | 14:946e62f44f4f | 320 | if(gest_code!=prev_gest_code) |
gallonm | 14:946e62f44f4f | 321 | { |
gallonm | 14:946e62f44f4f | 322 | prev_gest_code=gest_code; |
gallonm | 14:946e62f44f4f | 323 | sprintf(str,"%s","0-"); |
gallonm | 14:946e62f44f4f | 324 | board->display->DisplayDigit(valTap, 1); |
gallonm | 14:946e62f44f4f | 325 | } |
gallonm | 14:946e62f44f4f | 326 | else |
gallonm | 14:946e62f44f4f | 327 | { |
gallonm | 14:946e62f44f4f | 328 | if(strcmp(str,"0-")==0) |
gallonm | 14:946e62f44f4f | 329 | { |
gallonm | 14:946e62f44f4f | 330 | sprintf(str,"-0"); |
gallonm | 14:946e62f44f4f | 331 | board->display->DisplayDigit(valTap, 2); |
gallonm | 14:946e62f44f4f | 332 | } |
gallonm | 14:946e62f44f4f | 333 | else |
gallonm | 14:946e62f44f4f | 334 | { |
gallonm | 14:946e62f44f4f | 335 | sprintf(str,"0-"); |
gallonm | 14:946e62f44f4f | 336 | board->display->DisplayDigit(valTap, 1); |
gallonm | 14:946e62f44f4f | 337 | } |
gallonm | 14:946e62f44f4f | 338 | } |
gallonm | 15:b94bc967fecd | 339 | timeout=0; |
gallonm | 14:946e62f44f4f | 340 | break; |
gallonm | 14:946e62f44f4f | 341 | case 1: //left to right |
gallonm | 15:b94bc967fecd | 342 | board->display->DisplayDigit(valRight, 3); |
gallonm | 15:b94bc967fecd | 343 | timeout=0; |
gallonm | 14:946e62f44f4f | 344 | break; |
gallonm | 14:946e62f44f4f | 345 | case 2: //right to left |
gallonm | 14:946e62f44f4f | 346 | board->display->DisplayDigit(valLeft, 0); |
gallonm | 15:b94bc967fecd | 347 | timeout=0; |
gallonm | 14:946e62f44f4f | 348 | break; |
gallonm | 14:946e62f44f4f | 349 | default: |
gallonm | 14:946e62f44f4f | 350 | printf("Errore!\n\r"); |
gallonm | 14:946e62f44f4f | 351 | } |
gallonm | 14:946e62f44f4f | 352 | } |
gallonm | 14:946e62f44f4f | 353 | } |
gallonm | 14:946e62f44f4f | 354 | else |
gallonm | 14:946e62f44f4f | 355 | printf("Failed to start measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 356 | status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold); |
gallonm | 14:946e62f44f4f | 357 | if(status_t) |
gallonm | 14:946e62f44f4f | 358 | printf("Failed to stop measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 359 | status_t=board->sensor_top->StopMeasurement(als_continuous_interrupt); |
gallonm | 14:946e62f44f4f | 360 | if(status_t) |
gallonm | 14:946e62f44f4f | 361 | printf("Failed to stop measurement!\n\r"); |
gallonm | 14:946e62f44f4f | 362 | #endif |
gallonm | 11:657dbe7bf245 | 363 | |
gallonm | 15:b94bc967fecd | 364 | delete board; |
gallonm | 15:b94bc967fecd | 365 | |
gallonm | 4:ccd62fd7e137 | 366 | } |
gallonm | 5:fa65d931bd96 | 367 | |
gallonm | 5:fa65d931bd96 | 368 |