diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c29b3f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 tonejca + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 70bdebf..200f7b2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # fwlib & swig # Instructions -## Dependencies + +## I - Install package automatically +``` +pip3 install -e git+https://github.com/tonejca/pyfwlib.git@swig#egg=fwlib +``` + +## II - Install library semi-automated get fwlib ``` git submodule update --init --recursive diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fwlib.i b/fwlib.i index 27273c9..1213e62 100644 --- a/fwlib.i +++ b/fwlib.i @@ -1,4 +1,4 @@ -%module fwlib +%module pyfwlib %{ #include "./extern/fwlib/fwlib32.h" typedef struct odbaxdt_t { @@ -313,6 +313,25 @@ void deinit() $result = SWIG_Python_AppendOutput($result, o); %} +%typemap(in,numinputs=0) (unsigned long *num, double *data) %{ + unsigned long temp2 = 1; + $1 = &temp2; + double temp3 = 0; + $2 = &temp3; +%} +%typemap(argout) (unsigned long *num, double *data) %{ +PyObject *o = PyTuple_New(2); +PyObject *oo; +int num2 = *$1; +float num3 = *$2; + +oo = PyLong_FromLong(num2); +PyTuple_SetItem(o, 0, oo); +oo = PyLong_FromLong(num3); +PyTuple_SetItem(o, 1, oo); + +$result = SWIG_Python_AppendOutput($result, o); +%} %typemap(in, numinputs=0) (ODBEXEPRG *exeprg) %{ ODBEXEPRG_T temp; @@ -420,3 +439,4 @@ short cnc_exeprgname2(unsigned short, char *path_name); short cnc_rdopmsg(unsigned short, short type, short length, OPMSG *opmsg); short cnc_statinfo(unsigned short, ODBST *statinfo); short cnc_freelibhndl(unsigned short libh); +short cnc_rdmacror2(unsigned short, unsigned long s_no, unsigned long *num, double *data); \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..17b6899 --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +# -- Imports ------------------------------------------------------------------ +import fwlib # namespace +from types import TracebackType +from typing import Optional, Type + + +class FocasException(Exception): + pass + + +class UnableToReadMacro(FocasException): + """Raised when a macro could not be read""" + + +class UnableToReadAxis(FocasException): + """Raised when an axis could not be read""" + + +class FocasController: + def __init__(self, ip: str, port: int = 8193, sample_rate: int = 10): + self.ip = ip + self.port = port + self.sample_rate = sample_rate + self.library_handle = None + + def __enter__(self): # -> Focas: + """Initialize the connector class""" + + self.connect() + return self + + def __exit__( + self, + exception_type: Optional[Type[BaseException]], + exception_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> None: + """Clean up the resources used by the storage class + + Parameters + ---------- + + exception_type: + The type of the exception in case of an exception + + exception_value: + The value of the exception in case of an exception + + traceback: + The traceback in case of an exception + + """ + + self.disconnect() + + def connect(self): + ret, self.libh = fwlib.cnc_allclibhndl3(self.ip, self.port, self.sample_rate) + if ret != 0: + raise ConnectionError( + f"Unable to connect to Focas control (IP: {self.ip}, Port:" + f" {self.port})" + ) + + self.library_handle = self.libh + assert self.library_handle is not None, "Library handle not initialized" + + def disconnect(self): + fwlib.cnc_freelibhndl(self.library_handle) + + def read_macro(self, register: int) -> int: + ret, (_, dataFeature) = fwlib.cnc_rdmacror2(self.library_handle, register) + if ret != 0: + raise UnableToReadMacro(f"Unable to read macro “{register}”") + return dataFeature + + def read_axis(self, category: int, index: int) -> int: + ret, axisdata = fwlib.cnc_rdaxisdata(self.library_handle, category, [index]) + if ret != 0: + raise UnableToReadAxis(f"Unable to read axis data “{category}”, “{index}”") + return axisdata + + +if __name__ == "__main__": + nc_data = [] + focas_controller = FocasController("192.168.1.10") + with focas_controller: + try: + axis_tuples = [[1, 0], [2, 0], [3, 0], [5, 0], [5, 1]] + for tuple in axis_tuples: + axisdata = focas_controller.read_axis(tuple[0], tuple[1]) + for axis in axisdata: + nc_data.append(axis[1]) + print(nc_data) + except (ConnectionError, UnableToReadMacro, UnableToReadAxis) as error: + print(error) + focas_controller.disconnect() + print(nc_data) diff --git a/pyfwlib.egg-info/PKG-INFO b/pyfwlib.egg-info/PKG-INFO new file mode 100644 index 0000000..63350ff --- /dev/null +++ b/pyfwlib.egg-info/PKG-INFO @@ -0,0 +1,4 @@ +Metadata-Version: 2.1 +Name: pyfwlib +Version: 0.1 +License-File: LICENSE diff --git a/pyfwlib.egg-info/SOURCES.txt b/pyfwlib.egg-info/SOURCES.txt new file mode 100644 index 0000000..694828c --- /dev/null +++ b/pyfwlib.egg-info/SOURCES.txt @@ -0,0 +1,9 @@ +LICENSE +README.md +fwlib.i +setup.py +pyfwlib.egg-info/PKG-INFO +pyfwlib.egg-info/SOURCES.txt +pyfwlib.egg-info/dependency_links.txt +pyfwlib.egg-info/requires.txt +pyfwlib.egg-info/top_level.txt \ No newline at end of file diff --git a/pyfwlib.egg-info/dependency_links.txt b/pyfwlib.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pyfwlib.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/pyfwlib.egg-info/requires.txt b/pyfwlib.egg-info/requires.txt new file mode 100644 index 0000000..63e31bf --- /dev/null +++ b/pyfwlib.egg-info/requires.txt @@ -0,0 +1,3 @@ +swig==4.1.1 +wheel==0.41.0 +setuptools==68.0.0 diff --git a/pyfwlib.egg-info/top_level.txt b/pyfwlib.egg-info/top_level.txt new file mode 100644 index 0000000..9327967 --- /dev/null +++ b/pyfwlib.egg-info/top_level.txt @@ -0,0 +1 @@ +_fwlib diff --git a/setup.py b/setup.py index 63e4199..483d411 100644 --- a/setup.py +++ b/setup.py @@ -7,8 +7,10 @@ if not os.path.isfile(libpath): plat = "linux" machine = platform.machine() + print("machine") + print(machine) version = "1.0.5" - if machine == "x86_64": + if machine == "x86_64" or "AMD64": arch = "x64" elif machine == "i386": arch = "x86" @@ -17,12 +19,12 @@ else: pass fname = f"libfwlib32-{plat}-{arch}.so.{version}" - print(f"{fname=}", f"{libpath=}") + # print(f"{fname=}", f"{libpath=}") os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so")) os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so.1")) setup( - name="fwlib", + name="pyfwlib", version="0.1", description="", ext_modules=[ @@ -39,6 +41,7 @@ runtime_library_dirs=[fwlib_dir], ) ], + install_requires=["swig==4.1.1", "wheel==0.41.0", "setuptools==68.0.0",], # used to locate c header files include_dirs=[fwlib_dir], package_data={