ZephRay 发表于 2013-7-10 08:56:40

Ndless SDK系列教程——按键&TouchPad

本帖最后由 nbzwt 于 2014-7-2 17:43 编辑

(返回目录)

Ndless SDK 系列教程——按键&TouchPad
本节内容:
了解如何读取按键输入,如何读取Touchpad的状态。本节就两个程序,有详细注释,注释就当教程用了。

ProgA:各种按键扫描方法key.c

#include <os.h>
#include <nspireio2.h>

int main(void) {
    nio_console csl;
      char ch;
      char strbuf;
    lcd_ingray();
    clrscr();
    // 53 columns, 29 rows. 0px offset for x/y.
    // Background color 0 (black), foreground color 15 (white)
    nio_InitConsole(&csl, 53, 29, 0, 0, 0, 15);
    nio_DrawConsole(&csl);
    nio_printf(&csl, "Press any key to contiune...\r\n");//printf大家都懂的
    wait_key_pressed();//等待按键按下,所有按键都有效。
      ch=nio_fgetc(&csl);//从指定console获取一个字符
      while ((ch!='\r')&&(ch!='\0'))
      {
                nio_fputc(&csl,ch);//在指定console显示一个字符
                ch=nio_fgetc(&csl);
      }//不知道为什么,nio_gets不能正常使用               
      nio_printf(&csl, "Press 1 key to continue...\r\n");
      while (!isKeyPressed(KEY_NSPIRE_1));//isKeyPressed可以用于检测某一个键是否按下
    nio_CleanUp(&csl);
    return 0;
}ProgB:TouchPad库及使用
给大家个TouchPad库,来自mViewer,我做了一些小修改,大家可以直接拿到自己程序里用。touchpad.h

#define TOUCHPAD_SUPPORT
#define TOUCHPAD_DELAY      45

#ifdef TOUCHPAD_SUPPORT
touchpad_report_t* tpreport;
extern touchpad_info_t* tpinfo;
#endif

void initTP();//初始化TP
void endTP();//释放
void readTP();//读取TP状态
int getTouchedZone5();//把TouchPad分成5个区域,读取值
int getTouchedZone4();//把TouchPad分成4个区域,读取值
int getTouchedZone9();//把TouchPad分成9个区域,读取值
int getX_Velocity();//读取X的相对坐标
int getY_Velocity();//读取Y的相对坐标
int getX()//读取X的绝对坐标
int getY()//读取Y的绝对坐标
int isTPTouched();//TP是否被触摸
int isTPPressed();//TP是否被按下
int isTouchPad();//是否拥有Touchpadtouchpad.c

#include <os.h>
#include "touchpad.h"

#ifdef TOUCHPAD_SUPPORT
touchpad_info_t* tpinfo;
#endif

void initTP()//初始化TP
{      
#ifdef TOUCHPAD_SUPPORT
      tpinfo = touchpad_getinfo();
      tpreport = (touchpad_report_t*) malloc(sizeof(touchpad_report_t));
      memset(tpreport,0,sizeof(touchpad_report_t));
#endif
}

void endTP()//释放
{
#ifdef TOUCHPAD_SUPPORT
      free(tpreport);
#endif
}

void readTP()//读取TP状态
{
#ifdef TOUCHPAD_SUPPORT
      touchpad_scan(tpreport);
#endif
}

void readFullTP()//进行延时后再读取TP状态
{
#ifdef TOUCHPAD_SUPPORT
      wait(TOUCHPAD_DELAY); // lets the time to compute velocity
      readTP();
#endif
}

int getX_Velocity()//读取X的相对坐标
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      int t = tpreport->x_velocity;
      if(t<=127) return t;
      return t-256;
#else
      return 0;
#endif
}

int getY_Velocity()//读取Y的相对坐标
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      int t = tpreport->y_velocity;
      if(t<=127) return t;
      return t-256;
#else
      return 0;
#endif
}

