Programming pyBox

Probably the easiest way to develop software for pyBox and iterate reasonably fast, is using the PyCharm IDE with the MicroPython plugin enabled.

Create a new project, enable MircoPython and create three files: boot.py, main.py, and app.py. Remember, during the boot process, once the filesystem is mounted, boot.py is executed. Don’t do too crazy things here that could lead to a boot loop. As a final step of boot procedure, main.py is executed from filesystem, if exists. Main.py should import and launch the custom application.

Example

boot.py

"""
This file is executed on every boot (including wake-boot from deep sleep)
"""
import utime

utime.sleep(2)
from micropython import opt_level, alloc_emergency_exception_buf
from esp import osdebug

opt_level(1)  # Assertion and __debug__ are not compiled
alloc_emergency_exception_buf(100)  # allocate recommended RAM for the emergency exception buffer
osdebug(None)  # turn off vendor O/S debugging messages

main.py

"""
This file is executed on after boot.by
"""
from utime import sleep, localtime
import profile
import pybox
import pybox_tz as tz
import pybox_lcd as lcd  # display
import pybox_fg as fg  # fuel gauge
import app

# Connect pyBox to WiFi
lcd.print("Connecting to Wi-Fi ...")
pybox.connect(profile.WifiNetworks)
lcd.print(f"Hostname  {pybox.hostname}", pybox.ip_addr)
sleep(2)

# Show the battery level
lcd.print(f"Battery: {fg.remaining()}%", chr(0) * round(fg.remaining() * 0.16))
sleep(1)

# Fetch the time in my TimeZone and set the system's time
pybox.set_system_time(tz.get_time_stamp(profile.LOCAL_TZ))
ts = localtime()
lcd.print(f"Date  {ts[1]}/{ts[2]}/{ts[0]}", f"Time   {ts[3]:2}:{ts[4]:02}:{ts[5]:02}")
sleep(2)

lcd.clear_screen()
app.main()  # run the app

app.py

"""
pyBox app
"""
import pybox
import pybox_lcd as lcd  # display
import pybox_ct as ct  # timer


def main():
    # Create a simple task to blink the blue LED
    task = (lambda: pybox.blue_led.off() if pybox.blue_led.value() else pybox.blue_led.on(), 500)
    ct.register(task)  # blink the blue LED
    lcd.print("Hello World!")

 

 

With pxBox connected, select ESP8266 as the device type and the plugin should automatically find the port.
After that, open a source file, right click into the file, and in the context menu, select “Run ‘Flash ..'” ) to copy the file to the ESP32’s filesystem. The device will automatically reboot.

Connecting to /dev/cu.wchusbserial142440
Uploading files: 0% (0/1)
/Users/wolf/PycharmProjects/uPython/pyBox/main.py -> main.py
Uploading files: 100% (1/1)
Soft reboot

 

Capacitive Touch-Buttons

"""
Test Touch buttons
"""
import pybox
import pybox_lcd as lcd
from machine import reset


def on_click():
    global counter
    counter += 1
    lcd.print("Click", str(counter))


def on_long_click():
    global counter
    counter = 0
    lcd.print("Long Click", str(counter))


lcd.print("Button Demo", "Press the upper button to reach 5 ...")
counter = 0
pybox.btn_up.set_actions(on_click, on_long_click)
pybox.btn_up.enable()
while counter < 4:  # wait for a couple of button presses
    continue
lcd.print("", "...")
reset()

Changing LED, LCD, and NeoPixel

"""
    Blue LED, LCD, NeoPixel
"""
import pybox
import pybox_lcd as lcd  # display
import pybox_ct as ct  # timer

# Create a simple task to blink the blue LED
task = (lambda: pybox.blue_led.off() if pybox.blue_led.value() else pybox.blue_led.on(), 500)
ct.register(task)  # blink the blue LED
lcd.print("LCD & NEOPixel Colors")
for rgb in pybox.rgb_color():  # iterate over rgb color
    lcd.set_backlight(*rgb)
    lcd.print(None, f"{rgb[0]:3},{rgb[1]:3},{rgb[2]:3}")
    pybox.neo.color(*rgb)

ct.unregister(task)  # stop blinking the blue LED
pybox.neo.color(0, 0, 0)
lcd.clear_screen()
lcd.print("Hello World!")
pybox.blue_led.off()

Using pyBox Palette

"""
    Blue LED, LCD, NeoPixel
"""
import pybox
import pybox_lcd as lcd  # display
import pybox_ct as ct  # timer

# Create a simple task to blink the blue LED
task = (lambda: pybox.blue_led.off() if pybox.blue_led.value() else pybox.blue_led.on(), 500)
ct.register(task)  # blink the blue LED
lcd.print("LCD & NEOPixel Colors")
for rgb in pybox.rgb_color():  # iterate over rgb color
    lcd.set_backlight(*rgb)
    lcd.print(None, f"{rgb[0]:3},{rgb[1]:3},{rgb[2]:3}")
    pybox.neo.color(*rgb)

ct.unregister(task)  # stop blinking the blue LED
pybox.neo.color(0, 0, 0)
lcd.clear_screen()
lcd.print("Hello World!")
pybox.blue_led.off()

Leave a Reply