Skip to main content

Examples

Leds and masks

import olem

print("Begin leds mask test")
print("Turn on each led for 1000 ms")
mask = 1
for i in range(6):
print(mask)
olem.led_on(mask, "#FFFFFF")
olem.delay_ms(1000)
olem.led_off(olem.LEDALL)
mask = mask * 2

print("2 leds at once")
olem.led_on(olem.LED1 + olem.LED3, "#FFFFFF")
olem.delay_ms(1000)
olem.led_on(olem.LED0 + olem.LED5, "#FFFFFF")
olem.delay_ms(1000)
olem.led_on(olem.LED2 + olem.LED4, "#FFFFFF")
olem.delay_ms(1000)

print("End leds mask test")

Leds effects

import olem

print("Testing visual effects")

time_step = 1000
for i in range(3):
period = time_step * (i + 1)

print("\ton")
olem.led_on(olem.LEDALL, "#0000FF")
olem.delay_ms(period * 3)

print("\toff")
olem.led_off(olem.LEDALL)
olem.delay_ms(period * 3)

print("\ttransition")
olem.led_transition(olem.LEDALL, period, "#FFFF00")
olem.delay_ms(period * 3)

print("\tpulse")
olem.led_pulse(olem.LEDALL, period, "#00FFFF")
olem.delay_ms(period * 3)

print("\tblink")
olem.led_blink(olem.LEDALL, period, "#FF00FF")
olem.delay_ms(period * 3)

print("End of led testing")

Movement speed control

import olem

for i in range(10):
print(i)
olem.set_move_speed(i * 1 / 10)
olem.move(100, 0, 0)
olem.wait_stop()
olem.move(-100, 0, 0)
olem.wait_stop()

Moving in a square pattern

import olem

def square_path_relative(side_length, speed):
"""
OLEM move in a square of specified width at requested speed
always facing the same direction. OLEM's current position is
used as the bottom left corner.
:param side_length: square width in mm
:param speed: between 0 and 1.0
"""
olem.set_move_speed(speed)
olem.move(side_length, 0, 0)
olem.wait_stop_moving()
olem.move(0, side_length, 0)
olem.wait_stop_moving()
olem.move(-side_length, 0, 0)
olem.wait_stop_moving()
olem.move(0, -side_length, 0)
olem.wait_stop_moving()

def square_path_absolute(side_length, speed):
"""
OLEM move in a square of specified width at requested speed
always facing the same direction.
:param side_length: square width in mm
:param speed: between 0 and 1.0
"""
olem.set_move_speed(speed)
olem.move_to(side_length, 0, 0)
olem.wait_stop_moving()
olem.move_to(side_length, side_length, 0)
olem.wait_stop_moving()
olem.move_to(0, side_length, 0)
olem.wait_stop_moving()
olem.move_to(0, 0, 0)
olem.wait_stop_moving()

msg = ""
square_width = 100
speed = 8 / 10

square_path_relative(square_width, speed)
square_path_absolute(square_width, speed)

Hand detection

import olem

print("Starting ir hand detection test")

start_timestamp = olem.get_time_ms()

while (olem.get_time_ms() - start_timestamp) < 20000:

olem.led_off(olem.LEDALL)

if (olem.ir_side_is_hand_present(olem.FRONT)):
olem.led_on(olem.LEDALL, "#FF0000")

if (olem.ir_side_is_hand_present(olem.RIGHT)):
olem.led_on(olem.LEDALL, "#00FF00")

if (olem.ir_side_is_hand_present(olem.BACK)):
olem.led_on(olem.LEDALL, "#0000FF")

if (olem.ir_side_is_hand_present(olem.LEFT)):
olem.led_on(olem.LEDALL, "#00FFFF")

if (olem.ir_top_is_hand_present()):
olem.led_on(olem.LEDALL, "#FFFFFF")

olem.delay_ms(100)

print("End ir hand detection test")

Display - Text

import olem 

message = "Hello world !"

# Display in the middle of the screen
text_obj = olem.display_text(message, olem.FONT_DEFAULT, olem.ALIGN_CENTER)
olem.display_center(text_obj)
olem.delay_ms(5000)

# Change contents
olem.display_set_text(text_obj, "Now I barely fit on screen")
olem.delay_ms(5000)

# Resize
olem.display_set_size(text_obj, 200, 100)
olem.delay_ms(1000)

# Recolor
olem.display_set_text(text_obj, "Now I do.\nI can also change color!")
olem.display_set_text_color(text_obj, "#00FF00")
olem.delay_ms(5000)

# Move
olem.display_set_text(text_obj, "Or move")
for i in range(6):
olem.display_set_pos(text_obj, i * 10, i * 10)
olem.display_wait_for_next_frame()

# Delete
olem.display_del(text_obj)

# Font size
text_obj = olem.display_text("New texts are still white", olem.FONT_SMALL, olem.ALIGN_CENTER)
olem.display_center(text_obj)
olem.delay_ms(5000)

