Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pybot/device.py
blob: 81a9e577a9231579a6f581f7416b48d1fc239b1f (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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Device abstraction for USB4butia
#
# 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 = 2

READ_HEADER_SIZE = 3
MAX_BYTES = 64

ERROR = -1

class Device():

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

    def add_functions(self, func_list):
        """
        Add the functions to current device
        """
        for f in func_list:
            self.functions[f['name']] = f

    def module_send(self, call, params_length, params):
        """
        Send to the device the specifiy call and parameters
        """
        if len(params) == 1:
            if type(params[0]) == str:
                params = to_ord(params[0])

        send_packet_length = 0x04 + len(params)

        w = []
        w.append(self.handler_tosend)
        w.append(send_packet_length)
        w.append(NULL_BYTE)
        w.append(call)
        for p in params:
            w.append(p)

        self.baseboard.dev.write(w)

    def module_read(self):
        """
        Read the device data
        """
        raw = self.baseboard.dev.read(MAX_BYTES)
        if self.debug:
            print 'device:module_rad return', raw
        if raw[1] == 5:
            if raw[4] == 255:
                return -1
            else:
                return raw[4]
        elif raw[1] == 6:
            return raw[4] + raw[5] * 256
        else:
            ret = ''
            for r in raw[4:]:
                if not(r == 0):
                    ret = ret + chr(r)
            return ret

    def module_open(self):
        """
        Open this device. Return the handler
        """
        module_name = to_ord(self.name)
        module_name.append(0)
        
        open_packet_length = HEADER_PACKET_SIZE + len(module_name) 

        module_in_endpoint  = 0x01
        module_out_endpoint = 0x01

        w = []
        w.append(ADMIN_HANDLER_SEND_COMMAND)
        w.append(open_packet_length)
        w.append(NULL_BYTE)
        w.append(OPEN_COMMAND)
        w.append(module_in_endpoint)
        w.append(module_out_endpoint)
        w = w + module_name
        self.baseboard.dev.write(w)

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

        if self.debug:
            print 'device:module_open return', raw

        h = raw[4]
        self.handler = h
        self.handler_tosend = self.handler * 8
        return h

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

    def call_function(self, func, params):
        """
        Call specify func function with params parameters
        """
        self.module_send(self.functions[func]['call'], self.functions[func]['params'], params)
        return self.module_read()

def to_ord(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