Librairie adaptée au laboratoire 2

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers XNucleo6180XA1.cpp Source File

XNucleo6180XA1.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    XNucleo6180XA1.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    13-April-2015
00007  * @brief   Implementation file for the X_NUCLEO_VL6180XA1 singleton class
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 
00041 #include "XNucleo6180XA1.h"
00042 
00043 
00044 /* Static variables ----------------------------------------------------------*/
00045 
00046 XNucleo6180XA1 *XNucleo6180XA1::_instance = NULL;
00047 
00048 
00049 /* Methods -------------------------------------------------------------------*/
00050 
00051 XNucleo6180XA1 *XNucleo6180XA1::instance(DevI2C *ext_i2c)
00052 {
00053     if (_instance==NULL) {
00054         _instance=new XNucleo6180XA1(ext_i2c);
00055     } else {
00056         VL6180X_ErrLog("Failed to create X_NUCLEO_6180XA1 instance\n\r");
00057     }
00058     return _instance;
00059 }
00060 
00061 XNucleo6180XA1 *XNucleo6180XA1::instance(DevI2C *ext_i2c, 
00062                                          PinName gpio1_top, PinName gpio1_bottom,
00063                                          PinName gpio1_left, PinName gpio1_right)
00064 {
00065     if (_instance==NULL) {
00066         //_instance=new XNucleo6180XA1(ext_i2c);
00067         _instance=new XNucleo6180XA1(ext_i2c, gpio1_top, gpio1_bottom, gpio1_left, gpio1_right);
00068     } else {
00069         VL6180X_ErrLog("Failed to create X_NUCLEO_6180XA1 instance\n\r");
00070     }
00071     return _instance;
00072 }
00073 
00074 int XNucleo6180XA1::init_board()
00075 {   
00076     int status, n_dev=0; uint8_t sensor_address;
00077 
00078     if (sensor_top) {
00079         sensor_top->off();
00080     }
00081     if (sensor_bottom) {
00082         sensor_bottom->off();
00083     }
00084     if (sensor_left) {
00085         sensor_left->off();
00086     }
00087     if (sensor_right) {
00088         sensor_right->off();
00089     }
00090 
00091     sensor_address = NEW_SENSOR_TOP_ADDRESS;
00092     status=sensor_top->init(&sensor_address);
00093     if (status) {
00094         printf("Error: Mandatory top sensor fail, Init failed!\n\r");
00095         if (sensor_top !=NULL) {
00096             delete sensor_top;
00097             sensor_top=NULL;
00098         }
00099         if (sensor_left !=NULL) {
00100             delete sensor_left;
00101             sensor_left=NULL;
00102         }
00103         if (sensor_bottom !=NULL) {
00104             delete sensor_bottom;
00105             sensor_bottom=NULL;
00106         }
00107         if (sensor_right !=NULL) {
00108             delete sensor_right;
00109             sensor_right=NULL;
00110         }
00111         n_dev=0;
00112         return 1;
00113     } else {
00114         printf("Sensor top present\n\r");
00115         n_dev++;
00116     }
00117 
00118     sensor_address = NEW_SENSOR_BOTTOM_ADDRESS;
00119     status=1;
00120     if (sensor_bottom) {
00121         status=sensor_bottom->init(&sensor_address);
00122     }
00123     if (status)
00124     {
00125         printf("Sensor bottom not present\n\r");
00126         if (sensor_bottom !=NULL) {
00127             delete sensor_bottom;
00128             sensor_bottom=NULL;
00129         }
00130     } else {
00131         printf("Sensor bottom present\n\r");
00132         n_dev++;
00133     }
00134 
00135     sensor_address = NEW_SENSOR_LEFT_ADDRESS;
00136     status=1;
00137     if (sensor_left) {
00138         status=sensor_left->init(&sensor_address);
00139     }
00140     if (status) {
00141         printf("Sensor left not present\n\r");
00142         if (sensor_left !=NULL) {
00143             delete sensor_left;
00144             sensor_left=NULL;
00145         }
00146     } else {
00147         printf("Sensor left present\n\r");
00148         n_dev++;
00149     }
00150 
00151     sensor_address = NEW_SENSOR_RIGHT_ADDRESS;
00152     status=1;
00153     if (sensor_right) {
00154         status=sensor_right->init(&sensor_address);
00155     }
00156     if (status) {
00157         printf("Sensor right not present\n\r");
00158         if (sensor_right!=NULL) {
00159             delete sensor_right;
00160             sensor_right=NULL;
00161         }
00162     } else {
00163         printf("Sensor right present\n\r");
00164         n_dev++;
00165     }
00166 
00167     if (n_dev==0) {
00168         return 1;
00169     } else {
00170         return 0;
00171     }
00172 }