|
1 | 1 | from __future__ import print_function |
2 | | -import os |
3 | | -import sys |
4 | | -import glob |
5 | 2 | import time |
6 | 3 | import serial |
7 | 4 | import struct |
8 | | -from codebug_tether.i2c import * |
9 | | -from codebug_tether.serial_channel_device import SerialChannelDevice |
10 | | - |
11 | | - |
12 | | -######################################################################## |
13 | | -# setup DEFAULT_SERIAL_PORT which is different on Windows, MacOS, |
14 | | -# Raspberry Pi 2 and Raspberry Pi 3 |
15 | | -if sys.platform.startswith('win') or sys.platform.startswith('darwin'): |
16 | | - # On Windows or OSX take the first serial port we can find |
17 | | - def serial_ports(): |
18 | | - """ Lists serial port names |
19 | | -
|
20 | | - :raises EnvironmentError: |
21 | | - On unsupported or unknown platforms |
22 | | - :returns: |
23 | | - A list of the serial ports available on the system |
24 | | - """ |
25 | | - if sys.platform.startswith('win'): |
26 | | - ports = ['COM%s' % (i + 1) for i in range(256)] |
27 | | - elif (sys.platform.startswith('linux') or |
28 | | - sys.platform.startswith('cygwin')): |
29 | | - # this excludes your current terminal "/dev/tty" |
30 | | - ports = glob.glob('/dev/tty[A-Za-z0-9]*') |
31 | | - elif sys.platform.startswith('darwin'): |
32 | | - ports = glob.glob('/dev/tty.*') |
33 | | - else: |
34 | | - raise EnvironmentError('Unsupported platform') |
35 | | - |
36 | | - result = [] |
37 | | - for port in ports: |
38 | | - try: |
39 | | - s = serial.Serial(port) |
40 | | - s.close() |
41 | | - result.append(port) |
42 | | - except (OSError, serial.SerialException): |
43 | | - pass |
44 | | - return result |
45 | | - # use the first one |
46 | | - try: |
47 | | - DEFAULT_SERIAL_PORT = serial_ports()[0] |
48 | | - except IndexError: |
49 | | - print('ERROR: Could not find any serial ports.', file=sys.stderr) |
50 | | - DEFAULT_SERIAL_PORT = '' |
51 | | -else: |
52 | | - # otherwise assume we're on Raspberry Pi/Linux |
53 | | - def get_rpi_revision(): |
54 | | - """Returns the version number from the revision line.""" |
55 | | - for line in open("/proc/cpuinfo"): |
56 | | - if "Revision" in line: |
57 | | - import re |
58 | | - return re.sub('Revision\t: ([a-z0-9]+)\n', r'\1', line) |
59 | | - |
60 | | - rpi_revision = get_rpi_revision() |
61 | | - if (rpi_revision and |
62 | | - (rpi_revision != 'Beta') and |
63 | | - (int('0x'+rpi_revision, 16) >= 0xa02082)): |
64 | | - # RPi 3 and above |
65 | | - DEFAULT_SERIAL_PORT = '/dev/ttyS0' |
66 | | - else: |
67 | | - # RPi 2 and below |
68 | | - DEFAULT_SERIAL_PORT = '/dev/ttyACM0' |
69 | | -######################################################################## |
| 5 | +from .i2c import * |
| 6 | +from .serial_channel_device import SerialChannelDevice |
| 7 | +from .platform import get_platform_serial_port |
| 8 | + |
| 9 | + |
| 10 | +DEFAULT_SERIAL_PORT = get_platform_serial_port() |
70 | 11 |
|
71 | 12 | IO_DIGITAL_OUTPUT = 0 |
72 | 13 | IO_DIGITAL_INPUT = 1 |
|
0 commit comments