设为首页 收藏本站
查看: 613|回复: 0

[经验分享] 用python 控制gpio

[复制链接]

尚未签到

发表于 2015-4-26 11:16:46 | 显示全部楼层 |阅读模式
Blinky, Button & GPIO examples using Python
  
   
   
           
             Contents
             [hide]            
                
  • 1 Examples using Python                 
                       
    • 1.1 Blinky Example                  
    • 1.2 Button Example                  
    • 1.3 GPIO Example               
               
         
       Examples using Python
  The following application note covers the use of Electrum100's peripherals using Python. SYSFS method is used to access the GPIO's. To create the interfaces for GPIO's under /sys/class/gpio/*, the following configuration has to be enabled in the board configuration file
CONFIG_GPIO_SYSFS=y
Blinky Example
  User LED is connected to GPIO PA30. An interface for the user LED is created in the kernel by adding the following code in board-electrum-100.c

/*
* LEDs
*/
static struct gpio_led ek_leds[] = {
{/* led1, yellow */
.name= "ds1",
.gpio= AT91_PIN_PA25,
.default_trigger= "heartbeat",
},
{/* led2, green */
.name= "ds2",
.gpio= AT91_PIN_PA30,
//.active_low= 1,
.default_trigger= "none",
}
};
  And add the following lines in ek_board_init()

/* LEDs */
at91_gpio_leds(ek_leds, ARRAY_SIZE(ek_leds));
  Python script for Blinky Example:

import sys
import time
print "Blinking User LED"
print "Enter CTRL+C to exit"
def ledon():
value = open("/sys/class/leds/ds2/brightness","w")
value.write(str(1))
value.close()
def ledoff():
value = open("/sys/class/leds/ds2/brightness","w")
value.write(str(0))
value.close()
while True:
ledon()
time.sleep(.5)
ledoff()
time.sleep(.5)
Button Example
  In Electrum100, GPIO PA31 is used for the User Button.

import sys
import time
print "Button Example"
print "Press CTRL + C to exit"
def pins_export():
try:
pin1export = open("/sys/class/gpio/export","w")
pin1export.write(str(63))
pin1export.close()
except IOError:
print "INFO: GPIO 63 already Exists, skipping export gpio"
def read_button():
fp1 = open("/sys/class/gpio/gpio63/value","r")
value = fp1.read()
return value
fp1.close()
def write_led(value):
fp2 =  open("/sys/class/leds/ds2/brightness","w")
fp2.write(str(value))
fp2.close()
pins_export()
while True:
button_value =  read_button()
write_led(button_value)
GPIO Example
  The following example allows the user to set a selected GPIO pin as an Input or Output and read the values from the pins configured. As some of the GPIO's have alternate Functions, it is required that, pins have to be initialized as GPIO's before executing the following test example. set-gpio is a C-binary that initializes a single pin at a time. (Default as Input Pin)

import sys
import time
import re
def setpins(pin_no, pin_direction):
gpioopnum = "gpio%s" % (str(pin_no), )
pin1dir = open("/sys/class/gpio/"+gpioopnum+"/direction","w")
if pin_direction == 1:
pin1dir.write("high")
else:
pin1dir.write("in")
pin1dir.close()
def readpins(pin_no):
gpioopnum = "gpio%s" % (str(pin_no), )
pin1val = open("/sys/class/gpio/"+gpioopnum+"/value","r")
output = pin1val.read()
print "The value on the PIN %s is : %s" % (str(pin_no), str(output))
pin1val.close()
def unexport_pins(pins):
try:
fp4 = open("/sys/class/gpio/unexport","w")
fp4.write(str(pins))
fp4.close()
except IOError:
print "GPIO %s is not found, so skipping unexport gpio" % (str(pins), )
def export_pins(pins):
try:
fp1 = open("/sys/class/gpio/export","w")
fp1.write(str(pins))
fp1.close()
except IOError:
print "GPIO %s already Exists, so skipping export gpio" % (str(pins), )
print "Warning: Make sure that C-binary to Initialize GPIO's is executed prior to this. The script may not work as intended if the Ports are not initialized properly"+"\n"
for i in range(1 , len(sys.argv)):
export_pins(sys.argv)
direction = raw_input(" Configure PIN %s as OUTPUT(1) / INPUT(2) ?: " % (str(sys.argv), ))
setpins(sys.argv, int(direction))
print "\n"+"            Reading PIN values....."
for i in range(1 , len(sys.argv)):
readpins(sys.argv)
for i in range(1 , len(sys.argv) ):
unexport_pins(sys.argv)
  Procedure:

electrum100:/home/python# set-gpio 32
PIOA 0 set as Input
electrum100:/home/python# set-gpio 55
PIOA 23 set as Input
  Before Connecting the Short cable between PA0 and PA23

electrum100:/home/python# python gpio1.py 32 55
Warning: Make sure that C-binary to initialize GPIO's is executed prior to this. The script may not work as intended if the Ports are not initialized properly
Configure PIN 32 as OUTPUT(1) / INPUT(2) ?: 1
Configure PIN 55 as OUTPUT(1) / INPUT(2) ?: 2
Reading PIN values.....
The value on the PIN 32 is : 1
The value on the PIN 55 is : 0
  After connecting the Short cable between PA0 and PA23

electrum100:/home/python# python gpio1.py 32 55
Warning: Make sure that C-binary to initialize GPIO's is executed prior to this. The script may not work as intended if the Ports are not initialized properly
Configure PIN 32 as OUTPUT(1) / INPUT(2) ?: 1
Configure PIN 55 as OUTPUT(1) / INPUT(2) ?: 2
Reading PIN values.....
The value on the PIN 32 is : 1
The value on the PIN 55 is : 1
  The convention used to represent the Pin numbers using SYSFS procedure is explained with examples.

PA Base - 32
PB Base - 64
PC Base - 96
Ex: To initialize PA1, use PA-Base + 1 = 32 + 1 = 33
PA31, use 32 + 31 = 63
PB0, use PB-Base + 0 = 64 + 0 = 64
PC10, use 96 + 10 = 106

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60838-1-1.html 上篇帖子: 菜鸟说python中的继承 下篇帖子: python httplib2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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