Blinky

Auto Blinky

from st3m.application import Application
import leds

class Blinky(Application):
    def __init__(self, app_ctx):
        super().__init__(app_ctx)

        self.timer_ms = 0
        self.colors = (
            (1,1,0),
            (0,1,1),
            (1,0,1),
        )
        self.active_color = self.colors[0]
        self.blink_time_ms = 500

    def think(self, ins, delta_ms):
        self.timer_ms += delta_ms
        self.timer_ms %= self.blink_time_ms * len(self.colors)

        index = self.timer_ms // self.blink_time_ms
        self.active_color = colors[index]

        leds.set_all_rgb(*self.active_color)
        leds.update()

    def draw(self, ctx):
        ctx.rgb(*self.active_color).rectangle(-120, -120, 240, 240).fill()

    def get_help(self):
        return "This app blinks all LEDs and the display automatically."

Note how the active color is not reset when exiting and entering the application: The object does not get destroyed when the user exits (see Application.on_enter() in the st3m.application module).

Captouch Blinky

from st3m.application import Application
import leds

class Blinky(Application):
    def __init__(self, app_ctx):
        super().__init__(app_ctx)

        self.colors = (
            (1,1,0),
            (0,1,1),
            (1,0,1),
            (0,1,0),
            (0,0,1),
        )
        self.active_color = self.colors[0]

    def think(self, ins, delta_ms):

        for x in range(0, 10, 2):
            if self.input.captouch.petals[x].pressed:
                self.active_color = self.colors[x//2]

        leds.set_all_rgb(*self.active_color)
        leds.update()

    def draw(self, ctx):
        ctx.rgb(*self.active_color).rectangle(-120, -120, 240, 240).fill()

    def get_help(self):
        return "This app changes all LED and display color when touching a top petal."

The self.input object does edge detection here (documented in the st3m.input module).