summaryrefslogtreecommitdiff
path: root/display.c
blob: e164d21643e98308918129d6537cb4573490dba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

/* include files */
#include "config.h"
#include <stdio.h>

#include <Xm/Xm.h>
#include <Xm/ScrolledW.h>
#include <Xm/ScrollBar.h>
#include <Xm/PushB.h>
#include <Xm/ToggleB.h>
#include <Xm/ArrowB.h>
#include <Xm/CascadeB.h>
#include <Xm/Separator.h>
#include <Xm/DrawnB.h>
#include <Xm/Scale.h>
#include <Xm/Frame.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include <Xm/TextF.h>
#include <Xm/Text.h>
#include <Xm/List.h>
#include <Xm/DrawingA.h>
#include <Xm/MenuShell.h>
#include <X11/Shell.h>
#include <math.h>

#include "gps.h"

extern void register_canvas(Widget w, GC gc);
extern void draw_graphics(struct gps_data_t *gpsdata);

#define XCENTER         (double)(width/2)
#define YCENTER         (double)(height/2)
#define SCALE           (double)(diameter/2)
#define DEG2RAD         (3.1415926535897931160E0/180.0)
#define RM		20

#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))

static Widget draww;
static GC drawGC;
static Dimension width, height;
static int diameter;
static Pixmap pixmap;

void set_color(String color)
{
    Display *dpy = XtDisplay(draww);
    Colormap cmap = DefaultColormapOfScreen(XtScreen(draww));
    XColor col, unused;

    if (!XAllocNamedColor(dpy, cmap, color, &col, &unused)) {
	char buf[32];

	sprintf(buf, "Can't alloc %s", color);
	XtWarning(buf);
	return;
    }
    XSetForeground(dpy, drawGC, col.pixel);
}

void register_canvas(Widget w, GC gc)
{
    draww = w;
    drawGC = gc;

    XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL);
    pixmap = XCreatePixmap(XtDisplay(w),
			   RootWindowOfScreen(XtScreen(w)), width, height,
			   DefaultDepthOfScreen(XtScreen(w)));
    set_color("White");
    XFillRectangle(XtDisplay(draww), pixmap, drawGC, 0, 0, width, height);
    diameter = min(width, height) - RM;
}

/* #define PCORRECT */

static void pol2cart(double azimuth, double elevation, double *xout, double *yout)
{
    double sinelev;

    azimuth *= DEG2RAD;
    elevation = 90.0 - elevation;

#ifdef PCORRECT
    elevation *= DEG2RAD;
    sinelev = sin(elevation) * SCALE;
#else
    sinelev = (elevation / 90.0) * SCALE;
#endif
    *xout = XCENTER + sin(azimuth) * sinelev;
    *yout = YCENTER - cos(azimuth) * sinelev;
}


static void draw_arc(int x, int y, int diam)
{
    XDrawArc(XtDisplay(draww), pixmap, drawGC,
	     x - diam / 2, y - diam / 2,	/* x,y */
	     diam, diam,	/* width, height */
	     0, 360 * 64	/* angle1, angle2 */
	);
}


static int get_status(struct gps_data_t *gpsdata, int satellite)
{
    int i;
    int s;

    for (i = 0; i < 12; i++)
	if (satellite == gpsdata->PRN[i]) {
	    s = gpsdata->ss[i];

	    if (s<20) return 1;
	    if (s<40) return 2;

	    return 7;
	}
    return 0;
}


void draw_graphics(struct gps_data_t *gpsdata)
{
    int i;
    double x, y;
    char buf[20];

    if (SEEN(gpsdata->satellite_stamp)) {

	i = min(width, height);

	set_color("White");
	XFillRectangle(XtDisplay(draww), pixmap, drawGC, 0, 0, width, height);

	/* draw something in the center */
	set_color("Grey");
	draw_arc(width / 2, height / 2, 6);

	/* draw the 45 degree circle */
#ifdef PCORRECT
	draw_arc(width / 2, height / 2, ((i - RM) * 7) / 10);	/* sin(45) ~ 0.7 */
#else
	draw_arc(width / 2, height / 2, (i - RM) / 2);
#endif

	set_color("Black");
	draw_arc(width / 2, height / 2, i - RM);

	pol2cart(0, 0, &x, &y);
	set_color("Black");
	XDrawString(XtDisplay(draww), pixmap, drawGC,
			(int) x, (int) y, "N", 1);
	pol2cart(90, 0, &x, &y);
	set_color("Black");
	XDrawString(XtDisplay(draww), pixmap, drawGC,
			(int) x+2, (int) y, "E", 1);
	pol2cart(180, 0, &x, &y);
	set_color("Black");
	XDrawString(XtDisplay(draww), pixmap, drawGC,
			(int) x, (int) y+10, "S", 1);
	pol2cart(270, 0, &x, &y);
	set_color("Black");
	XDrawString(XtDisplay(draww), pixmap, drawGC,
			(int) x-5, (int) y, "W", 1);

	/* Now draw the satellites... */
	for (i = 0; i < gpsdata->satellites; i++) {
	    pol2cart(gpsdata->azimuth[i], gpsdata->elevation[i], &x, &y);

	    switch (get_status(gpsdata, gpsdata->PRN[i]) & 7) {
	    case 0:
	    case 1:
		set_color("Grey");
		break;
	    case 2:
	    case 3:
		set_color("Yellow");
		break;
	    case 4:
	    case 5:
	    case 6:
		set_color("Red");
		break;
	    case 7:
		set_color("Green");
		break;
	    }
	    XFillArc(XtDisplay(draww), pixmap, drawGC,
		     (int) x - 5, (int) y - 5,	/* x,y */
		     11, 11,	/* width, height */
		     0, 360 * 64	/* angle1, angle2 */
		);
	    sprintf(buf, "%02d", gpsdata->PRN[i]);
	    set_color("Blue");
	    XDrawString(XtDisplay(draww), pixmap, drawGC,
			(int) x + 0, (int) y + 17, buf, 2);
	}
	XCopyArea(XtDisplay(draww), pixmap, XtWindow(draww), drawGC,
		  0, 0, width, height, 0, 0);
    }
}

void redraw(Widget w, XtPointer client_data, XmDrawingAreaCallbackStruct * cbs)
{
    XCopyArea(XtDisplay(draww), pixmap, XtWindow(draww), drawGC,
	      cbs->event->xexpose.x, cbs->event->xexpose.y,
	      cbs->event->xexpose.width, cbs->event->xexpose.height,
	      cbs->event->xexpose.x, cbs->event->xexpose.y);
}