0

I was still learning basic python programming, and was checking this program from Adafruit. And I was confused about how I am able to interrupt/stop the program by clicking CTRL + C, but in this file I don't see any "KeyboardInterrupt" function etc. It's just a string of comment from the author. Here is the code :


# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
# Author: Tony DiCola
# License: Public Domain
import time

# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# Software SPI configuration:
# CLK  = 18
# MISO = 23
# MOSI = 24
# CS   = 25
# mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Hardware SPI configuration:
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)
# Main program loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5)

Is it maybe the interrupt is mentioned in the library?

4
  • Relevant Python docs: Built-in Exceptions - KeyboardInterrupt Commented Jan 7, 2023 at 16:37
  • KeyboardInterrupt is built in to Python. Commented Jan 7, 2023 at 16:37
  • @topsail Interrupt, not terminate. Commented Jan 7, 2023 at 16:38
  • For more general background, there's Signal Handling, and more specifically, termination signals from the GNU C docs. Commented Jan 7, 2023 at 16:47

1 Answer 1

1

KeyboardInterrupt is raised by the default signal handler for the interrupt signal (SIGINT). The module most likely uses the signal library to install a custom handler that produces the output you see, rather than raising KeyboardInterrupt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.