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