Skip to content

Commit 9ef7145

Browse files
upgrading to the latest version of the firmware for addons
1 parent 324396e commit 9ef7145

File tree

8 files changed

+242
-20
lines changed

8 files changed

+242
-20
lines changed

analog_write.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
10+
eb.analog_config(6)
11+
12+
for i in range(1, 200):
13+
eb.analog_write(6, i)
14+
time.sleep(0.2)
15+
16+
print("COMPLETE")

digital_write.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
10+
eb.digital_config(13)
11+
for _ in range(1, 10):
12+
eb.digital_write(13, 1)
13+
time.sleep(1)
14+
eb.digital_write(13, 0)
15+
time.sleep(1)
16+
17+
print("COMPLETE")

electroblocks/core.py

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import serial
22
import serial.tools.list_ports
33
import time
4+
from enum import Enum
5+
6+
class ComponentPins(Enum):
7+
UNKNOWN = 0
8+
LCD = 1
9+
RGBLED = 2
10+
DIGITAL_WRITE = 3
11+
ANALOG_WRITE = 4
12+
RGB_LED_STRIP = 5
13+
SERVO = 6
414

515
class ElectroBlocks:
616

717
last_sense_data = ""
818
verbose = False
19+
pins = {}
920

10-
def __init__(self, baudrate=9600, timeout=2, verbose = False):
21+
def __init__(self, baudrate=115200, timeout=2, verbose = False):
1122
self.ser = self._auto_connect(baudrate, timeout)
1223
self.verbose = verbose
1324
self._wait_for_ready()
@@ -31,6 +42,13 @@ def _drain_serial(self):
3142
self.ser.reset_input_buffer()
3243

3344

45+
def _add_pin(self, pinType, pin):
46+
if pinType not in self.pins:
47+
self.pins[pinType] = [str(pin)]
48+
else:
49+
self.pins[pinType].append(str(pin))
50+
51+
3452
def _wait_for_message(self, message):
3553
count = 0
3654
while count < 10:
@@ -71,7 +89,7 @@ def _wait_for_ready(self):
7189

7290
def _send(self, cmd):
7391
self.ser.write((cmd + "|\n").encode())
74-
self._wait_for_message("DONE_NEXT_COMMAND")
92+
self._wait_for_message("OK")
7593

7694
# Digital Write Method
7795
def config_digital_read(self, pin):
@@ -89,8 +107,18 @@ def rfid_tag_number(self):
89107

90108
def rfid_sensed_card(self):
91109
return len(self._find_sensor_str("0", "rfid")) > 0
110+
111+
#IR Remote
112+
113+
def config_ir_remote(self, pin):
114+
self._send(f"config:ir={pin}")
92115

116+
def ir_remote_has_sensed_code(self):
117+
return len(self._find_sensor_str("0", "ir")) > 0
93118

119+
def ir_remote_get_code(self):
120+
return self._find_sensor_str("0", "ir")
121+
94122
# Motion Sensors
95123
def config_motion_sensor(self, echoPin, trigPin):
96124
self._send(f"config:m={echoPin},{trigPin}")
@@ -107,49 +135,116 @@ def is_button_pressed(self, pin):
107135

108136
# Servo Methods
109137
def config_servo(self, pin):
110-
self._send(f"config:servo={pin}")
138+
self._send(f"register::servo::{pin}")
111139

112140
def move_servo(self, pin, angle):
113-
self._send(f"s:{pin}:{angle}")
141+
self._send(f"write::servo::{pin}::{angle}")
114142

115143
# RGB Methods
116-
def config_rgb(self, r_pin, g_pin, b_pin):
117-
self._send(f"config:rgb={r_pin},{g_pin},{b_pin}")
144+
def config_rgbled(self, r_pin, g_pin, b_pin):
145+
self._add_pin(ComponentPins.RGBLED, r_pin)
146+
self._add_pin(ComponentPins.RGBLED, g_pin)
147+
self._add_pin(ComponentPins.RGBLED, b_pin)
148+
self._send(f"register::rgb::{r_pin}::{g_pin}::{b_pin}")
118149

