ready to try executing actual Python within UPIWIN

This commit is contained in:
Amy G. Bowersox 2019-12-07 22:05:31 -07:00
parent f30a27ad7c
commit aa94d80145
8 changed files with 133 additions and 19 deletions

19
scripts/tmp_main.py Normal file
View File

@ -0,0 +1,19 @@
# Initial test script
FBPRIMCLR_BLACK = 0x0000
FBPRIMCLR_RED = 0xF800
FBPRIMCLR_GREEN = 0x07E0
FBPRIMCLR_BLUE = 0x001F
FBPRIMCLR_YELLOW = 0xFFE0
FBPRIMCLR_CYAN = 0x07FF
FBPRIMCLR_MAGENTA = 0xF81F
FBPRIMCLR_WHITE = 0xFFFF
upiwin_tmp.filled_rectangle(10, 10, 50, 50, FBPRIMCLR_RED, False)
upiwin_tmp.filled_rectangle(60, 10, 100, 50, FBPRIMCLR_GREEN, False)
upiwin_tmp.filled_rectangle(110, 10, 150, 50, FBPRIMCLR_BLUE, False)
upiwin_tmp.filled_rectangle(10, 60, 50, 100, FBPRIMCLR_CYAN, False)
upiwin_tmp.filled_rectangle(60, 60, 100, 100, FBPRIMCLR_MAGENTA, False)
upiwin_tmp.filled_rectangle(110, 60, 150, 100, FBPRIMCLR_YELLOW, False)
upiwin_tmp.rectangle(10, 110, 150, 150, FBPRIMCLR_WHITE, False)
upiwin_tmp.line(10, 110, 150, 150, FBPRIMCLR_WHITE, False)
upiwin_tmp.line(10, 150, 150, 110, FBPRIMCLR_WHITE, False)

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
@ -75,6 +76,7 @@ static HRESULT parse_cmdline(int argc, char *argv[], GLOBAL_CONFIG *parsed)
{ {
int c; int c;
PSTR pstr; PSTR pstr;
PPCSTR pargs;
BOOL help = FALSE; BOOL help = FALSE;
memset(parsed, 0, sizeof(GLOBAL_CONFIG)); memset(parsed, 0, sizeof(GLOBAL_CONFIG));
@ -123,6 +125,48 @@ static HRESULT parse_cmdline(int argc, char *argv[], GLOBAL_CONFIG *parsed)
fputs(helptext, stdout); fputs(helptext, stdout);
return S_FALSE; return S_FALSE;
} }
if (optind < argc)
{
pstr = realpath(argv[optind], NULL); /* implicit strdup */
if (!pstr)
{
Log(LERROR, "Out of memory in parse_cmdline");
return E_OUTOFMEMORY;
}
if (access(pstr, R_OK))
{
fprintf(stderr, "%s: script %s not found\n", argv[0], pstr);
return UPIWIN_E_INVALIDSCRIPT;
}
parsed->script_name = pstr;
if (++optind < argc)
{
parsed->script_arg_count = argc - optind;
pargs = (PPCSTR)malloc(sizeof(PCSTR) * parsed->script_arg_count);
if (!pargs)
{
Log(LERROR, "Out of memory in parse_cmdline");
return E_OUTOFMEMORY;
}
for (c = 0; c < parsed->script_arg_count; c++)
{
pargs[c] = strdup(argv[optind++]);
if (!(pargs[c]))
{
Log(LERROR, "Out of memory in parse_cmdline");
return E_OUTOFMEMORY;
}
}
parsed->script_args = pargs;
}
}
else
{
fprintf(stderr, "%s: no script specified\n", argv[0], c);
return UPIWIN_E_NOSCRIPT;
}
return S_OK; return S_OK;
} }
@ -132,6 +176,10 @@ static void overlay_config(GLOBAL_CONFIG *p)
Gconfig.framebuffer_device = p->framebuffer_device; Gconfig.framebuffer_device = p->framebuffer_device;
if (p->touchscreen_device) if (p->touchscreen_device)
Gconfig.touchscreen_device = p->touchscreen_device; Gconfig.touchscreen_device = p->touchscreen_device;
/* always overlay the script name and arguments */
Gconfig.script_name = p->script_name;
Gconfig.script_arg_count = p->script_arg_count;
Gconfig.script_args = p->script_args;
} }
HRESULT Config_setup(int argc, char *argv[]) HRESULT Config_setup(int argc, char *argv[])

View File

@ -12,6 +12,9 @@ typedef struct tagGLOBAL_CONFIG {
PCSTR python_loc; /* location of the Python3 executable */ PCSTR python_loc; /* location of the Python3 executable */
UINT32 button_debounce; /* minimum time between button up and next button down (ms) */ UINT32 button_debounce; /* minimum time between button up and next button down (ms) */
UINT32 sys_mq_length; /* length of system message queue */ UINT32 sys_mq_length; /* length of system message queue */
PCSTR script_name; /* script name to be run */
INT32 script_arg_count; /* number of arguments to pass to the script */
PPCSTR script_args; /* arguments to pass to the script */
} GLOBAL_CONFIG; } GLOBAL_CONFIG;
extern GLOBAL_CONFIG Gconfig; /* one global configuration to rule them all */ extern GLOBAL_CONFIG Gconfig; /* one global configuration to rule them all */

View File

