OpenCores
URL https://opencores.org/ocsvn/qaz_libs/qaz_libs/trunk

Subversion Repositories qaz_libs

[/] [qaz_libs/] [trunk/] [BFM/] [sim/] [tests/] [tb_video_frame_dpi/] [py_dpi.c] - Blame information for rev 44

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
  char v_filename[] = "try_it.py";
94
  unsigned int size = (sizeof(pre_string) + sizeof(post_string)) * sizeof(char);
95
  size += strlen(filename);
96
  char *run_string = (char *)malloc(size);
97
  sprintf(run_string, "%s%s%s", pre_string, filename, post_string);
98
 
99
  PyRun_SimpleString(run_string);
100
  free(run_string);
101
  return 0;
102 43 qaztronic
}
103
 
104
// --------------------------------------------------------------------
105 44 qaztronic
extern PyMODINIT_FUNC PyInit_py_to_video_frame(void);
106
 
107 43 qaztronic
void init_py_dpi(int width, int height)
108
{
109
  printf("^^^ | init_py_dpi() | %dx%d\n", width, height);
110 44 qaztronic
 
111
  PyImport_AppendInittab("py_to_video_frame", PyInit_py_to_video_frame);
112 43 qaztronic
  Py_Initialize();
113 44 qaztronic
 
114
  PyObject *sys = PyImport_ImportModule("sys");
115
  PyObject *path = PyObject_GetAttrString(sys, "path");
116
  PyList_Append(path, PyUnicode_FromString("."));
117
 
118 43 qaztronic
  PyObject *dva_m = PyImport_ImportModule("dpi_video_array");
119
  if(!dva_m)
120
  {
121
      PyErr_Print();
122
      printf("^^^ | init_py_dpi() | dva_m Error\n");
123
  }
124 44 qaztronic
 
125 43 qaztronic
  PyObject *dva_c = PyObject_GetAttrString(dva_m, "dpi_video_array");
126
  Py_DECREF(dva_m);
127
  if(!dva_c)
128
  {
129
      PyErr_Print();
130
      printf("^^^ | init_py_dpi() | dva_c Error\n");
131
  }
132 44 qaztronic
 
133 43 qaztronic
  PyObject *args = Py_BuildValue("(ii)", width, height);
134
  g_dva_i = PyObject_CallObject(dva_c, args);
135
  Py_DECREF(args);
136
  Py_DECREF(dva_c);
137
  if(!g_dva_i)
138
  {
139
      PyErr_Print();
140
      printf("^^^ | init_py_dpi() | g_dva_i Error\n");
141
      Py_DECREF(g_dva_i);
142
      g_dva_i = NULL;
143
  }
144
}
145
 
146
// --------------------------------------------------------------------
147
void exit_py_dpi()
148
{
149
  printf("^^^ | exit_py_dpi()\n");
150 44 qaztronic
 
151 43 qaztronic
  if(g_dva_i)
152
    Py_DECREF(g_dva_i);
153 44 qaztronic
 
154 43 qaztronic
  Py_Finalize();
155
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.