SDMP_IOT / Mbed OS AdiSense1000_SmartBabySeat

Fork of Babyseat_NewFirmware_copy_sean by Ross O'Halloran

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /*
00002 CONFIDENTIAL AND PROPRIETARY INFORMATION
00003 
00004 Copyright (c) 2018 Emutex Ltd. All rights reserved.
00005 This software and documentation contain confidential and
00006 proprietary information that is the property of
00007 Emutex Ltd. The software and documentation are
00008 furnished under a license agreement and may be used
00009 or copied only in accordance with the terms of the license
00010 agreement. No part of the software and documentation
00011 may be reproduced, transmitted, or translated, in any
00012 form or by any means, electronic, mechanical, manual,
00013 optical, or otherwise, without prior written permission
00014 of Emutex Ltd., or as expressly provided by the license agreement.
00015 Reverse engineering is prohibited, and reproduction,
00016 disclosure or use without specific written authorization
00017 of Emutex Ltd. is strictly forbidden.
00018  *
00019 Copyright 2017 (c) Analog Devices, Inc.
00020 
00021 All rights reserved.
00022 
00023 Redistribution and use in source and binary forms, with or without
00024 modification, are permitted provided that the following conditions are met:
00025   - Redistributions of source code must retain the above copyright
00026     notice, this list of conditions and the following disclaimer.
00027   - Redistributions in binary form must reproduce the above copyright
00028     notice, this list of conditions and the following disclaimer in
00029     the documentation and/or other materials provided with the
00030     distribution.
00031   - Neither the name of Analog Devices, Inc. nor the names of its
00032     contributors may be used to endorse or promote products derived
00033     from this software without specific prior written permission.
00034   - The use of this software may or may not infringe the patent rights
00035     of one or more patent holders. This license does not release you
00036     from the requirement that you obtain separate licenses from these
00037     patent holders to use this software.
00038   - Use of the software either in source or binary form, must be run
00039     on or directly connected to an Analog Devices Inc. component.
00040 
00041 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00042 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00043 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00044 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00045 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00046 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00047 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00048 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00049 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00050 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00051  *
00052  *****************************************************************************/
00053 /*!
00054  ******************************************************************************
00055  * @file:
00056  *-----------------------------------------------------------------------------
00057  *
00058  */
00059 #include "mbed.h"
00060 #include "inc/adi_sense_api.h"
00061 #include "inc/adi_sense_1000/adi_sense_1000_api.h"
00062 #include "inc/adi_sense_log.h"
00063 #include "common/utils.h"
00064 
00065 extern ADI_SENSE_CONFIG  babyseat_conf;
00066 extern ADI_SENSE_1000_LUT_DESCRIPTOR *sample_lut_desc_list[];
00067 extern ADI_SENSE_1000_LUT_TABLE_DATA  *sample_lut_data_list[];
00068 extern unsigned                       sample_lut_num_tables;
00069 
00070 
00071 /* Change the following pointer to select any of the configurations above */
00072 static ADI_SENSE_CONFIG  *pSelectedConfig = &babyseat_conf;
00073 
00074 static ADI_SENSE_CONNECTION  connectionInfo = {
00075     .type  = ADI_SENSE_CONNECTION_TYPE_SPI ,
00076     .spi = {
00077         .mosiPin    = SPI_MOSI,
00078         .misoPin    = SPI_MISO,
00079         .sckPin     = SPI_SCK,
00080         .csPin      = D10,
00081         .maxSpeedHz = 2000000,
00082     },
00083     .gpio = {
00084         .resetPin     = D6,
00085         .errorPin     = D3,
00086         .alertPin     = D4,
00087         .datareadyPin = D5,
00088     },
00089     .log = {
00090         .txPin        = PA_11,
00091         .rxPin        = PA_12,
00092         .baudRate     = 115200,
00093         .disableLogs  = false,
00094     },
00095 };
00096 
00097 int main()
00098 {
00099     ADI_SENSE_RESULT  res;
00100     ADI_SENSE_STATUS  status;
00101     ADI_SENSE_DEVICE_HANDLE  hDevice;
00102     ADI_SENSE_MEASUREMENT_MODE  eMeasurementMode = ADI_SENSE_MEASUREMENT_MODE_NORMAL ;
00103     bool_t bDeviceReady;
00104 
00105     /*
00106      * Open an ADI Sense device instance.
00107      */
00108     res = adi_sense_Open(0, &connectionInfo, &hDevice);
00109     if (res != ADI_SENSE_SUCCESS )
00110     {
00111         ADI_SENSE_LOG_ERROR("Failed to open device instance");
00112         return res;
00113     }
00114 
00115     /*
00116      * Reset the given ADI Sense device....
00117      */
00118     ADI_SENSE_LOG_INFO("Resetting ADI Sense device, please wait...");
00119     res = adi_sense_Reset(hDevice);
00120     if (res != ADI_SENSE_SUCCESS )
00121     {
00122         ADI_SENSE_LOG_ERROR("Failed to reset device");
00123         return res;
00124     }
00125     /*
00126      * ...and wait until the device is ready.
00127      */
00128     do {
00129         wait_ms(100);
00130         res = adi_sense_GetDeviceReadyState(hDevice, &bDeviceReady);
00131         if (res != ADI_SENSE_SUCCESS )
00132         {
00133             ADI_SENSE_LOG_ERROR("Failed to get device ready-state");
00134             return res;
00135         }
00136     } while (! bDeviceReady);
00137     ADI_SENSE_LOG_INFO("ADI Sense device ready");
00138 
00139   /*
00140      * Assemble the list of user-defined Look-Up Tables from sample_lut_data.c
00141      * into the single contiguous data format required by the device.
00142      */
00143     unsigned lutBufferSize = ADI_SENSE_LUT_MAX_SIZE;
00144     ADI_SENSE_1000_LUT *pLutBuffer = (ADI_SENSE_1000_LUT *) ::operator new (lutBufferSize);
00145     if (pLutBuffer == NULL)
00146     {
00147         ADI_SENSE_LOG_ERROR("Failed to allocate memory for user-defined LUT data buffer");
00148         return ADI_SENSE_NO_MEM ;
00149     }
00150 
00151     ADI_SENSE_LOG_INFO("Assembling LUT data");
00152     res = adi_sense_1000_AssembleLutData(pLutBuffer, lutBufferSize,
00153                                          sample_lut_num_tables,
00154                                          sample_lut_desc_list,
00155                                          sample_lut_data_list);
00156     if (res != ADI_SENSE_SUCCESS )
00157     {
00158         ADI_SENSE_LOG_ERROR("Failed to assemble user-defined LUT data");
00159         return res;
00160     }
00161 
00162     /*
00163      * Write assembled user-defined Look-Up Table data structure to the device
00164      * User-defined LUT data is not applied until adi_sense_ApplyConfigUpdates() is called.
00165      */
00166     ADI_SENSE_LOG_INFO("Setting LUT data");
00167     res = adi_sense_1000_SetLutData(hDevice, pLutBuffer);
00168     if (res != ADI_SENSE_SUCCESS )
00169     {
00170         ADI_SENSE_LOG_ERROR("Failed to set user-defined LUT data");
00171         return res;
00172     }
00173 
00174     delete pLutBuffer;
00175     /*
00176      * Write configuration settings to the device registers.
00177      * Settings are not applied until adi_sense_ApplyConfigUpdates() is called.
00178      */
00179     ADI_SENSE_LOG_INFO("Setting device configuration");
00180     res = adi_sense_SetConfig(hDevice, pSelectedConfig);
00181     if (res != ADI_SENSE_SUCCESS )
00182     {
00183         ADI_SENSE_LOG_ERROR("Failed to set device configuration");
00184         return res;
00185     }
00186     res = adi_sense_ApplyConfigUpdates(hDevice);
00187     if (res != ADI_SENSE_SUCCESS )
00188     {
00189         ADI_SENSE_LOG_ERROR("Failed to apply device configuration");
00190         return res;
00191     }
00192     /*
00193      * Check device status after updating the configuration
00194      */
00195     res = adi_sense_GetStatus(hDevice, &status);
00196     if (res != ADI_SENSE_SUCCESS )
00197     {
00198         ADI_SENSE_LOG_ERROR("Failed to retrieve device status");
00199         return res;
00200     }
00201     if (status.deviceStatus  &
00202         (ADI_SENSE_DEVICE_STATUS_ERROR  | ADI_SENSE_DEVICE_STATUS_ALERT ))
00203     {
00204         utils_printStatus(&status);
00205     }
00206 
00207     /*
00208      * Kick off the measurement cycle here
00209      */
00210     ADI_SENSE_LOG_INFO("Configuration completed, starting measurement cycles");
00211     utils_runMeasurement(hDevice, eMeasurementMode);
00212 
00213     /*
00214      * Clean up and exit
00215      */
00216     res = adi_sense_Close(hDevice);
00217     if (res != ADI_SENSE_SUCCESS )
00218     {
00219         ADI_SENSE_LOG_ERROR("Failed to close device instance");
00220         return res;
00221     }
00222 
00223     return 0;
00224 }