diff --git a/src/Makefile b/src/Makefile index 2d6dfaa..689392e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -OBJS=main.o sysinput.o ep_init.o ep_util.o fbinit.o fbprimitive.o log.o gpio.o msg_queue.o \ +OBJS=main.o sysinput.o ep_init.o ep_upiwin.o ep_backlight.o ep_util.o fbinit.o fbprimitive.o log.o gpio.o msg_queue.o \ time_func.o config.o splash.o LIBS=-lpython3.7m -lcrypt -lbcm2835 -lpthread -ldl -lutil -lm CFLAGS=-I/usr/include/python3.7m -Wall -fstack-protector -fwrapv -fno-PIE -g -O3 -DDEBUG_ASSERT diff --git a/src/ep_backlight.c b/src/ep_backlight.c new file mode 100644 index 0000000..04ccec9 --- /dev/null +++ b/src/ep_backlight.c @@ -0,0 +1,63 @@ +#define PY_SSIZE_T_CLEAN +#include +#include "gpio.h" +#include "ep_upiwin.h" +#include "ep_init.h" + +PyObject *Epython_get_backlight(PyObject *self, PyObject *args) +{ + PUPIWIN_STATE pstate; + + if (!PyArg_ParseTuple(args, "")) + return NULL; + pstate = (PUPIWIN_STATE)PyModule_GetState(UPIWIN_module); + return PyBool_FromLong((long)(pstate->backlight_on)); +} + +PyObject *Epython_set_backlight(PyObject *self, PyObject *args) +{ + PUPIWIN_STATE pstate; + int new_state; + + if (!PyArg_ParseTuple(args, "p", &new_state)) + return NULL; + pstate = (PUPIWIN_STATE)PyModule_GetState(UPIWIN_module); + if (new_state && !(pstate->backlight_on)) + Gpio_set_backlight(pstate->backlight_level); + else if (!new_state && pstate->backlight_on) + Gpio_set_backlight(0); + pstate->backlight_on = MAKEBOOL(new_state); + return PyLong_FromUnsignedLong(S_OK); +} + +PyObject *Epython_get_backlight_level(PyObject *self, PyObject *args) +{ + PUPIWIN_STATE pstate; + + if (!PyArg_ParseTuple(args, "")) + return NULL; + pstate = (PUPIWIN_STATE)PyModule_GetState(UPIWIN_module); + return PyLong_FromUnsignedLong(pstate->backlight_level); +} + +PyObject *Epython_set_backlight_level(PyObject *self, PyObject *args) +{ + PUPIWIN_STATE pstate; + UINT32 new_level; + HRESULT hr = S_OK; + + if (!PyArg_ParseTuple(args, "k", &new_level)) + return NULL; + if (new_level <= GSB_BACKLIGHT_MAX) + { + pstate = (PUPIWIN_STATE)PyModule_GetState(UPIWIN_module); + if (pstate->backlight_on) + Gpio_set_backlight(new_level); + pstate->backlight_level = new_level; + } + else + { + hr = E_INVALIDARG; + } + return PyLong_FromUnsignedLong(hr); +} diff --git a/src/ep_init.c b/src/ep_init.c index a61dd9f..2467a59 100644 --- a/src/ep_init.c +++ b/src/ep_init.c @@ -11,30 +11,40 @@ const PCSTR Mod_UPIWIN_tmp = "upiwin_tmp"; /* name of the temporary UPIWIN static wchar_t *python_name = NULL; /* location of the Python executable */ -static PyObject *upiwin_module = NULL; -static PyObject *upiwin_tmp_module = NULL; +PyObject *UPIWIN_module = NULL; +PyObject *UPIWIN_tmp_module = NULL; -static PyObject *init_upiwin_module(void) -{ -} +static PyModuleDef DefUPIWIN_tmp = { + PyModuleDef_HEAD_INIT, /* standard garbage */ + Mod_UPIWIN, /* module name */ + NULL, /* no doc string */ + -1, /* no per-module memory */ + NULL, /* method defs will be added later */ + NULL, /* no slots for multi-phase init */ + NULL, /* no traversal proc */ + NULL, /* no clear function */ + NULL /* no free function */ +}; static PyObject *init_upiwin_tmp_module(void) { + PyObject *module = PyModule_Create(&DefUPIWIN_tmp); + return module; } /* used to link the two modules into Python's init table */ static struct _inittab upiwin_inittab[] = { - { Mod_UPIWIN, init_upiwin_module }, - { Mod_UPIWIN_tmp, init_upiwin_tmp_module }, - { NULL, NULL } + { Mod_UPIWIN, Epython_init_upiwin_module }, + { Mod_UPIWIN_tmp, init_upiwin_tmp_module }, + { NULL, NULL } }; static void epython_cleanup(void) { - Py_DECREF(upiwin_tmp_module); - upiwin_tmp_module = NULL; - Py_DECREF(upiwin_module); - upiwin_module = NULL; + Py_DECREF(UPIWIN_tmp_module); + UPIWIN_tmp_module = NULL; + Py_DECREF(UPIWIN_module); + UPIWIN_module = NULL; if (!Py_FinalizeEx()) Log(LWARN, "errors encountered when Python uninitialized itself"); PyMem_RawFree(python_name); @@ -72,15 +82,15 @@ HRESULT Epython_setup(void) Py_Initialize(); /* Import the modules */ - upiwin_module = PyImport_ImportModule(Mod_UPIWIN); - if (!upiwin_module) + UPIWIN_module = PyImport_ImportModule(Mod_UPIWIN); + if (!UPIWIN_module) { Log(LFATAL, "error importing the upiwin module"); hr = Epython_trace_exception(); goto error_1; } - upiwin_tmp_module = PyImport_ImportModule(Mod_UPIWIN_tmp); - if (!upiwin_tmp_module) + UPIWIN_tmp_module = PyImport_ImportModule(Mod_UPIWIN_tmp); + if (!UPIWIN_tmp_module) { Log(LFATAL, "error importing the upiwin_tmp module"); hr = Epython_trace_exception(); @@ -93,8 +103,8 @@ HRESULT Epython_setup(void) return hr; error_2: - Py_DECREF(upiwin_module); - upiwin_module = NULL; + Py_DECREF(UPIWIN_module); + UPIWIN_module = NULL; error_1: Py_FinalizeEx(); error_0: diff --git a/src/ep_init.h b/src/ep_init.h index 3ef391c..8d12d97 100644 --- a/src/ep_init.h +++ b/src/ep_init.h @@ -1,11 +1,18 @@ #ifndef __EP_INIT_H_INCLUDED #define __EP_INIT_H_INCLUDED +#define PY_SSIZE_T_CLEAN +#include #include "wintype.h" extern const PCSTR Mod_UPIWIN; extern const PCSTR Mod_UPIWIN_tmp; +extern PyObject *UPIWIN_module; +extern PyObject *UPIWIN_tmp_module; + +extern PyObject *Epython_init_upiwin_module(void); + extern HRESULT Epython_setup(void); #endif /* __EP_INIT_H_INCLUDED */ diff --git a/src/ep_upiwin.c b/src/ep_upiwin.c new file mode 100644 index 0000000..0434d3c --- /dev/null +++ b/src/ep_upiwin.c @@ -0,0 +1,48 @@ +#define PY_SSIZE_T_CLEAN +#include +#include "scode.h" +#include "ep_init.h" +#include "gpio.h" +#include "ep_upiwin.h" + +static PyMethodDef UPIWINMethods[] = { + /* Backlight control functions */ + {"get_backlight", Epython_get_backlight, METH_VARARGS, + "Returns the current status of the backlight (True=on, False=off)."}, + {"set_backlight", Epython_set_backlight, METH_VARARGS, + "Sets the current status of the backlight (True=on, False=off). Returns a SCODE."}, + {"get_backlight_level", Epython_get_backlight_level, METH_VARARGS, + "Returns the current intensity level of the backlight."}, + {"set_backlight_level", Epython_set_backlight_level, METH_VARARGS, + "Sets the current intensity level of the backlight. Returns a SCODE."}, + {NULL, NULL, 0, NULL} +}; + +static PyModuleDef DefUPIWIN = { + PyModuleDef_HEAD_INIT, /* standard garbage */ + Mod_UPIWIN, /* module name */ + NULL, /* no doc string */ + sizeof(UPIWIN_STATE), /* per-module memory */ + UPIWINMethods, /* method defs */ + NULL, /* no slots for multi-phase init */ + NULL, /* no traversal proc */ + NULL, /* no clear function */ + NULL /* no free function */ +}; + +PyObject *Epython_init_upiwin_module(void) +{ + PyObject *module; + PUPIWIN_STATE pstate; + + module = PyModule_Create(&DefUPIWIN); + if (!module) + return NULL; + + /* set up the module state */ + pstate = (PUPIWIN_STATE)PyModule_GetState(module); + pstate->backlight_on = TRUE; + pstate->backlight_level = GSB_BACKLIGHT_DEFAULT; + + return module; +} diff --git a/src/ep_upiwin.h b/src/ep_upiwin.h new file mode 100644 index 0000000..fa41b43 --- /dev/null +++ b/src/ep_upiwin.h @@ -0,0 +1,19 @@ +#ifndef __EP_UPIWIN_H_INCLUDED +#define __EP_UPIWIN_H_INCLUDED + +#define PY_SSIZE_T_CLEAN +#include +#include "wintype.h" + +typedef struct tagUPIWIN_STATE { + BOOL backlight_on; + UINT32 backlight_level; +} UPIWIN_STATE, *PUPIWIN_STATE; + +/* method definitions go here */ +extern PyObject *Epython_get_backlight(PyObject *self, PyObject *args); +extern PyObject *Epython_set_backlight(PyObject *self, PyObject *args); +extern PyObject *Epython_get_backlight_level(PyObject *self, PyObject *args); +extern PyObject *Epython_set_backlight_level(PyObject *self, PyObject *args); + +#endif /* __EP_UPIWIN_H_INCLUDED */ diff --git a/src/gpio.c b/src/gpio.c index dfe0710..ff877c4 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -61,7 +61,7 @@ HRESULT Gpio_setup(void) bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2); bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 1); bcm2835_pwm_set_range(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX + 1); - bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX); + bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_DEFAULT); hr = Config_exitfunc(do_cleanup); if (FAILED(hr)) diff --git a/src/gpio.h b/src/gpio.h index 707393a..e368291 100644 --- a/src/gpio.h +++ b/src/gpio.h @@ -13,6 +13,8 @@ #define GSB_BACKLIGHT_MAX 1023 /* maximum level for backlight */ +#define GSB_BACKLIGHT_DEFAULT GSB_BACKLIGHT_MAX + extern HRESULT Gpio_setup(void); extern UINT32 Gpio_read_buttons(void); extern void Gpio_set_backlight(UINT32 level);