使用终端虚拟示波器显示数据

Hello World

#include <stdio.h>
#include <math.h>
#include <windows.h>
//定义隐藏光标函数
void HideCursor()
{
  CONSOLE_CURSOR_INFO cursor;    
  cursor.bVisible = FALSE;    
  cursor.dwSize = sizeof(cursor);    
  HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);    
  SetConsoleCursorInfo(handle, &cursor);
}
char output_buff[1024 * 1024 * 2];
void clearScreen(){
    printf("\e[1;1H\e[2J");
}
int main() {
    HideCursor();
    int buffer[128][64];
    for (int i = 0; i < 128; i++) {
        for (int j = 0; j < 64; j++) {
            buffer[i][j] = 0;
        }
    }
    int x_cnt = 0;
    while(1)
    {
      memset(output_buff,0,sizeof(output_buff));
      double x = x_cnt * 2 * M_PI / 64;
      double y = (sin(x) + 1) / 2 * (64 - 1);
      buffer[127][(int)(y + 0.5f)] = 1;
      x_cnt++;
      x_cnt%=64;
      // 将buffer打印出来
      for (int j = 0; j < 64; j++) {
          for (int i = 0; i < 128; i++) {
            if(buffer[i][j] == 1)
            {
              strcat(output_buff,"\033[1;33;42m*");
            }
            else
            {
              strcat(output_buff," ");
            }

          }
          strcat(output_buff,"\n");
      }
      clearScreen();
      printf("%s",output_buff);
      Sleep(100);
      //将buffer里的数据整体往左平移
      for (int i = 0; i < 128; i++) {
          for (int j = 0; j < 64; j++) {
              buffer[i][j] = buffer[i + 1][j];
          }
          // buffer[i][64-1] = 0;
      }

    }
    return 0;
}

alt text

posted @ 2024-03-08 17:45  USTHzhanglu  阅读(21)  评论(0编辑  收藏  举报