summaryrefslogtreecommitdiff
path: root/digital/io/tools/io/mex.py
blob: bbe2991531aef6f767d71811dba6478144f11726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
#
# Copyright (C) 2009 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."""

from utils.observable import Observable

ID_JACK = 0xb0
ID_COLOR = 0xb1
ID_SERVO = 0xb2
ID_ADC = 0xb3
ID_PATH = 0xb4

SERVO_NB = 6
SERVO_VALUE_MAX = 255

ADC_NB = 5

class Mex:
    """Handle communications with simulated io."""

    class Switch (Observable):
        """Switch input.

        - state: True (open) or False (closed).

        """

        def __init__ (self, node, id):
            Observable.__init__ (self)
            self.__node = node
            self.state = None
            node.register (id, self.__handle)

        def __handle (self, msg):
            assert self.state is not None
            m = msg
            m.push ('B', self.state)
            self.__node.response (m)

    class Servo (Observable):
        """Servo motor.

        - value: current servo position (hardware unit).

        """

        def __init__ (self):
            Observable.__init__ (self)
            self.value = None

        class Pack:
            """Handle reception of several Servo for one message."""

            def __init__ (self, node, list):
                self.__list = list
                node.register (ID_SERVO, self.__handle)

            def __handle (self, msg):
                values = msg.pop ('%dB' % len (self.__list))
                for servo, value in zip (self.__list, values):
                    servo.value = float (value) / SERVO_VALUE_MAX
                    servo.notify ()

    class ADC (Observable):
        """Analog to digital input."

        - value: hardware unit.

        """

        def __init__ (self):
            Observable.__init__ (self)
            self.value = None

        class Pack:
            """Handle transmission of several ADC for one message."""

            def __init__ (self, node, list):
                self.__node = node
                self.__list = list
                node.register (ID_ADC, self.__handle)

            def __handle (self, msg):
                m = msg
                for adc in self.__list:
                    assert adc.value is not None
                    m.push ('H', adc.value)
                self.__node.response (m)

    class Path (Observable):
        """Path finding algorithm report.

        - path: sequence of (x, y) coordinates (millimeters).

        """

        def __init__ (self, node):
            Observable.__init__ (self)
            self.path = [ ]
            node.register (ID_PATH, self.__handle)

        def __handle (self, msg):
            self.path = [ ]
            while len (msg) >= 4:
                self.path.append (msg.pop ('hh'))
            self.notify ()

    def __init__ (self, node):
        self.jack = self.Switch (node, ID_JACK)
        self.color_switch = self.Switch (node, ID_COLOR)
        self.servo = tuple (self.Servo () for i in range (0, SERVO_NB))
        self.__servo_pack = self.Servo.Pack (node, self.servo)
        self.adc = tuple (self.ADC () for i in range (0, ADC_NB))
        self.__adc_pack = self.ADC.Pack (node, self.adc)
        self.path = self.Path (node)