Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pybot/baseboard.py
blob: 7ae28f68dca2b06a78166a20334021d61f7767c3 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Baseboard abstraction for USB4butia
#
# Copyright (c) 2012-2013 Alan Aguiar alanjas@hotmail.com
# Copyright (c) 2012-2013 Butiá Team butia@fing.edu.uy 
# Butia is a free and open robotic platform
# www.fing.edu.uy/inco/proyectos/butia
# Facultad de Ingeniería - Universidad de la República - Uruguay
#
# 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
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


NULL_BYTE = 0x00

ADMIN_MODULE_IN_ENDPOINT   = 0x01
ADMIN_MODULE_OUT_ENDPOINT  = 0x81

DEFAULT_PACKET_SIZE               = 0x04
GET_USER_MODULES_SIZE_COMMAND     = 0x05
GET_USER_MODULE_LINE_COMMAND      = 0x06
GET_HANDLER_SIZE_COMMAND          = 0x0A
GET_HANDLER_TYPE_COMMAND          = 0x0B
ADMIN_HANDLER_SEND_COMMAND        = 0x00
CLOSEALL_COMMAND                  = 0x07
CLOSEALL_RESPONSE_PACKET_SIZE     = 0x05
SWITCH_TO_BOOT_BASE_BOARD_COMMAND = 0x09
RESET_BASE_BOARD_COMMAND          = 0xFF
GET_USER_MODULE_LINE_PACKET_SIZE  = 0x05
GET_LINES_RESPONSE_PACKET_SIZE    = 0x05
GET_LINE_RESPONSE_PACKET_SIZE     = 0x0C
GET_HANDLER_TYPE_PACKET_SIZE      = 0x05
GET_HANDLER_RESPONSE_PACKET_SIZE  = 0x05

ERROR = -1

class Baseboard():

    def __init__(self, dev, debug=False):
        self.dev = dev
        self.debug = debug
        self.listi = {}
        self.devices = {}
        self.openables_loaded = []

    def _debug(self, message, err=''):
        if self.debug:
            print message, err

    def open_baseboard(self):
        """
        Open the baseboard
        """
        self.dev.open_device()

    def close_baseboard(self):
        """
        Close the baseboard
        """
        self.dev.close_device()

    def get_info(self):
        """
        Get baseboard info: manufacture..
        """
        return self.dev.get_info()

    def add_device(self, handler, device):
        """
        Add a device with handler: handler to the dictionary
        """
        self.devices[handler] = device

    def reset_device_list(self):
        """
        Cleans the device dictionary
        """
        self.devices = {}

    def add_openable_loaded(self, name):
        """
        Add the name of device that was opened to prevent open twice
        """
        if not(name in self.openables_loaded):
            self.openables_loaded.append(name)

    def remove_openable_loaded(self, name):
        """
        Remove the name of device that was opened
        """
        if name in self.openables_loaded:
            self.openables_loaded.remove(name)

    def get_openables_loaded(self):
        """
        Get the list of modules that was openened (no pnp)
        """
        return self.openables_loaded

    def reset_openables_loaded(self):
        """
        Reset the list of openables modules
        """
        self.openables_loaded = []

    def add_to_listi(self, number, name):
        """
        Add a device to listi
        """
        self.listi[number] = name

    def get_listi(self, force=False):
        """
        Get the listi: the list of modules present in the board that can be
        opened (or pnp module opens)
        """
        if (self.listi == {}) or force:
            self.generate_listi()
        return self.listi

    def generate_listi(self):
        """
        Generate the listi: the list of modules present in the board that can be
        opened (or pnp module opens)
        """
        self.listi = {}
        try:
            s = self.get_user_modules_size()
            for m in range(s):
                self.listi[m] = self.get_user_module_line(m)
        except:
            self.listi = {}
            self._debug('ERROR:baseboard listi')

    def get_device_handler(self, name):
        """
        Get the handler of device with name: name
        """
        for e in self.devices:
            if self.devices[e].name == name:
                return e
        return ERROR

    def get_device_name(self, handler):
        """
        Get the name of device with handler: handler
        """
        if self.devices.has_key(handler):
            return self.devices[handler].name
        else:
            return ''

    def get_user_modules_size(self):
        """
        Get the size of the list of user modules (listi)
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, DEFAULT_PACKET_SIZE, NULL_BYTE]
        w.append(GET_USER_MODULES_SIZE_COMMAND)
        self.dev.write(w)
        raw = self.dev.read(GET_USER_MODULE_LINE_PACKET_SIZE)
        self._debug('baseboard:get_user_modules_size', raw)
        return raw[4]

    def get_user_module_line(self, index):
        """
        Get the name of device with index: index (listi)
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, GET_USER_MODULE_LINE_PACKET_SIZE, NULL_BYTE]
        w.append(GET_USER_MODULE_LINE_COMMAND)
        w.append(index)
        self.dev.write(w)
        raw = self.dev.read(GET_LINE_RESPONSE_PACKET_SIZE)
        self._debug('baseboard:get_user_module_line', raw)
        c = raw[4:len(raw)]
        t = ''
        for e in c:
            if not(e == NULL_BYTE):
                t = t + chr(e)
        return t

    def get_handler_size(self):
        """
        Get the number of handlers opened
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, DEFAULT_PACKET_SIZE, NULL_BYTE]
        w.append(GET_HANDLER_SIZE_COMMAND)
        self.dev.write(w)
        raw = self.dev.read(GET_HANDLER_RESPONSE_PACKET_SIZE)
        self._debug('baseboard:get_handler_size', raw)
        return raw[4]

    def get_handler_type(self, index):
        """
        Get the type of the handler: index (return listi index)
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, GET_HANDLER_TYPE_PACKET_SIZE, NULL_BYTE]
        w.append(GET_HANDLER_TYPE_COMMAND)
        w.append(index)
        self.dev.write(w)
        raw = self.dev.read(GET_HANDLER_RESPONSE_PACKET_SIZE)
        self._debug('baseboard:get_handler_type', raw)
        return raw[4]

    def switch_to_bootloader(self):
        """
        Admin module command to switch to bootloader
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, DEFAULT_PACKET_SIZE, NULL_BYTE]
        w.append(SWITCH_TO_BOOT_BASE_BOARD_COMMAND)
        self.dev.write(w)

    def reset(self):
        """
        Admin module command to reset the board
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, DEFAULT_PACKET_SIZE, NULL_BYTE]
        w.append(RESET_BASE_BOARD_COMMAND)
        self.dev.write(w)

    def force_close_all(self):
        """
        Admin module command to force close all opened modules
        """
        w = [ADMIN_HANDLER_SEND_COMMAND, DEFAULT_PACKET_SIZE, NULL_BYTE]
        w.append(CLOSEALL_COMMAND)
        self.dev.write(w)
        raw = self.dev.read(CLOSEALL_RESPONSE_PACKET_SIZE)
        self._debug('baseboard:force_close_all', raw)
        return raw[4]