SRC += parse_monitor.c
SRC += cmdline.c
SRC += beep.c
+SRC += eeprom_config.c
CFLAGS += -W -Wall -Werror
return c;
}
-static void
-valid_buffer(const char *buf, uint8_t size)
+void cmdline_valid_buffer(const char *buf, uint8_t size)
{
int8_t ret;
PGM_P ctx = (PGM_P)main_ctx;
}
-static void write_char(char c)
+void cmdline_write_char(char c)
{
cmdline_dev_send(c, NULL);
}
void cmdline_init(void)
{
- rdline_init(&xbeeboard.rdl, write_char, valid_buffer, complete_buffer);
+ rdline_init(&xbeeboard.rdl, cmdline_write_char, cmdline_valid_buffer, complete_buffer);
snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
}
int cmdline_dev_send(char c, FILE* f);
int cmdline_dev_recv(FILE* f);
+void cmdline_write_char(char c);
+void cmdline_valid_buffer(const char *buf, uint8_t size);
int xbee_dev_send(char c, FILE* f);
int xbee_dev_recv(FILE* f);
#include "main.h"
#include "cmdline.h"
#include "beep.h"
+#include "eeprom_config.h"
/* commands_gen.c */
extern const parse_inst_t PROGMEM cmd_reset;
/* stress test: send many commands, no wait between each servo
* of a series, and a variable delay between series */
printf_P(PSTR("stress test\r\n"));
- while (!cmdline_keypressed()) {
+ while (!cmdline_keypressed()) {
wait_time++;
if (wait_time > 20)
},
};
+/**********************************************************/
+
+/* this structure is filled when cmd_test_eeprom_config is parsed successfully */
+struct cmd_test_eeprom_config_result {
+ fixed_string_t arg0;
+};
+
+static void cmd_test_eeprom_config_parsed(void *parsed_result, void *data)
+{
+ (void)parsed_result;
+ (void)data;
+
+ eeprom_dump_cmds();
+ eeprom_append_cmd("salut1\n");
+ eeprom_dump_cmds();
+ eeprom_append_cmd("salut2\n");
+ eeprom_append_cmd("salut3\n");
+ eeprom_append_cmd("salut4\n");
+ eeprom_dump_cmds();
+ eeprom_insert_cmd_before("coin\n", 0);
+ eeprom_insert_cmd_before("coin2\n", 2);
+ eeprom_dump_cmds();
+ eeprom_delete_cmd(2);
+ eeprom_delete_cmd(0);
+ eeprom_dump_cmds();
+}
+
+const char PROGMEM str_test_eeprom_config_arg0[] = "test_eeprom_config";
+const parse_token_string_t PROGMEM cmd_test_eeprom_config_arg0 =
+ TOKEN_STRING_INITIALIZER(struct cmd_test_eeprom_config_result, arg0,
+ str_test_eeprom_config_arg0);
+
+const char PROGMEM help_test_eeprom_config[] = "Test the eeprom configuration";
+const parse_inst_t PROGMEM cmd_test_eeprom_config = {
+ .f = cmd_test_eeprom_config_parsed, /* function to call */
+ .data = NULL, /* 2nd arg of func */
+ .help_str = help_test_eeprom_config,
+ .tokens = { /* token list, NULL terminated */
+ (PGM_P)&cmd_test_eeprom_config_arg0,
+ NULL,
+ },
+};
+
+/* ************* */
+
+struct cmd_eeprom_del_result {
+ fixed_string_t cmd;
+ fixed_string_t action;
+ uint8_t n;
+};
+
+static void cmd_eeprom_del_parsed(void *parsed_result,
+ void *data)
+{
+ struct cmd_eeprom_del_result *res = parsed_result;
+
+ (void)data;
+ if (eeprom_delete_cmd(res->n) < 0)
+ printf_P(PSTR("cannot delete command\n"));
+ eeprom_dump_cmds();
+}
+
+const char PROGMEM str_eeprom_del_eeprom[] = "eeprom";
+const parse_token_string_t PROGMEM cmd_eeprom_del_cmd =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, cmd,
+ str_eeprom_del_eeprom);
+const char PROGMEM str_eeprom_del_del[] = "del";
+const parse_token_string_t PROGMEM cmd_eeprom_del_action =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, action,
+ str_eeprom_del_del);
+const parse_token_num_t PROGMEM cmd_eeprom_del_num =
+ TOKEN_NUM_INITIALIZER(struct cmd_eeprom_del_result, n,
+ UINT8);
+
+const char PROGMEM help_eeprom_del[] = "delete an eeprom init command";
+const parse_inst_t PROGMEM cmd_eeprom_del = {
+ .f = cmd_eeprom_del_parsed, /* function to call */
+ .data = NULL, /* 2nd arg of func */
+ .help_str = help_eeprom_del,
+ .tokens = { /* token list, NULL terminated */
+ (PGM_P)&cmd_eeprom_del_cmd,
+ (PGM_P)&cmd_eeprom_del_action,
+ (PGM_P)&cmd_eeprom_del_num,
+ NULL,
+ },
+};
+
+/* ************* */
+
+struct cmd_eeprom_add_result {
+ fixed_string_t cmd;
+ fixed_string_t action;
+ uint8_t n;
+};
+
+static void cmd_eeprom_add_parsed(void *parsed_result,
+ void *data)
+{
+ struct cmd_eeprom_add_result *res = parsed_result;
+ struct rdline rdl;
+ const char *buffer;
+ int8_t ret;
+ int16_t c;
+
+ rdline_init(&rdl, cmdline_write_char, NULL, NULL);
+ rdline_newline(&rdl, "> ");
+
+ /* XXX bad: we should not block as we do not serve callout */
+ while (1) {
+ c = cmdline_dev_recv(NULL);
+ if (c < 0)
+ continue;
+
+ ret = rdline_char_in(&rdl, c);
+ if (ret == -2) {
+ printf_P(PSTR("abort\n"));
+ return;
+ }
+ if (ret == 1)
+ break;
+ }
+
+ buffer = rdline_get_buffer(&rdl);
+ if (data == NULL)
+ eeprom_insert_cmd_before(buffer, res->n);
+ else
+ eeprom_append_cmd(buffer);
+ eeprom_dump_cmds();
+}
+
+const char PROGMEM str_eeprom_add_eeprom[] = "eeprom";
+const parse_token_string_t PROGMEM cmd_eeprom_add_cmd =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, cmd,
+ str_eeprom_add_eeprom);
+const char PROGMEM str_eeprom_add_add[] = "add";
+const parse_token_string_t PROGMEM cmd_eeprom_add_action =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, action,
+ str_eeprom_add_add);
+const parse_token_num_t PROGMEM cmd_eeprom_add_num =
+ TOKEN_NUM_INITIALIZER(struct cmd_eeprom_add_result, n,
+ UINT8);
+
+const char PROGMEM help_eeprom_add[] = "insert an eeprom init command";
+const parse_inst_t PROGMEM cmd_eeprom_add = {
+ .f = cmd_eeprom_add_parsed, /* function to call */
+ .data = NULL, /* 2nd arg of func */
+ .help_str = help_eeprom_add,
+ .tokens = { /* token list, NULL terminated */
+ (PGM_P)&cmd_eeprom_add_cmd,
+ (PGM_P)&cmd_eeprom_add_action,
+ (PGM_P)&cmd_eeprom_add_num,
+ NULL,
+ },
+};
+
+const char PROGMEM help_eeprom_add2[] = "append an eeprom init command";
+const parse_inst_t PROGMEM cmd_eeprom_add2 = {
+ .f = cmd_eeprom_add_parsed, /* function to call */
+ .data = (void *)1, /* 2nd arg of func */
+ .help_str = help_eeprom_add2,
+ .tokens = { /* token list, NULL terminated */
+ (PGM_P)&cmd_eeprom_add_cmd,
+ (PGM_P)&cmd_eeprom_add_action,
+ NULL,
+ },
+};
+
+/* ************* */
+
+struct cmd_eeprom_list_result {
+ fixed_string_t cmd;
+ fixed_string_t action;
+};
+
+static void cmd_eeprom_list_parsed(void *parsed_result,
+ void *data)
+{
+ (void)parsed_result;
+ (void)data;
+ eeprom_dump_cmds();
+}
+
+const char PROGMEM str_eeprom_list_eeprom[] = "eeprom";
+const parse_token_string_t PROGMEM cmd_eeprom_list_cmd =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, cmd,
+ str_eeprom_list_eeprom);
+const char PROGMEM str_eeprom_list_list[] = "list";
+const parse_token_string_t PROGMEM cmd_eeprom_list_action =
+ TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, action,
+ str_eeprom_list_list);
+
+const char PROGMEM help_eeprom_list[] = "list all eeprom init commands";
+const parse_inst_t PROGMEM cmd_eeprom_list = {
+ .f = cmd_eeprom_list_parsed, /* function to call */
+ .data = NULL, /* 2nd arg of func */
+ .help_str = help_eeprom_list,
+ .tokens = { /* token list, NULL terminated */
+ (PGM_P)&cmd_eeprom_list_cmd,
+ (PGM_P)&cmd_eeprom_list_action,
+ NULL,
+ },
+};
+
+
+/* ************* */
+
/* in progmem */
const parse_ctx_t PROGMEM main_ctx[] = {
&cmd_servo_bypassppm,
&cmd_servo_show,
&cmd_test_spi,
+ &cmd_test_eeprom_config,
+ &cmd_eeprom_del,
+ &cmd_eeprom_add,
+ &cmd_eeprom_add2,
+ &cmd_eeprom_list,
NULL,
};
--- /dev/null
+/*
+ * Copyright 2013 Olivier Matz <zer0@droids-corp.org>
+ *
+ * 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 <string.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#include <aversive/pgmspace.h>
+#include <aversive/eeprom.h>
+#include <aversive/error.h>
+
+#include <uart.h>
+
+
+#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;
+}
--- /dev/null
+/*
+ * Copyright 2013 Olivier Matz <zer0@droids-corp.org>
+ *
+ * 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
+ *
+ */
+
+#ifndef _EEPROM_CONFIG_H_
+#define _EEPROM_CONFIG_H_
+
+#define EEPROM_CONFIG_MAGIC 0x666bea57
+#define EEPROM_CMD_SIZE 64
+#define EEPROM_N_CMD_MAX 16
+
+struct eeprom_cmd {
+ char buf[EEPROM_CMD_SIZE];
+};
+
+struct eeprom_config
+{
+ uint32_t magic;
+ uint8_t ncmds;
+ struct eeprom_cmd cmds[EEPROM_N_CMD_MAX];
+};
+
+int8_t eeprom_load_config(void);
+uint8_t eeprom_get_ncmds(void);
+void eeprom_set_ncmds(uint8_t ncmds);
+void eeprom_get_cmd(struct eeprom_cmd *cmd, uint8_t n);
+void eeprom_set_cmd(struct eeprom_cmd *cmd, uint8_t n);
+void eeprom_dump_cmds(void);
+int8_t eeprom_insert_cmd_before(const char *str, uint8_t n);
+int8_t eeprom_append_cmd(const char *str);
+int8_t eeprom_delete_cmd(uint8_t n);
+
+#endif
#include <rdline.h>
#include <timer.h>
+#include "eeprom_config.h"
#include "beep.h"
#include "main.h"
val += 512;
spi_servo_set(0, val);
break;
- }
+ }
case RC_PROTO_TYPE_RANGE: {
struct rc_proto_range *rcr =
(struct rc_proto_range *) recvframe->data;
fprintf(stderr, "cannot register default channel\n");
return -1;
}
-
sei();
+ eeprom_load_config();
xbee_mainloop();
return 0;
}