summaryrefslogtreecommitdiff
path: root/host/simu
diff options
context:
space:
mode:
authorNicolas Schodet2013-03-12 21:02:23 +0100
committerNicolas Schodet2013-03-12 21:04:51 +0100
commit2686ad2bb0ad85f1a44a4611c1c3937ed6869e87 (patch)
tree7980e7d1d1fccd76fca8943409e8ba0d226b2447 /host/simu
parent37bb283f17a455014cafd2f0b383ae72443b051a (diff)
host/simu/link: add generic GPIO MEX link
Diffstat (limited to 'host/simu')
-rw-r--r--host/simu/link/__init__.py0
-rw-r--r--host/simu/link/mex_gpio.py77
2 files changed, 77 insertions, 0 deletions
diff --git a/host/simu/link/__init__.py b/host/simu/link/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/host/simu/link/__init__.py
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 ()
+