对于上面的问题,都用到

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

#include

#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

#include

#include

#include

#include

#include

#include

#include

#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

#include

#include

#include

#include

#include

#include

#include

#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

linux中c语言kbhit函数用法,linux下kbhit()函数 getch函数。相关推荐

  1. linux中du命令参数的用法,linux中du命令参数及df命令和dd命令介绍

    本文详细介绍了linux中du命令参数及df命令和dd命令的使用方法,并对之进行了举例说明. linux中df命令参数 linux中df命令参数功能:检查文件系统的磁盘空间占用情况.可以利用该命令来获 ...

  2. 为什么linux中c语言不一样,不懂linux为什么要死守c语言。

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 C++很先进吗?C++很优美吗? lz去看看unix编程艺术那本书会发现c++和*nix之类格格不入. 做系统底层开发,c++比c的优势在哪里?效率和简洁 ...

  3. linux中c语言开发实验报告,Linux下C语言编程实验报告.doc

    第五章: Linux下的C语言编程 姓名: 学号:520913080429 专业:信息安全09-04 实验内容: 1.c语言编程 2.vi编辑器 3.gcc编辑器 4.gdb编辑器 5. gdb中运行 ...

  4. linux之getcwd函数解析,Linux 中C语言getcwd()函数的用法

    Linux 中C语言getcwd()函数的用法 先来看该函数的声明: #include char *getcwd(char *buf,size_t size); 介绍: 参数说明:getcwd()会将 ...

  5. c语言open函数打开文件方式,Linux中C语言open函数打开或创建文件详细讲解

    Linux中C语言open函数打开或创建文件详细讲解 Linux中C语言open函数打开或创建文件详细讲解 头文件: #include #include #include 函数原型: int open ...

  6. Linux中C语言编程经验总结

    ​ 修改记录 版本号 日期 更改理由 V1.0 2022-03-15 MD化 V1.01 2023-02-18 0和-1同时作为非法值的建议 总则 仅总结一些常用且实用的编程规范和技巧,且避免记忆负担 ...

  7. 浅谈Linux中ldconfig和ldd的用法

    ldd 查看程序依赖库 ldd 作用:用来查看程式运行所需的共享库,常用来解决程式因缺少某个库文件而不能运行的一些问题. 示例:查看test程序运行所依赖的库: /opt/app/todeav1/te ...

  8. Linux下dislocate命令用法,在 Linux 中遨游手册页的海洋 | Linux 中国

    原标题:在 Linux 中遨游手册页的海洋 | Linux 中国 Linux 系统上的手册页可以做的不仅仅是提供特定命令的信息.它们可以帮助你发现你没有意识到的命令. https://linux.cn ...

  9. linux中c语言生日快乐_生日快乐,Linux:27岁

    linux中c语言生日快乐 Linux今天庆祝了另一个生日-27岁! 与我们分享激动之情,我们无比高兴. 我们的许多读者都是Linux用户,粉丝,书呆子...形容他们的形容词清单不胜枚举. 你会怎么称 ...

  10. linux的vi命令输入C,Linux中C语言简单实现之vi篇(详细内容)

    一.vi编辑器介绍 vi是UNIX和Linux操作系统使用的全屏幕文本编辑器,任何一台安装了UNIX或Linux的计算机都会提供vi编辑器,是一个功能强大的编辑器. 1.vi有三种操作模式:命令模式. ...

最新文章

  1. 【leetcode】1030. Matrix Cells in Distance Order
  2. 你是如何自学 Python 的?
  3. JavaScript语言标准
  4. R语言dplyr包将dataframe中的NA值替换(replace)为0实战:所有NA值替换(replace)为0、具体列的NA值替换(replace)为0、若干列的NA值替换(replace)为0
  5. 数据算法竞赛:ICDM 2020 TOP3方案
  6. SAP Smart Business design time = CDS view SADL
  7. python中的命名空间指什么_13.Python中的命名空间是什么
  8. sap生产工单报工_SAP系统PP (ProductionPlanning) 模块知识培训
  9. 基于QEMU的NVRAM仿真
  10. Android 系统(196)---Android 属性动画
  11. PTA-Hello World(C语言)
  12. 数据库实验6 数据库的分组查询和统计查询
  13. 原生Servlet文件上传和下载Servlet多个文件上传
  14. Newtonsoft.Json 序列化和反序列化 以及时间格式 2
  15. c语言入门编程题库100题,C语言入门100题
  16. 好程序员Java教程分享使用HttpClient抓取页面内容
  17. 几张趣图助你理解HTTP状态码
  18. iReport下载安装配置及编译
  19. 南邮CTF逆向题第三道Py交易解题思路
  20. 谷歌雅虎新闻大战-两种路线的PK

热门文章

  1. 90后的互联网社交病:面对面交流也要发微信
  2. 分枝限界法求解0/1背包问题
  3. PDF转换成ODT格式常用方法介绍
  4. Java进击框架:Spring(一)
  5. 2小时破解WIFI密码!WPS漏洞目前尚无解决方案
  6. 【C++】“凯撒密码”详解
  7. vue项目中axios配置(代理配置)
  8. anaconda navigator启动时一直卡在 loading applications 页面(已解决)
  9. tp框架中的facade
  10. TextView属性大全及跑马灯效果