led.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 Damien P. George
  7. * Copyright (c) 2015 - 2018 Glenn Ruben Bakke
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/runtime.h"
  28. #include "mphalport.h"
  29. #include "led.h"
  30. #include "mpconfigboard.h"
  31. #if MICROPY_HW_HAS_LED
  32. #define LED_OFF(pin) {(MICROPY_HW_LED_PULLUP) ? nrf_gpio_pin_set(pin) : nrf_gpio_pin_clear(pin); }
  33. #define LED_ON(pin) {(MICROPY_HW_LED_PULLUP) ? nrf_gpio_pin_clear(pin) : nrf_gpio_pin_set(pin); }
  34. typedef struct _board_led_obj_t {
  35. mp_obj_base_t base;
  36. mp_uint_t led_id;
  37. mp_uint_t hw_pin;
  38. uint8_t hw_pin_port;
  39. } board_led_obj_t;
  40. STATIC const board_led_obj_t board_led_obj[] = {
  41. #if MICROPY_HW_LED_TRICOLOR
  42. {{&board_led_type}, BOARD_LED_RED, MICROPY_HW_LED_RED},
  43. {{&board_led_type}, BOARD_LED_GREEN, MICROPY_HW_LED_GREEN},
  44. {{&board_led_type}, BOARD_LED_BLUE, MICROPY_HW_LED_BLUE},
  45. #elif (MICROPY_HW_LED_COUNT == 1)
  46. {{&board_led_type}, BOARD_LED1, MICROPY_HW_LED1},
  47. #elif (MICROPY_HW_LED_COUNT == 2)
  48. {{&board_led_type}, BOARD_LED1, MICROPY_HW_LED1},
  49. {{&board_led_type}, BOARD_LED2, MICROPY_HW_LED2},
  50. #else
  51. {{&board_led_type}, BOARD_LED1, MICROPY_HW_LED1},
  52. {{&board_led_type}, BOARD_LED2, MICROPY_HW_LED2},
  53. {{&board_led_type}, BOARD_LED3, MICROPY_HW_LED3},
  54. {{&board_led_type}, BOARD_LED4, MICROPY_HW_LED4},
  55. #endif
  56. };
  57. #define NUM_LEDS MP_ARRAY_SIZE(board_led_obj)
  58. void led_init(void) {
  59. for (uint8_t i = 0; i < NUM_LEDS; i++) {
  60. LED_OFF(board_led_obj[i].hw_pin);
  61. nrf_gpio_cfg_output(board_led_obj[i].hw_pin);
  62. }
  63. }
  64. void led_state(board_led_obj_t * led_obj, int state) {
  65. if (state == 1) {
  66. LED_ON(led_obj->hw_pin);
  67. } else {
  68. LED_OFF(led_obj->hw_pin);
  69. }
  70. }
  71. void led_toggle(board_led_obj_t * led_obj) {
  72. nrf_gpio_pin_toggle(led_obj->hw_pin);
  73. }
  74. /******************************************************************************/
  75. /* MicroPython bindings */
  76. void led_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  77. board_led_obj_t *self = self_in;
  78. mp_printf(print, "LED(%lu)", self->led_id);
  79. }
  80. /// \classmethod \constructor(id)
  81. /// Create an LED object associated with the given LED:
  82. ///
  83. /// - `id` is the LED number, 1-4.
  84. STATIC mp_obj_t led_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  85. // check arguments
  86. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  87. // get led number
  88. mp_int_t led_id = mp_obj_get_int(args[0]);
  89. // check led number
  90. if (!(1 <= led_id && led_id <= NUM_LEDS)) {
  91. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED(%d) does not exist", led_id));
  92. }
  93. // return static led object
  94. return (mp_obj_t)&board_led_obj[led_id - 1];
  95. }
  96. /// \method on()
  97. /// Turn the LED on.
  98. mp_obj_t led_obj_on(mp_obj_t self_in) {
  99. board_led_obj_t *self = self_in;
  100. led_state(self, 1);
  101. return mp_const_none;
  102. }
  103. /// \method off()
  104. /// Turn the LED off.
  105. mp_obj_t led_obj_off(mp_obj_t self_in) {
  106. board_led_obj_t *self = self_in;
  107. led_state(self, 0);
  108. return mp_const_none;
  109. }
  110. /// \method toggle()
  111. /// Toggle the LED between on and off.
  112. mp_obj_t led_obj_toggle(mp_obj_t self_in) {
  113. board_led_obj_t *self = self_in;
  114. led_toggle(self);
  115. return mp_const_none;
  116. }
  117. STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
  118. STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
  119. STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
  120. STATIC const mp_rom_map_elem_t led_locals_dict_table[] = {
  121. { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&led_obj_on_obj) },
  122. { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&led_obj_off_obj) },
  123. { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&led_obj_toggle_obj) },
  124. };
  125. STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);
  126. const mp_obj_type_t board_led_type = {
  127. { &mp_type_type },
  128. .name = MP_QSTR_LED,
  129. .print = led_obj_print,
  130. .make_new = led_obj_make_new,
  131. .locals_dict = (mp_obj_dict_t*)&led_locals_dict,
  132. };
  133. #endif // MICROPY_HW_HAS_LED