machine_signal.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Paul Sokolovsky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpconfig.h"
  27. #if MICROPY_PY_MACHINE
  28. #include <string.h>
  29. #include "py/obj.h"
  30. #include "py/runtime.h"
  31. #include "extmod/virtpin.h"
  32. #include "extmod/machine_signal.h"
  33. // Signal class
  34. typedef struct _machine_signal_t {
  35. mp_obj_base_t base;
  36. mp_obj_t pin;
  37. bool invert;
  38. } machine_signal_t;
  39. STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  40. mp_obj_t pin = args[0];
  41. bool invert = false;
  42. #if defined(MICROPY_PY_MACHINE_PIN_MAKE_NEW)
  43. mp_pin_p_t *pin_p = NULL;
  44. if (MP_OBJ_IS_OBJ(pin)) {
  45. mp_obj_base_t *pin_base = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
  46. pin_p = (mp_pin_p_t*)pin_base->type->protocol;
  47. }
  48. if (pin_p == NULL) {
  49. // If first argument isn't a Pin-like object, we filter out "invert"
  50. // from keyword arguments and pass them all to the exported Pin
  51. // constructor to create one.
  52. mp_obj_t *pin_args = alloca(n_args + n_kw * 2);
  53. memcpy(pin_args, args, n_args * sizeof(mp_obj_t));
  54. const mp_obj_t *src = args + n_args;
  55. mp_obj_t *dst = pin_args + n_args;
  56. mp_obj_t *sig_value = NULL;
  57. for (size_t cnt = n_kw; cnt; cnt--) {
  58. if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_invert)) {
  59. invert = mp_obj_is_true(src[1]);
  60. n_kw--;
  61. } else {
  62. *dst++ = *src;
  63. *dst++ = src[1];
  64. }
  65. if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_value)) {
  66. // Value is pertained to Signal, so we should invert
  67. // it for Pin if needed, and we should do it only when
  68. // inversion status is guaranteedly known.
  69. sig_value = dst - 1;
  70. }
  71. src += 2;
  72. }
  73. if (invert && sig_value != NULL) {
  74. *sig_value = mp_obj_is_true(*sig_value) ? MP_OBJ_NEW_SMALL_INT(0) : MP_OBJ_NEW_SMALL_INT(1);
  75. }
  76. // Here we pass NULL as a type, hoping that mp_pin_make_new()
  77. // will just ignore it as set a concrete type. If not, we'd need
  78. // to expose port's "default" pin type too.
  79. pin = MICROPY_PY_MACHINE_PIN_MAKE_NEW(NULL, n_args, n_kw, pin_args);
  80. }
  81. else
  82. #endif
  83. // Otherwise there should be 1 or 2 args
  84. {
  85. if (n_args == 1) {
  86. if (n_kw == 0) {
  87. } else if (n_kw == 1 && args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_invert)) {
  88. invert = mp_obj_is_true(args[2]);
  89. } else {
  90. goto error;
  91. }
  92. } else {
  93. error:
  94. mp_raise_TypeError(NULL);
  95. }
  96. }
  97. machine_signal_t *o = m_new_obj(machine_signal_t);
  98. o->base.type = type;
  99. o->pin = pin;
  100. o->invert = invert;
  101. return MP_OBJ_FROM_PTR(o);
  102. }
  103. STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  104. (void)errcode;
  105. machine_signal_t *self = MP_OBJ_TO_PTR(self_in);
  106. switch (request) {
  107. case MP_PIN_READ: {
  108. return mp_virtual_pin_read(self->pin) ^ self->invert;
  109. }
  110. case MP_PIN_WRITE: {
  111. mp_virtual_pin_write(self->pin, arg ^ self->invert);
  112. return 0;
  113. }
  114. }
  115. return -1;
  116. }
  117. // fast method for getting/setting signal value
  118. STATIC mp_obj_t signal_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  119. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  120. if (n_args == 0) {
  121. // get pin
  122. return MP_OBJ_NEW_SMALL_INT(mp_virtual_pin_read(self_in));
  123. } else {
  124. // set pin
  125. mp_virtual_pin_write(self_in, mp_obj_is_true(args[0]));
  126. return mp_const_none;
  127. }
  128. }
  129. STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) {
  130. return signal_call(args[0], n_args - 1, 0, args + 1);
  131. }
  132. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value);
  133. STATIC mp_obj_t signal_on(mp_obj_t self_in) {
  134. mp_virtual_pin_write(self_in, 1);
  135. return mp_const_none;
  136. }
  137. STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_on_obj, signal_on);
  138. STATIC mp_obj_t signal_off(mp_obj_t self_in) {
  139. mp_virtual_pin_write(self_in, 0);
  140. return mp_const_none;
  141. }
  142. STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_off_obj, signal_off);
  143. STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = {
  144. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) },
  145. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&signal_on_obj) },
  146. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&signal_off_obj) },
  147. };
  148. STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table);
  149. STATIC const mp_pin_p_t signal_pin_p = {
  150. .ioctl = signal_ioctl,
  151. };
  152. const mp_obj_type_t machine_signal_type = {
  153. { &mp_type_type },
  154. .name = MP_QSTR_Signal,
  155. .make_new = signal_make_new,
  156. .call = signal_call,
  157. .protocol = &signal_pin_p,
  158. .locals_dict = (void*)&signal_locals_dict,
  159. };
  160. #endif // MICROPY_PY_MACHINE