Subversion Repositories Projects

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

/*****************************************************************************
 *   Copyright (C) 2008 Thomas Kaiser, thomas@ft-fanpage.de                  *
 *   Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net                  *
 *   Copyright (C) 2013 Christian "Cebra" Brandtner, brandtner@brandtner.net *
 *   Copyright (C) 2011 Harald Bongartz                                      *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.,                                         *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
 *                                                                           *
 *                                                                           *
 *   Credits to:                                                             *
 *   Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN  *
 *                          http://www.mikrokopter.de                        *
 *   Gregor "killagreg" Stobrawa for his version of the MK code              *
 *   Thomas Kaiser "thkais" for the original project. See                    *
 *                          http://www.ft-fanpage.de/mikrokopter/            *
 *                          http://forum.mikrokopter.de/topic-4061-1.html    *
 *   Claas Anders "CaScAdE" Rathje for providing the font and his C-OSD code *
 *                          http://www.mylifesucks.de/oss/c-osd/             *
 *   Harald Bongartz "HaraldB" for providing his Ideas and Code for usibility*
 *****************************************************************************/



#include "../cpu.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <stdbool.h>
#include <avr/wdt.h>
#include "../lcd/lcd.h"
#include "../main.h"
#include "../timer/timer.h"
#include "eeprom.h"
#include "../wi232/Wi232.h"
#include "../setup/setup.h"
#include "../bluetooth/bluetooth.h"
#include "../mk-data-structs.h"
#include "../connect.h"
#include "../tracking/ng_servo.h"
#include "../tracking/tracking.h"
//------------------------------------------------------------------------------------------

ST EEMEM EEStruct;

ST Config;

//------------------------------------------------------------------------------------------
void WriteWiInitFlag(void)
{
       Config.WiIsSet = true;
//        WriteParameter();
}


//--------------------------------------------------------------
//
void WriteBTInitFlag(void)
{
     Config.BTIsSet = true;
//      WriteParameter();
}
//--------------------------------------------------------------
//
void WriteBTSlaveFlag(void)
{
     Config.BTIsSlave = true;
//      WriteParameter();
}
//--------------------------------------------------------------
//
void WriteBTMasterFlag(void)
{
     Config.BTIsSlave = false;
//      WriteParameter();

}



//--------------------------------------------------------------
//
void WriteLastPosition(uint32_t ELongitude,uint32_t ELatitude)

{
        Config.LastLongitude = ELongitude;
        Config.LastLatitude = ELatitude;
//      WriteParameter();

}


//--------------------------------------------------------------
//
void ReadParameter (void)
{
  uint8_t update=false;

     eeprom_read_block((void*)&Config, (const void*)&EEStruct, sizeof(ST));

     if (Config.Version != EEpromVersion)
     {

        switch (Config.Version)
        {
         case 0x77:                       // PKT Version 3.6.6a

         PointList_Clear();
           update = true;
         break;

         case 0x78:                       // PKT Version 3.6.6b
          /* Do necessary steps to perform upgrade */
           update = true;
         break;

         default:Delete_EEPROM(); //wenn kein Update möglich alles auf Default setzen
         break;
        }

        if (update)
          {
            Config.Version = EEpromVersion;  /* Update the EEPROM version number */
            sei();
            lcd_cls();
            lcd_printp_at (0, 2, PSTR("EEPromUpdate to new"), 0);
            lcd_printp_at (0, 3, PSTR("      Version      "), 0);
            lcd_printp_at (0, 5, PSTR("check the settings!"), 0);
            lcd_printp_at (18, 7, PSTR("OK"), 0);

            while (!get_key_press (1 << KEY_ENTER));
            WriteParameter();
            cli();
          }
     }
}


void WriteParameter (void)
{
        copy_line(7);
        lcd_printp_at (0, 7, PSTR("   Write EEPROM    "), 0);
        eeprom_update_block((const void*)&Config, (void*)&EEStruct, sizeof(ST));
        paste_line(7);

}



