Skip to content

Commit b5bd352

Browse files
committed
Simplifying python code with named constants #21
1 parent b559257 commit b5bd352

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

examples/python/hdlc_send_receive.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#
1919

2020
import tinyproto
21+
import tinyproto.options
2122

2223
p = tinyproto.Hdlc()
2324
def on_read(a):
@@ -29,9 +30,12 @@ def on_send(a):
2930
# setup protocol
3031
p.on_read = on_read
3132
p.on_send = on_send
33+
p.crc = tinyproto.options.HDLC_CRC_16
3234
p.begin()
3335

3436
# provide rx bytes to the protocol
37+
# For serial port communication you need to read the bytes from the serial port
38+
# and pass them to rx() method then.
3539
p.rx( bytearray([ 0x7E, 0xFF, 0x3F, 0xF3, 0x39, 0x7E ]) )
3640

3741
# Let's try to send single byte frame 0x65

python/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Copyright 2021 (C) Alexey Dynda
3+
4+
This file is part of Tiny Protocol Library.
5+
6+
Protocol Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Protocol Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18+
"""
19+
20+
import tinyproto_
21+
Hdlc = tinyproto_.Hdlc
22+
Fd = tinyproto_.Fd

python/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ static PyObject *pants(PyObject *self, PyObject *args)
3535
static PyMethodDef tinyproto_methods[] = {{"pants", pants, METH_VARARGS, "Returns a square of an integer."},
3636
{NULL, NULL, 0, NULL}};
3737

38-
static struct PyModuleDef tinyproto_definition = {PyModuleDef_HEAD_INIT, "tinyproto", "A Python tiny protocol module",
38+
static struct PyModuleDef tinyproto_definition = {PyModuleDef_HEAD_INIT, "tinyproto_", "A Python tiny protocol module",
3939
-1, tinyproto_methods};
4040

41-
PyMODINIT_FUNC PyInit_tinyproto(void)
41+
PyMODINIT_FUNC PyInit_tinyproto_(void)
4242
{
4343
Py_Initialize();
4444
PyObject *m = PyModule_Create(&tinyproto_definition);

python/options/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Copyright 2021 (C) Alexey Dynda
3+
4+
This file is part of Tiny Protocol Library.
5+
6+
Protocol Library is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Protocol Library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public License
17+
along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18+
"""
19+
20+
HDLC_CRC_DEFAULT = 0
21+
HDLC_CRC_8 = 8
22+
HDLC_CRC_16 = 16
23+
HDLC_CRC_32 = 32
24+
HDLC_CRC_OFF = 0xFF
25+

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
source_files.extend( glob.glob('./python/**/*.cpp', recursive=True ) )
2525

2626
tinyproto_module = Extension(
27-
'tinyproto',
27+
'tinyproto_',
2828
sources=source_files,
2929
include_dirs=['./src','./python'],
3030
define_macros=[("MYDEF", None), ("TINY_FD_DEBUG", 0), ("TINY_LOG_LEVEL_DEFAULT", 0)],
@@ -44,7 +44,8 @@
4444
version='0.12.1',
4545
description='tinyproto module wrapper',
4646
package_dir = { "tinyproto": "./python" },
47-
packages = ['tinyproto.wrappers'],
47+
packages = ['tinyproto', 'tinyproto.wrappers', 'tinyproto.options'],
48+
# py_modules = ['tinyproto.options'],
4849
ext_modules=[tinyproto_module],
4950
)
5051

0 commit comments

Comments
 (0)