UEFIStarter
a simple UEFI framework
Data Structures | Macros | Functions | Variables
snow.c File Reference

This application shows animated snow in text mode. More...

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <UEFIStarter/core.h>

Data Structures

struct  flake_t
 data type describing a snowflake More...
 

Macros

#define FLAKE_SCREEN_WIDTH   _screen_width
 the snowflake canvas width
 
#define FLAKE_SCREEN_HEIGHT   (_screen_height-2)
 the snowflake canvas height
 
#define FLAKE_DEFAULT_DURATION_SECONDS   60
 the default time (in seconds) this application should run
 
#define FLAKE_DEFAULT_UPDATE_INTERVAL_MILLISECONDS   100
 the default update interval (in ms) between frames
 
#define FLAKE_DEFAULT_COUNT   100
 the default number of flakes, including off-screen ones
 
#define FLAKE_DEFAULT_GROUND_LIFETIME   10
 the default lifetime (in seconds) of snowflakes on the ground
 
#define FLAKE_CROSS_STEP   0.3
 the default wind acceleration step size
 
#define FLAKE_MAX_CROSS_SPEED   2.0
 the default maximum wind speed
 
#define FLAKE_CROSS_SPEED_FALLOFF_MULT   0.8
 the default wind speed falloff multiplier
 
#define FLAKE_CROSS_SPEED_BASE   0.1
 the default base wind speed
 
#define ARG_SECONDS   _argument_list[0].value.uint64
 helper macro to access application lifetime parameter
 
#define ARG_FLAKE_COUNT   _argument_list[1].value.uint64
 helper macro to access flake count parameter
 
#define ARG_UPDATE_INTERVAL   _argument_list[2].value.uint64
 helper macro to access update interval parameter
 
#define ARG_GROUND_LIFETIME   _argument_list[3].value.uint64
 helper macro to access ground lifetime parameter
 
#define ARG_CROSS_STEP   _argument_list[4].value.dbl
 helper macro to access wind acceleration step size parameter
 
#define ARG_MAX_CROSS_SPEED   _argument_list[5].value.dbl
 helper macro to access max wind speed parameter
 
#define ARG_CROSS_SPEED_FALLOFF_MULT   _argument_list[6].value.dbl
 helper macro to access wind speed falloff parameter
 
#define ARG_CROSS_SPEED_BASE   _argument_list[7].value.dbl
 helper macro to access base wind speed parameter
 

Functions

UINT32 random ()
 This generates a very-pseudo random number. More...
 
void init_flake (flake_t *flake, int time_offset)
 Initializes a snowflake. More...
 
int update_flakes (flake_t flakes[], int iteration, float cross_speed, int land_times[])
 Handles snow flake movement for a single frame of animation. More...
 
EFI_INPUT_KEY read_key ()
 Reads keyboard input and returns the pressed key. More...
 
void print_cross_speed (double speed)
 Debugging function: prints the current wind speed if SHOW_CROSS_SPEED is defined. More...
 
void update_ground (int iteration, int land_times[])
 Removes any expired snowflakes on ground. More...
 
void do_print_snow ()
 Main animation loop.
 
void clear ()
 Clears the screen.
 
INTN EFIAPI ShellAppMain (UINTN argc, CHAR16 **argv)
 Main function, gets invoked by UEFI shell. More...
 

Variables

static cmdline_argument_t _argument_list []
 command-line arguments More...
 
static cmdline_argument_group_t _arguments ={ L"Weather options (in a UEFI boot time executable, mind you)" ,sizeof( _argument_list )/sizeof(cmdline_argument_t), _argument_list }
 command-line argument group
 
static UINTN _screen_width
 internal storage for screen's total width (in characters)
 
static UINTN _screen_height
 internal storage for screen's total height (in characters)
 
static UINTN _ground_lifetime_frames
 internal storage for snowflake's ground lifetime (in number of frames)
 

Detailed Description

This application shows animated snow in text mode.

Press the reft/right arrow keys for wind. Press 'q' to quit.

