X-Git-Url: http://git.droids-corp.org/?p=protos%2Fimu.git;a=blobdiff_plain;f=eeprom_config.c;fp=eeprom_config.c;h=f3274d8e48219df33a06cd70ee2ce214c50ebee8;hp=0000000000000000000000000000000000000000;hb=84796cd7a01e949167a155ec21f55fd71e788015;hpb=883f5aae494ab066938b0cebb554a1ee13766713 diff --git a/eeprom_config.c b/eeprom_config.c new file mode 100644 index 0000000..f3274d8 --- /dev/null +++ b/eeprom_config.c @@ -0,0 +1,157 @@ +/* + * Copyright 2013 Olivier Matz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include + +#include +#include +#include + +#include + + +#include "cmdline.h" +#include "eeprom_config.h" + +/* load configuration from eeprom */ +int8_t eeprom_load_config(void) +{ + struct eeprom_config *e = NULL; + struct eeprom_cmd cmd; + uint32_t magic; + uint8_t i, max; + + eeprom_read_block(&magic, &e->magic, sizeof(magic)); + if (magic != EEPROM_CONFIG_MAGIC) { + printf_P(PSTR("no EEPROM config\n")); + eeprom_set_ncmds(0); + return 0; + } + + max = eeprom_get_ncmds(); + for (i = 0; i < max; i++) { + eeprom_get_cmd(&cmd, i); + printf_P(PSTR("%s"), cmd.buf); + cmdline_valid_buffer(cmd.buf, strlen(cmd.buf)); + } + + return -1; +} + +uint8_t eeprom_get_ncmds(void) +{ + struct eeprom_config *e = NULL; + uint8_t ncmds; + + eeprom_read_block(&ncmds, &e->ncmds, sizeof(ncmds)); + return ncmds; +} + +void eeprom_set_ncmds(uint8_t ncmds) +{ + struct eeprom_config *e = NULL; + uint32_t magic = EEPROM_CONFIG_MAGIC; + eeprom_update_block(&ncmds, &e->ncmds, sizeof(ncmds)); + eeprom_update_block(&magic, &e->magic, sizeof(magic)); +} + +/* fill cmd struct with the n-th command from eeprom, no check is done + * on index or size. The \0 is added at the end of the string. */ +void eeprom_get_cmd(struct eeprom_cmd *cmd, uint8_t n) +{ + struct eeprom_config *e = NULL; + + eeprom_read_block(cmd, &e->cmds[n], sizeof(*cmd)); + cmd->buf[EEPROM_CMD_SIZE-1] = '\0'; +} + +/* fill n-th command of eeprom from struct, no check is done on index + * or size */ +void eeprom_set_cmd(struct eeprom_cmd *cmd, uint8_t n) +{ + struct eeprom_config *e = NULL; + + eeprom_update_block(cmd, &e->cmds[n], sizeof(*cmd)); +} + +void eeprom_dump_cmds(void) +{ + uint8_t i, max; + struct eeprom_cmd cmd; + + printf_P(PSTR("init commands:\n")); + max = eeprom_get_ncmds(); + for (i = 0; i < max; i++) { + eeprom_get_cmd(&cmd, i); + printf_P(PSTR("%.2d: %s"), i, cmd.buf); + } +} + +int8_t eeprom_insert_cmd_before(const char *str, uint8_t n) +{ + uint8_t i, max; + struct eeprom_cmd cmd; + + if (strlen(str) >= EEPROM_CMD_SIZE) + return -1; + + max = eeprom_get_ncmds(); + if (n > max) + return -1; + if (max >= EEPROM_N_CMD_MAX) + return -1; + + for (i = max; i > n; i--) { + eeprom_get_cmd(&cmd, i-1); + eeprom_set_cmd(&cmd, i); + } + + snprintf(cmd.buf, sizeof(cmd.buf), "%s", str); + eeprom_set_cmd(&cmd, n); + eeprom_set_ncmds(max + 1); + return 0; +} + +int8_t eeprom_append_cmd(const char *str) +{ + uint8_t max; + + max = eeprom_get_ncmds(); + return eeprom_insert_cmd_before(str, max); +} + +int8_t eeprom_delete_cmd(uint8_t n) +{ + uint8_t i, max; + struct eeprom_cmd cmd; + + max = eeprom_get_ncmds(); + if (n >= max) + return -1; + + for (i = n; i < max-1; i++) { + eeprom_get_cmd(&cmd, i+1); + eeprom_set_cmd(&cmd, i); + } + + eeprom_set_ncmds(max - 1); + return 0; +}