gpio.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. # Copyright 2016 by MPI-SWS and Data-Ken Research.
  2. # Licensed under the Apache 2.0 License.
  3. """Raspberry Pi GPIO Sensor for AntEvents.
  4. Allows digital (1/0 output) sensors to be connected straight to the
  5. Raspberry Pi (ADC needed for the Pi to take analogue output).
  6. This sensor class can only be used with sensors which send their output straight
  7. to the Raspberry Pi GPIO pins. For sensors which use I2C or SPI, with their
  8. own registers, a library to use them has to be written separately.
  9. """
  10. from thingflow.base import OutputThing, IndirectOutputThingMixin
  11. import RPi.GPIO as GPIO
  12. class RPISensor(OutputThing, IndirectOutputThingMixin):
  13. """Sensor connected to Raspberry Pi. Output of sensor is digital
  14. (RPi does not come with an ADC unlike the Arduino)
  15. """
  16. def __init__(self,sensor_id):
  17. """sensor_id is port number
  18. """
  19. super().__init__()
  20. self.sensor_id = sensor_id
  21. GPIO.setmode(GPIO.BOARD)
  22. GPIO.setup(sensor_id,GPIO.IN)
  23. def sample(self):
  24. val = GPIO.input(self.sensor_id)
  25. return val
  26. def __str__(self):
  27. return 'Raspberry Pi Sensor (port=%s)'% self.sensor_id