Author
Richard Nusser
License
GPLv3 (see http://www.gnu.org/licenses/)
See also
https://github.com/rinusser/UEFIStarter

Function Documentation

◆ random()

UINT32 random ( )

This generates a very-pseudo random number.

Don't use this function for anything but trivial applications, the numbers are nowhere near random.

Returns
a number random enough for this application
Todo:
There's a GetRandomNumber32() function in EDK2 but it seems to segfault all the time, see what's up

◆ init_flake()

void init_flake ( flake_t flake,
int  time_offset 
)

Initializes a snowflake.

Parameters
flakethe flake to initialize/reset
time_offsetthe time of the flake's creation
Todo:
use screen height and max cross speed to accurately calculate this

◆ update_flakes()

int update_flakes ( flake_t  flakes[],
int  iteration,
float  cross_speed,
int  land_times[] 
)

Handles snow flake movement for a single frame of animation.

This function doesn't handle flakes on the ground: those are actually just visual artifacts. The snowflakes fall "through" the ground, get reset to above the screen and start from there. The flakes seen on the ground were just never removed during animation. Instead the flake's landing time is written to land_times, update_ground() will draw over those flakes that landed more than the configured ground lifetime ago.

Parameters
flakesthe snowflakes' data
iterationthe animation loop iteration number
cross_speedthe current wind speed
land_timesthe list of flakes' last landing times, one entry per text column
Returns
the number of currently moving snowflakes; on error the (1-based) snowflake number the error happened at

◆ read_key()

EFI_INPUT_KEY read_key ( )

Reads keyboard input and returns the pressed key.

Some terminals and terminal multiplexers generate multiple input events for just one keystroke (e.g. escape sequences), this will return the last of them.

Returns
the pressed key

◆ print_cross_speed()

void print_cross_speed ( double  speed)

Debugging function: prints the current wind speed if SHOW_CROSS_SPEED is defined.

Parameters
speedthe wind speed to print

◆ update_ground()

void update_ground ( int  iteration,
int  land_times[] 
)

Removes any expired snowflakes on ground.

Parameters
iterationthe current animation iteration number
land_timesthe last flake landing times per text column

◆ ShellAppMain()

INTN EFIAPI ShellAppMain ( UINTN  argc,
CHAR16 **  argv 
)

Main function, gets invoked by UEFI shell.

Parameters
argcthe number of command-line arguments passed
argvthe command-line arguments passed
Returns
an EFI status code for the shell

Variable Documentation

◆ _argument_list

cmdline_argument_t _argument_list[]
static
Initial value:
= {
{{uint64:FLAKE_DEFAULT_DURATION_SECONDS}, ARG_INT, NULL,L"-duration", L"Duration (in seconds) snow should fall"},
{{uint64:FLAKE_DEFAULT_COUNT}, ARG_INT, NULL,L"-count", L"Number of flakes generated (about half of them on screen)"},
{{uint64:FLAKE_DEFAULT_UPDATE_INTERVAL_MILLISECONDS},ARG_INT, NULL,L"-interval", L"Interval (in milliseconds) between frames"},
{{uint64:FLAKE_DEFAULT_GROUND_LIFETIME}, ARG_INT, NULL,L"-lifetime", L"Lifetime (in seconds) of flakes on ground"},
{{dbl:FLAKE_CROSS_STEP}, ARG_DOUBLE,NULL,L"-cross-step", L"Crosswind increment step"},
{{dbl:FLAKE_MAX_CROSS_SPEED}, ARG_DOUBLE,NULL,L"-max-cross-speed", L"Maximum crosswind speed"},
{{dbl:FLAKE_CROSS_SPEED_FALLOFF_MULT}, ARG_DOUBLE,NULL,L"-cross-falloff-multi",L"Crosswind speed falloff multiplier (keep <=1.0)"},
{{dbl:FLAKE_CROSS_SPEED_BASE}, ARG_DOUBLE,NULL,L"-base-cross-speed", L"Base crosswind speed"}
}
#define FLAKE_DEFAULT_UPDATE_INTERVAL_MILLISECONDS
the default update interval (in ms) between frames
Definition: snow.c:25
#define FLAKE_CROSS_SPEED_BASE
the default base wind speed
Definition: snow.c:31
#define FLAKE_DEFAULT_DURATION_SECONDS
the default time (in seconds) this application should run
Definition: snow.c:24
#define FLAKE_MAX_CROSS_SPEED
the default maximum wind speed
Definition: snow.c:29
#define FLAKE_DEFAULT_GROUND_LIFETIME
the default lifetime (in seconds) of snowflakes on the ground
Definition: snow.c:27
#define FLAKE_CROSS_SPEED_FALLOFF_MULT
the default wind speed falloff multiplier
Definition: snow.c:30
#define FLAKE_CROSS_STEP
the default wind acceleration step size
Definition: snow.c:28
integer
Definition: cmdline.h:23
double
Definition: cmdline.h:24
#define FLAKE_DEFAULT_COUNT
the default number of flakes, including off-screen ones
Definition: snow.c:26

command-line arguments