# Change the default color, all new texts use this color
olem.display_set_default_text_color("#FF00FF")

# Delete all displayed objects
olem.display_del_all()

# New text uses the default color
olem.display_mutex_take()
text_obj = olem.display_text("But not anymore !", olem.FONT_BIG, olem.ALIGN_CENTER)
olem.display_set_size(text_obj, 320, 100)
olem.display_center(text_obj)
olem.display_mutex_give()

olem.delay_ms(5000)

Display - Buttons polling

import olem 

X_MARGIN = 10

# Events id
EVENT_RED = 1
EVENT_GREEN = 2
EVENT_BLUE = 3
EVENT_EXIT = 4

# Init display components
b = olem.display_button("Red", EVENT_RED)
olem.display_set_pos(b, X_MARGIN, 20)

b = olem.display_button("Green", EVENT_GREEN)
olem.display_set_pos(b, X_MARGIN, 80)

b = olem.display_button("Blue", EVENT_BLUE)
olem.display_set_pos(b, X_MARGIN, 140)

b = olem.display_img_button("/common/images/settings.bin")
olem.display_add_event_cb(b, EVENT_EXIT)
olem.display_set_pos(b, X_MARGIN, 180)
olem.display_set_size(b, 50, 50)

# Check events by polling
should_exit = False
while not should_exit:
event = olem.display_get_event()

if event == EVENT_RED:
olem.led_on(olem.LEDALL, "#FF0000")
elif event == EVENT_GREEN:
olem.led_on(olem.LEDALL, "#00FF00")
elif event == EVENT_BLUE:
olem.led_on(olem.LEDALL, "#0000FF")
elif event == EVENT_EXIT:
should_exit = True

olem.delay_ms(50)

Display - Buttons wait

import olem 

X_MARGIN = 10

# Events id
EVENT_RED = 1
EVENT_GREEN = 2
EVENT_BLUE = 3
EVENT_EXIT = 4

# Init display components
b = olem.display_button("Red", EVENT_RED)
olem.display_set_pos(b, X_MARGIN, 20)

b = olem.display_button("Green", EVENT_GREEN)
olem.display_set_pos(b, X_MARGIN, 80)

b = olem.display_button("Blue", EVENT_BLUE)
olem.display_set_pos(b, X_MARGIN, 140)

b = olem.display_img_button("/common/images/settings.bin")
olem.display_add_event_cb(b, EVENT_EXIT)
olem.display_set_pos(b, X_MARGIN, 180)
olem.display_set_size(b, 50, 50)

# Check events by waiting for event
should_exit = False
while not should_exit:
event = olem.display_wait_for_event()

if event == EVENT_RED:
olem.led_on(olem.LEDALL, "#FF0000")
elif event == EVENT_GREEN:
olem.led_on(olem.LEDALL, "#00FF00")
elif event == EVENT_BLUE:
olem.led_on(olem.LEDALL, "#0000FF")
elif event == EVENT_EXIT:
should_exit = True
else:
print("unknown event " + str(event))

Display - Buttons wait with timeout

import olem 

X_MARGIN = 10

# Events id
EVENT_RED = 1
EVENT_GREEN = 2
EVENT_BLUE = 3
EVENT_EXIT = 4

# Init display components
b = olem.display_button("Red", EVENT_RED)
olem.display_set_pos(b, X_MARGIN, 20)

b = olem.display_button("Green", EVENT_GREEN)
olem.display_set_pos(b, X_MARGIN, 80)

b = olem.display_button("Blue", EVENT_BLUE)
olem.display_set_pos(b, X_MARGIN, 140)

b = olem.display_img_button("/common/images/settings.bin")
olem.display_add_event_cb(b, EVENT_EXIT)
olem.display_set_pos(b, X_MARGIN, 180)
olem.display_set_size(b, 50, 50)

# Check events by waiting for event
should_exit = False
while not should_exit:
event = olem.display_wait_for_event_timeout(500)

if event == EVENT_RED:
olem.led_on(olem.LEDALL, "#FF0000")
elif event == EVENT_GREEN:
olem.led_on(olem.LEDALL, "#00FF00")
elif event == EVENT_BLUE:
olem.led_on(olem.LEDALL, "#0000FF")
elif event == EVENT_EXIT:
should_exit = True
elif event == 0:
print("no event before timeout")

Game Messenger - Basic led control

Controls the color of LEDs through shell messages in game

import olem

def parse_msg(msg):
argv = msg.split()
argc = len(argv)

if argv[0] == "leds":
if argc < 2:
print("Not enough args for leds command")
return

if argv[1][0] != '#':
print("Command leds expects a hex color (#RRGGBB) as 2nd arg")
return

olem.led_on(olem.LEDALL, argv[1])

else:
print("Unknown command")

print("start gmsg basic testing")
msg = "no msg received"

