![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
text export
Dependencies: X_NUCLEO_IKS01A3
Diff: main.cpp
- Revision:
- 9:8c7774aca512
- Parent:
- 6:94151942f287
diff -r f9a18e31c9d4 -r 8c7774aca512 main.cpp --- a/main.cpp Thu Oct 29 12:57:19 2020 +0000 +++ b/main.cpp Wed May 19 15:54:08 2021 +0000 @@ -1,56 +1,36 @@ -/** - ****************************************************************************** - * @file main.cpp - * @author SRA - * @version V1.0.0 - * @date 5-March-2019 - * @brief Simple Example application for using the X_NUCLEO_IKS01A3 - * MEMS Inertial & Environmental Sensor Nucleo expansion board. - ****************************************************************************** - * @attention - * - * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** -*/ +/* mbed Microcontroller Library + * Copyright (c) 2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mbed.h" +#include "BufferedSerial.h" +#include "platform/mbed_thread.h" + +BufferedSerial pc(USBTX, USBRX,115200); //Serial port for debug +BufferedSerial COM2(PC_12, PD_2,115200); //Serial port COM2 + -/* Includes */ -#include "mbed.h" -#include "rtos.h" -#include "XNucleoIKS01A3.h" +DigitalInOut MYPIO_0(PA_9); //D8 +DigitalInOut MYPIO_1(PC_7); //D9 +DigitalInOut MYPIO_2(PB_6); //D10 +DigitalInOut MYPIO_3(PA_7); //D11 +DigitalInOut MYPIO_4(PA_6); //D12 -/* Instantiate the expansion board */ -static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); +DigitalOut LED_FAB(PB_5); +DigitalOut GREEN_LED(PA_5); +DigitalIn WATER_DETECT(PB_1); //water detect 0=ok 1=Water -/* Retrieve the composing elements of the expansion board */ -static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer; -static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; -static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor; -static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro; -static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer; -static STTS751Sensor *temp = mems_expansion_board->t_sensor; +AnalogIn Analog_0(PA_0); +AnalogIn Analog_1(PA_1); +char strAdc[10]; + +// Blinking rate in milliseconds +#define BLINKING_RATE_MS 500 + +char cmdParams[10][10]; +unsigned char cmdParamsCount = 0; + /* Helper function for printing floats & doubles */ static char *print_double(char *str, double v, int decimalDigits = 2) @@ -59,16 +39,24 @@ int intPart, fractPart; int len; char *ptr; + uint8_t fracSign = 0; //0 = negativo 1=positivo + + if(v<0){ fracSign = 0; } + else{ fracSign=1; } + /* prepare decimal digits multiplicator */ for (; decimalDigits != 0; i *= 10, decimalDigits--); /* calculate integer & fractinal parts */ - intPart = (int)v; + intPart = abs((int)v); fractPart = (int)((v - (double)(int)v) * i); + fractPart = abs(fractPart); /* fill in integer part */ - sprintf(str, "%i.", intPart); + if(fracSign == 0){ sprintf(str, "-%i.", intPart); } + else{ sprintf(str, "%i.", intPart); } + /* prepare fill in of fractional part */ len = strlen(str); @@ -88,66 +76,100 @@ return str; } -/* Simple main function */ + +unsigned char splitString(char *textToSplit,const char *delimiter) +{ + cmdParamsCount = 0; + char * token = strtok(textToSplit,delimiter); + + + while(token != NULL) + { + sprintf(cmdParams[cmdParamsCount],"%s",token); + token = strtok(NULL, delimiter); + cmdParamsCount++; + } + + return cmdParamsCount; +} + +bool startsWith(const char *pre, const char *str) +{ + size_t lenpre = strlen(pre), + lenstr = strlen(str); + return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0; +} + + +char outBuffer[255]; +char inBuffer[255] = ""; +char inChar[1]; +char gpio_map[6] = "00000"; +void execute_com1_cmd() +{ + inBuffer[strlen(inBuffer)-1] = 0; + if(startsWith("SET GPIO1_IN",inBuffer)){ MYPIO_0.input(); gpio_map[0] = '1'; } + else if(startsWith("SET GPIO2_IN",inBuffer)){ MYPIO_1.input(); gpio_map[1] = '1';} + else if(startsWith("SET GPIO3_IN",inBuffer)){ MYPIO_2.input(); gpio_map[2] = '1';} + else if(startsWith("SET GPIO4_IN",inBuffer)){ MYPIO_3.input(); gpio_map[3] = '1';} + else if(startsWith("SET GPIO5_IN",inBuffer)){ MYPIO_4.input(); gpio_map[4] = '1';} + else if(startsWith("SET GPIO1_OUT",inBuffer)){ MYPIO_0.output(); gpio_map[0] = '0';} + else if(startsWith("SET GPIO2_OUT",inBuffer)){ MYPIO_1.output(); gpio_map[1] = '0';} + else if(startsWith("SET GPIO3_OUT",inBuffer)){ MYPIO_2.output(); gpio_map[2] = '0';} + else if(startsWith("SET GPIO4_OUT",inBuffer)){ MYPIO_3.output(); gpio_map[3] = '0';} + else if(startsWith("SET GPIO5_OUT",inBuffer)){ MYPIO_4.output(); gpio_map[4] = '0';} + else if(startsWith("SET GPIO1 1",inBuffer)){ MYPIO_0.write(1); } + else if(startsWith("SET GPIO1 0",inBuffer)){ MYPIO_0.write(0); } + else if(startsWith("SET GPIO2 1",inBuffer)){ MYPIO_1.write(1); } + else if(startsWith("SET GPIO2 0",inBuffer)){ MYPIO_1.write(0); } + else if(startsWith("SET GPIO3 1",inBuffer)){ MYPIO_2.write(1); } + else if(startsWith("SET GPIO3 0",inBuffer)){ MYPIO_2.write(0); } + else if(startsWith("SET GPIO4 1",inBuffer)){ MYPIO_3.write(1); } + else if(startsWith("SET GPIO4 0",inBuffer)){ MYPIO_3.write(0); } + else if(startsWith("SET GPIO5 1",inBuffer)){ MYPIO_4.write(1); } + else if(startsWith("SET GPIO5 0",inBuffer)){ MYPIO_4.write(0); } + else if(startsWith("GET GPIO1",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_0.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO2",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_1.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO3",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_2.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO4",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_3.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO5",inBuffer)){ sprintf(outBuffer,"%d\r\n",MYPIO_4.read()); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET GPIO_MAP",inBuffer)){ sprintf(outBuffer,"%s\r\n",gpio_map); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET ADC0",inBuffer)){ sprintf(outBuffer,"%s\r\n",print_double(strAdc,Analog_0*3.3,4)); pc.write(outBuffer,strlen(outBuffer)); } + else if(startsWith("GET ADC1",inBuffer)){ sprintf(outBuffer,"%s\r\n",print_double(strAdc,Analog_1*3.3,4)); pc.write(outBuffer,strlen(outBuffer));} + else if(startsWith("SET LED 1",inBuffer)){ LED_FAB.write(1); } + else if(startsWith("SET LED 0",inBuffer)){ LED_FAB.write(0); } +} + + + int main() { - uint8_t id; - float value1, value2; - char buffer1[32], buffer2[32]; - int32_t axes[3]; - - /* Enable all sensors */ - hum_temp->enable(); - press_temp->enable(); - temp->enable(); - magnetometer->enable(); - accelerometer->enable_x(); - acc_gyro->enable_x(); - acc_gyro->enable_g(); - - printf("\r\n--- Starting new run ---\r\n"); - - hum_temp->read_id(&id); - printf("HTS221 humidity & temperature = 0x%X\r\n", id); - press_temp->read_id(&id); - printf("LPS22HH pressure & temperature = 0x%X\r\n", id); - temp->read_id(&id); - printf("STTS751 temperature = 0x%X\r\n", id); - magnetometer->read_id(&id); - printf("LIS2MDL magnetometer = 0x%X\r\n", id); - accelerometer->read_id(&id); - printf("LIS2DW12 accelerometer = 0x%X\r\n", id); - acc_gyro->read_id(&id); - printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id); - - while (1) { - printf("\r\n"); - - hum_temp->get_temperature(&value1); - hum_temp->get_humidity(&value2); - printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); - - press_temp->get_temperature(&value1); - press_temp->get_pressure(&value2); - printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); - - temp->get_temperature(&value1); - printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1)); - - printf("---\r\n"); - - magnetometer->get_m_axes(axes); - printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - accelerometer->get_x_axes(axes); - printf("LIS2DW12 [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - acc_gyro->get_x_axes(axes); - printf("LSM6DSO [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - acc_gyro->get_g_axes(axes); - printf("LSM6DSO [gyro/mdps]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]); - - ThisThread::sleep_for(1500); + // Initialise the digital pin LED1 as an output + MYPIO_0.output(); MYPIO_0 = 0; + MYPIO_1.output(); MYPIO_1 = 0; + MYPIO_2.output(); MYPIO_2 = 0; + MYPIO_3.output(); MYPIO_3 = 0; + MYPIO_4.output(); MYPIO_4 = 0; + LED_FAB = 1; + while (true) { + //led = !led; + /* + while(pc.readable()) + { + pc.read(inChar,1); + if(inChar[0] == '\n') + { + execute_com1_cmd(); + sprintf(inBuffer,""); + } + else{ + sprintf(inBuffer,"%s%c",inBuffer,inChar[0]); + } + //pc.write(inBuffer,strlen(inBuffer)); + } + */ + thread_sleep_for(1000); + LED_FAB = !LED_FAB; + GREEN_LED =!GREEN_LED; } }