Rev 24 | Rev 26 | 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 | |
| 24 | mjames | 31 | uint32_t nextPosTime = 0; |
| 32 | |||
| 23 | mjames | 33 | // convert knots to MPH |
| 34 | double const KNOTS_TO_MPH = 1.128; |
||
| 9 | mjames | 35 | int heading = 0; |
| 36 | Location loc; |
||
| 37 | |||
| 11 | mjames | 38 | uint32_t lastTick = 0; |
| 39 | int32_t temp32 = 0; |
||
| 40 | uint32_t pres32 = 0; |
||
| 41 | int32_t rslt; |
||
| 42 | |||
| 6 | mjames | 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 | |||
| 9 | mjames | 55 | displayDial_t dial (display1, 96, 32, 32, 180); |
| 8 | mjames | 56 | |
| 22 | mjames | 57 | // debouncer for the button |
| 58 | char buttonState; |
||
| 59 | char buttonCount; |
||
| 60 | |||
| 6 | mjames | 61 | extern "C" void |
| 62 | cc_init () |
||
| 63 | { |
||
| 11 | mjames | 64 | display1.reset (); |
| 65 | |||
| 9 | mjames | 66 | display1.init (); |
| 6 | mjames | 67 | display1.clearDisplay (); |
| 68 | |||
| 9 | mjames | 69 | dial.draw_scale (0, 360, 8, 1, 45); |
| 6 | mjames | 70 | |
| 71 | display1.display (); |
||
| 9 | mjames | 72 | |
| 10 | mjames | 73 | memset (loc.time, '-', 6); |
| 6 | mjames | 74 | } |
| 75 | |||
| 9 | mjames | 76 | char fontBuf[] = "01234567"; |
| 6 | mjames | 77 | extern "C" void |
| 11 | mjames | 78 | cc_run (struct bmp280_dev *bmp) |
| 79 | |||
| 6 | mjames | 80 | { |
| 9 | mjames | 81 | display1.clearDisplay (); |
| 82 | dial.draw_scale (0, 360, 8, 1, 45); |
||
| 24 | mjames | 83 | while (1) |
| 9 | mjames | 84 | { |
| 24 | mjames | 85 | bool stat = updateLocation (&loc, &uc1); |
| 86 | if (!stat) |
||
| 87 | break; |
||
| 88 | if (stat && loc.good) |
||
| 89 | { |
||
| 23 | mjames | 90 | |
| 24 | mjames | 91 | heading = loc.heading; |
| 92 | // add in time * speed to give "distance" |
||
| 93 | if (loc.valid == 'A') |
||
| 23 | mjames | 94 | { |
| 24 | mjames | 95 | if (lastLocTime != 0) |
| 23 | mjames | 96 | { |
| 24 | mjames | 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 | { |
||
| 23 | mjames | 110 | lastLocTime = loc.utc; |
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 24 | mjames | 114 | else |
| 115 | { |
||
| 116 | memset (loc.time, '-', 6); |
||
| 117 | } |
||
| 11 | mjames | 118 | } |
| 23 | mjames | 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++; |
||
| 15 | mjames | 127 | |
| 23 | mjames | 128 | if (buttonCount == buttonLimit) |
| 129 | { |
||
| 130 | buttonState = newPush; |
||
| 131 | buttonCount = 0; |
||
| 15 | mjames | 132 | |
| 23 | mjames | 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 | |||
| 24 | mjames | 141 | // update the display once per second |
| 142 | if (HAL_GetTick() > nextPosTime ) |
||
| 143 | { |
||
| 144 | nextPosTime += 1000; |
||
| 9 | mjames | 145 | |
| 24 | mjames | 146 | // slow down the output of ata |
| 147 | if (speedAvgTime > 0) |
||
| 148 | speedAvg = (speedAvgSum / speedAvgTime) * KNOTS_TO_MPH; |
||
| 25 | mjames | 149 | else |
| 150 | speedAvg = 0.0; |
||
| 23 | mjames | 151 | |
| 24 | mjames | 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); |
||
| 9 | mjames | 157 | |
| 24 | mjames | 158 | display1.printString (small_font, ":", 1, WHITE); |
| 159 | display1.printString (small_font, &loc.time[4], 2, WHITE); |
||
| 9 | mjames | 160 | |
| 24 | mjames | 161 | int dial_ang = heading + 180; |
| 162 | dial.draw_needle (dial_ang); |
||
| 9 | mjames | 163 | |
| 24 | mjames | 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); |
||
| 9 | mjames | 171 | |
| 24 | mjames | 172 | if (loc.valid == 'A') |
| 173 | speedMPH = loc.speed * KNOTS_TO_MPH; |
||
| 174 | else |
||
| 175 | speedMPH = 0.0; |
||
| 9 | mjames | 176 | |
| 24 | mjames | 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); |
||
| 9 | mjames | 181 | |
| 24 | mjames | 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); |
||
| 10 | mjames | 186 | |
| 24 | mjames | 187 | struct bmp280_uncomp_data ucomp_data; |
| 188 | |||
| 189 | if (HAL_GetTick () - lastTick > 100) |
||
| 11 | mjames | 190 | { |
| 24 | mjames | 191 | lastTick = HAL_GetTick (); |
| 192 | /* Reading the raw data from sensor */ |
||
| 193 | rslt = bmp280_get_uncomp_data (&ucomp_data, bmp); |
||
| 10 | mjames | 194 | |
| 24 | mjames | 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); |
||
| 10 | mjames | 200 | |
| 24 | mjames | 201 | rslt = bmp280_get_comp_pres_32bit (&pres32, |
| 202 | ucomp_data.uncomp_press, bmp); |
||
| 203 | |||
| 15 | mjames | 204 | #if defined USB_DEVICE |
| 16 | mjames | 205 | /* |
| 206 | * $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF> \\ |
||
| 15 | mjames | 207 | |
| 16 | mjames | 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 | */ |
||
| 13 | mjames | 230 | // compile a logger message over USB |
| 16 | mjames | 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); |
||
| 13 | mjames | 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); |
||
| 15 | mjames | 239 | #endif |
| 13 | mjames | 240 | |
| 24 | mjames | 241 | } |
| 11 | mjames | 242 | } |
| 24 | mjames | 243 | display1.gotoxy (0, 56); |
| 25 | mjames | 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); |
||
| 24 | mjames | 248 | |
| 249 | display1.display (); |
||
| 11 | mjames | 250 | } |
| 6 | mjames | 251 | } |