Advertisement
AsmodHacker

Arduino GPS/UTFT

Mar 1st, 2015
3,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clone C 10.92 KB | None | 0 0
  1. //Matt aka Asmodeus
  2. //Created for arduino mega using sainsmart 3.2" touch screen display with adafruit gps v3
  3. //Motorcycle GPS Speedometer
  4. //Requires Fonts \/
  5. //1: http://www.henningkarlsen.com/electronics/dlfont.php?id=31&t=a
  6. //2: http://www.henningkarlsen.com/electronics/dlfont.php?id=7&t=a
  7. //Add the fonts to the UTFT FONT.c
  8.  
  9. #include <Adafruit_GPS.h>
  10. #include <SoftwareSerial.h>
  11. #include <UTFT.h>
  12. #include <UTouch.h>
  13.  
  14. SoftwareSerial mySerial(13, 11);
  15. UTFT myGLCD(ITDB32S,38,39,40,41);
  16. UTouch myTouch( 6, 5, 4, 3, 2);
  17.  
  18. extern uint8_t LargeFont[]; //Huge Font
  19. extern uint8_t SmallFont[]; //Small Font
  20. extern uint8_t DingFont[]; //Icons Small
  21. extern uint8_t SwissFont[]; //Ghosty Type Font
  22.  
  23. Adafruit_GPS GPS(&mySerial);
  24.  
  25. #define GPSECHO  true
  26.  
  27. word colorlist[] = {
  28.   VGA_WHITE, VGA_BLACK, VGA_RED, VGA_GREEN, VGA_BLUE};
  29. boolean usingInterrupt = false;
  30. void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
  31.  
  32. void select_Color(int col)
  33. {
  34.   myGLCD.setColor(colorlist[col]);
  35. }
  36.  
  37. void setup()  
  38. {
  39.   Serial.begin(115200);
  40.   //GPS ECT
  41.   GPS.begin(9600);
  42.   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  43.   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  44.   GPS.sendCommand(PGCMD_ANTENNA);
  45.   useInterrupt(true);
  46.   delay(1000);
  47.   mySerial.println(PMTK_Q_RELEASE);
  48.   myGLCD.InitLCD();
  49.   myGLCD.clrScr();
  50.   myTouch.InitTouch();
  51.   myTouch.setPrecision(PREC_HI);
  52.   //Setup The Displays GUI
  53.   myGLCD.setBackColor(VGA_TRANSPARENT);
  54.   init_GUI();
  55. }
  56.  
  57. String old_FIX = "x";
  58. String old_DEG = "x";
  59. String old_MPH = "x";
  60. String old_SAT = "x";
  61. String old_QUA = "x";
  62. String old_ALT = "x";
  63. String new_FIX = "x";
  64. String new_DEG = "x";
  65. String new_MPH = "x";
  66. String new_SAT = "x";
  67. String new_QUA = "x";
  68. String new_ALT = "x";
  69. String old_HOU = "x";
  70. String new_HOU = "x";
  71. String old_MIN = "x";
  72. String new_MIN = "x";
  73. String old_SEC = "x";
  74. String new_SEC = "x";
  75. String old_DAY = "x";
  76. String new_DAY = "x";
  77. String old_MON = "x";
  78. String new_MON = "x";
  79. String old_YEA = "x";
  80. String new_YEA = "x";
  81.  
  82. int mode = 1;
  83. int words = 1;
  84. int back = 0;
  85. void init_GUI() {
  86.   if(mode == 0)
  87.   {
  88.     words = 0;
  89.     back = 1;
  90.     myGLCD.setColor(VGA_BLACK);
  91.     myGLCD.fillRect(0, 0, 319, 239);
  92.   }
  93.   else {
  94.     words = 1;
  95.     back = 0;
  96.     myGLCD.setColor(VGA_WHITE);
  97.     myGLCD.fillRect(0, 0, 319, 239);
  98.   }
  99.  
  100.   //W = [CLOCK] DINGFONT
  101.   //E = [HOUR GLASS] DINGFONT
  102.   //} = [MOTORCYCLE] DINGFONT
  103.  
  104.   select_Color(3);
  105.   myGLCD.drawRect(10 - 5, 235, 40 - 5, 225);
  106.   myGLCD.drawRect(45 - 5, 235, 75 - 5, 215);
  107.   myGLCD.drawRect(80 - 5, 235, 110 - 5, 205);
  108.   myGLCD.drawRect(115 - 5,235, 145 - 5, 195);
  109.   myGLCD.drawRect(150 - 5,235, 180 - 5, 185);
  110.   myGLCD.drawRect(185 - 5,235, 215 - 5, 175);
  111.   myGLCD.drawRect(220 - 5,235, 250 - 5, 165);
  112.   myGLCD.drawRect(255 - 5,235, 285 - 5, 155);
  113.   myGLCD.drawRect(290 - 5,235, 319 - 5, 145);
  114.   select_Color(4);
  115.   myGLCD.setFont(DingFont);
  116.   myGLCD.print("#",286,5); //Day-Night Button Switch [Might add a LDR - Light Dependent Resistor]
  117.   myGLCD.print("}",236,38); //Motorcycle Icon
  118.   select_Color(words);
  119.   myGLCD.setFont(SmallFont);
  120.   myGLCD.print("Locate Fix:",10,10);
  121.   myGLCD.print("Satellites:",10,20);
  122.   myGLCD.setFont(SwissFont);
  123.   myGLCD.print("MPH",190,45);
  124.   myGLCD.setFont(SmallFont);
  125.   myGLCD.print("Location:",10,30);
  126.   myGLCD.print("Altitude:",10,40);
  127.   old_FIX = "x";
  128.   old_DEG = "x";
  129.   old_MPH = "x";
  130.   old_SAT = "x";
  131.   old_QUA = "x";
  132.   old_ALT = "x";
  133.   new_FIX = "x";
  134.   new_DEG = "x";
  135.   new_MPH = "x";
  136.   new_SAT = "x";
  137.   new_QUA = "x";
  138.   new_ALT = "x";
  139.   old_HOU = "x";
  140.   new_HOU = "x";
  141.   old_MIN = "x";
  142.   new_MIN = "x";
  143.   old_SEC = "x";
  144.   new_SEC = "x";
  145.   old_DAY = "x";
  146.   new_DAY = "x";
  147.   old_MON = "x";
  148.   new_MON = "x";
  149.   old_YEA = "x";
  150.   new_YEA = "x";
  151. }
  152.  
  153. // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
  154. SIGNAL(TIMER0_COMPA_vect) {
  155.   char c = GPS.read();
  156. }
  157.  
  158. void useInterrupt(boolean v) {
  159.   if (v) {
  160.     OCR0A = 0xAF;
  161.     TIMSK0 |= _BV(OCIE0A);
  162.     usingInterrupt = true;
  163.   }
  164.   else {
  165.     TIMSK0 &= ~_BV(OCIE0A);
  166.     usingInterrupt = false;
  167.   }
  168. }
  169.  
  170. int test_SPEED = 80;
  171. uint32_t timer = millis();
  172. void loop()                     // run over and over again
  173. {
  174.   myGLCD.setBackColor(VGA_TRANSPARENT);
  175.   // if a sentence is received, we can check the checksum, parse it...
  176.   if (GPS.newNMEAreceived()) {
  177.     if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
  178.       return;  // we can fail to parse a sentence in which case we should just wait for another
  179.   }
  180.  
  181.   // if millis() or timer wraps around, we'll just reset it
  182.   if (timer > millis())  timer = millis();
  183.  
  184.   if (millis() - timer > 100) {
  185.     timer = millis(); // reset the timer
  186.  
  187.     new_HOU = String(GPS.hour);
  188.     new_MIN = String(GPS.minute);
  189.     new_SEC = String(GPS.seconds);
  190.     new_DAY = String(GPS.day);
  191.     new_MON = String(GPS.month);
  192.     new_YEA = String(GPS.year);
  193.     new_FIX = String((int)GPS.fix);
  194.     new_MPH = String(int((int)GPS.speed * 1.15078));
  195.     new_DEG = String(GPS.latitudeDegrees,4) + "," + String(GPS.longitudeDegrees,4);
  196.     new_SAT = String((int)GPS.satellites);
  197.     new_ALT = String(GPS.altitude);
  198.     new_QUA = String(GPS.fixquality);
  199.  
  200.     if(new_MIN.length() == 1) {
  201.       new_MIN = "0" + new_MIN;
  202.     }
  203.     if(new_HOU.length() == 1) {
  204.       new_HOU = "0" + new_HOU;
  205.     }
  206.  
  207.     if(old_HOU != new_HOU || old_MIN != new_MIN)
  208.     {
  209.       myGLCD.setFont(SmallFont);
  210.       select_Color(back);
  211.       myGLCD.print("Time: " + old_HOU + ":" + old_MIN ,10,100);
  212.       select_Color(words);
  213.       myGLCD.print("Time: " + new_HOU + ":" + new_MIN ,10,100);
  214.       old_HOU = new_HOU;
  215.       old_MIN = new_MIN;
  216.       old_SEC = new_SEC;
  217.     }
  218.  
  219.     if(old_DAY != new_DAY || old_MON != new_MON || old_YEA != new_YEA)
  220.     {
  221.       myGLCD.setFont(SmallFont);
  222.       select_Color(back);
  223.       myGLCD.print("Date: " + old_DAY + "/" + old_MON + "/" + old_YEA,10,110);
  224.       select_Color(words);
  225.       myGLCD.print("Date: " + new_DAY + "/" + new_MON + "/" + new_YEA,10,110);
  226.       old_DAY = new_DAY;
  227.       old_MON = new_MON;
  228.       old_YEA = new_YEA;
  229.     }
  230.  
  231.     if(new_FIX == "1") {
  232.       new_FIX = "Yes";
  233.     }
  234.     else {
  235.       new_FIX = "No";
  236.     }
  237.  
  238.     if (GPS.fix == 1) {
  239.       myGLCD.setFont(SmallFont);
  240.       select_Color(back);
  241.       myGLCD.print("WAITING FOR FIX",10,50);
  242.       myGLCD.setFont(DingFont);
  243.       myGLCD.print("e",130,45);
  244.  
  245.  
  246.       myGLCD.setFont(SmallFont);
  247.       if(new_FIX != old_FIX) {
  248.         select_Color(back);
  249.         myGLCD.print(old_FIX,100,10);
  250.         select_Color(words);
  251.         myGLCD.print(new_FIX,100,10);
  252.         old_FIX = new_FIX;
  253.       }
  254.       myGLCD.setFont(LargeFont);
  255.       if(new_MPH != old_MPH) {
  256.         select_Color(back);
  257.         myGLCD.print(old_MPH,190,60);
  258.         select_Color(words);
  259.         myGLCD.print(new_MPH,190,60);
  260.         old_MPH = new_MPH;
  261.       }
  262.       myGLCD.setFont(SmallFont);
  263.  
  264.       if(new_SAT != old_SAT) {
  265.         select_Color(back);
  266.         myGLCD.print(old_SAT,100,20);  
  267.         select_Color(words);
  268.         myGLCD.print(new_SAT,100,20);
  269.         old_SAT = new_SAT;
  270.       }
  271.       if(new_ALT != old_ALT) {
  272.         select_Color(back);
  273.         myGLCD.print(old_ALT,85,40);  
  274.         select_Color(words);
  275.         myGLCD.print(new_ALT,85,40);
  276.         old_ALT = new_ALT;
  277.       }
  278.       if(new_DEG != old_DEG) {
  279.         select_Color(back);
  280.         myGLCD.print(old_DEG,85,30);
  281.         select_Color(words);
  282.         myGLCD.print(new_DEG,85,30);  
  283.         old_DEG = new_DEG;
  284.       }
  285.      
  286.       //SPEED BAR DISPLAY
  287.       int SPEED_BAR = new_MPH.toInt();
  288.       if(SPEED_BAR >= 10)
  289.       { select_Color(4);
  290.         myGLCD.fillRect(10 - 5 + 2, 235 - 2, 40 - 5 - 2, 225 + 2);
  291.       } else { select_Color(back); myGLCD.fillRect(10 - 5 + 2, 235 - 2, 40 - 5 - 2, 225 + 2);}
  292.      
  293.       if(SPEED_BAR >= 20)
  294.       { select_Color(4);
  295.         myGLCD.fillRect(45 - 5 + 2, 235 - 2, 75 - 5 - 2, 215 + 2);
  296.       } else { select_Color(back); myGLCD.fillRect(45 - 5 + 2, 235 - 2, 75 - 5 - 2, 215 + 2);}
  297.      
  298.       if(SPEED_BAR >= 30 )
  299.       { select_Color(4);
  300.         myGLCD.fillRect(80 - 5 + 2, 235 - 2, 110 - 5 - 2, 205 + 2);
  301.       } else { select_Color(back); myGLCD.fillRect(80 - 5 + 2, 235 - 2, 110 - 5 - 2, 205 + 2);}
  302.      
  303.       if(SPEED_BAR >= 40)
  304.       { select_Color(4);
  305.         myGLCD.fillRect(115 - 5 + 2,235 - 2, 145 - 5 - 2, 195 + 2);
  306.       } else { select_Color(back); myGLCD.fillRect(115 - 5 + 2,235 - 2, 145 - 5 - 2, 195 + 2);}
  307.      
  308.       if(SPEED_BAR >= 50)
  309.       { select_Color(4);
  310.         myGLCD.fillRect(150 - 5 + 2,235 - 2, 180 - 5 - 2, 185 + 2);
  311.       } else { select_Color(back); myGLCD.fillRect(150 - 5 + 2,235 - 2, 180 - 5 - 2, 185 + 2);}
  312.      
  313.       if(SPEED_BAR >= 60)
  314.       { select_Color(4);
  315.         myGLCD.fillRect(185 - 5 + 2,235 - 2, 215 - 5 - 2, 175 + 2);
  316.       } else { select_Color(back); myGLCD.fillRect(185 - 5 + 2,235 - 2, 215 - 5 - 2, 175 + 2);}
  317.      
  318.       if(SPEED_BAR >= 70)
  319.       { select_Color(4);
  320.         myGLCD.fillRect(220 - 5 + 2,235 - 2, 250 - 5 - 2, 165 + 2);
  321.       } else { select_Color(back); myGLCD.fillRect(220 - 5 + 2,235 - 2, 250 - 5 - 2, 165 + 2);}
  322.      
  323.       if(SPEED_BAR >= 80)
  324.       { select_Color(4);
  325.         myGLCD.fillRect(255 - 5 + 2,235 - 2, 285 - 5 - 2, 155 + 2);
  326.       } else { select_Color(back); myGLCD.fillRect(255 - 5 + 2,235 - 2, 285 - 5 - 2, 155 + 2);}
  327.      
  328.       if(SPEED_BAR >= 90)
  329.       { select_Color(4);
  330.         myGLCD.fillRect(290 - 5 + 2,235 - 2, 319 - 5 - 2, 145 + 2);
  331.       } else { select_Color(back); myGLCD.fillRect(290 - 5 + 2,235 - 2, 319 - 5 - 2, 145 + 2);}
  332.      
  333.     }
  334.     else {
  335.       myGLCD.setFont(SmallFont);
  336.       select_Color(2);
  337.       myGLCD.print("WAITING FOR FIX",10,50);
  338.       myGLCD.setFont(DingFont);
  339.       myGLCD.print("e",130,45);
  340.       myGLCD.setFont(SmallFont);
  341.       if(new_FIX != old_FIX) {
  342.         select_Color(back);
  343.         myGLCD.print(old_FIX,100,10);
  344.         select_Color(words);
  345.         myGLCD.print(new_FIX,100,10);
  346.         old_FIX = new_FIX;
  347.       }
  348.       if(new_SAT != old_SAT) {
  349.         select_Color(back);
  350.         myGLCD.print(old_SAT,100,20);  
  351.         select_Color(words);
  352.         myGLCD.print(new_SAT,100,20);
  353.         old_SAT = new_SAT;
  354.       }
  355.     }
  356.     myGLCD.setBackColor(VGA_TRANSPARENT);
  357.   }
  358.  
  359.   //DISPLAY TOUCH ECT
  360.   long x, y;
  361.  
  362.   while (myTouch.dataAvailable() == true)
  363.   {
  364.     myTouch.read();
  365.     x = myTouch.getX();
  366.     y = myTouch.getY();
  367.  
  368.     //Day,Night Button
  369.     if(x >= 300 && x <= myGLCD.getDisplayXSize() && y >= 0 && y <= 25)
  370.    {
  371.      if(mode == 0) {
  372.        mode = 1;
  373.       }
  374.       else {
  375.         mode = 0;
  376.       }
  377.       init_GUI();
  378.     }
  379.     test_SPEED -= 3;
  380.     //vector on touch
  381.     //select_Color(3);
  382.     //myGLCD.fillRect(136,30,218,43);
  383.     //select_Color(4);
  384.     //myGLCD.print("X:" + String(myTouch.getX()),137,31);
  385.     //myGLCD.print("Y:" + String(myTouch.getY()),178,31);
  386.   }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement