modpybled.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Damien P. George
  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/runtime.h"
  27. #include "board.h"
  28. #include "modpyb.h"
  29. typedef struct _pyb_led_obj_t {
  30. mp_obj_base_t base;
  31. } pyb_led_obj_t;
  32. STATIC const pyb_led_obj_t pyb_led_obj[] = {
  33. {{&pyb_led_type}},
  34. {{&pyb_led_type}},
  35. {{&pyb_led_type}},
  36. };
  37. #define NUM_LED MP_ARRAY_SIZE(pyb_led_obj)
  38. #define LED_ID(obj) ((obj) - &pyb_led_obj[0] + 1)
  39. void pyb_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  40. pyb_led_obj_t *self = self_in;
  41. mp_printf(print, "LED(%u)", LED_ID(self));
  42. }
  43. STATIC mp_obj_t pyb_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  44. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  45. mp_int_t led_id = mp_obj_get_int(args[0]);
  46. if (!(1 <= led_id && led_id <= NUM_LED)) {
  47. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED %d does not exist", led_id));
  48. }
  49. return (mp_obj_t)&pyb_led_obj[led_id - 1];
  50. }
  51. mp_obj_t pyb_led_on(mp_obj_t self_in) {
  52. pyb_led_obj_t *self = self_in;
  53. led_state(LED_ID(self), 1);
  54. return mp_const_none;
  55. }
  56. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_led_on_obj, pyb_led_on);
  57. mp_obj_t pyb_led_off(mp_obj_t self_in) {
  58. pyb_led_obj_t *self = self_in;
  59. led_state(LED_ID(self), 0);
  60. return mp_const_none;
  61. }
  62. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_led_off_obj, pyb_led_off);
  63. mp_obj_t pyb_led_toggle(mp_obj_t self_in) {
  64. pyb_led_obj_t *self = self_in;
  65. led_toggle(LED_ID(self));
  66. return mp_const_none;
  67. }
  68. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_led_toggle_obj, pyb_led_toggle);
  69. STATIC const mp_rom_map_elem_t pyb_led_locals_dict_table[] = {
  70. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&pyb_led_on_obj) },
  71. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&pyb_led_off_obj) },
  72. { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&pyb_led_toggle_obj) },
  73. };
  74. STATIC MP_DEFINE_CONST_DICT(pyb_led_locals_dict, pyb_led_locals_dict_table);
  75. const mp_obj_type_t pyb_led_type = {
  76. { &mp_type_type },
  77. .name = MP_QSTR_LED,
  78. .print = pyb_led_print,
  79. .make_new = pyb_led_make_new,
  80. .locals_dict = (mp_obj_t)&pyb_led_locals_dict,
  81. };