Subversion Repositories dashGPS

Rev

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