From 2686ad2bb0ad85f1a44a4611c1c3937ed6869e87 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Tue, 12 Mar 2013 21:02:23 +0100 Subject: host/simu/link: add generic GPIO MEX link --- host/simu/link/__init__.py | 0 host/simu/link/mex_gpio.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 host/simu/link/__init__.py create mode 100644 host/simu/link/mex_gpio.py diff --git a/host/simu/link/__init__.py b/host/simu/link/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/host/simu/link/mex_gpio.py b/host/simu/link/mex_gpio.py new file mode 100644 index 00000000..e689996f --- /dev/null +++ b/host/simu/link/mex_gpio.py @@ -0,0 +1,77 @@ +# simu - Robot simulation. {{{ +# +# Copyright (C) 2013 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. +# +# }}} +"""MEX GPIO link.""" +from utils.observable import Observable +import mex.msg + +class MexGpio (Observable): + """General purpose Input/Output. + + - output_state: state received from simulated board. + - input_state: state to send to simulated board. + - direction: 'input' or 'output', from simulated board. + + """ + + def __init__ (self, pack, name): + Observable.__init__ (self) + self.pack = pack + assert name not in self.pack.gpios + self.pack.gpios[name] = self + self.name = name + self.direction = None + self.output_state = None + self.input_state = None + self.register (self.__notified) + + def __notified (self): + self.pack.send_input (self) + + class Pack: + """Centralise GPIO handling.""" + + def __init__ (self, node, instance): + self.node = node + self.input_mtype = node.reserve (instance + ':gpio_in') + output_mtype = node.register (instance + ':gpio_out', + self.__handle_output) + self.gpios = { } + + def send_input (self, gpio): + state = gpio.input_state + if state is None: + state = 1 + m = mex.msg.Msg (self.input_mtype) + namelen = len (gpio.name) + m.push ('B%ds' % namelen, state, gpio.name) + self.node.send (m) + + def __handle_output (self, msg): + namelen = len (msg) - 2 + direction, output, name = msg.pop ('BB%ds' % namelen) + gpio = self.gpios[name] + gpio.direction = ('input', 'output')[direction] + gpio.output_state = output != 0 + gpio.notify () + -- cgit v1.2.3