view util.c @ 3:dc5ff2a1ed81

Be smarter about retries. If the interrupt endpoint can't be read 100 times then retry the whole request.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 10 Feb 2010 11:33:27 +1030
parents 9dab44dcb331
children b22a888eb975
line wrap: on
line source

/*
 * WH-1080 data input utility.
 * Utility functions
 *
 * Greg Lehey, 22 November 2009
 *
 * $Id: util.c,v 1.6 2009/12/18 02:13:23 grog Exp $
 */

#include "wh1080.h"

/* Used in many places, but not here */
char *wind_directions [] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                            "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW  "};

float print_previous_rain;                      /* counter for print_readings */

/* Print out readings */
void print_readings (struct readings *readings)
{
  static int lines = 0;
  char timestamp [STAMPSIZE];

  if (lines == 0)
    printf ("\n"
          "                     Page  sv    IT  IH   IDP    OT  OH   ODP    hPa     SL    abs    rel  Wind  Gust Dir Rain   CR    PR  \n");

  strftime (timestamp, STAMPSIZE, "%F %T", localtime (&readings->timestamp)); /* format time and date */

  printf ("%17s %5x %3d %5.1f %3d %5.1f %5.1f %3d %5.1f %6.1f %6.1f %6.1f %6.1f %5.1f %5.1f %3s %4.1f %4.1f %4.1f\n",
          timestamp,                            /* current time */
          readings->page,                       /* machine page number */
          readings->last_save_mins,             /* last save minutes (?) */
          readings->inside_temp,                /* inside temperature */
          readings->inside_humidity,            /* humidity in percent */
          readings->inside_dewpoint,            /* inside dewpoint */
          readings->outside_temp,               /* outside temperature */
          readings->outside_humidity,           /* humidity in percent */
          readings->outside_dewpoint,           /* outside dewpoint */
          readings->pressure,                   /* absolute pressure, hPa */
          readings->pressure_sea_level,         /* sea level pressure, hPa */
          readings->page1_abs_pressure,         /* absolute pressure, hPa */
          readings->page1_rel_pressure,         /* relative pressure, hPa */
          readings->wind_speed,                 /* wind speed in km/h */
          readings->wind_gust,                  /* wind gust speed in km/h */
          readings->wind_direction_text,        /* wind direction */
          readings->rain - print_previous_rain, /* delta rainfall in mm */
          readings->rain,                       /* current rainfall in mm */
          readings->previous_rain );            /* previous rainfall in mm */
  print_previous_rain = readings->rain;         /* reported this far */
  if (++lines > 60)
    lines = 0;
}

/*
 * Calculate dewpoint temperature.  Replace this with something with better references.
 */
float dewpoint (float temp, int humidity)
{
  float Es;                                     /* saturated water vapour pressure at dry-bulb temperature */
  float Ep;                                     /* partial pressure at this humidity */

  Es = 6.11 * pow (10.0, 7.5 * (temp / (237.7 + temp)));
  Ep = (humidity * Es) / 100;
  return (-430.22 + 237.7 * log (Ep)) / (-log (Ep) + 19.08);
}

/*
 * Calculate inside and outside dewpoints and store in the record.
 */
void set_dewpoints (struct readings *readings)
{
  readings->inside_dewpoint = dewpoint (readings->inside_temp, readings->inside_humidity);
  readings->outside_dewpoint = dewpoint (readings->outside_temp, readings->outside_humidity);
}

/*
 * Convert pressure to sea-level pressure.  The barometer is in the inside unit,
 * so we use the internal temperature as a base.
 */
void set_sea_level_pressure (struct readings *readings)
{
  float e = - config.elevation / (KELVIN (readings->inside_temp) * 29.263);

  e = exp (e);

  readings->pressure_sea_level = readings->pressure / e;
}

/*
 * Simplified date conversion.
 * text must be big enough for format; we don't check.
 */
void datetext (time_t date, char *text, char *format)
{
  struct tm *date_tm;                           /* struct tm format of date */

  date_tm = localtime (&date);                  /* and in struct tm format */
  strftime (text, 64, format, date_tm);
}