demo/pong/commands.c

See examples/demo/pong

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
#include "common.h"
RO_MEM pong_settings_t default_settings =
{
.magic_number = SETTINGS_MAGIC_NUMBER,
.update_interval = 600,
.min_update_interval = 200,
.pad_sensitivity = 55,
.speed_update = 20,
.pad_size = 3,
.max_score = 5
};
/*************************************************************
* Getters List
*/
ZOS_ADD_GETTER("pong.update.increment", update_increment),
ZOS_ADD_GETTER("pong.update.min_interval", update_min_interval),
ZOS_ADD_GETTER("pong.pad.sensitivity", pad_sensitivity),
ZOS_ADD_GETTER("pong.pad.size", pad_size),
ZOS_ADD_GETTER("pong.max_score", max_score),
/*************************************************************
* Setters List
*/
ZOS_ADD_SETTER("pong.update.increment", update_increment),
ZOS_ADD_SETTER("pong.update.min_interval", update_min_interval),
ZOS_ADD_SETTER("pong.pad.sensitivity", pad_sensitivity),
ZOS_ADD_SETTER("pong.pad.size", pad_size),
ZOS_ADD_SETTER("pong.max_score", max_score),
/*************************************************************
* Commands List
*/
ZOS_ADD_COMMAND("pong_restart", 0, 0, ZOS_FALSE, pong_restart)
/*************************************************************************************************/
void command_init(void)
{
ZOS_NVM_GET_REF(game_context.settings);
// check that app_settings_t hasn't overflowed the NVM
// if the build fails here then app_settings_t is too large to fit into the NVM
// you need to make this struct smaller
BUILD_CHECK_NVM_SIZE(pong_settings_t);
// if the nvm settings haven't been initialized, do it now
if(game_context.settings->magic_number != SETTINGS_MAGIC_NUMBER)
{
if(zn_load_ro_memory(game_context.settings, sizeof(pong_settings_t), &default_settings, 0) != ZOS_SUCCESS)
{
ZOS_LOG("Failed to loaded default settings");
}
}
}
/*************************************************************************************************/
void command_deinit(void)
{
}
/*************************************************************************************************/
ZOS_DEFINE_COMMAND(pong_restart)
{
game_update_state(STATE_SYNCHRONIZING);
}
/*************************************************************************************************/
ZOS_DEFINE_GETTER(update_increment)
{
zn_cmd_format_response(CMD_SUCCESS, "%u", game_context.settings->update_interval);
return CMD_SUCCESS;
}
/*************************************************************************************************/
ZOS_DEFINE_SETTER(update_increment)
{
ZOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, game_context.settings->update_interval, argv[1], 1, 200);
return CMD_SET_OK;
}
/*************************************************************************************************/
ZOS_DEFINE_GETTER(update_min_interval)
{
zn_cmd_format_response(CMD_SUCCESS, "%u", game_context.settings->min_update_interval);
return CMD_SUCCESS;
}
/*************************************************************************************************/
ZOS_DEFINE_SETTER(update_min_interval)
{
ZOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, game_context.settings->min_update_interval, argv[1], 50, 1000);
return CMD_SET_OK;
}
/*************************************************************************************************/
ZOS_DEFINE_GETTER(pad_sensitivity)
{
zn_cmd_format_response(CMD_SUCCESS, "%u", game_context.settings->pad_sensitivity);
return CMD_SUCCESS;
}
/*************************************************************************************************/
ZOS_DEFINE_SETTER(pad_sensitivity)
{
ZOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, game_context.settings->pad_sensitivity, argv[1], 20, 200);
return CMD_SET_OK;
}
/*************************************************************************************************/
{
zn_cmd_format_response(CMD_SUCCESS, "%u", game_context.settings->pad_size);
return CMD_SUCCESS;
}
/*************************************************************************************************/
{
ZOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, game_context.settings->pad_size, argv[1], 1, 5);
return CMD_SET_OK;
}
/*************************************************************************************************/
{
zn_cmd_format_response(CMD_SUCCESS, "%u", game_context.settings->max_score);
return CMD_SUCCESS;
}
/*************************************************************************************************/
{
ZOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, game_context.settings->max_score, argv[1], 1, 254);
return CMD_SET_OK;
}