From f833faabec7718e112d12203342804be0e19c604 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 17 May 2009 22:20:41 +0200 Subject: * digital/io, host/simu: - added contacts. --- digital/io/src/simu.host.c | 9 +++++++++ digital/io/tools/io/mex.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) (limited to 'digital/io') diff --git a/digital/io/src/simu.host.c b/digital/io/src/simu.host.c index f13f84a5..e3c9fc69 100644 --- a/digital/io/src/simu.host.c +++ b/digital/io/src/simu.host.c @@ -44,6 +44,7 @@ enum MSG_SIMU_IO_SHARPS = 0xb3, MSG_SIMU_IO_PATH = 0xb4, MSG_SIMU_IO_PWM = 0xb5, + MSG_SIMU_IO_CONTACT = 0xb6, }; /** Requested servo position. */ @@ -72,11 +73,19 @@ uint8_t PORTB; /** Contact registers. */ uint8_t PORTC, PINC; +void +handle_contact (void *user, mex_msg_t *msg) +{ + mex_msg_pop (msg, "B", &PINC); +} + /** Initialise simulation. */ void simu_init (void) { mex_node_connect (); + mex_node_register (MSG_SIMU_IO_CONTACT, handle_contact, 0); + PINC = 0x3f; simu_servo_update_cpt = 1; simu_switch_update_cpt = 1; simu_sharps_update_cpt = 1; diff --git a/digital/io/tools/io/mex.py b/digital/io/tools/io/mex.py index 6f762ccd..d3c814ef 100644 --- a/digital/io/tools/io/mex.py +++ b/digital/io/tools/io/mex.py @@ -24,6 +24,7 @@ """Mex interface to io.""" from utils.observable import Observable +import simu.mex.msg ID_JACK = 0xb0 ID_COLOR = 0xb1 @@ -31,6 +32,7 @@ ID_SERVO = 0xb2 ID_ADC = 0xb3 ID_PATH = 0xb4 ID_PWM = 0xb5 +ID_CONTACT = 0xb6 SERVO_NB = 6 SERVO_VALUE_MAX = 255 @@ -40,6 +42,9 @@ ADC_NB = 6 PWM_NB = 1 PWM_VALUE_MAX = 1024 +CONTACT_NB = 2 +CONTACT_INIT = 0x3f + class Mex: """Handle communications with simulated io.""" @@ -157,6 +162,42 @@ class Mex: 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): + self.node = node + self.contacts = CONTACT_INIT + + 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 (ID_CONTACT) + m.push ('B', self.contacts) + self.node.send (m) + def __init__ (self, node): self.jack = self.Switch (node, ID_JACK) self.color_switch = self.Switch (node, ID_COLOR) @@ -167,4 +208,7 @@ class Mex: self.path = self.Path (node) self.pwm = tuple (self.PWM () for i in range (0, PWM_NB)) self.__adc_pwm = self.PWM.Pack (node, self.pwm) + self.__contact_pack = self.Contact.Pack (node) + self.contact = tuple (self.Contact (self.__contact_pack, i) + for i in range (CONTACT_NB)) -- cgit v1.2.3