119-
def set_rgb(self, r, g, b):
120-
self._send(f"rgb:{r},{g},{b}")
150+
def set_color_rgbled(self, r, g, b):
151+
redpin = self.pins[ComponentPins.RGBLED][0]
152+
self._send(f"write::rgb::{redpin}::{r}::{g}::{b}")
121153

122154
# LCD Methods
123-
def config_lcd(self, rows=2, cols=16):
124-
self._send(f"config:lcd={rows},{cols}")
155+
def config_lcd(self, rows=2, cols=16, addr=39):
156+
self._add_pin(ComponentPins.DIGITAL_WRITE, "A5")
157+
self._add_pin(ComponentPins.DIGITAL_WRITE, "A4")
158+
self._send(f"register::lcd::{rows}::{cols}::{addr}")
125159

126160
def lcd_print(self, row, col, message):
127-
self._send(f"l:{row}:{col}:{message}")
161+
self._send(f"write::lcd::A5::9::{row}::{col}::{message}")
128162

129163
def lcd_clear(self):
130-
self._send("l:clear")
164+
self._send("write::lcd::A5::1")
131165

132166
def lcd_toggle_backlight(self, on):
133167
if on:
134-
self._send("l:backlighton")
168+
self._send("write::lcd::A5::2")
135169
else:
136-
self._send("l:backlightoff")
170+
self._send("write::lcd::A5::3")
137171

138172
def lcd_blink_curor(self, row, col, on):
139173
if on == True:
140-
self._send(f"l:cursor_on:{row}:{col}")
174+
self._send(f"write::lcd::A5::5::{row}::{col}")
141175
else:
142-
self._send(f"l:cursor_off:{row}:{col}")
176+
self._send(f"write::lcd::A5::4")
143177

144178
def lcd_scrollright(self):
145-
self._send("l:scroll_right")
179+
self._send("write::lcd::A5::6")
146180

147181
def lcd_scrollleft(self):
148-
self._send("l:scroll_left")
182+
self._send("write::lcd::A5::7")
149183

150184
# LED Methods
185+
186+
def digital_config(self, pin):
187+
self._add_pin(ComponentPins.DIGITAL_WRITE, pin)
188+
self._send(f"register::dw::{pin}")
189+
151190
def digital_write(self, pin, value):
152-
self._send(f"dw:{pin}:{value}")
191+
self._send(f"write::dw::{pin}::{value}")
192+
193+
def analog_write(self, pin, value):
194+
self._send(f"write::aw::{pin}::{value}")
195+
196+
def analog_config(self, pin):
197+
self._send(f"register::aw::{pin}")
198+
self._add_pin(ComponentPins.ANALOG_WRITE, pin)
199+
200+
# NEO PIXELS
201+
202+
def config_rgb_strip(self, pin, count, colorOrderString, brightness):
203+
orderToNumber = {
204+
"RGB": 128,
205+
"GRB": 129,
206+
"RBG": 130,
207+
"GBR": 131,
208+
"BRG": 132,
209+
"BGR": 133,
210+
}
211+
colorOrder = orderToNumber.get(colorOrderString) or 128
212+
self._add_pin(ComponentPins.RGB_LED_STRIP, pin)
213+
self._send(f"register::leds::{pin}::{count}::{colorOrder}::{brightness}")
214+
215+
216+
def rgb_strip_set_color(self, position, red, green, blue):
217+
"""Set color for RGB LED strip at specified position"""
218+
pin = self.pins[ComponentPins.RGB_LED_STRIP][0]
219+
color = self.rgb_to_hex(red, green, blue)
220+
self._send(f"write::leds::{pin}::2::{position}::{color}")
221+
222+
def rgb_strip_show_all(self):
223+
pin = self.pins[ComponentPins.RGB_LED_STRIP][0]
224+
self._send(f"write::leds::{pin}::1")
225+
226+
# Helpers
227+
228+
def rgb_to_hex(self, red, green, blue):
229+
"""
230+
Convert RGB values (0-255) to a hex color string format (#RRGGBB)
231+
232+
Args:
233+
red (int): Red value (0-255)
234+
green (int): Green value (0-255)
235+
blue (int): Blue value (0-255)
236+
237+
Returns:
238+
str: Hex color string in format RRGGBB
239+
"""
240+
# Ensure values are within valid range (0-255)
241+
red = max(0, min(255, int(red)))
242+
green = max(0, min(255, int(green)))
243+
blue = max(0, min(255, int(blue)))
244+
245+
# Convert to hex and format with leading zeros if needed
246+
return f"{red:02x}{green:02x}{blue:02x}".upper()
247+
153248

