Subversion Repositories dashGPS

Rev

Rev 23 | Rev 25 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "main.h"
  2. #include "libNMEA/nmea.h"
  3. #include <cstring>
  4. #include "libOLED/stm32_halDisplay.H"
  5. #include "libOLED/fontclass.H"
  6. #include "libOLED/displayDial.H"
  7.  
  8. #include "libBMP280/bmp280.h"
  9.  
  10. #include "libSmallPrintf/small_printf.h"
  11.  
  12. #if defined USB_DEVICE
  13. #include "usbd_cdc_if.h"
  14. #endif
  15.  
  16. namespace
  17. {
  18.   int const WIDTH = 128;
  19.   int const HEIGHT = 64;
  20.   int const DISPLAY_RAMWIDTH = 132;
  21.  
  22. }
  23.  
  24. float speedMPH = 0.0;
  25. float speedAvg = 0.0;
  26.  
  27. double speedAvgTime = 0;    // sum of all deltaTime;
  28. double speedAvgSum = 0.0;  // sum of deltaTime * speed for each second
  29. double lastLocTime = 0.0; // last time there was a location
  30.  
  31. uint32_t nextPosTime = 0;
  32.  
  33. // convert knots to MPH
  34. double const KNOTS_TO_MPH = 1.128;
  35. int heading = 0;
  36. Location loc;
  37.  
  38. uint32_t lastTick = 0;
  39. int32_t temp32 = 0;
  40. uint32_t pres32 = 0;
  41. int32_t rslt;
  42.  
  43. uint8_t displayBuffer[dataSize (WIDTH, HEIGHT)];
  44.  
  45. stm32_halDisplay_t display1 (WIDTH, HEIGHT, DISPLAY_RAMWIDTH, displayBuffer,
  46.                              &hspi1,
  47.  
  48.                              SPI_CD_GPIO_Port,
  49.                              SPI_CD_Pin,
  50.                              SPI_RESET_GPIO_Port,
  51.                              SPI_RESET_Pin,
  52.                              SPI_NSS1_GPIO_Port,
  53.                              SPI_NSS1_Pin);
  54.  
  55. displayDial_t dial (display1, 96, 32, 32, 180);
  56.  
  57. // debouncer for the button
  58. char buttonState;
  59. char buttonCount;
  60.  
  61. extern "C" void
  62. cc_init ()
  63. {
  64.   display1.reset ();
  65.  
  66.   display1.init ();
  67.   display1.clearDisplay ();
  68.  
  69.   dial.draw_scale (0, 360, 8, 1, 45);
  70.  
  71.   display1.display ();
  72.  
  73.   memset (loc.time, '-', 6);
  74. }
  75.  
  76. char fontBuf[] = "01234567";
  77. extern "C" void
  78. cc_run (struct bmp280_dev *bmp)
  79.  
  80. {
  81.   display1.clearDisplay ();
  82.   dial.draw_scale (0, 360, 8, 1, 45);
  83.   while (1)
  84.     {
  85.       bool stat = updateLocation (&loc, &uc1);
  86.       if (!stat)
  87.         break;
  88.       if (stat && loc.good)
  89.         {
  90.  
  91.           heading = loc.heading;
  92.           // add in time * speed to give "distance"
  93.           if (loc.valid == 'A')
  94.             {
  95.               if (lastLocTime != 0)
  96.                 {
  97.                   double delta = difftime (loc.utc, lastLocTime);
  98.                   // believe the speed .
  99.                   if (delta > 0 && delta < 2)
  100.                     {
  101.                       speedAvgSum += loc.speed * delta;
  102.                       speedAvgTime += delta;
  103.                       lastLocTime = loc.utc;
  104.                     }
  105.                   else
  106.                      lastLocTime = loc.utc;
  107.                 }
  108.               else
  109.                 {
  110.                   lastLocTime = loc.utc;
  111.                 }
  112.             }
  113.         }
  114.       else
  115.         {
  116.         memset (loc.time, '-', 6);
  117.         }
  118.     }
  119.   // process button press
  120.   uint8_t const buttonLimit = 3;
  121.   uint8_t newPush = HAL_GPIO_ReadPin ( encoder_push_GPIO_Port,
  122.   encoder_push_Pin);
  123.   if (newPush == buttonState)
  124.     buttonCount = 0;
  125.   else if (buttonCount < buttonLimit)
  126.     buttonCount++;
  127.  
  128.   if (buttonCount == buttonLimit)
  129.     {
  130.       buttonState = newPush;
  131.       buttonCount = 0;
  132.  
  133.       // if the button is held down , we set the average speed
  134.       if (buttonState == GPIO_PIN_RESET)
  135.         {
  136.           speedAvgSum = 0.0;
  137.           speedAvgTime = 0.0;
  138.         }
  139.     }
  140.  
  141.   // update the display once per second
  142.   if (HAL_GetTick() > nextPosTime )
  143.     {
  144.       nextPosTime += 1000;
  145.  
  146.       // slow down the output of ata
  147.       if (speedAvgTime > 0)
  148.         speedAvg = (speedAvgSum / speedAvgTime) * KNOTS_TO_MPH;
  149.  
  150.       // print out the GMT time at the top of the screen
  151.       display1.gotoxy (0, 0);
  152.       display1.printString (small_font, &loc.time[0], 2, WHITE);
  153.       display1.printString (small_font, ":", 1, WHITE);
  154.       display1.printString (small_font, &loc.time[2], 2, WHITE);
  155.  
  156.       display1.printString (small_font, ":", 1, WHITE);
  157.       display1.printString (small_font, &loc.time[4], 2, WHITE);
  158.  
  159.       int dial_ang = heading + 180;
  160.       dial.draw_needle (dial_ang);
  161.  
  162.       display1.gotoxy (70, 25);
  163.       if (loc.valid == 'A')
  164.         {
  165.           display1.fontDigits (large_font, 3, -1, heading);
  166.         }
  167.       else
  168.         display1.printString (large_font, "GPS?", 4, WHITE);
  169.  
  170.       if (loc.valid == 'A')
  171.         speedMPH = loc.speed * KNOTS_TO_MPH;
  172.       else
  173.         speedMPH = 0.0;
  174.  
  175.       display1.gotoxy (0, 8);
  176.       display1.printString (small_font, "Speed", 5, WHITE);
  177.       display1.gotoxy (0, 16);
  178.       display1.fontDigits (large_font, 4, 1, speedMPH * 10);
  179.  
  180.       display1.gotoxy (0, 32);
  181.       display1.printString (small_font, "Average", 7, WHITE);
  182.       display1.gotoxy (0, 40);
  183.       display1.fontDigits (large_font, 4, 1, speedAvg * 10);
  184.  
  185.       struct bmp280_uncomp_data ucomp_data;
  186.  
  187.       if (HAL_GetTick () - lastTick > 100)
  188.         {
  189.           lastTick = HAL_GetTick ();
  190.           /* Reading the raw data from sensor */
  191.           rslt = bmp280_get_uncomp_data (&ucomp_data, bmp);
  192.  
  193.           if (rslt == BMP280_OK)
  194.             {
  195.               /* Getting the 32 bit compensated temperature */
  196.               rslt = bmp280_get_comp_temp_32bit (&temp32,
  197.                                                  ucomp_data.uncomp_temp, bmp);
  198.  
  199.               rslt = bmp280_get_comp_pres_32bit (&pres32,
  200.                                                  ucomp_data.uncomp_press, bmp);
  201.  
  202. #if defined USB_DEVICE
  203. /*
  204.           *  $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF> \\
  205.  
  206.               Field Number:
  207.               1) Transducer Type
  208.               2) Measurement Data
  209.               3) Units of measurement
  210.               4) Name of transducer
  211.               x) More of the same
  212.               n) Checksum
  213.  
  214.               Example:
  215.               $IIXDR,C,19.52,C,TempAir*19
  216.               $IIXDR,P,1.02481,B,Barometer*29
  217.  
  218.               Currently, OpenCPN recognizes the following transducers:
  219.  
  220.           Measured Value | Transducer Type | Measured Data   | Unit of measure | Transducer Name
  221.           ------------------------------------------------------------------------------------------------------
  222.           barometric     | "P" pressure    | 0.8..1.1 or 800..1100           | "B" bar         | "Barometer"
  223.           air temperature| "C" temperature |   2 decimals                    | "C" celsius     | "TempAir" or "ENV_OUTAIR_T"
  224.           pitch          | "A" angle       |-180..0 nose down 0..180 nose up | "D" degrees     | "PTCH" or "PITCH"
  225.           rolling        | "A" angle       |-180..0 L         0..180 R       | "D" degrees     | "ROLL"
  226.           water temp     | "C" temperature |   2 decimals                    | "C" celsius     | "ENV_WATER_T"
  227. */
  228.           // compile a logger message over USB
  229.            char buffer[200];
  230.            int cnt = small_sprintf(buffer,"$MJXDR,C,%ld.%02ld,C,AirTemp,P,%01ld.%05ld,B,AirPres",temp32/100,temp32%100,pres32/100000,pres32%100000);
  231.            uint8_t sum=0;
  232.            for(int i=1; i<cnt; i++)
  233.              sum += buffer[i];
  234.            cnt+= small_sprintf(buffer+cnt,"*%02X\n",sum);
  235.  
  236.            CDC_Transmit_FS(reinterpret_cast<uint8_t*>(&buffer[0]),cnt);
  237. #endif
  238.  
  239.             }
  240.         }
  241.       display1.gotoxy (0, 56);
  242.       display1.printString (small_font, "T", 2, WHITE);
  243.       display1.fontDigits (small_font, 4, 1, temp32 / 10, WHITE);
  244.       display1.printString (small_font, " P", 2, WHITE);
  245.       display1.fontDigits (small_font, 5, 0, pres32 / 100, WHITE);
  246.       display1.printString (small_font, " ", 1, WHITE);
  247.  
  248.       display1.display ();
  249.     }
  250. }
  251.