按键检测树莓派4

问题描述 投票:0回答:1

我正在尝试做一个在液晶显示屏上打印某些内容的项目,如果我按遥控器上的某个键,将停止打印该文本并打印其他内容。我正在寻找与“键盘”包类似的东西,但我需要它与红外遥控器等外部设备或类似的东西一起使用,而不仅仅是键盘。

例如: 液晶屏打印: 你好 StackOverflow!

5秒后 液晶打印: 嗨!

如果我按遥控器上的任意键: 液晶屏打印: 你好 StackOverflow!

检测到按键 液晶屏打印: 再见! 没有最后一部分。

我想找到一个模块/包或命令来帮助我做到这一点。

python python-2.7 raspberry-pi4 remote-control
1个回答
0
投票

只需创建一个状态机并用它来决定是否运行代码。

enum State{
  RUNNING,
  STOPPED,
};

enum State currentState = RUNNING;

void setup() {
  // configure your IR device
}

void onButton1Pressed() {
  // call this function when button 1 from IR is pressed.
  // This function changes the state to RUNNING

  currentState = RUNNING;
}

void onButton2Pressed() {
  // call this function when button 2 from IR is pressed.
  // This function changes the state to STOPPED

  currentState = STOPPED;
}

void loop() {
  // Read the IR buttons and call the onButtonPressed functions if  button is pressed

  if (currentState == STOPPED) {
    // state is STOPPED so we don't execute anything
    return;
  }

  // Reaching this point means that state is RUNNING
  // Code to execute
}
© www.soinside.com 2019 - 2024. All rights reserved.