From b7b244209eaad5396e08fb9d9dfc4057a1a75dea Mon Sep 17 00:00:00 2001 From: Goten Xiao Date: Thu, 22 May 2008 21:40:48 +0100 Subject: [PATCH] setoffs and settext now confirmed working, although setoffs still needs some work... --- PyX52.c | 60 ++++++++++++++++++++++++++++++++++++++++++------------------ x52.py | 17 +++++++++++++++-- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/PyX52.c b/PyX52.c index 4e87489..fb319f1 100644 --- a/PyX52.c +++ b/PyX52.c @@ -42,10 +42,10 @@ enum x52pro_buttons X52PRO_BUTTON_MFD_FUNCTION, X52PRO_BUTTON_MFD_STARTSTOP, X52PRO_BUTTON_MFD_RESET, - X52PRO_BUTTON_MFD_PGUP, - X52PRO_BUTTON_MFD_PGDN, - X52PRO_BUTTON_MFD_SELUP, - X52PRO_BUTTON_MFD_SELDN, + X52PRO_BUTTON_MFD_PAGE_UP, + X52PRO_BUTTON_MFD_PAGE_DOWN, + X52PRO_BUTTON_MFD_SEL_UP, + X52PRO_BUTTON_MFD_SEL_DOWN, X52PRO_BUTTON_MFD_SEL }; @@ -72,8 +72,6 @@ static PyObject *PyX52_X52_setoffs(PyX52_X52_object *, PyObject *, PyObject *); static PyObject *PyX52_X52_setsecond(PyX52_X52_object *, PyObject *, PyObject *); static PyObject *PyX52_X52_setdate(PyX52_X52_object *, PyObject *, PyObject *); - - static PyObject * PyX52_X52_new(PyTypeObject *type) { @@ -133,7 +131,7 @@ PyX52_X52_settext(PyX52_X52_object *self, PyObject *args, PyObject *kws) PyObject *line = Py_BuildValue("s", ""); static char *kwlist[] = {"line_no", "line", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kws, "iO", kwlist, &line_no, line)) + if (!PyArg_ParseTupleAndKeywords(args, kws, "iO", kwlist, &line_no, &line)) return NULL; if (line_no < 0 || line_no > 2) @@ -141,16 +139,23 @@ PyX52_X52_settext(PyX52_X52_object *self, PyObject *args, PyObject *kws) PyErr_SetString(PyExc_AttributeError, "line_no must be in the range 0-2."); return NULL; } + if (!PyString_Check(line)) { PyErr_SetString(PyExc_AttributeError, "line must be a string or subtype of string."); return NULL; } - char *line_trunc = (char *)malloc(sizeof(char)*17); - memset(line_trunc, ' ', 16); - char *line_buf = PyString_AsString(line); - strncpy(line_trunc, line_buf, 16); + char *line_buf; + if (PyString_Size(line) > 0) + { + line_buf = (char *)malloc(sizeof(char)*17); + memset(line_buf, '\0', 16); + strncpy(line_buf, PyString_AsString(line), 17); + } else + { + line_buf = ""; + } if (x52_settext(self->handle, line_no, line_buf, strlen(line_buf)) != 0) { @@ -158,7 +163,6 @@ PyX52_X52_settext(PyX52_X52_object *self, PyObject *args, PyObject *kws) return NULL; } - Py_DECREF(line); Py_INCREF(Py_None); return Py_None; } @@ -287,13 +291,33 @@ PyX52_X52_setoffs(PyX52_X52_object *self, PyObject *args, PyObject *kws) return NULL; } - int offs2 = 0; - int offs3 = 0; + int offs2 = NULL; + int offs3 = NULL; static char *kwlist[] = {"offs2", "offs3", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kws, "|ii", kwlist, &offs2, &offs3)) return NULL; + int inv2, inv3 = 0; + if (offs2 != NULL) + { + if (offs2 < 0) + { + inv2 = 1; + offs2 *= -1; + } + x52_setoffs(self->handle, 0, 1, inv2, offs2); + } + if (offs3 != NULL) + { + if (offs3 < 0) + { + inv3 = 1; + offs3 *= -1; + } + x52_setoffs(self->handle, 1, 1, inv3, offs3); + } + Py_INCREF(Py_None); return Py_None; } @@ -485,10 +509,10 @@ initPyX52(void) PyModule_AddIntConstant(m, "BUTTON_MFD_FUNCTION", X52PRO_BUTTON_MFD_FUNCTION); PyModule_AddIntConstant(m, "BUTTON_MFD_STARTSTOP", X52PRO_BUTTON_MFD_STARTSTOP); PyModule_AddIntConstant(m, "BUTTON_MFD_RESET", X52PRO_BUTTON_MFD_RESET); - PyModule_AddIntConstant(m, "BUTTON_MFD_PGUP", X52PRO_BUTTON_MFD_PGUP); - PyModule_AddIntConstant(m, "BUTTON_MFD_PGDN", X52PRO_BUTTON_MFD_PGDN); - PyModule_AddIntConstant(m, "BUTTON_MFD_SELUP", X52PRO_BUTTON_MFD_SELUP); - PyModule_AddIntConstant(m, "BUTTON_MFD_SELDN", X52PRO_BUTTON_MFD_SELDN); + PyModule_AddIntConstant(m, "BUTTON_MFD_PAGE_UP", X52PRO_BUTTON_MFD_PAGE_UP); + PyModule_AddIntConstant(m, "BUTTON_MFD_PAGE_DN", X52PRO_BUTTON_MFD_PAGE_DOWN); + PyModule_AddIntConstant(m, "BUTTON_MFD_SEL_UP", X52PRO_BUTTON_MFD_SEL_UP); + PyModule_AddIntConstant(m, "BUTTON_MFD_SEL_DOWN", X52PRO_BUTTON_MFD_SEL_DOWN); PyModule_AddIntConstant(m, "BUTTON_MFD_SEL", X52PRO_BUTTON_MFD_SEL); PyModule_AddIntConstant(m, "TYPE_X52", DEV_X52); diff --git a/x52.py b/x52.py index 2ed6c41..f7654e6 100755 --- a/x52.py +++ b/x52.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from os import system from traceback import print_exc +import time build = system("setup.py build") @@ -31,35 +32,47 @@ try: print "Setting MFD brightness... ", x52.setbri(0, 127) + time.sleep(0.1) x52.setbri(0, 63) + time.sleep(0.1) x52.setbri(0, 0) + time.sleep(0.1) x52.setbri(0, 127) print "[ OK ]" print "Setting LED brightness... ", x52.setbri(1, 127) + time.sleep(0.1) x52.setbri(1, 63) + time.sleep(0.1) x52.setbri(1, 0) + time.sleep(0.1) x52.setbri(1, 127) print "[ OK ]" print "Setting text... ", x52.settext(0, "1234567890123456") + time.sleep(0.1) x52.settext(1, "1234567890123456") + time.sleep(0.1) x52.settext(2, "1234567890123456") + time.sleep(0.1) x52.settext(0, "") + time.sleep(0.1) x52.settext(1, "") + time.sleep(0.1) x52.settext(2, "") print "[ OK ]" print "Setting LED states... ", - for i in range(PyX52.LED_FIRE, PyX52.LED_IGREEN): + for i in range(PyX52.LED_FIRE, PyX52.LED_IGREEN+1): x52.setled(i, 1) + time.sleep(0.1) x52.setled(i, 0) print "[ OK ]" print "Setting time offset... ", - x52.setoffs(1, -5) + x52.setoffs(60, -5*60) print "[ OK ]" except: print "[ FAIL ]" -- 2.11.4.GIT