Show
Ignore:
Timestamp:
08/14/09 14:29:20 (15 years ago)
Author:
phil
Message:

Gas Meter counting finished, with accumulation timer and debounce timer. Todo: output and locking

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • webserver/example/EnergyMeters/Source/EnergyMeters/Meters.c

    r22 r30  
    120120  meterItems[METER_INDEX_GAS].timeLastTick = getEpochTimeWithMs(); /* when did the last tick occur? epoch (seconds) */ 
    121121  meterItems[METER_INDEX_GAS].timeBeforeLastTick = getEpochTimeWithMs(); /* when did the tick before last tick occur? epoch (seconds) */ 
    122   meterItems[METER_INDEX_GAS].valueLastTick = 0;   /* what value did we have at the last tick? */ 
    123   meterItems[METER_INDEX_GAS].valueBeforeLastTick = 1;   /* what value did we have at the tick before the last tick? */ 
     122  meterItems[METER_INDEX_GAS].valueLastTick = 1;   /* what value did we have at the last tick? */ 
     123  meterItems[METER_INDEX_GAS].currentValue = 1; /* same: start value of the meter */ 
     124  meterItems[METER_INDEX_GAS].valueBeforeLastTick = 0;   /* what value did we have at the tick before the last tick? */ 
    124125 
    125126} 
  • webserver/example/EnergyMeters/Source/EnergyMeters/MetersIncludes.h

    r22 r30  
    77#define METER_INDEX_GAS 0 /* index of the gas meter data */ 
    88 
     9#define DEBOUNCE_TICK_THRESHOLD_MS 50 /* when two ticks are less than this time period apart, the second tick is ignored (debouncing) */ 
     10#define ACCUMULATE_TICK_THRESHOLD_SEC 30 /* how long (seconds) should the ticks be accumulated before a new data item is created? */ 
    911 
    1012typedef struct 
     
    1517  portLONG valueLastTick;   /* what value did we have at the last tick? */ 
    1618  portLONG valueBeforeLastTick;   /* what value did we have at the tick before the last tick? */ 
     19  portLONG currentValue;  /* the value of the meter. gets incremented by the ISR */ 
    1720} meterItem; 
    1821 
  • webserver/example/EnergyMeters/Source/EnergyMeters/Meters_ISRs.c

    r22 r30  
    6161void handleGasMeterTick(void) 
    6262{ 
    63   meterItems[METER_INDEX_GAS].timeBeforeLastTick = meterItems[METER_INDEX_GAS].timeLastTick;     /* when did the tick before the last tick occur? */ 
    64   meterItems[METER_INDEX_GAS].timeLastTick = getEpochTimeWithMs(); /* when did the last tick occur? epoch (seconds) */ 
    65   meterItems[METER_INDEX_GAS].valueLastTick = 0;   /* what value did we have at the last tick? */ 
    66   meterItems[METER_INDEX_GAS].valueBeforeLastTick = 1;   /* what value did we have at the tick before the last tick? */ 
     63  unsigned long long currentTime = getEpochTimeWithMs(); /* current epoch time with ms in ms */ 
     64  if (  currentTime > (meterItems[METER_INDEX_GAS].timeLastTick + DEBOUNCE_TICK_THRESHOLD_MS) ) 
     65  { 
     66    /* we have a tick that is not a bounce */ 
     67    meterItems[METER_INDEX_GAS].currentValue++; /* one more tick. always increase the counter */ 
     68 
     69    /* now check if we should accumulate the ticks for this time period so we leave the new data item available long 
     70     * enough on the web page to be read by the server  
     71     */ 
     72    if (  currentTime > ((unsigned long long)meterItems[METER_INDEX_GAS].timeLastTick + ((unsigned long long)ACCUMULATE_TICK_THRESHOLD_SEC * (unsigned long long)1000)) ) 
     73    { 
     74      /* if the last meter tick happened longer than DEBOUNCE_TICK_THRESHOLD_MS ago (i.e. the switch did not just bounce) */ 
     75      meterItems[METER_INDEX_GAS].timeBeforeLastTick = meterItems[METER_INDEX_GAS].timeLastTick;     /* when did the tick before the last tick occur? (epoch in milliseconds) */ 
     76      meterItems[METER_INDEX_GAS].timeLastTick = currentTime; /* when did the last tick occur? (epoch in milliseconds) */ 
     77      meterItems[METER_INDEX_GAS].valueBeforeLastTick = meterItems[METER_INDEX_GAS].valueLastTick;   /* what value did we have at the tick before the last tick? */ 
     78      meterItems[METER_INDEX_GAS].valueLastTick = meterItems[METER_INDEX_GAS].currentValue;   /* what value did we have at the last tick? now one more! */ 
     79      /* at present, valueLastTick and currentValue are the same. This might be different for future versions. */ 
     80    } 
     81    else 
     82    { 
     83      ; /* nothing to do, curentValue was increased already above */ 
     84    } 
     85  } 
     86  else 
     87  { 
     88    ; /* we bounced, ignore this tick */ 
     89  } 
    6790} 
    6891