Subversion Repositories dashGPS

Rev

Rev 25 | Rev 27 | 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"
26 mjames 7
#include "libLSM9DS1/LSM9DS1.h"
7 mjames 8
 
11 mjames 9
#include "libBMP280/bmp280.h"
10
 
13 mjames 11
#include "libSmallPrintf/small_printf.h"
15 mjames 12
 
13
#if defined USB_DEVICE
13 mjames 14
#include "usbd_cdc_if.h"
15 mjames 15
#endif
13 mjames 16
 
26 mjames 17
LSM9DS1Class IMU (&hi2c2);
18
 
6 mjames 19
namespace
20
{
21
  int const WIDTH = 128;
22
  int const HEIGHT = 64;
23
  int const DISPLAY_RAMWIDTH = 132;
24
 
25
}
26
 
10 mjames 27
float speedMPH = 0.0;
23 mjames 28
float speedAvg = 0.0;
15 mjames 29
 
23 mjames 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
15 mjames 33
 
24 mjames 34
uint32_t nextPosTime = 0;
35
 
23 mjames 36
// convert knots to MPH
37
double const KNOTS_TO_MPH = 1.128;
9 mjames 38
int heading = 0;
39
Location loc;
40
 
11 mjames 41
uint32_t lastTick = 0;
42
int32_t temp32 = 0;
43
uint32_t pres32 = 0;
44
int32_t rslt;
26 mjames 45
int32_t rslt2; // result from read of second sensor
46
// second sensor
47
int32_t temp32_out = 0;
11 mjames 48
 
6 mjames 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
 
9 mjames 61
displayDial_t dial (display1, 96, 32, 32, 180);
8 mjames 62
 
22 mjames 63
// debouncer for the button
64
char buttonState;
65
char buttonCount;
66
 
6 mjames 67
extern "C" void
68
cc_init ()
69
{
11 mjames 70
  display1.reset ();
71
 
9 mjames 72
  display1.init ();
6 mjames 73
  display1.clearDisplay ();
74
 
9 mjames 75
  dial.draw_scale (0, 360, 8, 1, 45);
6 mjames 76
 
77
  display1.display ();
9 mjames 78
 
10 mjames 79
  memset (loc.time, '-', 6);
26 mjames 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
 
6 mjames 86
}
87
 
9 mjames 88
char fontBuf[] = "01234567";
6 mjames 89
extern "C" void
26 mjames 90
cc_run (struct bmp280_dev *bmp, struct bmp280_dev *bmp2)
11 mjames 91
 
6 mjames 92
{
9 mjames 93
  display1.clearDisplay ();
94
  dial.draw_scale (0, 360, 8, 1, 45);
24 mjames 95
  while (1)
9 mjames 96
    {
24 mjames 97
      bool stat = updateLocation (&loc, &uc1);
98
      if (!stat)
99
        break;
100
      if (stat && loc.good)
101
        {
23 mjames 102
 
24 mjames 103
          heading = loc.heading;
104
          // add in time * speed to give "distance"
105
          if (loc.valid == 'A')
23 mjames 106
            {
24 mjames 107
              if (lastLocTime != 0)
23 mjames 108
                {
24 mjames 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
26 mjames 118
                    lastLocTime = loc.utc;
24 mjames 119
                }
120
              else
121
                {
23 mjames 122
                  lastLocTime = loc.utc;
123
                }
124
            }
125
        }
24 mjames 126
      else
127
        {
26 mjames 128
          memset (loc.time, '-', 6);
24 mjames 129
        }
11 mjames 130
    }
23 mjames 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++;
15 mjames 139
 
23 mjames 140
  if (buttonCount == buttonLimit)
141
    {
142
      buttonState = newPush;
143
      buttonCount = 0;
15 mjames 144
 
23 mjames 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
 
24 mjames 153
  // update the display once per second
26 mjames 154
  if (HAL_GetTick () > nextPosTime)
24 mjames 155
    {
156
      nextPosTime += 1000;
9 mjames 157
 
24 mjames 158
      // slow down the output of ata
159
      if (speedAvgTime > 0)
160
        speedAvg = (speedAvgSum / speedAvgTime) * KNOTS_TO_MPH;
25 mjames 161
      else
162
        speedAvg = 0.0;
23 mjames 163
 
24 mjames 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);
9 mjames 169
 
24 mjames 170
      display1.printString (small_font, ":", 1, WHITE);
171
      display1.printString (small_font, &loc.time[4], 2, WHITE);
9 mjames 172
 
24 mjames 173
      int dial_ang = heading + 180;
174
      dial.draw_needle (dial_ang);
9 mjames 175
 
24 mjames 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);
9 mjames 183
 
24 mjames 184
      if (loc.valid == 'A')
185
        speedMPH = loc.speed * KNOTS_TO_MPH;
186
      else
187
        speedMPH = 0.0;
9 mjames 188
 
24 mjames 189
      display1.gotoxy (0, 8);
26 mjames 190
      display1.fontDigits (large_font, 4,1, speedMPH * 10 );
191
      display1.printString (small_font, "c", 2, WHITE);
192
      display1.gotoxy (0,24);
24 mjames 193
      display1.fontDigits (large_font, 4, 1, speedAvg * 10);
26 mjames 194
      display1.printString (small_font, "av", 2, WHITE);
10 mjames 195
 
26 mjames 196
      float x, y, z;
197
      if (IMU.magneticFieldAvailable ())
198
        {
24 mjames 199
 
26 mjames 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
 
24 mjames 210
      if (HAL_GetTick () - lastTick > 100)
11 mjames 211
        {
24 mjames 212
          lastTick = HAL_GetTick ();
213
          /* Reading the raw data from sensor */
214
          rslt = bmp280_get_uncomp_data (&ucomp_data, bmp);
10 mjames 215
 
26 mjames 216
          /* reading the raw data from the second sensor */
217
          rslt2 = bmp280_get_uncomp_data (&ucomp_data2, bmp2);
218
 
219
          if (rslt2 == BMP280_OK)
24 mjames 220
            {
26 mjames 221
              rslt2 = bmp280_get_comp_temp_32bit (&temp32_out,
222
                                                  ucomp_data2.uncomp_temp, bmp2);
223
            }
10 mjames 224
 
24 mjames 225
 
26 mjames 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
 
15 mjames 237
#if defined USB_DEVICE
16 mjames 238
/*
239
          *  $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF> \\
15 mjames 240
 
16 mjames 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
*/
13 mjames 263
          // compile a logger message over USB
16 mjames 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);
13 mjames 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);
15 mjames 272
#endif
13 mjames 273
 
24 mjames 274
            }
11 mjames 275
        }
26 mjames 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
 
24 mjames 280
      display1.gotoxy (0, 56);
25 mjames 281
      display1.fontDigits (small_font, 3, 1, temp32 / 10, WHITE);
282
      display1.printString (small_font, "C", 2, WHITE);
26 mjames 283
 
25 mjames 284
      display1.fontDigits (small_font, 4, -1, pres32 / 100, WHITE);
26 mjames 285
      display1.printString (small_font, "mb ", 2, WHITE);
24 mjames 286
 
287
      display1.display ();
26 mjames 288
 
11 mjames 289
    }
6 mjames 290
}