/* $Id: filter.c,v 1.4 2000/03/23 07:24:48 reinelt Exp $ * * smooth and damp functions * * Copyright 1999, 2000 by Michael Reinelt (reinelt@eunet.at) * * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. * * * $Log: filter.c,v $ * Revision 1.4 2000/03/23 07:24:48 reinelt * * PPM driver up and running (but slow!) * * Revision 1.3 2000/03/22 07:33:50 reinelt * * FAQ added * new modules 'processor.c' contains all data processing * * Revision 1.2 2000/03/06 06:04:06 reinelt * * minor cleanups * */ /* * * exported fuctions: * * smooth (name, period, value) * returns an average value over a given period * uses global variable "tick" * * damp (name, value) * damps a value with exp(-t/tau) * */ #include #include #include #include #include #include "cfg.h" #include "filter.h" extern int tick; #define SLOTS 64 #define SECONDS(x) (x.tv_sec+x.tv_usec/1000000.0) typedef struct { char *name; int slots; struct timeval *time; double *value; } FILTER; double smooth(char *name, int period, double value) { static FILTER *Filter=NULL; static int nFilter=0; struct timeval now; double t, v; int i, j; gettimeofday (&now, NULL); for (i=0; iSLOTS) slots=SLOTS; nFilter++; Filter=realloc(Filter, nFilter*sizeof(FILTER)); Filter[i].name=strdup(name); Filter[i].slots=slots; Filter[i].time=malloc(slots*sizeof(Filter[i].time[0])); Filter[i].value=malloc(slots*sizeof(Filter[i].value[0])); for (j=0; j0; j--) { Filter[i].time[j]=Filter[i].time[j-1]; Filter[i].value[j]=Filter[i].value[j-1]; } Filter[i].time[0]=now; Filter[i].value[0]=value; t = SECONDS(Filter[i].time[0]) - SECONDS(Filter[i].time[Filter[i].slots-1]); v = Filter[i].value[0]-Filter[i].value[Filter[i].slots-1]; if (t==0.0 || v<0.0) return 0; else return v/t; } double damp(char *name, double value) { static int tau=-1; static FILTER *Filter=NULL; static int nFilter=0; struct timeval now; double max; int i, j; if (tau==-1) tau=atoi(cfg_get("tau")?:"500"); if (tau==0.0) return value; gettimeofday (&now, NULL); for (i=0; iSLOTS) slots=SLOTS; nFilter++; Filter=realloc(Filter, nFilter*sizeof(FILTER)); Filter[i].name=strdup(name); Filter[i].slots=slots; Filter[i].time=malloc(slots*sizeof(Filter[i].time)); Filter[i].value=malloc(slots*sizeof(Filter[i].value)); for (j=0; j0; j--) { double t = SECONDS(Filter[i].time[j]) - SECONDS(Filter[i].time[j-1]); Filter[i].time[j]=Filter[i].time[j-1]; Filter[i].value[j]=Filter[i].value[j-1]*exp(-t/tau); if (Filter[i].value[j]>max) max=Filter[i].value[j]; } Filter[i].time[0]=now; Filter[i].value[0]=value; return max; } a id='n44' href='#n44'>44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/* $Id$
 * $URL$
 *
 * time plugin
 *
 * Copyright (C) 2003, 2004 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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, or (at your option)
 * any later version.
 *
 * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_time (void)
 *  adds some handy time functions
 *
 */


#include "config.h"

#include <time.h>

#include "debug.h"
#include "plugin.h"


static void my_time(RESULT * result)
{
    double value = time(NULL);
    SetResult(&result, R_NUMBER, &value);
}


static void my_strftime(RESULT * result, RESULT * arg1, RESULT * arg2)
{
    char value[256];
    time_t t = R2N(arg2);

    value[0] = '\0';
    strftime(value, sizeof(value), R2S(arg1), localtime(&t));

    SetResult(&result, R_STRING, value);
}


int plugin_init_time(void)
{

    /* register some basic time functions */
    AddFunction("time", 0, my_time);
    AddFunction("strftime", 2, my_strftime);

    return 0;
}

void plugin_exit_time(void)
{
    /* empty */
}