Skip to content

Commit 00aa6ce

Browse files
author
Thomas Preston
committed
Moved platform specific code into its own module
1 parent d2733ae commit 00aa6ce

File tree

2 files changed

+70
-65
lines changed

2 files changed

+70
-65
lines changed

codebug_tether/core.py

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,13 @@
11
from __future__ import print_function
2-
import os
3-
import sys
4-
import glob
52
import time
63
import serial
74
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()
7011

7112
IO_DIGITAL_OUTPUT = 0
7213
IO_DIGITAL_INPUT = 1

codebug_tether/platform.py

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

0 commit comments

Comments
 (0)