00001
00002
00003
00004
00005 #include "config.h"
00006 #include "xgpsspeed.h"
00007 #include "xgpsspeed.icon"
00008 #include "nmea.h"
00009 #include <unistd.h>
00010 #include <sys/socket.h>
00011 #include <sys/ioctl.h>
00012 #include <fcntl.h>
00013 #define BUFSIZE 4096
00014 #define DEFAULTPORT "2947"
00015 char latd, lond;
00016 double latitude, longitude;
00017 int device_type;
00018 int debug = 0;
00019
00020 #if defined(ultrix) || defined(SOLARIS)
00021 extern double rint();
00022 #endif
00023
00024 Widget toplevel, base;
00025 Widget tacho, label, title;
00026
00027 static XrmOptionDescRec options[] = {
00028 {"-rv", "*reverseVideo", XrmoptionNoArg, "TRUE"},
00029 {"-nc", "*needleColor", XrmoptionSepArg, NULL},
00030 {"-needlecolor", "*needleColor", XrmoptionSepArg, NULL},
00031 };
00032
00033
00034
00035
00036
00037 typedef struct _XGpsResources {
00038 } XGpsResources;
00039
00040 XGpsResources resources;
00041
00042 #define Offset(field) (XtOffset(XGpsResources *, field))
00043
00044 static XtResource my_resources[] = {
00045 };
00046
00047 String fallback_resources[] = {
00048 };
00049
00050 static void open_input(XtAppContext app);
00051
00052 #undef Offset
00053
00054 int errexit(char *s)
00055 {
00056 perror(s);
00057
00058 exit(1);
00059 }
00060
00061 int main(int argc, char **argv)
00062 {
00063 Arg args[10];
00064 XtAppContext app;
00065 Cardinal i;
00066 char *cp;
00067 int ret;
00068
00069
00070 toplevel =
00071 XtVaAppInitialize(&app, "XGpsSpeed", options, XtNumber(options),
00072 &argc, argv, fallback_resources, NULL);
00073
00074 XtGetApplicationResources(toplevel, (caddr_t) & resources,
00075 my_resources, XtNumber(my_resources),
00076 NULL, (Cardinal) 0);
00077
00078
00079 i = 0;
00080 XtSetArg(args[0], XtNiconPixmap,
00081 XCreateBitmapFromData(XtDisplay(toplevel),
00082 XtScreen(toplevel)->root, xgps_bits,
00083 xgps_width, xgps_height));
00084 i++;
00085 XtSetValues(toplevel, args, i);
00086
00087
00088 base =
00089 XtCreateManagedWidget("pane", panedWidgetClass, toplevel, NULL, 0);
00090
00091
00092 i = 0;
00093 XtSetArg(args[i], XtNlabel, "GPS Speedometer");
00094 i++;
00095 label = XtCreateManagedWidget("title", labelWidgetClass, base, args, i);
00096
00097
00098 i = 0;
00099 XtSetArg(args[i], XtNlabel, "km/s");
00100 i++;
00101 label = XtCreateManagedWidget("name", labelWidgetClass, base, args, i);
00102
00103
00104 tacho = XtCreateManagedWidget("metres",
00105 tachometerWidgetClass, base, NULL, 0);
00106
00107 XtRealizeWidget(toplevel);
00108 open_input(app);
00109
00110 XtAppMainLoop(app);
00111 }
00112
00113
00114
00115 Usage()
00116 {
00117 fprintf(stderr, "xgpsspeed <Toolkit Options> [-rv] [-nc needlecolor]\n");
00118 exit(-1);
00119 }
00120
00121
00122 #if 0
00123 #if defined(i386) || defined(__hpux)
00124 #define rint (int)
00125 #endif
00126 #endif
00127
00128 void update_display()
00129 {
00130
00131 int new = rint(gNMEAdata.speed);
00132 #if 0
00133 fprintf(stderr, "gNMEAspeed %f scaled %f %d\n", gNMEAdata.speed,
00134 rint(gNMEAdata.speed * 5208 / 6706.12),
00135 (int) rint(gNMEAdata.speed * 5208 / 6706.12));
00136 #endif
00137 if (new > 400)
00138 new = 400;
00139
00140 TachometerSetValue(tacho, new);
00141 }
00142
00143 static void handle_input(XtPointer client_data, int *source,
00144 XtInputId * id)
00145 {
00146 double speed;
00147 static UINT8 buf[BUFSIZE];
00148 static int offset = 0;
00149 int count;
00150 int flags;
00151
00152 ioctl(*source, FIONREAD, &count);
00153
00154
00155 if ((flags = fcntl(*source, F_GETFL)) < 0)
00156 return;
00157 if (fcntl(*source, F_SETFL, flags | O_NDELAY) < 0)
00158 return;
00159
00160 while (offset < BUFSIZE && count--) {
00161 if (read(*source, buf + offset, 1) != 1)
00162 return;
00163
00164 if (buf[offset] == '\n') {
00165 if (buf[offset - 1] == '\r')
00166 buf[offset - 1] = '\0';
00167 handle_message(buf);
00168 update_display();
00169 offset = 0;
00170 return;
00171 }
00172 offset++;
00173 }
00174 }
00175
00176 int my_serial_open()
00177 {
00178 char *temp;
00179 char *p;
00180 char *port = DEFAULTPORT;
00181 char *device_name = "localhost";
00182 int one = 1;
00183 int ttyfd;
00184
00185 temp = malloc(strlen(device_name) + 1);
00186 strcpy(temp, device_name);
00187
00188
00189 ttyfd = connectTCP(temp, port);
00190 free(temp);
00191 port = 0;
00192
00193 setsockopt(ttyfd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one));
00194
00195 if (write(ttyfd, "r\n", 2) != 2)
00196 errexit("Can't write to socket");
00197 return ttyfd;
00198 }
00199
00200 static void open_input(XtAppContext app)
00201 {
00202 int input = 0;
00203 XtInputId input_id;
00204
00205 input = my_serial_open();
00206
00207 input_id = XtAppAddInput(app, input, (XtPointer) XtInputReadMask,
00208 handle_input, NULL);
00209 }