1
1
import serial
2
2
import serial .tools .list_ports
3
3
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
4
14
5
15
class ElectroBlocks :
6
16
7
17
last_sense_data = ""
8
18
verbose = False
19
+ pins = {}
9
20
10
- def __init__ (self , baudrate = 9600 , timeout = 2 , verbose = False ):
21
+ def __init__ (self , baudrate = 115200 , timeout = 2 , verbose = False ):
11
22
self .ser = self ._auto_connect (baudrate , timeout )
12
23
self .verbose = verbose
13
24
self ._wait_for_ready ()
@@ -31,6 +42,13 @@ def _drain_serial(self):
31
42
self .ser .reset_input_buffer ()
32
43
33
44
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
+
34
52
def _wait_for_message (self , message ):
35
53
count = 0
36
54
while count < 10 :
@@ -71,7 +89,7 @@ def _wait_for_ready(self):
71
89
72
90
def _send (self , cmd ):
73
91
self .ser .write ((cmd + "|\n " ).encode ())
74
- self ._wait_for_message ("DONE_NEXT_COMMAND " )
92
+ self ._wait_for_message ("OK " )
75
93
76
94
# Digital Write Method
77
95
def config_digital_read (self , pin ):
@@ -89,8 +107,18 @@ def rfid_tag_number(self):
89
107
90
108
def rfid_sensed_card (self ):
91
109
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 } " )
92
115
116
+ def ir_remote_has_sensed_code (self ):
117
+ return len (self ._find_sensor_str ("0" , "ir" )) > 0
93
118
119
+ def ir_remote_get_code (self ):
120
+ return self ._find_sensor_str ("0" , "ir" )
121
+
94
122
# Motion Sensors
95
123
def config_motion_sensor (self , echoPin , trigPin ):
96
124
self ._send (f"config:m={ echoPin } ,{ trigPin } " )
@@ -107,49 +135,116 @@ def is_button_pressed(self, pin):
107
135
108
136
# Servo Methods
109
137
def config_servo (self , pin ):
110
- self ._send (f"config: servo= { pin } " )
138
+ self ._send (f"register:: servo:: { pin } " )
111
139
112
140
def move_servo (self , pin , angle ):
113
- self ._send (f"s: { pin } :{ angle } " )
141
+ self ._send (f"write::servo:: { pin } : :{ angle } " )
114
142
115
143
# 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 } " )
118
149
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 } " )
121
153
122
154
# 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 } " )
125
159
126
160
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 } " )
128
162
129
163
def lcd_clear (self ):
130
- self ._send ("l:clear " )
164
+ self ._send ("write::lcd::A5::1 " )
131
165
132
166
def lcd_toggle_backlight (self , on ):
133
167
if on :
134
- self ._send ("l:backlighton " )
168
+ self ._send ("write::lcd::A5::2 " )
135
169
else :
136
- self ._send ("l:backlightoff " )
170
+ self ._send ("write::lcd::A5::3 " )
137
171
138
172
def lcd_blink_curor (self , row , col , on ):
139
173
if on == True :
140
- self ._send (f"l:cursor_on: { row } :{ col } " )
174
+ self ._send (f"write::lcd::A5::5:: { row } : :{ col } " )
141
175
else :
142
- self ._send (f"l:cursor_off: { row } : { col } " )
176
+ self ._send (f"write::lcd::A5::4 " )
143
177
144
178
def lcd_scrollright (self ):
145
- self ._send ("l:scroll_right " )
179
+ self ._send ("write::lcd::A5::6 " )
146
180
147
181
def lcd_scrollleft (self ):
148
- self ._send ("l:scroll_left " )
182
+ self ._send ("write::lcd::A5::7 " )
149
183
150
184
# 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
+
151
190
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
+
153
248
154
249
def close (self ):
155
250
if self .ser and self .ser .is_open :
0 commit comments