Skip to content

Commit 6101f63

Browse files
committed
First commit of API for turning on LED
1 parent a3f5a84 commit 6101f63

File tree

4 files changed

+52
-195
lines changed

4 files changed

+52
-195
lines changed

neobot/model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1717
# Boston, MA 02111-1307 USA
1818

19+
from functools import reduce
20+
1921

2022
DeviceType = type("Enum", (), {"SENSOR": 0, "EFFECTOR": 1, "EVENT": 2, "COMMAND": 3})
2123
DataType = type("Enum", (), {"INTEGER": 4, "FLOAT": 5, "STRING": 6})
@@ -369,6 +371,10 @@ def _to_hex3(self, number):
369371
return result + value
370372
return value
371373

374+
def _make_checksum(self, cmd):
375+
# Strip header and accumulate to the end
376+
return reduce(lambda x, y: x+y, bytes.fromhex(cmd[4:])) & 0xFF
377+
372378
def _request_motoring_data(self):
373379
pass
374380

neobot/neosoco.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,9 @@
2525
class Neosoco(Robot):
2626
ID = "kr.neobot.physical.neosoco"
2727

28-
LEFT_WHEEL = 0x00400000
29-
RIGHT_WHEEL = 0x00400001
30-
BUZZER = 0x00400002
31-
OUTPUT_A = 0x00400003
32-
OUTPUT_B = 0x00400004
33-
TOPOLOGY = 0x00400005
34-
LEFT_LED = 0x00400006
35-
RIGHT_LED = 0x00400007
36-
NOTE = 0x00400008
37-
LINE_TRACER_MODE = 0x00400009
38-
LINE_TRACER_SPEED = 0x0040000a
39-
IO_MODE_A = 0x0040000b
40-
IO_MODE_B = 0x0040000c
41-
CONFIG_PROXIMITY = 0x0040000d
42-
CONFIG_GRAVITY = 0x0040000e
43-
CONFIG_BAND_WIDTH = 0x0040000f
44-
45-
SIGNAL_STRENGTH = 0x00400010
46-
LEFT_PROXIMITY = 0x00400011
47-
RIGHT_PROXIMITY = 0x00400012
48-
LEFT_FLOOR = 0x00400013
49-
RIGHT_FLOOR = 0x00400014
50-
ACCELERATION = 0x00400015
51-
LIGHT = 0x00400016
52-
TEMPERATURE = 0x00400017
53-
INPUT_A = 0x00400018
54-
INPUT_B = 0x00400019
55-
LINE_TRACER_STATE = 0x0040001a
56-
TILT = 0x0040001b
57-
BATTERY_STATE = 0x0040001c
58-
59-
TOPOLOGY_NONE = 0
60-
TOPOLOGY_DAISY_CHAIN = 1
61-
TOPOLOGY_STAR = 2
62-
TOPOLOGY_EXTENDED_STAR = 3
28+
OUTPUT_1 = 0x00400000
29+
OUTPUT_2 = 0x00400001
30+
OUTPUT_3 = 0x00400002
6331

6432
LED_OFF = 0
6533
LED_BLUE = 1
@@ -612,6 +580,21 @@ def leds(self, left_color, right_color=None):
612580
if isinstance(left_color, (int, float)):
613581
self.write(Neosoco.RIGHT_LED, int(left_color))
614582

583+
def led_on(self, port: str, brightness: str):
584+
if isinstance(port, str) and isinstance(brightness, int):
585+
if port.lower() =='out1':
586+
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
587+
elif port.lower() =='out2':
588+
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
589+
elif port.lower() =='out3':
590+
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
591+
elif port.lower() =='all':
592+
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
593+
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
594+
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
595+
else:
596+
raise TypeError
597+
615598
def left_led(self, color):
616599
if isinstance(color, (int, float)):
617600
self.write(Neosoco.LEFT_LED, int(color))

neobot/neosoco_neobot.py

Lines changed: 26 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from neobot.serial_connector import SerialConnector
3030
from neobot.linker import Linker
3131

32+
START_BYTES = 'CDAB'
33+
3234

3335
class NeosocoConnectionChecker(object):
3436
def __init__(self, neobot):
@@ -77,22 +79,12 @@ def __init__(self, index):
7779
self._thread = None
7880
self._thread_lock = threading.Lock()
7981

80-
self._left_wheel = 0
81-
self._right_wheel = 0
82+
self._output1 = 0
83+
self._output2 = 0
84+
self._output3 = 0
85+
self._mla = 0
86+
self._mra = 0
8287
self._buzzer = 0
83-
self._output_a = 0
84-
self._output_b = 0
85-
self._topology = 0
86-
self._left_led = 0
87-
self._right_led = 0
88-
self._note = 0
89-
self._line_tracer_mode = 0
90-
self._line_tracer_speed = 5
91-
self._io_mode_a = 0
92-
self._io_mode_b = 0
93-
self._config_proximity = 2
94-
self._config_gravity = 0
95-
self._config_band_width = 3
9688

