Librairie adaptée au laboratoire 2

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Committer:
davide.aliprandi@st.com
Date:
Mon Mar 13 17:57:03 2017 +0100
Revision:
56:37d1736bd896
Aligning to ARM mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davide.aliprandi@st.com 56:37d1736bd896 1 /**
davide.aliprandi@st.com 56:37d1736bd896 2 ******************************************************************************
davide.aliprandi@st.com 56:37d1736bd896 3 * @file XNucleo6180xa1.cpp
davide.aliprandi@st.com 56:37d1736bd896 4 * @author AST / EST
davide.aliprandi@st.com 56:37d1736bd896 5 * @version V0.0.1
davide.aliprandi@st.com 56:37d1736bd896 6 * @date 13-April-2015
davide.aliprandi@st.com 56:37d1736bd896 7 * @brief Implementation file for the X_NUCLEO_VL6180XA1 singleton class
davide.aliprandi@st.com 56:37d1736bd896 8 ******************************************************************************
davide.aliprandi@st.com 56:37d1736bd896 9 * @attention
davide.aliprandi@st.com 56:37d1736bd896 10 *
davide.aliprandi@st.com 56:37d1736bd896 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
davide.aliprandi@st.com 56:37d1736bd896 12 *
davide.aliprandi@st.com 56:37d1736bd896 13 * Redistribution and use in source and binary forms, with or without modification,
davide.aliprandi@st.com 56:37d1736bd896 14 * are permitted provided that the following conditions are met:
davide.aliprandi@st.com 56:37d1736bd896 15 * 1. Redistributions of source code must retain the above copyright notice,
davide.aliprandi@st.com 56:37d1736bd896 16 * this list of conditions and the following disclaimer.
davide.aliprandi@st.com 56:37d1736bd896 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
davide.aliprandi@st.com 56:37d1736bd896 18 * this list of conditions and the following disclaimer in the documentation
davide.aliprandi@st.com 56:37d1736bd896 19 * and/or other materials provided with the distribution.
davide.aliprandi@st.com 56:37d1736bd896 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
davide.aliprandi@st.com 56:37d1736bd896 21 * may be used to endorse or promote products derived from this software
davide.aliprandi@st.com 56:37d1736bd896 22 * without specific prior written permission.
davide.aliprandi@st.com 56:37d1736bd896 23 *
davide.aliprandi@st.com 56:37d1736bd896 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
davide.aliprandi@st.com 56:37d1736bd896 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
davide.aliprandi@st.com 56:37d1736bd896 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
davide.aliprandi@st.com 56:37d1736bd896 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
davide.aliprandi@st.com 56:37d1736bd896 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
davide.aliprandi@st.com 56:37d1736bd896 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
davide.aliprandi@st.com 56:37d1736bd896 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
davide.aliprandi@st.com 56:37d1736bd896 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
davide.aliprandi@st.com 56:37d1736bd896 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
davide.aliprandi@st.com 56:37d1736bd896 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
davide.aliprandi@st.com 56:37d1736bd896 34 *
davide.aliprandi@st.com 56:37d1736bd896 35 ******************************************************************************
davide.aliprandi@st.com 56:37d1736bd896 36 */
davide.aliprandi@st.com 56:37d1736bd896 37
davide.aliprandi@st.com 56:37d1736bd896 38
davide.aliprandi@st.com 56:37d1736bd896 39 /* Includes ------------------------------------------------------------------*/
davide.aliprandi@st.com 56:37d1736bd896 40
davide.aliprandi@st.com 56:37d1736bd896 41 #include "XNucleo6180xa1.h"
davide.aliprandi@st.com 56:37d1736bd896 42
davide.aliprandi@st.com 56:37d1736bd896 43
davide.aliprandi@st.com 56:37d1736bd896 44 /* Static variables ----------------------------------------------------------*/
davide.aliprandi@st.com 56:37d1736bd896 45
davide.aliprandi@st.com 56:37d1736bd896 46 XNucleo6180xa1 *XNucleo6180xa1::_instance = NULL;
davide.aliprandi@st.com 56:37d1736bd896 47
davide.aliprandi@st.com 56:37d1736bd896 48
davide.aliprandi@st.com 56:37d1736bd896 49 /* Methods -------------------------------------------------------------------*/
davide.aliprandi@st.com 56:37d1736bd896 50
davide.aliprandi@st.com 56:37d1736bd896 51 XNucleo6180xa1 *XNucleo6180xa1::instance(DevI2C *ext_i2c)
davide.aliprandi@st.com 56:37d1736bd896 52 {
davide.aliprandi@st.com 56:37d1736bd896 53 if (_instance==NULL) {
davide.aliprandi@st.com 56:37d1736bd896 54 _instance=new X_NUCLEO_6180XA1(ext_i2c);
davide.aliprandi@st.com 56:37d1736bd896 55 } else {
davide.aliprandi@st.com 56:37d1736bd896 56 VL6180x_ErrLog("Failed to create X_NUCLEO_6180XA1 instance\n\r");
davide.aliprandi@st.com 56:37d1736bd896 57 }
davide.aliprandi@st.com 56:37d1736bd896 58 return _instance;
davide.aliprandi@st.com 56:37d1736bd896 59 }
davide.aliprandi@st.com 56:37d1736bd896 60
davide.aliprandi@st.com 56:37d1736bd896 61 XNucleo6180xa1 *XNucleo6180xa1::instance(DevI2C *ext_i2c,
davide.aliprandi@st.com 56:37d1736bd896 62 PinName gpio1_top, PinName gpio1_bottom,
davide.aliprandi@st.com 56:37d1736bd896 63 PinName gpio1_left, PinName gpio1_right)
davide.aliprandi@st.com 56:37d1736bd896 64 {
davide.aliprandi@st.com 56:37d1736bd896 65 if (_instance==NULL) {
davide.aliprandi@st.com 56:37d1736bd896 66 //_instance=new X_NUCLEO_6180XA1(ext_i2c);
davide.aliprandi@st.com 56:37d1736bd896 67 _instance=new X_NUCLEO_6180XA1(ext_i2c, gpio1_top, gpio1_bottom, gpio1_left, gpio1_right);
davide.aliprandi@st.com 56:37d1736bd896 68 } else {
davide.aliprandi@st.com 56:37d1736bd896 69 VL6180x_ErrLog("Failed to create X_NUCLEO_6180XA1 instance\n\r");
davide.aliprandi@st.com 56:37d1736bd896 70 }
davide.aliprandi@st.com 56:37d1736bd896 71 return _instance;
davide.aliprandi@st.com 56:37d1736bd896 72 }
davide.aliprandi@st.com 56:37d1736bd896 73
davide.aliprandi@st.com 56:37d1736bd896 74 int XNucleo6180xa1::init_board()
davide.aliprandi@st.com 56:37d1736bd896 75 {
davide.aliprandi@st.com 56:37d1736bd896 76 int status, n_dev=0; uint8_t sensor_address;
davide.aliprandi@st.com 56:37d1736bd896 77
davide.aliprandi@st.com 56:37d1736bd896 78 if (sensor_top) {
davide.aliprandi@st.com 56:37d1736bd896 79 sensor_top->VL6180x_Off();
davide.aliprandi@st.com 56:37d1736bd896 80 }
davide.aliprandi@st.com 56:37d1736bd896 81 if (sensor_bottom) {
davide.aliprandi@st.com 56:37d1736bd896 82 sensor_bottom->VL6180x_Off();
davide.aliprandi@st.com 56:37d1736bd896 83 }
davide.aliprandi@st.com 56:37d1736bd896 84 if (sensor_left) {
davide.aliprandi@st.com 56:37d1736bd896 85 sensor_left->VL6180x_Off();
davide.aliprandi@st.com 56:37d1736bd896 86 }
davide.aliprandi@st.com 56:37d1736bd896 87 if (sensor_right) {
davide.aliprandi@st.com 56:37d1736bd896 88 sensor_right->VL6180x_Off();
davide.aliprandi@st.com 56:37d1736bd896 89 }
davide.aliprandi@st.com 56:37d1736bd896 90
davide.aliprandi@st.com 56:37d1736bd896 91 sensor_address = NEW_SENSOR_TOP_ADDRESS;
davide.aliprandi@st.com 56:37d1736bd896 92 status=sensor_top->init(&sensor_address);
davide.aliprandi@st.com 56:37d1736bd896 93 if (status) {
davide.aliprandi@st.com 56:37d1736bd896 94 printf("Error: Mandatory top sensor fail, Init failed!\n\r");
davide.aliprandi@st.com 56:37d1736bd896 95 if (sensor_top !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 96 delete sensor_top;
davide.aliprandi@st.com 56:37d1736bd896 97 sensor_top=NULL;
davide.aliprandi@st.com 56:37d1736bd896 98 }
davide.aliprandi@st.com 56:37d1736bd896 99 if (sensor_left !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 100 delete sensor_left;
davide.aliprandi@st.com 56:37d1736bd896 101 sensor_left=NULL;
davide.aliprandi@st.com 56:37d1736bd896 102 }
davide.aliprandi@st.com 56:37d1736bd896 103 if (sensor_bottom !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 104 delete sensor_bottom;
davide.aliprandi@st.com 56:37d1736bd896 105 sensor_bottom=NULL;
davide.aliprandi@st.com 56:37d1736bd896 106 }
davide.aliprandi@st.com 56:37d1736bd896 107 if (sensor_right !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 108 delete sensor_right;
davide.aliprandi@st.com 56:37d1736bd896 109 sensor_right=NULL;
davide.aliprandi@st.com 56:37d1736bd896 110 }
davide.aliprandi@st.com 56:37d1736bd896 111 n_dev=0;
davide.aliprandi@st.com 56:37d1736bd896 112 return 1;
davide.aliprandi@st.com 56:37d1736bd896 113 } else {
davide.aliprandi@st.com 56:37d1736bd896 114 printf("Sensor top present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 115 n_dev++;
davide.aliprandi@st.com 56:37d1736bd896 116 }
davide.aliprandi@st.com 56:37d1736bd896 117
davide.aliprandi@st.com 56:37d1736bd896 118 sensor_address = NEW_SENSOR_BOTTOM_ADDRESS;
davide.aliprandi@st.com 56:37d1736bd896 119 status=1;
davide.aliprandi@st.com 56:37d1736bd896 120 if (sensor_bottom) {
davide.aliprandi@st.com 56:37d1736bd896 121 status=sensor_bottom->init(&sensor_address);
davide.aliprandi@st.com 56:37d1736bd896 122 }
davide.aliprandi@st.com 56:37d1736bd896 123 if (status)
davide.aliprandi@st.com 56:37d1736bd896 124 {
davide.aliprandi@st.com 56:37d1736bd896 125 printf("Sensor bottom not present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 126 if (sensor_bottom !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 127 delete sensor_bottom;
davide.aliprandi@st.com 56:37d1736bd896 128 sensor_bottom=NULL;
davide.aliprandi@st.com 56:37d1736bd896 129 }
davide.aliprandi@st.com 56:37d1736bd896 130 } else {
davide.aliprandi@st.com 56:37d1736bd896 131 printf("Sensor bottom present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 132 n_dev++;
davide.aliprandi@st.com 56:37d1736bd896 133 }
davide.aliprandi@st.com 56:37d1736bd896 134
davide.aliprandi@st.com 56:37d1736bd896 135 sensor_address = NEW_SENSOR_LEFT_ADDRESS;
davide.aliprandi@st.com 56:37d1736bd896 136 status=1;
davide.aliprandi@st.com 56:37d1736bd896 137 if (sensor_left) {
davide.aliprandi@st.com 56:37d1736bd896 138 status=sensor_left->init(&sensor_address);
davide.aliprandi@st.com 56:37d1736bd896 139 }
davide.aliprandi@st.com 56:37d1736bd896 140 if (status) {
davide.aliprandi@st.com 56:37d1736bd896 141 printf("Sensor left not present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 142 if (sensor_left !=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 143 delete sensor_left;
davide.aliprandi@st.com 56:37d1736bd896 144 sensor_left=NULL;
davide.aliprandi@st.com 56:37d1736bd896 145 }
davide.aliprandi@st.com 56:37d1736bd896 146 } else {
davide.aliprandi@st.com 56:37d1736bd896 147 printf("Sensor left present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 148 n_dev++;
davide.aliprandi@st.com 56:37d1736bd896 149 }
davide.aliprandi@st.com 56:37d1736bd896 150
davide.aliprandi@st.com 56:37d1736bd896 151 sensor_address = NEW_SENSOR_RIGHT_ADDRESS;
davide.aliprandi@st.com 56:37d1736bd896 152 status=1;
davide.aliprandi@st.com 56:37d1736bd896 153 if (sensor_right) {
davide.aliprandi@st.com 56:37d1736bd896 154 status=sensor_right->init(&sensor_address);
davide.aliprandi@st.com 56:37d1736bd896 155 }
davide.aliprandi@st.com 56:37d1736bd896 156 if (status) {
davide.aliprandi@st.com 56:37d1736bd896 157 printf("Sensor right not present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 158 if (sensor_right!=NULL) {
davide.aliprandi@st.com 56:37d1736bd896 159 delete sensor_right;
davide.aliprandi@st.com 56:37d1736bd896 160 sensor_right=NULL;
davide.aliprandi@st.com 56:37d1736bd896 161 }
davide.aliprandi@st.com 56:37d1736bd896 162 } else {
davide.aliprandi@st.com 56:37d1736bd896 163 printf("Sensor right present\n\r");
davide.aliprandi@st.com 56:37d1736bd896 164 n_dev++;
davide.aliprandi@st.com 56:37d1736bd896 165 }
davide.aliprandi@st.com 56:37d1736bd896 166
davide.aliprandi@st.com 56:37d1736bd896 167 if (n_dev==0) {
davide.aliprandi@st.com 56:37d1736bd896 168 return 1;
davide.aliprandi@st.com 56:37d1736bd896 169 } else {
davide.aliprandi@st.com 56:37d1736bd896 170 return 0;
davide.aliprandi@st.com 56:37d1736bd896 171 }
davide.aliprandi@st.com 56:37d1736bd896 172 }