Changed energest_type_time() to first update the total time, if the device

is currently active, before returning the total time.

Added energest_flush() that updates the total time for all currently
active devices. It should be called periodically to avoid the time to
overflow for devices that are active for long periods of time.
This commit is contained in:
nifi 2008-06-02 13:12:07 +00:00
parent 61bd8009f1
commit d76474bc13
2 changed files with 36 additions and 3 deletions

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: energest.c,v 1.6 2008/01/14 16:18:39 thiemovoigt Exp $ * $Id: energest.c,v 1.7 2008/06/02 13:12:07 nifi Exp $
*/ */
/** /**
@ -49,6 +49,7 @@ unsigned short energest_current_time[ENERGEST_TYPE_MAX];
#ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVICE_LEVELS]; energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVICE_LEVELS];
#endif #endif
unsigned char energest_current_mode[ENERGEST_TYPE_MAX];
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
@ -57,8 +58,9 @@ energest_init(void)
int i; int i;
for(i = 0; i < ENERGEST_TYPE_MAX; ++i) { for(i = 0; i < ENERGEST_TYPE_MAX; ++i) {
energest_total_time[i].current = energest_current_time[i] = 0; energest_total_time[i].current = energest_current_time[i] = 0;
energest_current_mode[i] = 0;
} }
#ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
for(i = 0; i < ENERGEST_CONF_LEVELDEVICE_LEVELS; ++i) { for(i = 0; i < ENERGEST_CONF_LEVELDEVICE_LEVELS; ++i) {
energest_leveldevice_current_leveltime[i].current = 0; energest_leveldevice_current_leveltime[i].current = 0;
} }
@ -68,6 +70,15 @@ energest_init(void)
unsigned long unsigned long
energest_type_time(int type) energest_type_time(int type)
{ {
/* Note: does not support ENERGEST_CONF_LEVELDEVICE_LEVELS! */
#ifndef ENERGEST_CONF_LEVELDEVICE_LEVELS
if(energest_current_mode[type]) {
rtimer_clock_t now = RTIMER_NOW();
energest_total_time[type].current += (unsigned long)
((signed short)now - (signed short)energest_current_time[type]);
energest_current_time[type] = now;
}
#endif /* ENERGEST_CONF_LEVELDEVICE_LEVELS */
return energest_total_time[type].current; return energest_total_time[type].current;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -87,8 +98,25 @@ energest_type_set(int type, unsigned long val)
energest_total_time[type].current = val; energest_total_time[type].current = val;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Note: does not support ENERGEST_CONF_LEVELDEVICE_LEVELS! */
void
energest_flush(void)
{
rtimer_clock_t now;
int i;
for(i = 0; i < ENERGEST_TYPE_MAX; i++) {
if(energest_current_mode[i]) {
now = RTIMER_NOW();
energest_total_time[i].current += (unsigned long)
((signed short)now - (signed short)energest_current_time[i]);
energest_current_time[i] = now;
}
}
}
/*---------------------------------------------------------------------------*/
#else /* ENERGEST_CONF_ON */ #else /* ENERGEST_CONF_ON */
void energest_type_set(int type, unsigned long val) {} void energest_type_set(int type, unsigned long val) {}
void energest_init(void) {} void energest_init(void) {}
unsigned long energest_type_time(int type) { return 0; } unsigned long energest_type_time(int type) { return 0; }
void energest_flush(void) {}
#endif /* ENERGEST_CONF_ON */ #endif /* ENERGEST_CONF_ON */

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: energest.h,v 1.9 2008/01/21 12:36:17 adamdunkels Exp $ * $Id: energest.h,v 1.10 2008/06/02 13:12:07 nifi Exp $
*/ */
/** /**
@ -71,11 +71,13 @@ unsigned long energest_type_time(int type);
unsigned long energest_leveldevice_leveltime(int powerlevel); unsigned long energest_leveldevice_leveltime(int powerlevel);
#endif #endif
void energest_type_set(int type, unsigned long value); void energest_type_set(int type, unsigned long value);
void energest_flush(void);
#if ENERGEST_CONF_ON #if ENERGEST_CONF_ON
/*extern int energest_total_count;*/ /*extern int energest_total_count;*/
extern energest_t energest_total_time[ENERGEST_TYPE_MAX]; extern energest_t energest_total_time[ENERGEST_TYPE_MAX];
extern unsigned short energest_current_time[ENERGEST_TYPE_MAX]; extern unsigned short energest_current_time[ENERGEST_TYPE_MAX];
extern unsigned char energest_current_mode[ENERGEST_TYPE_MAX];
#ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS #ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
extern energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVICE_LEVELS]; extern energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVICE_LEVELS];
@ -84,16 +86,19 @@ extern energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVI
#define ENERGEST_ON(type) do { \ #define ENERGEST_ON(type) do { \
/*++energest_total_count;*/ \ /*++energest_total_count;*/ \
energest_current_time[type] = RTIMER_NOW(); \ energest_current_time[type] = RTIMER_NOW(); \
energest_current_mode[type] = 1; \
} while(0) } while(0)
#define ENERGEST_OFF(type) do { \ #define ENERGEST_OFF(type) do { \
energest_total_time[type].current += (unsigned long)((signed short)RTIMER_NOW() - \ energest_total_time[type].current += (unsigned long)((signed short)RTIMER_NOW() - \
(signed short)energest_current_time[type]); \ (signed short)energest_current_time[type]); \
energest_current_mode[type] = 0; \
} while(0) } while(0)
#define ENERGEST_OFF_LEVEL(type,level) do { \ #define ENERGEST_OFF_LEVEL(type,level) do { \
energest_leveldevice_current_leveltime[level].current += (unsigned long)((signed short)RTIMER_NOW() - \ energest_leveldevice_current_leveltime[level].current += (unsigned long)((signed short)RTIMER_NOW() - \
(signed short)energest_current_time[type]); \ (signed short)energest_current_time[type]); \
energest_current_mode[type] = 0; \
} while(0) } while(0)