Skip to content

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 screen

Understanding the Code

Let's break down the code:

  • from time import sleep_ms: This line imports the sleep_ms function, 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 the env dictionary.
  • oled.fill(0): This clears the display. The 0 means 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 the OK button to be pressed. The value() method returns 0 when the button is pressed and 1 when it's not.

Running Your Code

  1. Upload Your Code: After creating your file, you'll need to upload it to the Sidekick. You can do this by running pixi run upload again.
  2. Navigate to the App Launcher: From the main menu, select Code Loader.
  3. Select Your App: Scroll to your app's name (HelloWorld in our example) and press the OK button 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!)