machine_signal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 = mp_local_alloc((n_args + n_kw * 2) * sizeof(mp_obj_t));
  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. mp_local_free(pin_args);
  81. }
  82. else
  83. #endif
  84. // Otherwise there should be 1 or 2 args
  85. {
  86. if (n_args == 1) {
  87. if (n_kw == 0) {
  88. } else if (n_kw == 1 && args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_invert)) {
  89. invert = mp_obj_is_true(args[2]);
  90. } else {
  91. goto error;
  92. }
  93. } else {
  94. error:
  95. mp_raise_TypeError(NULL);
  96. }
  97. }
  98. machine_signal_t *o = m_new_obj(machine_signal_t);
  99. o->base.type = type;
  100. o->pin = pin;
  101. o->invert = invert;
  102. return MP_OBJ_FROM_PTR(o);
  103. }
  104. STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  105. (void)errcode;
  106. machine_signal_t *self = MP_OBJ_TO_PTR(self_in);
  107. switch (request) {
  108. case MP_PIN_READ: {
  109. return mp_virtual_pin_read(self->pin) ^ self->invert;
  110. }
  111. case MP_PIN_WRITE: {
  112. mp_virtual_pin_write(self->pin, arg ^ self->invert);
  113. return 0;
  114. }
  115. }
  116. return -1;
  117. }
  118. // fast method for getting/setting signal value
  119. STATIC mp_obj_t signal_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  120. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  121. if (n_args == 0) {
  122. // get pin
  123. return MP_OBJ_NEW_SMALL_INT(mp_virtual_pin_read(self_in));
  124. } else {
  125. // set pin
  126. mp_virtual_pin_write(self_in, mp_obj_is_true(args[0]));
  127. return mp_const_none;
  128. }
  129. }
  130. STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) {
  131. return signal_call(args[0], n_args - 1, 0, args + 1);
  132. }
  133. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value);
  134. STATIC mp_obj_t signal_on(mp_obj_t self_in) {
  135. mp_virtual_pin_write(self_in, 1);
  136. return mp_const_none;
  137. }
  138. STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_on_obj, signal_on);
  139. STATIC mp_obj_t signal_off(mp_obj_t self_in) {
  140. mp_virtual_pin_write(self_in, 0);
  141. return mp_const_none;
  142. }
  143. STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_off_obj, signal_off);
  144. STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = {
  145. { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) },
  146. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&signal_on_obj) },
  147. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&signal_off_obj) },
  148. };
  149. STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table);
  150. STATIC const mp_pin_p_t signal_pin_p = {
  151. .ioctl = signal_ioctl,
  152. };
  153. const mp_obj_type_t machine_signal_type = {
  154. { &mp_type_type },
  155. .name = MP_QSTR_Signal,
  156. .make_new = signal_make_new,
  157. .call = signal_call,
  158. .protocol = &signal_pin_p,
  159. .locals_dict = (void*)&signal_locals_dict,
  160. };
  161. #endif // MICROPY_PY_MACHINE