Skip to content

Commit 57d3102

Browse files
fix: new approach
1 parent 0e16887 commit 57d3102

File tree

2 files changed

+378
-131
lines changed

2 files changed

+378
-131
lines changed

sciencemode/__init__.py

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import sys
77
import ctypes
88
from pathlib import Path
9-
from . import sciencemode
109

11-
# First, try to load the shared library from our package directory
10+
# Import the main CFFI functionality
11+
from . import sciencemode
1212

1313

1414
def _find_and_load_library():
@@ -42,5 +42,101 @@ def _find_and_load_library():
4242
# Preload the library
4343
_libsmpt = _find_and_load_library()
4444

45+
# Import CFFI wrapper components and expose them at package level
46+
try:
47+
# Try to import the CFFI-generated module
48+
from ._sciencemode import lib, ffi
49+
50+
# Expose the CFFI library and FFI objects
51+
lib = lib
52+
ffi = ffi
53+
54+
# Create convenience functions for common structures
55+
def new_device():
56+
"""Create a new Smpt_device structure"""
57+
return ffi.new("Smpt_device *")
58+
59+
def new_ll_init():
60+
"""Create a new Smpt_ll_init structure"""
61+
return ffi.new("Smpt_ll_init *")
62+
63+
def new_ll_channel_config():
64+
"""Create a new Smpt_ll_channel_config structure"""
65+
return ffi.new("Smpt_ll_channel_config *")
66+
67+
def new_ml_init():
68+
"""Create a new Smpt_ml_init structure"""
69+
return ffi.new("Smpt_ml_init *")
70+
71+
def new_ml_update():
72+
"""Create a new Smpt_ml_update structure"""
73+
return ffi.new("Smpt_ml_update *")
74+
75+
# Expose commonly used enums and constants
76+
# These will be available directly as attributes
77+
try:
78+
# Result codes
79+
Smpt_Result_Successful = lib.Smpt_Result_Successful
80+
Smpt_Result_Unsuccessful = lib.Smpt_Result_Unsuccessful
81+
82+
# Channel numbers
83+
Smpt_Channel_Red = lib.Smpt_Channel_Red
84+
Smpt_Channel_Blue = lib.Smpt_Channel_Blue
85+
except AttributeError:
86+
# If these specific constants aren't available, that's okay
87+
pass
88+
89+
# Add commonly used structures as constructors
90+
try:
91+
# Make structures available as direct constructors
92+
def Smpt_device():
93+
return ffi.new("Smpt_device *")
94+
95+
def Smpt_ll_init():
96+
return ffi.new("Smpt_ll_init *")
97+
98+
def Smpt_ll_channel_config():
99+
return ffi.new("Smpt_ll_channel_config *")
100+
101+
def Smpt_ml_init():
102+
return ffi.new("Smpt_ml_init *")
103+
104+
def Smpt_ml_update():
105+
return ffi.new("Smpt_ml_update *")
106+
107+
except Exception:
108+
# If any structure creation fails, define minimal fallbacks
109+
pass
110+
111+
# Export main functionality
112+
__all__ = [
113+
"lib",
114+
"ffi",
115+
"sciencemode",
116+
"new_device",
117+
"new_ll_init",
118+
"new_ll_channel_config",
119+
"new_ml_init",
120+
"new_ml_update",
121+
"Smpt_device",
122+
"Smpt_ll_init",
123+
"Smpt_ll_channel_config",
124+
"Smpt_ml_init",
125+
"Smpt_ml_update",
126+
]
127+
128+
# Add result constants if available
129+
if "Smpt_Result_Successful" in locals():
130+
__all__.extend(["Smpt_Result_Successful", "Smpt_Result_Unsuccessful"])
131+
132+
# Add channel constants if available
133+
if "Smpt_Channel_Red" in locals():
134+
__all__.extend(["Smpt_Channel_Red", "Smpt_Channel_Blue"])
135+
136+
except ImportError as e:
137+
# If CFFI module is not available, provide a minimal interface
138+
print(f"Warning: CFFI module not available: {e}")
139+
print("You may need to rebuild the CFFI module.")
45140

46-
__all__ = ["sciencemode"]
141+
# Still export the sciencemode module for basic functionality
142+
__all__ = ["sciencemode"]

0 commit comments

Comments
 (0)