Advertisement
stanleyseow

Marinus APRS Map on DUE/DigiX

Dec 9th, 2014
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.33 KB | None | 0 0
  1. // Marinus APRS Map Display
  2. // Copyright 2012 Leigh L. Klotz, Jr.
  3. // License: http://www.opensource.org/licenses/mit-license
  4.  
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_ILI9340.h> // Using 2.2" TFT ILI9340
  7. #include <SPI.h>
  8. #include <SD.h>
  9. #include <MicroAPRS.h>        // Using MicroAPRS libs
  10.  
  11. #define SD_CS    87          // Chip select line for DigiX SD card
  12. #define _cs 48
  13. #define _rst 49
  14. #define _dc 50
  15.  
  16. // LCD Size
  17. // #define ILI9340_TFTHEIGHT 320
  18. // #define ILI9340_TFTWIDTH  240
  19.  
  20. #define LCD_HEIGHT ILI9340_TFTHEIGHT
  21. #define LCD_WIDTH ILI9340_TFTWIDTH
  22. //#define LCD_HEIGHT 160
  23. //#define LCD_WIDTH 128
  24.  
  25. // Colors
  26. #define CALL_TEXT_COLOR ILI9340_RED
  27. #define ICON_COLOR ILI9340_BLUE
  28. #define LINE_COLOR ILI9340_BLUE
  29.  
  30. // APRS Buffers
  31. #define BUFLEN (260)
  32. char packet[BUFLEN];
  33. int buflen = 0;
  34.  
  35. // Maps and calls
  36. int last_map_n;
  37. char last_call[10];
  38. int last_pix_down;
  39. int last_pix_right;
  40.  
  41. // MicroAPRS decoded fields
  42. char *call, *posit, *pmsgTo, *pmsg;
  43. char type, pmsgID;
  44. long lat, lon;
  45.  
  46. // POI and Map info
  47. // Location of center tile upper left corner
  48. long POI_TL_LAT;
  49. long POI_TL_LON;
  50.  
  51. int DEGREES_PER_PIXEL_LAT;
  52. int DEGREES_PER_PIXEL_LON;
  53.  
  54. byte MAP_WIDTH_IN_TILES;
  55. byte MAP_HEIGHT_IN_TILES;
  56.  
  57. Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);
  58.  
  59. //ArgentRadioShield  = MicroAPRS(&Serial);
  60. // Serial3 Rx3 is pin besides 96
  61. // Modem connected to Serial3 of DigiX
  62. MicroAPRS microaprs = MicroAPRS(&Serial3);
  63.  
  64.  
  65. #define FLOOR(a,b) (a < 0) ? (a/b)-1 : (a/b)
  66.  
  67. // Turn on/off DEUBGGING
  68. #define DEBUG true
  69.  
  70.  
  71. void setup(void) {
  72.  
  73. #ifdef DEBUG  
  74.       Serial.begin(9600);
  75.         while (!Serial) ;
  76.       Serial.println("DEBUG ON");;
  77. #endif
  78.  
  79.   Serial3.begin(9600);
  80.  
  81.   tft.begin();    
  82.   tft.setRotation(0);
  83.   tft.setTextWrap(true);
  84.   tft.setCursor(0,0);
  85.   tft.fillScreen(ILI9340_BLUE);
  86.   tft.setTextColor(ILI9340_WHITE);
  87.   tft.print(F("Marinus APRS Mapper\n\n(C) 2012 WA5ZNU\nMIT license\n\n"
  88.           "Data imagery and map\n"
  89.           "information provided\n"
  90.           "by MapQuest,\n"
  91.           "openstreetmap.org,\n"
  92.           "and contributors:\nCC-BY-SA-2.0\n\n"));
  93.  
  94.   if (!SD.begin(SD_CS)) {
  95.     tft.setTextColor(ILI9340_RED);
  96.     tft.print(F("SD failed!"));
  97.     return;
  98.   }
  99.  
  100.   if (! readMapPOI()) {
  101.         tft.setTextColor(ILI9340_RED);
  102.         tft.print(F("Read POI failed!\n"));
  103.         return;
  104.   }
  105.  
  106.  
  107.    tft.print(F("Drawing init map 0,0"));
  108.    
  109.    // Still not working ....
  110.    //bmpDraw("foo.bmp", 0, 0);
  111.  
  112.   delay(2000);
  113.  
  114.  
  115.   // start with center map.  inits last_map_n as well.
  116.  
  117.   drawMap(0, 0);
  118. }
  119.  
  120. void loop() {
  121.   while (Serial3.available()) {
  122.     char ch = microaprs.read();
  123.     if (ch == '\n') {
  124.       packet[buflen] = 0;
  125.       show_packet();
  126.       buflen = 0;
  127.     } else if ((ch > 31 || ch == 0x1c || ch == 0x1d || ch == 0x27) && buflen < BUFLEN) {
  128.       // Mic-E uses some non-printing characters
  129.       packet[buflen++] = ch;
  130.     }
  131.   }
  132. }
  133.  
  134. void show_packet() {
  135.  
  136. // Moved to global variables
  137. //  char *call, *posit, *pmsgTo, *pmsg;
  138. //  char type, pmsgID;
  139. //  long lat, lon;
  140.  
  141. #ifdef DEBUG    
  142.     Serial.print(F(" * "));
  143.     Serial.println(packet);
  144. #endif  
  145.  
  146.   if (microaprs.decode_posit(packet, &call, &type, &posit, &lon, &lat, &pmsgTo, &pmsg, &pmsgID )) {
  147. #ifdef DEBUG    
  148.       Serial.print(F(" call ")); Serial.print(call);
  149.       Serial.print(F(" packet type ")); Serial.print(type);
  150.       Serial.print(F(" posit ")); Serial.print(posit);
  151.       Serial.print(F(" lat ")); Serial.print(lat, DEC);
  152.       Serial.print(F(" lon ")); Serial.print(lon, DEC);
  153.       Serial.println();
  154. #endif
  155.  
  156.     long dy = (POI_TL_LAT - lat) / DEGREES_PER_PIXEL_LAT;
  157.     long dx = (lon - POI_TL_LON) / DEGREES_PER_PIXEL_LON;
  158.  
  159. #ifdef DEBUG    
  160.       Serial.print(F(" down ")); Serial.print(dy, DEC);
  161.       Serial.print(F(" right ")); Serial.println(dx, DEC);
  162. #endif
  163.     int map_down = FLOOR(dy, LCD_HEIGHT);
  164.     int map_right = FLOOR(dx, LCD_WIDTH);
  165.  
  166. #ifdef DEBUG    
  167.       Serial.print(F(" map down ")); Serial.println(map_down, DEC);
  168.       Serial.print(F(" map right ")); Serial.println(map_right, DEC);
  169. #endif    
  170.  
  171.     int pix_down = dy % LCD_HEIGHT;
  172.     int pix_right = dx % LCD_WIDTH;
  173.  
  174. #ifdef DEBUG    
  175.       Serial.print(F(" in map down ")); Serial.print(pix_down, DEC);
  176.       Serial.print(F(" in map right ")); Serial.println(pix_right, DEC);
  177. #endif
  178.  
  179.     display(call, posit, map_down, map_right, pix_down, pix_right);
  180.   } else {
  181.  
  182. #ifdef DEBUG    
  183.       Serial.println(F(" !!! Decode failed\n"));
  184. #endif
  185.   }
  186. }
  187.  
  188. void display(char *call, char *posit, int map_down,
  189.          int map_right, int pix_down, int pix_right) {
  190.   // If the map is too many tiles away from the center, it's out of range so don't display.
  191.   if ((abs(map_down) > MAP_HEIGHT_IN_TILES/2) || (abs(map_right) > MAP_WIDTH_IN_TILES/2))
  192.       return;
  193.  
  194.  
  195.   // Find the map number, and display it if it's not the current map.
  196.   drawMap(map_down, map_right);
  197.  
  198.   // Find the spot within the map.  Adjust negative coordinates
  199.   if (pix_right < 0) pix_right = LCD_WIDTH + pix_right;
  200.   if (pix_down < 0) pix_down = LCD_HEIGHT + pix_down;
  201.  
  202.   // If it's the same callsign as last time, draw a line.
  203.   if (strcmp(call, last_call) == 0) {
  204.     if (last_pix_down != 0) {
  205.      
  206.       // Disable line drawing
  207.       //tft.drawLine(last_pix_right, last_pix_down, pix_right, pix_down, LINE_COLOR);
  208.     }
  209.   } else {
  210.     // If not the same callsign, draw the callsign and start a new spot.
  211.     bmdrawtext(call, CALL_TEXT_COLOR, pix_right, pix_down);
  212.  
  213.     bmdrawtext(call, ILI9340_RED, 1,1 );
  214.    
  215.     tft.setCursor(1, 9);
  216.     tft.setTextColor(ILI9340_BLUE);
  217.     tft.print(lat);
  218.  
  219.     tft.setCursor(1,17);
  220.     tft.setTextColor(ILI9340_BLUE);
  221.     tft.print(lon);
  222.  
  223.     bmdrawtext(posit,ILI9340_BLUE, 1,26);
  224.  
  225.  
  226.     // Remember the new callsign
  227.     strlcpy(last_call, call, sizeof(last_call));
  228.   }
  229.   // Remember the last point we drew for this callsign.
  230.   last_pix_down = pix_down;
  231.   last_pix_right = pix_right;
  232.  
  233.   // Draw a simple icon at the position.
  234.   tft.fillCircle(pix_right, pix_down, 2, ICON_COLOR);
  235. }
  236.  
  237. // find the map file and load it if it's not the current map.
  238. void drawMap(int map_down, int map_right) {
  239.   char name[] = "map####.bmp";
  240.   map_down += MAP_HEIGHT_IN_TILES/2;
  241.   map_right += MAP_WIDTH_IN_TILES/2;
  242.   int n = map_right * 100 + map_down;
  243.  
  244.   // Do not check for last map, draw maps on every Rx
  245.   //if (n != last_map_n) {
  246.     char *p = name+3;
  247.     *p++ = '0' + map_right / 10;
  248.     *p++ = '0' + map_right % 10;
  249.     *p++ = '0' + map_down / 10;
  250.     *p++ = '0' + map_down % 10;
  251.     bmpDraw(name, 0, 0);
  252.  
  253.     // Set the map number and reset callsign and last icon positions.
  254.     last_map_n = n;
  255.     last_call[0] = 0;
  256.     last_pix_down = -1;
  257.     last_pix_right = -1;
  258.   //}
  259. }
  260.  
  261. void bmdrawtext(char *text, uint16_t color, byte x, byte y) {
  262.   tft.setCursor(x, y);
  263.   tft.setTextColor(color);
  264.   tft.setTextWrap(true);
  265.   tft.print(text);
  266. }
  267.  
  268. boolean readMapPOI() {
  269.   File poi;
  270.   char *fn = "POI.CSV";
  271.   if ( (poi = SD.open("POI.CSV") ) == NULL) {
  272.     tft.print("Error reading fn :");
  273.     tft.print(fn);
  274.     return false;
  275.   }
  276.  
  277.   // skip first line of header text
  278.   while (poi.read() != '\n') {
  279.     ;
  280.   }
  281.  
  282.  
  283.   if (LCD_WIDTH != poi.parseInt()) {
  284.     tft.print("Width mismatch");
  285.     return false;
  286.   }
  287.  
  288.   if (LCD_HEIGHT != poi.parseInt()) {
  289.     tft.print("Height mismatch");    
  290.     return false;
  291.   }
  292.  
  293.  
  294.   {
  295.     char qra[16];
  296.     // skip comma after previous field
  297.     poi.read();
  298.     // read QRA string
  299.     qra[poi.readBytesUntil(',', qra, sizeof(qra))] = 0;
  300.     byte zoom =  poi.parseInt();
  301.     tft.print(F("\n"));
  302.     tft.print(qra);
  303.     tft.print(F(" zoom "));
  304.     tft.println(zoom, DEC);
  305.   }
  306.  
  307.   POI_TL_LAT = poi.parseInt();
  308.   POI_TL_LON = poi.parseInt();
  309.   DEGREES_PER_PIXEL_LAT = poi.parseInt();
  310.   DEGREES_PER_PIXEL_LON = poi.parseInt();
  311.   MAP_WIDTH_IN_TILES = poi.parseInt();
  312.   MAP_HEIGHT_IN_TILES = poi.parseInt();
  313.  
  314.   poi.close();
  315.   return true;
  316. }
  317.  
  318.  
  319. /***************************************************
  320.   Written by Limor Fried/Ladyada for Adafruit Industries.
  321.   MIT license, all text above must be included in any redistribution
  322.  ****************************************************/
  323.  
  324. // This function opens a Windows Bitmap (BMP) file and
  325. // displays it at the given coordinates.  It's sped up
  326. // by reading many pixels worth of data at a time
  327. // (rather than pixel by pixel).  Increasing the buffer
  328. // size takes more of the Arduino's precious RAM but
  329. // makes loading a little faster.  20 pixels seems a
  330. // good balance.
  331.  
  332. #define BUFFPIXEL 40
  333.  
  334. void bmpDraw(char *filename, uint8_t x, uint8_t y) {
  335.  
  336.   File     bmpFile;
  337.   int      bmpWidth, bmpHeight;   // W+H in pixels
  338.   uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  339.   uint32_t bmpImageoffset;        // Start of image data in file
  340.   uint32_t rowSize;               // Not always = bmpWidth; may have padding
  341.   uint8_t  sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
  342.   uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  343.   boolean  goodBmp = false;       // Set to true on valid header parse
  344.   boolean  flip    = true;        // BMP is stored bottom-to-top
  345.   int      w, h, row, col;
  346.   uint8_t  r, g, b;
  347.   uint32_t pos = 0, startTime = millis();
  348.  
  349.   if((x >= tft.width()) || (y >= tft.height())) return;
  350.  
  351.   // Open requested file on SD card
  352.   if ((bmpFile = SD.open(filename)) == NULL) {
  353.     // Serial.print(F(" File not found"));
  354.     return;
  355.   }
  356.  
  357.   // Parse BMP header
  358.   if(read16(bmpFile) == 0x4D42) { // BMP signature
  359.     int bmpSize = read32(bmpFile);
  360.     // Serial.print(" File size: "); Serial.println(bmpSize);
  361.     (void)read32(bmpFile); // Read & ignore creator bytes
  362.     bmpImageoffset = read32(bmpFile); // Start of image data
  363.     // Serial.print(" Image Offset: "); Serial.println(bmpImageoffset, DEC);
  364.     // Read DIB header
  365.     int bmpHeaderSize = read32(bmpFile);
  366.     // Serial.print(" Header size: "); Serial.println(bmpHeaderSize);
  367.     bmpWidth  = read32(bmpFile);
  368.     bmpHeight = read32(bmpFile);
  369.     if(read16(bmpFile) == 1) { // # planes -- must be '1'
  370.       bmpDepth = read16(bmpFile); // bits per pixel
  371.       // Serial.print(" Bit Depth: "); Serial.println(bmpDepth);
  372.       if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
  373.  
  374.         goodBmp = true; // Supported BMP format -- proceed!
  375.         // Serial.print(" Image size: ");
  376.         // Serial.print(bmpWidth);
  377.         // Serial.print('x');
  378.         // Serial.println(bmpHeight);
  379.  
  380.         // BMP rows are padded (if needed) to 4-byte boundary
  381.         rowSize = (bmpWidth * 3 + 3) & ~3;
  382.  
  383.         // If bmpHeight is negative, image is in top-down order.
  384.         // This is not canon but has been observed in the wild.
  385.         if(bmpHeight < 0) {
  386.           bmpHeight = -bmpHeight;
  387.           flip      = false;
  388.         }
  389.  
  390.         // Crop area to be loaded
  391.         w = bmpWidth;
  392.         h = bmpHeight;
  393.         if((x+w-1) >= tft.width())  w = tft.width()  - x;
  394.         if((y+h-1) >= tft.height()) h = tft.height() - y;
  395.  
  396.         // Set TFT address window to clipped image bounds
  397.         tft.setAddrWindow(x, y, x+w-1, y+h-1);
  398.  
  399.         for (row=0; row<h; row++) { // For each scanline...
  400.  
  401.           // Seek to start of scan line.  It might seem labor-
  402.           // intensive to be doing this on every line, but this
  403.           // method covers a lot of gritty details like cropping
  404.           // and scanline padding.  Also, the seek only takes
  405.           // place if the file position actually needs to change
  406.           // (avoids a lot of cluster math in SD library).
  407.           if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
  408.             pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
  409.           else     // Bitmap is stored top-to-bottom
  410.             pos = bmpImageoffset + row * rowSize;
  411.           if(bmpFile.position() != pos) { // Need seek?
  412.             bmpFile.seek(pos);
  413.             buffidx = sizeof(sdbuffer); // Force buffer reload
  414.           }
  415.  
  416.           for (col=0; col<w; col++) { // For each pixel...
  417.             // Time to read more pixel data?
  418.             if (buffidx >= sizeof(sdbuffer)) { // Indeed
  419.               bmpFile.read(sdbuffer, sizeof(sdbuffer));
  420.               buffidx = 0; // Set index to beginning
  421.             }
  422.  
  423.             // Convert pixel from BMP to TFT format, push to display
  424.             b = sdbuffer[buffidx++];
  425.             g = sdbuffer[buffidx++];
  426.             r = sdbuffer[buffidx++];
  427.             tft.pushColor(tft.Color565(r,g,b));
  428.           } // end pixel
  429.         } // end scanline
  430.         // Serial.print(" Loaded in ");
  431.         // Serial.print(millis() - startTime);
  432.         // Serial.println(" ms");
  433.       } // end goodBmp
  434.     }
  435.   }
  436.  
  437.   bmpFile.close();
  438.   if(!goodBmp) {
  439.     // Serial.println(" BMP format not recognized.");
  440.   }
  441. }
  442.  
  443. // These read 16- and 32-bit types from the SD card file.
  444. // BMP data is stored little-endian, Arduino is little-endian too.
  445. // May need to reverse subscript order if porting elsewhere.
  446.  
  447. uint16_t read16(File f) {
  448.   uint16_t result;
  449.   ((uint8_t *)&result)[0] = f.read(); // LSB
  450.   ((uint8_t *)&result)[1] = f.read(); // MSB
  451.   return result;
  452. }
  453.  
  454. uint32_t read32(File f) {
  455.   uint32_t result;
  456.   ((uint8_t *)&result)[0] = f.read(); // LSB
  457.   ((uint8_t *)&result)[1] = f.read();
  458.   ((uint8_t *)&result)[2] = f.read();
  459.   ((uint8_t *)&result)[3] = f.read(); // MSB
  460.   return result;
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement