Advertisement
neuberfran

Untitled

Sep 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import mraa
  2. import time
  3.  
  4. # This is a simple light switch code
  5. # connect Touch Sensor to the D4 and the Relay to the D8
  6.  
  7. # inside a python interupt you cannot use 'basic' types so you'll need to use objects
  8. class Toggle:
  9. state = 0
  10.  
  11. t=Toggle()
  12.  
  13. # D8 for the relay
  14. mraa.Gpio(8).dir(mraa.DIR_OUT)
  15.  
  16. # D4 for the touch sensor
  17. mraa.Gpio(4).dir(mraa.DIR_IN)
  18.  
  19. def pressed(args):
  20. if t.state==0:
  21. mraa.Gpio(8).write(1)
  22. t.state = 1
  23. else:
  24. mraa.Gpio(8).write(0)
  25. t.state = 0
  26.  
  27. # here the interrupt is set on Gpio(4)
  28. # fourth argument is not used
  29. mraa.Gpio(4).isr(mraa.EDGE_RISING, pressed, pressed)
  30.  
  31. # Delay destructors on script termination otherwise ISR will not run
  32. time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement