使用終端虛擬示波器顯示資料

USTHzhanglu發表於2024-03-08

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);
}

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)
    {
      clearScreen();
      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)
            {
              printf("\033[1;33;42m* ");
            }
            else
            {
              printf("  ", buffer[i][j]);
            }

          }
          printf("\n");
      }
      Sleep(50);
      //將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

相關文章