@ -1,3 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <alloca.h>
#define PY_SSIZE_T_CLEAN #define PY_SSIZE_T_CLEAN
#include <Python.h> #include <Python.h>
#include "scode.h" #include "scode.h"
@ -24,8 +27,7 @@ static void epython_cleanup(void)
UPIWIN_tmp_module = NULL; UPIWIN_tmp_module = NULL;
Py_DECREF(UPIWIN_module); Py_DECREF(UPIWIN_module);
UPIWIN_module = NULL; UPIWIN_module = NULL;
if (!Py_FinalizeEx()) Py_FinalizeEx();
Log(LWARN, "errors encountered when Python uninitialized itself");
PyMem_RawFree(python_name); PyMem_RawFree(python_name);
python_name = NULL; python_name = NULL;
} }
@ -91,3 +93,54 @@ error_0:
python_name = NULL; python_name = NULL;
return hr; return hr;
} }
HRESULT Epython_run(void)
{
HRESULT hr = S_OK;
int i;
FILE *fp;
wchar_t **args;
Log(LINFO, "Ready to execute %s", Gconfig.script_name);
fp = fopen(Gconfig.script_name, "rb");
if (fp)
{
args = (wchar_t **)alloca((Gconfig.script_arg_count + 1) * sizeof(wchar_t *));
memset(args, 0, (Gconfig.script_arg_count + 1) * sizeof(wchar_t *));
args[0] = Py_DecodeLocale(Gconfig.script_name, NULL);
if (args[0])
{
for (i=0; i<Gconfig.script_arg_count; i++)
{
args[i + 1] = Py_DecodeLocale(Gconfig.script_args[i], NULL);
if (!(args[i + 1]))
{
hr = E_OUTOFMEMORY;
break;
}
}
if (SUCCEEDED(hr))
{
PySys_SetArgvEx(Gconfig.script_arg_count + 1, args, 1);
PyRun_SimpleFile(fp, Gconfig.script_name);
}
else
Log(LERROR, "out of memory running script %s", Gconfig.script_name);
}
else
{
Log(LERROR, "out of memory running script %s", Gconfig.script_name);
hr = E_OUTOFMEMORY;
}
for (i = 0; i<(Gconfig.script_arg_count + 1); i++)
if (args[i])
PyMem_RawFree(args[i]);
fclose(fp);
}
else
{
Log(LERROR, "Unable to open script file %s", Gconfig.script_name);
hr = E_ACCESSDENIED;
}
return hr;
}

View File

@ -15,5 +15,6 @@ extern PyObject *Epython_init_upiwin_module(void);
extern PyObject *Epython_init_upiwin_tmp_module(void); extern PyObject *Epython_init_upiwin_tmp_module(void);
extern HRESULT Epython_setup(void); extern HRESULT Epython_setup(void);
extern HRESULT Epython_run(void);
#endif /* __EP_INIT_H_INCLUDED */ #endif /* __EP_INIT_H_INCLUDED */

View File

@ -11,19 +11,6 @@
#include "ep_init.h" #include "ep_init.h"
#include "sysinput.h" #include "sysinput.h"
static void do_draw(void)
{
Fb_filled_rectangle(10, 10, 50, 50, FBPRIMCLR_RED, FALSE);
Fb_filled_rectangle(60, 10, 100, 50, FBPRIMCLR_GREEN, FALSE);
Fb_filled_rectangle(110, 10, 150, 50, FBPRIMCLR_BLUE, FALSE);
Fb_filled_rectangle(10, 60, 50, 100, FBPRIMCLR_CYAN, FALSE);
Fb_filled_rectangle(60, 60, 100, 100, FBPRIMCLR_MAGENTA, FALSE);
Fb_filled_rectangle(110, 60, 150, 100, FBPRIMCLR_YELLOW, FALSE);
Fb_rectangle(10, 110, 150, 150, FBPRIMCLR_WHITE, FALSE);
Fb_line(10, 110, 150, 150, FBPRIMCLR_WHITE, FALSE);
Fb_line(10, 150, 150, 110, FBPRIMCLR_WHITE, FALSE);
}
static void log_touch(const char *event, UINT_PTR x, UINT_PTR y) static void log_touch(const char *event, UINT_PTR x, UINT_PTR y)
{ {
Log(LINFO, "Touch %s at (%u, %u)", event, x, y); Log(LINFO, "Touch %s at (%u, %u)", event, x, y);
@ -54,10 +41,10 @@ int main(int argc, char *argv[])
sleep(2); /* wait to show off splash screen */ sleep(2); /* wait to show off splash screen */
Fb_clear(); Fb_clear();
/* temporary drawing here */ if (FAILED(Epython_run()))
do_draw(); return EXIT_FAILURE;
Log(LINFO, "System ready."); Log(LINFO, "Script returned and event loop ready.");
while (running) while (running)
{ {

View File

@ -64,6 +64,7 @@
#define E_UNEXPECTED SCODE_CAST(0x8000FFFF) /* unexpected error */ #define E_UNEXPECTED SCODE_CAST(0x8000FFFF) /* unexpected error */
/* UPIWIN-specific errorcodes */ /* UPIWIN-specific errorcodes */
#define UPIWIN_E_INVALIDSTRING SCODE_CAST(0x80060000) /* invalid string (decode error) */ #define UPIWIN_E_INVALIDSCRIPT SCODE_CAST(0x80060000) /* invalid script file */
#define UPIWIN_E_NOSCRIPT SCODE_CAST(0x80060001) /* no script specified */
#endif /* __SCODE_H_INCLUDED */ #endif /* __SCODE_H_INCLUDED */

View File

@ -117,6 +117,8 @@ typedef const CHAR *PCSTR;
typedef WCHAR *PWSTR; typedef WCHAR *PWSTR;
typedef const WCHAR *PCWSTR; typedef const WCHAR *PCWSTR;
typedef PCSTR *PPCSTR;
/* Boolean type */ /* Boolean type */
typedef int BOOL; typedef int BOOL;