python - Raspberry Pi 和 PiFace Digital 的中断

标签 python linux raspberry-pi interrupt interrupt-handling

我刚刚使用 PiFace Digital element14 I/O 板设置了 Raspberry Pi。到目前为止,我已经按照几个步骤让它工作,这样我就可以连接 I/O 端口(控制 LED 并操作开关来做事)我写的 python 代码工作正常,我可以让它做事.

目前我只是在闲逛,感受在 Pi 上编程的感觉,并试图了解这些功能。我想为一个简单的 6 位二进制计数器设置一个状态机,当我告诉它时,它会向上和向下计数,我可以很容易地做到这一点。但是,当我尝试将其提升到一个新的水平并使用中断来设置状态时,我遇到了问题。

我关注了 Manual以及This Guide获取激活中断的代码。

我编写的代码执行时没有错误,但是,要么未检测到中断,要么它们什么都不做,我不确定是哪一个。我的代码如下。我知道 while 循环适用于“等待”和“计数”状态,因为我可以定义初始条件。它计数正确,所以我非常确定 while 循环没问题,只是没有状态变化。

import pifacedigitalio as pfio
import os
import time

def startCounter(event):
 global state
 state = 'counting'
 print('counter started')

def stopCounter(event):
 global state
 state = 'waiting'

def stopProg(event):
 global state
 state = 'stop'

def resetCounter(event):
 global state
 state = 'reset'

def setLEDs(stateArray):
 i = 0
 for state in stateArray:
  pfio.digital_write(i,state)
  i = i + 1

def calcBools(count):
 binString = bin(count).rsplit('0b')[1]
 stringLength = len(binString)
 zeroString = '0' * (8 - stringLength)
 newString = zeroString + binString
 i = 0
 boolsOut = [0,0,0,0,0,0,0,0]
 for bit in newString:
  if bit == '1':
   boolsOut[i] = 1
  i = i + 1
 return boolsOut

####################
### MAIN PROGRAM ###
####################

pfio.init()

pifacedigital = pfio.PiFaceDigital()
listener = pfio.InputEventListener(chip=pifacedigital)

signalDirection = pfio.IODIR_RISING_EDGE
listener.register(0, signalDirection, stopProg)
listener.register(1, signalDirection, startCounter)
listener.register(2, signalDirection, stopCounter)
listener.register(3, signalDirection, resetCounter)
listener.activate()

counter = 0
running = True
state = 'waiting'
setLEDs([0,0,0,0,0,0,0,0])
direction = 'up'

while(running):
 if state == 'stop':
  running = False
  listener.deactivate()
  counter = 0
 elif state == 'waiting':
  time.sleep(1)
  print('waiting...')
 elif state == 'counting':
  if direction == 'up':
   counter = counter + 1
  else:
   counter = counter - 1
  if counter > 63:
   direction = 'down'
  elif counter == 0:
   direction = 'up'
 elif state == 'reset':
  counter = 0
 else:
  time.sleep(0.1)

 setLEDs(calcBools(counter))
 print(counter)
 time.sleep(0.25)

所以这段代码不起作用,我尝试了一些使用 pifacecommon 库也不起作用的东西,将一些代码行替换为:

import pifacecommon as pfc

readport = pfc.mcp23s17.GPIOA    # I also tried GPIOB to no avail
listener = pfc.interrupts.PortEventListener(readport, 0)

在此之后,两种方法的监听器命令是相同的。与此同时,我尝试使用 pfc.mcp23s17.write 命令,但显然它不存在或类似的愚蠢借口。

在此先感谢您阅读本文,如果您做出回应,我们将更加感谢您,如果您对我有答案,我们将更加感谢您!

-本

编辑(已解决):我的回答在我一直提供的第二个链接的评论中 :( 结果我写的一切都正确,我只需要从终端而不是 IDLE3 运行文件。

最佳答案

所以我的问题的答案在 2nd link 的评论中我提供了整个时间。我需要做的就是从终端运行程序,而不是 IDLE3。

我认为原因在于 Linux 中处理中断的方式。 Linux ISR 不是真正的 ISR,处理器将 ISR 置于 sleep 模式,直到它被调用。谁能详细说明为什么 IDLE3 不支持中断?

关于python - Raspberry Pi 和 PiFace Digital 的中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32429320/

相关文章:

linux - 由于 crontab 被锁定在 Raspberry Pi 之外

python - 使用 Python 的 PLAXIS API

python - 重组文本、文档字符串和 python 交互式 shell

linux - 在 Nginx 上将 http 重写为 https

java - 在java中为八 channel 声卡选择输出行

node.js - execSync 和 ffmpeg 执行差距

python - 如何在 MongoDB 中构建表单数据(Python/Flask)

python - 如何使用scrapy提取动态加载图片的src

linux - 将值从 bash for 循环转换为 json 对象

linux - Unix tar 返回参数列表太长