int getX()//读取X的绝对坐标
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      int t = tpreport->x;
      return t;
#else
      return 0;
#endif
}

int getY()//读取Y的绝对坐标
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      int t = tpreport->y;
      return t;
#else
      return 0;
#endif
}

/* 8
456
   2
*/
int getTouchedZone5()//把TouchPad分成5个区域,读取值
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      if(tpreport->x>tpinfo->width/3 && tpreport->x<2*tpinfo->width/3 && tpreport->y>tpinfo->height/3 && tpreport->y<2*tpinfo->height/3)
                return 5;
      float m = (float)tpinfo->height/tpinfo->width;
      float ac,ad;
      if(tpreport->x==0)
                if(tpreport->y==0)      ac=m;
                else                        ac=tpinfo->height;
      else      ac=(float)tpreport->y/tpreport->x;
      if(tpreport->x==0)
                if(tpreport->y==tpinfo->height)      ad=-m;
                else                        ad=-tpinfo->height;
      else      ad=(float)(tpreport->y-tpinfo->height)/tpreport->x;
      if(ac<=m)
                if(ad<=-m)      return 2;
                else               return 6;
      else
                if(ad<-m)      return 4;
                else                return 8;
#else
      return 0;
#endif
}

/* 8
4 6
   2
*/
int getTouchedZone4()//把TouchPad分成4个区域,读取值
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      float m = (float)tpinfo->height/tpinfo->width;
      float ac,ad;
      if(tpreport->x==0)
                if(tpreport->y==0)      ac=m;
                else                        ac=tpinfo->height;
      else      ac=(float)tpreport->y/tpreport->x;
      if(tpreport->x==0)
                if(tpreport->y==tpinfo->height)      ad=-m;
                else                        ad=-tpinfo->height;
      else      ad=(float)(tpreport->y-tpinfo->height)/tpreport->x;
      if(ac<=m)
                if(ad<=-m)      return 2;
                else               return 6;
      else
                if(ad<-m)      return 4;
                else                return 8;
#else
      return 0;
#endif
}

/* 789
   456
   123
*/
int getTouchedZone9()//把TouchPad分成9个区域,读取值
{
#ifdef TOUCHPAD_SUPPORT
      if(!is_touchpad) return 0;
      if(!tpreport->contact) return 0;
      if(tpreport->x<=tpinfo->width/3)
                if(tpreport->y<=tpinfo->height/3)                return 1;
                else if(tpreport->y<=2*tpinfo->height/3)      return 4;
                else                                                return 7;
      else if(tpreport->x<2*tpinfo->width/3)
                if(tpreport->y<=tpinfo->height/3)                return 2;
                else if(tpreport->y<2*tpinfo->height/3)                return 5;
                else                                                return 8;
      else
                if(tpreport->y<=tpinfo->height/3)                return 3;
                else if(tpreport->y<2*tpinfo->height/3)                return 6;
                else                                                return 9;
#else
      return 0;
#endif
}

int isTPTouched()//TP是否被触摸
{
#ifdef TOUCHPAD_SUPPORT
      return tpreport->contact;
#else
      return 0;
#endif
}

int isTPPressed()//TP是否被按下
{
#ifdef TOUCHPAD_SUPPORT
      return tpreport->pressed;
#else
      return 0;
#endif
}

int isTouchpad()//是否拥有Touchpad
{
#ifdef TOUCHPAD_SUPPORT
      return 1;
#else
      return 0;
#endif
}接下来是一个用这个库做的触摸小画板(lcd库暂时先不给,大家自己先想想怎么做?)paint.c

#include <os.h>
#include "touchpad.h"
#include "lcd.h"

