
#include "main.h"
#include "nmea.h"
#include <cstring>
#include "libOLED/stm32_halDisplay.H"
#include "libOLED/fontclass.H"
#include "libOLED/displayDial.H"

namespace
{
  int const WIDTH = 128;
  int const HEIGHT = 64;
  int const DISPLAY_RAMWIDTH = 132;

}

float speedMPH = 0.0;
float speedAVG = 0.0;
int heading = 0;
Location loc;

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.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 ()
{

  display1.clearDisplay ();
  dial.draw_scale (0, 360, 8, 1, 45);

  bool stat = updateLocation (&loc);
  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);




display1.display ();

}
