Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import spidev
- import io # used to create file streams
- import fcntl # used to access I2C parameters like addresses
- import os
- from datetime import datetime
- import time # used for sleep delay and timestamps
- import string # helps parse strings
- import RPi.GPIO as GPIO # Importamos la libreria GPIO con sus modulos
- GPIO.setmode(GPIO.BCM) # Le decimos como queremos contar los pines
- #We use pin number 23 as input conected to 3.3V to coun the pulses and using the pull down resistors
- GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- #Setting the off button with pull up resistors
- GPIO.setup(4, GPIO.IN,pull_up_down=GPIO.PUD_UP)
- n = 0
- flag = False
- p = 0
- spi =spidev.SpiDev()
- spi.open(0, 0)
- #A function to make sure that there are pulses but its not relevant for #my problem
- def comprobar(f):
- global flag, p
- if f == 1:
- flag = True
- else:
- flag = False
- return flag
- #Temperature non relevant
- def leeradc(numadc):
- r = spi.xfer2([1, 8 + numadc << 4, 0])
- adcout = ((r[1] & 3 ) << 8) + r[2]
- return adcout
- # The off interruption
- def apagar(channel):
- os.system("echo $(sudo shutdown -h now)")
- time.sleep(1)
- #Pulse interruption as you can see and I explained if i uncomment #os.system if works fine with this interruption it’s the other one that #doesn’t work
- def interrupcion(channel):
- #os.system("echo $(sudo shutdown -h now)")
- global n,p
- p = 1
- comprobar(p)
- n = n + 1
- return n
- #Non relevant code thats just for reading the sensors using I2C and SPI for a temperature
- # Main loop where i call the add_event detect
- def main():
- device = atlas_i2c() # creates the I2C port object, specify the address or bus if necessary
- GPIO.add_event_detect(23, GPIO.FALLING, callback = interrupcion, bouncetime = 800)
- GPIO.add_event_detect(4, GPIO.RISING, callback = apagar, bouncetime=600)
- global flag, n
- print(">> Atlas Scientific sample code")
- print(">> Any commands entered are passed to the board via I2C except:")
- print(">> Address,xx changes the I2C address the Raspberry Pi communicates with.")
- print(">> Poll,xx.x command continuously polls the board every xx.x seconds")
- print(" where xx.x is longer than the %0.2f second timeout." % atlas_i2c.long_timeout)
- print(" Pressing ctrl-c will stop the polling")
- global p
- # main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement