Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:

create_release:
name: Create Release
needs: [build_wheels, build_docs]
runs-on: ubuntu-22.04
needs: [build_wheels]
runs-on: ubuntu-24.04

steps:
- name: Get version
Expand Down
43 changes: 12 additions & 31 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ repos:
- id: check-useless-excludes
- id: check-hooks-apply
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v6.0.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: debug-statements
- id: requirements-txt-fixer
- id: fix-encoding-pragma
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
Expand All @@ -29,7 +28,7 @@ repos:
#exclude: '(conda/meta.yaml|.pep8speaks.yml)'

- repo: https://github.com/PyCQA/doc8
rev: v1.1.1
rev: v2.0.0
hooks:
- id: doc8
args:
Expand All @@ -45,25 +44,26 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 8.0.1
hooks:
- id: isort
args: [--settings-path=setup.cfg]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
rev: v1.20.0
hooks:
- id: mypy
additional_dependencies:
- pyusb
args:
- --follow-imports=normal
- --install-types
- --non-interactive
#- --check-untyped-defs
additional_dependencies:
- "pyusb"
files: ^cp210x.*\.py$

- repo: https://github.com/myint/autoflake
rev: v2.0.2
rev: v2.3.3
hooks:
- id: autoflake
files: ^cp210x.*\.py$
Expand All @@ -72,28 +72,9 @@ repos:
- --remove-duplicate-keys
- --remove-unused-variables

# use ffffff (black fork) for single quote normalization
# (otherwise switch to black for double quotes)
- repo: https://github.com/grktsh/ffffff
rev: v2020.8.31
- repo: https://github.com/ambv/black
rev: 26.3.1
hooks:
- id: ffffff
name: "Format code (ffffff)"
files: ^cp210x.*\.py$
- id: black
name: "Format code"
language_version: python3

ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
autofix_prs: false
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: weekly
skip: []
submodules: false

# re-running a pull request: you can trigger a re-run on a pull request by
# commenting pre-commit.ci run (must appear on a line by itself).
# skipping push runs: skip a run by putting [skip ci], [ci skip],
# [skip pre-commit.ci], or [pre-commit.ci skip] in the commit message.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ Read EEPROM content into hexfile::
Show EEPROM content from device 002 on bus 001::

$ cp210x-program --read-cp210x -m 001/002

*Hint: The bus and device number can be queried using the `lsusb -t` command, which is available on most linux distributions.*


*Hint: The bus and device number can be queried using the ``lsusb -t`` command,
which is available on most linux distributions.*


Write some data to device with vendor id 0x10C4 and product id 0xEA62::

Expand Down
2 changes: 0 additions & 2 deletions cp210x/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# -*- coding: utf-8 -*-

__license__ = 'GNU LGPL'
__version__ = '0.4.1'
27 changes: 7 additions & 20 deletions cp210x/cp210x.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Johannes Hölzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
Expand Down Expand Up @@ -77,7 +76,7 @@ def to_bcd(i):


def to_bcd2(i_j_tuple):
(i, j) = i_j_tuple
i, j = i_j_tuple
return to_bcd(i) << 8 | to_bcd(j)


Expand Down Expand Up @@ -173,9 +172,7 @@ class Cp210xProgrammer(object):
TIMEOUT = 300 # ms

@classmethod
def list_devices(
self, patterns=[{'idVendor': VID_SILABS, 'idProduct': PID_CP210x}]
):
def list_devices(self, patterns=[{'idVendor': VID_SILABS, 'idProduct': PID_CP210x}]):
"""Yields a list of devices matching certain patterns.

param patterns: This must be a list of dictionaries. Each device
Expand Down Expand Up @@ -227,18 +224,14 @@ def _set_config(self, value, index=0, data=None, request=CP210x_CONFIG):
CTRL_OUT | CTRL_TYPE_VENDOR, request, value, index, data
)
if data is not None and res != len(data):
raise Cp210xError(
'Short write (%d of %d bytes)' % (res, len(data))
)
raise Cp210xError('Short write (%d of %d bytes)' % (res, len(data)))

def _set_config_string(self, value, content, max_desc_size):
assert isinstance(content, str)
encoded = content.encode('utf-16-le')
desc_size = len(encoded) + 2
assert desc_size <= max_desc_size
self._set_config(
value, data=desc_size.to_bytes(1, 'big') + b'\x03' + encoded
)
self._set_config(value, data=desc_size.to_bytes(1, 'big') + b'\x03' + encoded)

def _get_config(self, value, length, index=0, request=CP210x_CONFIG):
print(
Expand Down Expand Up @@ -315,9 +308,7 @@ def get_locked(self):

def set_eeprom_content(self, content):
"""Write a 1024-byte blob to the EEPROM"""
assert len(content) == SIZE_EEPROM, (
'EEPROM data must be %i bytes.' % SIZE_EEPROM
)
assert len(content) == SIZE_EEPROM, 'EEPROM data must be %i bytes.' % SIZE_EEPROM
assert isinstance(content, bytes), 'EEPROM data must be bytes.'
self._set_config(REG_EEPROM, data=content)

Expand All @@ -339,9 +330,7 @@ def set_product_string(self, product_string):
For Unicode Plane 0 (BMP; code points 0-FFFF), this specifies
the maximum length of the string in characters.
"""
self._set_config_string(
REG_PRODUCT_STRING, product_string, SIZE_PRODUCT_STRING
)
self._set_config_string(REG_PRODUCT_STRING, product_string, SIZE_PRODUCT_STRING)

