Skip to main content

A Python API For The Circuit Playground Express

Project description

Control A Circuit Playground Express From Your PC With An Easy To Use Python 3 API

View the Installation and Usage Guide Here.

It supports the following CPX devices:

  • The Buttons and Slide Switch.
  • The D13 Board LED.
  • The 10 onboard neo-pixels.
  • Tone generation using the onboard speaker.
  • The accelerometer, including tap sensing.
  • The temperature sensor.
  • The light sensor.
  • The sound sensor.
  • Touchpad sensors.

An example to animate the neopixels and to start and stop the animation by simply tapping the Playground Express:

import random
import time

from pymata_cpx.pymata_cpx import PyMataCpx


class TheTapper():
    """
    Illuminate the neopixels in a counter-clockwise fashion with randomly generated colors.
    When you tap the playground express, the neopixels will stop changing and the
    program pauses. Tap again and the neopixels will start again.
    """
    def __init__(self):
        # create an instance of the API
        self.p = PyMataCpx()

        print('Tap the playground express to stop the neopixels from moving.')
        print('Tap again, to start them up')
        print('The tap state will be printed to the console')

        # Start monitoring for tap events and
        # send event notifications to the "tapped" callback method.
        self.p.cpx_tap_start(self.tapped)

        # flag to start and stop the light show
        self.go = True

        while True:
            try:
                # run the light show
                for neopixel in range(0, 10):
                    # check the go flag
                    if self.go:
                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        r = random.randint(0, 254)
                        g = random.randint(0, 254)
                        b = random.randint(0, 254)
                        self.p.cpx_pixel_set(neopixel, r, g, b)
                        self.p.cpx_pixels_show()
                        time.sleep(.2)
                    else:
                        self.p.cpx_pixels_clear()
                        self.p.cpx_pixels_show()
                        time.sleep(.001)
            except KeyboardInterrupt:
                # If you press control-C, cleanly exit
                self.p.cpx_pixels_clear()
                self.p.cpx_pixels_show()
                self.p.cpx_close_and_exit()

    def tapped(self, data):
        """
        :param data: data[0] = data type (analog = 2, digital =32)
                     data[1] = pin for device 27
                     data[2] = tap data - list of booleans.
                               First value for 1 tap
                               Second value for 2 taps
        """
        # for any taps, toggle the go flag
        # print out the current go state
        if data[2] != [False, False]:
            self.go = not self.go
            print(self.go)

# start the program
TheTapper()

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

pymata-cpx-1.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

pymata_cpx-1.0-py2.py3-none-any.whl (22.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pymata-cpx-1.0.tar.gz.

File metadata

  • Download URL: pymata-cpx-1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.5

File hashes

Hashes for pymata-cpx-1.0.tar.gz
Algorithm Hash digest
SHA256 ef7b96eeebfe3717e7f6858c9438268181c308aeba3712306437a66e3217751f
MD5 9ed2c448979368fef66ca9aed4a225c7
BLAKE2b-256 4fbe094b8c990980f48d7d3f2b923b6b0ee43243d01f4204514d6c404ebdf0c6

See more details on using hashes here.

File details

Details for the file pymata_cpx-1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pymata_cpx-1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.5

File hashes

Hashes for pymata_cpx-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 01577d8f0ba675c2cbd17a8ca55a8d385a6a8eafa8a141cc4d96d99f1853e41e
MD5 45dbf78b48b29567550352306f13ba19
BLAKE2b-256 07be127fa8050df883de19bcc2193cedcc698ae82213c6234422dd0154eec20f

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