#include "main.h"
#include "libNMEA/nmea.h"
#include <cstring>
#include "libOLED/stm32_halDisplay.H"
#include "libOLED/fontclass.H"
#include "libOLED/displayDial.H"
#include "libBME280/bme280.h"
#include "libSmallPrintf/small_printf.h"
#if defined USB_DEVICE
#include "usbd_cdc_if.h"
#endif
namespace
{
int const WIDTH = 128;
int const HEIGHT = 64;
int const DISPLAY_RAMWIDTH = 132;
}
float speedMPH = 0.0;
float speedAVG = 0.0;
int speedTimer = 0;
float speedDist = 0.0;
int heading = 0;
Location loc;
uint32_t lastTick = 0;
int32_t rslt;
uint8_t displayBuffer[dataSize (WIDTH, HEIGHT)];
stm32_halDisplay_t display1 (WIDTH, HEIGHT, DISPLAY_RAMWIDTH, displayBuffer,
&hspi1,
SPI_CD_GPIO_Port,
SPI_CD_Pin,
SPI_RESET_GPIO_Port,
SPI_RESET_Pin,
SPI_NSS1_GPIO_Port,
SPI_NSS1_Pin);
displayDial_t dial (display1, 96, 32, 32, 180);
extern "C" void
cc_init ()
{
display1.reset ();
display1.init ();
display1.clearDisplay ();
dial.draw_scale (0, 360, 8, 1, 45);
display1.display ();
memset (loc.time, '-', 6);
}
char fontBuf[] = "01234567";
extern "C" void
cc_run (struct bme280_dev *dev)
{
display1.clearDisplay ();
dial.draw_scale (0, 360, 8, 1, 45);
bool stat = updateLocation (&loc, &uc1);
if (loc.good)
{
heading = loc.heading;
loc.good = false;
}
if (loc.valid == 'V')
memset (loc.time, '-', 6);
// print out the GMT time at the top of the screen
display1.gotoxy (0, 0);
display1.printString (small_font, &loc.time[0], 2, WHITE);
display1.printString (small_font, ":", 1, WHITE);
display1.printString (small_font, &loc.time[2], 2, WHITE);
display1.printString (small_font, ":", 1, WHITE);
display1.printString (small_font, &loc.time[4], 2, WHITE);
int dial_ang = heading + 180;
dial.draw_needle (dial_ang);
display1.gotoxy (70, 25);
if (loc.valid == 'A')
{
display1.fontDigits (large_font, 3, -1, heading);
}
else
display1.printString (large_font, "GPS?", 4, WHITE);
if (loc.valid == 'A')
speedMPH = loc.speed / 1.815;
display1.gotoxy (0, 8);
display1.printString (small_font, "Speed", 5, WHITE);
display1.gotoxy (0, 16);
display1.fontDigits (large_font, 4, 1, speedMPH * 10);
display1.gotoxy (0, 32);
display1.printString (small_font, "Average", 7, WHITE);
display1.gotoxy (0, 40);
display1.fontDigits (large_font, 4, 1, speedAVG * 10);
if (HAL_GetTick () - lastTick > 100)
{
lastTick = HAL_GetTick ();
// storage for readings
struct bme280_data comp_data;
rslt = bme280_get_sensor_data (BME280_ALL, &comp_data, dev);
if (rslt == BME280_OK)
{
float temp, press, hum;
#ifdef BME280_FLOAT_ENABLE
temp = comp_data.temperature;
press = 0.01 * comp_data.pressure;
hum = comp_data.humidity;
#else
#ifdef BME280_64BIT_ENABLE
temp = 0.01f * comp_data.temperature;
press = 0.0001f * comp_data.pressure;
hum = 1.0f / 1024.0f * comp_data.humidity;
#else
temp = 0.01f * comp_data.temperature;
press = 0.01f * comp_data.pressure;
hum = 1.0f / 1024.0f * comp_data.humidity;
#endif
#endif
uint32_t temp32 = 0;
uint32_t press32 = 0;
uint32_t hum32 = 0;
#if defined USB_DEVICE
/*
* $--XDR,a,x.x,a,c--c, ..... *hh<CR><LF> \\
Field Number:
1) Transducer Type
2) Measurement Data
3) Units of measurement
4) Name of transducer
x) More of the same
n) Checksum
Example:
$IIXDR,C,19.52,C,TempAir*19
$IIXDR,P,1.02481,B,Barometer*29
Currently, OpenCPN recognizes the following transducers:
Measured Value | Transducer Type | Measured Data | Unit of measure | Transducer Name
------------------------------------------------------------------------------------------------------
barometric | "P" pressure | 0.8..1.1 or 800..1100 | "B" bar | "Barometer"
air temperature| "C" temperature | 2 decimals | "C" celsius | "TempAir" or "ENV_OUTAIR_T"
pitch | "A" angle |-180..0 nose down 0..180 nose up | "D" degrees | "PTCH" or "PITCH"
rolling | "A" angle |-180..0 L 0..180 R | "D" degrees | "ROLL"
water temp | "C" temperature | 2 decimals | "C" celsius | "ENV_WATER_T"
*/
// compile a logger message over USB
char buffer[200];
int cnt = small_sprintf(buffer,"$MJXDR,C,%ld.%02ld,C,AirTemp,P,%01ld.%05ld,B,AirPres",temp32/100,temp32%100,press32/100000,press32%100000);
uint8_t sum=0;
for(int i=1; i<cnt; i++)
sum += buffer[i];
cnt+= small_sprintf(buffer+cnt,"*%02X\n",sum);
CDC_Transmit_FS(reinterpret_cast<uint8_t*>(&buffer[0]),cnt);
#endif
display1.gotoxy (0, 56);
display1.printString (small_font, "T", 2, WHITE);
display1.fontDigits (small_font, 4, 1, temp32 / 10, WHITE);
display1.printString (small_font, " P", 2, WHITE);
display1.fontDigits (small_font, 5, 0, press32 / 100, WHITE);
display1.printString (small_font, " ", 1, WHITE);
display1.display ();
}
}
}