9789
self._topology_written = False
9890
self._left_led_written = False
@@ -125,36 +117,10 @@ def _set_model_code(self, code):
125117
def _create_model(self):
126118
from neobot.neosoco import Neosoco
127119
dict = self._device_dict = {}
128-
dict[Neosoco.LEFT_WHEEL] = self._left_wheel_device = self._add_device(Neosoco.LEFT_WHEEL, "LeftWheel", DeviceType.EFFECTOR, DataType.INTEGER, 1, -100, 100, 0)
129-
dict[Neosoco.RIGHT_WHEEL] = self._right_wheel_device = self._add_device(Neosoco.RIGHT_WHEEL, "RightWheel", DeviceType.EFFECTOR, DataType.INTEGER, 1, -100, 100, 0)
130-
dict[Neosoco.BUZZER] = self._buzzer_device = self._add_device(Neosoco.BUZZER, "Buzzer", DeviceType.EFFECTOR, DataType.FLOAT, 1, 0, 167772.15, 0.0)
131-
dict[Neosoco.OUTPUT_A] = self._output_a_device = self._add_device(Neosoco.OUTPUT_A, "OutputA", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
132-
dict[Neosoco.OUTPUT_B] = self._output_b_device = self._add_device(Neosoco.OUTPUT_B, "OutputB", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
133-
dict[Neosoco.TOPOLOGY] = self._topology_device = self._add_device(Neosoco.TOPOLOGY, "Topology", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 15, 0)
134-
dict[Neosoco.LEFT_LED] = self._left_led_device = self._add_device(Neosoco.LEFT_LED, "LeftLed", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 7, 0)
135-
dict[Neosoco.RIGHT_LED] = self._right_led_device = self._add_device(Neosoco.RIGHT_LED, "RightLed", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 7, 0)
136-
dict[Neosoco.NOTE] = self._note_device = self._add_device(Neosoco.NOTE, "Note", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 88, 0)
137-
dict[Neosoco.LINE_TRACER_MODE] = self._line_tracer_mode_device = self._add_device(Neosoco.LINE_TRACER_MODE, "LineTracerMode", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 15, 0)
138-
dict[Neosoco.LINE_TRACER_SPEED] = self._line_tracer_speed_device = self._add_device(Neosoco.LINE_TRACER_SPEED, "LineTracerSpeed", DeviceType.COMMAND, DataType.INTEGER, 1, 1, 8, 5)
139-
dict[Neosoco.IO_MODE_A] = self._io_mode_a_device = self._add_device(Neosoco.IO_MODE_A, "IoModeA", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 15, 0)
140-
dict[Neosoco.IO_MODE_B] = self._io_mode_b_device = self._add_device(Neosoco.IO_MODE_B, "IoModeB", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 15, 0)
141-
dict[Neosoco.CONFIG_PROXIMITY] = self._config_proximity_device = self._add_device(Neosoco.CONFIG_PROXIMITY, "ConfigProximity", DeviceType.COMMAND, DataType.INTEGER, 1, 1, 7, 2)
142-
dict[Neosoco.CONFIG_GRAVITY] = self._config_gravity_device = self._add_device(Neosoco.CONFIG_GRAVITY, "ConfigGravity", DeviceType.COMMAND, DataType.INTEGER, 1, 0, 3, 0)
143-
dict[Neosoco.CONFIG_BAND_WIDTH] = self._config_band_width_device = self._add_device(Neosoco.CONFIG_BAND_WIDTH, "ConfigBandWidth", DeviceType.COMMAND, DataType.INTEGER, 1, 1, 8, 3)
144-
dict[Neosoco.SIGNAL_STRENGTH] = self._signal_strength_device = self._add_device(Neosoco.SIGNAL_STRENGTH, "SignalStrength", DeviceType.SENSOR, DataType.INTEGER, 1, -128, 0, 0)
145-
dict[Neosoco.LEFT_PROXIMITY] = self._left_proximity_device = self._add_device(Neosoco.LEFT_PROXIMITY, "LeftProximity", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
146-
dict[Neosoco.RIGHT_PROXIMITY] = self._right_proximity_device = self._add_device(Neosoco.RIGHT_PROXIMITY, "RightProximity", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
147-
dict[Neosoco.LEFT_FLOOR] = self._left_floor_device = self._add_device(Neosoco.LEFT_FLOOR, "LeftFloor", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
148-
dict[Neosoco.RIGHT_FLOOR] = self._right_floor_device = self._add_device(Neosoco.RIGHT_FLOOR, "RightFloor", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
149-
dict[Neosoco.ACCELERATION] = self._acceleration_device = self._add_device(Neosoco.ACCELERATION, "Acceleration", DeviceType.SENSOR, DataType.INTEGER, 3, -32768, 32767, 0)
150-
dict[Neosoco.LIGHT] = self._light_device = self._add_device(Neosoco.LIGHT, "Light", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 65535, 0)
151-
dict[Neosoco.TEMPERATURE] = self._temperature_device = self._add_device(Neosoco.TEMPERATURE, "Temperature", DeviceType.SENSOR, DataType.INTEGER, 1, -40, 88, 0)
152-
dict[Neosoco.INPUT_A] = self._input_a_device = self._add_device(Neosoco.INPUT_A, "InputA", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
153-
dict[Neosoco.INPUT_B] = self._input_b_device = self._add_device(Neosoco.INPUT_B, "InputB", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
154-
dict[Neosoco.LINE_TRACER_STATE] = self._line_tracer_state_device = self._add_device(Neosoco.LINE_TRACER_STATE, "LineTracerState", DeviceType.EVENT, DataType.INTEGER, 1, 0, 255, 0)
155-
dict[Neosoco.TILT] = self._tilt_device = self._add_device(Neosoco.TILT, "Tilt", DeviceType.EVENT, DataType.INTEGER, 1, -3, 3, 0)
156-
dict[Neosoco.BATTERY_STATE] = self._battery_state_device = self._add_device(Neosoco.BATTERY_STATE, "BatteryState", DeviceType.EVENT, DataType.INTEGER, 1, 0, 2, 2)
157-
120+
dict[Neosoco.OUTPUT_1] = self._output_1_device = self._add_device(Neosoco.OUTPUT_1, "Output1", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
121+
dict[Neosoco.OUTPUT_2] = self._output_2_device = self._add_device(Neosoco.OUTPUT_2, "Output2", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
122+
dict[Neosoco.OUTPUT_3] = self._output_3_device = self._add_device(Neosoco.OUTPUT_3, "Output3", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
123+
158124
def find_device_by_id(self, device_id):
159125
return self._device_dict.get(device_id)
160126

@@ -247,44 +213,9 @@ def _reset(self):
247213

248214
def _request_motoring_data(self):
249215
with self._thread_lock:
250-
self._left_wheel = self._left_wheel_device.read()
251-
self._right_wheel = self._right_wheel_device.read()
252-
self._buzzer = self._buzzer_device.read()
253-
self._output_a = self._output_a_device.read()
254-
self._output_b = self._output_b_device.read()
255-
if self._topology_device._is_written():
256-
self._topology = self._topology_device.read()
257-
self._topology_written = True
258-
if self._left_led_device._is_written():
259-
self._left_led = self._left_led_device.read()
260-
self._left_led_written = True
261-
if self._right_led_device._is_written():
262-
self._right_led = self._right_led_device.read()
263-
self._right_led_written = True
264-
if self._note_device._is_written():
265-
self._note = self._note_device.read()
266-
self._note_written = True
267-
if self._line_tracer_mode_device._is_written():
268-
self._line_tracer_mode = self._line_tracer_mode_device.read()
269-
self._line_tracer_mode_written = True
270-
if self._line_tracer_speed_device._is_written():
271-
self._line_tracer_speed = self._line_tracer_speed_device.read()
272-
self._line_tracer_speed_written = True
273-
if self._io_mode_a_device._is_written():
274-
self._io_mode_a = self._io_mode_a_device.read()
275-
self._io_mode_a_written = True
276-
if self._io_mode_b_device._is_written():
277-
self._io_mode_b = self._io_mode_b_device.read()
278-
self._io_mode_b_written = True
279-
if self._config_proximity_device._is_written():
280-
self._config_proximity = self._config_proximity_device.read()
281-
self._config_proximity_written = True
282-
if self._config_gravity_device._is_written():
283-
self._config_gravity = self._config_gravity_device.read()
284-
self._config_gravity_written = True
285-
if self._config_band_width_device._is_written():
286-
self._config_band_width = self._config_band_width_device.read()
287-
self._config_band_width_written = True
216+
self._output_1 = self._output_1_device.read()
217+
self._output_2 = self._output_2_device.read()
218+
self._output_3 = self._output_3_device.read()
288219
self._clear_written()
289220

290221
def _color_to_rgb(self, color):
@@ -306,81 +237,16 @@ def _speed_to_gain(self, speed):
306237
def _encode_motoring_packet(self, address):
307238
result = ""
308239
with self._thread_lock:
309-
if self._model_code == 0x0E:
310-
result += "10"
311-
result += self._to_hex(self._left_wheel)
312-
result += self._to_hex(self._right_wheel)
313-
rgb = self._color_to_rgb(self._left_led)
314-
result += self._to_hex(rgb[0])
315-
result += self._to_hex(rgb[1])
316-
result += self._to_hex(rgb[2])
317-
rgb = self._color_to_rgb(self._right_led)
318-
result += self._to_hex(rgb[0])
319-
result += self._to_hex(rgb[1])
320-
result += self._to_hex(rgb[2])
321-
result += "000000"
322-
temp = self._line_tracer_mode & 0x0f
323-
if temp > 7: temp += 1
324-
if self._line_tracer_mode_written:
325-
self._line_tracer_count = 0
326-
if temp > 0:
327-
self._line_tracer_flag = (self._line_tracer_flag % 15) + 1
328-
self._line_tracer_event = 1
329-
else:
330-
self._line_tracer_event = 0
331-
self._line_tracer_mode_written = False
332-
temp |= (self._line_tracer_flag & 0x0f) << 4
333-
result += self._to_hex(temp)
334-
temp = (self._line_tracer_speed & 0x0f) << 4
335-
temp |= self._speed_to_gain(self._line_tracer_speed) & 0x0f
336-
result += self._to_hex(temp)
337-
temp = (self._config_proximity & 0x07) << 5
338-
temp |= (self._config_band_width & 0x07) << 2
339-
temp |= self._config_gravity & 0x03
340-
result += self._to_hex(temp)
341-
temp = (self._io_mode_a & 0x0f) << 4
342-
temp |= self._io_mode_b & 0x0f
343-
result += self._to_hex(temp)
344-
result += self._to_hex(self._output_a)
345-
result += self._to_hex(self._output_b)
346-
if self._note > 0:
347-
result += "01"
348-
result += self._to_hex(self._note)
349-
else:
350-
temp = self._buzzer
351-
if temp > 6500: temp = 6500
352-
result += self._to_hex2(Util.round(temp * 10) + 512)
353-
else:
354-
result += self._to_hex(self._topology & 0x0f)
355-
result += "0010"
356-
result += self._to_hex(self._left_wheel)
357-
result += self._to_hex(self._right_wheel)
358-
result += self._to_hex(self._left_led)
359-
result += self._to_hex(self._right_led)
360-
result += self._to_hex3(Util.round(self._buzzer * 100))
361-
result += self._to_hex(self._note)
362-
if self._line_tracer_mode_written:
363-
if self._line_tracer_mode > 0:
364-
self._line_tracer_flag ^= 0x80
365-
self._line_tracer_event = 1
366-
self._line_tracer_mode_written = False
367-
temp = (self._line_tracer_mode & 0x0f) << 3
368-
temp |= (self._line_tracer_speed - 1) & 0x07
369-
temp |= self._line_tracer_flag & 0x80
370-
result += self._to_hex(temp)
371-
result += self._to_hex(self._config_proximity)
372-
temp = (self._config_gravity & 0x0f) << 4
373-
temp |= self._config_band_width & 0x0f
374-
result += self._to_hex(temp)
375-
temp = (self._io_mode_a & 0x0f) << 4
376-
temp |= self._io_mode_b & 0x0f
377-
result += self._to_hex(temp)
378-
result += self._to_hex(self._output_a)
379-
result += self._to_hex(self._output_b)
380-
result += "000000"
381-
result += "-"
382-
result += address
383-
result += "\r"
240+
result += START_BYTES
241+
result += self._to_hex(self._output_1) # OUT1
242+
result += self._to_hex(self._output_2) # OUT2
243+
result += self._to_hex(self._output_3) # OUT3
244+
result += self._to_hex(0) # MLA
245+
result += self._to_hex(0) # MRA
246+
result += self._to_hex(0) # BUZZER
247+
result += self._to_hex(0) # FND
248+
result += self._to_hex(0) # Not Used
249+
result += self._to_hex(self._make_checksum(result)) # Checksum
384250
return result
385251

386252
def _decode_sensory_packet(self, packet):
@@ -522,12 +388,12 @@ def _receive(self, connector):
522388
if connector:
523389
packet = connector.read()
524390
if packet:
525-
if self._decode_sensory_packet(packet):
391+
# if self._decode_sensory_packet(packet): # Temporary blocking
526392
if self._ready == False:
527393
self._ready = True
528394
Runner.register_checked()
529395
self._notify_sensory_device_data_changed()
530-
return True
396+
return True
531397
return False
532398

533399
def _send(self, connector):

test.py

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

33
n = Neosoco()
44

5+
n.led_on("out1", 255)
6+
wait(1000)

0 commit comments

Comments
 (0)