Skip to content

Commit 96330dd

Browse files
updating python package
1 parent a5b377d commit 96330dd

4 files changed

Lines changed: 180 additions & 13 deletions

File tree

examples/rgb_led_strip.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
4+
from dataclasses import dataclass
5+
import time # imports the time library
6+
7+
8+
@dataclass
9+
class RGB:
10+
red: float
11+
green: float
12+
blue: float
13+
14+
# Function Code
15+
16+
def set_item_in_list(lst, pos, value, fill=None):
17+
"""
18+
Sets a 1-based position in a list.
19+
Grows the list if needed using the fill value.
20+
"""
21+
idx = max(0, int(pos) - 1)
22+
23+
while idx >= len(lst):
24+
lst.append(fill)
25+
26+
lst[idx] = value
27+
28+
29+
def get_item_from_list(lst, pos, default=None):
30+
"""
31+
Gets a 1-based position from a list.
32+
Returns default if the position is out of range or the list is empty.
33+
"""
34+
idx = max(0, int(pos) - 1)
35+
if 0 <= idx < len(lst):
36+
return lst[idx]
37+
return default
38+
39+
40+
41+
# Variable Declaration
42+
j = 0
43+
44+
i = 0
45+
46+
k = 0
47+
48+
tempcolor = RGB(0, 0, 0)
49+
50+
colors = []
51+
52+
53+
# Initialise the program settings and configurations
54+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
55+
eb.config_rgb_strip("3", 30, "GRB", 10) # Configures the NEOPIXEL strip
56+
57+
58+
def setup():
59+
tempcolor = RGB(255, 0, 255)
60+
for i in range(1, 7, 1):
61+
set_item_in_list(colors, i, RGB(255, 0, 72), RGB(0,0,0))
62+
for i in range(7, 13, 1):
63+
set_item_in_list(colors, i, RGB(255, 255, 0), RGB(0,0,0))
64+
for i in range(13, 19, 1):
65+
set_item_in_list(colors, i, RGB(0, 255, 30), RGB(0,0,0))
66+
for i in range(19, 25, 1):
67+
set_item_in_list(colors, i, RGB(0, 72, 255), RGB(0,0,0))
68+
for i in range(25, 31, 1):
69+
set_item_in_list(colors, i, RGB(255, 0, 191), RGB(0,0,0))
70+
71+
72+
# Call Setup Function to do what the arduino does. Only gets called once.
73+
setup()
74+
75+
76+
while True:
77+
for j in range(1, 31, 1):
78+
developer_temp_color = get_item_from_list(colors, j, RGB(0,0,0)) # create a variable to store the color
79+
eb.rgb_strip_set_color_in_memory(j, developer_temp_color.red, developer_temp_color.green, developer_temp_color.blue)
80+
eb.rgb_strip_show_all_in_memory() # Sets the color the led strip.
81+
time.sleep(0.2) # Wait for the given/defined seconds.
82+
tempcolor = get_item_from_list(colors, 30, RGB(0,0,0))
83+
for k in range(30, 1, -1):
84+
set_item_in_list(colors, k, get_item_from_list(colors, (k - 1), RGB(0,0,0)), RGB(0,0,0))
85+
set_item_in_list(colors, 0, tempcolor, RGB(0,0,0))

examples/slow_rgb_led_strip.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
4+
from dataclasses import dataclass
5+
import time # imports the time library
6+
7+
8+
@dataclass
9+
class RGB:
10+
red: float
11+
green: float
12+
blue: float
13+
14+
# Function Code
15+
16+
def set_item_in_list(lst, pos, value, fill=None):
17+
"""
18+
Sets a 1-based position in a list.
19+
Grows the list if needed using the fill value.
20+
"""
21+
idx = max(0, int(pos) - 1)
22+
23+
while idx >= len(lst):
24+
lst.append(fill)
25+
26+
lst[idx] = value
27+
28+
29+
def get_item_from_list(lst, pos, default=None):
30+
"""
31+
Gets a 1-based position from a list.
32+
Returns default if the position is out of range or the list is empty.
33+
"""
34+
idx = max(0, int(pos) - 1)
35+
if 0 <= idx < len(lst):
36+
return lst[idx]
37+
return default
38+
39+
40+
41+
# Variable Declaration
42+
j = 0
43+
44+
i = 0
45+
46+
k = 0
47+
48+
tempcolor = RGB(0, 0, 0)
49+
50+
colors = []
51+
52+
53+
# Initialise the program settings and configurations
54+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
55+
eb.config_rgb_strip("3", 30, "GRB", 10) # Configures the NEOPIXEL strip
56+
57+
58+
def setup():
59+
tempcolor = RGB(255, 0, 255)
60+
for i in range(1, 7, 1):
61+
set_item_in_list(colors, i, RGB(255, 0, 72), RGB(0,0,0))
62+
for i in range(7, 13, 1):
63+
set_item_in_list(colors, i, RGB(255, 255, 0), RGB(0,0,0))
64+
for i in range(13, 19, 1):
65+
set_item_in_list(colors, i, RGB(0, 255, 30), RGB(0,0,0))
66+
for i in range(19, 25, 1):
67+
set_item_in_list(colors, i, RGB(0, 72, 255), RGB(0,0,0))
68+
for i in range(25, 31, 1):
69+
set_item_in_list(colors, i, RGB(255, 0, 191), RGB(0,0,0))
70+
71+
72+
# Call Setup Function to do what the arduino does. Only gets called once.
73+
setup()
74+
75+
76+
while True:
77+
for j in range(1, 31, 1):
78+
developer_temp_color = get_item_from_list(colors, j, RGB(0,0,0)) # create a variable to store the color
79+
eb.rgb_strip_set_color(j, developer_temp_color.red, developer_temp_color.green, developer_temp_color.blue)
80+
eb.rgb_strip_show_all() # Sets the color the led strip.
81+
#time.sleep(0.01) # Wait for the given/defined seconds.
82+
tempcolor = get_item_from_list(colors, 30, RGB(0,0,0))
83+
for k in range(30, 1, -1):
84+
set_item_in_list(colors, k, get_item_from_list(colors, (k - 1), RGB(0,0,0)), RGB(0,0,0))
85+
set_item_in_list(colors, 0, tempcolor, RGB(0,0,0))

motion_sensor.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
#Import ElectroBlocks library
21
from electroblocks import ElectroBlocks
3-
import time # imports the time library
4-
5-
6-
# Variable Declaration
7-
82

93
# Initialise the program settings and configurations
104
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
11-
eb.config_motion_sensor(10, 11) # Echo and Trig
5+
eb.config_motion_sensor(10, 11) # Setup Motion Sensor. EchoPin = 10 TrigPin = 11
6+
eb.digital_write_config(13)
7+
8+
129

13-
for _ in range(1, 4):
14-
cm = eb.motion_distance_cm()
15-
print(f"Distance: {cm} centimeters")
16-
time.sleep(1)
17-
print("CONTINUE")
10+
while True:
11+
if (eb.motion_distance_cm() < (2 * 3)):
12+
eb.digital_write(13, 1) # Turns the led on
13+
else:
14+
eb.digital_write(13, 0) # Turns the led off

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "electroblocks"
7-
version = "0.1.24"
7+
version = "0.1.25"
88
description = "Python client library to interact with ElectroBlocks Arduino firmware"
99
authors = [
1010
{ name = "Noah Glaser", email = "glaserpower@gmail.com" }

0 commit comments

Comments
 (0)