// NokiaMain.cc

#include <stdio.h>
#include <unistd.h>

#include "Platform.h"

#include "allegro.h"
#include "NokiaLCD.h"
#include "Temperature.h"
#include "Sample.h"
#include "DS1822.h"
#include "DS1822OD.h"

#include "SimuLcd.h"

BITMAP *screen_buffer = 0;
BITMAP *stretch_buffer = 0;

void test_case_1 (void );

int main () {
  // Init
  allegro_init();
  install_keyboard();
  
  if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 200, 0, 0) != 0) {
    set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
    allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
    return 1;
  }
  set_color_depth (24);
  set_color_conversion(COLORCONV_TOTAL);
  //text_mode (-1);

  screen_buffer = create_bitmap(LCD_X, LCD_Y);
  stretch_buffer = create_bitmap(STRETCH_X, STRETCH_Y);

  // TEST here
  test_case_1 ();


  // terminate
  if (screen_buffer) {
    destroy_bitmap (screen_buffer);
    screen_buffer=0;
  }

  if (stretch_buffer) {
    destroy_bitmap (stretch_buffer);
    stretch_buffer=0;
  }
  
  return 0;
}
END_OF_MAIN();

void test_case_1 () {
  int v;

  // DS1822 values
  // 0550 : +85.0
  // 0191 : +25.0625
  // 00A2 : +10.125
  // 0008 : +0.5
  // 0000 : 0
  // FFF8 : -0.5
  // FF5E : -10.125
  // FE6F : -25.0625
  // FC90 : -55.0

  UInt16 sec; 
  UInt8 lsb, msb; // DS registers
  UInt8 sampleA, sampleB; // y coordinate for the sample
  UInt8 bcdvalue [6]; // to store ds temperature in BCD
  UInt8 sign, tvalue, treste;

  // ADCON1=0x07; // DS1822 <-> RA Digital in/out
  DS1822_Init ((UInt8)DS_MODE_9BITS);
  DS1822OD_Init ((UInt8) DS_MODE_9BITS);
  
  // Starts first conversions
  DS1822_StartConversion ();
  DS1822OD_StartConversion ();

  // Nokia LCD Init
  LcdInit ();
  LcdGotoXY (0, 0);
  LcdChr (FONT_1X, '#'); // 30
  LcdChr (FONT_1X, '&');
  LcdGotoXY (0, 1);
  LcdChr (FONT_1X, '$'); // 20
  LcdChr (FONT_1X, '&');
  LcdGotoXY (0, 2);
  LcdChr (FONT_1X, '%'); // 10
  LcdChr (FONT_1X, '&');
  LcdGotoXY (1, 3);
  LcdChr (FONT_1X, '&'); // 0
  LcdGotoXY (0, 4);
  LcdChr (FONT_1X, '\''); // -10
  LcdChr (FONT_1X, '&');
  LcdGotoXY (0, 5);
  LcdChr (FONT_1X, '!'); // Home
  LcdGotoXY (8, 5);
  LcdChr (FONT_1X, '\"'); // Tree
  LcdUpdate ();

  // Samples init
  InitSamples ();
  sec = 0;

  for (v=0; v < 130; v++) { 
    // Read & display inside temperature
    DS1822_ReadTemperature (&lsb, &msb);
    DSToTemperature(lsb, msb, &sign, &tvalue, &treste);
    TemperatureToBCD (sign, tvalue, treste, bcdvalue);
    LcdGotoXY (1, 5); // Inside temperature

    LcdStr ( FONT_1X, bcdvalue );
    sampleA = TemperatureToSample (sign, tvalue, treste);
    
    // Read & display outside temperature
    DS1822OD_ReadTemperature (&lsb, &msb);
    DSToTemperature(lsb, msb, &sign, &tvalue, &treste);
    sampleB = TemperatureToSample (sign, tvalue, treste);
    TemperatureToBCD (sign, tvalue, treste, bcdvalue);
    
    LcdGotoXY (9, 5); // Outside temperature
    LcdStr ( FONT_1X, bcdvalue );

    // Display new samples every 300 sec (5 minutes)
    if (sec == 0) {
      AddSample (sampleA, sampleB);
      DrawSamples ();
      // sec = 300; // 1 sample every 5 minutes => 6 hours
    }
    // sec--;
    LcdUpdate (); // Refresh LCD

    // Start a new conversion
    DS1822_StartConversion ();
    DS1822OD_StartConversion ();

    usleep (100000);
  }
  
  printf ("Press space to finish\n");
  while (!key[KEY_SPACE]);
  while (key[KEY_SPACE]);
}

// End of file
