-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* Top level C program for embedded version of radae_tx.py */ | ||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION | ||
#include "numpy/arrayobject.h" | ||
|
||
#define NARGS 2 | ||
|
||
int main(void) | ||
{ | ||
PyObject *pName, *pModule, *pFunc; | ||
PyObject *pValue; | ||
//PyObject *pArgs; | ||
char *python_name = "radae_tx"; | ||
char *func_name = "get_nb_floats"; | ||
|
||
Py_Initialize(); | ||
|
||
// need import array for numpy | ||
int ret = _import_array(); | ||
fprintf(stderr, "import_array returned: %d\n", ret); | ||
|
||
// name of Python script | ||
pName = PyUnicode_DecodeFSDefault(python_name); | ||
/* Error checking of pName left out */ | ||
pModule = PyImport_Import(pName); | ||
Py_DECREF(pName); | ||
|
||
if (pModule != NULL) { | ||
pFunc = PyObject_GetAttrString(pModule, func_name); | ||
/* pFunc is a new reference */ | ||
|
||
if (pFunc && PyCallable_Check(pFunc)) { | ||
#ifdef T | ||
pArgs = PyTuple_New(NARGS); | ||
|
||
// first two args from command line | ||
pValue = PyLong_FromLong(atol(argv[3])); | ||
PyTuple_SetItem(pArgs, 0, pValue); | ||
pValue = PyLong_FromLong(atol(argv[4])); | ||
PyTuple_SetItem(pArgs, 1, pValue); | ||
|
||
// 3rd Python function arg - set up numpy array | ||
long dims = 3; | ||
float arr_in[] = {1.0,2.0,3.0}; | ||
pValue = PyArray_SimpleNewFromData(1, &dims, NPY_FLOAT, arr_in); | ||
if (pValue == NULL) { | ||
PyErr_Print(); | ||
fprintf(stderr,"Error setting up numpy array\n"); | ||
} | ||
PyTuple_SetItem(pArgs, 2, pValue); | ||
|
||
// 4th Python arg is a numpy array used for output to C | ||
float arr_out[] = {0.0,0.0,0.0}; | ||
pValue = PyArray_SimpleNewFromData(1, &dims, NPY_FLOAT, arr_out); | ||
if (pValue == NULL) { | ||
PyErr_Print(); | ||
fprintf(stderr,"Error setting up numpy array\n"); | ||
} | ||
PyTuple_SetItem(pArgs, 3, pValue); | ||
#endif | ||
|
||
// do the function call | ||
pValue = PyObject_CallObject(pFunc, NULL); | ||
//Py_DECREF(pArgs); | ||
if (pValue != NULL) { | ||
printf("Result of call: %ld\n", PyLong_AsLong(pValue)); | ||
Py_DECREF(pValue); | ||
|
||
// not sure how to return arrays but can modify input arrays in place as a hack | ||
//printf("returned array: %f %f %f\n", arr_out[0], arr_out[1], arr_out[2]); | ||
} | ||
else { | ||
Py_DECREF(pFunc); | ||
Py_DECREF(pModule); | ||
PyErr_Print(); | ||
fprintf(stderr,"Call failed\n"); | ||
return 1; | ||
} | ||
} | ||
else { | ||
if (PyErr_Occurred()) | ||
PyErr_Print(); | ||
fprintf(stderr, "Cannot find function \"%s\"\n", func_name); | ||
} | ||
Py_XDECREF(pFunc); | ||
Py_DECREF(pModule); | ||
} | ||
else { | ||
PyErr_Print(); | ||
fprintf(stderr, "Failed to load \"%s\"\n", python_name); | ||
return 1; | ||
} | ||
if (Py_FinalizeEx() < 0) { | ||
return 120; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters