From b63782bbf5e30a7f178af0a954b94da4f2706d39 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 15 Sep 2017 21:25:26 -0500 Subject: [PATCH] added log/lin toggle, AA define, rearanged into map.c Former-commit-id: 5ccce0dd66b202b6e0178cc3cff2e4f9a70b736f Former-commit-id: 3616fa85a3b74c0acc493e50224746adc8cf3c31 --- sdl1090/ArialBold.ttf.REMOVED.git-id | 1 + sdl1090/Makefile | 17 ++++--- sdl1090/defs.h | 5 ++ sdl1090/draw.c | 62 ++++++++++++++++--------- sdl1090/dump1090.h | 10 ++++ sdl1090/init.c | 27 ++++++----- sdl1090/input.c | 18 +++++--- sdl1090/interactive.c | 28 ------------ sdl1090/maps.c | 68 ++++++++++++++++++++++++++++ sdl1090/structs.h | 16 +++++++ sdl1090/view1090.REMOVED.git-id | 2 +- sdl1090/view1090.c | 45 ++++++------------ 12 files changed, 193 insertions(+), 106 deletions(-) create mode 100644 sdl1090/ArialBold.ttf.REMOVED.git-id create mode 100644 sdl1090/maps.c diff --git a/sdl1090/ArialBold.ttf.REMOVED.git-id b/sdl1090/ArialBold.ttf.REMOVED.git-id new file mode 100644 index 0000000..9b5c6b4 --- /dev/null +++ b/sdl1090/ArialBold.ttf.REMOVED.git-id @@ -0,0 +1 @@ +940e255d0140b07a15ea0371695c25c02087f368 \ No newline at end of file diff --git a/sdl1090/Makefile b/sdl1090/Makefile index a8772db..c1518d7 100644 --- a/sdl1090/Makefile +++ b/sdl1090/Makefile @@ -10,18 +10,23 @@ SHAREDIR=$(PREFIX)/share/$(PROGNAME) EXTRACFLAGS=-DHTMLPATH=\"$(SHAREDIR)\" endif -CFLAGS=-O2 -g -Wall -W `pkg-config --cflags librtlsdr` -std=c99 -LIBS=`pkg-config --libs librtlsdr` -lpthread -lm `sdl-config --libs` -lSDL -lSDL_ttf -lSDL_gfx -lwiringPi -CC=gcc - +ifdef RPI + CFLAGS=-O2 -g -Wall -W `pkg-config --cflags librtlsdr` -std=c99 + LIBS=`pkg-config --libs librtlsdr` -lpthread -lm `sdl-config --libs` -lSDL -lSDL_ttf -lSDL_gfx -lwiringPi -DRPI + CC=gcc +else + CFLAGS=-O2 -g -Wall -W `pkg-config --cflags librtlsdr` + LIBS=`pkg-config --libs librtlsdr` -lpthread -lm `sdl-config --libs` -lSDL -lSDL_ttf -lSDL_gfx + CC=gcc +endif all: view1090 %.o: %.c $(CC) $(CFLAGS) $(EXTRACFLAGS) -c $< -view1090: view1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o input.o draw.o font.o init.o - $(CC) -g -o view1090 view1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o input.o draw.o font.o init.o $(LIBS) $(LDFLAGS) +view1090: view1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o input.o draw.o font.o init.o maps.o + $(CC) -g -o view1090 view1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o input.o draw.o font.o init.o maps.o $(LIBS) $(LDFLAGS) clean: rm -f *.o view1090 diff --git a/sdl1090/defs.h b/sdl1090/defs.h index f9f1677..6674d3b 100644 --- a/sdl1090/defs.h +++ b/sdl1090/defs.h @@ -1,8 +1,13 @@ #include #include #include +#include #include "SDL/SDL.h" #include "SDL/SDL_ttf.h" +#ifdef RPI + #include +#endif + #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 diff --git a/sdl1090/draw.c b/sdl1090/draw.c index 21e0103..e396680 100644 --- a/sdl1090/draw.c +++ b/sdl1090/draw.c @@ -3,11 +3,10 @@ #include "parula.h" #include "SDL/SDL_gfxPrimitives.h" -extern void drawString(char *, int, int, TTF_Font *, SDL_Color); +#define LOGMAXDIST 1000.0 +#define MAXDIST 50.0 -#define LOGPROJECTION 1 - -#define MAXDIST 1000.0 +#define AA 0 void CROSSVP(double *v, double *u, double *w) { @@ -25,16 +24,16 @@ SDL_Color setColor(uint8_t r, uint8_t g, uint8_t b) { } int screenDist(double d) { - if(LOGPROJECTION) { - return round((double)SCREEN_WIDTH * 0.5 * log(1.0+d) / log(1.0+MAXDIST)); + if(Modes.mapLogDist) { + return round((double)SCREEN_WIDTH * 0.5 * log(1.0+fabs(d)) / log(1.0+LOGMAXDIST)); } else { - return round((double)SCREEN_WIDTH * 0.5 * d / MAXDIST); + return round((double)SCREEN_WIDTH * 0.5 * fabs(d) / MAXDIST); } } void screenCoords(int *outX, int *outY, double dx, double dy) { - *outX = (SCREEN_WIDTH>>1) + screenDist(dx); - *outY = (SCREEN_HEIGHT>>1) + screenDist(dy); + *outX = (SCREEN_WIDTH>>1) + ((dx>0) ? 1 : -1) * screenDist(dx); + *outY = (SCREEN_HEIGHT>>1) + ((dy>0) ? 1 : -1) * screenDist(dy); } int outOfBounds(int x, int y) { @@ -45,10 +44,8 @@ int outOfBounds(int x, int y) { } } - void drawPlaneHeading(double dx, double dy, double heading, int signal, char *flight) { - //int planeWidth = 2; int x, y; screenCoords(&x, &y, dx, dy); @@ -92,8 +89,15 @@ void drawPlaneHeading(double dx, double dy, double heading, int signal, char *fl x2 = x + round(body*vec[0]); y2 = y + round(body*vec[1]); - //thickLineRGBA(game.screen,x1,y1,x2,y2,planeWidth,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); - aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + if(AA) { + aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } else { + thickLineRGBA(game.screen,x,y,x2,y2,2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + //lineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + filledTrigonRGBA (game.screen, x + round(-wing*.35*out[0]), y + round(-wing*.35*out[1]), x + round(wing*.35*out[0]), y + round(wing*.35*out[1]), x1, y1,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + //filledTrigonRGBA (game.screen, x + round(-wing*.35*out[0]), y + round(-wing*.35*out[1]), x + round(wing*.35*out[0]), y + round(wing*.35*out[1]), x2, y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + filledCircleRGBA (game.screen, x2,y2,1,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } //wing @@ -103,17 +107,26 @@ void drawPlaneHeading(double dx, double dy, double heading, int signal, char *fl y2 = y + round(wing*out[1]); //thickLineRGBA(game.screen,x1,y1,x2,y2,planeWidth,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); - aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); - + if(AA) { + aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } else { + filledTrigonRGBA (game.screen, x1, y1, x2, y2, x+round(body*.35*vec[0]), y+round(body*.35*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + //lineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } //tail - x1 = x + round(-body*vec[0]) + round(-tail*out[0]); - y1 = y + round(-body*vec[1]) + round(-tail*out[1]); - x2 = x + round(-body*vec[0]) + round(tail*out[0]); - y2 = y + round(-body*vec[1]) + round(tail*out[1]); + x1 = x + round(-body*.75*vec[0]) + round(-tail*out[0]); + y1 = y + round(-body*.75*vec[1]) + round(-tail*out[1]); + x2 = x + round(-body*.75*vec[0]) + round(tail*out[0]); + y2 = y + round(-body*.75*vec[1]) + round(tail*out[1]); //thickLineRGBA(game.screen,x1,y1,x2,y2,planeWidth,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); - aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + if(AA) { + aalineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } else { + //lineRGBA(game.screen,x1,y1,x2,y2,planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + filledTrigonRGBA (game.screen, x1, y1, x2, y2, x+round(-body*.5*vec[0]), y+round(-body*.5*vec[1]),planeColor.r,planeColor.g,planeColor.b,SDL_ALPHA_OPAQUE); + } if(strlen(flight)) { drawString(flight, x + 10, y + 10, game.font, planeColor); @@ -189,8 +202,13 @@ void drawTrail(double *oldDx, double *oldDy, time_t * oldSeen, int idx) { } uint8_t colorVal = (uint8_t)floor(127.0 * age); - - aalineRGBA(game.screen, prevX, prevY, currentX, currentY,colorVal, colorVal, colorVal, SDL_ALPHA_OPAQUE); + + if(AA) { + aalineRGBA(game.screen, prevX, prevY, currentX, currentY,colorVal, colorVal, colorVal, SDL_ALPHA_OPAQUE); + } else { + //lineRGBA(game.screen, prevX, prevY, currentX, currentY,colorVal, colorVal, colorVal, SDL_ALPHA_OPAQUE); + thickLineRGBA(game.screen, prevX, prevY, currentX, currentY, 1, colorVal, colorVal, colorVal, SDL_ALPHA_OPAQUE); + } } } diff --git a/sdl1090/dump1090.h b/sdl1090/dump1090.h index d4cff6d..90d3999 100644 --- a/sdl1090/dump1090.h +++ b/sdl1090/dump1090.h @@ -327,6 +327,9 @@ struct { // Internal state int mlat; // Use Beast ascii format for raw data output, i.e. @...; iso *...; int interactive_rtl1090; // flight table in interactive mode is formatted like RTL1090 + int map; + int mapLogDist; + // User details double fUserLat; // Users receiver/antenna lat/lon needed for initial surface location double fUserLon; // Users receiver/antenna lat/lon needed for initial surface location @@ -467,6 +470,13 @@ void modesSendAllClients (int service, void *msg, int len); void modesQueueOutput (struct modesMessage *mm); void modesReadFromClient(struct client *c, char *sep, int(*handler)(struct client *, char *)); +// +// Functions exported from maps.c +// + +void drawMap (void); + + #ifdef __cplusplus } #endif diff --git a/sdl1090/init.c b/sdl1090/init.c index ae30a36..6d201c9 100644 --- a/sdl1090/init.c +++ b/sdl1090/init.c @@ -1,22 +1,21 @@ #include "init.h" #include "SDL/SDL_getenv.h" -#include - -extern void closeFont(TTF_Font *); - void init(char *title) { - wiringPiSetupGpio() ; + #ifdef RPI + wiringPiSetupGpio() ; - pinMode(23, INPUT); - pullUpDnControl (23, PUD_UP); - pinMode(22, INPUT); - pullUpDnControl (22, PUD_UP); + pinMode(23, INPUT); + pullUpDnControl (23, PUD_UP); + pinMode(22, INPUT); + pullUpDnControl (22, PUD_UP); + + putenv((char*)"FRAMEBUFFER=/dev/fb1"); + putenv((char*)"SDL_FBDEV=/dev/fb1"); + #endif - putenv((char*)"FRAMEBUFFER=/dev/fb1"); - putenv((char*)"SDL_FBDEV=/dev/fb1"); /* Initialise SDL */ if (SDL_Init(SDL_INIT_VIDEO) < 0) @@ -45,7 +44,11 @@ void init(char *title) exit(1); } - + + /* Load the font */ + + game.font = loadFont("ArialBold.ttf", 12); + /* Set the screen title */ SDL_WM_SetCaption(title, NULL); diff --git a/sdl1090/input.c b/sdl1090/input.c index 2ae62e9..a09b7d4 100644 --- a/sdl1090/input.c +++ b/sdl1090/input.c @@ -1,21 +1,21 @@ #include "input.h" -#include +#include "view1090.h" void getInput() { - if(!digitalRead(23) || !digitalRead(22)) { - exit(0); - } - + #ifdef RPI + if(!digitalRead(23) || !digitalRead(22)) { + exit(0); + } + #endif + SDL_Event event; /* Loop through waiting messages and process them */ while (SDL_PollEvent(&event)) { - fprintf(stderr,"key: %d\n", event.key.keysym.sym); - switch (event.type) { /* Closing the Window or pressing Escape will exit the program */ @@ -30,6 +30,10 @@ void getInput() case SDLK_ESCAPE: exit(0); break; + + case SDLK_l: + Modes.mapLogDist = !Modes.mapLogDist; + break; default: break; diff --git a/sdl1090/interactive.c b/sdl1090/interactive.c index ee2e430..ad1007a 100644 --- a/sdl1090/interactive.c +++ b/sdl1090/interactive.c @@ -29,14 +29,6 @@ // #include "dump1090.h" -#include "structs.h" - -Game game; - -extern void drawPlaneHeading(double , double , double, int, char *); -extern void drawPlane(double , double, int); -extern void drawTrail(double *, double *, time_t *, int); -extern void drawGrid(); // // ============================= Utility functions ========================== @@ -472,9 +464,6 @@ void interactiveShowData(void) { } printf( "-------------------------------------------------------------------------------\n"); - SDL_FillRect(game.screen, NULL, 0); - - drawGrid(); while(a && (count < Modes.interactive_rows)) { @@ -539,21 +528,6 @@ void interactiveShowData(void) { snprintf(strLat, 8,"%7.03f", a->dx); snprintf(strLon, 9,"%8.03f", a->dy); - - drawTrail(a->oldDx, a->oldDy, a->oldSeen, a->oldIdx); - - int colorIdx; - if((int)(now - a->seen) > MODES_INTERACTIVE_DISPLAY_ACTIVE) { - colorIdx = -1; - } else { - colorIdx = signalAverage; - } - - if(MODES_ACFLAGS_HEADING_VALID) { - drawPlaneHeading(a->dx, a->dy,a->track, colorIdx, a->flight); - } else { - drawPlane(a->dx, a->dy, colorIdx); - } } @@ -572,8 +546,6 @@ void interactiveShowData(void) { } a = a->next; } - - SDL_Flip(game.screen); } // //========================================================================= diff --git a/sdl1090/maps.c b/sdl1090/maps.c new file mode 100644 index 0000000..fea42f7 --- /dev/null +++ b/sdl1090/maps.c @@ -0,0 +1,68 @@ + +#include "dump1090.h" +#include "structs.h" + +Game game; + +extern void drawPlaneHeading(double , double , double, int, char *); +extern void drawPlane(double , double, int); +extern void drawTrail(double *, double *, time_t *, int); +extern void drawGrid(); + +// +// ============================= Utility functions ========================== +// +static uint64_t mstime(void) { + struct timeval tv; + uint64_t mst; + + gettimeofday(&tv, NULL); + mst = ((uint64_t)tv.tv_sec)*1000; + mst += tv.tv_usec/1000; + return mst; +} + + +void drawMap(void) { + struct aircraft *a = Modes.aircrafts; + time_t now = time(NULL); + + // Refresh screen every (MODES_INTERACTIVE_REFRESH_TIME) miliseconde + if ((mstime() - Modes.interactive_last_update) < MODES_INTERACTIVE_REFRESH_TIME) + {return;} + + Modes.interactive_last_update = mstime(); + + SDL_FillRect(game.screen, NULL, 0); + + drawGrid(); + + while(a) { + if ((now - a->seen) < Modes.interactive_display_ttl) { + if (a->bFlags & MODES_ACFLAGS_LATLON_VALID) { + + unsigned char * pSig = a->signalLevel; + unsigned int signalAverage = (pSig[0] + pSig[1] + pSig[2] + pSig[3] + + pSig[4] + pSig[5] + pSig[6] + pSig[7] + 3) >> 3; + + drawTrail(a->oldDx, a->oldDy, a->oldSeen, a->oldIdx); + + int colorIdx; + if((int)(now - a->seen) > MODES_INTERACTIVE_DISPLAY_ACTIVE) { + colorIdx = -1; + } else { + colorIdx = signalAverage; + } + + if(MODES_ACFLAGS_HEADING_VALID) { + drawPlaneHeading(a->dx, a->dy,a->track, colorIdx, a->flight); + } else { + drawPlane(a->dx, a->dy, colorIdx); + } + } + } + a = a->next; + } + + SDL_Flip(game.screen); +} diff --git a/sdl1090/structs.h b/sdl1090/structs.h index f66fce9..10321de 100644 --- a/sdl1090/structs.h +++ b/sdl1090/structs.h @@ -5,3 +5,19 @@ typedef struct Game SDL_Surface *screen; TTF_Font *font; } Game; + +// functions + +//font.c +TTF_Font *loadFont(char *, int); +void closeFont(TTF_Font *); +void drawString(char *, int, int, TTF_Font *, SDL_Color); + +//init.c +void init(char *); +void cleanup(void); + +//input.c +void getInput(void); + +//draw.c diff --git a/sdl1090/view1090.REMOVED.git-id b/sdl1090/view1090.REMOVED.git-id index 3988ac3..5407539 100644 --- a/sdl1090/view1090.REMOVED.git-id +++ b/sdl1090/view1090.REMOVED.git-id @@ -1 +1 @@ -ed63e44c0b11204e27b1007445b40aa85a15684a \ No newline at end of file +8b349b0a3cce846239af437d2b37503588a29b20 \ No newline at end of file diff --git a/sdl1090/view1090.c b/sdl1090/view1090.c index 50f46d2..594035e 100644 --- a/sdl1090/view1090.c +++ b/sdl1090/view1090.c @@ -33,15 +33,6 @@ Game game; -#define SDL_MAIN_HANDLED - -extern void init(char *); -extern void cleanup(void); -extern void getInput(void); -extern void draw(void); -extern TTF_Font *loadFont(char *, int); -extern void delay(unsigned int); - int go = 1; // @@ -91,7 +82,10 @@ void view1090InitConfig(void) { Modes.fUserLat = MODES_USER_LATITUDE_DFLT; Modes.fUserLon = MODES_USER_LONGITUDE_DFLT; - Modes.interactive = 1; + Modes.interactive = 0; + Modes.quiet = 1; + Modes.map = 1; + Modes.mapLogDist = 1; } // //========================================================================= @@ -311,7 +305,8 @@ int main(int argc, char **argv) { c = (struct client *) malloc(sizeof(*c)); while(1) { if ((fd = setupConnection(c)) == ANET_ERR) { - fprintf(stderr, "Waiting on %s:%d\n", View1090.net_input_beast_ipaddr, Modes.net_input_beast_port); + fprintf(stderr, "Waiting on %s:%d\n", View1090.net_input_beast_ipaddr, Modes.net_input_beast_port); + sleep(1); } else { break; } @@ -346,13 +341,7 @@ int main(int argc, char **argv) { atexit(cleanup); go = 1; - - - /* Load the font */ - - game.font = loadFont("Arial.ttf", 10); - while (go == 1) { @@ -360,7 +349,15 @@ int main(int argc, char **argv) { interactiveRemoveStaleAircrafts(); - interactiveShowData(); + + if (Modes.interactive) { + interactiveShowData(); + } + + if (Modes.map) { + drawMap(); + } + if ((fd == ANET_ERR) || (recv(c->fd, pk_buf, sizeof(pk_buf), MSG_PEEK | MSG_DONTWAIT) == 0)) { free(c); usleep(1000000); @@ -369,20 +366,8 @@ int main(int argc, char **argv) { continue; } modesReadFromClient(c,"",decodeBinMessage); - usleep(50000); - - // draw(); - - /* Sleep briefly to stop sucking up all the CPU time */ - - // delay(frameLimit); - - // frameLimit = SDL_GetTicks() + 16; } - - - // The user has stopped us, so close any socket we opened if (fd != ANET_ERR) {close(fd);}