Skip to content

Commit fe012ac

Browse files
First Pass
1 parent d0d8485 commit fe012ac

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ElectroBlocks
2+
3+
**ElectroBlocks** is a Python library for communicating with Arduino-based educational hardware using a custom serial protocol. It simplifies control of servos, RGB LEDs, LCD screens, and more—especially in block-based coding environments like Blockly.
4+
5+
## Features
6+
7+
- Auto-detects the first available Arduino Uno or Mega over USB
8+
- Sends `config:` and control commands to the Arduino firmware
9+
- Waits for `"System:READY"` and `"DONE_NEXT_COMMAND"` to sync logic
10+
- Supports digital write, RGB LEDs, LCD printing, and servo control
11+
12+
## Installation
13+
14+
```bash
15+
pip install git+https://github.com/ElectroBlocks/python.git
16+
```
17+
18+
## Example
19+
20+
```python
21+
from electroblocks import ElectroBlocks
22+
23+
eb = ElectroBlocks()
24+
eb.config_servo(5)
25+
eb.move_servo(5, 90)
26+
eb.config_rgb(3, 5, 6)
27+
eb.set_rgb(255, 128, 0)
28+
eb.config_lcd()
29+
eb.lcd_print(0, 0, "Hello")
30+
eb.lcd_clear()
31+
eb.digital_write(13, 1)
32+
eb.close()
33+
```
34+
35+
## License
36+
37+
MIT

electroblocks.zip

1.3 KB
Binary file not shown.

electroblocks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .core import ElectroBlocks

electroblocks/core.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import serial
2+
import serial.tools.list_ports
3+
import time
4+
5+
class ElectroBlocks:
6+
def __init__(self, baudrate=115200, timeout=2):
7+
self.ser = self._auto_connect(baudrate, timeout)
8+
self._wait_for_ready()
9+
10+
def _auto_connect(self, baudrate, timeout):
11+
ports = list(serial.tools.list_ports.comports())
12+
for p in ports:
13+
if "Arduino" in p.description or "ttyACM" in p.device or "ttyUSB" in p.device:
14+
try:
15+
ser = serial.Serial(p.device, baudrate, timeout=timeout)
16+
time.sleep(2) # Give Arduino time to reset
17+
return ser
18+
except serial.SerialException:
19+
continue
20+
raise Exception("No Arduino Uno or Mega found.")
21+
22+
def _wait_for_ready(self):
23+
self.ser.write(b"IAM_READY|\n")
24+
while True:
25+
if self.ser.in_waiting:
26+
line = self.ser.readline().decode().strip()
27+
if "System:READY" in line:
28+
break
29+
30+
def _send(self, cmd):
31+
self.ser.write((cmd + "|\n").encode())
32+
while True:
33+
if self.ser.in_waiting:
34+
line = self.ser.readline().decode().strip()
35+
if "DONE_NEXT_COMMAND" in line:
36+
break
37+
38+
def config_servo(self, pin):
39+
self._send(f"config:servo={pin}")
40+
41+
def move_servo(self, pin, angle):
42+
self._send(f"s:{pin}:{angle}")
43+
44+
def config_rgb(self, r_pin, g_pin, b_pin):
45+
self._send(f"config:rgb={r_pin},{g_pin},{b_pin}")
46+
47+
def set_rgb(self, r, g, b):
48+
self._send(f"rgb:{r},{g},{b}")
49+
50+
def config_lcd(self, rows=2, cols=16):
51+
self._send(f"config:lcd={rows},{cols}")
52+
53+
def lcd_print(self, row, col, message):
54+
self._send(f"l:{row}:{col}:{message}")
55+
56+
def lcd_clear(self):
57+
self._send("l:clear")
58+
59+
def digital_write(self, pin, value):
60+
self._send(f"dw:{pin}:{value}")
61+
62+
def close(self):
63+
if self.ser and self.ser.is_open:
64+
self.ser.close()

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='electroblocks',
5+
version='0.1.0',
6+
packages=find_packages(),
7+
install_requires=['pyserial'],
8+
author='Your Name',
9+
author_email='[email protected]',
10+
description='Python client library to interact with ElectroBlocks Arduino firmware',
11+
url='https://github.com/yourusername/electroblocks',
12+
classifiers=[
13+
'Programming Language :: Python :: 3',
14+
'Operating System :: OS Independent',
15+
],
16+
python_requires='>=3.6',
17+
)

0 commit comments

Comments
 (0)