查看: 371|回复: 0

RA0E1的printf

[复制链接]

49

主题

142

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
11615
发表于 2024-5-19 06:48:22 | 显示全部楼层 |阅读模式
  1. /* 重定向 printf 输出 */
  2. #if defined __GNUC__ && !defined __clang__
  3. int _write(int fd, char *pBuffer, int size); //防止编译警告
  4. int _write(int fd, char *pBuffer, int size)
  5. {
  6.     (void)fd;
  7.     struct UartDev *pLogDevice = UartDeviceFind("Log");
  8.     pLogDevice->Write(pLogDevice, (unsigned char*)&ch, 1);
  9.     return size;
  10. }


  11. int _read (int fd, char *pBuffer, int size)
  12. {
  13.     (void)fd;
  14.     struct UartDev *pLogDevice = UartDeviceFind("Log");
  15.         /* 启动接收字符 */
  16.     while(pLogDevice->Read(pLogDevice, (unsigned char*)&pBuffer, size) != size)
  17.     {}
  18.     return size;
  19. }
  20. #else
  21. #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION < 6010050)
  22. struct __FILE{
  23.    int handle;
  24. };
  25. #endif
  26. FILE __stdout;
  27. int fputc(int ch, FILE *f)
  28. {
  29.     (void)f;
  30.     struct UartDev *pLogDevice = UartDeviceFind("Log");
  31.     pLogDevice->Write(pLogDevice, (unsigned char*)&ch, 1);
  32.     return ch;
  33. }

  34. /* 重写这个函数,重定向scanf */
  35. int fgetc(FILE *f)
  36. {
  37.         uint8_t ch;
  38.        
  39.         (void)f;
  40.     struct UartDev *pLogDevice = UartDeviceFind("Log");
  41.         /* 启动接收字符 */
  42.     while(pLogDevice->Read(pLogDevice, (unsigned char*)&ch, 1) != 1)
  43.     {}
  44.         /* 回显 */
  45.         {
  46.                 fputc(ch, &__stdout);
  47.                
  48.                 /* 回车之后加换行 */
  49.                 if (ch == '\r')
  50.                 {
  51.                         fputc('\n', &__stdout);
  52.                 }
  53.         }
  54.    
  55.         return (int)ch;
  56. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户排行榜

RA助手

主题: 116帖子:134精华:0

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 49帖子:142精华:1

xujiwei263

主题: 16帖子:79精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 10帖子:105精华:0
快速回复 返回顶部 返回列表