temp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Bander F. Ajba
  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 <string.h>
  28. #include "py/nlr.h"
  29. #include "py/runtime.h"
  30. #include "py/mphal.h"
  31. #include "temp.h"
  32. #include "nrf_temp.h"
  33. #if BLUETOOTH_SD
  34. #include "py/nlr.h"
  35. #include "ble_drv.h"
  36. #include "nrf_soc.h"
  37. #define BLUETOOTH_STACK_ENABLED() (ble_drv_stack_enabled())
  38. #endif // BLUETOOTH_SD
  39. #if MICROPY_PY_MACHINE_TEMP
  40. typedef struct _machine_temp_obj_t {
  41. mp_obj_base_t base;
  42. } machine_temp_obj_t;
  43. /// \method __str__()
  44. /// Return a string describing the Temp object.
  45. STATIC void machine_temp_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  46. machine_temp_obj_t *self = o;
  47. (void)self;
  48. mp_printf(print, "Temp");
  49. }
  50. /******************************************************************************/
  51. /* MicroPython bindings for machine API */
  52. STATIC mp_obj_t machine_temp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  53. static const mp_arg_t allowed_args[] = {
  54. { },
  55. };
  56. // parse args
  57. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  58. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  59. machine_temp_obj_t *self = m_new_obj(machine_temp_obj_t);
  60. self->base.type = &machine_temp_type;
  61. return MP_OBJ_FROM_PTR(self);
  62. }
  63. /// \method read()
  64. /// Get temperature.
  65. STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) {
  66. #if BLUETOOTH_SD
  67. if (BLUETOOTH_STACK_ENABLED() == 1) {
  68. int32_t temp;
  69. (void)sd_temp_get(&temp);
  70. return MP_OBJ_NEW_SMALL_INT(temp / 4); // resolution of 0.25 degree celsius
  71. }
  72. #endif // BLUETOOTH_SD
  73. return MP_OBJ_NEW_SMALL_INT(nrf_temp_read());
  74. }
  75. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_temp_read_obj, 0, 1, machine_temp_read);
  76. STATIC const mp_rom_map_elem_t machine_temp_locals_dict_table[] = {
  77. // instance methods
  78. // class methods
  79. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_temp_read_obj) },
  80. };
  81. STATIC MP_DEFINE_CONST_DICT(machine_temp_locals_dict, machine_temp_locals_dict_table);
  82. const mp_obj_type_t machine_temp_type = {
  83. { &mp_type_type },
  84. .name = MP_QSTR_Temp,
  85. .make_new = machine_temp_make_new,
  86. .locals_dict = (mp_obj_dict_t*)&machine_temp_locals_dict,
  87. .print = machine_temp_print,
  88. };
  89. #endif // MICROPY_PY_MACHINE_TEMP