//--------------------------------------------------------------
void Delete_EEPROM(void)
{
        // EEPROM auf Default setzen

        lcd_cls();
        lcd_printp_at (0, 0, PSTR(" EEPROM Parameter    "), 2);
        lcd_printp_at (0, 1, PSTR("werden auf"), 0);
        lcd_printp_at (0, 2, PSTR("Standardwerte gesetzt"), 0);

        Config.MK_LowBat        = 137;                          // 13,7V
        Config.DisplayTimeout   = 0;                            // Display immer an
        Config.DisplayLanguage  = 5;                            // default undefined
        Config.WiTXRXChannel    = 1;                            // Kanal 1 MK Standard
        Config.WiNetworkGroup   = 66;                           // Gruppe 66  MK Standard
        Config.WiNetworkMode    = NetMode_Normal;       // MK Standard
        Config.WiTXTO           = TWaitTime16;          // MK Standard
        Config.WiUartMTU        = UartMTU64;            // MK Standard
        Config.LCD_ORIENTATION  = 0;                            // normale Ansicht
        Config.LCD_DisplayMode  = 0;                            // Normal
        Config.LCD_Kontrast     = 20;                           // Kontrast normal
        Config.LCD_Helligkeit   = 100;                          // Helligkeit in %
        Config.USBBT            = 0;                            // USB Betrieb
        Config.U02SV2           = 0;                            // SV2 (Kabel) Standard
        Config.Debug            = 0;                            // kein Debug
        Config.UseWi            = false;                        // Wi.232 eingebaut?
        Config.UseBT            = false;                        // BT-222 eingebaut?
        Config.WiIsSet          = true;                         // Flag für die Initialisierung Wi232
        Config.BTIsSet          = true;                         // Flag für die Initialisierung Bluetooth
        Config.BTIsSlave        = true;                          // Slave Flag setzen
        Config.PKT_IdleBeep     = 0;                            // kein Piepsen bei Inaktivität
        Config.PKT_StartInfo    = false;                        // Startinformationen anzeigen
        Config.PKT_Accutyp     = true;                          // True = Lipo, False= LiON
        Config.OSD_RCErrorbeep = true;                        // OSD Receiveerrorbeep
        Config.OSD_InvertOut   = false;                        // LED Anzeige invertiren
        Config.OSD_LEDform     = 1;                           // Form der Anzeige ( + oder schwarz)
        Config.OSD_SendOSD     = false;                        // OSD Daten an SV2
        Config.OSD_Fallspeed   = 40;                            // maximale Sinkrate
        Config.OSD_VarioBeep   = 1;                            // Vario Beep ein
        Config.OSD_HomeMKView  = true;                         // Home Circle from MK View
        Config.OSD_mAh_Warning = 10000;                        //mAh Warnschwelle
        Config.OSD_ScreenMode  = 0;                            // Variante des OSD Screen
        Config.OSD_LipoBar     = 0;                            //Bargraphanzeige für MK Lipo
        Config.PKT_Baudrate    = Baud_57600;                  //Baudrate für BT und Wi232
        Config.Lipo_UOffset    = 6000;                         // Offset für PKT-Lipomessung
        Config.FM_Refresh      = 500;                          // FollowMe interval
        Config.FM_Speed        = 30;                           // FollowMe Speed in m/s *0.1
        Config.FM_Radius       = 5;                             // Waypoint Tolerance Radius in meter
        Config.HWSound         = 0;                            // Hardware Sounderweiterung an PD7
        Config.HWBeeper        = 1;                            // Hardware Beeper an PC7
        Config.Volume          = 0;                            //Lautstärke
        Config.LastLongitude   = 88199720;
        Config.LastLatitude    = 522039630;

        strcpy_P(Config.bt_pin, PSTR("0000"));
        strcpy_P(Config.bt_name, PSTR("PKT Cebra "));  // Wenn Name kürzer als "bt_name_length" mit Leerzeichen auffüllen
        strcpy_P(Config.bt_Mac, PSTR("0000-00-000000"));
        strcpy_P(Config.RE_ID, PSTR("0000"));
        for(uint8_t i = 0; i < 20; i++)
          {
           Config.gps_UsedDevName[i]      = 0;                                         // benutztes GPS Device Name
          }
        for(uint8_t i = 0; i < 14; i++)
          {
           Config.gps_UsedMac[i]          = '0';                                         // benutztes GPS Device Mac Adresse
          }
        Config.gps_UseGPS              = false;                                     // ist GPS aktiv?
        Config.gps_UsedGPSMouse        = GPS_Bluetoothmouse1;
        Config.WiIsSet = false;
        Config.BTIsSet = false;
        Config.Version = EEpromVersion;
        Config.sIdxSteps=  STEPS_255;
        Config.servo[0].rev = SERVO_REV;
        Config.servo[0].min = SERVO_I0_RIGHT;
        Config.servo[0].max = SERVO_I0_LEFT;
        Config.servo[0].mid = SERVO_I0_MIDDLE;
        Config.servo[1].rev = SERVO_REV;
        Config.servo[1].min = SERVO_I0_RIGHT;
        Config.servo[1].max = SERVO_I0_LEFT;
        Config.servo[1].mid = SERVO_I0_MIDDLE;
        Config.servo_frame = SERVO_PERIODE;
        Config.single_step= SINGLE_STEP;                // nur bei Test-Servo
        Config.repeat=  REPEAT;                          // nur bei Test-Servo
        Config.pause =  PAUSE;                            // nur bei Test-Servo
        Config.pause_step= PAUSE_STEP;                  // nur bei Test-Servo
        Config.tracking =  TRACKING_MIN;
        Config.track_hyst = TRACKING_HYSTERESE;
        Config.track_tx = 0;

        for (uint8_t i = 0; i < 5; i++) {
           Config.stick_min[i] = 30+i;
           Config.stick_max[i] = 270+i;
           Config.stick_typ[i] = 0;
           Config.stick_dir[i] = 0;
           Config.stick_neutral[i] = 0;
          }
        Config.Lipomessung = true;
        PointList_Clear();
        WriteParameter();

//      lcd_printp_at (0, 4, PSTR("Waypoints loeschen"), 0);
//      EEWayPointList_Clear();

        lcd_printp_at (0, 6, PSTR("!!Check Parameter!! "), 0);
        lcd_printp_at (18, 7, PSTR("OK"), 0);
        sei ();
        set_beep ( 200, 0x0080, BeepNormal);
      do{}
        while (!(get_key_press (1 << KEY_ENTER)));

#if defined HWVERSION3_9
        clr_V_On();
#else

        wdt_enable( WDTO_250MS );
                        while (1)
                        {;}
#endif
}


