示例#1
0
# Read the various information coming from the trackball
#
#  See GitHub: https://github.com/mchobby/esp8266-upy/tree/master/trackball

from machine import I2C
from trackball import Trackball
import time

i2c = I2C(2)  # Y9=scl, Y10=sda or Pyboard-Uno-R3 (I2C over pin 13)

# initialize the trackball
trackball = Trackball(i2c)
trackball.set_rgbw(255, 0, 0, 0)

while True:
    up, down, left, right, switch, state = trackball.read()
    print("r: {:02d} u: {:02d} d: {:02d} l: {:02d} switch: {:03d} state: {}".
          format(right, up, down, left, switch, state))
    time.sleep(0.200)
示例#2
0
        # Calculate hue and brightness
        h = x / 100.0
        v = y / 100.0

        # Prevents button from retriggering
        debounce = 0.5

        # Change toggled state if switch is pressed
        if state and not toggled:
            toggled = True
            time.sleep(debounce)
        elif state and toggled:
            toggled = False
            time.sleep(debounce)

        # Set brightness to zero if switch toggled
        if toggled:
            v = 0

        # Calculate RGB vals
        w = 0
        r, g, b = [int(c * 255) for c in hsv_to_rgb(h, 1.0, v)]

        # Set LEDs
        trackball.set_rgbw(r, g, b, w)

        time.sleep(0.01)
finally:
    # Ligth off the trackball
    trackball.set_rgbw(0, 0, 0, 0)