summaryrefslogtreecommitdiff
path: root/nxt-python-fantom/nxt/fantomglue.py
blob: 4a2497aabf85663415b94874b59205ce9a58c883 (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
# fantom.py module -- Glue code from NXT_Python to Fantom, allowing
# NXT_Python to run on Mac without modification.  Supports subset of
# PyBluez/bluetooth.py used by NXT_Python.
#
# Copyright (C) 2011  Tat-Chee Wan
#
# 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 3 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.

import pyfantom

FANTOM_BT='BT'
FANTOM_USB='USB'


def discover_devices(lookup_names=False):  # parameter is ignored
    pairs = []
    d = pyfantom.finddevices(proto = FANTOM_BT)
    for p in d:
        h = p[0]
        n = p[1]
        pairs.append((h, n))
    return pairs

class BluetoothSocket:

    def __init__(self, proto = FANTOM_BT, _sock=None):
        if _sock is None:
            _sock = pyfantom.socket(proto)
        self._sock = _sock
        self._proto = proto

    def connect(self, addrport):
        addr, port = addrport
        self._sock.connect( (addr, port ))
    
    def send(self, data):
        return self._sock.send( data )

    def recv(self, numbytes):
        return self._sock.recv( numbytes )
    
    def close(self):
        return self._sock.close()
    
class BluetoothError(IOError):
    pass