From 554c4baf249f68bb8f716af97cbf23daad73ef5c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 6 May 2011 23:44:36 +0200 Subject: digital/io-hub: add contacts --- digital/io-hub/src/common/contact.avr.c | 98 +++++++++++++++++++++++++++ digital/io-hub/src/common/contact.h | 55 +++++++++++++++ digital/io-hub/src/common/contact.host.c | 98 +++++++++++++++++++++++++++ digital/io-hub/src/robospierre/Makefile | 1 + digital/io-hub/src/robospierre/contact_defs.h | 37 ++++++++++ digital/io-hub/src/robospierre/main.c | 15 ++++ digital/io-hub/src/robospierre/simu.host.c | 3 + digital/io-hub/src/robospierre/simu.host.h | 2 + 8 files changed, 309 insertions(+) create mode 100644 digital/io-hub/src/common/contact.avr.c create mode 100644 digital/io-hub/src/common/contact.h create mode 100644 digital/io-hub/src/common/contact.host.c create mode 100644 digital/io-hub/src/robospierre/contact_defs.h (limited to 'digital') diff --git a/digital/io-hub/src/common/contact.avr.c b/digital/io-hub/src/common/contact.avr.c new file mode 100644 index 00000000..9f6869b4 --- /dev/null +++ b/digital/io-hub/src/common/contact.avr.c @@ -0,0 +1,98 @@ +/* contact.avr.c */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT 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 "common.h" +#include "contact.h" + +/** Filter depth. */ +#define CONTACT_JACK_FILTER_DEPTH 50 + +/** Contact context. */ +struct contact_t +{ + /** Jack current state. */ + uint8_t jack_state; + /** Jack filter, number of updates before state can change. */ + uint8_t jack_filter; +}; + +/** Global context. */ +struct contact_t contact_global; +#define ctx contact_global + +void +contact_init (void) +{ + /* Pull-up. */ +#define CONTACT(io) IO_SET_ (io); + CONTACT (CONTACT_COLOR); + CONTACT (CONTACT_JACK); + CONTACT_LIST +#undef CONTACT + /* Default to jack out. */ + ctx.jack_state = 1; +} + +void +contact_update (void) +{ + uint8_t jack_now = IO_GET (CONTACT_JACK); + /* If state is unchanged, reset filter. */ + if (jack_now == ctx.jack_state) + ctx.jack_filter = 0; + /* If state changed, should be stable for CONTACT_JACK_FILTER_DEPTH before + * it goes through the filter. */ + else if (ctx.jack_filter++ == CONTACT_JACK_FILTER_DEPTH) + { + ctx.jack_state = jack_now; + ctx.jack_filter = 0; + } +} + +enum team_color_e +contact_get_color (void) +{ + return !IO_GET (CONTACT_COLOR) ? TEAM_COLOR_LEFT : TEAM_COLOR_RIGHT; +} + +uint8_t +contact_get_jack (void) +{ + return ctx.jack_state; +} + +uint32_t +contact_all (void) +{ + uint32_t contacts = 0, bit = 1; +#define CONTACT(io) do { \ + contacts |= (IO_GET_ (io) ? bit : 0); \ + bit <<= 1; \ +} while (0); + CONTACT (CONTACT_COLOR); + CONTACT (CONTACT_JACK); + CONTACT_LIST +#undef CONTACT + return contacts; +} diff --git a/digital/io-hub/src/common/contact.h b/digital/io-hub/src/common/contact.h new file mode 100644 index 00000000..fce06f84 --- /dev/null +++ b/digital/io-hub/src/common/contact.h @@ -0,0 +1,55 @@ +#ifndef contact_h +#define contact_h +/* contact.h */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT 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 "simu.host.h" +#include "defs.h" +#include "io.h" + +#include "contact_defs.h" + +/** To get a state of a generic contact, use: IO_GET (CONTACT_EXAMPLE). */ + +/** Initialize contact module. */ +void +contact_init (void); + +/** Update contact module. This handle debouncing. */ +void +contact_update (void); + +/** Get color switch state, unfiltered. */ +enum team_color_e +contact_get_color (void); + +/** Get filtered jack state. */ +uint8_t +contact_get_jack (void); + +/** Return state of all contact, unfiltered. */ +uint32_t +contact_all (void); + +#endif /* contact_h */ diff --git a/digital/io-hub/src/common/contact.host.c b/digital/io-hub/src/common/contact.host.c new file mode 100644 index 00000000..5e2309c4 --- /dev/null +++ b/digital/io-hub/src/common/contact.host.c @@ -0,0 +1,98 @@ +/* contact.host.c */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT 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 "common.h" +#include "contact.h" + +#include "modules/host/host.h" +#include "modules/host/mex.h" + +/** Contact context. */ +struct contact_t +{ + /** All contact state. */ + uint32_t all; + /** Jack current state. */ + uint8_t jack_state; + /** Color current state. */ + enum team_color_e color_state; +}; + +/** Global context. */ +struct contact_t contact_global; +#define ctx contact_global + +static void +contact_handle (void *user, mex_msg_t *msg) +{ + uint32_t contacts; + mex_msg_pop (msg, "L", &contacts); + ctx.all = contacts; + ctx.color_state = !(contacts & 1) ? TEAM_COLOR_LEFT : TEAM_COLOR_RIGHT; + ctx.jack_state = (contacts & 2) ? 1 : 0; + contacts >>= 2; +#define CONTACT(io) do { \ + if (contacts & 1) \ + IO_PIN_ (io) |= IO_BV_ (io); \ + else \ + IO_PIN_ (io) &= ~IO_BV_ (io); \ + contacts >>= 1; \ +} while (0); + CONTACT_LIST +#undef CONTACT +} + +void +contact_init (void) +{ + const char *mex_instance = host_get_instance ("io-hub0", 0); + uint8_t mtype = mex_node_reservef ("%s:contact", mex_instance); + mex_node_register (mtype, contact_handle, 0); + /* Default to jack out. */ + ctx.jack_state = 1; +} + +void +contact_update (void) +{ + /* Nothing to do. */ +} + +enum team_color_e +contact_get_color (void) +{ + return ctx.color_state; +} + +uint8_t +contact_get_jack (void) +{ + return ctx.jack_state; +} + +uint32_t +contact_all (void) +{ + return ctx.all; +} diff --git a/digital/io-hub/src/robospierre/Makefile b/digital/io-hub/src/robospierre/Makefile index 9d0035fe..a95d7586 100644 --- a/digital/io-hub/src/robospierre/Makefile +++ b/digital/io-hub/src/robospierre/Makefile @@ -5,6 +5,7 @@ PROGS = io_hub # Sources to compile. io_hub_SOURCES = main.c \ pwm.avr.c pwm.host.c \ + contact.avr.c contact.host.c \ twi_master.c asserv.c mimot.c \ chrono.c timer.avr.c simu.host.c # Modules needed for IO. diff --git a/digital/io-hub/src/robospierre/contact_defs.h b/digital/io-hub/src/robospierre/contact_defs.h new file mode 100644 index 00000000..55b34354 --- /dev/null +++ b/digital/io-hub/src/robospierre/contact_defs.h @@ -0,0 +1,37 @@ +#ifndef contact_defs_h +#define contact_defs_h +/* contact_defs.h */ +/* robospierre - Eurobot 2011 AI. {{{ + * + * Copyright (C) 2011 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT 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. + * + * }}} */ + +#define CONTACT_COLOR A, 7 +#define CONTACT_JACK F, 7 +#define CONTACT_EX1 E, 0 +#define CONTACT_EX2 E, 1 + +#define CONTACT_LIST \ + CONTACT (CONTACT_EX1) \ + CONTACT (CONTACT_EX2) + +#endif /* contact_defs_h */ diff --git a/digital/io-hub/src/robospierre/main.c b/digital/io-hub/src/robospierre/main.c index 12a35054..bbc9bdb9 100644 --- a/digital/io-hub/src/robospierre/main.c +++ b/digital/io-hub/src/robospierre/main.c @@ -37,6 +37,7 @@ #include "twi_master.h" #include "pwm.h" +#include "contact.h" #include "io.h" @@ -46,6 +47,9 @@ enum team_color_e team_color; /** Asserv stats counters. */ static uint8_t main_stats_asserv_, main_stats_asserv_cpt_; +/** Contact stats counters. */ +static uint8_t main_stats_contact_, main_stats_contact_cpt_; + /** Main initialisation. */ static void main_init (void) @@ -62,6 +66,7 @@ main_init (void) twi_master_init (); /* IO modules. */ pwm_init (); + contact_init (); /* Initialization done. */ proto_send0 ('z'); } @@ -94,6 +99,7 @@ main_loop (void) proto_accept (uart0_getc ()); /* Update IO modules. */ pwm_update (); + contact_update (); /* Only manage events if slaves are synchronised. */ if (twi_master_sync ()) main_event_to_fsm (); @@ -108,6 +114,11 @@ main_loop (void) /* Reset stats counter */ main_stats_asserv_cpt_ = main_stats_asserv_; } + if (main_stats_contact_ && !--main_stats_contact_cpt_) + { + proto_send1d ('P', contact_all ()); + main_stats_contact_cpt_ = main_stats_contact_; + } } } @@ -144,6 +155,10 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) /* Position stats. */ main_stats_asserv_ = main_stats_asserv_cpt_ = args[0]; break; + case c ('P', 1): + /* Contact stats. */ + main_stats_contact_ = main_stats_contact_cpt_ = args[0]; + break; default: /* Unknown commands */ proto_send0 ('?'); diff --git a/digital/io-hub/src/robospierre/simu.host.c b/digital/io-hub/src/robospierre/simu.host.c index 76e48edd..8f4019aa 100644 --- a/digital/io-hub/src/robospierre/simu.host.c +++ b/digital/io-hub/src/robospierre/simu.host.c @@ -30,6 +30,9 @@ #include "modules/host/mex.h" #include "io.h" +/** AVR registers. */ +uint8_t PINE; + /** Initialise simulation. */ void simu_init (void) diff --git a/digital/io-hub/src/robospierre/simu.host.h b/digital/io-hub/src/robospierre/simu.host.h index 76326a31..4b461e86 100644 --- a/digital/io-hub/src/robospierre/simu.host.h +++ b/digital/io-hub/src/robospierre/simu.host.h @@ -27,6 +27,8 @@ #ifdef HOST +extern uint8_t PINE; + #else /* !defined (HOST) */ #endif /* !defined (HOST) */ -- cgit v1.2.3