|
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 |
@@ -231,19 +172,19 @@ def set_leg_io(self, leg_index, direction): |
231 | 172 | def pwm_on(self, t2_prescale, full_period, on_period): |
232 | 173 | """Turns on the PWM generator with the given settings. |
233 | 174 |
|
234 | | - :param t2_prescale: One of T2_PS_1_1, T2_PS_1_4, T2_PS_1_16 |
235 | | - Scales down the 12MHz instruction clock by |
236 | | - 1, 4 or 16. |
237 | | - :param full_period: 8-bit value - which is scaled up to 10-bits |
238 | | - (<< 2) - to which timer 2 will count up to |
239 | | - before resetting PWM output to 1. |
240 | | - :param on_period: 10-bit value to which timer 2 will count up to |
241 | | - before setting PWM output to 0. Use this with |
242 | | - full_period to control duty cycle. For |
243 | | - example: |
| 175 | + Args: |
| 176 | + t2_prescale: One of T2_PS_1_1, T2_PS_1_4, T2_PS_1_16 |
| 177 | + Scales down the 12MHz instruction clock by |
| 178 | + 1, 4 or 16. |
| 179 | + full_period: 8-bit value - which is scaled up to 10-bits |
| 180 | + (<< 2) - to which timer 2 will count up to |
| 181 | + before resetting PWM output to 1. |
| 182 | + on_period: 10-bit value to which timer 2 will count up to |
| 183 | + before setting PWM output to 0. Use this with |
| 184 | + full_period to control duty cycle. For example: |
244 | 185 |
|
245 | | - # 12MHz / 16 with 50% duty cycle |
246 | | - codebug.pwm_on(T2_PS_1_16, 0xff, 0x200) |
| 186 | + # 12MHz / 16 with 50% duty cycle |
| 187 | + codebug.pwm_on(T2_PS_1_16, 0xff, 0x200) |
247 | 188 |
|
248 | 189 | """ |
249 | 190 | # full period |
@@ -394,6 +335,34 @@ def draw_sprite(self, x, y, sprite, clear_first=True): |
394 | 335 | for i, row in enumerate(cb_rows): |
395 | 336 | self.or_mask(i, bytes(row)) |
396 | 337 |
|
| 338 | + def scroll_sprite(self, sprite, interval=0.1, direction='L'): |
| 339 | + """Scrolls a sprite. |
| 340 | +
|
| 341 | + Args: |
| 342 | + sprite: The sprite to scroll. |
| 343 | + interval: The time delay between each movement in seconds. |
| 344 | + (optional) |
| 345 | + direction: The direction of the scroll ('L', 'R', 'U', 'D'). |
| 346 | +
|
| 347 | + """ |
| 348 | + direction = direction.upper()[0] # only take the first char |
| 349 | + if direction == 'L': |
| 350 | + for i in range(sprite.width+5): |
| 351 | + self.draw_sprite(5-i, 0, sprite) |
| 352 | + time.sleep(interval) |
| 353 | + elif direction == 'D': |
| 354 | + for i in range(sprite.height+5): |
| 355 | + self.draw_sprite(0, 5-i, sprite) |
| 356 | + time.sleep(interval) |
| 357 | + elif direction == 'R': |
| 358 | + for i in reversed(range(sprite.width+5)): |
| 359 | + self.draw_sprite(5-i, 0, sprite) |
| 360 | + time.sleep(interval) |
| 361 | + elif direction == 'U': |
| 362 | + for i in reversed(range(sprite.height+5)): |
| 363 | + self.draw_sprite(0, 5-i, sprite) |
| 364 | + time.sleep(interval) |
| 365 | + |
397 | 366 | def config_extension_io(self): |
398 | 367 | self.set(CHANNEL_INDEX_EXT_CONF, EXTENSION_CONF_IO) |
399 | 368 |
|
@@ -449,10 +418,12 @@ def i2c_transaction(self, |
449 | 418 | """Run an I2C transaction using the extensions pins. Be sure to |
450 | 419 | configure the extension pins first. |
451 | 420 |
|
452 | | - :param add_stop_last_message: Adds stop flag to the last I2CMessage. |
453 | | - :type add_stop_last_message: boolean |
454 | | - :param interval: Adds delay of `interval` seconds between I2C messages. |
455 | | - :type interval: interger |
| 421 | + Args: |
| 422 | + messages: The I2C messages. |
| 423 | + add_stop_last_message: Adds stop flag to the last |
| 424 | + I2CMessage. |
| 425 | + interval: Adds delay of `interval` seconds between I2C |
| 426 | + messages. |
456 | 427 |
|
457 | 428 | Example: |
458 | 429 |
|
|
0 commit comments