Sample Program for Maker Faire Tokyo 2017
Dependencies: X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1 by
main.cpp@43:f03152407731, 2016-09-12 (annotated)
- Committer:
- mapellil
- Date:
- Mon Sep 12 09:19:46 2016 +0000
- Revision:
- 43:f03152407731
- Parent:
- 42:ee8c5b1f2e1d
- Child:
- 44:3c68d5aa842e
Moved to als/range single shot polling mode measure and updated the 6180 lib.
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 | 43:f03152407731 | 34 | int int_stop_measure=false; |
gallonm | 9:1d0e839edee8 | 35 | |
mapellil | 30:c8efec21544f | 36 | /* ISR callback function of the user blue button to stop program */ |
gallonm | 15:b94bc967fecd | 37 | void StopMeasureIRQ(void) |
gallonm | 15:b94bc967fecd | 38 | { |
mapellil | 30:c8efec21544f | 39 | int_stop_measure=true; |
gallonm | 15:b94bc967fecd | 40 | } |
gallonm | 14:946e62f44f4f | 41 | |
mapellil | 30:c8efec21544f | 42 | /* On board 4 digit local display refresh */ |
gallonm | 19:394c9a4a3046 | 43 | void DisplayRefresh(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 44 | { |
gallonm | 25:2c4710283e02 | 45 | char str[5]; |
gallonm | 19:394c9a4a3046 | 46 | |
mapellil | 43:f03152407731 | 47 | if(op_mode==range_continuous_interrupt || op_mode==range_single_shot_polling) |
gallonm | 19:394c9a4a3046 | 48 | { |
mapellil | 43:f03152407731 | 49 | if(data_sensor_top.range_mm!=255) |
gallonm | 19:394c9a4a3046 | 50 | { |
gallonm | 19:394c9a4a3046 | 51 | sprintf(str,"%d",data_sensor_top.range_mm); |
gallonm | 19:394c9a4a3046 | 52 | } |
gallonm | 24:018a59122fe3 | 53 | else |
gallonm | 22:bb440c864489 | 54 | { |
gallonm | 25:2c4710283e02 | 55 | sprintf(str,"%s","----"); |
gallonm | 24:018a59122fe3 | 56 | } |
gallonm | 19:394c9a4a3046 | 57 | } |
mapellil | 43:f03152407731 | 58 | else if(op_mode==als_continuous_interrupt || op_mode==als_single_shot_polling) |
gallonm | 19:394c9a4a3046 | 59 | { |
gallonm | 19:394c9a4a3046 | 60 | if(data_sensor_top.lux!=0xFFFFFFFF) |
gallonm | 19:394c9a4a3046 | 61 | { |
gallonm | 19:394c9a4a3046 | 62 | sprintf(str,"%d",data_sensor_top.lux); |
gallonm | 19:394c9a4a3046 | 63 | } |
gallonm | 24:018a59122fe3 | 64 | else |
gallonm | 22:bb440c864489 | 65 | { |
gallonm | 25:2c4710283e02 | 66 | sprintf(str,"%s","----"); |
gallonm | 24:018a59122fe3 | 67 | } |
mapellil | 30:c8efec21544f | 68 | } |
mapellil | 30:c8efec21544f | 69 | board->display->DisplayString(str, strlen(str)); |
gallonm | 19:394c9a4a3046 | 70 | } |
gallonm | 19:394c9a4a3046 | 71 | |
mapellil | 30:c8efec21544f | 72 | /* On board red slider position check */ |
licio.mapelli@st.com | 32:724d2afb0ca2 | 73 | enum OpModeIntPoll_t{ PollMeasure, IntMeasure }; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 74 | |
licio.mapelli@st.com | 33:b903a0ec8803 | 75 | OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) { |
licio.mapelli@st.com | 33:b903a0ec8803 | 76 | |
licio.mapelli@st.com | 33:b903a0ec8803 | 77 | OperatingMode ret; |
licio.mapelli@st.com | 33:b903a0ec8803 | 78 | int measure= board->RdSwitch(); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 79 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 80 | switch (OpMode) { |
licio.mapelli@st.com | 32:724d2afb0ca2 | 81 | case PollMeasure: |
licio.mapelli@st.com | 32:724d2afb0ca2 | 82 | if(measure==RANGE) |
mapellil | 43:f03152407731 | 83 | ret = range_single_shot_polling; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 84 | else if(measure==ALS) |
mapellil | 43:f03152407731 | 85 | ret = als_single_shot_polling; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 86 | break; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 87 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 88 | case IntMeasure: |
licio.mapelli@st.com | 32:724d2afb0ca2 | 89 | if(measure==RANGE) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 90 | ret = range_continuous_interrupt; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 91 | else if(measure==ALS) |
licio.mapelli@st.com | 32:724d2afb0ca2 | 92 | ret = als_continuous_interrupt; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 93 | break; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 94 | } |
mapellil | 43:f03152407731 | 95 | return ret; |
gallonm | 27:2afb9baf718f | 96 | } |
gallonm | 27:2afb9baf718f | 97 | |
mapellil | 30:c8efec21544f | 98 | /* Print on USB Serial the started OperatingMode */ |
mapellil | 30:c8efec21544f | 99 | void PrintStartMessage(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 100 | { |
mapellil | 43:f03152407731 | 101 | switch (op_mode) { |
mapellil | 43:f03152407731 | 102 | case range_single_shot_polling: |
mapellil | 43:f03152407731 | 103 | printf("\nStarted range single shot polling measure\n\r"); |
mapellil | 43:f03152407731 | 104 | break; |
mapellil | 43:f03152407731 | 105 | case als_single_shot_polling: |
mapellil | 43:f03152407731 | 106 | printf("\nStarted als single shot polling measure\n\r"); |
mapellil | 43:f03152407731 | 107 | break; |
mapellil | 43:f03152407731 | 108 | case range_continuous_interrupt: |
mapellil | 43:f03152407731 | 109 | printf("\nStarted range continuous interrupt measure\n\r"); |
mapellil | 43:f03152407731 | 110 | break; |
mapellil | 43:f03152407731 | 111 | case als_continuous_interrupt: |
mapellil | 43:f03152407731 | 112 | printf("\nStarted als continuous interrupt measure\n\r"); |
mapellil | 43:f03152407731 | 113 | break; |
mapellil | 43:f03152407731 | 114 | default: |
mapellil | 43:f03152407731 | 115 | break; |
mapellil | 43:f03152407731 | 116 | } |
gallonm | 27:2afb9baf718f | 117 | } |
gallonm | 27:2afb9baf718f | 118 | |
mapellil | 30:c8efec21544f | 119 | /* Print on USB Serial the stopped OperatingMode */ |
mapellil | 30:c8efec21544f | 120 | void PrintStopMessage(OperatingMode op_mode) |
gallonm | 27:2afb9baf718f | 121 | { |
mapellil | 43:f03152407731 | 122 | switch (op_mode) { |
mapellil | 43:f03152407731 | 123 | case range_single_shot_polling: |
mapellil | 43:f03152407731 | 124 | printf("\nStopped range single shot polling measure\n\r"); |
mapellil | 43:f03152407731 | 125 | break; |
mapellil | 43:f03152407731 | 126 | case als_single_shot_polling: |
mapellil | 43:f03152407731 | 127 | printf("\nStopped als single shot polling measure\n\r"); |
mapellil | 43:f03152407731 | 128 | break; |
mapellil | 43:f03152407731 | 129 | case range_continuous_interrupt: |
mapellil | 43:f03152407731 | 130 | printf("\nStopped range continuous interrupt measure\n\r"); |
mapellil | 43:f03152407731 | 131 | break; |
mapellil | 43:f03152407731 | 132 | case als_continuous_interrupt: |
mapellil | 43:f03152407731 | 133 | printf("\nStopped als continuous interrupt measure\n\r"); |
mapellil | 43:f03152407731 | 134 | break; |
mapellil | 43:f03152407731 | 135 | default: |
mapellil | 43:f03152407731 | 136 | break; |
mapellil | 43:f03152407731 | 137 | } |
gallonm | 27:2afb9baf718f | 138 | } |
gallonm | 27:2afb9baf718f | 139 | |
mapellil | 35:8b4a5cc0fb1f | 140 | /* Print on board 4 Digit display the indicated message <= 4 char */ |
mapellil | 31:1aaf6b8b066e | 141 | #define DELAY 2000 // 2Sec |
licio.mapelli@st.com | 32:724d2afb0ca2 | 142 | void DisplayMsg(const char * msg) |
gallonm | 27:2afb9baf718f | 143 | { |
mapellil | 30:c8efec21544f | 144 | Timer timer; |
gallonm | 27:2afb9baf718f | 145 | char str[5]; |
gallonm | 27:2afb9baf718f | 146 | |
mapellil | 30:c8efec21544f | 147 | timer.start(); |
mapellil | 30:c8efec21544f | 148 | for(int i=0; i<DELAY; i=timer.read_ms()) |
gallonm | 27:2afb9baf718f | 149 | { |
licio.mapelli@st.com | 32:724d2afb0ca2 | 150 | sprintf(str,"%s",msg); |
gallonm | 27:2afb9baf718f | 151 | board->display->DisplayString(str, strlen(str)); |
gallonm | 27:2afb9baf718f | 152 | } |
mapellil | 30:c8efec21544f | 153 | timer.stop(); |
gallonm | 27:2afb9baf718f | 154 | } |
gallonm | 27:2afb9baf718f | 155 | |
licio.mapelli@st.com | 32:724d2afb0ca2 | 156 | |
mapellil | 43:f03152407731 | 157 | void PollSingleALSorRangeMeasure (DevI2C *device_i2c) { |
mapellil | 35:8b4a5cc0fb1f | 158 | int status; |
mapellil | 35:8b4a5cc0fb1f | 159 | /* creates the 6180XA1 expansion board singleton obj */ |
mapellil | 39:851cfabf7aa6 | 160 | board=X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2); |
mapellil | 43:f03152407731 | 161 | DisplayMsg ("pOLL"); |
mapellil | 35:8b4a5cc0fb1f | 162 | /* init the 6180XA1 expansion board with default values */ |
gallonm | 10:5319abadb31e | 163 | status=board->InitBoard(); |
mapellil | 43:f03152407731 | 164 | if(status) printf("Failed to init board!\n\r"); |
mapellil | 35:8b4a5cc0fb1f | 165 | /* check the red slider position for ALS/Range measure */ |
mapellil | 43:f03152407731 | 166 | operating_mode=CheckSlider(PollMeasure); |
mapellil | 43:f03152407731 | 167 | PrintStartMessage(operating_mode); |
mapellil | 43:f03152407731 | 168 | while(1) |
gallonm | 14:946e62f44f4f | 169 | { |
mapellil | 43:f03152407731 | 170 | if(int_stop_measure) /* Blue Button isr was triggered */ |
gallonm | 14:946e62f44f4f | 171 | { |
mapellil | 43:f03152407731 | 172 | PrintStopMessage(prev_operating_mode); |
mapellil | 43:f03152407731 | 173 | int_stop_measure = false; |
mapellil | 43:f03152407731 | 174 | printf("\nProgram stopped!\n\n\r"); |
mapellil | 43:f03152407731 | 175 | break; |
licio.mapelli@st.com | 32:724d2afb0ca2 | 176 | } |
mapellil | 43:f03152407731 | 177 | operating_mode=CheckSlider(PollMeasure); /* check if red slider was moved */ |
mapellil | 43:f03152407731 | 178 | if (operating_mode == range_single_shot_polling) { |
mapellil | 43:f03152407731 | 179 | board->sensor_top->GetDistance(&data_sensor_top.range_mm); |
mapellil | 43:f03152407731 | 180 | } else if (operating_mode == als_single_shot_polling) { |
mapellil | 43:f03152407731 | 181 | board->sensor_top->GetLux(&data_sensor_top.lux); |
mapellil | 43:f03152407731 | 182 | } |
mapellil | 43:f03152407731 | 183 | DisplayRefresh(operating_mode); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 184 | } |
mapellil | 35:8b4a5cc0fb1f | 185 | DisplayMsg("BYE"); |
licio.mapelli@st.com | 32:724d2afb0ca2 | 186 | } |
licio.mapelli@st.com | 32:724d2afb0ca2 | 187 | |
mapellil | 35:8b4a5cc0fb1f | 188 | /*=================================== Main ================================== |
mapellil | 35:8b4a5cc0fb1f | 189 | Move the VL6180X Expansion board red slider to switch between ALS or Range |
mapellil | 35:8b4a5cc0fb1f | 190 | measures. |
mapellil | 35:8b4a5cc0fb1f | 191 | Press the blue user button to stop the measurements in progress |
mapellil | 35:8b4a5cc0fb1f | 192 | =============================================================================*/ |
mapellil | 35:8b4a5cc0fb1f | 193 | int main() |
mapellil | 43:f03152407731 | 194 | { |
mapellil | 43:f03152407731 | 195 | |
mapellil | 40:204f84887d70 | 196 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
mapellil | 35:8b4a5cc0fb1f | 197 | InterruptIn stop_button (USER_BUTTON); |
mapellil | 35:8b4a5cc0fb1f | 198 | stop_button.rise (&StopMeasureIRQ); |
mapellil | 36:0d4aa5b3234d | 199 | #endif |
mapellil | 39:851cfabf7aa6 | 200 | DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
mapellil | 35:8b4a5cc0fb1f | 201 | |
mapellil | 43:f03152407731 | 202 | PollSingleALSorRangeMeasure (device_i2c); // start continous measures Interrupt based |
gallonm | 4:ccd62fd7e137 | 203 | } |
gallonm | 5:fa65d931bd96 | 204 |