linux下kbhit()函数 getch函数。

怎样在C语言执行循环程序时按键盘任意键停止程序,再按再继续执行?
参考http://zhidao.baidu.com/question/272086855.html

在linux下C语言写了一个while循环,怎么实现按任意键退出。如何编写程序?
参考:http://zhidao.baidu.com/question/461121586.html


对于上面的问题,都用到

linux下的getch函数与kbhit函数。

参考了http://kpld8888.wordpress.com/2007/03/07/linux%E4%B8%8B%E7%9A%84getch%E5%87%BD%E6%95%B0%E4%B8%8Ekbhit%E5%87%BD%E6%95%B0-2/
和:http://zhidao.baidu.com/question/461121586.html

但是我照搬上面两条都不能很好的编译,总是有错误。奉上我的文件:
将下列三个文件放在工程目录下,build project即可。

main.c:
#include "kbhit.h"
#include <stdio.h>
#include <stdlib.h>
#include "accelerometer.h" //contains the middle level macros, state variable
						  // definition and declaration for the
                        //middle layer (class layer) functions



void delay(int a){
	int i,j,delay;
	for(i = 0 ;  i < 1170*(200/a); i++){
		delay = 0;
		for(j = 0; j < 100; j++){
			delay = delay+1;
		}
	}
}

int main()
{

	float x,y,z;       // store x,y,z acceleration

    acc_t state;

    int i = 0;
    int Hz,N;


    // initialize using ADXL345 at 50 HZ
    if (acc_Init(&state,ADXL345,50,"/dev/input/event3") < 0) {

    	printf("Cannot Init \n");
    	return (-1);
    }

    // No Auto Sleep1:
    if (acc_autosleepSet(&state,NO_AUTOSLEEP) < 0) {

    	printf("Failed to set AutoSleep condition\n");
    }
    // No Auto Sleep2:
    system("echo 0 > /sys/bus/i2c/devices/0-0053/autosleep");

    //--------------Initialize finished. --------------//

    printf("input the number of data, Hz: ");
    scanf("%d,%d", &N, &Hz);

    while(i < N){
    	if (acc_xyzGet(&state,&x,&y,&z) < 0) {  // get acceleration

    		printf("Failed to read acceleration values via non blocking read\n");
    	}
    	else {

    		printf("%f,%f,%f\n",x-0.573,y,z); //x correction
    	}
    	delay(Hz);
        i++;
        if( kbhit()>0 ){
        	break;
        }
    }



    if (acc_Release(&state) < 0) {

    	printf("Failed to Release Accelerometer Nuggets\n");

    	return (-1);
    }
    else{
    	printf("Accelerometer Released! \n");
    }

	return 0;
}

/* 	if (acc_xyzGet(&state,&x,&y,&z) < 0) {  // get acceleration

		printf("Failed to read acceleration values via non blocking read\n");
	}
	else {

		printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);
	}

    if (acc_xyzEventGet(&state,&x,&y,&z) < 0) {  // get acceleration

    	printf("Failed to read acceleration values via event get\n");
    }
    else {

    	printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);

    }

    if (acc_xyzEventGet(&state,&x,&y,&z) < 0) {  // get acceleration

    	printf("Failed to read acceleration values via event get\n");
    }
    else {

    	printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);

    }

	if (acc_freefallEventGet(&state) < 0) {

    	printf("Failed to wait for Freefall \n");
    }
    else {

    	printf ("Freefall!!\n");
    }


	*/


kbhit.c
/*#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include "kbhit.h"

static struct termios initial_settings, new_settings;
static int peek_character = -1;

void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}

void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}

int kbhit()
{
unsigned char ch;
int nread;

if (peek_character != -1) return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0,&ch,1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}

int readch()
{
char ch;

if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
*/

#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include "kbhit.h"

#undef TERMIOSECHO
#define TERMIOSFLUSH

/*
 * kbhit() — a keyboard lookahead monitor
 *
 * returns the number of characters available to read
 */
int kbhit ( void )
{
    struct timeval tv;
    struct termios old_termios, new_termios;
    int            error;
    int            count = 0;
    tcgetattr( 0, &old_termios );
    new_termios              = old_termios;
    /*
     * raw mode
     */
    new_termios.c_lflag     &= ~ICANON;
    /*
     * disable echoing the char as it is typed
     */
    new_termios.c_lflag     &= ~ECHO;
    /*
     * minimum chars to wait for
     */
    new_termios.c_cc[VMIN]   = 1;
    /*
     * minimum wait time, 1 * 0.10s
     */
    new_termios.c_cc[VTIME]  = 1;
    error                    = tcsetattr( 0, TCSANOW, &new_termios );
    tv.tv_sec                = 0;
    tv.tv_usec               = 100;
    /*
     * insert a minimal delay
     */
    select( 1, NULL, NULL, NULL, &tv );
    error                   += ioctl( 0, FIONREAD, &count );
    error                   += tcsetattr( 0, TCSANOW, &old_termios );
    return( error == 0 ? count : -1 );
}  /* end of kbhit */
/*————————————————*/


kbhit.h
#ifndef KBHITh
#define KBHITh

void init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);

#endif


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值