Subversion Repositories dashGPS

Rev

Rev 24 | Rev 26 | 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.       else
  150.         speedAvg = 0.0;
  151.  
  152.       // print out the GMT time at the top of the screen
  153.       display1.gotoxy (0, 0);
  154.       display1.printString (small_font, &loc.time[0], 2, WHITE);
  155.       display1.printString (small_font, ":", 1, WHITE);
  156.       display1.printString (small_font, &loc.time[2], 2, WHITE);
  157.  
  158.       display1.printString (small_font, ":", 1, WHITE);
  159.       display1.printString (small_font, &loc.time[4], 2, WHITE);
  160.  
  161.       int dial_ang = heading + 180;
  162.       dial.draw_needle (dial_ang);
  163.  
  164.       display1.gotoxy (70, 25);
  165.       if (loc.valid == 'A')
  166.         {
  167.           display1.fontDigits (large_font, 3, -1, heading);
  168.         }
  169.       else
  170.         display1.printString (large_font, "GPS?", 4, WHITE);
  171.  
  172.       if (loc.valid == 'A')
  173.         speedMPH = loc.speed * KNOTS_TO_MPH;
  174.       else
  175.         speedMPH = 0.0;
  176.  
  177.       display1.gotoxy (0, 8);
  178.       display1.printString (small_font, "Speed", 5, WHITE);
  179.       display1.gotoxy (0, 16);
  180.       display1.fontDigits (large_font, 4, 1, speedMPH * 10);
  181.  
  182.       display1.gotoxy (0, 32);
  183.       display1.printString (small_font, "Average", 7, WHITE);
  184.       display1.gotoxy (0, 40);
  185.       display1.fontDigits (large_font, 4, 1, speedAvg * 10);
  186.  
  187.       struct bmp280_uncomp_data ucomp_data;
  188.  
  189.       if (HAL_GetTick () - lastTick > 100)
  190.         {
  191.           lastTick = HAL_GetTick ();
  192.           /* Reading the raw data from sensor */
  193.           rslt = bmp280_get_uncomp_data (&ucomp_data, bmp);
  194.  
  195.           if (rslt == BMP280_OK)
  196.             {
  197.               /* Getting the 32 bit compensated temperature */
  198.               rslt = bmp280_get_comp_temp_32bit (&temp32,
  199.                                                  ucomp_data.uncomp_temp, bmp);
  200.  
  201.               rslt = bmp280_get_comp_pres_32bit (&pres32,
  202.                                                  ucomp_data.uncomp_press, bmp);
  203.  
  204. #if defined USB_DEVICE
  205. /*
  206.           *  $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF> \\
  207.  
  208.               Field Number:
  209.               1) Transducer Type
  210.               2) Measurement Data
  211.               3) Units of measurement
  212.               4) Name of transducer
  213.               x) More of the same
  214.               n) Checksum
  215.  
  216.               Example:
  217.               $IIXDR,C,19.52,C,TempAir*19
  218.               $IIXDR,P,1.02481,B,Barometer*29
  219.  
  220.               Currently, OpenCPN recognizes the following transducers:
  221.  
  222.           Measured Value | Transducer Type | Measured Data   | Unit of measure | Transducer Name
  223.           ------------------------------------------------------------------------------------------------------
  224.           barometric     | "P" pressure    | 0.8..1.1 or 800..1100           | "B" bar         | "Barometer"
  225.           air temperature| "C" temperature |   2 decimals                    | "C" celsius     | "TempAir" or "ENV_OUTAIR_T"
  226.           pitch          | "A" angle       |-180..0 nose down 0..180 nose up | "D" degrees     | "PTCH" or "PITCH"
  227.           rolling        | "A" angle       |-180..0 L         0..180 R       | "D" degrees     | "ROLL"
  228.           water temp     | "C" temperature |   2 decimals                    | "C" celsius     | "ENV_WATER_T"
  229. */
  230.           // compile a logger message over USB
  231.            char buffer[200];
  232.            int cnt = small_sprintf(buffer,"$MJXDR,C,%ld.%02ld,C,AirTemp,P,%01ld.%05ld,B,AirPres",temp32/100,temp32%100,pres32/100000,pres32%100000);
  233.            uint8_t sum=0;
  234.            for(int i=1; i<cnt; i++)
  235.              sum += buffer[i];
  236.            cnt+= small_sprintf(buffer+cnt,"*%02X\n",sum);
  237.  
  238.            CDC_Transmit_FS(reinterpret_cast<uint8_t*>(&buffer[0]),cnt);
  239. #endif
  240.  
  241.             }
  242.         }
  243.       display1.gotoxy (0, 56);
  244.       display1.fontDigits (small_font, 3, 1, temp32 / 10, WHITE);
  245.       display1.printString (small_font, "C", 2, WHITE);
  246.       display1.fontDigits (small_font, 4, -1, pres32 / 100, WHITE);
  247.       display1.printString (small_font, "mb", 2, WHITE);
  248.  
  249.       display1.display ();
  250.     }
  251. }
  252.