Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. //начало вставки кода №1
  3. #include <Wire.h>
  4. //#include <LiquidCrystal_I2C.h>
  5. //конец вставки кода №1
  6. #include <TinyGPS.h>
  7.  
  8. /* This sample code demonstrates the normal use of a TinyGPS object.
  9.    It requires the use of SoftwareSerial, and assumes that you have a
  10.    4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  11. */
  12.  
  13. TinyGPS gps;
  14. SoftwareSerial ss(2, 7);
  15.  
  16. static void smartdelay(unsigned long ms);
  17. static void print_float(float val, float invalid, int len, int prec);
  18. static void print_int(unsigned long val, unsigned long invalid, int len);
  19. static void print_date(TinyGPS &gps);
  20. static void print_str(const char *str, int len);
  21. //начало вставки кода №2
  22. //LiquidCrystal_I2C lcd(0x27, 20, 4);
  23. //конец вставки кода №2
  24. void setup()
  25. {
  26.   Serial.begin(9600);
  27.  
  28.  // Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  29.   Serial.println("by Mikal Hart");
  30.   Serial.println();
  31.   Serial.println("Sats HDOP Latitude  Longitude  Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  32.   Serial.println("          (deg)     (deg)      Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  33.   Serial.println("-------------------------------------------------------------------------------------------------------------------------------------");
  34.  
  35.   ss.begin(9600);
  36.  
  37.  //начало вставки кода №3
  38. //  lcd.begin();
  39. //lcd.backlight();
  40. //конец вставки кода №3
  41. }
  42.  
  43. void loop()
  44. {
  45.   float flat, flon;
  46.   unsigned long age, date, time, chars = 0;
  47.   unsigned short sentences = 0, failed = 0;
  48.   static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  49.  
  50.  
  51. //начало вставки кода №4
  52. //      lcd.clear();
  53. //lcd.print(gps.f_altitude());
  54. //lcd.print(" N ");
  55. //lcd.print(flat, 6);
  56. //lcd.setCursor(0, 1);
  57. //lcd.print(" E ");
  58. //lcd.print(flon, 6);
  59. //начало вставки кода №4
  60.  
  61.  
  62.   print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  63.   print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  64.   gps.f_get_position(&flat, &flon, &age);
  65.   print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  66.   print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  67.   print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  68.   print_date(gps);
  69.   print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  70.   print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  71.   print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  72.   print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  73.   print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  74.   print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  75.   print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);
  76.  
  77.   gps.stats(&chars, &sentences, &failed);
  78.   print_int(chars, 0xFFFFFFFF, 6);
  79.   print_int(sentences, 0xFFFFFFFF, 10);
  80.   print_int(failed, 0xFFFFFFFF, 9);
  81.   Serial.println();
  82.  
  83.  
  84.  
  85.  
  86.   smartdelay(1000);
  87. }
  88.  
  89. static void smartdelay(unsigned long ms)
  90. {
  91.   unsigned long start = millis();
  92.   do
  93.   {
  94.     while (ss.available())
  95.       gps.encode(ss.read());
  96.   } while (millis() - start < ms);
  97. }
  98.  
  99. static void print_float(float val, float invalid, int len, int prec)
  100. {
  101.   if (val == invalid)
  102.   {
  103.     while (len-- > 1)
  104.       Serial.print('*');
  105.     Serial.print(' ');
  106.   }
  107.   else
  108.   {
  109.     Serial.print(val, prec);
  110.     int vi = abs((int)val);
  111.     int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  112.     flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  113.     for (int i=flen; i<len; ++i)
  114.       Serial.print(' ');
  115.   }
  116.   smartdelay(0);
  117. }
  118.  
  119. static void print_int(unsigned long val, unsigned long invalid, int len)
  120. {
  121.   char sz[32];
  122.   if (val == invalid)
  123.     strcpy(sz, "*******");
  124.   else
  125.     sprintf(sz, "%ld", val);
  126.   sz[len] = 0;
  127.   for (int i=strlen(sz); i<len; ++i)
  128.     sz[i] = ' ';
  129.   if (len > 0)
  130.     sz[len-1] = ' ';
  131.   Serial.print(sz);
  132.   smartdelay(0);
  133. }
  134.  
  135. static void print_date(TinyGPS &gps)
  136. {
  137.   int year;
  138.   byte month, day, hour, minute, second, hundredths;
  139.   unsigned long age;
  140.   gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  141.   if (age == TinyGPS::GPS_INVALID_AGE)
  142.     Serial.print("********** ******** ");
  143.   else
  144.   {
  145.     char sz[32];
  146.     sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
  147.         month, day, year, hour, minute, second);
  148.     Serial.print(sz);
  149.   }
  150.   print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  151.   smartdelay(0);
  152. }
  153.  
  154. static void print_str(const char *str, int len)
  155. {
  156.   int slen = strlen(str);
  157.   for (int i=0; i<len; ++i)
  158.     Serial.print(i<slen ? str[i] : ' ');
  159.   smartdelay(0);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement