Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# -- Executable Configuration --

# Note that for the main executable, we use the project forced to lowercase
# Note that for the main executable, we use the project name forced to lowercase
string(TOLOWER "${CMAKE_PROJECT_NAME}" EXECUTABLE_NAME)
set(EXECUTABLE_SOURCE application/buzzer.c
application/buzzer.h
Expand All @@ -22,18 +22,24 @@ set(EXECUTABLE_SOURCE application/buzzer.c
application/keyer.h
application/led.c
application/led.h
application/storage.c
application/storage.h
application/wpm.c
application/wpm.h
core/main.c
core/sys.c
core/sys.h
core/version.c
core/version.h
drivers/eeprom.c
drivers/eeprom.h
drivers/gpio.c
drivers/gpio.h
drivers/usart.c
drivers/usart.h
utility/constants.h
utility/crc.c
utility/crc.h
utility/debug.c
utility/debug.h
utility/types.h
Expand Down
66 changes: 62 additions & 4 deletions src/main/application/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,26 @@
#include "application/input.h"
#include "application/keyer.h"
#include "application/led.h"
#include "application/storage.h"
#include "core/sys.h"
#include "utility/debug.h"
#include "utility/types.h"
#include "utility/utility.h"

/* --------------------------------------------------- CONSTANTS ---------------------------------------------------- */

/**
* @def CONFIG_VERSION_CURRENT
* @brief The currently active configuration version.
*/
#define CONFIG_VERSION_CURRENT ( 1 )

/**
* @def MINIMUM_SAVE_PERIOD
* @brief Minimum elapsed time between saving config to storage.
*/
#define MINIMUM_SAVE_PERIOD ( 5 * TICKS_PER_SEC )

/* ----------------------------------------------------- MACROS ----------------------------------------------------- */

_Static_assert( _CONFIG_DFLT_WPM >= WPM_MINIMUM &&
Expand All @@ -31,9 +48,17 @@ _Static_assert( _CONFIG_DFLT_BUZZER_FREQUENCY >= BUZZER_MINIMUM_FREQUENCY &&
/* --------------------------------------------------- VARIABLES ---------------------------------------------------- */

static config_t s_config; /**< Currently active app configuration. */
static bool s_modified = false; /**< Has the configuration been modified? */
static tick_t s_save_tick = 0; /**< Tick config was last saved. */

/* ---------------------------------------------- PROCEDURE PROTOTYPES ---------------------------------------------- */

/**
* @fn flush( tick_t )
* @brief Writes the configuration to non-volatile storage and updates state.
*/
static void flush( tick_t tick );

/**
* @fn validate_config( config_t const * )
* @brief Returns `true` if all fields in the specified configuration struct are valid.
Expand Down Expand Up @@ -83,17 +108,31 @@ void config_default( config_t * config )
} /* config_default() */


void config_flush( void )
{
flush( sys_get_tick() );

} /* config_flush() */


void config_get( config_t * config )
{
memcpy( config, & s_config, sizeof( config_t ) );
* config = s_config;

} /* config_get() */


void config_init( void )
{
// Set configuration to defaults, for now
config_default( & s_config );
// Try to get configuration from storage, and restore defaults if that fails.
// In this case, any existing configuration will be lost the next time config is saved.
config_t config;
if( ! storage_get_config( CONFIG_VERSION_CURRENT, sizeof( config_t ), & config ) ||
! validate_config( & config ) )
config_default( & config );

// Set local copy
s_config = config;

} /* config_init() */

Expand All @@ -110,12 +149,31 @@ bool config_set( config_t const * config )
if( ! validate_config( config ) )
return( false );

memcpy( & s_config, config, sizeof( config_t ) );
s_config = * config;
s_modified = true;
return( true );

} /* config_set() */


void config_tick( tick_t tick )
{
if( ! s_modified || sys_elapsed( tick, s_save_tick ) < MINIMUM_SAVE_PERIOD )
return;
flush( tick );

} /* config_tick() */


static void flush( tick_t tick )
{
storage_set_config( CONFIG_VERSION_CURRENT, sizeof( config_t ), & s_config );
s_modified = false;
s_save_tick = tick;

} /* flush() */


static bool validate_config( config_t const * config )
{
if( config->wpm < WPM_MINIMUM ||
Expand Down
18 changes: 18 additions & 0 deletions src/main/application/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ typedef struct

} config_t;

/**
* @typedef config_version_t
* @brief Typedef for the configuration version number.
*/
typedef uint8_t config_version_t;

/* ----------------------------------------------------- MACROS ----------------------------------------------------- */

/**
Expand All @@ -74,6 +80,12 @@ typedef struct
*/
void config_default( config_t * config );

/**
* @fn config_flush( void )
* @brief Immediately writes the current configuration to storage.
*/
void config_flush( void );

/**
* @fn config_get( config_t * )
* @brief Copies the current application configuration into the specified struct.
Expand All @@ -100,4 +112,10 @@ config_t const * config_read_only( void );
*/
bool config_set( config_t const * config );

/**
* @fn config_tick( tick_t )
* @brief Performs periodic processing at the specified tick.
*/
void config_tick( tick_t tick );

#endif /* !defined( APPLICATION_CONFIG_H ) */
Loading