Skip to content

Commit c1ed7ca

Browse files
Attempting to fix github scripts and updated buttons and digital read
1 parent 58e1b64 commit c1ed7ca

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

.github/workflows/manual_publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ on:
44
workflow_dispatch:
55

66
jobs:
7-
upload:
7+
deploy:
88
runs-on: ubuntu-latest
9+
permissions:
10+
id-token: write
911

1012
steps:
1113
- name: Check out repository

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
**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.
44

5+
## Upgrade To New Version
6+
7+
- Go to this file [pyproject.toml](pyproject.toml)
8+
- Change the version number by .1 `version = "0.1.8"`
9+
- Push to main
10+
511
## Features
612

713
- Auto-detects the first available Arduino Uno or Mega over USB

button.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+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
11+
eb.config_button(7) # Configures the LCD Screen pins
12+
13+
for _ in range(1, 4):
14+
if eb.is_button_pressed(7):
15+
print("BUTTON PRESSED 7")
16+
else:
17+
print("BUTTON NOT PRESSED")
18+
19+
time.sleep(5)
20+
print("CONTINUE")

digital_read.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+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks() # Create an instance of the ElectroBlocks class
11+
eb.config_digital_read(7) # Configures the LCD Screen pins
12+
13+
for _ in range(1, 4):
14+
if eb.digital_read(7):
15+
print("ON")
16+
else:
17+
print("OFF")
18+
19+
time.sleep(5)
20+
print("CONTINUE")

electroblocks/core.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import time
44

55
class ElectroBlocks:
6+
7+
last_sense_data = ""
8+
69
def __init__(self, baudrate=115200, timeout=2):
710
self.ser = self._auto_connect(baudrate, timeout)
811
self._wait_for_ready()
@@ -15,44 +18,77 @@ def _auto_connect(self, baudrate, timeout):
1518
ser = serial.Serial(p.device, baudrate, timeout=timeout)
1619
time.sleep(2) # Give Arduino time to reset
1720
return ser
18-
except serial.SerialException:
21+
except serial.SerialException as e:
22+
print(f"Failed to connect to {e}. Trying next port...")
1923
continue
2024
raise Exception("No Arduino Uno or Mega found.")
2125

22-
def _wait_for_ready(self):
23-
self.ser.write(b"IAM_READY|")
26+
def _wait_for_message(self, message):
2427
while True:
2528
if self.ser.in_waiting:
2629
line = self.ser.readline().decode("utf-8", errors="ignore").strip()
27-
if "System:READY" in line:
28-
break
30+
if message in line:
31+
return line
32+
33+
def _get_sensor_str(self):
34+
self._send("sense")
35+
message = self._wait_for_message("SENSE_COMPLETE")
36+
message = message.replace("SENSE_COMPLETE", "")
37+
sensorsStr = message.split(";")
38+
return sensorsStr
39+
40+
# return the result of pin read that is being sensed
41+
def _find_sensor_str(self, sensorPin, sensorType):
42+
sensorsStr = self._get_sensor_str()
43+
for sensor in sensorsStr:
44+
if len(sensor) == 0:
45+
continue
46+
[type, pin, result] = sensor.split(":")
47+
if (type == sensorType and pin == str(sensorPin)):
48+
return result
49+
50+
return ""
51+
52+
def _wait_for_ready(self):
53+
self.ser.write(b"IAM_READY|")
54+
self._wait_for_message("System:READY")
2955

3056
def _send(self, cmd):
3157
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
58+
self._wait_for_message("DONE_NEXT_COMMAND")
3759

60+
# Digital Write Method
61+
def config_digital_read(self, pin):
62+
self._send(f"config:b={pin}")
63+
64+
def digital_read(self, pin):
65+
return self._find_sensor_str(pin, "dr") == "1"
66+
67+
# Button Methods
68+
def config_button(self, pin):
69+
self._send(f"config:b={pin}")
70+
71+
def is_button_pressed(self, pin):
72+
return self._find_sensor_str(pin, "b") == "0"
73+
74+
# Servo Methods
3875
def config_servo(self, pin):
3976
self._send(f"config:servo={pin}")
4077

4178
def move_servo(self, pin, angle):
4279
self._send(f"s:{pin}:{angle}")
4380

81+
# RGB Methods
4482
def config_rgb(self, r_pin, g_pin, b_pin):
4583
self._send(f"config:rgb={r_pin},{g_pin},{b_pin}")
4684

4785
def set_rgb(self, r, g, b):
4886
self._send(f"rgb:{r},{g},{b}")
4987

88+
# LCD Methods
5089
def config_lcd(self, rows=2, cols=16):
5190
self._send(f"config:lcd={rows},{cols}")
5291

53-
54-
# LCD Methods
55-
5692
def lcd_print(self, row, col, message):
5793
self._send(f"l:{row}:{col}:{message}")
5894

@@ -77,8 +113,7 @@ def lcd_scrollright(self):
77113
def lcd_scrollleft(self):
78114
self._send("l:scroll_left")
79115

80-
# LCD Methods
81-
116+
# LED Methods
82117
def digital_write(self, pin, value):
83118
self._send(f"dw:{pin}:{value}")
84119

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.7"
7+
version = "0.1.8"
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)