Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to read macro variables #2

Open
wants to merge 11 commits into
base: swig
Choose a base branch
from
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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Empty file added __init__.py
Empty file.
22 changes: 21 additions & 1 deletion fwlib.i
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%module fwlib
%module pyfwlib
%{
#include "./extern/fwlib/fwlib32.h"
typedef struct odbaxdt_t {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
97 changes: 97 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions pyfwlib.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Metadata-Version: 2.1
Name: pyfwlib
Version: 0.1
License-File: LICENSE
9 changes: 9 additions & 0 deletions pyfwlib.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions pyfwlib.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions pyfwlib.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
swig==4.1.1
wheel==0.41.0
setuptools==68.0.0
1 change: 1 addition & 0 deletions pyfwlib.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_fwlib
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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=[
Expand All @@ -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={
Expand Down