Using the pycubed.py Library
Libraries in CircuitPython
Generally, a "Library" for embedded devices usually refers to code someone else has written to control specific hardware or execute particular functions. Importing libraries allows us to quickly interface and use different electrical devices or perform repeated tasks.
Aside for a few built-in exceptions, libraries in CircuitPython are also plain-text python files like we experienced in HelloWorld.py. See the next section for details on finding and using new libraries with PyCubed.
/PYCUBED/
, as well as the lib folder: /PYCUBED/lib
of your board.Installing new libraries is easy
See the ❔FAQ for detailed examples on installing new libraries.
How to add a CircuitPython library
This is where CircuitPython really shines!
- Find a CircuitPython library online
(there's a section in the
❔FAQ about searching for libraries)
- Copy the library file (or folder) to your board's
/PYCUBED/lib/
directory
- Restart your board (
Ctrl+D
from the REPL)
- Done! 🎉 You can now import the library from your code or the REPL
- Find a CircuitPython library online
The pycubed.py Helper Library
PyCubed ships with a "helper" library called pycubed.py
and is located in the lib folder. It's a plain-text file written in Python just like our previous examples, which makes it easy to read through and learn from.
This helper library initializes the hardware on-board PyCubed as well as provides functions for common tasks that you may find helpful when using the board. You are encouraged to add your own functions to pycubed.py by using the existing code as a template.
How to Use pycubed.py
If you followed along for the final example in HelloWorld.py, you've already been exposed to the helper library.
In Example 2, we manually initialized the RGB LED like this:
import board
import neopixel
RGB = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5, auto_write=True)
Looking at Example 3, however, we notice that controlling the RGB LED is performed like this:
from pycubed import cubesat
cubesat.RGB = (0,255,0)
This is made possible by the pycubed.py helper library we import on line 1 above.
from pycubed import cubesat
. From then on, you interface with the library through the cubesat
object.
This is just scratching the surface of what pycubed.py can do. For example, to read the accelerometer data from the IMU, it's as simple as:
from pycubed import cubesat
x_accel, y_accel, z_accel = cubesat.acceleration # m/s^2
Detailed pycubed.py Documentation 📚
Examples for every function contained in the pycubed.py library can be found in the library-specific quick start page: ⭐pycubed.py