/* $Id$ * $URL$ * * gps plugin (nmea), code by michu / www.neophob.com, based on nmeap, http://sourceforge.net/projects/nmeap/ * * Copyright (C) 2007 Michu - http://www.neophob.com * Copyright (C) 2004, 2005, 2006, 2007 The LCD4Linux Team * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * GPS Plugin for lcd4linux, by michael vogt / http://www.neophob.com * contact: michu@neophob.com * * based on nmeap by daveh, http://www.dmh2000.com/ or http://sourceforge.net/projects/nmeap/ * you need a compiled libnmeap to compile this plugin. * * History: * v0.1 -initial release * v0.2 -fix include files, include and (embedded devices) * -fixed emulation mode (read only x bytes instead of the whole emulated nmea data) * -improved error handling (if there are no parameters defined, lcd4linx will display "GPS ARG ERR") * -display raw nmea string from the gps receiver * -added support for gps device with 9600 baud (env. variable) * -added the option that the widget graps data from the buffer and not from the gps device (usefull for multible widgets) * v0.3 -improved nmea parsing * -improved gps-emulator * -time is now updated with rmc and gga sentence * * TODO: * -update direction only when speed > 5 kmh * -add more data fields * -dump nmea string to external file * * Exported functions: * int plugin_init_gps (void) - default plugin initialisation * void parse(SHOW-OPTIONS, DISPLAY-OPTIONS) * display option define, what the plugin should display * * OPTION: EXAMPLE: * ------- -------- * * PARAMETER 1: * #define SHOW_ALTITUDE 0x000000001 alt:500 * #define SHOW_SPEED 0x000000010 spd:30 * #define SHOW_COURSE 0x000000100 dir:NO * #define SHOW_SATELLITES 0x000001000 sat:4 * #define SHOW_QUALITY 0x000010000 qua:1 * #define SHOW_STATUS 0x000100000 sta:V * #define SHOW_TIME_UTC 0x001000000 utc:113459 * #define SHOW_DATE 0x010000000 dat:190204 (19.02.2004) * * PARAMETER 2: * #define OPTION_NO_PREFIX 0x000000001 disable prefix (example, instead of "alt:500" it displays "500" * #define OPTION_SPEED_IN_KNOTS 0x000000010 when use the SHOW_SPEED option, display speed in knots instead in km/h * #define OPTION_RAW_NMEA 0x000000100 outputs the parsed nmea string, only valid when EMULATE is not defined! * #define OPTION_GET_BUFFERDATA 0x000001000 when you define more than 1 gps widget * each widget will get updates and cause some ugly side effects, specially when you display the time * by enabling this option, the widget will not read any nmea data from the serial port. * KEEP IN MIND that there must be ONE widget which get buffered data (means read data from the port) * * #define SHOW_NMEA_STATUS 0x010000000 OK:0033/Error:0002/Incomplete:0002 * * * Examples: * - gps::parse('0x011','0') will display the altitude and speed -> alt:500 spd:43 * - gps::parse('0x01100','0') will display the course and the numbers of satellites -> dir:NO sat:3 * - gps::parse('0x01','0x01') will display the speed without prefix -> 50 * - gps::parse('0x01','0x01') will display the speed in knots without prefix -> 27 * * ENV VARIABLES * To finetune plugin_gps you may define some env variables: * GPS_PORT gps device, default value is /dev/usb/tts/1 * GPS_9600 serial port speed, default is 4800 baud, if you define this var speed will be 9600 (export GPS_9600=dummy) */ /* define the include files you need */ #include "config.h" #include #include #include #include #include #include //used for serial port flags #include /* these should always be included */ #include "debug.h" #include "plugin.h" #ifdef WITH_DMALLOC #include #endif #include "nmeap.h" //#define EMULATE //remove comment to enable gps data emulation... #define EMU_BUFFER_READ_SIZE 128 //how many bytes are read each loop aka emulation speed #define BUFFER_SIZE 256 #define SHOW_ALTITUDE 0x000000001 #define SHOW_SPEED 0x000000010 #define SHOW_COURSE 0x000000100 #define SHOW_SATELLITES 0x000001000 #define SHOW_QUALITY 0x000010000 #define SHOW_STATUS 0x000100000 #define SHOW_TIME_UTC 0x001000000 #define SHOW_DATE 0x010000000 #define OPTION_NO_PREFIX 0x000000001 #define OPTION_SPEED_IN_KNOTS 0x000000010 #define OPTION_RAW_NMEA 0x000000100 //outputs the parsed nmea string, only valid when EMULATE is not defined! #define OPTION_GET_BUFFERDATA 0x000001000 //when you define more than 1 gps widget //each widget will get updates and cause some ugly side effects, specially when you display the time //by enabling this option, the widget will not read any nmea data from the serial port. //KEEP IN MIND that there must be ONE widget which does not get buffered data (means read data from the port) #define OPTION_DEBUG 0x000010000 #define SHOW_NMEA_STATUS 0x010000000 static float course = 0.f; //degrees static float altitude = 0.f; static float speed = 0.f; //Speed over ground in KNOTS!! static int satellites = 0; //Number of satellites in use (00-12) static int quality = 0; //GPS quality indicator (0 - fix not valid, 1 - GPS fix, 2 - DGPS fix) static char gpsStatus = 'V'; //A=active or V=Void static unsigned long gpsTime = 0; //UTC of position fix in hhmmss format static unsigned long gpsDate = 0; //Date in ddmmyy format static int msgCounter = 0; //parsed nmea-sentence static int errCounter = 0; //parsed error nmea-sentence static int incomplCounter = 0; //incomplete parsed nmea-sentence /* ---------------------------------------------------------------------------------------*/ /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */ /* they need to be live for the duration of the parser */ /* ---------------------------------------------------------------------------------------*/ static nmeap_context_t nmea; /* parser context */ static nmeap_gga_t gga; /* this is where the data from GGA messages will show up */ static nmeap_rmc_t rmc; /* this is where the data from RMC messages will show up */ static int user_data; /* user can pass in anything. typically it will be a pointer to some user data */ static int fd_g; /* port handler */ static unsigned int emu_read_ofs = 0; static int debug = 0; //debug flag static char Name[] = "plugin_gps.c"; static int fndStr = 0; //how many bytes were saved from the last read static char backBuffer[BUFFER_SIZE]; //the buffer to save incomplete nmea strings #ifdef EMULATE char test_vector[] = { /* "$GPGGA,123519,3929.946667,N,11946.086667,E,1,08,0.9,545.4,M,46.9,M,,*4A\r\n" // good "$xyz,1234,asdfadfasdfasdfljsadfkjasdfk\r\n" // junk "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68\r\n" // good "$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*48\r\n" // checksum error "$GPRMC,165110.000,A,5601.0318,N,01211.3503,E,0.09,28.11,190706,,*35\r\n" //some more data "$GPGGA,165111.000,5601.0318,N,01211.3503,E,1,07,1.2,22.3,M,41.6,M,,0000*65\r\n" "$GPRMC,165111.000,A,5601.0318,N,01211.3503,E,0.08,43.53,190706,,*3E\r\n" "$GPGGA,165112.000,5601.0318,N,01211.3503,E,1,07,1.2,22.3,M,41.6,M,,0000*66\r\n" "$GPRMC,165112.000,A,5601.0318,N,01211.3503,E,0.08,37.38,190706,,*33\r\n" "$GPGGA,165113.000,5601.0318,N,01211.3503,E,1,07,1.2,22.4,M,41.6,M,,0000*60\r\n" "$GPRMC,165113.000,A,5601.0318,N,01211.3503,E,0.11,21.03,190706,,*35\r\n" "$GPGGA,165114.000,5601.0318,N,01211.3504,E,1,07,1.2,22.6,M,41.6,M,,0000*62\r\n" "$GPRMC,165114.000,A,5601.0318,N,01211.3504,E,0.08,45.67,190706,,*3D\r\n" "$GPGGA,165115.000,5601.0318,N,01211.3504,E,1,07,1.2,22.7,M,41.6,M,,0000*62\r\n" "$GPRMC,165115.000,A,5601.0318,N,01211.3504,E,0.10,59.41,190706,,*3C\r\n" "$GPGGA,165116.000,5601.0318,N,01211.3504,E,1,07,1.2,22.8,M,41.6,M,,0000*6E\r\n" "$GPRMC,165116.000,A,5601.0318,N,01211.3504,E,0.06,50.22,190706,,*34\r\n" "$GPGGA,165117.000,5601.0318,N,01211.3505,E,1,07,1.2,22.9,M,41.6,M,,0000*6F\r\n" "$GPRMC,165117.000,A,5601.0318,N,01211.3505,E,0.08,45.32,190706,,*3F\r\n" "$GPGGA,165118.000,5601.0318,N,01211.3505,E,1,07,1.2,23.0,M,41.6,M,,0000*68\r\n" "$GPRMC,165118.000,A,5601.0318,N,01211.3505,E,0.10,37.49,190706,,*30\r\n"*/ "$GPGGA,165119.000,5601.0318,N,01211.3504,E,1,06,1.2,23.0,M,41.6,M,,0000*69\r\n" "$GPRMC,165119.000,A,5601.0318,N,01211.3504,E,0.08,27.23,190706,,*34\r\n" "$GPGGA,165120.000,5601.0318,N,01211.3504,E,1,07,1.2,23.0,M,41.6,M,,0000*62\r\n" "$GPRMC,165120.000,A,5601.0318,N,01211.3504,E,0.08,41.52,190706,,*38\r\n" "$GPGGA,165121.000,5601.0319,N,01211.3505,E,1,07,1.2,23.1,M,41.6,M,,0000*62\r\n" "$GPRMC,165121.000,A,5601.0319,N,01211.3505,E,0.09,57.19,190706,,*30\r\n" "$GPGGA,165122.000,5601.0319,N,01211.3505,E,1,07,1.2,23.2,M,41.6,M,,0000*62\r\n" "$GPRMC,165122.000,A,5601.0319,N,01211.3505,E,0.10,30.60,190706,,*34\r\n" "$GPGGA,165123.000,5601.0319,N,01211.3505,E,1,07,1.2,23.3,M,41.6,M,,0000*62\r\n" "$GPRMC,165123.000,A,5601.0319,N,01211.3505,E,0.07,45.49,190706,,*3A\r\n" "$GPGGA,165124.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*62\r\n" "$GPRMC,165124.000,A,5601.0319,N,01211.3505,E,0.09,34.85,190706,,*35\r\n" "$GPGGA,165125.000,5601.0319,N,01211.3506,E,1,07,1.2,23.4,M,41.6,M,,0000*60\r\n" "$GPRMC,165125.000,A,5601.0319,N,01211.3506,E,0.06,43.06,190706,,*33\r\n" "$GPGGA,165126.000,5601.0319,N,01211.3506,E,1,07,1.2,23.4,M,41.6,M,,0000*63\r\n" "$GPRMC,165126.000,A,5601.0319,N,01211.3506,E,0.10,37.63,190706,,*37\r\n" "$GPGGA,165127.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*61\r\n" "$GPRMC,165127.000,A,5601.0319,N,01211.3505,E,0.07,42.23,190706,,*35\r\n" "$GPGGA,165128.000,5601.0319,N,01211.3505,E,1,07,1.2,23.4,M,41.6,M,,0000*6E\r\n" "$GPRMC,165128.000,A,5601.0319,N,01211.3505,E,0.09,27.98,190706,,*37\r\n" "$GPGGA,165129.000,5601.0319,N,01211.3505,E,1,07,1.2,23.3,M,41.6,M,,0000*68\r\n" "$GPRMC,165129.000,A,5601.0319,N,01211.3505,E,0.08,51.89,190706,,*36\r\n" "$GPGGA,165130.000,5601.0319,N,01211.3505,E,1,07,1.2,23.2,M,41.6,M,,0000*61\r\n" "$GPRMC,094055.000,A,5409.998645,N,00859.370546,E,0.044,0.00,301206,,,A*55\r\n" "$GPGGA,094056.000,5409.998657,N,00859.370528,E,1,12,0.82,-5.168,M,45.414,M,,*47\r\n" "$GPRMC,094056.000,A,5409.998657,N,00859.370528,E,0.263,0.00,301206,,,A*5A\r\n" "$GPGGA,094057.000,5409.998726,N,00859.370512,E,1,12,0.82,-5.171,M,45.414,M,,*40\r\n" "$GPRMC,094057.000,A,5409.998726,N,00859.370512,E,0.311,0.00,301206,,,A*51\r\n" "$GPGGA,094058.000,5409.998812,N,00859.370518,E,1,12,0.82,-5.172,M,45.414,M,,*4E\r\n" "$GPRMC,094058.000,A,5409.998812,N,00859.370518,E,0.423,0.00,301206,,,A*5A\r\n" "$GPGGA,094059.000,5409.998934,N,00859.370505,E,1,12,0.82,-5.177,M,45.414,M,,*43\r\n" "$GPRMC,094059.000,A,5409.998934,N,00859.370505,E,0.576,0.00,301206,,,A*53\r\n" "$GPGGA,094100.000,5409.999097,N,00859.370542,E,1,12,0.82,-5.177,M,45.414,M,,*4C\r\n" "$GPRMC,094100.000,A,5409.999097,N,00859.370542,E,0.705,0.00,301206,,,A\r\n" "$GPGGA,004037.851,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,0.0,M,0.0,0000*7A\r\n" "$GPGGA,175218.255,4657.3391,N,00726.2666,E,1,04,9.0,568.6,M,48.0,M,0.0,0000*7B\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.08\r\n" "$GPRMC,175218.255,A,4657.3391,N,00726.2666,E,0.$GPGGA,175219.255,4657.3391,N,00726.2666,E,1,04,9.0,568.5,M,48.0,M,0.0,0000*79\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n" "$7,,*06\r\n" "$GPGGA,175220.255,4657.3392,N,00726.2667,E,1,04,9.0,568.4,M,48.0,M,0.0,0000*70\r\n" "$GPGSA,A,3,26,29,17,12,,,,$GPGGA,175221.255,4657.3392,N,00726.2667,E,1,04,9.0,568.3,M,48.0,M,0.0,0000*76\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n" "$GPRMC,175221.255,A,4657.3392,N,00726.2667,E,0.14,150.24,230507,,*0A\r\n" "$GPGGA,175222.255,4657.3393,N,00726.2668,E,1,04,9.0,568,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n" "$GPGSV,2,1,08,26,71,153,45,29,60,14$GPGGA,175223.255,4657.3393,N,00726.2669,E,1,04,9.0,568.2,M,48.0,M,7.3393,N,00726.2669,E,0.11,141.32,230507,,*05\r\n" "$GPGGA,175224.255,4657.3394,N,00726.2670,E,1,04,9.0,568.0,M,48.0,M,0.0,0000*70\r\n" "$GPGSA,$GPGGA,175225.255,4657.3395,N,00726.2670,E,1,04,9.0,567.8,M,48.0,M,0.0,0000*77\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n" "$GPRMC,175225.255,A,4657.3395,N,00726.2670,E,0.10,146.99,230507,,*0A\r\n" "$GPGGA,175226.255,465567.5,M,48.0,M,0.0,0000*7B\r\n" "$GPGSA,A,3,26,29,17,12$GPGGA,175227.255,4657.3397,N,00726.2671,E,1,04,9.0,567.1,M,48.0,M,0.0,0000*7F\r\n" "$.7*38\r\n" "$GPGSV,2,1,08,26,71,153,46,29,60,149,46,09,55,286,00,28,32,051$GPGGA,175228.254,4657.3399,N,00726.2672,E,1,04,9.0,566.8,M,48.0,M,0.0,0000*74\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.72,E,0.12,146.43,230507,,*0D\r\n" "$GPGGA,175229.254,4657.3400,N,00726.2673,E,1,04,9.0,566.4,M,48.0,M,0.0,0000*7F\r\n" "$GPGSA,A,3,26,29,17,1$GPGGA,175230.254,4657.3401,N,00726.2675,E,1,04,9.0,566.0,M,48.0,M,0.0,0000*74\r\n" "$GPGSA,A,3,26,29,17,12$GPGGA,175231.254,4657.3402,N,00726.2676,E,1,04,9.0,565.7,M,48.0,M,0.0,0000*71\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.7*38\r\n" "$GPRMC,175231.2$GPGGA,175232.254,4657.3404,N,00726.2677,E,1,04,9.,153,45,29,60,149,46,09,55,286,00,28,32,051,00*7F\r\n" "$GPGSV,2,2,08,17,32,102,38,18,28,298,00,12,12,21$GPGGA,175233.254,4657.3405,N,00726.2678,E,1,04,9.0,565.1,M,48.0,M,0.0,0000*7C\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$,*09\r\n" "$GPGGA,175234.254,4657.3406,N,00726.2679,E,1,04,9.0,564.9,M,48.0,M,0.0,0000*70\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175234.254,A,4657.3$GPGGA,175235.254,4657.3408,N,00726.2680,E,1,04,9.00,M,0.0,0000*76\r\n" "$GPGSA,A,3,26,$GPGGA,175236.254,439\r\n" "$GPRMC,175236.254,A,4657.3409,N,00726.2681,E,0.14,142.56,230507,,*06\r\n" "$GPGGA,175237.254,4657.3410,N,00726.2682,E,1,04,9.0,564.5,M,48.0,M,0.0,0000*7C\r\n" "$GPGSA,A,3,26,29,17,12,,,,00,28,32,051,00*7F\r\n" "$GPGSV,2,2,08,17,32,102,39,18,28,298,00,12,12,218,35,22,09,3$GPGGA,175238.254,4657.3411,N,00726.2682,E,1,04$GPGGA,175239.254,4657.3412,N,00726.2683,E,1,04,9.0,564.7,M,48.0,M,0.0,0000*73\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175239.254,A,4657.3412,N$GPGGA,175240.254,4657.3412,N,00726.2684,E,1,04,9.0,564.8,M,48.0,M,0.0,0000*75\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,$GPGGA,175241.254,4657.3413,N,00726.2684,E,1,04,9.0,565.1,M,48.0,M,0.0,0000*7D\r\n" ",,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175241.254,A,4657.3413,N,00726.2684,E,0.$GPGGA,175242.254,4657.3413,N,00726.2684,E,1,04,9.0,565.3,M,48.0,M,0.0,0000*7C\r\n" "$GPGSA,A,3,26,29,179,60,149,46,09,55,286,00,28,32,051,00*7C\r\n" "$GPGS$GPGGA,175243.254,4657.3414,N,00726.2685,E,1,04,9.0,565.7,M,48.0,M,0.0,0000*7F\r\n" "$GPGSA,A,3$GPGGA,175244.253,4657.3414,N,00726.2685,E,1,04,9.0,566.0,M,48.0,M,0.0,0000*7B\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPGGA,175245.253,4657.3415,N,00726.2685,E,1,04,9.0,566.3,M,48.0,M,0.0,0000*78\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175245.253,A,4657.3415,N,00726.2685,E,0.2$GPGGA,175246.253,4657.3415,N,00726.2686,E,1,04,9.0,566.5,M,48.0,M,0.0,0000*7E\r\n" ",175246.253,A,4657.3415,N,00726.2686,E,0.13,145$GPGGA,175247.253,4657.3416,N,00726.2686,E,1,04,9.0,566.8,M,48.0,M,0.0,0000*71\r\n" "$GPGSA,A,3,26,17,32,101,39*7D\r\n" "$GPGSV,2,2,08,28,32,051,00,18,$GPGGA,175248.253,4657.3417,N,00726.2686,E,1,04,9.0,567.0,M,48.0,M,0.0,0000*76\r\n" "$GPGSA,A,3,26,29$GPGGA,175249.253,4657.3417,N,00726.2686,E,1,04.3,M,48.0,M,0.0,0000*7$GPGGA,175250.253,4657.3418,N,00726.2686,E,1,04,9.0,567.4,M,48.0,M,0.0,0000*74\r\n" ".5,9.0,2.6*39\r\n" "$GPRMC,175250.253,A,4657.3$GPGGA,175251.253,4657.3418,N,00726.2687,E,1,04,9.0,567.5,M,48.0,M,0.0,0000*75\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" ".2687,E,0.14,146.26,230507,,*05\r\n" "$GPGGA,175252.253,4657.,00,18,28,298,00,12,12,218,36,22,09,326,00*7E\r\n" "$GPRMC,175252.253,A,4657.3419,N,04,9.0,567.9,M,48.0,M,0.0,0000*7A\r\n" "$GPGSA,A,3,26,29,17,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175253.253,A,4657.$GPGGA,175254.253,4657.3420,N,00726.2687,E,1,0417,12,,,,,,,,,9.5,9.0,2.6*39\r\n" "$GPRMC,175254.253,A,4657.3420,N,00726.26$GPGGA,175255.253,4657.3421,N,00726.2688,E,1,04,9.0,568.4,M,48.0,M,0.0,0000*7A\r\n" "$GPGSA,A,,E,0.16,151.85,230507,,*09\r\n" }; #endif /*************************************************************************/ /* * open the specified serial port for read/write * @return port file descriptor or -1 */ #ifndef EMULATE static int openPort(const char *tty, int baud) { int status; int fd; struct termios newtio; /* open the tty */ fd = open(tty, O_RDWR | O_NOCTTY); if (fd < 0) { error("openPort: error open"); return fd; } /* flush serial port */ status = tcflush(fd, TCIFLUSH); if (status < 0) { error("openPort: error tcflush"); close(fd); return -1; } /* get current terminal state */ tcgetattr(fd, &newtio); /* set to raw terminal type */ newtio.c_cflag = baud | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNBRK | IGNPAR; newtio.c_oflag = 0; /* control parameters */ newtio.c_cc[VMIN] = 1; /* block for at least one charater */ /* set its new attrigutes */ status = tcsetattr(fd, TCSANOW, &newtio); if (status < 0) { //error("tcsetattr() failed: %s", strerror(errno)); error("tcsetattr() failed: __ERRNO removed due lazy coder"); close(fd); fd = -1; return fd; } return fd; } #endif /** called when a gpgga message is received and parsed */ static void gpgga_callout( __attribute__ ((unused)) nmeap_context_t * context, void *data, __attribute__ ((unused)) void *user_data) { nmeap_gga_t *gga = (nmeap_gga_t *) data; altitude = gga->altitude; satellites = gga->satellites; quality = gga->quality; gpsTime = gga->time; if (debug == 1) debug("gps:debug: get gga callout\n"); } //search a buffer for a string (forwards) int strLastOcc(char *theBuffer, char searchChar, int size) { int i, ret; ret = -1; for (i = size; i >= 0; i--) { if (theBuffer[i] == searchChar) { ret = i; break; } } return ret; } //search a buffer for a string (backwards) int strFirstOcc(char *theBuffer, char searchChar, int size) { int i, ret; ret = -1; for (i = 0; i < size; i++) { if (theBuffer[i] == searchChar) { ret = i; break; } } return ret; } /** called when a gprmc message is received and parsed */ static void gprmc_callout( __attribute__ ((unused)) nmeap_context_t * context, void *data, __attribute__ ((unused)) void *user_data) { nmeap_rmc_t *rmc = (nmeap_rmc_t *) data; speed = rmc->speed; gpsStatus = rmc->warn; gpsTime = rmc->time; gpsDate = rmc->date; if (debug == 1) debug("gps:debug: get rmc callout\n"); } static int prepare_gps_parser() { int status; char *port = "/dev/usb/tts/1"; char *test; int speed = 0; // 0 = default 4800 baud, 1 is 9600 baud if ((test = getenv("GPS_PORT"))) { /* define your port via env variable */ port = test; } if ((test = getenv("GPS_9600"))) { /* define your port via env variable */ speed = 1; } /* --------------------------------------- */ /* open the serial port device */ /* using default 4800 baud for most GPS */ /* --------------------------------------- */ #ifndef EMULATE if (speed == 0) fd_g = openPort(port, B4800); else fd_g = openPort(port, B9600); if (fd_g < 0) { /* open failed */ error("GPS PLUGIN, Error: openPort %d", fd_g); return fd_g; } #endif /* --------------------------------------- */ /*STEP 2 : initialize the nmea context */ /* --------------------------------------- */ status = nmeap_init(&nmea, (void *) &user_data); if (status != 0) { error("GPS PLUGIN, Error: nmeap_init %d", status); exit(1); } /* --------------------------------------- */ /*STEP 3 : add standard GPGGA parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea, "GPGGA", nmeap_gpgga, gpgga_callout, &gga); if (status != 0) { error("GPS PLUGIN, Error: nmeap_add GPGGA parser, error:%d", status); return -1; } /* --------------------------------------- */ /*STEP 4 : add standard GPRMC parser */ /* -------------------------------------- */ /* status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc); */ status = nmeap_addParser(&nmea, "GPRMC", nmeap_gprmc, gprmc_callout, &rmc); if (status != 0) { error("GPS PLUGIN, Error: nmeap_add GPRMC parser, error:%d", status); return -1; } return fd_g; } static void parse(RESULT * result, RESULT * theOptions, RESULT * displayOptions) { int rem; int offset; int status; int len; long options; long dispOptions; char buffer[BUFFER_SIZE]; char bufferTmp[BUFFER_SIZE]; int validStart, validEnd; options = R2N(theOptions); dispOptions = R2N(displayOptions); //error("options: %x\n",options); if (dispOptions & OPTION_DEBUG) debug = 1; if ((dispOptions & OPTION_GET_BUFFERDATA) == 0) { /* ---------------------------------------- */ /* STEP 6 : get a buffer of input */ /* --------------------------------------- */ memset(buffer, 0, BUFFER_SIZE); if (fndStr > BUFFER_SIZE) fndStr = 0; //copy unfinished nmea strings back if (fndStr > 0) { memcpy(buffer, backBuffer, fndStr); } #ifdef EMULATE memcpy(&buffer[fndStr], &test_vector[emu_read_ofs], BUFFER_SIZE - fndStr); len = rem = EMU_BUFFER_READ_SIZE; emu_read_ofs += EMU_BUFFER_READ_SIZE; if (emu_read_ofs > (sizeof(test_vector) - BUFFER_SIZE)) { emu_read_ofs = 0; memset(buffer, 0, BUFFER_SIZE); } #else len = rem = read(fd_g, buffer, BUFFER_SIZE - fndStr); if (len <= 0) { error("GPS Plugin, Error read from port, try using the GPS_PORT env variable (export GPS_PORT=/dev/mydev)"); //break; } if (debug == 1) debug("gps:debug: read %d bytes\n", len); #endif if (dispOptions & OPTION_RAW_NMEA) printf("\n__[%s]", buffer + '\0'); /* ---------------------------------------------- */ /* STEP 7 : process input until buffer is used up */ /* ---------------------------------------------- */ validStart = strFirstOcc(buffer, '$', len); validEnd = strLastOcc(buffer, '\n', len); if (validStart >= 0 && validEnd > 0 && validStart < validEnd) { //valid string found memcpy(bufferTmp, buffer, sizeof(buffer)); //save buffer memset(backBuffer, 0, sizeof(backBuffer)); //clear backup buffer memcpy(backBuffer, buffer + validEnd, len - validEnd); // save incomplete nmea string memset(buffer, 0, sizeof(buffer)); //clean buffer memcpy(buffer, bufferTmp + validStart, validEnd - validStart + 1); //copy valid name string fndStr = len - validEnd + validStart; //save the size of the buffer } else { //no valid nmea string found fndStr = 0; memset(buffer, 0, sizeof(buffer)); memset(backBuffer, 0, sizeof(backBuffer)); } offset = 0; if (debug == 1) debug("backBuffer: %s\n", backBuffer); //the nmeap_parseBuffer function needs whole nmea strings, combined string will NOT work! validStart = strFirstOcc(buffer, '$', len); validEnd = strFirstOcc(buffer, '\n', len); while (validStart >= 0 && validEnd > 0 && validStart < validEnd) { memset(bufferTmp, 0, sizeof(bufferTmp)); //empty temp buffer memcpy(bufferTmp, buffer + offset + validStart, validEnd - validStart + 1); //fill temp buffer if (debug == 1) debug("submit: %s\n", bufferTmp); rem = len - offset; status = nmeap_parseBuffer(&nmea, (const char *) &bufferTmp, &rem); //parse it if (status == -1) { errCounter++; error("parser error occured! (cnt: %i)\n", errCounter); } else if (status == 0) { incomplCounter++; } else if (status > 0) msgCounter++; offset += validEnd - validStart + 1; //update offset validStart = strFirstOcc(buffer + offset, '$', len - offset); //find next sentence validEnd = strFirstOcc(buffer + offset, '\n', len - offset); } /* while (rem > 0) { status = nmeap_parseBuffer(&nmea, &buffer[offset], &rem); debug("\nGPS::debug: remaining: %d bytes\n",rem); offset += (len - rem); }*/ } // end of OPTION get bufferdata /* --------------------------------------- */ /* DISPLAY stuff comes here... */ /* --------------------------------------- */ char *value = " "; char outputStr[80]; memset(outputStr, 0, 80); if (options & SHOW_ALTITUDE) { if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%.0f ", outputStr, altitude); else sprintf(outputStr, "%salt:%.0f ", outputStr, altitude); } if (options & SHOW_SPEED) { float knotsConvert = 1.852f; //default speed display=km/h if (dispOptions & OPTION_SPEED_IN_KNOTS) knotsConvert = 1.0f; //use knots if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%.0f ", outputStr, speed * knotsConvert); else sprintf(outputStr, "%sspd:%.0f ", outputStr, speed * knotsConvert); } if (options & SHOW_COURSE) { char courses[8][3] = { "N ", "NO", "O ", "SO", "S ", "SW", "W ", "NW" }; float degrees[8] = { 22.5f, 67.5f, 112.5f, 157.5f, 202.5f, 247.5f, 292.5f, 337.5f }; int selectedDegree = 0; int n; for (n = 0; n < 8; n++) { if (course < degrees[n]) { selectedDegree = n; break; } } if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%s ", outputStr, courses[selectedDegree]); else sprintf(outputStr, "%sdir:%s ", outputStr, courses[selectedDegree]); } if (options & SHOW_SATELLITES) { if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%d ", outputStr, satellites); else sprintf(outputStr, "%ssat:%d ", outputStr, satellites); } if (options & SHOW_QUALITY) { if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%d ", outputStr, quality); else sprintf(outputStr, "%squa:%d ", outputStr, quality); } if (options & SHOW_STATUS) { if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%c ", outputStr, gpsStatus); else sprintf(outputStr, "%ssta:%c ", outputStr, gpsStatus); } if (options & SHOW_TIME_UTC) { char digitizer[9]; //01:34:67 sprintf(digitizer, "%.6ld", gpsTime); //<012345> digitizer[7] = digitizer[5]; digitizer[6] = digitizer[4]; digitizer[4] = digitizer[3]; digitizer[3] = digitizer[2]; digitizer[2] = ':'; digitizer[5] = ':'; digitizer[8] = '\0'; if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%s ", outputStr, digitizer); else sprintf(outputStr, "%sutc:%s ", outputStr, digitizer); } if (options & SHOW_DATE) { char digitizer[9]; //01:34:67 sprintf(digitizer, "%.6ld", gpsDate); //<012345> digitizer[7] = digitizer[5]; digitizer[6] = digitizer[4]; digitizer[4] = digitizer[3]; digitizer[3] = digitizer[2]; digitizer[2] = '/'; digitizer[5] = '/'; digitizer[8] = '\0'; if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%s ", outputStr, digitizer); else sprintf(outputStr, "%sdat:%s ", outputStr, digitizer); } if (dispOptions & SHOW_NMEA_STATUS) { if (dispOptions & OPTION_NO_PREFIX) sprintf(outputStr, "%s%04d/%04d/%04d ", outputStr, msgCounter, errCounter, incomplCounter); else sprintf(outputStr, "%sOK:%03d/Er:%03d/In:%03d ", outputStr, msgCounter, errCounter, incomplCounter); } if (options == 0 && dispOptions == 0) { //error, no parameter defined! error("gps::parse() ERROR, no parameter specified!"); value = strdup("GPS ARG ERR"); } else { value = strdup(outputStr); } SetResult(&result, R_STRING, value); free(value); } /* plugin initialization */ /* MUST NOT be declared 'static'! */ int plugin_init_gps(void) { info("%s: v%s", Name, "0.2"); prepare_gps_parser(); /* register all our cool functions */ /* the second parameter is the number of arguments */ /* -1 stands for variable argument list */ AddFunction("gps::parse", 2, parse); return 0; } void plugin_exit_gps(void) { info("%s: shutting down plugin.", Name); #ifndef EMULATE close(fd_g); #endif } * Revision 1.11 2000/03/25 05:50:43 reinelt * * memory leak in Raster_flush closed * driver family logic changed * * Revision 1.10 2000/03/23 07:24:48 reinelt * * PPM driver up and running (but slow!) * * Revision 1.9 2000/03/22 07:33:50 reinelt * * FAQ added * new modules 'processor.c' contains all data processing * * Revision 1.8 2000/03/19 08:41:28 reinelt * * documentation available! README, README.MatrixOrbital, README.Drivers * added Skeleton.c as a starting point for new drivers * * Revision 1.7 2000/03/18 08:07:04 reinelt * * vertical bars implemented * bar compaction improved * memory information implemented * * Revision 1.6 2000/03/17 09:21:42 reinelt * * various memory statistics added * * Revision 1.5 2000/03/13 15:58:24 reinelt * * release 0.9 * moved row parsing to parser.c * all basic work finished * * Revision 1.4 2000/03/10 17:36:02 reinelt * * first unstable but running release * */ /* * * exported fuctions: * * struct LCD MatrixOrbital[] * */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <signal.h> #include <termios.h> #include <fcntl.h> #include "cfg.h" #include "lock.h" #include "display.h" #define SPEED 19200 #define XRES 5 #define YRES 8 #define CHARS 8 #define BARS ( BAR_L | BAR_R | BAR_U | BAR_D | BAR_H2 ) static LCD Lcd; static char *Port=NULL; static speed_t Speed; static int Device=-1; typedef struct { int len1; int len2; int type; int segment; } BAR; typedef struct { int len1; int len2; int type; int used; int ascii; } SEGMENT; static char Txt[4][40]; static BAR Bar[4][40]; static int nSegment=2; static SEGMENT Segment[128] = {{ len1:0, len2:0, type:255, used:0, ascii:32 }, { len1:255, len2:255, type:255, used:0, ascii:255 }}; static int MO_open (void) { int fd; pid_t pid; struct termios portset; if ((pid=lock_port(Port))!=0) { if (pid==-1) fprintf (stderr, "MatrixOrbital: port %s could not be locked\n", Port); else fprintf (stderr, "MatrixOrbital: port %s is locked by process %d\n", Port, pid); return -1; } fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY); if (fd==-1) { fprintf (stderr, "MatrixOrbital: open(%s) failed: %s\n", Port, strerror(errno)); return -1; } if (tcgetattr(fd, &portset)==-1) { fprintf (stderr, "MatrixOrbital: tcgetattr(%s) failed: %s\n", Port, strerror(errno)); return -1; } cfmakeraw(&portset); cfsetospeed(&portset, Speed); if (tcsetattr(fd, TCSANOW, &portset)==-1) { fprintf (stderr, "MatrixOrbital: tcsetattr(%s) failed: %s\n", Port, strerror(errno)); return -1; } return fd; } static void MO_write (char *string, int len) { if (Device==-1) return; if (write (Device, string, len)==-1) { if (errno==EAGAIN) { usleep(1000); if (write (Device, string, len)>=0) return; } fprintf (stderr, "MatrixOrbital: write(%s) failed: %s\n", Port, strerror(errno)); } } static int MO_contrast (void) { char buffer[4]; int contrast; contrast=atoi(cfg_get("Contrast")?:"160"); snprintf (buffer, 4, "\376P%c", contrast); MO_write (buffer, 3); return 0; } static void MO_process_bars (void) { int row, col; int i, j; for (i=2; i<nSegment && Segment[i].used; i++); for (j=i+1; j<nSegment; j++) { if (Segment[j].used) Segment[i++]=Segment[j]; } nSegment=i; for (row=0; row<Lcd.rows; row++) { for (col=0; col<Lcd.cols; col++) { if (Bar[row][col].type==0) continue; for (i=0; i<nSegment; i++) { if (Segment[i].type & Bar[row][col].type && Segment[i].len1== Bar[row][col].len1 && Segment[i].len2== Bar[row][col].len2) break; } if (i==nSegment) { nSegment++; Segment[i].len1=Bar[row][col].len1; Segment[i].len2=Bar[row][col].len2; Segment[i].type=Bar[row][col].type; Segment[i].used=0; Segment[i].ascii=-1; } Bar[row][col].segment=i; } } } static int MO_segment_diff (int i, int j) { int RES; int i1, i2, j1, j2; if (i==j) return 65535; if (!(Segment[i].type & Segment[j].type)) return 65535; if (Segment[i].len1==0 && Segment[j].len1!=0) return 65535; if (Segment[i].len2==0 && Segment[j].len2!=0) return 65535; RES=Segment[i].type & BAR_H ? XRES:YRES; if (Segment[i].len1>=RES && Segment[j].len1<RES) return 65535; if (Segment[i].len2>=RES && Segment[j].len2<RES) return 65535; if (Segment[i].len1==Segment[i].len2 && Segment[j].len1!=Segment[j].len2) return 65535; i1=Segment[i].len1; if (i1>RES) i1=RES; i2=Segment[i].len2; if (i2>RES) i2=RES; j1=Segment[j].len1; if (j1>RES) i1=RES; j2=Segment[j].len2; if (j2>RES) i2=RES; return (i1-i2)*(i1-i2)+(j1-j2)*(j1-j2); } static void MO_compact_bars (void) { int i, j, r, c, min; int pack_i, pack_j; int pass1=1; int error[nSegment][nSegment]; if (nSegment>CHARS+2) { for (i=2; i<nSegment; i++) { for (j=0; j<nSegment; j++) { error[i][j]=MO_segment_diff(i,j); } } while (nSegment>CHARS+2) { min=65535; pack_i=-1; pack_j=-1; for (i=2; i<nSegment; i++) { if (pass1 && Segment[i].used) continue; for (j=0; j<nSegment; j++) { if (error[i][j]<min) { min=error[i][j]; pack_i=i; pack_j=j; } } } if (pack_i==-1) { if (pass1) { pass1=0; continue; } else { fprintf (stderr, "MatrixOrbital: unable to compact bar characters\n"); nSegment=CHARS; break; } } nSegment--; Segment[pack_i]=Segment[nSegment]; for (i=0; i<nSegment; i++) { error[pack_i][i]=error[nSegment][i]; error[i][pack_i]=error[i][nSegment]; } for (r=0; r<Lcd.rows; r++) { for (c=0; c<Lcd.cols; c++) { if (Bar[r][c].segment==pack_i) Bar[r][c].segment=pack_j; if (Bar[r][c].segment==nSegment) Bar[r][c].segment=pack_i; } } } } } static void MO_define_chars (void) { int c, i, j; char buffer[12]="\376N"; for (i=2; i<nSegment; i++) { if (Segment[i].used) continue; if (Segment[i].ascii!=-1) continue; for (c=0; c<CHARS; c++) { for (j=2; j<nSegment; j++) { if (Segment[j].ascii==c) break; } if (j==nSegment) break; } Segment[i].ascii=c; buffer[2]=c; switch (Segment[i].type) { case BAR_L: for (j=0; j<4; j++) { char Pixel[] = { 0, 1, 3, 7, 15, 31 }; buffer[j+3]=Pixel[Segment[i].len1]; buffer[j+7]=Pixel[Segment[i].len2]; } break; case BAR_R: for (j=0; j<4; j++) { char Pixel[] = { 0, 16, 24, 28, 30, 31 }; buffer[j+3]=Pixel[Segment[i].len1]; buffer[j+7]=Pixel[Segment[i].len2]; } break; case BAR_U: for (j=0; j<Segment[i].len1; j++) { buffer[10-j]=31; } for (; j<YRES; j++) { buffer[10-j]=0; } break; case BAR_D: for (j=0; j<Segment[i].len1; j++) { buffer[j+3]=31; } for (; j<YRES; j++) { buffer[j+3]=0; } break; } MO_write (buffer, 11); } } int MO_clear (void) { int row, col; for (row=0; row<Lcd.rows; row++) { for (col=0; col<Lcd.cols; col++) { Txt[row][col]='\t'; Bar[row][col].len1=-1; Bar[row][col].len2=-1; Bar[row][col].type=0; Bar[row][col].segment=-1; } } MO_write ("\014", 1); return 0; } static void MO_quit (int signal); //forward decvlaration int MO_init (LCD *Self) { char *port; char *speed; Lcd=*Self; if (Port) { free (Port); Port=NULL; } port=cfg_get ("Port"); if (port==NULL || *port=='\0') { fprintf (stderr, "MatrixOrbital: no 'Port' entry in %s\n", cfg_file()); return -1; } Port=strdup(port); speed=cfg_get("Speed")?:"19200"; switch (atoi(speed)) { case 1200: Speed=B1200; break; case 2400: Speed=B2400; break; case 9600: Speed=B9600; break; case 19200: Speed=B19200; break; default: fprintf (stderr, "MatrixOrbital: unsupported speed '%s' in %s\n", speed, cfg_file()); return -1; } Device=MO_open(); if (Device==-1) return -1; signal(SIGINT, MO_quit); signal(SIGQUIT, MO_quit); signal(SIGTERM, MO_quit); MO_clear(); MO_contrast(); MO_write ("\376B", 3); // backlight on MO_write ("\376K", 2); // cursor off MO_write ("\376T", 2); // blink off MO_write ("\376D", 2); // line wrapping off MO_write ("\376R", 2); // auto scroll off MO_write ("\376V", 2); // GPO off return 0; } int MO_put (int row, int col, char *text) { char *p=&Txt[row][col]; char *t=text; while (*t && col++<=Lcd.cols) { *p++=*t++; } return 0; } int MO_bar (int type, int row, int col, int max, int len1, int len2) { int rev=0; if (len1<1) len1=1; else if (len1>max) len1=max; if (len2<1) len2=1; else if (len2>max) len2=max; switch (type) { case BAR_L: len1=max-len1; len2=max-len2; rev=1; case BAR_R: while (max>0 && col<=Lcd.cols) { Bar[row][col].type=type; Bar[row][col].segment=-1; if (len1>=XRES) { Bar[row][col].len1=rev?0:XRES; len1-=XRES; } else { Bar[row][col].len1=rev?XRES-len1:len1; len1=0; } if (len2>=XRES) { Bar[row][col].len2=rev?0:XRES; len2-=XRES; } else { Bar[row][col].len2=rev?XRES-len2:len2; len2=0; } max-=XRES; col++; } break; case BAR_U: len1=max-len1; len2=max-len2; rev=1; case BAR_D: while (max>0 && row<=Lcd.rows) { Bar[row][col].type=type; Bar[row][col].segment=-1; if (len1>=YRES) { Bar[row][col].len1=rev?0:YRES; len1-=YRES; } else { Bar[row][col].len1=rev?YRES-len1:len1; len1=0; } if (len2>=YRES) { Bar[row][col].len2=rev?0:YRES; len2-=YRES; } else { Bar[row][col].len2=rev?YRES-len2:len2; len2=0; } max-=YRES; row++; } break; } return 0; } int MO_flush (void) { char buffer[256]="\376G"; char *p; int s, row, col; MO_process_bars(); MO_compact_bars(); MO_define_chars(); for (s=0; s<nSegment; s++) { Segment[s].used=0; } for (row=0; row<Lcd.rows; row++) { buffer[3]=row+1; for (col=0; col<Lcd.cols; col++) { s=Bar[row][col].segment; if (s!=-1) { Segment[s].used=1; Txt[row][col]=Segment[s].ascii; } } for (col=0; col<Lcd.cols; col++) { if (Txt[row][col]=='\t') continue; buffer[2]=col+1; for (p=buffer+4; col<Lcd.cols; col++, p++) { if (Txt[row][col]=='\t') break; *p=Txt[row][col]; } MO_write (buffer, p-buffer); } } return 0; } int lcd_hello (void); // prototype from lcd4linux.c static void MO_quit (int signal) { MO_clear(); lcd_hello(); close (Device); unlock_port(Port); exit (0); } LCD MatrixOrbital[] = { { "LCD0821", 2, 8, XRES, YRES, BARS, MO_init, MO_clear, MO_put, MO_bar, MO_flush }, { "LCD1621", 2, 16, XRES, YRES, BARS, MO_init, MO_clear, MO_put, MO_bar, MO_flush }, { "LCD2021", 2, 20, XRES, YRES, BARS, MO_init, MO_clear, MO_put, MO_bar, MO_flush }, { "LCD2041", 4, 20, XRES, YRES, BARS, MO_init, MO_clear, MO_put, MO_bar, MO_flush }, { "LCD4021", 2, 40, XRES, YRES, BARS, MO_init, MO_clear, MO_put, MO_bar, MO_flush }, { NULL } };