Skip to content

Commit f56185a

Browse files
Adjust library to support new firmware wip
1 parent e07cd75 commit f56185a

File tree

11 files changed

+177
-160
lines changed

11 files changed

+177
-160
lines changed

electroblocks/core.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class ComponentPins(Enum):
2424
RFID = 18,
2525
TEMP = 19,
2626
THERMISTOR = 20
27-
ANALOG_WRITE = 21
28-
2927

3028
class ElectroBlocks:
3129

@@ -275,7 +273,7 @@ def set_color_rgbled(self, r, g, b):
275273
def config_lcd(self, rows=2, cols=16, addr=39):
276274
self._add_pin(ComponentPins.LCD, "A5")
277275
self._add_pin(ComponentPins.LCD, "A4")
278-
self._send(f"register::lcd::{rows}::{cols}::{addr}")
276+
self._send(f"register::lcd::A5::{rows}::{cols}::{addr}")
279277

280278
def lcd_print(self, row, col, message):
281279
self._send(f"write::lcd::A5::9::{row}::{col}::{message}")
@@ -301,6 +299,13 @@ def lcd_scrollright(self):
301299
def lcd_scrollleft(self):
302300
self._send("write::lcd::A5::7")
303301

302+
def lcd_print_all_2(self, row1, row2):
303+
self._send(f"write::lcd::A5::8::{row1}::{row2}")
304+
305+
def lcd_print_all_4(self, row1, row2, row3, row4):
306+
self._send(f"write::lcd::A5::8::{row1}::{row2}::{row3}::{row4}")
307+
308+
304309
# Pins
305310

306311
def analog_write_config(self, pin):
@@ -335,16 +340,22 @@ def analog_read(self, pin):
335340

336341
# LED MATRIX
337342

338-
def config_led_matrix(self, data_pin, cs_pin, clk_pin):
343+
def config_led_matrix(self, data_pin, cs_pin, clk_pin, is_breadboard):
339344
self._add_pin(ComponentPins.LED_MATRIX, data_pin)
340345
self._add_pin(ComponentPins.LED_MATRIX, cs_pin)
341346
self._add_pin(ComponentPins.LED_MATRIX, clk_pin)
342-
self._send(f"register::ma::{data_pin}::{cs_pin}::{clk_pin}")
347+
is_breadboard_arduino = "1" if is_breadboard else "0"
348+
self._send(f"register::ma::{data_pin}::{cs_pin}::{clk_pin}::{is_breadboard_arduino}")
343349

344350
def set_led_matrix_led(self, row, col, isOn):
345351
pin = self.pins[ComponentPins.LED_MATRIX][0]
346352
isLedOnNumber = "1" if isOn else "0"
347-
self._send(f"write::ma::{pin}::1::{col - 1}::{8 - row}::{isLedOnNumber}")
353+
self._send(f"write::ma::{pin}::1::{col}::{row}::{isLedOnNumber}")
354+
355+
def draw_led_matrix(self, rows):
356+
pin = self.pins[ComponentPins.LED_MATRIX][0]
357+
self._send(f"write::ma::{pin}::2::" + "::".join(rows))
358+
348359

349360
# TM Digital Display
350361

@@ -400,14 +411,18 @@ def stop_motor(self, which_motor):
400411

401412
def config_rgb_strip(self, pin, count, colorOrderString, brightness):
402413
orderToNumber = {
403-
"RGB": 128,
404-
"GRB": 129,
405-
"RBG": 130,
406-
"GBR": 131,
407-
"BRG": 132,
408-
"BGR": 133,
414+
"RGB": 80,
415+
"GRB": 81,
416+
"RBG": 82,
417+
"GBR": 83,
418+
"BRG": 84,
419+
"BGR": 85,
420+
"BGR": 85,
421+
"RGBW": 86,
422+
"GRBW": 87,
423+
"WRGB": 88,
409424
}
410-
colorOrder = orderToNumber.get(colorOrderString) or 128
425+
colorOrder = orderToNumber.get(colorOrderString) or 81
411426
self._add_pin(ComponentPins.RGB_LED_STRIP, pin)
412427
self._send(f"register::leds::{pin}::{count}::{colorOrder}::{brightness}")
413428

