#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#include "common.h"
#include "setup.h"

void log(int level, const char * fmt, ...) {
  char tmpstr[BUFSIZ];
  char timestr[16];
  va_list ap;
  time_t now;
  struct tm local;

  va_start(ap, fmt);

  if (PvrSetup.LogLevel >= level) {
    time(&now);
    localtime_r(&now, &local);
    vsnprintf(tmpstr, sizeof(tmpstr), fmt, ap);
    strftime(timestr, sizeof(timestr), "%H:%M:%S", &local);
    printf("pvrinput: %s %s\n", timestr, tmpstr);
    }
  va_end(ap);
}

/*
This function takes a ivtv V4L2 integer value
and returns it as rounded 0..100% integer value
-wirbel-
*/

int IntVal2Percent(int NumVal, int MinVal, int MaxVal){
 double t=0;
 log(3, "IntVal2Percent(NumVal=%d, MinVal=%d, MaxVal=%d)\n",
    NumVal, MinVal, MaxVal);    
 t=(double) (MaxVal - MinVal);
 t=(NumVal-MinVal)/t;    
 t=100.0*t + 0.5;    /* rounding to nearest int) */
 return (int) t;
}

/*
This function takes a 0..100% integer value
and returns it as rounded ivtv V4L2 integer value
-wirbel-
*/

int Percent2IntVal(int Percent, int MinVal, int MaxVal){
 double t=0;
 log(3, "Percent2IntVal(Percent=%d, MinVal=%d, MaxVal=%d)\n",
    Percent, MinVal, MaxVal);
 t=Percent/100.0;
 t=0.5 + t*(MaxVal-MinVal);  /* +0.5 is rounding to int */
 t=MinVal+t;
 return (int) t;
}

