From 2cd5319ff81c25e4ee773a00862d26666ef2ced6 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 7 May 2011 00:31:15 +0200 Subject: digital/io-hub/tools: add io-hub python libs --- digital/io-hub/tools/io_hub/__init__.py | 2 + digital/io-hub/tools/io_hub/init.py | 4 ++ digital/io-hub/tools/io_hub/io_hub.py | 54 +++++++++++++++ digital/io-hub/tools/io_hub/mex.py | 119 ++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 digital/io-hub/tools/io_hub/__init__.py create mode 100644 digital/io-hub/tools/io_hub/init.py create mode 100644 digital/io-hub/tools/io_hub/io_hub.py create mode 100644 digital/io-hub/tools/io_hub/mex.py (limited to 'digital/io-hub/tools/io_hub') diff --git a/digital/io-hub/tools/io_hub/__init__.py b/digital/io-hub/tools/io_hub/__init__.py new file mode 100644 index 00000000..e84cb000 --- /dev/null +++ b/digital/io-hub/tools/io_hub/__init__.py @@ -0,0 +1,2 @@ +from io_hub import Proto +from mex import Mex diff --git a/digital/io-hub/tools/io_hub/init.py b/digital/io-hub/tools/io_hub/init.py new file mode 100644 index 00000000..aa5aa10c --- /dev/null +++ b/digital/io-hub/tools/io_hub/init.py @@ -0,0 +1,4 @@ +"""Default parameters for io-hub.""" +host = dict ( + ) +target = host diff --git a/digital/io-hub/tools/io_hub/io_hub.py b/digital/io-hub/tools/io_hub/io_hub.py new file mode 100644 index 00000000..36e5e4df --- /dev/null +++ b/digital/io-hub/tools/io_hub/io_hub.py @@ -0,0 +1,54 @@ +# io-hub - Modular Input/Output. {{{ +# +# 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. +# +# }}} +import proto, time + +class Proto: + + def __init__ (self, file, time = time.time, **param): + self.proto = proto.Proto (file, time, 0.1) + self.async = False + self.param = param + self.send_param () + + def send_param (self): + pass + + def reset (self): + self.proto.send ('z') + self.proto.send ('z') + + def pwm_set (self, index, value): + self.proto.send ('w', 'Bh', index, value) + + def pwm_set_timed (self, index, value, time, rest_value): + self.proto.send ('w', 'BhHh', index, value, time, rest_value) + + def close (self): + self.reset () + self.proto.wait (lambda: True) + self.proto.file.close () + + def fileno (self): + return self.proto.fileno () + diff --git a/digital/io-hub/tools/io_hub/mex.py b/digital/io-hub/tools/io_hub/mex.py new file mode 100644 index 00000000..7c8c0012 --- /dev/null +++ b/digital/io-hub/tools/io_hub/mex.py @@ -0,0 +1,119 @@ +# io-hub - Modular Input/Output. {{{ +# +# 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. +# +# }}} +"""Mex interface to io-hub.""" + +from utils.observable import Observable +import simu.mex.msg + +ADC_NB = 8 + +PWM_NB = 6 +PWM_VALUE_MAX = 1024 + +CONTACT_NB = 4 +CONTACT_INIT = 0xffffffff + +class Mex: + """Handle communications with simulated io-hub.""" + + class ADC (Observable): + """Analog to digital input." + + - value: volts. + + """ + + def __init__ (self): + Observable.__init__ (self) + self.value = None + + class PWM (Observable): + """PWM output. + + - value: current PWM value (-1 ... +1). + + """ + + def __init__ (self): + Observable.__init__ (self) + self.value = None + + class Pack: + """Handle reception of several PWM for one message.""" + + def __init__ (self, node, instance, list): + self.__list = list + node.register (instance + ':pwm', self.__handle) + + def __handle (self, msg): + values = msg.pop ('%dh' % len (self.__list)) + for pwm, value in zip (self.__list, values): + pwm.value = float (value) / PWM_VALUE_MAX + pwm.notify () + + class Contact (Observable): + """Contact input. + + - state: True (open) or False (closed). + + """ + + def __init__ (self, pack, index): + Observable.__init__ (self) + self.pack = pack + self.index = index + self.state = None + self.register (self.__notified) + + def __notified (self): + self.pack.set (self.index, self.state) + + class Pack: + """Handle emission of several contacts for one message.""" + + def __init__ (self, node, instance): + self.node = node + self.contacts = CONTACT_INIT + self.mtype = node.reserve (instance + ':contact') + + def set (self, index, state): + if state is None or state: + self.contacts |= 1 << index + else: + self.contacts &= ~(1 << index) + self.__send () + + def __send (self): + m = simu.mex.msg.Msg (self.mtype) + m.push ('L', self.contacts) + self.node.send (m) + + def __init__ (self, node, instance = 'io-hub0'): + self.adc = tuple (self.ADC () for i in range (0, ADC_NB)) + self.pwm = tuple (self.PWM () for i in range (0, PWM_NB)) + self.__adc_pwm = self.PWM.Pack (node, instance, self.pwm) + self.__contact_pack = self.Contact.Pack (node, instance) + self.contact = tuple (self.Contact (self.__contact_pack, i) + for i in range (CONTACT_NB)) + -- cgit v1.2.3