mpputsnport.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 Armink (armink.ztl@gmail.com)
  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 <rtthread.h>
  28. #include <rtdevice.h>
  29. static rt_device_t console_dev = NULL;
  30. static struct rt_device dummy_console = { 0 };
  31. static rt_uint16_t console_open_flag;
  32. void mp_putsn(const char *str, size_t len) {
  33. if (console_dev) {
  34. rt_device_write(console_dev, 0, str, len);
  35. }
  36. }
  37. void mp_putsn_stream(const char *str, size_t len) {
  38. if (console_dev) {
  39. rt_uint16_t old_flag = console_dev->open_flag;
  40. console_dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  41. rt_device_write(console_dev, 0, str, len);
  42. console_dev->open_flag = old_flag;
  43. }
  44. }
  45. void mp_putsn_init(void) {
  46. {/* register dummy console device */
  47. #ifdef RT_USING_DEVICE_OPS
  48. static struct rt_device_ops _ops = {0};
  49. dummy_console.ops = &_ops;
  50. #endif
  51. dummy_console.type = RT_Device_Class_Char;
  52. rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR);
  53. }
  54. /* backup the console device */
  55. console_dev = rt_console_get_device();
  56. console_open_flag = console_dev->open_flag;
  57. console_dev->open_flag = 0;
  58. /* set the new console device to dummy console */
  59. rt_console_set_device(dummy_console.parent.name);
  60. /* reopen the old console device for mp_putsn */
  61. rt_device_open(console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  62. }
  63. void mp_putsn_deinit(void) {
  64. /* close the old console, it's already in mp_putsn_init */
  65. rt_device_close(console_dev);
  66. /* restore the old console device */
  67. rt_console_set_device(console_dev->parent.name);
  68. console_dev->open_flag = console_open_flag;
  69. rt_device_unregister(&dummy_console);
  70. }