cnCalc计算器论坛

 找回密码
 注册
搜索
查看: 4165|回复: 5

[Nspire] Ndless SDK系列教程——按键&TouchPad

[复制链接]
发表于 2013-7-10 08:56:40 | 显示全部楼层 |阅读模式
本帖最后由 nbzwt 于 2014-7-2 17:43 编辑

(返回目录)

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

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

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

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

  2. #define TOUCHPAD_SUPPORT
  3. #define TOUCHPAD_DELAY        45

  4. #ifdef TOUCHPAD_SUPPORT
  5. touchpad_report_t* tpreport;
  6. extern touchpad_info_t* tpinfo;
  7. #endif

  8. void initTP();//初始化TP
  9. void endTP();//释放
  10. void readTP();//读取TP状态
  11. int getTouchedZone5();//把TouchPad分成5个区域,读取值
  12. int getTouchedZone4();//把TouchPad分成4个区域,读取值
  13. int getTouchedZone9();//把TouchPad分成9个区域,读取值
  14. int getX_Velocity();//读取X的相对坐标
  15. int getY_Velocity();//读取Y的相对坐标
  16. int getX()//读取X的绝对坐标
  17. int getY()//读取Y的绝对坐标
  18. int isTPTouched();//TP是否被触摸
  19. int isTPPressed();//TP是否被按下
  20. int isTouchPad();//是否拥有Touchpad
