Exemple #1
0
void setRTCFromText(char *time) {
	// This function parses the date and or time using any of the following format:
	//  1. #h#m#s
	//  2. #m#d
	//  3. #m#d#h#m#s
	//  where '#' represents any valid positive integer.  
	//  Note that 'm' is used for month and for minute. 'm' will mean month, unless it follows an 'h'.
	//  Note that year is not set because it is used to indicate the time period (see cold_start code)
	
	int month = -1, date = -1, hour = -1, min = -1, sec = -1;
	char *ptr;
	
	if ((ptr = findTimePart(time,'m'))) {
		month = strToInt(ptr);
		if ((ptr = findTimePart(time,'d')))
			date = strToInt(ptr);
		else 
			month = -1;
	}
	if ((ptr = findTimePart(time,'h'))) {
		hour = strToInt(ptr);
		if ((ptr = findTimePart(ptr,'m')))
			min = strToInt(ptr);
		if ((ptr = findTimePart(ptr,'s')))
			sec = strToInt(ptr);
	}
	if (month >= 1 && date >= 1)
		setDate(month,date);
	if (hour >= 0 && min >= 0 && sec >= 0)
		setRTC(hour,min,sec);
}
eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
{
	instance=0;
	if (ready())
	{
		eDebug("[eDVBLocalTimeHandler] set RTC to previous valid time");
		setRTC(::time(0));
	}
}
Exemple #3
0
eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
{
	instance=0;
	if (ready())
	{
		eDebug("set RTC to previous valid time");
		if (strncmp(mybox,"gb800solo", sizeof(mybox)) == 0 || strncmp(mybox,"gb800se", sizeof(mybox)) == 0 || strncmp(mybox,"gb800ue", sizeof(mybox)) == 0)
				eDebug("Dont set RTC to previous valid time, giga box");
			else
				setRTC(::time(0));
	}
}
Exemple #4
0
void godob::handle_time_keys(btnval_e lcd_key)
{
  switch (lcd_key)
    {
    case btnRIGHT:
      if(t_set==true){
	t_idx++;
	t_idx %= 7;
      }
      t_set = true;
      t_adjust=show_adjust(t_idx);
      break;
    case btnLEFT:
      if(t_set==true){
	t_idx+=6;
	t_idx %= 7;
      }
      t_set = true;
      t_adjust=show_adjust(t_idx);
      break;
    case btnUP:
      if(t_set == true){
	//adjustTime(t_adjust); /* doesn't work when t_adjust>0. weird! */
	setTime(now() + t_adjust);
      }
      else{
	handle_main_keys(lcd_key);
      }
      break;
    case btnDOWN:
      if(t_set == true){
	adjustTime(-t_adjust);
      }
      else{
	handle_main_keys(lcd_key);
      }
      break;
    case btnSELECT:
      if(t_set==false){
	handle_main_keys(lcd_key);
      }
      else{
	setRTC();
	LCD->noCursor();
      }
      t_set=false;
      break;
    case btnNONE:
      break;
    }
}
Exemple #5
0
//----------------------------------------------------------------------
// Sets the Real-Time clock (RTC) of the 1-Wire device to the time 
// on the PC, with an option to start the oscillator.
//
// Parameters:
//  portnum   The port number of the port being used for the
//            1-Wire network.
//  SNum      The 1-Wire address of the device to communicate.
//  OscEnable Sets the Oscillator enable bit of the control register.  A 
//            TRUE will turn the oscillator one and a FALSE will turn 
//            the oscillator off.
//
// Returns:  TRUE  if the write worked.
//           FALSE if there was an error in writing to the part.
//
SMALLINT setRTCFromPC(int portnum, uchar * SNum, SMALLINT OscEnable)
{
   ulong timelong = 0;
   timedate td;

   // get seconds since Jan 1, 1970
   getPCTime(&td); // first, get timedate
   timelong = DateToSeconds(&td); // convert timedate to seconds since Jan 1, 1970
   // write 4 bytes to 1-Wire clock device (memory bank 2) 
   // starting at address 0x03.
   if (setRTC(portnum, SNum, timelong, OscEnable) != TRUE)
   {
	   return FALSE;
   }
   return TRUE;
}
void eDVBServiceController::disableFrontend()
{
	if ( transponder && (!service || service.path) &&
#ifndef DISABLE_FILE
		!dvb.recorder &&
#endif
		!dvb.getScanAPI() )  // no more service need the frontend..
	{
		eDebug("no more dvb service running.. disable Frontend");
		transponder=0;
		eFrontend::getInstance()->savePower();
		if ( eDVB::getInstance()->time_difference &&  // have valid time?
			eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
			&& eSystemInfo::getInstance()->hasStandbyWakeupTimer() )
		{
			eDebug("set RTC when frontend is disabled..");
			time_t nowTime=time(0)+eDVB::getInstance()->time_difference;
			setRTC(nowTime);
		}
	}
}
Exemple #7
0
extern void getRTC(char * str) {
	unsigned long r,c,p,d,h,m,s;
	char time[RTC_STRING_LENGTH];
	
	h = (unsigned long) *P_Hour;	
	m = (unsigned long) *P_Minute;
	s = (unsigned long) *P_Second;
	if (h >23) {
		h -= 24;
		setRTC(h,m,s);
		incrementCumulativeDays();
	}
	r = (long)getRotation();
	c = (long)getPowerups();
	p = (long)getPeriod();  
	d = (long)getCumulativeDays();

	if (r >= 0)
		time[0] = r + '0';
	else
		time[0] = '_';
	time[1] = 'r';
	longToDecimalString(c,time+2,4);
	time[6] = 'c';
	if (p >= 0)
		longToDecimalString(p,time+7,3);
	else
		strcpy(time+7,(char *)"___");
	time[10] = 'p';
	longToDecimalString(d,time+11,3);
	time[14] = 'd';
	longToDecimalString(h,time+15,2);
	time[17] = 'h';
	longToDecimalString(m,time+18,2);
	time[20] = 'm';
	longToDecimalString(s,time+21,2);
	time[23] = 's';
	time[24] = 0;
	strcpy(str,time);
}
void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan, int update_count )
{
	int time_difference;
	bool restart_tdt = false;
	if (!tp_time)
		restart_tdt = true;
	else if (tp_time == -1)
	{
		restart_tdt = true;
		/*if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
		( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
			&& eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) )     TODO !!!!!!! */
		{
			eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
			time_t rtc_time = getRTC();
			if ( rtc_time ) // RTC Ready?
			{
				tm now;
				localtime_r(&rtc_time, &now);
				eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d",
					now.tm_hour,
					now.tm_min,
					now.tm_sec);
				time_t linuxTime=time(0);
				localtime_r(&linuxTime, &now);
				eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d",
					now.tm_hour,
					now.tm_min,
					now.tm_sec);
				time_difference = rtc_time - linuxTime;
				eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %ld seconds", linuxTime - rtc_time );
				if ( time_difference )
				{
					eDebug("[eDVBLocalTimerHandler] set Linux Time to RTC Time");
					timeval tnow;
					gettimeofday(&tnow,0);
					tnow.tv_sec=rtc_time;
					settimeofday(&tnow,0);
				}
				else if ( !time_difference )
					eDebug("[eDVBLocalTimerHandler] no change needed");
				else
					eDebug("[eDVBLocalTimerHandler] set to RTC time");
				/*emit*/ m_timeUpdated();
			}
			else
				eDebug("[eDVBLocalTimerHandler]    getRTC returned time=0. RTC problem?");
		}
	}
	else
	{
		std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );

 // current linux time
		time_t linuxTime = time(0);

	// difference between current enigma time and transponder time
		int enigma_diff = tp_time-linuxTime;

		int new_diff=0;

		bool updated = m_time_ready;

		if ( m_time_ready )  // ref time ready?
		{
			// difference between reference time (current enigma time)
			// and the transponder time
			eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
			if ( abs(enigma_diff) < 120 )
			{
				eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
				m_timeOffsetMap[chan->getChannelID()] = 0;
				new_diff = enigma_diff;
			}
			else if ( it != m_timeOffsetMap.end() ) // correction saved?
			{
				eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
				time_t CorrectedTpTime = tp_time+it->second;
				int ddiff = CorrectedTpTime-linuxTime;
				eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
				if ( abs(it->second) < 300 ) // stored correction < 5 min
				{
					eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
					new_diff = ddiff;
				}
				else if ( getRTC() )
				{
					time_t rtc=getRTC();
					m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
					new_diff = rtc-linuxTime;  // set enigma time to rtc
					eDebug("[eDVBLocalTimerHandler] update stored correction to %ld (calced against RTC time)", rtc-tp_time );
				}
				else if ( abs(ddiff) <= 120 )
				{
// with stored correction calced time difference is lower 2 min
// this don't help when a transponder have a clock running to slow or to fast
// then its better to have a DM7020 with always running RTC
					eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
					new_diff = ddiff;
				}
				else  // big change in calced correction.. hold current time and update correction
				{
					eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
					m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
				}
			}
			else
			{
				eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
				m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
			}
		}
		else  // no time setted yet
		{
			if ( it != m_timeOffsetMap.end() )
			{
				enigma_diff += it->second;
				eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
			}
			else
				eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
			new_diff=enigma_diff;
			m_time_ready=true;
		}

		time_t t = linuxTime+new_diff;
		m_last_tp_time_difference=tp_time-t;

		if (!new_diff &&
			updated) // overrride this check on first received TDT
		{
			eDebug("[eDVBLocalTimerHandler] not changed");
			return;
		}

		if ( !update_count )
		{
			// set rtc to calced transponder time when the first tdt is received on this
			// transponder
			setRTC(t);
			eDebug("[eDVBLocalTimerHandler] update RTC");
		}
		else if (getRTC())
		{
			if (abs(getRTC() - t) > 60)
			{
				eDebug("[eDVBLocalTimerHandler] difference between new linux time and RTC time is > 60 sec... transponder time looks not ok... use rtc time");
				t = getRTC();
			}
			else
				eDebug("[eDVBLocalTimerHandler] difference between linux time and RTC time is < 60 sec... so the transponder time looks ok");
		}
		else
			eDebug("[eDVBLocalTimerHandler] no RTC available :(");

		tm now;
		localtime_r(&t, &now);
		eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
			now.tm_hour,
			now.tm_min,
			now.tm_sec);

		time_difference = t - linuxTime;   // calc our new linux_time -> enigma_time correction
		eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", time_difference );

		if ( time_difference )
		{
			eDebug("[eDVBLocalTimerHandler] set Linux Time");
			timeval tnow;
			gettimeofday(&tnow,0);
			tnow.tv_sec=t;
			settimeofday(&tnow,0);
		}

 		 /*emit*/ m_timeUpdated();
	}

	if ( restart_tdt )
	{
		std::map<iDVBChannel*, channel_data>::iterator it =
			m_knownChannels.find(chan);
		if ( it != m_knownChannels.end() )
		{
			int updateCount = it->second.tdt->getUpdateCount();
			it->second.tdt = 0;
			it->second.tdt = new TDT(chan, updateCount);
			it->second.tdt->startTimer(TIME_UPDATE_INTERVAL);  // restart TDT for this transponder in 30min
		}
	}
}
void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan, int update_count )
{
	int time_difference;
	bool restart_tdt = false;
	if (!tp_time)
		restart_tdt = true;
	else if (tp_time == -1)
	{
		restart_tdt = true;

		eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
		time_t rtc_time = getRTC();
		if (rtc_time) // RTC Ready?
		{
			tm now;

			localtime_r(&rtc_time, &now);
			eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
			time_t linuxTime=time(0);
			localtime_r(&linuxTime, &now);
			eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
			time_difference = rtc_time - linuxTime;
			eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %ld seconds", linuxTime - rtc_time );

			if (time_difference)
			{
				if ((time_difference >= -15) && (time_difference <= 15))
				{
					timeval tdelta, tolddelta;

					// Slew small diffs ...
					// Even good transponders can differ by 0-5 sec, if we would step these
					// the system clock would permanentely jump around when zapping.

					tdelta.tv_sec = time_difference;

					if(adjtime(&tdelta, &tolddelta) == 0)
						eDebug("[eDVBLocalTimerHandler] slewing Linux Time by %03d seconds", time_difference);
					else
						eDebug("[eDVBLocalTimerHandler] slewing Linux Time by %03d seconds FAILED", time_difference);
				}
				else
				{
					timeval tnow;

					// ... only step larger diffs

					gettimeofday(&tnow, 0);
					tnow.tv_sec = rtc_time;
					settimeofday(&tnow, 0);
					linuxTime = time(0);
					localtime_r(&linuxTime, &now);
					eDebug("[eDVBLocalTimerHandler] stepped Linux Time to %02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
				}
			}
			else if ( !time_difference )
				eDebug("[eDVBLocalTimerHandler] no change needed");
			else
				eDebug("[eDVBLocalTimerHandler] set to RTC time");
			/*emit*/ m_timeUpdated();
		}
		else
			eDebug("[eDVBLocalTimerHandler]    getRTC returned time=0. RTC problem?");
	}
	else
	{
		std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );

 // current linux time
		time_t linuxTime = time(0);

	// difference between current enigma time and transponder time
		int enigma_diff = tp_time-linuxTime;

		int new_diff=0;

		bool updated = m_time_ready;

		if ( m_time_ready )  // ref time ready?
		{
			// difference between reference time (current enigma time)
			// and the transponder time
			eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
			if ( abs(enigma_diff) < 120 )
			{
				eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
				m_timeOffsetMap[chan->getChannelID()] = 0;
				new_diff = enigma_diff;
			}
			else if ( it != m_timeOffsetMap.end() ) // correction saved?
			{
				eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
				time_t CorrectedTpTime = tp_time+it->second;
				int ddiff = CorrectedTpTime-linuxTime;
				eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
				if ( abs(it->second) < 300 ) // stored correction < 5 min
				{
					eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
					new_diff = ddiff;
				}
				else if ( getRTC() )
				{
					time_t rtc=getRTC();
					m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
					new_diff = rtc-linuxTime;  // set enigma time to rtc
					eDebug("[eDVBLocalTimerHandler] update stored correction to %ld (calced against RTC time)", rtc-tp_time );
				}
				else if ( abs(ddiff) <= 120 )
				{
// with stored correction calced time difference is lower 2 min
// this don't help when a transponder have a clock running to slow or to fast
// then its better to have a DM7020 with always running RTC
					eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
					new_diff = ddiff;
				}
				else  // big change in calced correction.. hold current time and update correction
				{
					eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
					m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
				}
			}
			else
			{
				eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
				m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
			}
		}
		else  // no time setted yet
		{
			if ( it != m_timeOffsetMap.end() )
			{
				enigma_diff += it->second;
				eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
			}
			else
				eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
			new_diff=enigma_diff;
			m_time_ready=true;
		}

		time_t t = linuxTime+new_diff;
		m_last_tp_time_difference=tp_time-t;

		if (!new_diff &&
			updated) // overrride this check on first received TDT
		{
			eDebug("[eDVBLocalTimerHandler] not changed");
			return;
		}

		if ( !update_count )
		{
			// set rtc to calced transponder time when the first tdt is received on this
			// transponder
			setRTC(t);
			eDebug("[eDVBLocalTimerHandler] update RTC");
		}
		else if (getRTC())
		{
			if (abs(getRTC() - t) > 60)
			{
				eDebug("[eDVBLocalTimerHandler] difference between new linux time and RTC time is > 60 sec... transponder time looks not ok... use rtc time");
				t = getRTC();
			}
			else
				eDebug("[eDVBLocalTimerHandler] difference between linux time and RTC time is < 60 sec... so the transponder time looks ok");
		}
		else
			eDebug("[eDVBLocalTimerHandler] no RTC available :(");

		tm now;
		localtime_r(&t, &now);
		eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
			now.tm_hour,
			now.tm_min,
			now.tm_sec);

		time_difference = t - linuxTime;   // calc our new linux_time -> enigma_time correction
		eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", time_difference );

		if ( time_difference )
		{
			eDebug("[eDVBLocalTimerHandler] set Linux Time");
			timeval tnow;
			gettimeofday(&tnow,0);
			tnow.tv_sec=t;
			settimeofday(&tnow,0);
		}

 		 /*emit*/ m_timeUpdated();
	}

	if ( restart_tdt )
	{
		std::map<iDVBChannel*, channel_data>::iterator it =
			m_knownChannels.find(chan);
		if ( it != m_knownChannels.end() )
		{
			int system = iDVBFrontend::feSatellite;
			ePtr<iDVBFrontendParameters> parms;
			chan->getCurrentFrontendParameters(parms);
			if (parms)
			{
				parms->getSystem(system);
			}

			int updateCount = it->second.timetable->getUpdateCount();
			it->second.timetable = NULL;

			if (system == iDVBFrontend::feATSC)
			{
				it->second.timetable = new STT(chan, updateCount);
			}
			else
			{
				it->second.timetable = new TDT(chan, updateCount);
			}
			it->second.timetable->startTimer(TIME_UPDATE_INTERVAL); // restart TDT for this transponder in 30min
		}
	}
}
Exemple #10
0
void SkServer::dataReady()
{
  QByteArray data;

  data = m_client->readAll();

  qDebug() << data;

  QStringList commands;
  QString str;

  for (int i = 0; i < data.count(); i++)
  {
    if ((data.mid(i, 3) == "\r\n\n") || (data.mid(i, 3) == "\r\n"))
    {
      commands.append(str);
      i += 2;
      str.clear();
    }
    else
    {
      str += data[i];
    }
  }

  if (commands.count() == 0)
  {
    sendData(SKS_INVALID);
    return;
  }

  foreach (const QString &command, commands)
  {
    if (command.startsWith("Echo ", Qt::CaseInsensitive))
    {
      sendData(command.mid(5).toLocal8Bit());
    }
    else
    if (command.startsWith("SetPos ", Qt::CaseInsensitive))
    {
      setRA_Dec(command.mid(6));
    }
    else
    if (!command.compare("GetPos", Qt::CaseInsensitive))
    {
      getPos();
    }
    else
    if (!command.compare("GetJD", Qt::CaseInsensitive))
    {
      getJD();
    }
    else
    if (command.startsWith("SetJD ", Qt::CaseInsensitive))
    {
      setJD(command.mid(5));
    }
    else
    if (!command.compare("ZoomIn", Qt::CaseInsensitive))
    {
      zoom(1);
    }
    else
    if (!command.compare("ZoomOut", Qt::CaseInsensitive))
    {
      zoom(-1);
    }
    else
    if (command.startsWith("SetMode ", Qt::CaseInsensitive))
    {
      setMode(command.mid(8));
    }
    else
    if (!command.compare("ServerVer", Qt::CaseInsensitive))
    {
      sendData(SK_SERVER_VERSION);
    }
    else
    if (!command.compare("SwVer", Qt::CaseInsensitive))
    {
      sendData(SK_VERSION);
    }
    else
    if (command.startsWith("SetExtFrame ", Qt::CaseInsensitive))
    {
      setExtFrame(command.mid(11));
    }
    else
    if (!command.compare("GetExtFrame", Qt::CaseInsensitive))
    {
      getExtFrame();
    }
    else
    if (command.startsWith("SetRTC ", Qt::CaseInsensitive))
    {
      setRTC(command.mid(7));
    }
    else
    if (!command.compare("GetRTC", Qt::CaseInsensitive))
    {
      getRTC();
    }
    else
    if (!command.compare("Redraw", Qt::CaseInsensitive))
    {
      m_mainWin->getView()->repaintMap();
      sendData(SKS_OK);
    }
    else
    {
      sendData(SKS_UNKNOWN);
    }
  }
}
Exemple #11
0
void resetRTC(void) {
	setRTC(0,0,1);
}
void eDVBServiceController::TDTready(int error)
{
	eDebug("TDTready %d", error);
	// receive new TDT every 60 minutes
	updateTDTTimer.start(60*60*1000,true);
	if (!error && transponder)
	{
		std::map<tsref,int> &tOffsMap = eTransponderList::getInstance()->TimeOffsetMap;
		std::map< tsref, int >::iterator it( tOffsMap.find( *transponder ) );

 // current linux time
		time_t linuxTime = time(0);

 // current enigma time
		time_t nowTime=linuxTime+dvb.time_difference;

	// difference between current enigma time and transponder time
		int enigma_diff = tdt->UTC_time-nowTime;

		int new_diff=0;

		if (timeSet)  // ref time ready?
		{
			// difference between reference time (current enigma time) 
			// and the transponder time
			eDebug("[TIME] diff is %d", enigma_diff);
			if ( abs(enigma_diff) < 120 )
			{
				eDebug("[TIME] diff < 120 .. use Transponder Time");
				tOffsMap[*transponder] = 0;
				new_diff = enigma_diff;
			}
			else if ( it != tOffsMap.end() ) // correction saved?
			{
				eDebug("[TIME] we have correction %d", it->second);
				time_t CorrectedTpTime = tdt->UTC_time+it->second;
				int ddiff = CorrectedTpTime-nowTime;
				eDebug("[TIME] diff after add correction is %d", ddiff);
				if ( abs(it->second) < 300 ) // stored correction < 5 min
				{
					eDebug("[TIME] use stored correction(<5 min)");
					new_diff = ddiff;
				}
				else if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 &&
						getRTC() )
				{
					time_t rtc=getRTC();
					tOffsMap[*transponder] = rtc-tdt->UTC_time;
					new_diff = rtc-nowTime;  // set enigma time to rtc
					eDebug("[TIME] update stored correction to %d (calced against RTC time)", rtc-tdt->UTC_time );
				}
				else if ( abs(ddiff) <= 120 )
				{
// with stored correction calced time difference is lower 2 min
// this don't help when a transponder have a clock running to slow or to fast
// then its better to have a DM7020 with always running RTC
					eDebug("[TIME] use stored correction(corr < 2 min)");
					new_diff = ddiff;
				}
				else  // big change in calced correction.. hold current time and update correction
				{
					eDebug("[TIME] update stored correction to %d", -enigma_diff);
					tOffsMap[*transponder] = -enigma_diff;
				}
			}
			else
			{
				eDebug("[TIME] no correction found... store calced correction(%d)",-enigma_diff);
				tOffsMap[*transponder] = -enigma_diff;
			}
		}
		else  // no time setted yet
		{
			if ( it != tOffsMap.end() )
			{
				enigma_diff += it->second;
				eDebug("[TIME] we have correction (%d)... use", it->second );
			}
			else
				eDebug("[TIME] dont have correction.. set Transponder Diff");
			new_diff=enigma_diff;
		}

		time_t t = nowTime+new_diff;
		lastTpTimeDifference=tdt->UTC_time-t;

		if (!new_diff)
		{
			eDebug("[TIME] not changed");
			return;
		}

		tm now = *localtime(&t);
		eDebug("[TIME] time update to %02d:%02d:%02d",
			now.tm_hour,
			now.tm_min,
			now.tm_sec);

		dvb.time_difference = t - linuxTime;   // calc our new linux_time -> enigma_time correction
		eDebug("[TIME] time_difference is %d", dvb.time_difference );

		if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 )
			setRTC(t);

		if ( abs(dvb.time_difference) > 59 )
		{
			eDebug("[TIME] set Linux Time");
			timeval tnow;
			gettimeofday(&tnow,0);
			tnow.tv_sec=t;
			settimeofday(&tnow,0);
			for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
				;it != eMainloop::existing_loops.end(); ++it)
				it->setTimerOffset(dvb.time_difference);
			dvb.time_difference=1;
		}
		else if ( !dvb.time_difference )
			dvb.time_difference=1;

		timeSet = true;

		/*emit*/ dvb.timeUpdated();
	}
	else if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
		( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
			&& eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) )
	{
		eDebug("[TIME] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
		time_t rtc_time = getRTC();
		if ( rtc_time ) // RTC Ready?
		{
			tm now = *localtime(&rtc_time);
			eDebug("[TIME] RTC time is %02d:%02d:%02d",
				now.tm_hour,
				now.tm_min,
				now.tm_sec);
			time_t linuxTime=time(0);
			time_t nowTime=linuxTime+dvb.time_difference;
			now = *localtime(&nowTime);
			eDebug("[TIME] Receiver time is %02d:%02d:%02d",
				now.tm_hour,
				now.tm_min,
				now.tm_sec);
			dvb.time_difference = rtc_time - linuxTime;
			eDebug("[TIME] RTC to Receiver time difference is %d seconds", nowTime - rtc_time );
			if ( abs(dvb.time_difference) > 59 )
			{
				eDebug("[TIME] set Linux Time to RTC Time");
				timeval tnow;
				gettimeofday(&tnow,0);
				tnow.tv_sec=rtc_time;
				settimeofday(&tnow,0);
				for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
					;it != eMainloop::existing_loops.end(); ++it)
					it->setTimerOffset(dvb.time_difference);
				dvb.time_difference=1;
			}
			else if ( !dvb.time_difference )
				dvb.time_difference=1;
			else 
				eDebug("[TIME] set to RTC time");
			/*emit*/ dvb.timeUpdated();
		}
		else
			eDebug("[TIME] shit RTC not ready :(");
	}
}
Exemple #13
0
int main(void)
{
	// Configure port directions
	DDRB = 0xFF;
	DDRC = 0xFF;
	DDRD = 0xFF;
	
	// Clear all ports
	PORTB = 0x00;
	PORTC = 0x00;
	PORTD = 0x00;
	
	// Power up delay of 1 second
	// This is required since we don't have a capacitor on the reset line
	// which can cause the reset to bounce as power ramps up after being
	// turned on
	for (unsigned int delay = 0; delay < 100; delay++) _delay_ms(10);
	
	// Initialise the TLC5940s
	initialiseTlc5940();

	// Initialise the LED fading control
	initialiseFadingLeds();
	
	// Enable interrupts globally
	sei();
	
	// Initialise the DS1302 RTC
	initialiseRTC();
	
	// Check if the RTC is set or unset (first run)
	if (readClockStatus() == CLOCK_UNSET)
	{
		// Clock is unset, so we set it to 00:00
		datetime.hours = 0;
		datetime.minutes = 0;
		datetime.seconds = 0;
		datetime.dayNo = 0;
		datetime.day = 12;
		datetime.month = 6;
		datetime.year = 11;
		
		// Set the clock
		setRTC();
	}
	
	// Initialise the LDR
	initialiseLdr();
	
	// Initialise the buttons
	initialiseButtons();
	
	// Initialise state-machine to run power-up test
	unsigned char clockState = STATE_CHASETEST;
	//unsigned char clockState = STATE_CLOCKRUNNING;
	
	// Set the start brightness
	int displayBrightness = 4095;
	
	// State-machine delay counter
	unsigned int delayCounter1 = 0;
	
	// Button functions are hours, minutes, LDR on/off and test
	unsigned char minuteButtonDownFlag = 0;
	unsigned char hourButtonDownFlag = 0;
	
	unsigned char ldrActiveFlag = 1; // LDR is active
	
	// Set the led fading speed
	setLedFadeSpeed(30, 100);
	
	while(1)
	{
		// Update the delay counter
		delayCounter1++;
		
		// Poll the button states
		pollButtons();
		
		// Clock running state
		if (clockState == STATE_CLOCKRUNNING)
		{
			// Update the clock display ------------------------------------------------------------------
			if (delayCounter1 > 30000)
			{	
				int minuteOfDay = 0;
			
				// Read the real-time clock
				readRTC();
			
				// Calculate the minute of the day
				minuteOfDay = (datetime.hours * 60) + datetime.minutes;
			
				// Update the display
				displayMinute(minuteOfDay, displayBrightness);
				
				// Reset the delay counter
				delayCounter1 = 0;
			}
			
			// Button handling ---------------------------------------------------------------------------
			
			// Minute button pressed?
			if (button[BUTTON_MINUTE].buttonState == PRESSED && minuteButtonDownFlag == 0)
			{
				// Read the current time
				readRTC();
				
				// Advance one minute and reset seconds
				if (datetime.minutes == 59)
				{
					datetime.minutes = 0;
				}
				else datetime.minutes++;
				datetime.seconds = 0;
				
				// Set the RTC
				setRTC();
				
				minuteButtonDownFlag = 1;
			}
			
			// Minute button released?
			if (button[BUTTON_MINUTE].buttonState == RELEASED && minuteButtonDownFlag == 1)
			{
				minuteButtonDownFlag = 0;
			}
			
			// Hour button pressed?
			if (button[BUTTON_HOUR].buttonState == PRESSED && hourButtonDownFlag == 0)
			{
				// Read the current time
				readRTC();
				
				// Advance one hour and reset seconds
				if (datetime.hours == 23)
				{
					datetime.hours = 0;
				}
				else datetime.hours++;
				datetime.seconds = 0;
				
				// Set the RTC
				setRTC();
				
				hourButtonDownFlag = 1;
			}
			
			// Minute button released?
			if (button[BUTTON_HOUR].buttonState == RELEASED && hourButtonDownFlag == 1)
			{
				hourButtonDownFlag = 0;
			}
			
			// Test button pressed? If so change state
			if (button[BUTTON_TEST].buttonState == 1) clockState = STATE_CHASETEST;
			
			// LDR brightness control --------------------------------------------------------------------
			if (ldrActiveFlag == 1)
			{
				unsigned int ldrValue = 0;
				
				// Read the LDR brightness level (0-15)
				ldrValue = readLdrValue();
				
				// Here we use a switch statement to translate the LDR level into the brightness
				// level for the PWM (which allows us to choose a logarithmic or linear scale)
				switch(ldrValue)
				{
					case 0 :	displayBrightness = 100;
								break;
					
					case 1 :	displayBrightness = (4095/16) * 1;
								break;
					
					case 2 :	displayBrightness = (4095/16) * 2;
								break;
					
					case 3 :	displayBrightness = (4095/16) * 3;
								break;
					
					case 4 :	displayBrightness = (4095/16) * 4;
								break;
					
					case 5 :	displayBrightness = (4095/16) * 5;
								break;
					
					case 6 :	displayBrightness = (4095/16) * 6;
								break;
					
					case 7 :	displayBrightness = (4095/16) * 7;
								break;
					
					case 8 :	displayBrightness = (4095/16) * 8;
								break;
					
					case 9 :	displayBrightness = (4095/16) * 9;
								break;
					
					case 10 :	displayBrightness = (4095/16) * 10;
								break;
					
					case 11 :	displayBrightness = (4095/16) * 11;
								break;
					
					case 12 :	displayBrightness = (4095/16) * 12;
								break;
					
					case 13 :	displayBrightness = (4095/16) * 13;
								break;
					
					case 14 :	displayBrightness = (4095/16) * 14;
								break;
					
					case 15 :	displayBrightness = 4095;
								break;
				}
			}
		}
		
		// Clock test state
		if (clockState == STATE_CHASETEST)
		{
			// Perform the test
			chaseTest();
			emrTest();

			// Set the led fading speed
			setLedFadeSpeed(30, 100);

			// Go back to the clock running state
			clockState = STATE_CLOCKRUNNING;
		}
	}
}