//--------------------------------------------------------------
//
//void EEWayPointList_Clear(void)                       // löschen der Waypointliste im EEProm
//{
//      uint8_t i;
//      PKTWayPoint.Waypoint.Position.Latitude = 0;
//      PKTWayPoint.Waypoint.Position.Longitude = 0;
//      PKTWayPoint.Waypoint.Position.Altitude = 0;
//      PKTWayPoint.Waypoint.Heading = 361;

//      for(i = 0; i < MAX_WPLIST_LEN; i++)
//      {
//              PKTWayPointDirectory.WPList.WPDirectory[i] = 0;
//      }

//      for(i = 0; i < NumberOfWaypoints; i++)
//      {
//              lcd_printp (PSTR("."), 0);
//              eeprom_write_byte (&EEWayPointList[i].WPIndex, i);
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.Position.Status, INVALID);
//              eeprom_write_block ((const void*)&PKTWayPoint.Waypoint.Position.Latitude, (void*)&EEWayPointList[i].Waypoint.Position.Latitude, sizeof(EEWayPointList[i].Waypoint.Position.Latitude));
//              eeprom_write_block ((const void*)&PKTWayPoint.Waypoint.Position.Longitude, (void*)&EEWayPointList[i].Waypoint.Position.Longitude, sizeof(EEWayPointList[i].Waypoint.Position.Longitude));
//              eeprom_write_block ((const void*)&PKTWayPoint.Waypoint.Position.Altitude, (void*)&EEWayPointList[i].Waypoint.Position.Altitude, sizeof(EEWayPointList[i].Waypoint.Position.Altitude));
//              eeprom_write_block ((const void*)&PKTWayPoint.Waypoint.Heading, (void*)&EEWayPointList[i].Waypoint.Heading, sizeof(EEWayPointList[i].Waypoint.Heading));
//
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.ToleranceRadius, 0);             // in meters, if the MK is within that range around the target, then the next target is triggered
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.HoldTime, 0);                    // in seconds, if the was once in the tolerance area around a WP, this time defines the delay before the next WP is triggered
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.Type, POINT_TYPE_INVALID);
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.Event_Flag, 0);                  // future implementation
//              eeprom_write_byte (&EEWayPointList[i].Waypoint.AltitudeRate, 0);                // no change of setpoint
//      }

//      for(i = 0; i < NumberOfWPLists; i++)
//      {
//              lcd_printp (PSTR("."), 0);
//              eeprom_write_byte (&EEWPDirectory[i].WPList.WPListnumber, i);
//              eeprom_write_byte (&EEWPDirectory[i].WPList.WPListAktiv, false);
//              eeprom_write_byte (&EEWPDirectory[i].WPList.POI_CAM_NICK_CTR, 0);
//              eeprom_write_byte (&EEWPDirectory[i].WPList.UsePOI, 0);
//              eeprom_write_block ((const void*)&PKTWayPointDirectory.WPList.WPDirectory, (void*)&EEWPDirectory[i].WPList.WPDirectory, sizeof(EEWPDirectory[i].WPList.WPDirectory));
//
//      }
//      lcd_printp (PSTR("\r\n"), 0);

//}