复制代码
  1. touchpad.c

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

  4. #ifdef TOUCHPAD_SUPPORT
  5. touchpad_info_t* tpinfo;
  6. #endif

  7. void initTP()//初始化TP
  8. {        
  9. #ifdef TOUCHPAD_SUPPORT
  10.         tpinfo = touchpad_getinfo();
  11.         tpreport = (touchpad_report_t*) malloc(sizeof(touchpad_report_t));
  12.         memset(tpreport,0,sizeof(touchpad_report_t));
  13. #endif
  14. }

  15. void endTP()//释放
  16. {
  17. #ifdef TOUCHPAD_SUPPORT
  18.         free(tpreport);
  19. #endif
  20. }

  21. void readTP()//读取TP状态
  22. {
  23. #ifdef TOUCHPAD_SUPPORT
  24.         touchpad_scan(tpreport);
  25. #endif
  26. }

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

  34. int getX_Velocity()//读取X的相对坐标
  35. {
  36. #ifdef TOUCHPAD_SUPPORT
  37.         if(!is_touchpad) return 0;
  38.         if(!tpreport->contact) return 0;
  39.         int t = tpreport->x_velocity;
  40.         if(t<=127) return t;
  41.         return t-256;
  42. #else
  43.         return 0;
  44. #endif
  45. }

  46. int getY_Velocity()//读取Y的相对坐标
  47. {
  48. #ifdef TOUCHPAD_SUPPORT
  49.         if(!is_touchpad) return 0;
  50.         if(!tpreport->contact) return 0;
  51.         int t = tpreport->y_velocity;
  52.         if(t<=127) return t;
  53.         return t-256;
  54. #else
  55.         return 0;
  56. #endif
  57. }

  58. int getX()//读取X的绝对坐标
  59. {
  60. #ifdef TOUCHPAD_SUPPORT
  61.         if(!is_touchpad) return 0;
  62.         if(!tpreport->contact) return 0;
  63.         int t = tpreport->x;
  64.         return t;
  65. #else
  66.         return 0;
  67. #endif
  68. }

  69. int getY()//读取Y的绝对坐标
  70. {
  71. #ifdef TOUCHPAD_SUPPORT
  72.         if(!is_touchpad) return 0;
  73.         if(!tpreport->contact) return 0;
  74.         int t = tpreport->y;
  75.         return t;
  76. #else
  77.         return 0;
  78. #endif
  79. }

  80. /* 8
  81.   456
  82.    2
  83. */
  84. int getTouchedZone5()//把TouchPad分成5个区域,读取值
  85. {
  86. #ifdef TOUCHPAD_SUPPORT
  87.         if(!is_touchpad) return 0;
  88.         if(!tpreport->contact) return 0;
  89.         if(tpreport->x>tpinfo->width/3 && tpreport->x<2*tpinfo->width/3 && tpreport->y>tpinfo->height/3 && tpreport->y<2*tpinfo->height/3)
  90.                 return 5;
  91.         float m = (float)tpinfo->height/tpinfo->width;
  92.         float ac,ad;
  93.         if(tpreport->x==0)
  94.                 if(tpreport->y==0)        ac=m;
  95.                 else                        ac=tpinfo->height;
  96.         else        ac=(float)tpreport->y/tpreport->x;
  97.         if(tpreport->x==0)
  98.                 if(tpreport->y==tpinfo->height)        ad=-m;
  99.                 else                        ad=-tpinfo->height;
  100.         else        ad=(float)(tpreport->y-tpinfo->height)/tpreport->x;
  101.         if(ac<=m)
  102.                 if(ad<=-m)        return 2;
  103.                 else                 return 6;
  104.         else
  105.                 if(ad<-m)        return 4;
  106.                 else                return 8;
  107. #else
  108.         return 0;
  109. #endif
  110. }

  111. /* 8
  112.   4 6
  113.    2
  114. */
  115. int getTouchedZone4()//把TouchPad分成4个区域,读取值
  116. {
  117. #ifdef TOUCHPAD_SUPPORT
  118.         if(!is_touchpad) return 0;
  119.         if(!tpreport->contact) return 0;
  120.         float m = (float)tpinfo->height/tpinfo->width;
  121.         float ac,ad;
  122.         if(tpreport->x==0)
  123.                 if(tpreport->y==0)        ac=m;
  124.                 else                        ac=tpinfo->height;
  125.         else        ac=(float)tpreport->y/tpreport->x;
  126.         if(tpreport->x==0)
  127.                 if(tpreport->y==tpinfo->height)        ad=-m;
  128.                 else                        ad=-tpinfo->height;
  129.         else        ad=(float)(tpreport->y-tpinfo->height)/tpreport->x;
  130.         if(ac<=m)
  131.                 if(ad<=-m)        return 2;
  132.                 else                 return 6;
  133.         else
  134.                 if(ad<-m)        return 4;
  135.                 else                return 8;
  136. #else
  137.         return 0;
  138. #endif
  139. }

  140. /* 789
  141.    456
  142.    123
  143. */
  144. int getTouchedZone9()//把TouchPad分成9个区域,读取值
  145. {
  146. #ifdef TOUCHPAD_SUPPORT
  147.         if(!is_touchpad) return 0;
  148.         if(!tpreport->contact) return 0;
  149.         if(tpreport->x<=tpinfo->width/3)
  150.                 if(tpreport->y<=tpinfo->height/3)                return 1;
  151.                 else if(tpreport->y<=2*tpinfo->height/3)        return 4;
  152.                 else                                                return 7;
  153.         else if(tpreport->x<2*tpinfo->width/3)
  154.                 if(tpreport->y<=tpinfo->height/3)                return 2;
  155.                 else if(tpreport->y<2*tpinfo->height/3)                return 5;
  156.                 else                                                return 8;
  157.         else
  158.                 if(tpreport->y<=tpinfo->height/3)                return 3;
  159.                 else if(tpreport->y<2*tpinfo->height/3)                return 6;
  160.                 else                                                return 9;
  161. #else
  162.         return 0;
  163. #endif
  164. }

  165. int isTPTouched()//TP是否被触摸
  166. {
  167. #ifdef TOUCHPAD_SUPPORT
  168.         return tpreport->contact;
  169. #else
  170.         return 0;
  171. #endif
  172. }

  173. int isTPPressed()//TP是否被按下
  174. {
  175. #ifdef TOUCHPAD_SUPPORT
  176.         return tpreport->pressed;
  177. #else
  178.         return 0;
  179. #endif
  180. }

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

  2. #include <os.h>
  3. #include "touchpad.h"
  4. #include "lcd.h"

  5. int main(void) {
  6.         int x,y,lastx,lasty;
  7.         int bgcolor=0x0000;
  8.         int fgcolor=0xffff;
  9.         char connect=0;
  10.         lcd_incolor();
  11.         LCD_Fill(bgcolor);
  12.         LCD_Rect(10,220,20,230,fgcolor);
  13.         LCD_String(0,0,"Simple Paint V1.0",0xffff);
  14.         initTP();
  15.         while (!isKeyPressed(KEY_NSPIRE_ESC))
  16.         {
  17.                 wait(TOUCHPAD_DELAY);
  18.                 readTP();
  19.                 if (isTPTouched())
  20.                 {
  21.                         x=tpreport->x;
  22.                         y=tpreport->y;
  23.                         x=(x*320)/tpinfo->width;
  24.                         y=240-((y*240)/tpinfo->height);
  25.                         if (connect==1) {
  26.                                 LCD_Line(lastx,lasty,x,y,fgcolor);
  27.                         }
  28.                         else
  29.                         {
  30.                                 LCD_Point(x,y,fgcolor);
  31.                                 connect=1;
  32.                         }
  33.                         lastx=x;
  34.                         lasty=y;
  35.                 }
  36.                 else
  37.                         connect=0;
  38.                 if (isKeyPressed(KEY_NSPIRE_DEL)) LCD_Fill(bgcolor);
  39.                 if (isKeyPressed(KEY_NSPIRE_CTRL))
  40.                 {
  41.                         if (isKeyPressed(KEY_NSPIRE_1)) bgcolor=0x0000;
  42.                         if (isKeyPressed(KEY_NSPIRE_2)) bgcolor=0xFFFF;
  43.                         if (isKeyPressed(KEY_NSPIRE_3)) bgcolor=0xF800;
  44.                         if (isKeyPressed(KEY_NSPIRE_4)) bgcolor=0xFFE0;
  45.                         if (isKeyPressed(KEY_NSPIRE_5)) bgcolor=0x07E0;
  46.                         if (isKeyPressed(KEY_NSPIRE_6)) bgcolor=0x07FF;
  47.                         if (isKeyPressed(KEY_NSPIRE_7)) bgcolor=0x001F;
  48.                         LCD_Rect(10,220,20,230,fgcolor);
  49.                         LCD_Fill(bgcolor);
  50.                 }
  51.                 else
  52.                 {
  53.                         if (isKeyPressed(KEY_NSPIRE_1)) fgcolor=0x0000;
  54.                         if (isKeyPressed(KEY_NSPIRE_2)) fgcolor=0xFFFF;
  55.                         if (isKeyPressed(KEY_NSPIRE_3)) fgcolor=0xF800;
  56.                         if (isKeyPressed(KEY_NSPIRE_4)) fgcolor=0xFFE0;
  57.                         if (isKeyPressed(KEY_NSPIRE_5)) fgcolor=0x07E0;
  58.                         if (isKeyPressed(KEY_NSPIRE_6)) fgcolor=0x07FF;
  59.                         if (isKeyPressed(KEY_NSPIRE_7)) fgcolor=0x001F;
  60.                         LCD_Rect(10,220,20,230,fgcolor);
  61.                 }
  62.         }
  63.         return 0;
  64. }
复制代码
如果有什么不懂的,可以在下面问。如果看见imath来灌水,大家帮忙点下举报,谢谢了!

(返回目录)
发表于 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()
发表于 2013-7-10 20:04:45 来自手机 | 显示全部楼层
支持!~
发表于 2013-7-11 07:30:36 | 显示全部楼层
本帖最后由 Zentauit 于 2013-7-11 16:29 编辑

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

我这里顺便提供了吧:
Paint.rar (18.73 KB, 下载次数: 54)
发表于 2013-10-20 11:42:15 | 显示全部楼层
本帖最后由 中国好程序 于 2013-10-20 11:44 编辑

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

除此以外,能否解释一下那个lua.h到底是干什么的?
 楼主| 发表于 2013-10-20 15:59:38 | 显示全部楼层
中国好程序 发表于 2013-10-20 11:42
能否直接给出ndless支持的API及其所在的头文件?
这个SDK真心不习惯...

基本就是os.h libndl.h nspireio.h里面的东西。lua不太清楚
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cnCalc计算器论坛

GMT+8, 2024-4-29 04:50 , Processed in 0.087754 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表