用Python使用pcDuino的外部中断
来源:互联网 发布:淘宝客服美女头像 编辑:程序博客网 时间:2024/05/18 01:14
pcDuino上有两个引脚可作外部中断(GPIO2和GPIO3),在网上找了下相关例子,基本上是这两个例子:
- 手把手教你使用pcDuino的外部中断 (http://cnlearn.linksprite.com/?p=1745
- 在pcDuino上使用Arduino风格的中断处理(http://cnlearn.linksprite.com/?p=824)
前一个是写驱动,对于我这种刚接触pcDuino的人又没有写驱动的经验的人来说,基本是看不懂,后一个是Arduio风格,使用c语言,但我使用的是Python,不想重写其它代码。
一番google后,找到了pcDuino - Interrupt Tutorial(http://digitalhacksblog.blogspot.com/2013/09/pcduinointerrupt-tutorial.html)这个教程,他也是用c语言写的, 不过不是Arduino风格,他的代码是:
#include <stdio.h> #include <signal.h> #include <stdlib.h> #include <inttypes.h> #include <sys/ioctl.h> #include <fcntl.h><p></p>// sw_interrupt must be loaded if it isn't load using // modprobe sw_interrup<p></p>// Data structure for defining an interrupt on the pcDuino typedef struct swIRQ { uint8_t channel; int mode; int pid; } swIRQt,*irqSWp; // Pseudo device for controlling interrupts static const char *swirq_dev = "/dev/swirq"; #define SWIRQ_START (0x201) #define SWIRQ_STOP (0x202) #define SWIRQ_SETPID (0x203) #define SWIRQ_ENABLE (0x204) #define SWIRQ_DISABLE (0x205) #define SWIRQ_RISING (0x00) #define SWIRQ_FALLING (0x01) #define SWIRQ_HIGH (0x02) #define SWIRQ_LOW (0x03) #define SWIRQ_CHANGE (0x04) #define SWIRQ_PIN1 (0x0) #define SWIRQ_PIN2 (0x1) int main(int _argc, char **_argv) { int fd; int ret; uint8_t swIrqNum = 0; swIRQt swIrqCfg; // Define ISR int irqPin1Cnt = 0; int irqPin1Func (void) { irqPin1Cnt++; } // Attach the ISR to the USR1 signal which is triggered by the OS when the interupt is signaled. signal(SIGUSR1, (void (*) (int))irqPin1Func); // Setup to use pin1 / gpio2 when the signal goes from low to high. swIrqCfg.channel = SWIRQ_PIN1; swIrqCfg.mode = SWIRQ_RISING; swIrqCfg.pid = getpid(); swIrqNum = SWIRQ_PIN1; // Connect a file descriptor to the pseudo device used to control interrupts fd = open(swirq_dev, O_RDONLY); if ( fd < 0 ) { printf("open swirq device fail"); exit(0); } // Disable interrupts using pin1 / gpio2 ret = ioctl(fd, SWIRQ_STOP, &swIrqNum); if (ret < 0) { printf("can't set SWIRQ_STOP"); exit(0); } // Configure pin1 / gpio2 interrupts ret = ioctl(fd, SWIRQ_SETPID, &swIrqCfg); if (ret < 0) { printf("can't set SWIRQ_SETPID"); exit(0); } // Enable interrupts on pin1 / gpio2 ret = ioctl(fd, SWIRQ_START, &swIrqNum); if (ret < 0) { printf("can't set SWIRQ_START"); exit(0); } // Disconnect from the pseudo device if (fd) close(fd); // Loop forever. Print out the counter when the ISR has been called. int oldValue = 0; while (2>1) { if (oldValue != irqPin1Cnt) { printf("irqPin1Funct: %d\n", irqPin1Cnt); oldValue = irqPin1Cnt; } } }
他的代码虽然是c写的,不过可以用Python来重写,靠加查文档边写代码,试着用Python来实现。下面是我写的代码:
import osimport sysimport signalimport structimport fcntlswirq_dev = "/dev/swirq"SWIRQ_START = 0x201 SWIRQ_STOP = 0x202 SWIRQ_SETPID = 0x203 SWIRQ_ENABLE = 0x204 SWIRQ_DISABLE = 0x205 SWIRQ_RISING = 0x00 SWIRQ_FALLING = 0x01 SWIRQ_HIGH = 0x02 SWIRQ_LOW = 0x03 SWIRQ_CHANGE = 0x04 SWIRQ_PIN1 = 0x0SWIRQ_PIN2 = 0x1irqPin1Cnt = 0def irqPin1Func(a, b): global irqPin1Cnt irqPin1Cnt = irqPin1Cnt + 1signal.signal(signal.SIGUSR1, irqPin1Func)pid = os.getpid()fd = open(swirq_dev)swIrqNum = SWIRQ_PIN1fcntl.ioctl(fd, SWIRQ_STOP, struct.pack("@B", swIrqNum))fcntl.ioctl(fd, SWIRQ_SETPID, struct.pack("@Bii", SWIRQ_PIN1, SWIRQ_RISING, pid))fcntl.ioctl(fd, SWIRQ_START, struct.pack("@B", swIrqNum))fd.close()oldValue = 0while True: if oldValue != irqPin1Cnt: print irqPin1Cnt oldValue = irqPin1Cnt我的代码基本上是用用Python改写他的代码,用到了signal、struct、fcntl这几个模块,这里要注意使用struct.pack时的格式问题,符上官方文档:
struct — Interpret strings as packed binary data(https://docs.python.org/2/library/struct.html)
字节顺序我用的是“@”,试出来的,其它的都不行。
这个程序要用root身份运行,sudo python interrupt.py,可以运行。分享一下。
0 0
- 用Python使用pcDuino的外部中断
- 外部中断的使用~~
- 外部中断的使用
- STM32外部中断的使用
- LPC2478的外部中断使用
- STM32外部中断的使用
- STM32F1外部中断的使用
- STM32学习笔记:外部中断的使用
- TI C6727外部中断的使用
- STM32学习笔记:外部中断的使用
- 转:STM32外部中断的使用
- STM32学习笔记:外部中断的使用
- STM32学习笔记:外部中断的使用
- STM32学习笔记:外部中断的使用
- avr的外部中断
- S3C2410的外部中断
- STM32的外部中断
- stm8l051的外部中断
- hdu 2062 Subset sequence
- POJ 3083 Children of the Candy Corn(基础题)
- extjs 前端注重点
- 2286 The Rotation Game (1,状压 2,迭代加深搜索)
- 基于数据中心的VM迁移算法总结(1)
- 用Python使用pcDuino的外部中断
- 用gcc编译opencv程序出错
- 【CSE 104 – Assignment 01 – Java单链表建立】
- oracle在windows命令行启动
- 仿QQ5.0缩放式侧滑栏的实现
- lua函数说明
- STL中 如何在遍历中使用 iterator/reverse_iterator 删除元素
- visitedUserIdvisitedUserIdvisitedUserIdvisitedUserIdvisitedUserIdvisitedUserIdvisitedUserIdvisitedUs
- Cocos2d-JS环境配置教程