# HG changeset patch # User StephaneLenclud # Date 1423145388 -3600 # Node ID 7f649078cb523a6cf84be8cc5fbce42ff89e0574 # Parent 9b44c6e1651c5c75e19f4f1c01be5bd42bdce618 MDM166AA: Clock is now synchronized at the second. diff -r 9b44c6e1651c -r 7f649078cb52 FutabaMDM166AA.cpp --- a/FutabaMDM166AA.cpp Thu Feb 05 14:26:29 2015 +0100 +++ b/FutabaMDM166AA.cpp Thu Feb 05 15:09:48 2015 +0100 @@ -21,6 +21,7 @@ MDM166AA::MDM166AA(): iOffScreenMode(true), + iNeedAccurateClockData(false), iFrameNext(NULL), iFrameCurrent(NULL), iFramePrevious(NULL), @@ -78,9 +79,13 @@ SetNonBlocking(1); // SendCommandReset(); - // - ShowClock(); - + + //We will need accurate clock data + iNeedAccurateClockData=true; + //Until we get it just use rough time instead + //We don't set clock data here as it turns on clock display too and cause an unpleasant clock flash + //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. + //SetClockData(); } return success; } @@ -116,22 +121,6 @@ } /** -*/ -/* -void MDM166AA::BitBlit(const BitArray& aBitmap, int aSrcWidth, int aSrcHeight, int aTargetX, int aTargetY) const - { - //TODO: amend loop values so that we don't keep on looping past our frame buffer dimensions. - for (int i=0;iSetBitValue((aTargetX+i)*HeightInPixels()+aTargetY+j,aBitmap[+i*aSrcHeight+j]); - } - } - } -*/ - -/** Clear our client side back buffer. Call to SwapBuffers must follow to actually clear the display. */ @@ -197,11 +186,41 @@ } /** +Check if accurate clock data is needed and update display clock if system clock seconds are zero. +This is intended to be called every frame from our SwapBuffers function. +*/ +void MDM166AA::AttemptClockSynchronization() + { + //Check if accurate clock data is needed + if (!iNeedAccurateClockData) + { + return; + } + + //Fetch local time + time_t rawtime; + struct tm * timeinfo; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + + //If our seconds are zero we synchronize our display clock + if (timeinfo->tm_sec==0) + { + SendCommandSetClockData(timeinfo->tm_hour,timeinfo->tm_min); + //Our clock is as accurate as it can for the time being + iNeedAccurateClockData=false; + } + } + +/** Put our off screen buffer on screen. On screen buffer goes off screen. */ void MDM166AA::SwapBuffers() { + //We need to synchronize our clock seconds + AttemptClockSynchronization(); + //Only perform buffer swapping if off screen mode is enabled if (OffScreenMode()) { @@ -344,7 +363,11 @@ */ void MDM166AA::ShowClock() { - SetClockData(); + //Assuming display clock is at least roughly set since we do it when opening our display connection. + //We will need accurate clock data next we get a chance. + //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. + iNeedAccurateClockData=true; + //Show clock using specified styles SendCommandClockDisplay(EClockLarge,EClock24); } @@ -384,7 +407,13 @@ /** Set display clock data according to local system time. -This needs to be redone whenever we open or turn on our display. +This will only provide 30s accuracy. +In fact display clock seconds are set to zero whenever clock data is set. +So you would only get second accuracy if this function was called when system time is at zero second. +It's the responsibility of AttemptClockSynchronization function to obtain second accuracy. +The present function is intended to provide only rough clock synchronization. + +@note Unfortunately this command also turns on clock display. */ void MDM166AA::SetClockData() { diff -r 9b44c6e1651c -r 7f649078cb52 FutabaMDM166AA.h --- a/FutabaMDM166AA.h Thu Feb 05 14:26:29 2015 +0100 +++ b/FutabaMDM166AA.h Thu Feb 05 15:09:48 2015 +0100 @@ -76,25 +76,26 @@ //Clock commands void SendCommandSetClockData(unsigned char aHour, unsigned char aMinute); void SendCommandClockDisplay(TClockSize aClockSize, TClockFormat aClockFormat); + void AttemptClockSynchronization(); //Graphics commands void SendCommandSetAddressCounter(unsigned char aAddressCounter); void SendCommandWriteGraphicData(int aSize, unsigned char* aPixels); -private: void RequestDeviceId(); void RequestFirmwareRevision(); void RequestPowerSupplyStatus(); // void SetClockData(); - -private: + // void ResetBuffers(); private: ///Off screen mode is the recommended default settings to avoid tearing. ///Though turning it off can be useful for debugging bool iOffScreenMode; + ///We use this flag to align display clock seconds with system time + bool iNeedAccurateClockData; // BitArrayLow* iFrameNext; BitArrayLow* iFrameCurrent;