Advertisement
Guest User

Untitled

a guest
Nov 14th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #!/usr/bin/python
  2. import spidev
  3. import io # used to create file streams
  4. import fcntl # used to access I2C parameters like addresses
  5. import os
  6. from datetime import datetime
  7. import time # used for sleep delay and timestamps
  8. import string # helps parse strings
  9. import RPi.GPIO as GPIO # Importamos la libreria GPIO con sus modulos
  10.  
  11.  
  12. GPIO.setmode(GPIO.BCM) # Le decimos como queremos contar los pines
  13.  
  14.  
  15. #We use pin number 23 as input conected to 3.3V to coun the pulses and using the pull down resistors
  16. GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  17.  
  18. #Setting the off button with pull up resistors
  19. GPIO.setup(4, GPIO.IN,pull_up_down=GPIO.PUD_UP)
  20.  
  21. n = 0
  22. flag = False
  23. p = 0
  24. spi =spidev.SpiDev()
  25. spi.open(0, 0)
  26.  
  27. #A function to make sure that there are pulses but its not relevant for #my problem
  28. def comprobar(f):
  29. global flag, p
  30. if f == 1:
  31. flag = True
  32. else:
  33. flag = False
  34. return flag
  35. #Temperature non relevant
  36. def leeradc(numadc):
  37. r = spi.xfer2([1, 8 + numadc << 4, 0])
  38. adcout = ((r[1] & 3 ) << 8) + r[2]
  39. return adcout
  40.  
  41. # The off interruption
  42. def apagar(channel):
  43. os.system("echo $(sudo shutdown -h now)")
  44. time.sleep(1)
  45.  
  46. #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
  47. def interrupcion(channel):
  48. #os.system("echo $(sudo shutdown -h now)")
  49. global n,p
  50. p = 1
  51. comprobar(p)
  52. n = n + 1
  53. return n
  54. #Non relevant code thats just for reading the sensors using I2C and SPI for a temperature
  55. # Main loop where i call the add_event detect
  56. def main():
  57. device = atlas_i2c() # creates the I2C port object, specify the address or bus if necessary
  58. GPIO.add_event_detect(23, GPIO.FALLING, callback = interrupcion, bouncetime = 800)
  59. GPIO.add_event_detect(4, GPIO.RISING, callback = apagar, bouncetime=600)
  60. global flag, n
  61. print(">> Atlas Scientific sample code")
  62. print(">> Any commands entered are passed to the board via I2C except:")
  63. print(">> Address,xx changes the I2C address the Raspberry Pi communicates with.")
  64. print(">> Poll,xx.x command continuously polls the board every xx.x seconds")
  65. print(" where xx.x is longer than the %0.2f second timeout." % atlas_i2c.long_timeout)
  66. print(" Pressing ctrl-c will stop the polling")
  67. global p
  68. # main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement