4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-02-23 06:29:06 +08:00

[kservice] optimize console_device

This commit is contained in:
Meco Man 2024-12-09 21:46:24 -05:00
parent ebe05a91df
commit 6cad23bc31

View File

@ -187,23 +187,15 @@ RTM_EXPORT(rt_console_get_device);
*/ */
rt_device_t rt_console_set_device(const char *name) rt_device_t rt_console_set_device(const char *name)
{ {
rt_device_t new_device, old_device; rt_device_t old_device = _console_device;
rt_device_t new_device = rt_device_find(name);
/* save old device */ if (new_device != RT_NULL && new_device != old_device)
old_device = _console_device;
/* find new console device */
new_device = rt_device_find(name);
/* check whether it's a same device */
if (new_device == old_device) return RT_NULL;
if (new_device != RT_NULL)
{ {
if (_console_device != RT_NULL) if (old_device != RT_NULL)
{ {
/* close old console device */ /* close old console device */
rt_device_close(_console_device); rt_device_close(old_device);
} }
/* set new console device */ /* set new console device */
@ -319,20 +311,23 @@ static void _console_release(void)
*/ */
static void _kputs(const char *str, long len) static void _kputs(const char *str, long len)
{ {
RT_UNUSED(len); #ifdef RT_USING_DEVICE
rt_device_t console_device = rt_console_get_device();
#endif /* RT_USING_DEVICE */
CONSOLE_TAKE; CONSOLE_TAKE;
#ifdef RT_USING_DEVICE #ifdef RT_USING_DEVICE
if (_console_device == RT_NULL) if (console_device == RT_NULL)
{ {
rt_hw_console_output(str); rt_hw_console_output(str);
} }
else else
{ {
rt_device_write(_console_device, 0, str, len); rt_device_write(console_device, 0, str, len);
} }
#else #else
RT_UNUSED(len);
rt_hw_console_output(str); rt_hw_console_output(str);
#endif /* RT_USING_DEVICE */ #endif /* RT_USING_DEVICE */