def set_serial_number(self, serial_number):
"""Set the serial number string.
Expand All @@ -351,9 +340,7 @@ def set_serial_number(self, serial_number):
For Unicode Plane 0 (BMP; code points 0-FFFF), this specifies
the maximum length of the string in characters.
"""
self._set_config_string(
REG_SERIAL_NUMBER, serial_number, SIZE_SERIAL_NUMBER
)
self._set_config_string(REG_SERIAL_NUMBER, serial_number, SIZE_SERIAL_NUMBER)

def set_max_power(self, max_power):
"""Set maximum power consumption."""
Expand Down
30 changes: 9 additions & 21 deletions cp210x/eeprom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Johannes Hölzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
Expand Down Expand Up @@ -51,9 +50,10 @@ def set(self, value):
def _str_value(position, max_desc_size):
def get(self):
desc_size = from_binary(self.get(position, 1))
assert (
desc_size <= max_desc_size and desc_size >= 2
), 'desc_size: %d, max: %d' % (desc_size, max_desc_size)
assert desc_size <= max_desc_size and desc_size >= 2, 'desc_size: %d, max: %d' % (
desc_size,
max_desc_size,
)
assert self.get(position + 1, 1) == b'\x03', 'Missing 0x03 at %04X' % (
position + 1
)
Expand Down Expand Up @@ -109,8 +109,7 @@ def parse_hex_file(self, hex_content):
if tag_type == 0x00:
if tag_address != address:
raise HexFileError(
'Expected address %04X but found %04X'
% (address, tag_address)
'Expected address %04X but found %04X' % (address, tag_address)
)
self.content += line
address += len(line)
Expand All @@ -126,12 +125,7 @@ def build_hex_file(self):
for tag_start in range(0, len(self.content), 0x10):
line = self.content[tag_start : tag_start + 0x10]
address = self.START_ADDRESS + tag_start
tag = (
to_binary(len(line), 1)
+ to_binary(address, le=False)
+ b'\x00'
+ line
)
tag = to_binary(len(line), 1) + to_binary(address, le=False) + b'\x00' + line
cs = checksum(tag)
if cs == 0:
tag += b'\x00'
Expand Down Expand Up @@ -170,20 +164,14 @@ def get(self, pos, length):
return self.content[pos : pos + length]

def set(self, pos, data):
self.content = (
self.content[:pos] + data + self.content[pos + len(data) :]
)
self.content = self.content[:pos] + data + self.content[pos + len(data) :]

@property
def baudrate_table(self):
dat = self.get(POS_BAUDRATE_TABLE, cp210x.SIZE_BAUDRATE_TABLE)
return [
cp210x.parse_baudrate_cfg(
dat[pos : pos + cp210x.SIZE_BAUDRATE_CFG]
)
for pos in range(
0, cp210x.SIZE_BAUDRATE_TABLE, cp210x.SIZE_BAUDRATE_CFG
)
cp210x.parse_baudrate_cfg(dat[pos : pos + cp210x.SIZE_BAUDRATE_CFG])
for pos in range(0, cp210x.SIZE_BAUDRATE_TABLE, cp210x.SIZE_BAUDRATE_CFG)
]

@baudrate_table.setter
Expand Down
20 changes: 6 additions & 14 deletions cp210x/valuefile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Johannes Hölzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
Expand Down Expand Up @@ -66,9 +65,7 @@ def write_bool(b):
def read_bool(s):
s = s.strip().lower()
if s not in ['true', 'yes', 'false', 'no']:
raise ValueError(
"Boolean must be either 'true', 'yes', 'false' or 'no'."
)
raise ValueError("Boolean must be either 'true', 'yes', 'false' or 'no'.")
return s in ['true', 'yes']


Expand Down Expand Up @@ -126,15 +123,12 @@ def read_file(fp):
baudrate = int(name)
except ValueError:
raise ValuesFileError(
"Key names in 'baudrate table' must be"
' baudrate numbers.'
"Key names in 'baudrate table' must be" ' baudrate numbers.'
)
try:
baudrate_table.append(read_baudrate_info(value) + (baudrate,))
except ValueError as err:
raise ValuesFileError(
'Wrong baudrate info %i: %s' % (baudrate, str(err))
)
raise ValuesFileError('Wrong baudrate info %i: %s' % (baudrate, str(err)))
baudrate_table.sort(key=(lambda i: i[3]), reverse=True)

values['baudrate_table'] = baudrate_table
Expand All @@ -154,7 +148,7 @@ def write_file(fp, values):
if 'baudrate_table' in values:
fp.write('\n')
fp.write('[baudrate table]\n')
for (baudgen, timegen, prescaler, baudrate) in sorted(
for baudgen, timegen, prescaler, baudrate in sorted(
values['baudrate_table'], key=(lambda i: i[3]), reverse=True
):
fp.write(
Expand Down Expand Up @@ -210,9 +204,7 @@ def update_values(v, new, dev):
baudrate_table = old_baudrate_table
else:
baudrate_table = list(
merge_baudrate_table(
dev.baudrate_table, old_baudrate_table
)
merge_baudrate_table(dev.baudrate_table, old_baudrate_table)
)
else:
baudrate_table = dev.baudrate_table
Expand All @@ -225,7 +217,7 @@ def update_values(v, new, dev):


def merge_baudrate_table(old, new):
for (old_info, (start, stop)) in zip(old, REQUEST_BAUDRATE_RANGES):
for old_info, (start, stop) in zip(old, REQUEST_BAUDRATE_RANGES):
for baudgen, timer, prescaler, baudrate in new:
if (start is None or baudrate <= start) and baudrate >= stop:
yield (baudgen, timer, prescaler, baudrate)
Expand Down
Loading
Loading