pyxytronic.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Xytronic LF-1600
  3. * Open Source firmware
  4. * Simulator Python interface
  5. *
  6. * Copyright (c) 2018 Michael Buesch <m@bues.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include "simulator.h"
  23. extern "C" {
  24. #include <Python.h>
  25. static PyObject * xy_simulator_setting_write(PyObject *self, PyObject *args)
  26. {
  27. int value = 0;
  28. char *name = NULL;
  29. bool ok;
  30. if (!PyArg_ParseTuple(args, "si", &name, &value))
  31. return NULL;
  32. ok = simulator_setting_access(name, &value, true);
  33. if (!ok) {
  34. PyErr_SetString(PyExc_RuntimeError, "simulator_settings_access() failed");
  35. return NULL;
  36. }
  37. Py_RETURN_NONE;
  38. }
  39. static PyObject * xy_simulator_setting_read(PyObject *self, PyObject *args)
  40. {
  41. int value = 0;
  42. char *name = NULL;
  43. bool ok;
  44. PyObject *retValue;
  45. if (!PyArg_ParseTuple(args, "s", &name))
  46. return NULL;
  47. ok = simulator_setting_access(name, &value, false);
  48. if (!ok) {
  49. PyErr_SetString(PyExc_RuntimeError, "simulator_settings_access() failed");
  50. return NULL;
  51. }
  52. retValue = Py_BuildValue("i", value);
  53. return retValue;
  54. }
  55. static PyObject * xy_simulator_adc_set(PyObject *self, PyObject *args)
  56. {
  57. int adc_index;
  58. unsigned int adc_value;
  59. bool ok;
  60. if (!PyArg_ParseTuple(args, "iI", &adc_index, &adc_value))
  61. return NULL;
  62. ok = simulator_adc_set(adc_index, std::min(adc_value, (unsigned int)UINT16_MAX));
  63. if (!ok) {
  64. PyErr_SetString(PyExc_RuntimeError, "simulator_adc_set() failed");
  65. return NULL;
  66. }
  67. Py_RETURN_NONE;
  68. }
  69. static PyObject * xy_simulator_pwm_get(PyObject *self, PyObject *args)
  70. {
  71. int pwm_index;
  72. uint16_t value, max_value;
  73. bool ok;
  74. PyObject *retTuple;
  75. if (!PyArg_ParseTuple(args, "i", &pwm_index))
  76. return NULL;
  77. ok = simulator_pwm_get(pwm_index, &value, &max_value);
  78. if (!ok) {
  79. PyErr_SetString(PyExc_RuntimeError, "simulator_pwm_get() failed");
  80. return NULL;
  81. }
  82. retTuple = Py_BuildValue("II", (unsigned int)value, (unsigned int)max_value);
  83. return retTuple;
  84. }
  85. static PyObject * xy_simulator_uart_get_tx(PyObject *self, PyObject *args)
  86. {
  87. uint8_t buf[4096];
  88. size_t count;
  89. PyObject *bytes;
  90. count = simulator_uart_get_tx(buf, sizeof(buf));
  91. bytes = PyBytes_FromStringAndSize((char *)buf, count);
  92. return bytes;
  93. }
  94. static PyObject * xy_simulator_stats_ena(PyObject *self, PyObject *args)
  95. {
  96. int mainloop_stats_ena;
  97. if (!PyArg_ParseTuple(args, "p", &mainloop_stats_ena))
  98. return NULL;
  99. simulator_stats_ena(mainloop_stats_ena);
  100. Py_RETURN_NONE;
  101. }
  102. static PyObject * xy_simulator_mainloop_once(PyObject *self, PyObject *args)
  103. {
  104. simulator_mainloop_once();
  105. Py_RETURN_NONE;
  106. }
  107. static PyObject * xy_simulator_exit(PyObject *self, PyObject *args)
  108. {
  109. simulator_exit();
  110. Py_RETURN_NONE;
  111. }
  112. static PyObject * xy_simulator_init(PyObject *self, PyObject *args)
  113. {
  114. simulator_init();
  115. Py_RETURN_NONE;
  116. }
  117. static void xy_free(void *arg)
  118. {
  119. simulator_exit();
  120. }
  121. static PyMethodDef xy_methods[] = {
  122. { "simulator_setting_write", xy_simulator_setting_write, METH_VARARGS, "", },
  123. { "simulator_setting_read", xy_simulator_setting_read, METH_VARARGS, "", },
  124. { "simulator_adc_set", xy_simulator_adc_set, METH_VARARGS, "", },
  125. { "simulator_pwm_get", xy_simulator_pwm_get, METH_VARARGS, "", },
  126. { "simulator_uart_get_tx", xy_simulator_uart_get_tx, METH_NOARGS, "", },
  127. { "simulator_stats_ena", xy_simulator_stats_ena, METH_VARARGS, "", },
  128. { "simulator_mainloop_once", xy_simulator_mainloop_once, METH_NOARGS, "", },
  129. { "simulator_exit", xy_simulator_exit, METH_NOARGS, "", },
  130. { "simulator_init", xy_simulator_init, METH_NOARGS, "", },
  131. { NULL, NULL, 0, NULL, },
  132. };
  133. static struct PyModuleDef xy_module = {
  134. PyModuleDef_HEAD_INIT,
  135. "pyxytronic", /* m_name */
  136. NULL, /* m_doc */
  137. -1, /* m_size */
  138. xy_methods, /* m_methods */
  139. NULL, /* m_slots */
  140. NULL, /* m_traverse */
  141. NULL, /* m_clear */
  142. xy_free, /* m_free */
  143. };
  144. PyMODINIT_FUNC PyInit_pyxytronic(void)
  145. {
  146. PyObject *mod;
  147. mod = PyModule_Create(&xy_module);
  148. if (!mod)
  149. return NULL;
  150. return mod;
  151. }
  152. } /* extern "C" */