Subversion Repositories dashGPS

Rev

Rev 22 | Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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