Analog clock: Difference between revisions

From FSDeveloper Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they 'flick over' - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy'n'paste code.
The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they 'flick over' - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy'n'paste code, but check that you haven't already declared the module_vars under different names.




Line 11: Line 11:
   
   
  MODULE_VAR localsecond = { CLOCK_SECOND };
  MODULE_VAR localsecond = { CLOCK_SECOND };
MODULE_VAR tick18 = { TICK18 };
   
   
    
    
Line 35: Line 36:
   // Trap a time change by menu. Video and gauge processing is suspended while the menu is active
   // Trap a time change by menu. Video and gauge processing is suspended while the menu is active
   // Use this to force a clock update (set_clock = 0) when video processing resumes
   // Use this to force a clock update (set_clock = 0) when video processing resumes
   if (prev_tick == -1)prev_tick = getTick();
   if (prev_tick == -1)prev_tick = tick18.var_value.n;
   if (prev_tick != getTick())
   if (prev_tick != tick18.var_value.n)
   {
   {
     if (getTick() > prev_tick + 1)set_clock = 0;
     if (tick18.var_value.n > prev_tick + 1)set_clock = 0;
     prev_tick = getTick();
     prev_tick = tick18.var_value.n;
   }
   }
    
    

Revision as of 15:56, 7 January 2020

The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they 'flick over' - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy'n'paste code, but check that you haven't already declared the module_vars under different names.


//----------------------------------------------------------
// Time - 12 hour analog clock
// Driven from seconds only to get better movement of the hands
// 3,600 seconds to an hour
// 43,200 seconds to twelve hours
//----------------------------------------------------------
 

MODULE_VAR localsecond = { CLOCK_SECOND };
MODULE_VAR tick18 = { TICK18 };

 
// Global clock hand variables for the gauge display 
double second_hand = 0;
double minute_hand = 0;
double hour_hand = 0;

 
//----------------------------------------------------------
// Function to be called from the gauge update -
// Can be used in any project without having to change anything
//----------------------------------------------------------
void analogClock()
{
  int x = 0;
 
  static double mins = -1;
  static double hours = -1;
  static double prev_secs = -1;
  static double set_clock = 0;
  static double prev_tick = -1;
 
  // Trap a time change by menu. Video and gauge processing is suspended while the menu is active
  // Use this to force a clock update (set_clock = 0) when video processing resumes
  if (prev_tick == -1)prev_tick = tick18.var_value.n;
  if (prev_tick != tick18.var_value.n)
  {
    if (tick18.var_value.n > prev_tick + 1)set_clock = 0;
    prev_tick = tick18.var_value.n;
  }
 
  // Set clock to the current simulator time
  if (set_clock == 0)
  {
    set_clock = 1;
    mins = localminutes.var_value.n;
    hours = localhours.var_value.n;
    // 12 hour trap
    if (hours > 12) hours -= 12;
    // Adjust needle positioning for when normal updates resume
    hours *= 3600;
    mins *= 60;
    hours += mins;
  }
  else
  {
    // Resume normal updates
    second_hand = localsecond.var_value.n;
 
    x = (int)second_hand;
    if (x != prev_secs)
    {
      prev_secs = x;
      // Minute hand
      mins += 1;
      if (mins >= 3599)mins = 0;
      minute_hand = mins;
      // Hour hand
      hours += 1;
      // Reset if passing through 00.00 or 12.00
      if (hours >= 43199)hours = 0;
      hour_hand = hours;
    }
  }
  return;
}