2 * Copyright 2013 Olivier Matz <zer0@droids-corp.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <aversive/pgmspace.h>
25 #include <aversive/eeprom.h>
26 #include <aversive/error.h>
32 #include "eeprom_config.h"
34 /* load configuration from eeprom */
35 int8_t eeprom_load_config(void)
37 struct eeprom_config *e = NULL;
38 struct eeprom_cmd cmd;
42 eeprom_read_block(&magic, &e->magic, sizeof(magic));
43 if (magic != EEPROM_CONFIG_MAGIC) {
44 printf_P(PSTR("no EEPROM config\n"));
49 max = eeprom_get_ncmds();
50 for (i = 0; i < max; i++) {
51 eeprom_get_cmd(&cmd, i);
52 printf_P(PSTR("%s"), cmd.buf);
53 cmdline_valid_buffer(cmd.buf, strlen(cmd.buf));
59 uint8_t eeprom_get_ncmds(void)
61 struct eeprom_config *e = NULL;
64 eeprom_read_block(&ncmds, &e->ncmds, sizeof(ncmds));
68 void eeprom_set_ncmds(uint8_t ncmds)
70 struct eeprom_config *e = NULL;
71 uint32_t magic = EEPROM_CONFIG_MAGIC;
72 eeprom_update_block(&ncmds, &e->ncmds, sizeof(ncmds));
73 eeprom_update_block(&magic, &e->magic, sizeof(magic));
76 /* fill cmd struct with the n-th command from eeprom, no check is done
77 * on index or size. The \0 is added at the end of the string. */
78 void eeprom_get_cmd(struct eeprom_cmd *cmd, uint8_t n)
80 struct eeprom_config *e = NULL;
82 eeprom_read_block(cmd, &e->cmds[n], sizeof(*cmd));
83 cmd->buf[EEPROM_CMD_SIZE-1] = '\0';
86 /* fill n-th command of eeprom from struct, no check is done on index
88 void eeprom_set_cmd(struct eeprom_cmd *cmd, uint8_t n)
90 struct eeprom_config *e = NULL;
92 eeprom_update_block(cmd, &e->cmds[n], sizeof(*cmd));
95 void eeprom_dump_cmds(void)
98 struct eeprom_cmd cmd;
100 printf_P(PSTR("init commands:\n"));
101 max = eeprom_get_ncmds();
102 for (i = 0; i < max; i++) {
103 eeprom_get_cmd(&cmd, i);
104 printf_P(PSTR("%.2d: %s"), i, cmd.buf);
108 int8_t eeprom_insert_cmd_before(const char *str, uint8_t n)
111 struct eeprom_cmd cmd;
113 if (strlen(str) >= EEPROM_CMD_SIZE)
116 max = eeprom_get_ncmds();
119 if (max >= EEPROM_N_CMD_MAX)
122 for (i = max; i > n; i--) {
123 eeprom_get_cmd(&cmd, i-1);
124 eeprom_set_cmd(&cmd, i);
127 snprintf(cmd.buf, sizeof(cmd.buf), "%s", str);
128 eeprom_set_cmd(&cmd, n);
129 eeprom_set_ncmds(max + 1);
133 int8_t eeprom_append_cmd(const char *str)
137 max = eeprom_get_ncmds();
138 return eeprom_insert_cmd_before(str, max);
141 int8_t eeprom_delete_cmd(uint8_t n)
144 struct eeprom_cmd cmd;
146 max = eeprom_get_ncmds();
150 for (i = n; i < max-1; i++) {
151 eeprom_get_cmd(&cmd, i+1);
152 eeprom_set_cmd(&cmd, i);
155 eeprom_set_ncmds(max - 1);
159 void eeprom_reset(void)