beep when GPS ready
[protos/xbee-avr.git] / cmdline.c
index 60dc9a6..568dcad 100644 (file)
--- a/cmdline.c
+++ b/cmdline.c
 #include <parse.h>
 #include <rdline.h>
 #include <uart.h>
-#include <clock_time.h>
 
 #include "callout.h"
 #include "main.h"
 #include "cmdline.h"
 
+#define FLUSH_LOGS_MS 1000 /* every second */
+#define LOG_PER_SEC_MAX 10
 
-#ifdef USE_USB
-#include "DualVirtualSerial.h"
-#endif
-
+extern const parse_ctx_t PROGMEM main_ctx[];
 
-extern parse_pgm_ctx_t main_ctx[];
+static struct callout flush_log_timer;
+static uint8_t log_count;
 
 int cmdline_dev_send(char c, FILE* f)
 {
-#ifdef USE_USB
-       CDC_Device_SendByte(&VirtualSerial1_CDC_Interface, (uint8_t)c);
-#else
+       (void)f;
        uart_send(CMDLINE_UART, c);
-#endif
        return 0;
 }
 
 int cmdline_dev_recv(FILE* f)
 {
        int16_t c;
-#ifdef USE_USB
-       /* non-blocking ! */
-       c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
-#else
+
+       (void)f;
        c = uart_recv_nowait(CMDLINE_UART);
-#endif
        if (c < 0)
                return _FDEV_EOF;
 
@@ -72,11 +65,8 @@ int cmdline_dev_recv(FILE* f)
 
 int xbee_dev_send(char c, FILE* f)
 {
-#ifdef USE_USB
-       CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)c);
-#else
+       (void)f;
        uart_send(XBEE_UART, c);
-#endif
        return 0;
 }
 
@@ -84,24 +74,21 @@ int xbee_dev_recv(FILE* f)
 {
        int16_t c;
 
-#ifdef USE_USB
-       /* non-blocking ! */
-       c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
-#else
+       (void)f;
        c = uart_recv_nowait(XBEE_UART);
-#endif
        if (c < 0)
                return _FDEV_EOF;
 
        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;
 
-       ret = parse(main_ctx, buf);
+       (void)size;
+       ret = parse(ctx, buf);
        if (ret == PARSE_AMBIGUOUS)
                printf_P(PSTR("Ambiguous command\r\n"));
        else if (ret == PARSE_NOMATCH)
@@ -114,23 +101,17 @@ static int8_t
 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
                int16_t *state)
 {
-       return complete(main_ctx, buf, state, dstbuf, dstsize);
+       PGM_P ctx = (PGM_P)main_ctx;
+       return complete(ctx, buf, state, dstbuf, dstsize);
 }
 
 
-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);
-       snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
-}
-
-
 /* sending "pop" on cmdline uart resets the robot */
 void emergency(char c)
 {
@@ -142,21 +123,28 @@ void emergency(char c)
                i++;
        else if ( !(i == 1 && c == 'p') )
                i = 0;
-       if (i == 3)
-               bootloader();
+       if (i == 3) {
+               //bootloader();
+               reset();
+       }
 }
 
-/* log function, add a command to configure
- * it dynamically */
+/* log function, configured dynamically */
 void mylog(struct error * e, ...)
 {
-       va_list ap;
 #ifndef HOST_VERSION
        u16 stream_flags = stdout->flags;
 #endif
-       uint8_t i;
-       time_h tv;
+       va_list ap;
+       uint8_t i, flags;
+       uint32_t ms;
+       uint8_t prio;
+
+       /* too many logs */
+       if (log_count >= LOG_PER_SEC_MAX)
+               return;
 
+       /* higher log value means lower criticity */
        if (e->severity > ERROR_SEVERITY_ERROR) {
                if (xbeeboard.log_level < e->severity)
                        return;
@@ -168,18 +156,42 @@ void mylog(struct error * e, ...)
                        return;
        }
 
-       va_start(ap, e);
-       tv = time_get_time();
-       printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
+       /* get time */
+       IRQ_LOCK(flags);
+       ms = global_ms;
+       IRQ_UNLOCK(flags);
+
+       /* prevent flush log to occur */
+       prio = callout_mgr_set_prio(&xbeeboard.intr_cm,
+               LOW_PRIO);
 
+       /* display the log */
+       va_start(ap, e);
+       printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL));
        vfprintf_P(stdout, e->text, ap);
        printf_P(PSTR("\r\n"));
        va_end(ap);
+
 #ifndef HOST_VERSION
        stdout->flags = stream_flags;
 #endif
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 }
 
+static void flush_logs_cb(struct callout_mgr *cm, struct callout *tim,
+       void *arg)
+{
+       (void)cm;
+       (void)tim;
+       (void)arg;
+
+       if (log_count == LOG_PER_SEC_MAX)
+               printf_P("some logs were dropped\n");
+       callout_reschedule(&xbeeboard.intr_cm, &flush_log_timer,
+               FLUSH_LOGS_MS);
+}
+
+
 int cmdline_poll(void)
 {
        const char *history, *buffer;
@@ -210,3 +222,15 @@ int cmdline_poll(void)
        return 0;
 }
 
+void cmdline_init(void)
+{
+       /* init command line */
+       rdline_init(&xbeeboard.rdl, cmdline_write_char, cmdline_valid_buffer,
+               complete_buffer);
+       snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt),
+               PSTR("mainboard > "));
+
+       /* load a timer for flushing logs */
+       callout_init(&flush_log_timer, flush_logs_cb, NULL, LOW_PRIO);
+       callout_schedule(&xbeeboard.intr_cm, &flush_log_timer, FLUSH_LOGS_MS);
+}