/* serial.c */ /* APBTasserv - asservissement Robot 2004 {{{ * * Copyright (C) 2003 Nicolas Schodet * * Robot APB Team/Efrei 2004. * Web: http://assos.efrei.fr/robot/ * Mail: robot AT efrei DOT fr * * 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 "serial.h" #include "motor.h" /* Macros de lecture du tampon. */ #define HEXDIGIT(a) ((a) <= 9 ? (a) + '0' : (a) + ('a' - 10)) #define SERIAL2INT(a, b) (HEX (a) << 4 | HEX (b)) #define CRLF "\r" /* Tampon de reception. */ char serial_recv_buf[SERIAL_RECV_BUF_LEN]; int serial_recv_buf_n; short serial_recv_full, serial_recv_working; /* Tampon d'emmission. */ #define SERIAL_SEND_BUF_LEN 20 char serial_send_buf[SERIAL_SEND_BUF_LEN]; int serial_send_buf_n; short serial_send_full, serial_send_working; /* Initialise la reception serie. */ void serial_init (void) { serial_recv_buf_n = 0; serial_recv_full = 0; serial_recv_working = 0; serial_send_buf_n = 0; serial_send_full = 0; serial_send_working = 0; printf ("!z" CRLF); } /* Recois des caractères. */ #int_RDA serial_recv () { char c; if (serial_recv_full) return; c = getc (); if (c == '\n' || c == '\r') { if (serial_recv_buf_n) serial_recv_full = 1; } else if (c == '!' || serial_recv_buf_n >= SERIAL_RECV_BUF_LEN) serial_recv_buf_n = 0; else serial_recv_buf[serial_recv_buf_n++] = c; } /* Traite la commande recue. */ void serial_parse (void) { restart_wdt (); /* Si le tampon n'est pas plein, bye, bye. */ if (!serial_recv_full || serial_recv_working) return; serial_recv_working = 1; /* Vérifie la taille du tampon. */ if (serial_recv_buf_n) { /* Est-ce une commande moteur. */ if (!motor_parse ()) { switch (serial_recv_com ()) { case 'z': reset_cpu (); break; default: serial_send_error ('?'); break; } } } serial_recv_buf_n = 0; serial_recv_working = 0; serial_recv_full = 0; } /* Envoie un code d'erreur. */ void serial_send_error (char c) { printf ("!e%c" CRLF, c); } /* Envoie un code ok. */ void serial_send_ok (char c) { printf ("!o%c" CRLF, c); } /* Envoie un rapport d'un moteur. */ void serial_send_motor_stat (char side, unsigned int vacc, unsigned long &e, unsigned long &pwm) { printf ("!m%c", side); serial_send_int (vacc); putc (','); serial_send_long (e); putc (','); serial_send_long (pwm); puts (CRLF); } /* Envoie un long. */ void serial_send_long (unsigned long &value) { putc (HEXDIGIT ((make8 (value, 1) >> 4) & 0xf)); putc (HEXDIGIT (make8 (value, 1) & 0xf)); putc (HEXDIGIT ((make8 (value, 0) >> 4) & 0xf)); putc (HEXDIGIT (make8 (value, 0) & 0xf)); } /* Envoie un int. */ void serial_send_int (unsigned int &value) { putc (HEXDIGIT ((value >> 4) & 0xf)); putc (HEXDIGIT (value & 0xf)); }