int main(void) {
      int x,y,lastx,lasty;
      int bgcolor=0x0000;
      int fgcolor=0xffff;
      char connect=0;
      lcd_incolor();
      LCD_Fill(bgcolor);
      LCD_Rect(10,220,20,230,fgcolor);
      LCD_String(0,0,"Simple Paint V1.0",0xffff);
      initTP();
      while (!isKeyPressed(KEY_NSPIRE_ESC))
      {
                wait(TOUCHPAD_DELAY);
                readTP();
                if (isTPTouched())
                {
                        x=tpreport->x;
                        y=tpreport->y;
                        x=(x*320)/tpinfo->width;
                        y=240-((y*240)/tpinfo->height);
                        if (connect==1) {
                              LCD_Line(lastx,lasty,x,y,fgcolor);
                        }
                        else
                        {
                              LCD_Point(x,y,fgcolor);
                              connect=1;
                        }
                        lastx=x;
                        lasty=y;
                }
                else
                        connect=0;
                if (isKeyPressed(KEY_NSPIRE_DEL)) LCD_Fill(bgcolor);
                if (isKeyPressed(KEY_NSPIRE_CTRL))
                {
                        if (isKeyPressed(KEY_NSPIRE_1)) bgcolor=0x0000;
                        if (isKeyPressed(KEY_NSPIRE_2)) bgcolor=0xFFFF;
                        if (isKeyPressed(KEY_NSPIRE_3)) bgcolor=0xF800;
                        if (isKeyPressed(KEY_NSPIRE_4)) bgcolor=0xFFE0;
                        if (isKeyPressed(KEY_NSPIRE_5)) bgcolor=0x07E0;
                        if (isKeyPressed(KEY_NSPIRE_6)) bgcolor=0x07FF;
                        if (isKeyPressed(KEY_NSPIRE_7)) bgcolor=0x001F;
                        LCD_Rect(10,220,20,230,fgcolor);
                        LCD_Fill(bgcolor);
                }
                else
                {
                        if (isKeyPressed(KEY_NSPIRE_1)) fgcolor=0x0000;
                        if (isKeyPressed(KEY_NSPIRE_2)) fgcolor=0xFFFF;
                        if (isKeyPressed(KEY_NSPIRE_3)) fgcolor=0xF800;
                        if (isKeyPressed(KEY_NSPIRE_4)) fgcolor=0xFFE0;
                        if (isKeyPressed(KEY_NSPIRE_5)) fgcolor=0x07E0;
                        if (isKeyPressed(KEY_NSPIRE_6)) fgcolor=0x07FF;
                        if (isKeyPressed(KEY_NSPIRE_7)) fgcolor=0x001F;
                        LCD_Rect(10,220,20,230,fgcolor);
                }
      }
      return 0;
}如果有什么不懂的,可以在下面问。如果看见imath来灌水,大家帮忙点下举报,谢谢了!

(返回目录)

DAS 发表于 2013-7-10 09:16:28

猜一下LCD库:
*可能不存在
LCD_Image()*
LCD_Line()
LCD_PolyLine()*
LCD_Rect()
LCD_String()
LCD_Polygon()*
LCD_fill()
LCD_fillRect()
LCD_Font()

rphero 发表于 2013-7-10 20:04:45

支持!~

.zyz 发表于 2013-7-11 07:30:36

本帖最后由 Zentauit 于 2013-7-11 16:29 编辑

nbzwt,
你也应该提供一下paint.c构建完的程序来吸引更多人的兴趣吧、、、

我这里顺便提供了吧:

晓零辛纳 发表于 2013-10-20 11:42:15

本帖最后由 中国好程序 于 2013-10-20 11:44 编辑

能否直接给出ndless支持的API及其所在的头文件?
这个SDK真心不习惯...

除此以外,能否解释一下那个lua.h到底是干什么的?

ZephRay 发表于 2013-10-20 15:59:38

中国好程序 发表于 2013-10-20 11:42 static/image/common/back.gif
能否直接给出ndless支持的API及其所在的头文件?
这个SDK真心不习惯...



基本就是os.h libndl.h nspireio.h里面的东西。lua不太清楚
页: [1]
查看完整版本: Ndless SDK系列教程——按键&TouchPad