Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pybot/device.py
blob: d7e92553022a7d9e607fc15f5155b201ce7661fa (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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Device 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
OPEN_COMMAND = 0x00
CLOSE_COMMAND = 0x01
HEADER_PACKET_SIZE = 0x06
ADMIN_HANDLER_SEND_COMMAND = 0x00
OPEN_RESPONSE_PACKET_SIZE = 5
CLOSE_RESPONSE_PACKET_SIZE = 5
READ_HEADER_SIZE = 3
MAX_BYTES = 64

ERROR = -1

class Device():

    def __init__(self, baseboard, name, handler=None, func=None):
        self.baseboard = baseboard
        self.name = name
        self.handler = handler
        self.shifted = None
        if not(self.handler == None):
            self.shifted = self.handler * 8
        self.functions = func
        self.debug = False

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

    def send(self, msg):
        """
        Send to the device the specifiy call and parameters
        """
        w = [self.shifted, 0x03 + len(msg), NULL_BYTE] + msg
        self.baseboard.dev.write(w)

    def read(self, lenght):
        """
        Read the device data
        """
        raw = self.baseboard.dev.read(0x03 + lenght)
        return raw[3:]

    def module_open(self):
        """
        Open this device. Return the handler
        """
        module_name = self.to_ord(self.name)
        module_name.append(NULL_BYTE)

        w = [ADMIN_HANDLER_SEND_COMMAND]
        w.append(HEADER_PACKET_SIZE + len(module_name))
        w.append(NULL_BYTE)
        w.append(OPEN_COMMAND)
        w.append(0x01)
        w.append(0x01)
        self.baseboard.dev.write(w + module_name)

        raw = self.baseboard.dev.read(OPEN_RESPONSE_PACKET_SIZE)

        self._debug('device:module_open', raw)

        if not(raw[4] == 255):
            self.handler = raw[4]
            self.shifted = self.handler * 8
            return self.handler
        else:
            self._debug('device:module_open:cannot open module:', self.name)
            return 255

    def module_close(self):
        w = [ADMIN_HANDLER_SEND_COMMAND, 0x05, NULL_BYTE, CLOSE_COMMAND, self.handler]
        self.baseboard.dev.write(w)
        raw = self.baseboard.dev.read(CLOSE_RESPONSE_PACKET_SIZE)
        return raw[4]

    def has_function(self, func):
        """
        Check if this device has func function
        """
        return hasattr(self.functions, func)

    def call_function(self, func, params):
        """
        Call specify func function with params parameters
        """
        f = getattr(self.functions, func)
        if func == 'send':
            return f(self, params)
        else:
            par = []
            for e in params:
                par.append(int(e))
            if func == 'sendPacket':
                return f(self, par)
            else:
                return f(self, *par)

    def to_ord(self, string):
        """
        Useful function to convert characters into ordinal Unicode
        """
        s = []
        for l in string:
            o = ord(l)
            if not(o == 0):
                s.append(o)
        return s

    def to_text(self, raw):
        """
        Useful function to convert ordinal Unicode into text
        """
        ret = ''
        for r in raw:
            if not(r == 0):
                ret = ret + chr(r)
        return ret