I have updated the code to look like this:
import RPi.GPIO as GPIO
import time
import datetime
BUTTON_PIN = 16
GPIO.setmode(GPIO.BCM)
class TimedButton:
def __init__(self, pin, callback, pull_up_down=GPIO.PUD_UP, package=None):
"""
A TimedButton initiates a callback after a button has been pressed and then released. It passes the duration
the button was pressed to the callback
:param pin: pin the button is on
:param callback: callback to call when pressed
:param pull_up_down: indicates the button is pulled up or down with a resistor
:param package: package to pass to the callback
"""
self.pin = pin
self.package = package
self.last_push = datetime.datetime.now()
self.callback = callback
self.press_time = None
GPIO.setup(pin, GPIO.IN, pull_up_down=pull_up_down)
GPIO.add_event_detect(pin, edge=GPIO.BOTH, callback=self._debounce_function)
def _debounce_function(self, pin):
"""
This function debounces the button. Buttons are inherently noisy (they ring when pressed) This function waits
a period of time before declaring the button to have settled into the current state.
:param pin: pin the button is on
:return: None
"""
time_now = datetime.datetime.now()
current_state = GPIO.input(self.pin)
if (time_now - self.last_push).microseconds > .1 * units.microseconds_per_second:
if current_state and self.press_time is not None:
self.callback(pin, datetime.datetime.now() - self.press_time, self.package)
else:
self.press_time = datetime.datetime.now()
self.last_push = time_now
def button_callback(pin, state, argument):
print('{} Pin {} now at {}. Message:{}'.format(datetime.datetime.now(), pin, state, argument))
def main():
import RPi.GPIO as GPIO
print('Starting test_Button')
GPIO.remove_event_detect(BUTTON_PIN)
TimedButton(pin=BUTTON_PIN, callback=button_callback)
while True:
time.sleep(1)
if __name__ == "__main__":
main()
I have tried:
- sudoing as root
- running as root
- changing ownership of /dev/mem
- reinstalling rpi.gpio
I always get the same error: RuntimeError: No access to /dev/mem. Try running as root!
Here is what I have tried: (as pi):
pi@snail-patrol:python3 test.py
Starting test_Button
Traceback (most recent call last):
File "test.py", line 56, in <module>
main()
File "test.py", line 49, in main
GPIO.remove_event_detect(BUTTON_PIN)
RuntimeError: No access to /dev/mem. Try running as root!
(su to root - note tried su without the - also)
pi@snail-patrol:sudo su -
root@snail-patrol:/home/pi/temp# python3 test.py
Starting test_Button
Traceback (most recent call last):
File "test.py", line 56, in <module>
main()
File "test.py", line 49, in main
GPIO.remove_event_detect(BUTTON_PIN)
RuntimeError: No access to /dev/mem. Try running as root!
(sudo)
pi@snail-patrol:sudo python3 test.py
Starting test_Button
Traceback (most recent call last):
File "test.py", line 56, in <module>
main()
File "test.py", line 49, in main
GPIO.remove_event_detect(BUTTON_PIN)
RuntimeError: No access to /dev/mem. Try running as root!
Here is what the permissions look like
pi@snail-patrol:ls -l /dev/mem
crw-r----- 1 root kmem 1, 1 Jul 17 20:56 /dev/mem
pi@snail-patrol:ls -l test.py
-rw-r--r-- 1 pi pi 2110 Jul 18 17:01 test.p
Note that I also tried:
sudo rpi-update
sudo reboot
sudo apt-get update
sudo apt-get upgrade
sudo adduser pi gpio
sudo chown root.gpio /dev/mem && sudo chmod g+rw /dev/mem
And I still get the same error!