MDM166AA: Clock is now synchronized at the second.
authorStephaneLenclud
Thu, 05 Feb 2015 15:09:48 +0100
changeset 307f649078cb52
parent 29 9b44c6e1651c
child 31 0a2b658e0d56
MDM166AA: Clock is now synchronized at the second.
FutabaMDM166AA.cpp
FutabaMDM166AA.h
     1.1 --- a/FutabaMDM166AA.cpp	Thu Feb 05 14:26:29 2015 +0100
     1.2 +++ b/FutabaMDM166AA.cpp	Thu Feb 05 15:09:48 2015 +0100
     1.3 @@ -21,6 +21,7 @@
     1.4  
     1.5  MDM166AA::MDM166AA():
     1.6      iOffScreenMode(true),
     1.7 +	iNeedAccurateClockData(false),
     1.8      iFrameNext(NULL),
     1.9      iFrameCurrent(NULL),
    1.10      iFramePrevious(NULL),
    1.11 @@ -78,9 +79,13 @@
    1.12  		SetNonBlocking(1);
    1.13  		//
    1.14  		SendCommandReset();
    1.15 -		//
    1.16 -		ShowClock();
    1.17 -
    1.18 +		
    1.19 +		//We will need accurate clock data
    1.20 +		iNeedAccurateClockData=true;
    1.21 +		//Until we get it just use rough time instead
    1.22 +		//We don't set clock data here as it turns on clock display too and cause an unpleasant clock flash
    1.23 +		//Only side effect from not doing this here is that for at most one minute the first time you cold boot your display the time should be wrong.
    1.24 +		//SetClockData();
    1.25  		}
    1.26  	return success;
    1.27  	}
    1.28 @@ -116,22 +121,6 @@
    1.29  	}
    1.30  
    1.31  /**
    1.32 -*/
    1.33 -/*
    1.34 -void MDM166AA::BitBlit(const BitArray& aBitmap, int aSrcWidth, int aSrcHeight, int aTargetX, int aTargetY) const
    1.35 -	{
    1.36 -	//TODO: amend loop values so that we don't keep on looping past our frame buffer dimensions.
    1.37 -	for (int i=0;i<aSrcWidth;i++)
    1.38 -		{
    1.39 -		for (int j=0;j<aSrcHeight;j++)
    1.40 -			{
    1.41 -            iFrameNext->SetBitValue((aTargetX+i)*HeightInPixels()+aTargetY+j,aBitmap[+i*aSrcHeight+j]);
    1.42 -			}
    1.43 -		}
    1.44 -	}
    1.45 -*/
    1.46 -
    1.47 -/**
    1.48  Clear our client side back buffer.
    1.49  Call to SwapBuffers must follow to actually clear the display.
    1.50  */
    1.51 @@ -197,11 +186,41 @@
    1.52  	}
    1.53  
    1.54  /**
    1.55 +Check if accurate clock data is needed and update display clock if system clock seconds are zero.
    1.56 +This is intended to be called every frame from our SwapBuffers function.
    1.57 +*/
    1.58 +void MDM166AA::AttemptClockSynchronization()
    1.59 +	{
    1.60 +	//Check if accurate clock data is needed
    1.61 +	if (!iNeedAccurateClockData)
    1.62 +		{
    1.63 +		return;
    1.64 +		}
    1.65 +
    1.66 +	//Fetch local time
    1.67 +	time_t rawtime;
    1.68 +	struct tm * timeinfo;
    1.69 +	time ( &rawtime );
    1.70 +	timeinfo = localtime ( &rawtime );
    1.71 +
    1.72 +	//If our seconds are zero we synchronize our display clock
    1.73 +	if (timeinfo->tm_sec==0)
    1.74 +		{
    1.75 +		SendCommandSetClockData(timeinfo->tm_hour,timeinfo->tm_min);
    1.76 +		//Our clock is as accurate as it can for the time being
    1.77 +		iNeedAccurateClockData=false;
    1.78 +		}
    1.79 +	}
    1.80 +
    1.81 +/**
    1.82  Put our off screen buffer on screen.
    1.83  On screen buffer goes off screen.
    1.84  */
    1.85  void MDM166AA::SwapBuffers()
    1.86  	{
    1.87 +	//We need to synchronize our clock seconds
    1.88 +	AttemptClockSynchronization();
    1.89 +
    1.90  	//Only perform buffer swapping if off screen mode is enabled
    1.91  	if (OffScreenMode())
    1.92  		{
    1.93 @@ -344,7 +363,11 @@
    1.94  */
    1.95  void MDM166AA::ShowClock()
    1.96  	{
    1.97 -	SetClockData();
    1.98 +	//Assuming display clock is at least roughly set since we do it when opening our display connection.
    1.99 +	//We will need accurate clock data next we get a chance.
   1.100 +	//This should guarantee that if our display remain open for weeks our clock will be synchronized whenever we switch back from clock mode to render mode.
   1.101 +	iNeedAccurateClockData=true;
   1.102 +	//Show clock using specified styles
   1.103  	SendCommandClockDisplay(EClockLarge,EClock24);
   1.104  	}
   1.105  
   1.106 @@ -384,7 +407,13 @@
   1.107  
   1.108  /**
   1.109  Set display clock data according to local system time.
   1.110 -This needs to be redone whenever we open or turn on our display.
   1.111 +This will only provide 30s accuracy.
   1.112 +In fact display clock seconds are set to zero whenever clock data is set.
   1.113 +So you would only get second accuracy if this function was called when system time is at zero second.
   1.114 +It's the responsibility of AttemptClockSynchronization function to obtain second accuracy.
   1.115 +The present function is intended to provide only rough clock synchronization.
   1.116 +
   1.117 +@note Unfortunately this command also turns on clock display.
   1.118  */
   1.119  void MDM166AA::SetClockData()
   1.120  	{
     2.1 --- a/FutabaMDM166AA.h	Thu Feb 05 14:26:29 2015 +0100
     2.2 +++ b/FutabaMDM166AA.h	Thu Feb 05 15:09:48 2015 +0100
     2.3 @@ -76,25 +76,26 @@
     2.4  	//Clock commands
     2.5  	void SendCommandSetClockData(unsigned char aHour, unsigned char aMinute);
     2.6  	void SendCommandClockDisplay(TClockSize aClockSize, TClockFormat aClockFormat);	
     2.7 +	void AttemptClockSynchronization();
     2.8  
     2.9  	//Graphics commands
    2.10  	void SendCommandSetAddressCounter(unsigned char aAddressCounter);
    2.11  	void SendCommandWriteGraphicData(int aSize, unsigned char* aPixels);
    2.12  
    2.13 -private:
    2.14      void RequestDeviceId();
    2.15      void RequestFirmwareRevision();
    2.16      void RequestPowerSupplyStatus();
    2.17  	//
    2.18  	void SetClockData();
    2.19 -
    2.20 -private:
    2.21 +	//
    2.22  	void ResetBuffers();
    2.23  
    2.24  private:
    2.25  	///Off screen mode is the recommended default settings to avoid tearing.
    2.26  	///Though turning it off can be useful for debugging
    2.27  	bool iOffScreenMode;
    2.28 +	///We use this flag to align display clock seconds with system time
    2.29 +	bool iNeedAccurateClockData;
    2.30      //
    2.31  	BitArrayLow* iFrameNext;
    2.32      BitArrayLow* iFrameCurrent;