154249
def close(self):
155250
if self.ser and self.ser.is_open:

ir_remote.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
10+
eb.config_ir_remote(2) # Configures the LCD Screen pins
11+
12+
for _ in range(1, 10):
13+
sensed_code = eb.ir_remote_has_sensed_code()
14+
code = eb.ir_remote_get_code()
15+
print(f"Sensed Code: {sensed_code} | Code: {code}")
16+
time.sleep(10)
17+
print("CONTINUE")

lcd.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
#Import ElectroBlocks library
4+
from electroblocks import ElectroBlocks
5+
import time # imports the time library
6+
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
10+
eb.config_lcd(2, 16, 39) # Configures the LCD Screen pins
11+
12+
13+
14+
for _ in range(1, 3):
15+
eb.lcd_toggle_backlight(False) # Turn off the LCD backlight
16+
time.sleep(2) # Wait for the given/defined seconds.
17+
eb.lcd_toggle_backlight(True) # Turn on the LCD backlight
18+
time.sleep(2) # Wait for the given/defined seconds.
19+
eb.lcd_clear() # Clear the LCD screen
20+
eb.lcd_print(0, 0, "HELLO") # Print the first row text on the LCD screen
21+
eb.lcd_print(1, 0, "WORLD") # Print the second row text on the LCD screen
22+
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.
30+
eb.lcd_scrollright()
31+
time.sleep(2) # Wait for the given/defined seconds.
32+
eb.lcd_scrollleft()
33+
time.sleep(2) # Wait for the given/defined seconds.
34+
eb.lcd_blink_curor(0, 0, True) # Turn on the blink.
35+
time.sleep(4) # Wait for the given/defined seconds.
36+
eb.lcd_blink_curor(0, 0, False) # Turn off the blink.
37+
time.sleep(4) # Wait for the given/defined seconds.
38+
39+
print("COMPLETE")

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.11"
7+
version = "0.1.12"
88
description = "Python client library to interact with ElectroBlocks Arduino firmware"
99
authors = [
1010
{ name = "Noah Glaser", email = "glaserpower@gmail.com" }

rgb_strip.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
10+
eb.config_rgb_strip("A0", 30, "GRB", 10)
11+
12+
for i in range(0, 29):
13+
eb.rgb_strip_set_color(i, 100, 0, 100)
14+
15+
eb.rgb_strip_show_all()
16+
time.sleep(4)
17+
18+
print("COMPLETE")

rgbled.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
# Initialise the program settings and configurations
9+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
10+
eb.config_rgbled(9,10,11) # Configures the LCD Screen pins
11+
12+
for _ in range(1, 10):
13+
eb.set_color_rgbled(100, 0, 0)
14+
time.sleep(1)
15+
eb.set_color_rgbled(0, 100, 0)
16+
time.sleep(1)
17+
eb.set_color_rgbled(0, 0, 100)
18+
time.sleep(1)
19+
20+
print("COMPLETE")

0 commit comments

Comments
 (0)