用于控制raspihats.com板的包
raspihats的Python项目详细描述
这个包提供了从raspihats.com:
典型用法通常如下:
#!/usr/bin/env python# In this setup there are two I2C-HATs stacked, one DI16ac and one DQ10rly.fromraspihats.i2c_hatsimportDI16ac,DQ10rlydi16ac=DI16ac(0x40)# 0x40 is the I2C bus addressdq10rly=DQ10rly(0x50)# 0x50 is the I2C bus addresswhileTrue:state=di16ac.di.channels[0]# get digital input channel 0dq10rly.dq.channels[0]=state# set digital output channel 0dq10rly.dq.channels[1]=notstate# set digital output channel 1
IRQ功能(从2.3.0版开始)
Starting from hardware revision 2.0, DI16ac and DI6acDQ6rly boards can trigger an IRQ line that’s connected to GPIO21 of the Raspberry Pi.
try:importQueueasqueueexceptImportError:importqueuefromtimeimportsleepimportRPi.GPIOasGPIOfromraspihats.i2c_hatsimportDI16ac,DI6acDQ6rlyIRQ_PIN=21GPIO.setmode(GPIO.BCM)# IRQ pin setup as input with pull-up enabledGPIO.setup(IRQ_PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)# this queue is use to safely exchange information between threadsevent_queue=queue.Queue(maxsize=20)defisr(pin):event_queue.put(pin)GPIO.add_event_detect(IRQ_PIN,GPIO.FALLING,callback=isr)# b = DI16ac(0x40) # 0x40 is the I2C bus addressb=DI6acDQ6rly(0x60)# 0x60 is the I2C bus addressprint(str(b.name)+' '+str(b.fw_version))print('Use Ctrl+C to stop program.')# enable raising edge IRQs for Digital Input channels 0 and 2b.di.irq_reg.rising_edge_control=0x05# enable falling edge IRQs for Digital Input channels 1 and 2b.di.irq_reg.falling_edge_control=0x06# dump DigitalInputs IRQ CaptureQueue contents and release IRQ line by# writing 0 to DigitalInputs IRQ Capture Registerb.di.irq_reg.capture=0whileTrue:try:# wait until there is something in the queue, timeout is here because a# queue.get without a timeout can't be interrupted with a KeyboardInterruptpin=event_queue.get(block=True,timeout=0.2)ifpin==IRQ_PIN:# read the DigitalInputs IRQ Capture Register(to read the values# stored in the DigitalInputs IRQ CaptureQueue) until the# returned value is 0, this means DigitalInputs IRQ CaptureQueue# is empty and the IRQ line is releasedwhileTrue:capture=b.di.irq_reg.captureifcapture==0:breakstatus=capture&0xFFFFstates=(capture>>16)&0xFFFFforchannelinrange(0,16):mask=0x01<<channelif(status&mask)>0:print('IRQ detected on channel: %d, state: %d'%(channel,(states&mask)>>channel))exceptqueue.Empty:passexceptKeyboardInterrupt:# disable raising edge IRQs for Digital Input channelsb.di.irq_reg.raising_edge_control=0# disable falling edge IRQs for Digital Input channelsb.di.irq_reg.falling_edge_control=0GPIO.remove_event_detect(IRQ_PIN)GPIO.cleanup()break
列出属性和方法(从v2.0.0开始)
#!/usr/bin/env pythonfromraspihats.i2c_hatsimportDI6acDQ6rlyboard=DI6acDQ6rly(0x60)# 0x60 is the I2C bus addressboard.name# get board name, in this case 'DI6acDQ6rly'board.status.value# get status wordboard.reset()# reset board# cwdt - Communication WatchDog Timerboard.cwdt.period# get CommunicationWatchDogTimer(CWDT) periodboard.cwdt.period=1# set CWDT period, any value greather than 0 enables the CWDTboard.cwdt.period=0# 0 disables the CWDT# di - Digital Inputsboard.di.value# get all digital input channel states, bit 0 represents channel 0 state and so on ..board.di.channels[0]# get digital input channel 0 state, access using channel indexboard.di.channels['I0']# get digital input channel 0 state, access using channel labelboard.di.r_counters[0]# get digital input channel 0 raising edge counterboard.di.r_counters['I0']# get digital input channel 0 raising edge counterboard.di.r_counters[0]=0# reset digital input channel 0 raising edge counterboard.di.r_counters['I0']=0# reset digital input channel 0 raising edge counterboard.di.f_counters[0]# get digital input channel 0 falling edge counterboard.di.f_counters['I0']# get digital input channel 0 falling edge counterboard.di.f_counters[0]=0# reset digital input channel 0 falling edge counterboard.di.f_counters['I0']=0# reset digital input channel 0 falling edge counterboard.di.reset_counters()# reset all counters(rising and falling edge) for all channelsboard.di.labels# get digital input labels# dq - Digital Outputsboard.dq.value# get all digital output channel states, bit 0 represents channel 0 and so on ..board.dq.value=0# set all digital output channel statesboard.dq.channels[0]# get digital output channel 0 state, access using channel indexboard.dq.channels[0]=0# set digital output channel 0 stateboard.dq.channels['Q0']# get digital output channel 0 state, access using channel labelboard.dq.channels['Q0']=0# set digital output channel 0 state# PowerOnValue -- loaded to Digital Outputs at board power onboard.dq.power_on_value# get digital output channels PowerOnValue, bit 0 represents channel 0 and so on ..board.dq.power_on_value=0# set digital output channels PowerOnValue# SafetyValue -- loaded to Digital Outputs at CWDT timeoutboard.dq.safety_value# get digital output channels SafetyValue, bit 0 represents channel 0 and so on ..board.dq.safety_value=0# set digital output channels SafetyValueboard.dq.labels# get digital output labels
更改日志
2.3.0版
- Added IRQ support
v2.2.3
- enum34 is loaded for python<3.4
- Setup script warning if it’s not run with sudo(used to setup I2C ClockStretchTimeout)
v2.2.2
- Bug fix in setup script, BCM2835 platform hardware is now recognized.
- Bug fix in robotframework interface, status.value is now returned by get_status()
v2.2.1
- Added StatusWord class. To get raw int value use board.status.value, to get beautiful string representation use str(board.status).
v2.1.1
- String representation of I2CHat object doesn’t use an I2C bus transfer any more.
- Improved exception messages
v2.1.0
- Improved exception handling
v2.0.1
- Fixed I2C clock stretch timeout setup script
v2.0.0
v1.1.1
#!/usr/bin/env python# In this setup there are two I2C-HATs stacked, one Di16 and one Rly10.fromraspihats.i2c_hatsimportDi16,Rly10di16=Di16(0x40)# 0x40 is the I2C bus addressrly10=Rly10(0x50)# 0x50 is the I2C bus address# The I2C-HAT address high nibble is fixed(0x4 for Di16, 0x5 for Rly10), the low nibble# value is set using the on-board address jumper, range is [0x0 .. 0xF].whileTrue:state=di16.di_get_channel_state('Di1.1')rly10.do_set_channel_state('Rly1',state)rly10.do_set_channel_state('Rly2',notstate)
安装
安装依赖项
python smbus包
$ sudo apt-get install python-smbus # or if using python 3$ sudo apt-get install python3-smbus
从存储库安装
# Make sure you have git, pip and setuptools installed $ git clone git@github.com:raspihats/raspihats.git $cd raspihats $ sudo python setup.py install # or if using python 3$ sudo python3 setup.py install
使用pip
安装# Make sure you have pip and setuptools installed $ sudo pip install raspihats # or if using python 3$ sudo pip3 install raspihats