查看: 2294|回复: 1

[原创] 【 钛极小龟】按键Button

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2018-11-23 17:22:52 | 显示全部楼层 |阅读模式
    分享到:
    做这个按键Button实际上比较麻烦,钛极小龟的开发板上提给用户的两类按键,一类是机械按键,另个一类是触摸按键
    机械按键虽然有三个,但实际上给用户应用的按键就一个。

    触摸按键 使用的是 import tijos.framework.sensor.button.TiButton  ITiButtonEventListener ; 做的
    可是机械按键 使用的是 import tijos.framework.platform.peripheral.TiKeyboard; ITiKeyboardListener 来做的,使上述的方法是无法使用。
    具体的代码如下:
    1. import tijos.framework.devicecenter.TiGPIO;
    2. import tijos.framework.platform.peripheral.ITiKeyboardListener;
    3. import tijos.framework.platform.peripheral.TiKeyboard;
    4. import tijos.framework.sensor.button.ITiButtonEventListener;
    5. import tijos.framework.sensor.button.TiButton;
    6. import tijos.framework.util.Delay;

    7. public class TiJOSButton {

    8.         public static void main(String[] args) {
    9.                 // TODO Auto-generated method stub
    10.                 try {
    11.                         /*
    12.                          * 定义使用的TiGPIO port
    13.                          */
    14.                         int gpioPort0 = 0;
    15.                         /*
    16.                          * 定义使用的TiGPIO pin
    17.                          */
    18.                         int gpioPin6 = 6;
    19.                         int gpioPin4 = 4;
    20.                         /*
    21.                          * 资源分配, 将gpioPort0与gpioPin0/1/3/4分配给TiGPIO对象gpio0
    22.                          */
    23.                         TiGPIO gpio0 = TiGPIO.open(gpioPort0, gpioPin4,gpioPin6);
    24.                         /*
    25.                          * 资源绑定, 创建TiButton对象buttonS1/S2/S3/S4并将gpio0和gpioPin0/1/3/4与其绑定
    26.                          */
    27.                         //TiButton buttonS1 = new TiButton(gpio0, gpioPin6,false);
    28.                         TiButton buttonTouch = new TiButton(gpio0, gpioPin4,true);//触发电平:高电平
    29.                         // 创建监听对象
    30.                         KeyEventListener keyLc = new KeyEventListener();
    31.                         TiKeyboard keyBoard = TiKeyboard.getInstance();
    32.                         /*
    33.                          * 资源使用, 创建事件监听对象并设置事件监听 在事件监听中处理按键事件逻辑
    34.                          */
    35.                         ButtonEventListener lc = new ButtonEventListener();
    36.                         //buttonS1.setEventListener(lc);
    37.                         buttonTouch.setEventListener(lc);
    38.                         keyBoard.setEventListener(keyLc);// 设置键盘事件监听对象
    39.                         while (true) {
    40.                                 Delay.msDelay(1000);
    41.                         }
    42.                 }catch (Exception e) {
    43.                         // TODO: handle exception
    44.                         e.printStackTrace();
    45.                 }
    46.         }
    47. }


    48. /**
    49. * 1.此类实现了ITiButtonEventListener四按键事件监听接口
    50. * 在onPressed与onReleased方法中不要阻塞处理事件太久,
    51. * 因为TiJOS系统所有事件分发在同一个事件监听线程中,若阻塞
    52. * 太久会影响其他事件的分发,导致事件响应不及时甚至丢失。
    53. *
    54. * 2.如果实际应用中,需要较长时间来处理某事件,建议在新的线程中
    55. * 处理。
    56. */
    57. class ButtonEventListener implements ITiButtonEventListener{

    58.         @Override
    59.         /**
    60.          * 按键按下事件处理
    61.          */
    62.         public void onPressed(TiButton button) {
    63.                 // TODO Auto-generated method stub
    64.                 System.out.println("onPressed" + "  time(us):" + button.getEventTime() + "  buttonPinID:" + button.getSignalPinID());
    65.         }

    66.         @Override
    67.         /**
    68.          * 按键释放事件处理
    69.          */
    70.         public void onReleased(TiButton button) {
    71.                 // TODO Auto-generated method stub
    72.                 System.out.println("onReleased" + "  time(us):" + button.getEventTime() + "  buttonPinID:" + button.getSignalPinID());
    73.         }
    74. }

    75. class KeyEventListener implements ITiKeyboardListener{

    76.         @Override
    77.         public void onPressed(int id, long time) {
    78.                 // TODO Auto-generated method stub
    79.                 System.out.println("onPressed" + "  time(us):" + time + "  buttonPinID:" + id);
    80.         }

    81.         @Override
    82.         public void onReleased(int id, long time) {
    83.                 // TODO Auto-generated method stub
    84.                 System.out.println("onReleased" + "  time(us):" + time + "  buttonPinID:" + id);
    85.         }
    86.        
    87. }
    88.        
    复制代码
    TiButton  ITiButtonEventListener  两个类的代码如下:
    button.rar (1.3 KB, 下载次数: 0, 售价: 1 与非币)
    回复

    使用道具 举报

  • TA的每日心情

    2023-7-25 22:49
  • 签到天数: 385 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2018-11-26 17:04:30 | 显示全部楼层
    谢谢分享…… null9.png null8.png

    null7.png null6.png

    null5.png null4.png

    null3.png null2.png

    null1.png null0.png


    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 16:22 , Processed in 0.118732 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.