/* ------------ Standard  Include Files --------------- */
#include <stdint.h>
/* ------------ Special   Include Files --------------- */
#include "tm_stm32f4_ili9341.h"
#include "tm_stm32f4_adc.h"
#include "tm_stm32f4_exti.h"
#include "tm_stm32f4_disco.h"
#include "tm_stm32f4_delay.h"
#include "tm_stm32f4_low_power.h"
#include "tm_stm32f4_nrf24l01_cj.h"
#include "tm_stm32f4_hd44780.h"

/* ------------ Projekt   Include Files --------------- */
#include "config.h"
#include "rtc_user.h"
#include "init_system.h"
#include "graphik.h"
#include "eeprom.h"
#include "tools.h"
#include "processing.h"
#include "max7219.h"

volatile _Bool      f_SwitchToTextMode = false;   // Umschaltung auf Textbildschirm

Work_t   Work BKRAM;             // Instanz der Haupt Datenbasis im Backup RAM

//////////////////////////////////////
/*         Hauptprogramm            */
//////////////////////////////////////

int main(void)
{
    char buf[40];


    //////////////////////////////////////
    /* Initialisierung des Systems      */
    //////////////////////////////////////

    SystemInit();               // Init der Clocks und wichtiger Register
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    SysTick_Config(SystemCoreClock/1000);

    flags.backlight = true;

    TM_DELAY_Init();
    Init_BackupRam();                           // Backup Ram aktivieren
    Init_Ports();                               // Ports einstellen

    TM_DISCO_ButtonInit();
    TM_ADC_Init(ADC1,TM_ADC_Channel_1);

    Init_RTC();                                 // RTC aktivieren
    cfg_Init_Runtime();
    Init_BMP180();                              // Drucksensor
    CalcSonnenAufgang(&Time,&SonnenZeiten);     // Sonnenaufgang und Untergang berechnen
//  Init_DS18B20();
    Init_LCD();                                 // LCD einstellen

    // Daten ungültig machen, wenn Userbutton beim Reset gedrückt wurde
    if (TM_DISCO_ButtonPressed()) {
        Work.id = 0;
        Init_WorkStructure(&Work, sizeof(Work));
    }

    // LCD Display
    TM_HD44780_Init(16,2);                        // LCD Display Blau
    TM_HD44780_Clear();
    TM_HD44780_Puts(0, 0,"STM32 Wetter");
    TM_HD44780_Puts(0, 1,"C.Julius 2015");

    // Werte für Druck einstellen und alles zeichnen
    Init_WorkStructure(&Work,sizeof(Work));
    proc_UpdateHistory(&Work);
    gfx_DrawDateTime(&Time);
    gfx_DrawPressure(&Work);
    gfx_DrawTemperature(&Work);
    gfx_DrawHistory(&Work);
    proc_HandleLEDs(&Work);

    // Wakeup Interrupt alle 1s setzen
    TM_RTC_Interrupts(TM_RTC_Int_1s);

    /* ---------------------------------------------- */
    //             Hauptschleife                      //
    /* ---------------------------------------------- */

    while (1) {

        if (flags.rtc_1s == true) {
            // ----- 1 Sekunde ----------------------
            gfx_DrawDateTime(&Time);

            // Umschalten zwischen Text/Grafik Modus
            if (TM_DISCO_ButtonPressed())
                f_SwitchToTextMode = !f_SwitchToTextMode;

           // ----- 1 Minute ----------------------
           if (flags.rtc_1min) {
               proc_UpdateHistory(&Work);
               gfx_DrawTemperature(&Work);
               gfx_DrawPressure(&Work);
               gfx_DrawHistory(&Work);
               tls_RFSendWork(&Work);
               proc_HandleLEDs(&Work);           // LEDs schalten
               flags.rtc_1min = false;
           }

           // ----- 1 Stunde ----------------------
           // Stuendlich History updaten
           if  (flags.rtc_1hr) {
                proc_FreezeHistory(&Work);       // Historie hochzählen
                if (e2p_Backup_Work((uint8_t*)&Work, sizeof(Work)) == ERROR)                     // Backup die Daten ins E2PROM hinein
                {
                    TM_ILI9341_Fill(ILI9341_COLOR_BLACK);
                    TM_ILI9341_Puts(TransX(0),TransY(239),"EEPROM Backup Error!",&TM_Font_11x18,ILI9341_COLOR_RED, ILI9341_COLOR_BLACK);
                    while(TM_DISCO_ButtonPressed() == 0);
                }
                e2p_DeInit_I2C();

                flags.rtc_1hr = false;
           }

           // Tägliche Aktionen
           if (flags.rtc_1day) {
                CalcSonnenAufgang(&Time,&SonnenZeiten);
               flags.rtc_1day = false;
           }

           flags.rtc_1s = false;

        }
        // Core anhalten bis 1s Wakeup von RTC (bei Debug Mode crashed der Debugger sonst)
        #ifdef RELEASE_MODE
            TM_LOWPOWER_StopUntilEvent();
        #endif
    }


}
