Control remoto universal con gestos

Dependencies:   RemoteIR X_NUCLEO_IKS01A2 mbed

Fork of 6DOrientation_IKS01A2 by ST

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  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    2-December-2016
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A2 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 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 /* Includes */
00040 #include "mbed.h"
00041 #include "XNucleoIKS01A2.h"
00042 #include "ReceiverIR.h"
00043 #include "TransmitterIR.h"
00044 
00045 #define TEST_LOOP_BACK  0
00046 
00047 ReceiverIR ir_rx(PA_14);
00048 TransmitterIR ir_tx(PA_15);
00049 static int RC_Status = -1;
00050 
00051 /**
00052  * Receive.
00053  *
00054  * @param format Pointer to a format.
00055  * @param buf Pointer to a buffer.
00056  * @param bufsiz Size of the buffer.
00057  *
00058  * @return Bit length of the received data.
00059  */
00060 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 1000) {
00061     int cnt = 0;
00062     while (ir_rx.getState() != ReceiverIR::Received) {
00063         cnt++;
00064         if (timeout < cnt) {
00065             return -1;
00066         }
00067     }
00068     return ir_rx.getData(format, buf, bufsiz * 8);
00069 }
00070 
00071 /**
00072  * Transmit.
00073  *
00074  * @param format Format.
00075  * @param buf Pointer to a buffer.
00076  * @param bitlength Bit length of the data.
00077  *
00078  * @return Bit length of the received data.
00079  */
00080 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) {
00081     int cnt = 0;
00082     while (ir_tx.getState() != TransmitterIR::Idle) {
00083         cnt++;
00084         if (timeout < cnt) {
00085             return -1;
00086         }
00087     }
00088     return ir_tx.setData(format, buf, bitlength);
00089 }
00090 
00091 /* Instantiate the expansion board */
00092 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00093 
00094 /* Retrieve the composing elements of the expansion board */
00095 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00096 
00097 InterruptIn mybutton(USER_BUTTON);
00098 DigitalOut myled(LED1);
00099 
00100 volatile int mems_event = 0;
00101 volatile int send_orientation_request = 0;
00102 
00103 /* User button callback. */
00104 void pressed_cb() {
00105   send_orientation_request = 1;
00106 }
00107 
00108 /* Interrupt 1 callback. */
00109 void int1_cb() {
00110   mems_event = 1;
00111 }
00112 
00113 /* Print the orientation. */
00114 void send_orientation() {
00115   uint8_t xl = 0;
00116   uint8_t xh = 0;
00117   uint8_t yl = 0;
00118   uint8_t yh = 0;
00119   uint8_t zl = 0;
00120   uint8_t zh = 0;
00121   
00122   acc_gyro->get_6d_orientation_xl(&xl);
00123   acc_gyro->get_6d_orientation_xh(&xh);
00124   acc_gyro->get_6d_orientation_yl(&yl);
00125   acc_gyro->get_6d_orientation_yh(&yh);
00126   acc_gyro->get_6d_orientation_zl(&zl);
00127   acc_gyro->get_6d_orientation_zh(&zh);
00128   
00129   if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 ) {
00130     printf( "\r\n  ________________  " \
00131             "\r\n |                | " \
00132             "\r\n |  *             | " \
00133             "\r\n |                | " \
00134             "\r\n |                | " \
00135             "\r\n |                | " \
00136             "\r\n |                | " \
00137             "\r\n |________________| \r\n" );
00138             RC_Status = 6;
00139   }
00140 
00141   else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00142     printf( "\r\n  ________________  " \
00143             "\r\n |                | " \
00144             "\r\n |             *  | " \
00145             "\r\n |                | " \
00146             "\r\n |                | " \
00147             "\r\n |                | " \
00148             "\r\n |                | " \
00149             "\r\n |________________| \r\n" );
00150             RC_Status = 5;
00151   }
00152   
00153   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 ) {
00154     printf( "\r\n  ________________  " \
00155             "\r\n |                | " \
00156             "\r\n |                | " \
00157             "\r\n |                | " \
00158             "\r\n |                | " \
00159             "\r\n |                | " \
00160             "\r\n |  *             | " \
00161             "\r\n |________________| \r\n" );
00162             RC_Status = 4;
00163   }
00164   
00165   else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00166     printf( "\r\n  ________________  " \
00167             "\r\n |                | " \
00168             "\r\n |                | " \
00169             "\r\n |                | " \
00170             "\r\n |                | " \
00171             "\r\n |                | " \
00172             "\r\n |             *  | " \
00173             "\r\n |________________| \r\n" );
00174             RC_Status = 3;
00175   }
00176   
00177   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) {
00178     printf( "\r\n  __*_____________  " \
00179             "\r\n |________________| \r\n" );
00180             RC_Status = 2;
00181   }
00182   
00183   else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 ) {
00184     printf( "\r\n  ________________  " \
00185             "\r\n |________________| " \
00186             "\r\n    *               \r\n" );
00187             RC_Status = 1;            
00188   }
00189   
00190   else {
00191     printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
00192   }
00193 }
00194 
00195 /* Simple main function */
00196 int main() {
00197   /* Attach callback to User button press */
00198   mybutton.fall(&pressed_cb);
00199   /* Attach callback to LSM6DSL INT1 */
00200   acc_gyro->attach_int1_irq(&int1_cb);
00201 
00202   /* Enable LSM6DSL accelerometer */
00203   acc_gyro->enable_x();
00204   /* Enable 6D Orientation. */
00205   acc_gyro->enable_6d_orientation();
00206   
00207   printf("\r\n--- Starting new run ---\r\n");
00208  
00209   while(1) {
00210     if (mems_event) {
00211       mems_event = 0;
00212       LSM6DSL_Event_Status_t status;
00213       acc_gyro->get_event_status(&status);
00214       if (status.D6DOrientationStatus) {
00215         /* Send 6D Orientation */
00216         send_orientation();
00217         printf("%d\r\n",RC_Status);
00218       }
00219     }
00220 
00221     if(send_orientation_request) {
00222       send_orientation_request = 0;
00223       send_orientation();
00224     }
00225   }
00226 }