1. Hello, World!
This first lesson will guide you through creating a simple "Hello, World!" app. This will teach you the basic structure of a custom app and how to display text on the screen.
Creating the File
Create a new file named custom_code_HelloWorld.py and place it in the custom_code/ directory with the following content:
python
# custom_code_HelloWorld.py
from time import sleep_ms
def run(env):
# Get the hardware objects from the environment dictionary
oled = env.get('oled')
ok_button = env.get('ok_button')
menu_button = env.get('menu_button')
# Check if the necessary hardware is available
if not all([oled, ok_button, menu_button]):
print("Missing required hardware")
return
# Clear the display
oled.fill(0)
# Display the text
oled.text("Hello, World!", 0, 28)
oled.show()
# Wait for the OK button to be pressed
while ok_button.value() == 1:
# We can also check for the menu button to exit
if menu_button.value() == 0:
break
sleep_ms(50)
# The app will exit when the run function returns
# The menu system will automatically clear the screenUnderstanding the Code
Let's break down the code:
from time import sleep_ms: This line imports thesleep_msfunction, which allows us to pause the execution of the code for a specified number of milliseconds.def run(env):: This is the main function of our app. It takes a single argument,env, which is a dictionary containing all the hardware objects.oled = env.get('oled'): We get the OLED display object from theenvdictionary.oled.fill(0): This clears the display. The0means the color is black.oled.text("Hello, World!", 0, 28): This draws the text "Hello, World!" on the screen at the coordinates(0, 28).oled.show(): This updates the display to show the changes we've made.while ok_button.value() == 1:: This loop waits for theOKbutton to be pressed. Thevalue()method returns0when the button is pressed and1when it's not.
Running Your Code
- Upload Your Code: After creating your file, you'll need to upload it to the Sidekick. You can do this by running
pixi run uploadagain. - Navigate to the App Launcher: From the main menu, select
Code Loader. - Select Your App: Scroll to your app's name (
HelloWorldin our example) and press theOKbutton to launch it.
Next Steps
Now that you've created your first app, you can move on to the next lesson in the series.
Next Lesson: 2. Button Press (Coming Soon!)