| 1 |
43 |
qaztronic |
//////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//// ////
|
| 3 |
|
|
//// Copyright (C) 2018 Authors and OPENCORES.ORG ////
|
| 4 |
|
|
//// ////
|
| 5 |
|
|
//// This source file may be used and distributed without ////
|
| 6 |
|
|
//// restriction provided that this copyright statement is not ////
|
| 7 |
|
|
//// removed from the file and that any derivative work contains ////
|
| 8 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
| 9 |
|
|
//// ////
|
| 10 |
|
|
//// This source file is free software; you can redistribute it ////
|
| 11 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
| 12 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
| 13 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
| 14 |
|
|
//// later version. ////
|
| 15 |
|
|
//// ////
|
| 16 |
|
|
//// This source is distributed in the hope that it will be ////
|
| 17 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
| 18 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
| 19 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
| 20 |
|
|
//// details. ////
|
| 21 |
|
|
//// ////
|
| 22 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
| 23 |
|
|
//// Public License along with this source; if not, download it ////
|
| 24 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
| 25 |
|
|
//// ////
|
| 26 |
|
|
//////////////////////////////////////////////////////////////////////
|
| 27 |
|
|
#include <stdio.h>
|
| 28 |
|
|
#include <conio.h>
|
| 29 |
|
|
#include <Python.h>
|
| 30 |
|
|
#include "vpi_user.h"
|
| 31 |
|
|
#include "py_dpi.h"
|
| 32 |
|
|
|
| 33 |
|
|
// --------------------------------------------------------------------
|
| 34 |
|
|
PyObject *g_dva_i;
|
| 35 |
|
|
|
| 36 |
|
|
// --------------------------------------------------------------------
|
| 37 |
|
|
void c_do_it()
|
| 38 |
|
|
{
|
| 39 |
|
|
printf("^^^ | c_do_it()\n");
|
| 40 |
44 |
qaztronic |
|
| 41 |
43 |
qaztronic |
/* result = instance.method(x,y) */
|
| 42 |
|
|
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "do_it");
|
| 43 |
|
|
// Py_DECREF(pinst);
|
| 44 |
|
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
| 45 |
|
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
| 46 |
|
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
| 47 |
|
|
Py_DECREF(pmeth);
|
| 48 |
|
|
// Py_DECREF(pargs);
|
| 49 |
|
|
Py_DECREF(pres);
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
// --------------------------------------------------------------------
|
| 53 |
|
|
unsigned int *g_va = NULL;
|
| 54 |
|
|
|
| 55 |
|
|
void c_get_array(const svOpenArrayHandle va)
|
| 56 |
|
|
{
|
| 57 |
|
|
int va_y = svSize(va, 1);
|
| 58 |
|
|
int va_x = svSize(va, 2);
|
| 59 |
44 |
qaztronic |
|
| 60 |
43 |
qaztronic |
printf("^^^ | c_get_array() | %dx%d\n", va_x, va_y);
|
| 61 |
|
|
g_va = (unsigned int *)svGetArrElemPtr2(va, 0, 0);
|
| 62 |
44 |
qaztronic |
|
| 63 |
43 |
qaztronic |
PyObject *pmeth = PyObject_GetAttrString(g_dva_i, "py_get_array");
|
| 64 |
|
|
// pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
|
| 65 |
|
|
// PyObject *pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
|
| 66 |
|
|
PyObject *pres = PyEval_CallObject(pmeth, NULL);
|
| 67 |
44 |
qaztronic |
|
| 68 |
43 |
qaztronic |
Py_DECREF(pmeth);
|
| 69 |
|
|
// Py_DECREF(pargs);
|
| 70 |
|
|
Py_DECREF(pres);
|
| 71 |
|
|
}
|
| 72 |
|
|
|
| 73 |
|
|
// --------------------------------------------------------------------
|
| 74 |
44 |
qaztronic |
#define MAX_ARGV 4
|
| 75 |
|
|
|
| 76 |
43 |
qaztronic |
int py_run_file
|
| 77 |
|
|
( const char *filename
|
| 78 |
|
|
, int argc
|
| 79 |
|
|
, const svOpenArrayHandle args
|
| 80 |
|
|
)
|
| 81 |
|
|
{
|
| 82 |
|
|
printf("^^^ | py_run_file | %s |\n", filename);
|
| 83 |
|
|
|
| 84 |
44 |
qaztronic |
wchar_t *w_argv[MAX_ARGV]; // limit of MAX_ARGV args
|
| 85 |
43 |
qaztronic |
int i;
|
| 86 |
44 |
qaztronic |
for(i = 0; i < argc && i < MAX_ARGV; i++)
|
| 87 |
|
|
w_argv[i] = Py_DecodeLocale(*((char **)svGetArrElemPtr1(args, i)), NULL);
|
| 88 |
43 |
qaztronic |
|
| 89 |
44 |
qaztronic |
PySys_SetArgv(argc, w_argv);
|
| 90 |
|
|
|
| 91 |
|
|
char pre_string[] = "exec(open('./";
|
| 92 |
|
|
char post_string[] = "').read())";
|
| 93 |
|
|
unsigned int size = (sizeof(pre_string) + sizeof(post_string)) * sizeof(char);
|
| 94 |
|
|
size += strlen(filename);
|
| 95 |
|
|
char *run_string = (char *)malloc(size);
|
| 96 |
|
|
sprintf(run_string, "%s%s%s", pre_string, filename, post_string);
|
| 97 |
|
|
|
| 98 |
|
|
PyRun_SimpleString(run_string);
|
| 99 |
|
|
free(run_string);
|
| 100 |
|
|
return 0;
|
| 101 |
43 |
qaztronic |
}
|
| 102 |
|
|
|
| 103 |
|
|
// --------------------------------------------------------------------
|
| 104 |
44 |
qaztronic |
extern PyMODINIT_FUNC PyInit_py_to_video_frame(void);
|
| 105 |
|
|
|
| 106 |
43 |
qaztronic |
void init_py_dpi(int width, int height)
|
| 107 |
|
|
{
|
| 108 |
|
|
printf("^^^ | init_py_dpi() | %dx%d\n", width, height);
|
| 109 |
44 |
qaztronic |
|
| 110 |
|
|
PyImport_AppendInittab("py_to_video_frame", PyInit_py_to_video_frame);
|
| 111 |
43 |
qaztronic |
Py_Initialize();
|
| 112 |
44 |
qaztronic |
|
| 113 |
|
|
PyObject *sys = PyImport_ImportModule("sys");
|
| 114 |
|
|
PyObject *path = PyObject_GetAttrString(sys, "path");
|
| 115 |
|
|
PyList_Append(path, PyUnicode_FromString("."));
|
| 116 |
|
|
|
| 117 |
43 |
qaztronic |
PyObject *dva_m = PyImport_ImportModule("dpi_video_array");
|
| 118 |
|
|
if(!dva_m)
|
| 119 |
|
|
{
|
| 120 |
|
|
PyErr_Print();
|
| 121 |
|
|
printf("^^^ | init_py_dpi() | dva_m Error\n");
|
| 122 |
|
|
}
|
| 123 |
44 |
qaztronic |
|
| 124 |
43 |
qaztronic |
PyObject *dva_c = PyObject_GetAttrString(dva_m, "dpi_video_array");
|
| 125 |
|
|
Py_DECREF(dva_m);
|
| 126 |
|
|
if(!dva_c)
|
| 127 |
|
|
{
|
| 128 |
|
|
PyErr_Print();
|
| 129 |
|
|
printf("^^^ | init_py_dpi() | dva_c Error\n");
|
| 130 |
|
|
}
|
| 131 |
44 |
qaztronic |
|
| 132 |
43 |
qaztronic |
PyObject *args = Py_BuildValue("(ii)", width, height);
|
| 133 |
|
|
g_dva_i = PyObject_CallObject(dva_c, args);
|
| 134 |
|
|
Py_DECREF(args);
|
| 135 |
|
|
Py_DECREF(dva_c);
|
| 136 |
|
|
if(!g_dva_i)
|
| 137 |
|
|
{
|
| 138 |
|
|
PyErr_Print();
|
| 139 |
|
|
printf("^^^ | init_py_dpi() | g_dva_i Error\n");
|
| 140 |
|
|
Py_DECREF(g_dva_i);
|
| 141 |
|
|
g_dva_i = NULL;
|
| 142 |
|
|
}
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
// --------------------------------------------------------------------
|
| 146 |
|
|
void exit_py_dpi()
|
| 147 |
|
|
{
|
| 148 |
|
|
printf("^^^ | exit_py_dpi()\n");
|
| 149 |
44 |
qaztronic |
|
| 150 |
43 |
qaztronic |
if(g_dva_i)
|
| 151 |
|
|
Py_DECREF(g_dva_i);
|
| 152 |
44 |
qaztronic |
|
| 153 |
43 |
qaztronic |
Py_Finalize();
|
| 154 |
|
|
}
|