Beagle Board - beagleboard.org
HRM Resident
Published © GPL3+

Who says Python/Linux can't accept interrupts?

A simple demo to show how to write an ISR on a BBB in Python running Debian.

BeginnerProtip1 hour748
Who says Python/Linux can't accept interrupts?

Things used in this project

Story

Read more

Schematics

BBB simple push-button Interrupt diagram

Code

pi_Int.py

Python
Simply run it from the command line:

debian@puppy:~/python_play$ python3 pi_int.py
Press the button. I'm counting them while calculating Pi to 100 places, Use ctrl-C to exit.
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798
^C
Keyboard interrupt

Detected 4 button presses
GPIO Cleaned
debian@puppy:~/python_play$
#
# How to write an ISR (a 'callback' in Python speak) in Python 3.7.3 on a
# ARM Element 14 BeagleBone Black Rev C - 4GB running Debian Buster IoT
# image 2020-04-06. I'm pretty sure  this general concept will work on a
# Raspberry Pi, but I haven't tried it.
#
# Calculate pi to 100 places while constantally being interrupted by button pushes
#
# We need to use the Adafruit GPIO library for the BeagleBone Black. This is typically
# pre-insalled with the O/S.  If not, instructions for doing so are found here:
#
# https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/installation-on-ubuntu
# (Link says ububtu, but it also covers Debian.  Also substutute pip3 and python3 for pip and python
#
# HRM Resident - 2021-06-26
#
#
import Adafruit_BBIO.GPIO as GPIO
import time
from math import factorial
from decimal import Decimal, getcontext
getcontext().prec = 120
#
times=0
bounce=40
#
# The right header is designated as P8 and the left is designated P9
# I am  using header P9 and pin 12, a/k/a GPIO_60
#
Pin = "P9_12"
def ISR_P9_12(channel):  # This is the ISR that's invoked when a pulse is detected
    global times
    times = times + 1
#
# Clear the interrupt and re-enable it for the next time
#
    GPIO.remove_event_detect(Pin)
    time.sleep(0.1)
    GPIO.add_event_detect(Pin, GPIO.RISING, callback=ISR_P9_12, bouncetime=bounce)
#
# Initial set up of the ISR to fire on the rising edge of a pulse applied to GPIO bank P9, pin 12
#
GPIO.setup(Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(Pin, GPIO.RISING, callback=ISR_P9_12, bouncetime=bounce)
#
# Calculate pi using the Chudnovsky algorithm as per:
# https://www.craig-wood.com/nick/articles/pi-chudnovsky/
#
def cal_pi(digits):
    t= Decimal(0)
    pi = Decimal(0)
    deno= Decimal(0)

    for k in range(digits):
        t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
        deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
        pi += Decimal(t)/Decimal(deno)
    pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
    pi = 1/pi
    return round(pi,digits+1)
#
# Main loop. Repeditly computes Pi to keep things busy.
#
try:
    print("Press the button. I'm counting them while calculating Pi to 100 places, Use ctrl-C to exit.")
    while True:
      print(cal_pi(100))
except KeyboardInterrupt:
    print()
    print("Keyboard interrupt")
    print()
    print("Detected",times,"button presses")
    GPIO.cleanup()
    print("GPIO Cleaned")

Credits

HRM Resident

HRM Resident

1 project • 0 followers

Comments

Add projectSign up / Login