@@ -416,11 +431,37 @@ def rgb_strip_set_color(self, position, red, green, blue):
416431
"""Set color for RGB LED strip at specified position"""
417432
pin = self.pins[ComponentPins.RGB_LED_STRIP][0]
418433
color = self.rgb_to_hex(red, green, blue)
419-
self._send(f"write::leds::{pin}::2::{position}::{color}")
434+
self._send(f"write::leds::{pin}::3::{position}::{color}")
435+
436+
def rgb_strip_set_all_colors(self, colors):
437+
"""
438+
Sets all the colors in an RGB LED strip.
439+
440+
colors: list of (r, g, b) tuples, each 0–255
441+
"""
442+
if not colors:
443+
return
444+
445+
pin = self.pins[ComponentPins.RGB_LED_STRIP][0]
446+
447+
# Build RRGGBB stream
448+
hex_stream = []
449+
for (r, g, b) in colors:
450+
# Clamp defensively (kids / callers make mistakes)
451+
r = max(0, min(255, int(r)))
452+
g = max(0, min(255, int(g)))
453+
b = max(0, min(255, int(b)))
454+
hex_stream.append(f"{r:02X}{g:02X}{b:02X}")
455+
456+
payload = "".join(hex_stream)
457+
self._send(f"write::leds::{pin}::2::{payload}")
458+
420459

421460
def rgb_strip_show_all(self):
422461
pin = self.pins[ComponentPins.RGB_LED_STRIP][0]
423462
self._send(f"write::leds::{pin}::1")
463+
464+
424465

425466
# Passive Buzzer
426467

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Initialise the program settings and configurations
1010

1111
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
12-
eb.config_dht_temp(4, "DHT11")
12+
eb.config_dht_temp(6, "DHT11")
1313

1414
for _ in range(1, 4):
1515
temp = eb.dht_temp_celcius()

lcd.py renamed to examples/lcd.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Initialise the program settings and configurations
99
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
10-
eb.config_lcd(2, 16, 39) # Configures the LCD Screen pins
10+
eb.config_lcd(4, 20, 63) # Configures the LCD Screen pins
1111

1212

1313

@@ -20,20 +20,16 @@
2020
eb.lcd_print(0, 0, "HELLO") # Print the first row text on the LCD screen
2121
eb.lcd_print(1, 0, "WORLD") # Print the second row text on the LCD screen
2222
time.sleep(3) # Pause / Wait
23-
time.sleep(2) # Wait for the given/defined seconds.
24-
eb.lcd_print(0, 0, "Hi") # Print a message on the LCD screen at specified row and column
25-
time.sleep(2) # Wait for the given/defined seconds.
26-
eb.lcd_clear() # Clear the LCD screen
27-
time.sleep(2) # Wait for the given/defined seconds.
28-
eb.lcd_print(0, 6, "Hi") # Print a message on the LCD screen at specified row and column
29-
time.sleep(2) # Wait for the given/defined seconds.
3023
eb.lcd_scrollright()
3124
time.sleep(2) # Wait for the given/defined seconds.
3225
eb.lcd_scrollleft()
3326
time.sleep(2) # Wait for the given/defined seconds.
3427
eb.lcd_blink_curor(0, 0, True) # Turn on the blink.
35-
time.sleep(4) # Wait for the given/defined seconds.
28+
time.sleep(2) # Wait for the given/defined seconds.
3629
eb.lcd_blink_curor(0, 0, False) # Turn off the blink.
37-
time.sleep(4) # Wait for the given/defined seconds.
38-
30+
time.sleep(2) # Wait for the given/defined seconds.
31+
eb.lcd_print_all_4(". HELLO", " WORLD. .", "Talk", " Test Testing ! !!. *")
32+
time.sleep(2) # Wait for the given/defined seconds.
33+
eb.lcd_clear()
34+
time.sleep(2) # Wait for the given/defined seconds.
3935
print("COMPLETE")

