11import serial
22import serial .tools .list_ports
33import 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
515class 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 :
0 commit comments