machine_dac.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Nick Moore
  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 "esp_log.h"
  28. #include "driver/gpio.h"
  29. #include "driver/dac.h"
  30. #include "py/runtime.h"
  31. #include "py/mphal.h"
  32. #include "modmachine.h"
  33. typedef struct _mdac_obj_t {
  34. mp_obj_base_t base;
  35. gpio_num_t gpio_id;
  36. dac_channel_t dac_id;
  37. } mdac_obj_t;
  38. STATIC const mdac_obj_t mdac_obj[] = {
  39. {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1},
  40. {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2},
  41. };
  42. STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
  43. const mp_obj_t *args) {
  44. mp_arg_check_num(n_args, n_kw, 1, 1, true);
  45. gpio_num_t pin_id = machine_pin_get_id(args[0]);
  46. const mdac_obj_t *self = NULL;
  47. for (int i = 0; i < MP_ARRAY_SIZE(mdac_obj); i++) {
  48. if (pin_id == mdac_obj[i].gpio_id) { self = &mdac_obj[i]; break; }
  49. }
  50. if (!self) mp_raise_ValueError("invalid Pin for DAC");
  51. esp_err_t err = dac_output_enable(self->dac_id);
  52. if (err == ESP_OK) {
  53. err = dac_output_voltage(self->dac_id, 0);
  54. }
  55. if (err == ESP_OK) return MP_OBJ_FROM_PTR(self);
  56. mp_raise_ValueError("Parameter Error");
  57. }
  58. STATIC void mdac_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  59. mdac_obj_t *self = self_in;
  60. mp_printf(print, "DAC(Pin(%u))", self->gpio_id);
  61. }
  62. STATIC mp_obj_t mdac_write(mp_obj_t self_in, mp_obj_t value_in) {
  63. mdac_obj_t *self = self_in;
  64. int value = mp_obj_get_int(value_in);
  65. if (value < 0 || value > 255) mp_raise_ValueError("Value out of range");
  66. esp_err_t err = dac_output_voltage(self->dac_id, value);
  67. if (err == ESP_OK) return mp_const_none;
  68. mp_raise_ValueError("Parameter Error");
  69. }
  70. MP_DEFINE_CONST_FUN_OBJ_2(mdac_write_obj, mdac_write);
  71. STATIC const mp_rom_map_elem_t mdac_locals_dict_table[] = {
  72. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mdac_write_obj) },
  73. };
  74. STATIC MP_DEFINE_CONST_DICT(mdac_locals_dict, mdac_locals_dict_table);
  75. const mp_obj_type_t machine_dac_type = {
  76. { &mp_type_type },
  77. .name = MP_QSTR_DAC,
  78. .print = mdac_print,
  79. .make_new = mdac_make_new,
  80. .locals_dict = (mp_obj_t)&mdac_locals_dict,
  81. };