Skip to main content

A Python library to simulate Raspberry Pi GPIO devices in TkInter

Project description

tkgpio

A Python library to simulate electronic devices connected to the GPIO on a Raspberry Pi, using TkInter.

About

Due to the Coronavirus outbreak in 2020, students enrolled in my Microcontroller course could not use the laboratory. So I decided to build a Raspberry Pi circuit simulator, to make remote class activities possible. And it works pretty well!

The main goal here is to enable students to use the same APIs that control physical devices connected to the GPIO, but interacting with a GUI instead. I'm not worried about creating a realistic electronic simulation.

Since we were already using Python to program on Raspberry Pi, I've built the interface with TkInter. Some libraries (like gpiozero) allowed me to easily mock the devices in the GUI. In other cases, I had to reimplement the API inside the library – but since I can temporaraly add folders to Python's PATH, import commands keep working exactly as the original ones.

TkGPIO currently supports the following devices:

  • Some gpiozero components (more coming soon)
    • LED / PWMLED
    • (Active) Buzzer
    • DC Motor
    • Servomotor
    • Push Button
    • Toggle Switch
    • Distance Sensor (HC-SR04)
    • Light Sensor (LDR)
    • Motion Sensor (HC-SR501)
    • Potentiometer (via MCP3xxx chip)
  • LCD display (reimplementing Adafruit_CharLCD API)
    • Only message and clear methods are supported for now (more coming soon)
  • Infrared emmitter (reimplementing py_irsend API)
  • Infrared receiver (reimplementing python-lirc API)

TkGPIO also supports labels with text, border and background color.

Usage

This is a simple example to create and control 2 LEDs and 1 Button.

from tkgpio import TkCircuit

# initialize the circuit inside the GUI

configuration = {
    "width": 300,
    "height": 200,
    "leds": [
        {"x": 50, "y": 40, "name": "LED 1", "pin": 21},
        {"x": 100, "y": 40, "name": "LED 2", "pin": 22}
    ],
    "buttons": [
        {"x": 50, "y": 130, "name": "Press to toggle LED 2", "pin": 11},
    ]
}

circuit = TkCircuit(configuration)
@circuit.run
def main ():
    
    # now just write the code you would use on a real Raspberry Pi
    
    from gpiozero import LED, Button
    from time import sleep
    
    led1 = LED(21)
    led1.blink()
    
    def button_pressed():
        print("button pressed!")
        led2.toggle()
    
    led2 = LED(22)
    button = Button(11)
    button.when_pressed = button_pressed
    
    while True:
        sleep(0.1)

You could also initialize the circuit in a function inside a separate Python file, to hide it from students.

Check more sample code files in docs/examples.

Instalation

Use PyPI to install TkGPIO in your computer.

pip install tkgpio

By default, buzzers will flash on the window but will not play any sound. To enable that, install numpy and sounddevice, or install tkgpio using the command

pip install 'tkgpio[sound]'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkgpio-0.1.1.tar.gz (70.9 kB view details)

Uploaded Source

Built Distribution

tkgpio-0.1.1-py3-none-any.whl (76.0 kB view details)

Uploaded Python 3

File details

Details for the file tkgpio-0.1.1.tar.gz.

File metadata

  • Download URL: tkgpio-0.1.1.tar.gz
  • Upload date:
  • Size: 70.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Darwin/22.4.0

File hashes

Hashes for tkgpio-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5540f3b694bb213eeae22ce626fc24aa47c7af9ff6a64ea36e089a70c30cc748
MD5 36d8c03128cd8169b929c5b974f06fc0
BLAKE2b-256 e1e8abebdf4f8a05c572772b58d98f64d82f518af98133130fac504ddb4698c9

See more details on using hashes here.

File details

Details for the file tkgpio-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tkgpio-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 76.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Darwin/22.4.0

File hashes

Hashes for tkgpio-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8fd772df58331616510a9177a25137506800ed6c8a9894ff5bb2a18e133733e
MD5 730e9a12bd131c183054149fe9782deb
BLAKE2b-256 f4fe11806df2419507c1ed656c1af1ce47c118a3f0b4c9ab4c6da1811676b0aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page