view Discovery/Src/tDebug.c @ 224:ceecabfddb57 div-fixes-3

Bugfix, deco: fix 2 (small) problems with calculated ceiling This fixes 1 trivial, and 1 not really trivial bug in the calculation of the ceiling. When simulating a bounce dive to 80m, things become clear (tried this on a CCR dive, fixed setpoint 1.2bar, about 15 minutes of bottom time). Closely watch the behavior of the ceiling data. At some point during the ascent, the ceiling begins to decrease in 10cm steps. Then suddenly (while still ascending), the ceiling increases again with 1m, does not change for some time, and then suddenly steps 1.1m less deep. While not very relevant to real deco diving, it is simply wrong. The reason for this is subtle. The algorithm used to find the ceiling is a sort of linear search, stepping down a meter, overshoot the depth, and search back in 10cm steps. It seems some numerical instability. Fixing this, was a bit more computational intensive search by stepping up down in equal steps of 10cm. But, I'm pretty sure that things can be speeded up here, as a ceiling does not change fast, so it should be not that difficult to limit the search space, or use a binary search algorithm instead. The trivial second problem fixed, is that the ceiling ends at the surface and not at 1m depth. This small issue became visible after changing the step down size above. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Sun, 31 Mar 2019 19:35:51 +0200
parents 5f11787b4f42
children 717f335cc5c9
line wrap: on
line source

///////////////////////////////////////////////////////////////////////////////
/// -*- coding: UTF-8 -*-
///
/// \file   Discovery/Src/tDebug.c
/// \brief  Screen with Terminal Out
/// \author heinrichs weikamp gmbh
/// \date   06-April-2016
///
/// \details
///
/// $Id$
///////////////////////////////////////////////////////////////////////////////
/// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
///
///     This program is free software: you can redistribute it and/or modify
///     it under the terms of the GNU General Public License as published by
///     the Free Software Foundation, either version 3 of the License, or
///     (at your option) any later version.
///
///     This program is distributed in the hope that it will be useful,
///     but WITHOUT ANY WARRANTY; without even the implied warranty of
///     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///     GNU General Public License for more details.
///
///     You should have received a copy of the GNU General Public License
///     along with this program.  If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////////////

/* Includes ------------------------------------------------------------------*/
#include "tDebug.h"

#include "data_exchange_main.h"
#include "gfx_engine.h"
#include "gfx_fonts.h"
#include "ostc.h"
#include "tInfo.h"

#include "stm32f4xx_hal.h"

#include <string.h>

uint8_t tD_selection_page = 1;
uint8_t tD_debugModeActive = 0;
uint8_t tD_status = 0;

char tD_communication[6][40];

void tDebug_Action(void);
void tDebug_NextPage(void);


void setDebugMode(void)
{
    tD_debugModeActive = 1;
}


void exitDebugMode(void)
{
    MX_Bluetooth_PowerOff();
    settingsGetPointer()->debugModeOnStart = 0;
    tD_debugModeActive = 0;
}


uint8_t inDebugMode(void)
{
    return tD_debugModeActive;
/*
    if(settingsGetPointer()->showDebugInfo == 2)
        return 1;
    else
        return 0;
*/
}


void tDebug_start(void)
{
    MX_Bluetooth_PowerOn();

    tD_debugModeActive = 1;
    tD_status = 0;
    for(int i=0;i<6;i++)
        tD_communication[i][0] = 0;

    set_globalState(StIDEBUG);
}


void tDebugControl(uint8_t sendAction)
{
    switch(sendAction)
    {
    case ACTION_BUTTON_ENTER:
            tDebug_Action();
        break;
    case ACTION_BUTTON_NEXT:
            tDebug_NextPage();
        break;
    case ACTION_TIMEOUT:
    case ACTION_MODE_CHANGE:
    case ACTION_BUTTON_BACK:
    exitInfo();
    default:
        break;
    case ACTION_IDLE_TICK:
    case ACTION_IDLE_SECOND:
        break;
    }
}


