Skip to content

Commit

Permalink
Examples for use with the Multi Sensor Stick
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdr committed Sep 16, 2024
1 parent e06642b commit d77106e
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/multi-sensor-breakout/double_tap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import time
from machine import I2C
from lsm6ds3 import LSM6DS3, NORMAL_MODE_104HZ
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8)
WIDTH, HEIGHT = display.get_bounds()

# Some colours we'll need later on
BG = display.create_pen(70, 130, 180)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)

# Create the I2C instance and pass that to LSM6DS3
i2c = I2C(0, scl=21, sda=20)
sensor = LSM6DS3(i2c, mode=NORMAL_MODE_104HZ)

# Text size and Offset for the drop shadow. We'll use these later!
text_size = 9
offset = 3

while True:

tap = sensor.double_tap_detected()
start_tick = time.ticks_ms()

# Clear the screen
display.set_pen(BG)
display.clear()

if tap == 1:
while (time.ticks_ms() - start_tick < 1000):
display.set_pen(BLACK)
display.text("Who's there!?", 10 + offset, 10 + offset, WIDTH, text_size)
display.set_pen(WHITE)
display.text("Who's there!?", 10, 10, WIDTH, text_size)
display.update()

else:
display.set_pen(BLACK)
display.text("Knock Knock..", 10 + offset, 10 + offset, WIDTH, text_size)
display.set_pen(WHITE)
display.text("Knock Knock..", 10, 10, WIDTH, text_size)

# Finally, we tell the screen to update so we can see our work!
display.update()
68 changes: 68 additions & 0 deletions examples/multi-sensor-breakout/double_tap_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from machine import I2C
from lsm6ds3 import LSM6DS3, NORMAL_MODE_104HZ
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8
import asyncio

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8)

WIDTH, HEIGHT = display.get_bounds()

# Some colours we'll need later on
BG = display.create_pen(70, 130, 180)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)

# Create the I2C instance and pass that to LSM6DS3
i2c = I2C(0, scl=21, sda=20)
sensor = LSM6DS3(i2c, mode=NORMAL_MODE_104HZ)

# Text size and Offset for the drop shadow. We'll use these later!
text_size = 10
offset = 3

tap = 0


async def tapped():

global tap

while True:

tap = sensor.double_tap_detected()

if tap:
await asyncio.sleep_ms(1000)
else:
await asyncio.sleep_ms(10)


async def main():

global tap
asyncio.create_task(tapped())
await asyncio.sleep(0)

while True:

# Clear the screen
display.set_pen(BG)
display.clear()

if tap == 1:

display.set_pen(BLACK)
display.text("Who's there!?", 10 + offset, 10 + offset, WIDTH, text_size)
display.set_pen(WHITE)
display.text("Who's there!?", 10, 10, WIDTH, text_size)
else:
display.set_pen(BLACK)
display.text("Knock Knock..", 10 + offset, 10 + offset, WIDTH, text_size)
display.set_pen(WHITE)
display.text("Knock Knock..", 10, 10, WIDTH, text_size)

# Finally, we tell the screen to update so we can see our work!
display.update()
await asyncio.sleep_ms(250)

asyncio.run(main())
131 changes: 131 additions & 0 deletions examples/multi-sensor-breakout/shake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import time
from machine import I2C, Pin
from lsm6ds3 import LSM6DS3, NORMAL_MODE_104HZ
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8)
WIDTH, HEIGHT = display.get_bounds()

# Some colours we'll need later on
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(255, 0, 0)
GREEN = display.create_pen(0, 255, 0)
BLUE = display.create_pen(0, 0, 255)
BG = WHITE

# Create the I2C instance and pass that to LSM6DS3
i2c = I2C(0, scl=21, sda=20)
sensor = LSM6DS3(i2c, mode=NORMAL_MODE_104HZ)

# Setup the pins used by the Buttons
button_a = Pin(16, Pin.IN, Pin.PULL_UP)
button_b = Pin(15, Pin.IN, Pin.PULL_UP)
button_c = Pin(14, Pin.IN, Pin.PULL_UP)
button_x = Pin(17, Pin.IN, Pin.PULL_UP)
button_z = Pin(19, Pin.IN, Pin.PULL_UP)


class PALETTE(object):

def __init__(self):

self.selected_colour_1 = []
self.selected_colour_2 = []
self.mixed_colour = []
self.mixed_pen = WHITE

def add_colour(self, colour):

if not self.selected_colour_1:
self.selected_colour_1 = colour
elif not self.selected_colour_2:
self.selected_colour_2 = colour

