modpyb.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 <stdio.h>
  27. #include "py/obj.h"
  28. #include "py/mphal.h"
  29. #include "modpyb.h"
  30. STATIC mp_obj_t pyb_millis(void) {
  31. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
  32. }
  33. STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
  34. STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
  35. uint32_t startMillis = mp_obj_get_int(start);
  36. uint32_t currMillis = mp_hal_ticks_ms();
  37. return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x1fff);
  38. }
  39. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
  40. STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
  41. mp_int_t ms = mp_obj_get_int(ms_in);
  42. if (ms >= 0) {
  43. mp_hal_delay_ms(ms);
  44. }
  45. return mp_const_none;
  46. }
  47. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
  48. STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
  49. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
  50. { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&pyb_millis_obj) },
  51. { MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
  52. { MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&pyb_delay_obj) },
  53. { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
  54. { MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
  55. };
  56. STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
  57. const mp_obj_module_t pyb_module = {
  58. .base = { &mp_type_module },
  59. .globals = (mp_obj_dict_t*)&pyb_module_globals,
  60. };