void tDebug_refresh(void)
{
    uint8_t color;
    char text[50];

    tInfo_write_content_simple( 700,780,  20, &FontT24, "\0021/1", CLUT_NiceGreen);

    tInfo_write_content_simple(  20,780,  20, &FontT24, "Debug Terminal", CLUT_NiceGreen);
/*
    snprintf(text,50,"X: %i  Y: %i  Z: %i  %03.0f  %03.0f"
                                    ,stateUsed->lifeData.compass_DX_f
                                    ,stateUsed->lifeData.compass_DY_f
                                    ,stateUsed->lifeData.compass_DZ_f
                                    ,stateUsed->lifeData.compass_roll
                                    ,stateUsed->lifeData.compass_pitch
    );
*/
    snprintf(text,50,"roll  %.0f  pitch  %.0f"
                                    ,stateUsed->lifeData.compass_roll
                                    ,stateUsed->lifeData.compass_pitch
    );

    tInfo_write_content_simple(  20,780,  60, &FontT24, text, CLUT_NiceGreen);

    for(int i=0;i<6;i++)
    {
        if(i%2)
            color = CLUT_WarningRed;
        else
            color = CLUT_WarningYellow;
        tInfo_write_content_simple(  20,780, (60*i)+100, &FontT42, tD_communication[i], color);
    }
}


void tDebug_NextPage(void)
{

}


void tDebug_helper_replaceCRLF(char *text, uint8_t maxlength)
{
    for(int i=0; i<maxlength; i++)
    {
        if(text[i] == 0)
            break;
        if((text[i] == '\r') || (text[i] == '\n'))
            text[i] = ' ';
    }
}


void tDebug_Action(void)
{
    char aRxBuffer[50];

    char aTxBufferEscapeSequence[4] = "+++";
    char aTxBufferName[22] = "AT+BNAME=OSTC4-12345\r";
    char aTxBufferWrite[6] = "AT&W\r";
    gfx_number_to_string(5,1,&aTxBufferName[15],hardwareDataGetPointer()->primarySerial);

    tD_status++;

    switch(tD_status)
    {
    case 1:
        if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000) == HAL_OK)
        {
            strcpy(tD_communication[0],aTxBufferEscapeSequence);
        }
        else
        {
            strcpy(tD_communication[0],"Error.");
        }

        if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 3, 2000) == HAL_OK)
        {
            aRxBuffer[3] = 0;
            tDebug_helper_replaceCRLF(aRxBuffer, 3);
            strcpy(tD_communication[1],aRxBuffer);
        }
        else
        {
            strcpy(tD_communication[1],"Error.");
        }
        break;

    case 2:
        if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferName, 21, 2000) == HAL_OK)
        {
            strcpy(tD_communication[2],aTxBufferName);
        }
        else
        {
            strcpy(tD_communication[2],"Error.");
        }

        if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 21+6, 2000) == HAL_OK)
        {
            aRxBuffer[21+6] = 0;
            tDebug_helper_replaceCRLF(aRxBuffer, 21+6);
            strcpy(tD_communication[3],aRxBuffer);
        }
        else
        {
            strcpy(tD_communication[3],"Error.");
        }
        break;

    case 3:
        if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferWrite, 5, 2000) == HAL_OK)
        {
            strcpy(tD_communication[4],aTxBufferWrite);
        }
        else
        {
            strcpy(tD_communication[4],"Error.");
        }

        if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 5+6, 2000) == HAL_OK)
        {
            aRxBuffer[5+6] = 0;
            tDebug_helper_replaceCRLF(aRxBuffer, 5+6);
            strcpy(tD_communication[5],aRxBuffer);
        }
        else
        {
            strcpy(tD_communication[5],"Error.");
        }
        break;

    default:
        tD_status = 0;
        break;
    }
}



/************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/