Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LSM303AGR
LSM303AGR_JS.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LSM303AGR_JS.cpp 00004 * @author ST 00005 * @version V1.0.0 00006 * @date 9 October 2017 00007 * @brief Implementation of an LSM303AGR Accelerometer and Magnetometer sensor for 00008 * use with Javascript. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 00040 /* Includes ------------------------------------------------------------------*/ 00041 00042 #include "LSM303AGR_JS.h" 00043 #include <inttypes.h> 00044 #include <stdlib.h> /* atoi */ 00045 #include "mbed.h" 00046 00047 /* Helper function for printing floats & doubles */ 00048 static char *print_double(char* str, double v, int decimalDigits=2) 00049 { 00050 int i = 1; 00051 int intPart, fractPart; 00052 int len; 00053 char *ptr; 00054 00055 /* prepare decimal digits multiplicator */ 00056 for (;decimalDigits!=0; i*=10, decimalDigits--); 00057 00058 /* calculate integer & fractinal parts */ 00059 intPart = (int)v; 00060 fractPart = (int)((v-(double)(int)v)*i); 00061 00062 /* fill in integer part */ 00063 sprintf(str, "%i.", intPart); 00064 00065 /* prepare fill in of fractional part */ 00066 len = strlen(str); 00067 ptr = &str[len]; 00068 00069 /* fill in leading fractional zeros */ 00070 for (i/=10;i>1; i/=10, ptr++) { 00071 if (fractPart >= i) { 00072 break; 00073 } 00074 *ptr = '0'; 00075 } 00076 00077 /* fill in (rest of) fractional part */ 00078 sprintf(ptr, "%i", fractPart); 00079 00080 return str; 00081 } 00082 00083 /* Helper function for creating JSON for data */ 00084 char *LSM303AGR_JS::make_json(char* str, int32_t *data, char *axes, int data_count) 00085 { 00086 char *ptr; 00087 ptr = str; 00088 str[0] = 0; 00089 00090 for(int i = 0; i < data_count; i++){ 00091 //printf("len: %i\n", strlen(str)); 00092 if(i == 0){ 00093 sprintf(ptr, "{"); 00094 ptr = &str[strlen(str)]; 00095 } 00096 else{ 00097 sprintf(ptr, ","); 00098 ptr = &str[strlen(str)]; 00099 } 00100 sprintf(ptr, "\"%c\":%i", axes[i], static_cast<int>(data[i])); 00101 ptr = &str[strlen(str)]; 00102 } 00103 sprintf(ptr, "}"); 00104 return str; 00105 } 00106 00107 /* Class Implementation ------------------------------------------------------*/ 00108 00109 /** init_acc 00110 * @brief Initializing the component. 00111 * @param SPI object of an helper class which handles the SPI peripheral 00112 * @param CS pin 00113 */ 00114 void LSM303AGR_JS::init_acc(SPI &spi, PinName cs_pin){ 00115 accelerometer = new LSM303AGRAccSensor (&spi, cs_pin); 00116 accelerometer->init(NULL); 00117 accelerometer->enable(); 00118 } 00119 00120 /** init_acc 00121 * @brief Initializing the component. 00122 * @param SPI object of an helper class which handles the SPI peripheral 00123 * @param CS pin 00124 * @param INT1 pin 00125 * @param INT2 pin 00126 */ 00127 void LSM303AGR_JS::init_acc(SPI &spi, PinName cs_pin, PinName int1_pin, PinName int2_pin){ 00128 accelerometer = new LSM303AGRAccSensor (&spi, cs_pin, int1_pin, int2_pin); 00129 accelerometer->init(NULL); 00130 accelerometer->enable(); 00131 } 00132 00133 /** init_acc 00134 * @brief Initializing the component. 00135 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00136 */ 00137 void LSM303AGR_JS::init_acc(DevI2C &devI2c){ 00138 accelerometer = new LSM303AGRAccSensor (&devI2c); 00139 accelerometer->init(NULL); 00140 accelerometer->enable(); 00141 } 00142 00143 /** init_acc 00144 * @brief Initializing the component. 00145 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00146 * @param INT1 pin 00147 * @param INT2 pin 00148 */ 00149 void LSM303AGR_JS::init_acc(DevI2C &devI2c, PinName int1_pin, PinName int2_pin){ 00150 accelerometer = new LSM303AGRAccSensor (&devI2c, LSM303AGR_ACC_I2C_ADDRESS, int1_pin, int2_pin); 00151 accelerometer->init(NULL); 00152 accelerometer->enable(); 00153 } 00154 00155 /** init_acc 00156 * @brief Initializing the component. 00157 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00158 * @param INT1 pin 00159 * @param INT2 pin 00160 * @param Address 00161 */ 00162 void LSM303AGR_JS::init_acc(DevI2C &devI2c, PinName int1_pin, PinName int2_pin, uint8_t address){ 00163 accelerometer = new LSM303AGRAccSensor (&devI2c, address, int1_pin, int2_pin); 00164 accelerometer->init(NULL); 00165 accelerometer->enable(); 00166 } 00167 00168 /** init_mag 00169 * @brief Initializing the component. 00170 * @param SPI object of an helper class which handles the SPI peripheral 00171 * @param CS pin 00172 */ 00173 void LSM303AGR_JS::init_mag(SPI &spi, PinName cs_pin){ 00174 magnetometer = new LSM303AGRMagSensor (&spi, cs_pin); 00175 magnetometer->init(NULL); 00176 magnetometer->enable(); 00177 } 00178 00179 /** init_mag 00180 * @brief Initializing the component. 00181 * @param SPI object of an helper class which handles the SPI peripheral 00182 * @param CS pin 00183 * @param INT pin 00184 */ 00185 void LSM303AGR_JS::init_mag(SPI &spi, PinName cs_pin, PinName int_pin){ 00186 magnetometer = new LSM303AGRMagSensor (&spi, cs_pin, int_pin); 00187 magnetometer->init(NULL); 00188 magnetometer->enable(); 00189 } 00190 00191 /** init_mag 00192 * @brief Initializing the component. 00193 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00194 */ 00195 void LSM303AGR_JS::init_mag(DevI2C &devI2c){ 00196 magnetometer = new LSM303AGRMagSensor (&devI2c); 00197 magnetometer->init(NULL); 00198 magnetometer->enable(); 00199 } 00200 00201 /** init_mag 00202 * @brief Initializing the component. 00203 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00204 * @param INT pin 00205 */ 00206 void LSM303AGR_JS::init_mag(DevI2C &devI2c, PinName int_pin){ 00207 magnetometer = new LSM303AGRMagSensor (&devI2c, LSM303AGR_MAG_I2C_ADDRESS, int_pin); 00208 magnetometer->init(NULL); 00209 magnetometer->enable(); 00210 } 00211 00212 /** init_mag 00213 * @brief Initializing the component. 00214 * @param DevI2c object of an helper class which handles the DevI2C peripheral 00215 * @param INT pin 00216 * @param Address 00217 */ 00218 void LSM303AGR_JS::init_mag(DevI2C &devI2c, PinName int_pin, uint8_t address){ 00219 magnetometer = new LSM303AGRMagSensor (&devI2c, address, int_pin); 00220 magnetometer->init(NULL); 00221 magnetometer->enable(); 00222 } 00223 00224 /** Destructor 00225 * @brief Recycle the component. 00226 * Deletes the Sensor Object 00227 */ 00228 LSM303AGR_JS::~LSM303AGR_JS(){ 00229 if(magnetometer != NULL){ 00230 delete magnetometer; 00231 } 00232 if(accelerometer != NULL){ 00233 delete accelerometer; 00234 } 00235 } 00236 00237 /** 00238 * @brief Read ID address of LSM303AGR Magnetometer 00239 * @retval The ID of the Sensor 00240 */ 00241 uint8_t LSM303AGR_JS::read_magnetometer_id(){ 00242 uint8_t result; 00243 magnetometer->read_id(&result); 00244 return result; 00245 } 00246 00247 /** 00248 * @brief Read ID address of LSM303AGR Accelerometer 00249 * @retval The ID of the Sensor 00250 */ 00251 uint8_t LSM303AGR_JS::read_accelerometer_id(){ 00252 uint8_t result; 00253 accelerometer->read_id(&result); 00254 return result; 00255 } 00256 00257 /** 00258 * @brief Get the accleremeter reading from LSM303AGR 00259 * @retval Accleremeter value 00260 */ 00261 int32_t *LSM303AGR_JS::get_accelerometer_axes(int32_t *axes){ 00262 accelerometer->get_x_axes(axes); 00263 printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00264 return axes; 00265 } 00266 00267 /** 00268 * @brief Get the accleremeter reading from LSM303AGR 00269 * @retval Accleremeter value in JSON string form 00270 */ 00271 00272 char *LSM303AGR_JS::get_accelerometer_axes_json(char *data){ 00273 int32_t axes[3]; 00274 accelerometer->get_x_axes(axes); 00275 //printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00276 00277 char axes_labels[3] = {'x', 'y', 'z'}; 00278 make_json(data, axes, axes_labels, 3); 00279 00280 return data; 00281 } 00282 00283 00284 /** 00285 * @brief Get the Magnetometer reading from LSM303AGR 00286 * @retval Magnetometer value 00287 */ 00288 int32_t *LSM303AGR_JS::get_magnetometer_axes(int32_t *axes){ 00289 magnetometer->get_m_axes(axes); 00290 printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00291 return axes; 00292 } 00293 00294 /** 00295 * @brief Get the Magnetometer reading from LSM303AGR 00296 * @retval Magnetometer value 00297 */ 00298 char *LSM303AGR_JS::get_magnetometer_axes_json(char * data){ 00299 int32_t axes[3]; 00300 magnetometer->get_m_axes(axes); 00301 //printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); 00302 00303 char axes_labels[3] = {'x', 'y', 'z'}; 00304 make_json(data, axes, axes_labels, 3); 00305 00306 return data; 00307 }
Generated on Thu Jul 14 2022 02:47:33 by