def clear_colours(self):

self.selected_colour_1 = []
self.selected_colour_2 = []
self.mixed_colour = []
self.mixed_pen = WHITE

def mix_colours(self):

if self.selected_colour_1 and self.selected_colour_2:
self.mixed_colour = [min(a + b, 255) for a, b in zip(self.selected_colour_1, self.selected_colour_2)]
self.mixed_pen = display.create_pen(self.mixed_colour[0], self.mixed_colour[1], self.mixed_colour[2])

def process_input(self):

if button_a.value() == 0:
self.add_colour([255, 0, 0])
time.sleep(0.1)

if button_b.value() == 0:
self.add_colour([0, 255, 0])
time.sleep(0.1)

if button_c.value() == 0:
self.add_colour([0, 0, 255])
time.sleep(0.1)

if button_x.value() == 0:
self.clear_colours()
time.sleep(0.1)

if button_z.value() == 0:
self.mix_colours()
time.sleep(0.1)

if sensor.sig_motion_detected():
self.mix_colours()
time.sleep(0.1)

def draw(self):
# Clear the screen
display.set_pen(BG)
display.clear()

display.set_pen(RED)
display.rectangle(0, 25, 29, 29)
display.circle(30, 39, 14)

display.set_pen(GREEN)
display.rectangle(0, 102, 29, 29)
display.circle(30, 116, 14)

display.set_pen(BLUE)
display.rectangle(0, 178, 29, 29)
display.circle(30, 192, 14)

display.set_pen(BLUE)
display.rectangle(0, 178, 29, 29)
display.circle(30, 192, 14)

display.set_pen(BLACK)
display.rectangle(135 + 2, 75 + 2, 100, 100)
display.set_pen(self.mixed_pen)
display.rectangle(135, 75, 100, 100)

if self.selected_colour_1 and self.selected_colour_2:
display.set_pen(BLACK)
display.text("SHAKE TO MIX!", 90 + 1, 20 + 1, WIDTH, 3)
display.text("Press X to Reset", 105, 215, WIDTH, 2)
display.set_pen(BLUE)
display.text("SHAKE TO MIX!", 90, 20, WIDTH, 3)

else:
display.set_pen(BLACK)
display.text("<< Select 2 colours", 80 + 1, 20 + 1, WIDTH, 2)
display.set_pen(BLUE)
display.text("<< Select 2 colours", 80, 20, WIDTH, 2)

display.update()


p = PALETTE()

while True:

p.process_input()
p.draw()
53 changes: 53 additions & 0 deletions examples/multi-sensor-breakout/step_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import time
import pngdec
from machine import I2C
from lsm6ds3 import LSM6DS3, NORMAL_MODE_104HZ
from picographics import PicoGraphics, DISPLAY_EXPLORER, PEN_P8

display = PicoGraphics(display=DISPLAY_EXPLORER, pen_type=PEN_P8)
png = pngdec.PNG(display)

WIDTH, HEIGHT = display.get_bounds()

# Some colours we'll need later on
BG = display.create_pen(255, 99, 71)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)

# Create the I2C instance and pass that to LSM6DS3
i2c = I2C(0, scl=21, sda=20)
sensor = LSM6DS3(i2c, mode=NORMAL_MODE_104HZ)

# Text size and Offset for the drop shadow. We'll use these later!
text_size = 12
offset = 3

while True:
# Get the latest step count from the sensor
steps = sensor.get_step_count()

# Clear the screen
display.set_pen(BG)
display.clear()

# Open the png and decode it
try:
png.open_file("walking.png")
png.decode(170, 20, scale=1)
except OSError:
print("Error: PNG File missing. Copy the PNG file from the example folder to your Pico using Thonny and run the example again.")

# First we draw the drop shadow, we want this to appear behind our text and offset slightly.
display.set_pen(BLACK)
display.text(str(steps), 10 + offset, HEIGHT - 220 + offset, WIDTH, text_size)
display.text("Steps", 10 + offset, HEIGHT - 80 + offset, WIDTH, 5)

# Now we draw the main text on top.
display.set_pen(WHITE)
length = display.measure_text(str(steps), text_size)
display.text("Steps", 10, HEIGHT - 80, WIDTH, 5)
display.text(str(steps), 10, HEIGHT - 220, WIDTH, text_size)

# Finally, we tell the screen to update so we can see our work!
display.update()
time.sleep(0.01)
Binary file added examples/multi-sensor-breakout/walking.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d77106e

Please sign in to comment.