src/analog/functions.h

Fri, 18 Nov 2022 16:57:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 18 Nov 2022 16:57:02 +0100
changeset 443
3c195eb4e7a1
parent 322
e7ca120d93c7
permissions
-rw-r--r--

Details CO2 monitor shows the style limits for the specific beer. Adjust the scale of the pressure widget to the beer limits. Moved more functions to the global Utils. Fix expected pressure in the package screen for other priming sugars. Disabled some debug log messages.

/***************************************************************************
 *   Copyright (C) 2006-2008 by Tomasz Ziobrowski                          *
 *   http://www.3electrons.com                                             *
 *   e-mail: t.ziobrowski@3electrons.com                                   *
 *                                                                         *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_

#include <assert.h>
#include <cmath>
//#include <iostream> // dla testów 

using namespace std;

/** 
* Znajduje największą wartość mniejszą od scaleSize/steps i
* jednocześnie będącą całkowicie podzielną przez wartość 
* 10^N  gdzie n jest dowolną liczbą całkowitą.
* dla 5,2,1 jest to  odpowiedniio {...,500,50,5,0.5,...},
* {...200,20,2,0.2,0.02,...} etc. 
*/ 
 

double minimalStep(double scaleSize, int steps);


/**
 * Template function for determining the scale in a given interval with a designated number of scale points.
 * On the basis of the minimum and maximum value that we want to achieve, sets the values of m_min and m_max
 * in such a way that the entire scale range (m_max-m_min) was divisible by the number of scale points and
 * the distance between the scale points was the value which is a multiplication of number 5.
 * Additionally, it is possible to shift the scale left or right so that it starts as close to the minimum
 * value or ends as close to the maximum value.
 *
 * @param m_minimum - minimum value on the scale to be visible
 * @param m_maximum - maximum value on the scale to be visible
 * @param m_min     - the computed starting value of the scale
 * @param m_max     - the computed end value of the scale
 * @param stesp     - number of nodes to have in the scale
 * @param left      - whether the scale should be left or right aligned (default is right).
 * @return          - The function returns the value true if the values of m_min and m_max
 *                    changed their value as a result of changing the range.
 *                    Based on this value, it is known whether it is necessary, for example,
 *                    to redraw the scale - giving previously previous values of the scale range
 */

template <typename T>
bool range(T m_minimum,T m_maximum, T & m_min, T & m_max,unsigned int steps, bool left = false,double inc = 5.0)
{
  //cout<<"("<<m_minimum<<","<<m_maximum<<")  ("<<m_min<<","<<m_max<<")"<<endl;
  T max_tmp = m_max, min_tmp = m_min;
  m_max=m_maximum;
  m_min=m_minimum;
  assert( m_max > m_min );
  //  assert( (m_max - m_min) > 0 );
  //  if (m_max<0) left!=left; 
  
  T diff = abs(m_max - m_min);
  T scale = 0, factor = 0 ;

  while (inc * steps > (m_maximum-m_minimum))
  if (inc/10 > 0 ) inc/=10;
  else break;

  bool done = false;
  while ( diff > scale ) 
   { factor+=static_cast<T>(inc);  scale = factor * steps;  }
   
  while (!done)
  {
    
    // dirty hack to have zero equal exactly zero 
    if (m_max<0)  m_max=m_min - fmodf(m_min,steps);
    else m_max = 0.0; 
    
     while ( m_max < m_maximum ) m_max +=factor;
     m_min = m_max - scale;
     if (m_min <= m_minimum ) done = true;
     else { factor+=static_cast<T>(inc); scale = factor * steps; }
  }
  // Wprowadzenie koretkty by skala nie przesuwała się w lewo na osi X
  if (left)
  	while (m_min + factor <= m_minimum)
  	{
	   	m_min+=factor;
   		m_max+=factor;
  	}
//  cout<<"Min:"<<m_min<<" Max:"<<m_max<<endl;
 return (m_max != max_tmp) | (m_min != min_tmp);
}


#endif // _FUNCTIONS_H_

mercurial