Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
__pycache__
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# arduino-modulino-lessons
# arduino-modulino-lessons
63 changes: 63 additions & 0 deletions micropython/01-pixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Animation
# Difficulty: medium
# Material:. Modulino Pixels

# Exercise:
# Using the Modulino Pixels, create a continuous animation (that never ends) that lights up one LED at a time starting from LED 0 to LED 7
# and then goes back lighting up one LED from LED 6 to LED 0 (and then repeats the sequence without ever stopping).
# At any given moment, one and only one LED is on.

# The sequence should be as follows, executed forever:
# - LED 0 on, LED 0 off, LED 1 on, LED 1 off,..., LED 7 on, LED 7 off
# - LED 6 on, LED 6 off, LED 5 on, LED 5 off, … , LED 1 on, LED 1 off


from modulino import VirtualModulinoPixels, ModulinoColor

from time import sleep

pixels = VirtualModulinoPixels()

pixels.set_rgb(0, 0, 0, 0, 20)
pixels.show()

# print(ModulinoColor.GREEN)

# for index in range(0, 8):
# color_wheel_colors = [
# (255, 0, 0),
# (255, 85, 0),
# (255, 255, 0),
# (0, 255, 0),
# (0, 255, 255),
# (0, 0, 255),
# (255, 0, 255),
# (255, 0, 0)
# ]
# pixels.set_rgb(index, *color_wheel_colors[index], 40)
# pixels.show()


# pixels.set_all_rgb(255, 255, 255)
# pixels.show()


# Night Rider animation

for j in range(0, 3):
for i in range(0, 8):
pixels.clear_all()
pixels.set_rgb(i, 255, 0, 0, 100)
pixels.show()
sleep(0.05)

for i in range(7, -1, -1):
pixels.clear_all()
pixels.set_rgb(i, 255, 0, 0, 100)
pixels.show()
sleep(0.05)


# Turn off all LEDs
pixels.clear_all()
pixels.show()
13 changes: 13 additions & 0 deletions micropython/02-buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from modulino import ModulinoButtons


buttons = ModulinoButtons()


buttons.on_button_a_press = lambda : print("Button A pressed")
buttons.on_button_b_press = lambda : print("Button B pressed")
buttons.on_button_c_press = lambda : print("Button C pressed")

while True:
buttons_state_changed = buttons.update()
31 changes: 31 additions & 0 deletions micropython/03-pixels-buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

from modulino import ModulinoButtons, VirtualModulinoPixels

buttons = ModulinoButtons()
pixels = VirtualModulinoPixels()


buttons.on_button_a_press = lambda : print("Button A pressed")
buttons.on_button_b_press = lambda : print("Button B pressed")
buttons.on_button_c_press = lambda : print("Button C pressed")

buttons.on_button_a_release = lambda : print("Button A released")
buttons.on_button_b_release = lambda : print("Button B released")
buttons.on_button_c_release = lambda : print("Button C released")

while True:
buttons_state_changed = buttons.update()
if buttons_state_changed:
if buttons.is_pressed(0):
pixels.set_rgb(0, 255, 255, 255)
if buttons.is_pressed(1):
pixels.set_rgb(1, 255, 255, 255)
if buttons.is_pressed(2):
pixels.set_rgb(2, 255, 255, 255)
if not buttons.is_pressed(0):
pixels.set_rgb(0, 0, 0, 0)
if not buttons.is_pressed(1):
pixels.set_rgb(1, 0, 0, 0)
if not buttons.is_pressed(2):
pixels.set_rgb(2, 0, 0, 0)
pixels.show()
4 changes: 4 additions & 0 deletions micropython/modulino/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from .pixels import VirtualModulinoPixels
from .pixels import ModulinoColor
from .buttons import ModulinoButtons
172 changes: 172 additions & 0 deletions micropython/modulino/buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@

class ModulinoButtons():

def __init__(self):
self._current_buttons_status = [None, None, None]

# Button callbacks
self._on_button_a_press = None
self._on_button_b_press = None
self._on_button_c_press = None

# Button callbacks
self._on_button_a_release = None
self._on_button_b_release = None
self._on_button_c_release = None

@property
def on_button_a_press(self):
"""
Returns the callback for the press event of button A.
"""
return self._on_button_a_press

@on_button_a_press.setter
def on_button_a_press(self, value) -> None:
"""
Sets the callback for the press event of button A.
"""
self._on_button_a_press = value

@property
def on_button_a_release(self):
"""
Returns the callback for the release event of button A.
"""
return self._on_button_a_release

@on_button_a_release.setter
def on_button_a_release(self, value) -> None:
"""
Sets the callback for the release event of button A.
"""
self._on_button_a_release = value

@property
def on_button_b_press(self):
"""
Returns the callback for the press event of button B.
"""
return self._on_button_b_press

@on_button_b_press.setter
def on_button_b_press(self, value) -> None:
"""
Sets the callback for the press event of button B.
"""
self._on_button_b_press = value

@property
def on_button_b_release(self):
"""
Returns the callback for the release event of button B.
"""
return self._on_button_b_release

@on_button_b_release.setter
def on_button_b_release(self, value) -> None:
"""
Sets the callback for the release event of button B.
"""
self._on_button_b_release = value

@property
def on_button_c_press(self):
"""
Returns the callback for the press event of button C.
"""
return self._on_button_c_press

@on_button_c_press.setter
def on_button_c_press(self, value) -> None:
"""
Sets the callback for the press event of button C.
"""
self._on_button_c_press = value

@property
def on_button_c_release(self):
"""
Returns the callback for the release event of button C.
"""
return self._on_button_c_release

@on_button_c_release.setter
def on_button_c_release(self, value) -> None:
"""
Sets the callback for the release event of button C.
"""
self._on_button_c_release = value

def printButtons(self):
print("#########")
print("# #")
print("# ", end="")
print(self, end="")
print(" #")
print("# #")
print("#########")

def update(self) -> bool:
# TODO: remove input becuase this code runs on micropython boards.
key = input()
if str.capitalize(key) == "A":
self._current_buttons_status[0] = True
self._on_button_a_press()
elif str.capitalize(key) == "B":
self._current_buttons_status[1] = True
self._on_button_b_press()
elif str.capitalize(key) == "C":
self._current_buttons_status[2] = True
self._on_button_c_press()
elif str.capitalize(key) == "X":
self._current_buttons_status[0] = False
self._on_button_a_release()
elif str.capitalize(key) == "Y":
self._current_buttons_status[1] = False
self._on_button_b_release()
elif str.capitalize(key) == "Z":
self._current_buttons_status[2] = False
self._on_button_c_release()
else:
raise ValueError("Invalid pressed button. Allowed keys are: 'A', 'B', 'C', 'X', 'Y', 'Z")

return True

def __str__(self):
a = "a" if self._current_buttons_status[0] else "A"
b = "b" if self._current_buttons_status[1] else "B"
c = "c" if self._current_buttons_status[2] else "C"
return f"{a} {b} {c}"

return

def is_pressed(self, index: int) -> bool:
"""
Returns True if the button at the given index is currently pressed.

Parameters:
index (int): The index of the button. A = 0, B = 1, C = 2.
"""
return self._current_buttons_status[index]

@property
def button_a_pressed(self) -> bool:
"""
Returns True if button A is currently pressed.
"""
return self.is_pressed(0)

@property
def button_b_pressed(self) -> bool:
"""
Returns True if button B is currently pressed.
"""
return self.is_pressed(1)

@property
def button_c_pressed(self) -> bool:
"""
Returns True if button C is currently pressed.
"""
return self.is_pressed(2)
Loading