Sample Program for Maker Faire Tokyo 2017

Dependencies:   X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Committer:
matsuya
Date:
Thu Jul 27 04:44:31 2017 +0000
Revision:
48:5f6dbaa777ea
Parent:
47:04733a0905ba
Sample Program for Maker Faire Tokyo 2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 47:04733a0905ba 1 /*
Davidroid 47:04733a0905ba 2 This VL6180X Expansion board test application performs a range measurement
Davidroid 47:04733a0905ba 3 and als measurement in polling mode on the onboard embedded top sensor.
Davidroid 47:04733a0905ba 4 The result of both the measures are printed on the serial over.
Davidroid 47:04733a0905ba 5 get_distance() and get_lux() are synchronous! They block the caller until the
Davidroid 47:04733a0905ba 6 result will be ready.
Davidroid 47:04733a0905ba 7 */
Davidroid 47:04733a0905ba 8
Davidroid 47:04733a0905ba 9
Davidroid 47:04733a0905ba 10 /* Includes ------------------------------------------------------------------*/
Davidroid 47:04733a0905ba 11
gallonm 0:83c628a58feb 12 #include "mbed.h"
Davidroid 47:04733a0905ba 13 #include "XNucleo6180XA1.h"
gallonm 4:ccd62fd7e137 14 #include <string.h>
gallonm 4:ccd62fd7e137 15 #include <stdlib.h>
gallonm 4:ccd62fd7e137 16 #include <stdio.h>
gallonm 8:4c05f7a5bb60 17 #include <assert.h>
matsuya 48:5f6dbaa777ea 18 #include <sstream>
Davidroid 47:04733a0905ba 19
Davidroid 47:04733a0905ba 20 /* Definitions ---------------------------------------------------------------*/
gallonm 20:b2e0b41a0e6b 21
matsuya 48:5f6dbaa777ea 22 /*user parameter -------------------------------------------------------------*/
matsuya 48:5f6dbaa777ea 23 #define th 150
matsuya 48:5f6dbaa777ea 24 #define mode 1
matsuya 48:5f6dbaa777ea 25 #define interval 30
matsuya 48:5f6dbaa777ea 26
matsuya 48:5f6dbaa777ea 27 /*board parameter ------------------------------------------------------------*/
mapellil 37:724632fff9c1 28 #define VL6180X_I2C_SDA D14
mapellil 37:724632fff9c1 29 #define VL6180X_I2C_SCL D15
matsuya 48:5f6dbaa777ea 30 //#define EXPANDER_I2C_ADDRESS (0x42*2)
matsuya 48:5f6dbaa777ea 31 //#define RANAGE_MAX (765)
matsuya 48:5f6dbaa777ea 32 #define RANAGE_MAX (500)
matsuya 48:5f6dbaa777ea 33 #define DISTMAX (400)
matsuya 48:5f6dbaa777ea 34 #define OFFSET (-5)
matsuya 48:5f6dbaa777ea 35 #define DISP_TH (DISTMAX+OFFSET)
matsuya 48:5f6dbaa777ea 36 #define LUX_TH (1000)
Davidroid 47:04733a0905ba 37 /* Variables -----------------------------------------------------------------*/
Davidroid 47:04733a0905ba 38
Davidroid 47:04733a0905ba 39 static XNucleo6180XA1 *board = NULL;
Davidroid 47:04733a0905ba 40
matsuya 48:5f6dbaa777ea 41 int dist_scal(int dist);
matsuya 48:5f6dbaa777ea 42 void disp_dist(int dist, XNucleo6180XA1 *board);
matsuya 48:5f6dbaa777ea 43 int lux_scal(int lux);
matsuya 48:5f6dbaa777ea 44 void disp_lux(int dist, XNucleo6180XA1 *board);
matsuya 48:5f6dbaa777ea 45 void lg_dist(int dist, XNucleo6180XA1 *board);
matsuya 48:5f6dbaa777ea 46 void lg_lux(int dist, XNucleo6180XA1 *board);
Davidroid 47:04733a0905ba 47
matsuya 48:5f6dbaa777ea 48 InterruptIn user_button(USER_BUTTON);
matsuya 48:5f6dbaa777ea 49 DigitalOut green_led(LED2);
matsuya 48:5f6dbaa777ea 50 int measure_flg=0;
matsuya 48:5f6dbaa777ea 51 //PortIn p
Davidroid 47:04733a0905ba 52 /* Functions -----------------------------------------------------------------*/
licio.mapelli@st.com 32:724d2afb0ca2 53
mapellil 35:8b4a5cc0fb1f 54 /*=================================== Main ==================================
mapellil 44:3c68d5aa842e 55 Prints on the serial over USB the measured distance and lux.
mapellil 44:3c68d5aa842e 56 The measures are run in single shot polling mode.
mapellil 35:8b4a5cc0fb1f 57 =============================================================================*/
matsuya 48:5f6dbaa777ea 58 void button_pressed()
matsuya 48:5f6dbaa777ea 59 {
matsuya 48:5f6dbaa777ea 60 green_led = 1;
matsuya 48:5f6dbaa777ea 61 }
matsuya 48:5f6dbaa777ea 62
matsuya 48:5f6dbaa777ea 63 void button_released()
matsuya 48:5f6dbaa777ea 64 {
matsuya 48:5f6dbaa777ea 65 green_led = 0;
matsuya 48:5f6dbaa777ea 66 measure_flg=1;
matsuya 48:5f6dbaa777ea 67 }
matsuya 48:5f6dbaa777ea 68
mapellil 35:8b4a5cc0fb1f 69 int main()
mapellil 43:f03152407731 70 {
Davidroid 47:04733a0905ba 71 int status;
matsuya 48:5f6dbaa777ea 72 uint32_t lux=0, dist=765;
matsuya 48:5f6dbaa777ea 73 uint32_t sdist, slux;
matsuya 48:5f6dbaa777ea 74 int mflg=0;
matsuya 48:5f6dbaa777ea 75
Davidroid 47:04733a0905ba 76 DevI2C *device_i2c = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
Davidroid 47:04733a0905ba 77
Davidroid 47:04733a0905ba 78 /* Creates the 6180XA1 expansion board singleton obj. */
Davidroid 47:04733a0905ba 79 board = XNucleo6180XA1::instance(device_i2c, A3, A2, D13, D2);
matsuya 48:5f6dbaa777ea 80 user_button.rise(&button_pressed);
matsuya 48:5f6dbaa777ea 81 user_button.fall(&button_released);
Davidroid 47:04733a0905ba 82
Davidroid 47:04733a0905ba 83 /* Initializes the 6180XA1 expansion board with default values. */
Davidroid 47:04733a0905ba 84 status = board->init_board();
Davidroid 47:04733a0905ba 85 if (status) {
Davidroid 47:04733a0905ba 86 printf("Failed to init board!\n\r");
Davidroid 47:04733a0905ba 87 return 0;
Davidroid 47:04733a0905ba 88 }
matsuya 48:5f6dbaa777ea 89 #if mode == 1
matsuya 48:5f6dbaa777ea 90 while (true) {
matsuya 48:5f6dbaa777ea 91 mflg=board->the_switch->rd_switch();
matsuya 48:5f6dbaa777ea 92 if(mflg==0){
matsuya 48:5f6dbaa777ea 93 board->sensor_top->get_distance(&dist);
matsuya 48:5f6dbaa777ea 94 sdist=dist_scal(dist);
matsuya 48:5f6dbaa777ea 95 disp_dist(sdist, board);
matsuya 48:5f6dbaa777ea 96 }else{
matsuya 48:5f6dbaa777ea 97 board->sensor_top->get_lux(&lux);
matsuya 48:5f6dbaa777ea 98 slux=lux_scal(lux);
matsuya 48:5f6dbaa777ea 99 disp_lux(slux, board);
matsuya 48:5f6dbaa777ea 100 }
matsuya 48:5f6dbaa777ea 101 //printf ("Distance: %d, Lux: %d\n\r", dist, lux);
matsuya 48:5f6dbaa777ea 102 //printf ("Distance: %d\n\r", dist);
matsuya 48:5f6dbaa777ea 103 //wait_ms(60);
matsuya 48:5f6dbaa777ea 104 }
matsuya 48:5f6dbaa777ea 105 #endif
matsuya 48:5f6dbaa777ea 106 #if mode == 2
matsuya 48:5f6dbaa777ea 107 while (true) {
matsuya 48:5f6dbaa777ea 108 mflg=board->the_switch->rd_switch();
matsuya 48:5f6dbaa777ea 109 if(mflg==0){
matsuya 48:5f6dbaa777ea 110 if(measure_flg){
matsuya 48:5f6dbaa777ea 111 board->sensor_top->get_distance(&dist);
matsuya 48:5f6dbaa777ea 112 sdist=dist_scal(dist);
matsuya 48:5f6dbaa777ea 113 disp_dist(sdist, board);
matsuya 48:5f6dbaa777ea 114 measure_flg=0;
matsuya 48:5f6dbaa777ea 115 }else{
matsuya 48:5f6dbaa777ea 116 disp_dist(sdist, board);
matsuya 48:5f6dbaa777ea 117 }
matsuya 48:5f6dbaa777ea 118 }else {
matsuya 48:5f6dbaa777ea 119 if(measure_flg){
matsuya 48:5f6dbaa777ea 120 board->sensor_top->get_lux(&lux);
matsuya 48:5f6dbaa777ea 121 slux=lux_scal(lux);
matsuya 48:5f6dbaa777ea 122 disp_lux(slux, board);
matsuya 48:5f6dbaa777ea 123 measure_flg=0;
matsuya 48:5f6dbaa777ea 124 }else{
matsuya 48:5f6dbaa777ea 125 disp_lux(lux_scal(lux), board);
matsuya 48:5f6dbaa777ea 126 }
matsuya 48:5f6dbaa777ea 127 }
matsuya 48:5f6dbaa777ea 128 }
matsuya 48:5f6dbaa777ea 129 #endif
matsuya 48:5f6dbaa777ea 130 #if mode == 3
Davidroid 47:04733a0905ba 131 while (true) {
matsuya 48:5f6dbaa777ea 132 mflg=board->the_switch->rd_switch();
matsuya 48:5f6dbaa777ea 133 if(mflg==0){
matsuya 48:5f6dbaa777ea 134 board->sensor_top->get_distance(&dist);
matsuya 48:5f6dbaa777ea 135 sdist=dist_scal(dist);
matsuya 48:5f6dbaa777ea 136 if(sdist<th){
matsuya 48:5f6dbaa777ea 137 lg_dist(sdist, board);
matsuya 48:5f6dbaa777ea 138 }else{
matsuya 48:5f6dbaa777ea 139 disp_dist(DISP_TH, board);
matsuya 48:5f6dbaa777ea 140 }
matsuya 48:5f6dbaa777ea 141 }else{
matsuya 48:5f6dbaa777ea 142 board->sensor_top->get_lux(&lux);
matsuya 48:5f6dbaa777ea 143 slux=lux_scal(lux);
matsuya 48:5f6dbaa777ea 144 if(slux<th){
matsuya 48:5f6dbaa777ea 145 lg_lux(slux, board);
matsuya 48:5f6dbaa777ea 146 }else{
matsuya 48:5f6dbaa777ea 147 disp_lux(LUX_TH, board);
matsuya 48:5f6dbaa777ea 148 }
matsuya 48:5f6dbaa777ea 149 }
Davidroid 47:04733a0905ba 150 }
matsuya 48:5f6dbaa777ea 151 #endif
matsuya 48:5f6dbaa777ea 152
gallonm 4:ccd62fd7e137 153 }
matsuya 48:5f6dbaa777ea 154
matsuya 48:5f6dbaa777ea 155 int dist_scal(int dist){
matsuya 48:5f6dbaa777ea 156 return (dist*DISTMAX)/RANAGE_MAX + OFFSET;
matsuya 48:5f6dbaa777ea 157 }
matsuya 48:5f6dbaa777ea 158
matsuya 48:5f6dbaa777ea 159 void disp_dist(int dist, XNucleo6180XA1 *board){
matsuya 48:5f6dbaa777ea 160 char val[4] = {'R', ' ', ' ', ' '};
matsuya 48:5f6dbaa777ea 161 int i;
matsuya 48:5f6dbaa777ea 162
matsuya 48:5f6dbaa777ea 163 if(dist<DISP_TH && dist>0){
matsuya 48:5f6dbaa777ea 164 if(dist/100) sprintf(&val[1], "%d", dist);
matsuya 48:5f6dbaa777ea 165 else if(dist/10) sprintf(&val[2], "%d", dist);
matsuya 48:5f6dbaa777ea 166 else sprintf(&val[3], "%d", dist);
matsuya 48:5f6dbaa777ea 167 }else if (dist < 0) val[3]='0';
matsuya 48:5f6dbaa777ea 168
matsuya 48:5f6dbaa777ea 169 if(mode != 1) board->display->display_string(val, 4);
matsuya 48:5f6dbaa777ea 170 else {
matsuya 48:5f6dbaa777ea 171 for(i=0;i<5;i++){
matsuya 48:5f6dbaa777ea 172 board->display->display_string(val, 4);
matsuya 48:5f6dbaa777ea 173 }
matsuya 48:5f6dbaa777ea 174 }
matsuya 48:5f6dbaa777ea 175 }
matsuya 48:5f6dbaa777ea 176
matsuya 48:5f6dbaa777ea 177 int lux_scal(int lux){
matsuya 48:5f6dbaa777ea 178 return lux;
matsuya 48:5f6dbaa777ea 179 }
matsuya 48:5f6dbaa777ea 180
matsuya 48:5f6dbaa777ea 181 void disp_lux(int lux, XNucleo6180XA1 *board){
matsuya 48:5f6dbaa777ea 182 char val[4] = {'A', ' ', ' ', ' '};
matsuya 48:5f6dbaa777ea 183 int i;
matsuya 48:5f6dbaa777ea 184
matsuya 48:5f6dbaa777ea 185 if(lux<LUX_TH){
matsuya 48:5f6dbaa777ea 186 if(lux/100) sprintf(&val[1], "%d", lux);
matsuya 48:5f6dbaa777ea 187 else if(lux/10) sprintf(&val[2], "%d", lux);
matsuya 48:5f6dbaa777ea 188 else sprintf(&val[3], "%d", lux);
matsuya 48:5f6dbaa777ea 189 }
matsuya 48:5f6dbaa777ea 190
matsuya 48:5f6dbaa777ea 191 if(mode != 1) board->display->display_string(val, 4);
matsuya 48:5f6dbaa777ea 192 else {
matsuya 48:5f6dbaa777ea 193 for(i=0;i<40;i++){
matsuya 48:5f6dbaa777ea 194 board->display->display_string(val, 4);
matsuya 48:5f6dbaa777ea 195 }
matsuya 48:5f6dbaa777ea 196 }
matsuya 48:5f6dbaa777ea 197 }
matsuya 48:5f6dbaa777ea 198
matsuya 48:5f6dbaa777ea 199 void lg_dist(int dist, XNucleo6180XA1 *board){
matsuya 48:5f6dbaa777ea 200 int i,j;
matsuya 48:5f6dbaa777ea 201
matsuya 48:5f6dbaa777ea 202 for(j=0;j<5;j++){
matsuya 48:5f6dbaa777ea 203 for(i=0;i<interval;i++){
matsuya 48:5f6dbaa777ea 204 disp_dist(dist, board);
matsuya 48:5f6dbaa777ea 205 }
matsuya 48:5f6dbaa777ea 206 for(i=0;i<interval;i++){
matsuya 48:5f6dbaa777ea 207 wait_ms(10);
matsuya 48:5f6dbaa777ea 208 }
matsuya 48:5f6dbaa777ea 209 }
matsuya 48:5f6dbaa777ea 210
matsuya 48:5f6dbaa777ea 211 }
matsuya 48:5f6dbaa777ea 212
matsuya 48:5f6dbaa777ea 213 void lg_lux(int lux, XNucleo6180XA1 *board){
matsuya 48:5f6dbaa777ea 214 int i,j;
matsuya 48:5f6dbaa777ea 215
matsuya 48:5f6dbaa777ea 216 for(j=0;j<5;j++){
matsuya 48:5f6dbaa777ea 217 for(i=0;i<interval;i++){
matsuya 48:5f6dbaa777ea 218 disp_lux(lux, board);
matsuya 48:5f6dbaa777ea 219 }
matsuya 48:5f6dbaa777ea 220 for(i=0;i<interval;i++){
matsuya 48:5f6dbaa777ea 221 wait_ms(10);
matsuya 48:5f6dbaa777ea 222 }
matsuya 48:5f6dbaa777ea 223 }
matsuya 48:5f6dbaa777ea 224
matsuya 48:5f6dbaa777ea 225 }