Analog clock: Difference between revisions
From FSDeveloper Wiki
Jump to navigationJump to search
No edit summary |
mNo edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| 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. | ||
Edited 22 August 2020 to replace the menu time change with a more reliable trap. | |||
| Line 8: | Line 10: | ||
// 43,200 seconds to twelve hours | // 43,200 seconds to twelve hours | ||
//---------------------------------------------------------- | //---------------------------------------------------------- | ||
MODULE_VAR localsecond = { CLOCK_SECOND }; | MODULE_VAR localsecond = { CLOCK_SECOND }; | ||
MODULE_VAR tick18 = { TICK18 }; | |||
// Global clock hand variables for the gauge display | // Global clock hand variables for the gauge display | ||
double second_hand = 0; | double second_hand = 0; | ||
double minute_hand = 0; | double minute_hand = 0; | ||
double hour_hand = 0; | double hour_hand = 0; | ||
//---------------------------------------------------------- | //---------------------------------------------------------- | ||
// Function to be called from the gauge update - | // Function to be called from the gauge update - | ||
// Can be used in any project without having to change anything | // Can be used in any project without having to change anything | ||
//---------------------------------------------------------- | //---------------------------------------------------------- | ||
void analogClock() | void analogClock() | ||
| Line 33: | Line 32: | ||
static double set_clock = 0; | static double set_clock = 0; | ||
static double prev_tick = -1; | static double prev_tick = -1; | ||
int min_check = 0; | |||
// | int force_update_period = 5; // Number of minutes difference between menu and clock that will force a clock update | ||
// Set clock to the current simulator time | // Set clock to the current simulator time | ||
| Line 48: | Line 40: | ||
set_clock = 1; | set_clock = 1; | ||
mins = localminutes.var_value.n; | mins = localminutes.var_value.n; | ||
// Apply the current minutes to the check variable | |||
min_check = mins; | |||
hours = localhours.var_value.n; | hours = localhours.var_value.n; | ||
// | // 24 hour trap | ||
if (hours > 12) hours -= 12; | if (hours > 12) hours -= 12; | ||
// Adjust needle positioning | // Adjust for needle positioning | ||
hours *= 3600; | hours *= 3600; | ||
mins *= 60; | mins *= 60; | ||
| Line 59: | Line 53: | ||
{ | { | ||
// Resume normal updates | // Resume normal updates | ||
x = localseconds.var_value.n; | |||
if (x != prev_secs) | if (x != prev_secs) | ||
{ | { | ||
| Line 68: | Line 60: | ||
mins += 1; | mins += 1; | ||
if (mins >= 3599)mins = 0; | if (mins >= 3599)mins = 0; | ||
// Apply the actual current minutes to the check variable | |||
min_check = (int)mins/60; | |||
// Hour hand | // Hour hand | ||
hours += 1; | hours += 1; | ||
if (hours >= 43199)hours = 0; | if (hours >= 43199)hours = 0; | ||
} | } | ||
} | } | ||
// Trap a time change by menu. | |||
// A difference of five minutes or more will trigger a clock update | |||
x = (int)localminutes.var_value.n; | |||
if (abs(x - min_check) > force_update_period)set_clock = 0; | |||
return; | return; | ||
} | } | ||
[[category:Aircraft Design]] [[category:Panel and Gauge Design]] | [[category:Aircraft Design]] [[category:Panel and Gauge Design]] | ||
Latest revision as of 06:28, 5 November 2023
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.
Edited 22 August 2020 to replace the menu time change with a more reliable trap.
//----------------------------------------------------------
// 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;
int min_check = 0;
int force_update_period = 5; // Number of minutes difference between menu and clock that will force a clock update
// Set clock to the current simulator time
if (set_clock == 0)
{
set_clock = 1;
mins = localminutes.var_value.n;
// Apply the current minutes to the check variable
min_check = mins;
hours = localhours.var_value.n;
// 24 hour trap
if (hours > 12) hours -= 12;
// Adjust for needle positioning
hours *= 3600;
mins *= 60;
hours += mins;
}
else
{
// Resume normal updates
x = localseconds.var_value.n;
if (x != prev_secs)
{
prev_secs = x;
// Minute hand
mins += 1;
if (mins >= 3599)mins = 0;
// Apply the actual current minutes to the check variable
min_check = (int)mins/60;
// Hour hand
hours += 1;
if (hours >= 43199)hours = 0;
}
}
// Trap a time change by menu.
// A difference of five minutes or more will trigger a clock update
x = (int)localminutes.var_value.n;
if (abs(x - min_check) > force_update_period)set_clock = 0;
return;
}