annotate wh1080.c @ 8:e3d2b5500b53 default tip

Missed part of earlier commit to only print the query if -vv is used as well as compiling in the mysql stuff (now that I know -n DTRT)
author Daniel O'Connor <darius@dons.net.au>
date Thu, 11 Feb 2010 12:48:57 +1030
parents 9a14029b3782
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 * Data input utility for Fine Offset WH-1080 weather station.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 * This bases on code supplied to me by Steve Woodford.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 * Greg Lehey, 12 November 2009
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 * $Id: wh1080.c,v 1.18 2010/02/07 03:40:37 grog Exp $
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
2
9da35e705144 Add includes so that it builds without warnings.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
10 #include <stdlib.h>
9da35e705144 Add includes so that it builds without warnings.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
11
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 #include "wh1080.h"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 struct usb_device *station_device;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 usb_dev_handle *station;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 /* Data from the device */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 struct wh1080_page0 page0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 struct wh1080_page1 page1;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 struct wh1080_readings current_data [2]; /* current data readings, from station */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 struct readings current_readings; /* current data readings, our version */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 struct readings previous_readings; /* copy of previous set of readings */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 char previous_wind_direction_text [4]; /* reuse previous direction if we have no wind */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 int previous_rain; /* previous rainfall reading: we need a difference */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 float db_previous_rain; /* previous rainfall reading for database */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 struct libusb_context **usb_context;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 #if BYTE_ORDER == BIG_ENDIAN
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 * The station sends data in big-endian format. If we're running on a
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 * big-endian machine, we need to turn the 16 bit fields around.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 void reend_page0 ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 page0.current_page = le16toh (page0.current_page);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 void reend_page1 ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 page1.rel_pressure = le16toh (page1.rel_pressure);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 page1.abs_pressure = le16toh (page1.abs_pressure);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 void reend_readings (struct wh1080_readings *page)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 page->inside_temp = le16toh (page->inside_temp); /* inside temperature */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 page->outside_temp = le16toh (page->outside_temp); /* outside temperature */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 page->pressure = le16toh (page->pressure); /* absolute pressure, hPa */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 page->rain = le16toh (page->rain); /* rainfall in 0.3 mm units */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 * Scan USB busses for our device.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 * Return 1 and device information to devp if found.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 int find_usb_device (int vendor, int product, struct usb_device **devp)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 struct usb_bus *bus;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 int count;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 count = usb_find_busses ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 count = usb_find_devices ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 for (bus = usb_get_busses (); bus; bus = bus->next)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 for (*devp = bus->devices; *devp; *devp = (*devp)->next)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 if ((*devp)->descriptor.idVendor == vendor
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 && (*devp)->descriptor.idProduct == product )
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 return 1;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 return 0; /* not found */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 * Set up communications with the weather station.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 * On error, print message and die. Return means success.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 void device_setup ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 usb_init (); /* initialize libusb */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 if (! find_usb_device (WH1080_USB_VENDOR, WH1080_USB_PRODUCT, &station_device))
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 fprintf (stderr, "Can't find WH-1080 device\n");
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 exit (1);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 /* Open station */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 if (! (station = usb_open (station_device)))
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 "Can't open weather station: %s (%d)\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 usb_strerror (),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 errno);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 exit (1);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 #ifdef linux
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 * For some reason, Linux gives a device to the kernel, so we need
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 * to prise it away again.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 http://blemings.org/hugh/blog/blosxom.cgi/2008/01/15#20080115a
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 struct usb_bus *bus_list;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 struct usb_device *dev = NULL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 struct usb_dev_handle *handle;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 /* Look for the first u4xx device we can find then try and open */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 if((dev = find_u4xx(bus_list)) == NULL) {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 return NULL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 /* Try and get a handle to the device */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 if((handle = usb_open(dev)) == NULL) {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 return NULL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 /* The kernel's HID driver will seize the USBMicro device as it
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 says it's a HID device - we need to tell the kernel to
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 let go of it */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 if (usb_detach_kernel_driver_np(handle, 0) < 0) {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 /* If this fails, usually just means that no kernel driver
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 had attached itself to the device so just ignore/warn */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 /* Set the configuration */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 if(usb_set_configuration(handle, 1) != 0) {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 usb_close(handle);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 return NULL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 /* Clain interface - gather would need to this for each
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 interface if the device has more than one */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 if (usb_claim_interface(handle, 0) != 0) {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 usb_close(handle);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 return NULL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 /* etc. etc. */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 if (usb_detach_kernel_driver_np (station, 0) < 0)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 fprintf (stderr, "%s (%d)\n", usb_strerror (), errno);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 /* exit (1); */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 /* And grab the interface */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 if (usb_claim_interface (station, 0) < 0)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 fprintf (stderr, "%s (%d)\n", usb_strerror (), errno);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 exit (1);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 * Try to recover from USB breakage.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 * XXX This doesn't work in the current form.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 void device_reset ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 if (usb_release_interface (station, 0) < 0)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 "Can't release interface: %s (%d)\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 usb_strerror (),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 errno);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 if (usb_close (station) < 0)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 "Can't close interface: %s (%d)\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 usb_strerror (),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 errno);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 device_setup ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 * Write control data to device. We don't write real data.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 int write_station_control (char *buf)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 int written;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 char textdate [64];
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 /* XXX Find out what all this means, and be cleverer about retries */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 do
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 if (written = (usb_control_msg (station,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 USB_TYPE_CLASS + USB_RECIP_INTERFACE,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 0x9,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 0x200,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 0,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 buf,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 8, /* we always write 8 bytes */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 WH1080_WRITE_TIMEOUT) > 0))
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 return written;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 if (errno == ENOTTY)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 fprintf (stderr, "USB bus stuck, reinitializing\n");
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 device_reset ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 while (errno == EINTR); /* || (errno == ENOTTY)); */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 datetext (time (NULL), textdate, "%e %B %Y %T");
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 "%s: PID %d: can't write to device: %s (%d)\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 textdate,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 getpid (),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 usb_strerror (),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 errno );
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 return 0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 * Read 8 bytes from the device.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 int read_station (char *buf)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 {
7
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
233 #define MAXTRIES 10
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
234 int bytes_read, i = 0;
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 /* XXX Find out what all this means, and be cleverer about retries */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 do
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 if ((bytes_read = usb_interrupt_read (station,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 USB_ENDPOINT_IN | USB_RECIP_INTERFACE,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 buf,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 8,
7
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
243 100 )) == 8 ){
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
244 if (i > 0)
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
245 fprintf(stderr, "Read OK after %d tries\n", i + 1);
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 return bytes_read;
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
247 }
7
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
248 fprintf(stderr, "Retrying..\n");
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
249
9a14029b3782 - Wait longer (100msec) for a read, and reduce the number of retries to match.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
250 usleep (10000);
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
251 i++;
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 }
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
253 while (errno != EINTR && i < MAXTRIES);
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
254 if (i == MAXTRIES) {
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
255 fprintf(stderr,
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
256 "Can't read from device after %d attempts\n", MAXTRIES);
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
257 } else
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
258 fprintf (stderr,
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
259 "Can't read device: %s (%d)\n",
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
260 usb_strerror (),
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
261 errno );
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 return 0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 /* Read a page (32 bytes) from the device. */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 int read_station_page (uint16_t page, char *buf)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 {
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
268 int i, bytes_read, tries = 0;
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 struct read_page_request
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 char command; /* XXX I'm guessing at this */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 uint16_t address;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 char length;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 } __attribute__ ((packed)) request [2];
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 #if BYTE_ORDER == LITTLE_ENDIAN
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 page = htobe16 (page); /* in big-endian for the command */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 #endif
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
279 rerequest:
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
280 bytes_read = 0;
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
281 tries++;
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
282 if (tries > 10) {
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
283 fprintf(stderr, "Unable to read page after 10 tries\n");
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
284 exit(1);
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
285 }
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
286
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 request [0] = (struct read_page_request) {0xa1, page, 32};
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288 request [1] = request [0];
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 if (write_station_control ((char *) request) == 0)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290 exit (1); /* XXX we've already complained */
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
291
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
292 while (bytes_read < 32) {
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
293 if ((i = read_station ((char *) &buf [bytes_read])) == 0) {
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
294 fprintf(stderr, "Couldn't read from the device, rerequesting\n");
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
295 goto rerequest;
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
296 }
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
297
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
298 bytes_read += i;
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
299 }
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
300
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
301 return bytes_read;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 * Read and compare page from station. Keep trying until we get two that are
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 * the same.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
307 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
308 * XXX don't loop for ever.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 int read_valid_station_page (uint16_t page, char *buf)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 char duplicate [WH1080_PAGE_SIZE];
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 read_station_page (page, duplicate); /* read once for comparison */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315 while (1)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 read_station_page (page, buf); /* and once where we want it */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 if (! memcmp (buf, duplicate, WH1080_PAGE_SIZE)) /* bingo! */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319 return WH1080_PAGE_SIZE;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
320 memcpy (duplicate, buf, WH1080_PAGE_SIZE);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 /* Read observations from specified page.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 * We have to read 32 bytes from the device, but the data we want is only 16
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 * bytes. If it's the last 16 bytes in memory, who knows what will happen? To
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
328 * be on the safe side, always read from an even-numbered page and then return 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 * or 1 to point to the correct entry in (global) current_data.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 void read_readings (int page, struct readings *readings)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 int pagehalf = (page & 0x10) >> 4; /* index in our two entries */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 /* Page 1: pressure readings, currently unused */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337 read_valid_station_page (WH1080_PAGE1, (char *) &page1);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 #if BYTE_ORDER == BIG_ENDIAN
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339 /* reend_page0 (); */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
340 reend_page1 ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
342 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 /* Read the data itself */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
345 read_valid_station_page (page & WH1080_PAGE_MASK, (char *) &current_data);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
346
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 printf ("\n");
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 hexdump ((unsigned char *) current_data, 2 * sizeof (struct wh1080_readings));
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351 #if BYTE_ORDER == BIG_ENDIAN
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
352 /* Turn both around to avoid surprises */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353 reend_readings (&current_data [0]);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 reend_readings (&current_data [1]);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
355 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
356
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
357 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358 * Now copy the info back to readings. Note that we don't set the timestamp
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
359 * here; only the caller knows when this reading was made.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
360 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361 readings->page = page; /* save page number too */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
362 readings->last_save_mins
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
363 = current_data [pagehalf].last_save_mins; /* last save minutes (?) */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
364 readings->inside_humidity
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
365 = current_data [pagehalf].inside_humidity; /* humidity in percent */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
366 /* This appears to be an "out of range" situation. */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
367 if (readings->inside_humidity == 255)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
368 readings->inside_humidity = previous_readings.inside_humidity;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
369 readings->inside_temp
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
370 = ((float) current_data [pagehalf].inside_temp) / 10; /* inside temperature */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
371 readings->outside_humidity
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
372 = current_data [pagehalf].outside_humidity; /* humidity in percent */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
373 if (readings->outside_humidity == 255)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
374 readings->outside_humidity = previous_readings.outside_humidity;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
375 readings->outside_temp
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
376 = ((float) current_data [pagehalf].outside_temp) / 10; /* outside temperature */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
377 readings->pressure
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
378 = ((float) current_data [pagehalf].pressure) / 10 /* absolute pressure, hPa */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
379 - config.pressure_error; /* adjust for inaccuracies */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
380 readings->wind_speed
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
381 = ((float) current_data [pagehalf].wind_speed) / 3.6; /* wind speed in km/h */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
382 readings->wind_gust
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
383 = ((float) current_data [pagehalf].wind_gust) / 3.6; /* wind gust speed in km/h */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
385 * Wind direction is confusing. It's normally a value between 0 and 15, but
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
386 * it's 0x80 if there's no wind at all. Following an idea by Steve Woodford,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
387 * reuse the previous value if it's 0x80.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
388 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
389 * Other values shouldn't happen. If they do, report them in numeric form.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
390 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
391 * In addition to the text, we save the original value in degrees for
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
392 * Wunderground and friends. If it's invalid, we simply don't send it.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
393 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
394 if (current_data [pagehalf].wind_direction == 0x80) /* no wind */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
395 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
396 strcpy (readings->wind_direction_text, previous_wind_direction_text);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
397 readings->wind_direction = INVALID_DIRECTION;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
398 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399 else if (current_data [pagehalf].wind_direction < 16) /* valid direction */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
400 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401 strcpy (previous_wind_direction_text, wind_directions [current_data [pagehalf].wind_direction]);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
402 strcpy (readings->wind_direction_text, wind_directions [current_data [pagehalf].wind_direction]);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
403 readings->wind_direction = ((float) current_data [pagehalf].wind_direction) * 22.5;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
404 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
405 else /* invalid value */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
406 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
407 sprintf (readings->wind_direction_text,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
408 "%3d",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
409 current_data [pagehalf].wind_direction); /* just put in the number */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
410 readings->wind_direction = INVALID_DIRECTION;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
411 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
412
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
413 /* Count incremental rainfall in readings->rain */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
414 if (current_data [pagehalf].rain != previous_rain)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
415 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
416 int temprain = current_data [pagehalf].rain - previous_rain;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
417
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
418 if (temprain < 0) /* wraparound */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
419 temprain += USHRT_MAX; /* add 65536 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
420 readings->rain += ((float) temprain) * 0.3; /* rainfall in mm */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
421 previous_rain = current_data [pagehalf].rain;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
422 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
423
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
424 set_dewpoints (readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
425 set_sea_level_pressure (readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
426 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
427
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
428 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
429 * Read page 0.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
430 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
431
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
432 void read_page0 ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
433 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
434 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
435 * Page 0: magic and offset of current page
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
436 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
437 do
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
438 read_valid_station_page (WH1080_PAGE0, (char *) &page0);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
439 while (((page0.magic != WH1080_PAGE0_MAGIC1A)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
440 && (page0.magic != WH1080_PAGE0_MAGIC1B) )
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
441 || (page0.magic2 != WH1080_PAGE0_MAGIC2) );
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
442 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
443
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
444 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
445 * Get a set of readings from the station.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
446 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
447 void read_station_data ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
448 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
449 read_page0 (); /* get current page number from page0 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
450 read_readings (page0.current_page, &current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
451 current_readings.timestamp = time (NULL);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
452 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
453
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
454 void dump_memory ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
455 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
456 int page;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
457 struct readings readings;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
458 time_t reading_time; /* calculate time of the reading */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
459 struct tm *reading_tm; /* for trimming off seconds */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
460
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
461 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
462 * Calculate the time of the most recent archive reading, which is
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
463 * current_readings.last_save_mins old. This is confused by the fact that the
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
464 * times don't include seconds, so for some semblence of uniformity, assume
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
465 * that we're in the middle of the minute.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
466 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
467 reading_time = time (NULL); /* now */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
468 reading_tm = localtime (&reading_time); /* and in struct tm format */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
469 reading_time -= reading_tm->tm_sec; /* adjust to beginning of the minute */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
470 /* XXX decide how to round */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
471
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
472 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
473 * Note that there's a race condition in the end condition.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
474 * page0.current_page could increment during dumping. That's perfectly
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
475 * acceptable.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
476 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
477 for (page = page0.current_page + WH1080_ARCHIVE_RECORD_SIZE;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
478 page != page0.current_page;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
479 page += WH1080_ARCHIVE_RECORD_SIZE)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
480 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
481 if (page >= 0xfff0) /* wrap around */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
482 page = WH1080_FIRST_ARCHIVE; /* back to last one in memory */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
483 read_readings (page, &readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
484 #if 0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
485 if (readings.last_save_mins != 30) /* this seems to always be the value in archive readings */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
486 return; /* this would be the beginning of time? */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
487 #endif
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
488 readings.timestamp = reading_time;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
489 print_readings (&readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
490 reading_time += WH1080_ARCHIVE_INTERVAL;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
491 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
492 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
493
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
494 void insert_db_row (struct readings *readings)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
495 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
496 char reading_date [STAMPSIZE]; /* formatted date */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
497 char reading_time [STAMPSIZE]; /* and time */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
498
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
499 strftime (reading_date, STAMPSIZE, "%F", localtime (&readings->timestamp)); /* format time and date */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
500 strftime (reading_time, STAMPSIZE, "%T", localtime (&readings->timestamp)); /* format time and date */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
501
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
502 sprintf (mysql_querytext,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
503 "INSERT INTO %s\n"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
504 " (station_id, date, time, inside_humidity, inside_temp, inside_dewpoint,\n"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
505 " outside_humidity, outside_temp, outside_dewpoint, pressure_abs, pressure_msl,\n"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
506 " wind_speed, wind_gust, wind_direction, wind_direction_text, rain)\n"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
507 "VALUES\n"
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
508 " (\"%s\", \"%s\", \"%s\", %d, %6.1f, %6.1f, "
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
509 " %d, %6.1f, %6.1f, %6.1f, %6.1f, "
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
510 " %6.1f, %6.1f, %6.1f, \"%s\", %6.1f);",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
511 config.db_table,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
512 config.station_id,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
513 reading_date,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
514 reading_time,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
515 readings->inside_humidity,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
516 readings->inside_temp,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
517 readings->inside_dewpoint,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
518 readings->outside_humidity,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
519 readings->outside_temp,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
520 readings->outside_dewpoint,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
521 readings->pressure,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
522 readings->pressure_sea_level,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
523 readings->wind_speed,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
524 readings->wind_gust,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
525 readings->wind_direction,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
526 readings->wind_direction_text,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
527 readings->rain - db_previous_rain );
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
528 db_previous_rain = readings->rain; /* update our current rainfall XXX fix this */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
529 if (update) /* only if updates set */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
530 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
531 if (mysql_query (mysql, mysql_querytext))
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
532 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
533 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
534 "Can't insert database record: %s (%d)\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
535 mysql_error (mysql),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
536 mysql_errno (mysql) );
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
537 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
538 }
8
e3d2b5500b53 Missed part of earlier commit to only print the query if -vv is used as well as compiling in the mysql stuff (now that I know -n DTRT)
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
539 else if (verbose > 1)
e3d2b5500b53 Missed part of earlier commit to only print the query if -vv is used as well as compiling in the mysql stuff (now that I know -n DTRT)
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
540 puts (mysql_querytext);
0
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
541 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
542
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
543 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
544 * Check if we have missed any updates since we last ran.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
545 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
546 * Not so affectionately named after Powercor (http://www.powercor.com.au/),
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
547 * whose continual power outages are the main reason that this function is
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
548 * needed.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
549 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
550 * XXX check this for relationship to share file.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
551 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
552 void recover_powercor_breakage ()
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
553 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
554 int page;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
555 time_t reading_time;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
556
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
557 read_page0 ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
558 if (current_readings.page != page0.current_page) /* we've moved on */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
559 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
560 /* Go back to the previous record to read rain */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
561 if ((page = current_readings.page - WH1080_ARCHIVE_RECORD_SIZE)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
562 < WH1080_FIRST_ARCHIVE) /* wrap around */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
563 page = WH1080_LAST_ARCHIVE;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
564 reading_time = current_readings.timestamp /* time of this reading */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
565 - (current_readings.last_save_mins * 60);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
566 read_readings (page, &current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
567 do
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
568 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
569 /* Next page, with wraparound */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
570 page += WH1080_ARCHIVE_RECORD_SIZE;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
571 if (page > WH1080_LAST_ARCHIVE)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
572 page = WH1080_FIRST_ARCHIVE;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
573 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
574 * The archive records appear to be every 30 minutes, but since the
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
575 * duration is stored in them, there's no reason not to calculate the
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
576 * time the record was completed.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
577 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
578 read_readings (page, &current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
579 reading_time += current_readings.last_save_mins * 60; /* time this record was finished */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
580 current_readings.timestamp = reading_time;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
581 insert_db_row (&current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
582 if (verbose)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
583 print_readings (&current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
584 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
585 while (page != page0.current_page);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
586 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
587 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
588
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
589 void usage (char *me)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
590 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
591 fprintf (stderr,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
592 "Usage: %s [-d] [-n] [-v] [station ID] [db user] [password] [db host] [database]\n",
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
593 me);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
594 exit (1);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
595 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
596
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
597
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
598 int main (int argc, char *argv [])
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
599 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
600 read_config (argc, argv);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
601
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
602 device_setup (); /* initialize the device */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
603 if (recover)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
604 recover_powercor_breakage (); /* check for missed updates */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
605 read_station_data (); /* at least to set the rainfall counter */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
606
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
607 /*
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
608 * Rainfall is a pain. We have several ways of reporting it:
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
609 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
610 * Print out from this program.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
611 * Print out in various forms from report.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
612 * Insert into database.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
613 * Inform Wunderground.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
614 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
615 * Each of these can be done asynchronously, so we maintain cumulative values of
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
616 * current rainfall and a rainfall value for each of these reporting methods.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
617 * They are:
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
618 *
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
619 * Print out from this program (really util.c)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
620 * current_readings.rain - print_previous_rain
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
621 * Inform Wunderground.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
622 * current_readings.rain - current_readings.previous_rain
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
623 * Print out from report
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
624 * This is done from util.c as well, so we need to set print_previous_rain
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
625 * to current_readings.previous_rain
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
626 * Insert into database.
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
627 * Done from wh1080,
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
628 * current_readings.rain - db_previous_rain
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
629 */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
630 current_readings.rain = 0.0; /* And current rainfall */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
631 current_readings.previous_rain = 0.0; /* Initialize previous rainfall for report */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
632 print_previous_rain = 0.0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
633 db_previous_rain = 0.0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
634
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
635 if (debug)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
636 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
637 dump_memory (); /* show old data */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
638 exit (0);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
639 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
640
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
641 while (1)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
642 {
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
643 read_station_data ();
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
644 insert_db_row (&current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
645 if (verbose)
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
646 print_readings (&current_readings);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
647
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
648 /* XXX calculate the exact time to wait, which will be marginally smaller */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
649 sleep (config.poll_interval);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
650 previous_readings = current_readings; /* make a copy of the current readings */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
651 }
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
652 /* If we ever get here, this is what we should do */
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
653 usb_release_interface (station, 0);
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
654 return 0;
9dab44dcb331 Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
655 }
3
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
656
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
657
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
658 /*
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
659 *;;; Local Variables: ***
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
660 *;;; c-basic-offset:2 ***
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
661 *;;; End: ***
dc5ff2a1ed81 Be smarter about retries.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
662 */