while "exit" not in msg:
msg = olem.gmsg_wait_for_message()
parse_msg(msg)

print("end gmsg basic testing")

NFC

import olem 

olem.nfc_enable()

while not olem.ir_top_is_hand_present():
while olem.nfc_get_number_of_cards_waiting() == 0:
olem.delay_ms(50)

data = olem.nfc_get_card_data()
print(data)

Extension module - GPIO periodic

import olem

print("Starting extension module gpio periodic test")

olem.extmod_mode_set(0)

for i in range(0, 10):
olem.extmod_gpio_set(olem.GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.GPIO2, olem.LOW)
olem.delay_ms(3000)
olem.extmod_gpio_set(olem.GPIO1, olem.HIGH)
olem.extmod_gpio_set(olem.GPIO2, olem.HIGH)
olem.delay_ms(3000)

olem.extmod_gpio_set(olem.GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.GPIO2, olem.LOW)

print("End of extension module gpio periodic test")

Extension module - GPIO & IR

import olem

FRONT = 0
RIGHT = 1
BACK = 2
LEFT = 3

GPIO1 = 1
GPIO2 = 2

HIGH = 1
LOW = 0

print("Starting extension module gpio ir test")

olem.extmod_mode_set(0)

timestamp_start = olem.get_time_ms()

while ((olem.get_time_ms() - timestamp_start) < 20000):

if (olem.ir_side_is_hand_present(FRONT)):
olem.extmod_gpio_set(GPIO1, HIGH)
else:
olem.extmod_gpio_set(GPIO1, LOW)

if (olem.ir_side_is_hand_present(BACK)):
olem.extmod_gpio_set(GPIO2, HIGH)
else:
olem.extmod_gpio_set(GPIO2, LOW)

olem.extmod_gpio_set(GPIO1, LOW)
olem.extmod_gpio_set(GPIO2, LOW)

print("End of extension module gpio ir test")

Extension Module - PWM

import olem

GPIO1 = 1
GPIO2 = 2
LOW = 0
period = 20

print("Start pwm")
olem.extmod_mode_set(2)

for i in range(0, period):
print("compare value " + str(i))
olem.extmod_pwm_set(GPIO1, period, i)
olem.extmod_pwm_set(GPIO2, period, period - i)
olem.delay_ms(2000)

olem.extmod_mode_set(0)
olem.extmod_gpio_set(GPIO1, LOW)
olem.extmod_gpio_set(GPIO2, LOW)

print("End pwm")

Extension Module - I2C

import olem

print("Starting extension module gpio i2c test")

addr = 0x43
olem.extmod_mode_set(olem.EXTMOD_I2C)
timestamp_start = olem.get_time_ms()

while ((olem.get_time_ms() - timestamp_start) < 20000):
olem.extmod_i2c_write(addr, [0x86, 0x87])
olem.delay_ms(1000)
olem.extmod_i2c_read(addr, 4)
olem.delay_ms(1000)

print("End of extension module gpio i2c test")

Use the force (To be fixed)

Untitled

import olem


THRESHOLD = 500
DEADZONE = 100
MIN_VALUE = 150
STEP = 20
MAX_SPEED = 10 / 10

def get_translation(value):
translation = (value - THRESHOLD) / THRESHOLD
if abs(value - THRESHOLD) < DEADZONE:
return 0
else:
return translation

def set_speed(x, y):
if abs(x) > abs(y):
speed = abs(x)
else:
speed = abs(y)
speed = speed + 1 / 2

if (speed > MAX_SPEED):
olem.set_move_speed(MAX_SPEED)
else:
olem.set_move_speed(speed)

def force_loop():
x = 0
y = 0
front = olem.ir_side_get_sensor_value(olem.FRONT)
right = olem.ir_side_get_sensor_value(olem.RIGHT)
back = olem.ir_side_get_sensor_value(olem.BACK)
left = olem.ir_side_get_sensor_value(olem.LEFT)

# Get translations
if front > MIN_VALUE:
x -= get_translation(front)

if back > MIN_VALUE:
x += get_translation(back)

if right > MIN_VALUE:
y -= get_translation(right)

if left > MIN_VALUE:
y += get_translation(left)

# Update speed based on translations
set_speed(x, y)

if x != 0 or y != 0:
if (x != 0):
x = x / abs(x) * STEP
if (y != 0):
y = y / abs(y) * STEP
olem.move(x, y, 0)

#########################################################
#########################################################
#########################################################

olem.led_off(olem.LEDALL)

# Wait for button press
while olem.get_power_button_state() == 1:
olem.delay_ms(20)

# Display logo and turn on led
img = olem.display_img("/common/images/olem_logo.bin")
olem.led_on(olem.LEDALL, "#FFFFFF")

# Change filter param to allow for faster detection
olem.ir_side_set_filter(2)

while not olem.ir_top_is_hand_present():
force_loop()