machine_pinbase.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 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 "py/obj.h"
  29. #include "py/runtime.h"
  30. #include "extmod/virtpin.h"
  31. #include "extmod/machine_pinbase.h"
  32. // PinBase class
  33. // As this is abstract class, its instance is null.
  34. // But there should be an instance, as the rest of instance code
  35. // expects that there will be concrete object for inheritance.
  36. typedef struct _mp_pinbase_t {
  37. mp_obj_base_t base;
  38. } mp_pinbase_t;
  39. STATIC const mp_pinbase_t pinbase_singleton = {
  40. .base = { &machine_pinbase_type },
  41. };
  42. STATIC mp_obj_t pinbase_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  43. (void)type;
  44. (void)n_args;
  45. (void)n_kw;
  46. (void)args;
  47. return MP_OBJ_FROM_PTR(&pinbase_singleton);
  48. }
  49. mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
  50. mp_uint_t pinbase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
  51. (void)errcode;
  52. switch (request) {
  53. case MP_PIN_READ: {
  54. mp_obj_t dest[2];
  55. mp_load_method(obj, MP_QSTR_value, dest);
  56. return mp_obj_get_int(mp_call_method_n_kw(0, 0, dest));
  57. }
  58. case MP_PIN_WRITE: {
  59. mp_obj_t dest[3];
  60. mp_load_method(obj, MP_QSTR_value, dest);
  61. dest[2] = (arg == 0 ? mp_const_false : mp_const_true);
  62. mp_call_method_n_kw(1, 0, dest);
  63. return 0;
  64. }
  65. }
  66. return -1;
  67. }
  68. STATIC const mp_pin_p_t pinbase_pin_p = {
  69. .ioctl = pinbase_ioctl,
  70. };
  71. const mp_obj_type_t machine_pinbase_type = {
  72. { &mp_type_type },
  73. .name = MP_QSTR_PinBase,
  74. .make_new = pinbase_make_new,
  75. .protocol = &pinbase_pin_p,
  76. };
  77. #endif // MICROPY_PY_MACHINE