在同一行,而不是新的更新值的printf [英] Update printf value on same line instead of new one

查看:125
本文介绍了在同一行,而不是新的更新值的printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有在 C 办法覆盖已打印,而不是创建一个新的行每一次,或只是动过现有值的空间。我需要从传感器获得的实时数据,并希望它只是坐在那里,并保持没有任何滚动更新现有值。这可能吗?

更新:增加了code

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdint.h GT;
#包括LT&;&time.h中GT;#包括LT&;&wiringPi.h GT;
#包括LT&;&wiringPiI2C.h GT;CTRL_REG1的#define 0x20的
#定义CTRL_REG2为0x21
#定义CTRL_REG3 0x22
#定义CTRL_REG4 0x23
INT的fd;
短X = 0;
短Y = 0;
短Z = 0;
诠释主(){    FD = wiringPiI2CSetup(0×69); //陀螺I2C地址
    wiringPiI2CWriteReg8(FD,CTRL_REG1,0x1F的); //打开所有轴,禁用断电
    wiringPiI2CWriteReg8(FD,CTRL_REG3,0×08); //启用控制就绪信号
    wiringPiI2CWriteReg8(FD,CTRL_REG4,0x80的); //设置规模(500度/秒)
    延迟(200); //等待同步无效getGyroValues​​(){
    INT MSB,LSB;    LSB = wiringPiI2CReadReg8(FD,0x28);
    MSB = wiringPiI2CReadReg8(FD,0x29);
    X =((MSB&所述;&下; 8)| LSB);    MSB = wiringPiI2CReadReg8(FD,0x2B访问);
    LSB = wiringPiI2CReadReg8(FD,0x2A);
    Y =((MSB&所述;&下; 8)| LSB);    MSB = wiringPiI2CReadReg8(FD,0x2D);
    LSB = wiringPiI2CReadReg8(FD,0x2C)设置;
    Z =((MSB&所述;&下; 8)| LSB);
}
    对(INT I = 0; I&小于10;我++){
    getGyroValues​​();
    //在114以下Divinding降低噪音
    的printf(X的数值为:%d个\\ r,即X / 114);
//的printf(Y的值是:%D,Y / 114);
//的printf(Z的值是:%d个\\ R,Z / 114);
    INT T = wiringPiI2CReadReg8(FD,值为0x26);
    T =(T * 1.8)+32; //摄氏转换到Fareinheit
    INT A = wiringPiI2CReadReg8(FD,0x2B访问);
    INT B = wiringPiI2CReadReg8(FD,0x2A);
//输出(Y_L等于数:%d \\ r,即一);
//输出(Y_H等于数:%d \\ R,B);
    INT C = wiringPiI2CReadReg8(FD,0x28);
    INT D = wiringPiI2CReadReg8(FD,0x29);
//输出(X_L等于数:%d \\ R,C);
//输出(X_H等于数:%d \\ r,即D);
    INT E = wiringPiI2CReadReg8(FD,0x2C)设置;
    INT F = wiringPiI2CReadReg8(FD,0x2D);
//输出(Z_L等于数:%d \\ r,即E);
//输出(Z_H等于数:%d \\ r,即F);//的printf(温度为:%d个\\ R,T);
    延迟(2000);
}
};


解决方案

您可以打印出尽可能多的换行符控制台屏幕了。这将有效地清除屏幕。

这是有关以不同的方式清除屏幕很大链接

I would like to know if there is a way in C to overwrite an existing value that has already been printed instead of creating a new line every time or just moving over a space. I need to obtain real time data from a sensor and would like it to just sit there and keep updating the existing values without any scrolling. Is this possible?

UPDATE: ADDED CODE

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23


int fd;
short x = 0;
short y = 0;
short z = 0;
int main (){



    fd = wiringPiI2CSetup(0x69); // I2C address of gyro
    wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down
    wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal
    wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec)
    delay(200);                    // Wait to synchronize

void getGyroValues (){
    int MSB, LSB;

    LSB = wiringPiI2CReadReg8(fd, 0x28);
    MSB = wiringPiI2CReadReg8(fd, 0x29);
    x = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2B);
    LSB = wiringPiI2CReadReg8(fd, 0x2A);
    y = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2D);
    LSB = wiringPiI2CReadReg8(fd, 0x2C);
    z = ((MSB << 8) | LSB);
}
    for (int i=0;i<10;i++){
    getGyroValues();
    // In following Divinding by 114 reduces noise
    printf("Value of X is: %d\r", x/114);
//  printf("Value of Y is: %d", y/114);
//  printf("Value of Z is: %d\r", z/114);
    int t = wiringPiI2CReadReg8(fd, 0x26);
    t = (t*1.8)+32;//convert Celcius to Fareinheit
    int a = wiringPiI2CReadReg8(fd,0x2B);
    int b = wiringPiI2CReadReg8(fd,0x2A);
//  printf("Y_L equals: %d\r", a);
//  printf("Y_H equals: %d\r", b);
    int c = wiringPiI2CReadReg8(fd,0x28);
    int d = wiringPiI2CReadReg8(fd,0x29);
//  printf("X_L equals: %d\r", c);
//  printf("X_H equals: %d\r", d);
    int e = wiringPiI2CReadReg8(fd,0x2C);
    int f = wiringPiI2CReadReg8(fd,0x2D);
//  printf("Z_L equals: %d\r", e);
//  printf("Z_H equals: %d\r", f); 

//  printf("The temperature is: %d\r", t); 
    delay(2000);
}
};

解决方案

You can print out as many newlines as the console screen has. This will effectively clear the screen.

This is a great link about clearing screen in different ways.

这篇关于在同一行,而不是新的更新值的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