examples/led_matrix.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
11+
eb.config_led_matrix(10, 11, 12, False) # Echo and Trig
12+
13+
for i in range(1, 9):
14+
eb.set_led_matrix_led(i, i, True)
15+
time.sleep(0.2)
16+
eb.set_led_matrix_led(i, i, False)
17+
18+
for i in range(8, 0, -1):
19+
eb.set_led_matrix_led(i, i, True)
20+
time.sleep(0.2)
21+
eb.set_led_matrix_led(i, i, False)
22+
23+
24+
for _ in range(1, 4):
25+
eb.draw_led_matrix([
26+
"B00000000",
27+
"B01100110",
28+
"B01100110",
29+
"B00000000",
30+
"B00011000",
31+
"B00000000",
32+
"B11111111",
33+
"B01111110"
34+
])
35+
time.sleep(1)
36+
eb.draw_led_matrix([
37+
"B00000000",
38+
"B01100000",
39+
"B01100110",
40+
"B00000000",
41+
"B00011000",
42+
"B00000000",
43+
"B11111111",
44+
"B01111110"
45+
])
46+
time.sleep(1)
47+
print("COMPLETE")

examples/rgb_strip.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
n_leds = 30
8+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
11+
eb.config_rgb_strip("A0", 30, "GRB", 10)
12+
13+
# Scarier colors (darker, less toy-like)
14+
PUMPKIN_ORANGE = (255, 35, 0) # deep orange
15+
WITCH_PURPLE = (90, 0, 130) # dark purple
16+
17+
18+
for j in range(4):
19+
colors = []
20+
first_color = PUMPKIN_ORANGE if j % 2 == 0 else WITCH_PURPLE
21+
second_color = PUMPKIN_ORANGE if j % 2 != 0 else WITCH_PURPLE
22+
for i in range(n_leds):
23+
if i % 2 == 0:
24+
colors.append(first_color)
25+
else:
26+
colors.append(second_color)
27+
28+
# Send frame and show
29+
eb.rgb_strip_set_all_colors(colors)
30+
eb.rgb_strip_show_all()
31+
time.sleep(2)
32+
33+
# Test setting all color individually
34+
for i in range(n_leds):
35+
eb.rgb_strip_set_color(i, 0, 200, 0)
36+
37+
eb.rgb_strip_show_all()
38+
time.sleep(2)
39+
40+
41+
42+
print("COMPLETE")

examples/servos.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from electroblocks import ElectroBlocks
2+
import time # imports the time library
3+
4+
5+
# Variable Declaration
6+
i = 0
7+
8+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
11+
eb.config_servo(9) # Configures the servo motor on pin 8
12+
eb.config_servo(11) # Configures the servo motor on pin 7
13+
14+
15+
16+
while True:
17+
for i in range(0, 91, 5):
18+
eb.move_servo(9, i) # Rotate servo position to i degrees
19+
eb.move_servo(11, (180 - i)) # Rotate servo position to (180 - i) degrees
20+
time.sleep(0.05) # Wait for the given/defined seconds.
21+
for i in range(90, -1, -5):
22+
eb.move_servo(9, i) # Rotate servo position to i degrees
23+
eb.move_servo(11, (180 - i)) # Rotate servo position to (180 - i) degrees
24+
time.sleep(0.05) # Wait for the given/defined seconds.

led_matrix.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

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.16"
7+
version = "0.1.17"
88
description = "Python client library to interact with ElectroBlocks Arduino firmware"
99
authors = [
1010
{ name = "Noah Glaser", email = "[email protected]" }

0 commit comments

Comments
 (0)