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