Simplifying the process of creating games and apps for the Sprig.
Project description
sprig-essentials
Useful functions to simplify the process of creating games and apps with Sprig when using CircuitPython.
This package can currently only be used when developing using a device with internet. The package is not available for download on the Raspberry Pi Pico itself yet.
Installation
Basic Installation
To install the correct CircuitPython firmware and libraries for the Raspberry Pi Pico, follow these steps:
- Download the .UF2 from the CircuitPython website here.
- Press and hold the white button on the RPi Pico, then plug it into your computer while pressing the button. It should appear as a USB drive.
- Drag the downloaded
.UF2
file into the USB drive. The RPi Pico should automatically reboot and CircuitPython should be installed. - In the USB drive, create a new folder called
lib
if it doesn't already exist. - Download the necessary libraries from this sub-folder and place them in the
lib
folder. - At this point, you can now start using the
sprig_essentials
package on the Raspberry Pi Pico.
To install sprig_essentials
and use it on your Windows machine when developing for the Raspberry Pi Pico, you need to do the following:
pip install sprig-essentials
Then, to use it in a program you can import the entire package:
import sprig_essentials as se
Or import specific modules (for example, the display module):
from sprig_essentials.io import display
Manual Installation
This package can be installed manually by downloading and compiling the source code. To do this, follow these steps:
-
Clone the repository and nvaigate to it:
git clone https://github.com/WhenLifeHandsYouLemons/sprig-essentials.git cd sprig-essentials
-
Compile the package:
python convert_to_mpy.py
or
python3 convert_to_mpy.py
-
Copy the files inside the
output
folder to your Raspberry Pi Pico'slib
folder. -
You can now start using the
sprig_essentials
package on the Raspberry Pi Pico.
This package is intended to run on the Raspberry Pi Pico H
.
This package assumes you've installed CircuitPython
and are using the ST7735
display.
Wiring Diagram
The wiring diagram that this package assumes is intended for anyone using a Sprig, however, you can also wire this manually and achieve the same effect.
Here's a clearer pin connection diagram for GPIO pin numbers:
Documentation
Core module
Initialisation
Importing any other module from sprig_essentials
will automatically initialise the core module.
convertRGBToHex
Converts an RGB value to a hex integer value.
Raises a ValueError
if the RGB value is not between 0 and 255.
Raises a TypeError
if the RGB value is not a list of integers.
Raises an IndexError
if the RGB value is not a list of length 3.
- Parameters:
rgb
: List of r, g, and b value.
- Returns: Hex integer value.
Example:
hex_value = core.convertRGBToHex([255, 255, 255])
display
module
Initialisation
import board
from sprig_essentials.io import display
display_device = display.Display()
- Parameters:
backlight_pin
: Pin number for the backlight (default:board.GP17
)clock_pin
: Pin number for the clock (default:board.GP18
)MOSI_pin
: Pin number for MOSI (default:board.GP19
)MISO_pin
: Pin number for MISO (default:board.GP16
)cs_pin
: Pin number for chip select (default:board.GP20
)dc_pin
: Pin number for data/command (default:board.GP22
)reset_pin
: Pin number for reset (default:board.GP26
)screen_width
: Width of the screen (default:160
)screen_height
: Height of the screen (default:128
)rotation
: Display rotation in degrees (default:270
)bgr
: Whether the display uses the BGR format or the RGB format (default:True
)auto_refresh
: Whether auto-refresh is enabled (default:True
)
- Returns:
display
object.
The Display
class manages the Sprig's display device and its components.
The initialisation functions in this class don't have to be called if you are using this with a Sprig. If you're using a Sprig, chances are the wiring and parts are going to be the exact same for all boards, so you can initialise the screen using:
screen = display.Display()
startBacklight
Turns on the backlight. You can also change the value of display_device.backlight.value
to turn it on or off manually.
- Parameters:
backlight_pin
: Pin number for the backlight
- Returns:
digitalio__digital_in_out
object.
Example:
backlight = display_device.startBacklight(board.GP17)
createSPI
Creates an SPI object.
- Parameters:
clock
: Clock pinmosi
: MOSI pinmiso
: MISO pin
- Returns:
busio__spi
object.
Example:
display_device_spi = display_device.createSPI(board.GP18, board.GP19, board.GP16)
createDisplayBus
Creates a display bus object.
- Parameters:
spi
: SPI objectcs_pin
: Chip select pindc_pin
: Data/command pinreset_pin
: Reset pin
- Returns:
displayio__display_bus
object.
Example:
display_bus = display_device.createDisplayBus(spi, board.GP20, board.GP22, board.GP26)
initDisplay
Initializes the display object. Requires self.display_bus
to be stored inside the class object already.
- Parameters:
width
: Width of the displayheight
: Height of the displayrotation
: Display rotationbgr
: Whether the display uses BGRauto_refresh
: Whether auto-refresh is enabled
- Returns:
st7735r
object.
Example:
display = display_device.initDisplay(200, 100, 0, False, True)
showDisplayGroup
Shows a display group on the display.
- Parameters:
group
: Display group
- Returns:
None
.
Example:
display_device.showDisplayGroup(group)
showSprite
Shows a sprite on the display.
- Parameters:
group
: Display groupsprite
: Sprite object
- Returns:
None
.
Example:
display_device.showSprite(group, sprite)
createDisplayGroup
Creates a display group.
- Parameters:
x
: X-coordinate (default:0
)y
: Y-coordinate (default:0
)scale
: Scale factor (default:1
)
- Returns:
displayio__group
object.
Example:
group = createDisplayGroup()
createBitmap
Creates a bitmap object.
- Parameters:
width
: Width of the bitmapheight
: Height of the bitmapvalue_count
: Number of values (default:1
)
- Returns:
displayio__bitmap
object.
Example:
bitmap = createBitmap(10, 20)
createColourPalette
Creates a colour palette object.
- Parameters:
colours
: Integer of a colour (use theconvertRGBToHex
function before inputting this value)
- Returns:
displayio__palette
object.
Example:
colours = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] # Example list of colours as a Tuple
palette = createColourPalette(colours)
createSprite
Creates a sprite.
- Parameters:
bitmap
: Bitmap for the spritepixel_shader
: Pixel shader for the spritex
: X-coordinate (default:0
)y
: Y-coordinate (default:0
)
- Returns:
displayio__sprite
object.
Example:
sprite = createSprite(bitmap, palette)
createTextSprite
Creates a text sprite.
- Parameters:
text
: Text content for the spritecolour
: Colour for the textx
: X-coordinate (default:0
)y
: Y-coordinate (default:0
)font
: Font for the text (default:terminalio.FONT
)
- Returns:
label__label
object.
Example:
text_sprite = createTextSprite("Hello world!", (255, 0, 255))
releaseDisplays
Releases the display and display bus objects.
- Parameters:
None
. - Returns:
None
.
Example:
releaseDisplays()
audio
module
Initialisation
import board
from sprig_essentials.io import audio
audio_device = audio.Audio()
- Parameters:
bit_clock_pin
: Pin number for the bit clock (default:board.GP10
)word_select_pin
: Pin number for word select (default:board.GP11
)data_pin
: Pin number for data (default:board.GP9
)
- Returns:
Audio
object.
The Audio
class manages audio output via the I2S interface. The initialization functions in this class don't have to be called if you are using this with a Sprig.
createI2S
Creates an I2S output object.
- Parameters:
bit_clock_pin
: Pin number for the bit clockword_select_pin
: Pin number for word selectdata_pin
: Pin number for data
- Returns:
audiobusio__i2s
object.
Example:
i2s_output = audio_device.createI2S(board.GP10, board.GP11, board.GP9)
playWaveFile
Plays a .wav file directly without additional lines.
- Parameters:
wave_filename
: Filename of the .wav fileloop
: Boolean for looped playback (default:False
)
- Returns:
None
.
Example:
audio_device.playWaveFile("example.wav", loop=True)
playMP3File
Plays an .mp3 file directly without additional lines.
- Parameters:
mp3_filename
: Filename of the .mp3 fileloop
: Boolean for looped playback (default:False
)
- Returns:
None
.
Example:
audio_device.playMP3File("example.mp3")
playAudio
Plays audio received from createAudioSample
or openWaveFile
.
- Parameters:
audio
: Audio sample object (could be fromcreateAudioSample
oropenWaveFile
oropenMP3File
)loop
: Boolean for looped playback (default:False
)
- Returns:
None
.
Example:
audio_device.playAudio(audio_sample, loop=True)
isPlaying
Checks if audio is currently playing.
- Parameters:
None
- Returns:
bool
.
Example:
playing = audio_device.isPlaying()
stopAudio
Stops all audio playback.
- Parameters:
None
- Returns:
None
.
Example:
audio_device.stopAudio()
pauseAudio
Pauses current audio playback.
- Parameters:
None
- Returns:
None
.
Example:
audio_device.pauseAudio()
resumeAudio
Resumes paused audio playback.
- Parameters:
None
- Returns:
None
.
Example:
audio_device.resumeAudio()
createAudioSample
Creates an audio sample from an input buffer for use with playAudio
.
- Parameters:
audio_buffer
: Input audio buffer (aslist
object withint
values)sample_rate
: Sample rate (default:8000
)
- Returns:
circuitpython_typing__AudioSample
object.
Example:
audio_sample = createAudioSample(audio_buffer, sample_rate=16000)
openWaveFile
Opens 8-bit unsigned or 16-bit signed .wav files as samples.
- Parameters:
wave_filename
: Filename of the .wav file
- Returns:
circuitpython_typing__AudioSample
object.
Example:
wav_sample = openWaveFile("example.wav")
openMP3File
Opens .mp3 files as samples.
- Parameters:
mp3_filename
: Filename of the .mp3 file
- Returns:
circuitpython_typing__AudioSample
object.
Example:
mp3_sample = openMP3File("example.mp3")
createSineWave
Generates one period of a sine wave. Used as an example to show how an audio buffer can be created.
- Parameters:
None
- Returns:
list[int]
.
Example:
audio_buffer = createSineWave()
buttons
module
Initialisation
import board
from sprig_essentials.io import button
# Initialize a button using a pin number
button_1 = button.Button(board.GP5)
# Or if using a Sprig
buttons = button.Button(quick_start=True)
- Parameters:
button_pin
: Pin number for the button (default:None
)quick_start
: IfTrue
, initializes quick start buttons; ifFalse
, uses the specifiedbutton_pin
(default:False
)
- Returns:
Button
object.
The Button
class manages button inputs on the Sprig. It allows the creation of buttons using specific pins or enables a quick start mode for predefined buttons.
createButton
Creates a digital input object for the specified pin.
- Parameters:
btn_pin
: Pin number for the button
- Returns:
digitalio__digital_in_out
object.
Example:
button = button.Button()
button_object = button.createButton(board.GP5)
quickStartButtons
Automates the creation of buttons assuming you're using a Sprig, providing quick start buttons.
- Parameters:
None
- Returns: Tuple of digital input objects:
(w, a, s, d, i, j, k, l)
.
Example:
button = button.Button()
quick_buttons = button.quickStartButtons()
getPressed
Gets the current state of the button. This automatically updates the current and previous state of the button.
- Parameters:
None
- Returns:
- Returns
True
if pressed - Returns
False
if released - If no button is specified, returns a list of states for all buttons.
- Returns
Example:
button = button.Button(board.GP5)
state = button.getPressed()
# Or if using the Sprig's 8 buttons
buttons = button.Button()
states = buttons.getPressed() # Get state of all buttons
# Getting the state of a specific button
w_button = buttons[0]
w_state = buttons.getPressed(w_button)
updateButton
Updates the current and previous state of the button.
- Parameters:
None
- Returns:
None
.
Example:
button = button.Button(board.GP5)
button.updateButton() # Update state of specific button
# Or if using Sprig's 8 buttons
buttons = button.Button()
buttons.updateButton() # Update state of all buttons
getButtonStateChange
Returns the state change of the button. This automatically updates the current and previous state of the button.
- Parameters:
None
- Returns::
"pressed"
if state changes fromFalse
toTrue
"released"
if state changes fromTrue
toFalse
"no change"
if state did not change.
Example:
button = button.Button(board.GP5)
state_change = button.getButtonStateChange()
# Or if using the Sprig's 8 buttons
buttons = button.Button()
state_changes = buttons.getButtonStateChange() # Get state change of all buttons
resetButtonStates
Resets the current and previous state of the button.
- Parameters:
None
- Returns:
None
.
Example:
button = button.Button(board.GP5)
button.resetButtonStates() # Reset state of specific button
# Or if using Sprig's 8 buttons
buttons = button.Button()
buttons.resetButtonStates() # Reset state of all buttons
led
module
Initialisation
import board
from sprig_essentials.io import led
# If you want to manually initialise the LED
led_device = led.LED(board.GP28)
# If you want to create the LED object and initialise it later
led_device = led.LED()
# If you are using this with a Sprig
led_device = led.LED(quick_start=True)
- Parameters:
led_pin
: Pin number for the LED (default:board.GP25
)
- Returns:
LED
object.
The LED
class manages the Sprig's LED and its components. The initialisation functions in this class don't have to be called if you are using this with a Sprig.
createLED
Creates a digital output object for the specified pin.
- Parameters:
led_pin
: Pin number for the LED
- Returns:
digitalio__digital_in_out
object.
Example:
led_object = led.LED()
led_object.createLED(board.GP28)
on
Turns the LED on.
- Parameters:
None
- Returns:
None
Example:
led_object = led.LED(board.GP28)
led_object.on()
off
Turns the LED off.
- Parameters:
None
- Returns:
None
Example:
led_object = led.LED(board.GP28)
led_object.off()
toggle
Toggles the LED.
- Parameters:
None
- Returns:
None
Example:
led_object = led.LED(board.GP28)
led_object.toggle()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sprig_essentials-0.5.0.tar.gz
.
File metadata
- Download URL: sprig_essentials-0.5.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a58daf94b675c04af8dd1a63e4ec7d01eec65666b73068fbbca2c64675923320 |
|
MD5 | 4444f67fe3338e6d4d175b976f4baf19 |
|
BLAKE2b-256 | 65d646394dbbce26ef650607ecef2940348bbe466cfa8a6201603c7330b38c5a |
File details
Details for the file sprig_essentials-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: sprig_essentials-0.5.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a5c9b5537301711c6d80a69e6295af8b12fb663c263f277f8956b2047a13862 |
|
MD5 | 16897e68a890dd628000ad212ef8426a |
|
BLAKE2b-256 | ed95ab93029e54da26e5870a7e52aaaa634e44b62521b4d00ac4969b8aa2ad47 |