![](/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@39:851cfabf7aa6, 2016-02-23 (annotated)
- Committer:
- mapellil
- Date:
- Tue Feb 23 09:29:18 2016 +0000
- Revision:
- 39:851cfabf7aa6
- Parent:
- 37:724632fff9c1
- Child:
- 40:204f84887d70
I2c is now passed by address to IntContinousALSorRangeMeasure
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 | |
mapellil | 30:c8efec21544f | 8 | /* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode |
mapellil | 30:c8efec21544f | 9 | on the onboard embedded top sensor. |
mapellil | 30:c8efec21544f | 10 | The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the |
mapellil | 30:c8efec21544f | 11 | on bord 4digits display. |
mapellil | 30:c8efec21544f | 12 | |
mapellil | 30:c8efec21544f | 13 | User Blue button allows to stop current measurement and the entire program releasing all the resources. |
gallonm | 20:b2e0b41a0e6b | 14 | Reset button is used to restart the program. */ |
gallonm | 20:b2e0b41a0e6b | 15 | |
gallonm | 14:946e62f44f4f | 16 | /* Polling operating modes don`t require callback function that handles IRQ |
mapellil | 35:8b4a5cc0fb1f | 17 | Callback IRQ functions are used only for measure that require interrupt */ |
gallonm | 14:946e62f44f4f | 18 | |
gallonm | 20:b2e0b41a0e6b | 19 | /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value |
gallonm | 20:b2e0b41a0e6b | 20 | is not ready to be read from the corresponding register. So you need to wait |
gallonm | 20:b2e0b41a0e6b | 21 | for the result to be ready */ |
gallonm | 20:b2e0b41a0e6b | 22 | |
mapellil | 37:724632fff9c1 | 23 | #define VL6180X_I2C_SDA D14 |
mapellil | 37:724632fff9c1 | 24 | #define VL6180X_I2C_SCL D15 |
gallonm | 5:fa65d931bd96 | 25 | |
gallonm | 19:394c9a4a3046 | 26 | #define RANGE 0 |
gallonm | 19:394c9a4a3046 | 27 | #define ALS 1 |
gallonm | 10:5319abadb31e | 28 | |
gallonm | 27:2afb9baf718f | 29 | static X_NUCLEO_6180XA1 *board=NULL; |
gallonm | 19:394c9a4a3046 | 30 | MeasureData_t data_sensor_top; |
gallonm | 27:2afb9baf718f | 31 | OperatingMode operating_mode, prev_operating_mode; |
gallonm | 11:657dbe7bf245 | 32 | |
gallonm | 15:b94bc967fecd | 33 | /* flags that handle interrupt request */ |
mapellil | 30:c8efec21544f | 34 | bool int_sensor_top=false, int_stop_measure=false; |
gallonm | 15:b94bc967fecd | 35 | |
mapellil | 30:c8efec21544f | 36 | /* ISR callback function of the sensor_top */ |
gallonm | 15:b94bc967fecd | 37 | void SensorTopIRQ(void) |
gallonm | 9:1d0e839edee8 | 38 | { |
mapellil | 30:c8efec21544f | 39 | int_sensor_top=true; |
gallonm | 14:946e62f44f4f | 40 | board->sensor_top->DisableInterruptMeasureDetectionIRQ(); |
gallonm | 9:1d0e839edee8 | 41 | } |
gallonm | 9:1d0e839edee8 | 42 | |
mapellil | 30:c8efec21544f | 43 | /* ISR callback function of the user blue button to stop program */ |
gallonm | 15:b94bc967fecd | 44 | void StopMeasureIRQ(void) |
gallonm | 15:b94bc967fecd | 45 | { |
mapellil | 30:c8efec21544f | 46 | int_stop_measure=true; |
gallonm | 15:b94bc967fecd | 47 | } |
gallonm | 14:946e62f44f4f | 48 | |
mapellil | 30:c8efec21544f | 49 | /* On board 4 digit local display refresh */ |
gallonm | 19:394c9a4a3046 | 50 | void DisplayRefresh(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 51 | { |
gallonm | 25:2c4710283e02 | 52 | char str[5]; |
gallonm | 19:394c9a4a3046 | 53 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 54 | if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling) |
gallonm | 19:394c9a4a3046 | 55 | { |
gallonm | 19:394c9a4a3046 | 56 | if(data_sensor_top.range_mm!=0xFFFFFFFF) |
gallonm | 19:394c9a4a3046 | 57 | { |
gallonm | 19:394c9a4a3046 | 58 | sprintf(str,"%d",data_sensor_top.range_mm); |
gallonm | 19:394c9a4a3046 | 59 | } |
gallonm | 24:018a59122fe3 | 60 | else |
gallonm | 22:bb440c864489 | 61 | { |
gallonm | 25:2c4710283e02 | 62 | sprintf(str,"%s","----"); |
gallonm | 24:018a59122fe3 | 63 | } |
gallonm | 19:394c9a4a3046 | 64 | } |
licio.mapelli@st.com | 32:724d2afb0ca2 | 65 | else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling) |
gallonm | 19:394c9a4a3046 | 66 | { |
gallonm | 19:394c9a4a3046 | 67 | if(data_sensor_top.lux!=0xFFFFFFFF) |
gallonm | 19:394c9a4a3046 | 68 | { |
gallonm | 19:394c9a4a3046 | 69 | sprintf(str,"%d",data_sensor_top.lux); |
gallonm | 19:394c9a4a3046 | 70 | } |
gallonm | 24:018a59122fe3 | 71 | else |
gallonm | 22:bb440c864489 | 72 | { |
gallonm | 25:2c4710283e02 | 73 | sprintf(str,"%s","----"); |
gallonm | 24:018a59122fe3 | 74 | } |
mapellil | 30:c8efec21544f | 75 | } |
mapellil | 30:c8efec21544f | 76 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 77 | } |
gallonm | 19:394c9a4a3046 | 78 | |
mapellil | 30:c8efec21544f | 79 | /* On board red slider position check */ |
licio.mapelli@st.com | 32:724d2afb0ca2 | 80 | enum OpModeIntPoll_t{ PollMeasure, IntMeasure }; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 81 | |
licio.mapelli@st.com | 33:b903a0ec8803 | 82 | OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) { |
licio.mapelli@st.com | 33:b903a0ec8803 | 83 | |
licio.mapelli@st.com | 33:b903a0ec8803 | 84 | OperatingMode ret; |
licio.mapelli@st.com | 33:b903a0ec8803 | 85 | int measure= board->RdSwitch(); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 86 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 87 | switch (OpMode) { |
licio.mapelli@st.com | 32:724d2afb0ca2 | 88 | case PollMeasure: |
licio.mapelli@st.com | 32:724d2afb0ca2 | 89 | if(measure==RANGE) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 90 | ret = range_continuous_polling; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 91 | else if(measure==ALS) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 92 | ret = als_continuous_polling; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 93 | break; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 94 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 95 | case IntMeasure: |
licio.mapelli@st.com | 32:724d2afb0ca2 | 96 | if(measure==RANGE) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 97 | ret = range_continuous_interrupt; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 98 | else if(measure==ALS) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 99 | ret = als_continuous_interrupt; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 100 | break; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 101 | } |
licio.mapelli@st.com | 33:b903a0ec8803 | 102 | return ret; |
gallonm | 27:2afb9baf718f | 103 | } |
gallonm | 27:2afb9baf718f | 104 | |
mapellil | 30:c8efec21544f | 105 | /* Print on USB Serial the started OperatingMode */ |
mapellil | 30:c8efec21544f | 106 | void PrintStartMessage(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 107 | { |
gallonm | 27:2afb9baf718f | 108 | if(op_mode==range_continuous_interrupt) |
gallonm | 27:2afb9baf718f | 109 | printf("\nStarted range continuous interrupt measure\n\r"); |
gallonm | 27:2afb9baf718f | 110 | else if(prev_operating_mode==als_continuous_interrupt) |
gallonm | 27:2afb9baf718f | 111 | printf("\nStarted als continuous interrupt measure\n\r"); |
gallonm | 27:2afb9baf718f | 112 | } |
gallonm | 27:2afb9baf718f | 113 | |
mapellil | 30:c8efec21544f | 114 | /* Print on USB Serial the stopped OperatingMode */ |
mapellil | 30:c8efec21544f | 115 | void PrintStopMessage(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 116 | { |
gallonm | 27:2afb9baf718f | 117 | if(op_mode==range_continuous_interrupt) |
gallonm | 27:2afb9baf718f | 118 | printf("Stopped range continuous interrupt measure\n\r"); |
gallonm | 27:2afb9baf718f | 119 | else if(prev_operating_mode==als_continuous_interrupt) |
gallonm | 27:2afb9baf718f | 120 | printf("Stopped als continuous interrupt measure\n\r"); |
gallonm | 27:2afb9baf718f | 121 | } |
gallonm | 27:2afb9baf718f | 122 | |
mapellil | 35:8b4a5cc0fb1f | 123 | /* Print on board 4 Digit display the indicated message <= 4 char */ |
mapellil | 31:1aaf6b8b066e | 124 | #define DELAY 2000 // 2Sec |
licio.mapelli@st.com | 32:724d2afb0ca2 | 125 | void DisplayMsg(const char * msg) |
gallonm | 27:2afb9baf718f | 126 | { |
mapellil | 30:c8efec21544f | 127 | Timer timer; |
gallonm | 27:2afb9baf718f | 128 | char str[5]; |
gallonm | 27:2afb9baf718f | 129 | |
mapellil | 30:c8efec21544f | 130 | timer.start(); |
mapellil | 30:c8efec21544f | 131 | for(int i=0; i<DELAY; i=timer.read_ms()) |
gallonm | 27:2afb9baf718f | 132 | { |
licio.mapelli@st.com | 32:724d2afb0ca2 | 133 | sprintf(str,"%s",msg); |
gallonm | 27:2afb9baf718f | 134 | board->display->DisplayString(str, strlen(str)); |
gallonm | 27:2afb9baf718f | 135 | } |
mapellil | 30:c8efec21544f | 136 | timer.stop(); |
gallonm | 27:2afb9baf718f | 137 | } |
gallonm | 27:2afb9baf718f | 138 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 139 | |
mapellil | 39:851cfabf7aa6 | 140 | void IntContinousALSorRangeMeasure (DevI2C *device_i2c) { |
mapellil | 35:8b4a5cc0fb1f | 141 | int status; |
mapellil | 35:8b4a5cc0fb1f | 142 | /* creates the 6180XA1 expansion board singleton obj */ |
mapellil | 39:851cfabf7aa6 | 143 | board=X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 144 | DisplayMsg ("INT"); |
mapellil | 35:8b4a5cc0fb1f | 145 | /* init the 6180XA1 expansion board with default values */ |
gallonm | 10:5319abadb31e | 146 | status=board->InitBoard(); |
gallonm | 14:946e62f44f4f | 147 | if(status) |
gallonm | 27:2afb9baf718f | 148 | printf("Failed to init board!\n\r"); |
mapellil | 35:8b4a5cc0fb1f | 149 | /* check the red slider position for ALS/Range measure */ |
licio.mapelli@st.com | 33:b903a0ec8803 | 150 | operating_mode=CheckSlider(IntMeasure); |
mapellil | 35:8b4a5cc0fb1f | 151 | /* start the measure on sensor top */ |
gallonm | 20:b2e0b41a0e6b | 152 | status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); |
gallonm | 14:946e62f44f4f | 153 | if(!status) |
gallonm | 14:946e62f44f4f | 154 | { |
gallonm | 27:2afb9baf718f | 155 | prev_operating_mode=operating_mode; |
mapellil | 30:c8efec21544f | 156 | PrintStartMessage(operating_mode); |
gallonm | 14:946e62f44f4f | 157 | while(1) |
gallonm | 14:946e62f44f4f | 158 | { |
mapellil | 35:8b4a5cc0fb1f | 159 | if(int_sensor_top) /* 6180 isr was triggered */ |
gallonm | 14:946e62f44f4f | 160 | { |
mapellil | 30:c8efec21544f | 161 | int_sensor_top=false; |
mapellil | 35:8b4a5cc0fb1f | 162 | status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */ |
gallonm | 22:bb440c864489 | 163 | DisplayRefresh(operating_mode); |
gallonm | 14:946e62f44f4f | 164 | } |
mapellil | 35:8b4a5cc0fb1f | 165 | if(int_stop_measure) /* Blue Button isr was triggered */ |
gallonm | 19:394c9a4a3046 | 166 | { |
mapellil | 35:8b4a5cc0fb1f | 167 | status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */ |
gallonm | 27:2afb9baf718f | 168 | if(!status) |
mapellil | 30:c8efec21544f | 169 | PrintStopMessage(prev_operating_mode); |
mapellil | 35:8b4a5cc0fb1f | 170 | int_stop_measure = false; |
gallonm | 19:394c9a4a3046 | 171 | printf("\nProgram stopped!\n\n\r"); |
gallonm | 19:394c9a4a3046 | 172 | break; |
gallonm | 19:394c9a4a3046 | 173 | } |
mapellil | 35:8b4a5cc0fb1f | 174 | operating_mode=CheckSlider(IntMeasure); /* check if red slider was moved */ |
gallonm | 19:394c9a4a3046 | 175 | if(operating_mode!=prev_operating_mode) |
gallonm | 14:946e62f44f4f | 176 | { |
gallonm | 19:394c9a4a3046 | 177 | DisplayRefresh(prev_operating_mode); |
mapellil | 35:8b4a5cc0fb1f | 178 | status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */ |
gallonm | 27:2afb9baf718f | 179 | if(!status) |
mapellil | 30:c8efec21544f | 180 | PrintStopMessage(prev_operating_mode); |
gallonm | 19:394c9a4a3046 | 181 | prev_operating_mode=operating_mode; |
mapellil | 35:8b4a5cc0fb1f | 182 | status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */ |
gallonm | 27:2afb9baf718f | 183 | if(!status) |
mapellil | 30:c8efec21544f | 184 | PrintStartMessage(operating_mode); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 185 | } else |
licio.mapelli@st.com | 32:724d2afb0ca2 | 186 | DisplayRefresh(operating_mode); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 187 | } |
licio.mapelli@st.com | 32:724d2afb0ca2 | 188 | } |
mapellil | 35:8b4a5cc0fb1f | 189 | DisplayMsg("BYE"); |
mapellil | 35:8b4a5cc0fb1f | 190 | delete board; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 191 | } |
licio.mapelli@st.com | 32:724d2afb0ca2 | 192 | |
mapellil | 35:8b4a5cc0fb1f | 193 | /*=================================== Main ================================== |
mapellil | 35:8b4a5cc0fb1f | 194 | Move the VL6180X Expansion board red slider to switch between ALS or Range |
mapellil | 35:8b4a5cc0fb1f | 195 | measures. |
mapellil | 35:8b4a5cc0fb1f | 196 | Press the blue user button to stop the measurements in progress |
mapellil | 35:8b4a5cc0fb1f | 197 | =============================================================================*/ |
mapellil | 35:8b4a5cc0fb1f | 198 | int main() |
mapellil | 35:8b4a5cc0fb1f | 199 | { |
mapellil | 36:0d4aa5b3234d | 200 | #ifdef USER_BUTTON |
mapellil | 35:8b4a5cc0fb1f | 201 | InterruptIn stop_button (USER_BUTTON); |
mapellil | 35:8b4a5cc0fb1f | 202 | stop_button.rise (&StopMeasureIRQ); |
mapellil | 36:0d4aa5b3234d | 203 | #endif |
mapellil | 39:851cfabf7aa6 | 204 | DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
mapellil | 35:8b4a5cc0fb1f | 205 | |
mapellil | 35:8b4a5cc0fb1f | 206 | IntContinousALSorRangeMeasure (device_i2c); // start continous measures Interrupt based |
gallonm | 4:ccd62fd7e137 | 207 | } |
gallonm | 5:fa65d931bd96 | 208 |