Skip to content

Commit e9cecb7

Browse files
committed
Refactor APIs and test cases
1 parent d20450a commit e9cecb7

File tree

3 files changed

+63
-111
lines changed

3 files changed

+63
-111
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
Part of the RoboticsWare project - https://roboticsware.uz
55
Copyright (C) 2022 RoboticsWare (neopia.uz@google.com)
66

7-
Currently only NEO SoCo support
7+
Currently support NEO SoCo only
88

99
## Installation
10-
``pip install -U neopia`` should work for most users.
10+
``pip install -U neopia``
1111

1212
## API documentation
1313
Refer to the [Wiki](https://github.com/roboticsware/pylib_neobot/wiki/List-of-APIs)

neopia/neosoco.py

+27-65
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,18 @@ class Neosoco(Robot):
3939
RIGHT_MOTOR = 0x00400009
4040
NOTE = 0x0040000a
4141

42-
LED_OFF = 0
43-
LED_BLUE = 1
44-
LED_GREEN = 2
45-
LED_CYAN = 3
46-
LED_SKY_BLUE = 3
47-
LED_RED = 4
48-
LED_MAGENTA = 5
49-
LED_PURPLE = 5
50-
LED_YELLOW = 6
51-
LED_WHITE = 7
52-
53-
COLOR_NAME_OFF = "off"
42+
COLOR_NAME_WHITE = "white"
5443
COLOR_NAME_RED = "red"
5544
COLOR_NAME_YELLOW = "yellow"
5645
COLOR_NAME_GREEN = "green"
57-
COLOR_NAME_SKY_BLUE = "sky blue"
5846
COLOR_NAME_BLUE = "blue"
59-
COLOR_NAME_PURPLE = "purple"
60-
COLOR_NAME_WHITE = "white"
6147

6248
_COLORS = {
63-
"off": LED_OFF,
64-
"red": LED_RED,
65-
"yellow": LED_YELLOW,
66-
"green": LED_GREEN,
67-
"sky_blue": LED_CYAN,
68-
"skyblue": LED_CYAN,
69-
"sky blue": LED_CYAN,
70-
"blue": LED_BLUE,
71-
"purple": LED_MAGENTA,
72-
"white": LED_WHITE
49+
"white": COLOR_NAME_WHITE,
50+
"red": COLOR_NAME_RED,
51+
"yellow": COLOR_NAME_YELLOW,
52+
"green": COLOR_NAME_GREEN,
53+
"blue": COLOR_NAME_BLUE
7354
}
7455

7556
_NOTE_OFF = 0
@@ -374,6 +355,9 @@ def _notify_motoring_device_data_changed(self):
374355

375356
def set_value(self, port='out1', value=255):
376357
if isinstance(port, str) and isinstance(value, int):
358+
if value < 0 or value > 255:
359+
raise ValueError('Wrong value of input value')
360+
377361
if port.lower() =='out1':
378362
self.write(Neosoco.OUTPUT_1, Util.round(value))
379363
elif port.lower() =='out2':
@@ -493,6 +477,20 @@ def check_color(self, port='in1', color='white'):
493477
else:
494478
raise TypeError
495479

480+
def _write_to_output_port(self, port, out_val):
481+
if port.lower() == 'out1':
482+
self.write(Neosoco.OUTPUT_1, out_val)
483+
elif port.lower() == 'out2':
484+
self.write(Neosoco.OUTPUT_2, out_val)
485+
elif port.lower() == 'out3':
486+
self.write(Neosoco.OUTPUT_3, out_val)
487+
elif port.lower() == 'all':
488+
self.write(Neosoco.OUTPUT_1, out_val)
489+
self.write(Neosoco.OUTPUT_2, out_val)
490+
self.write(Neosoco.OUTPUT_3, out_val)
491+
else:
492+
raise ValueError('Wrong value of out port')
493+
496494
def led_on(self, port='out1', brightness='100'):
497495
percent_cvt = {
498496
'100': 255,
@@ -510,43 +508,22 @@ def led_on(self, port='out1', brightness='100'):
510508
cvt_val = percent_cvt[brightness]
511509
else:
512510
raise ValueError('Wrong value of percentage')
511+
513512
if isinstance(port, str):
514-
if port.lower() =='out1':
515-
self.write(Neosoco.OUTPUT_1, cvt_val)
516-
elif port.lower() =='out2':
517-
self.write(Neosoco.OUTPUT_2, cvt_val)
518-
elif port.lower() =='out3':
519-
self.write(Neosoco.OUTPUT_3, cvt_val)
520-
elif port.lower() =='all':
521-
self.write(Neosoco.OUTPUT_1, cvt_val)
522-
self.write(Neosoco.OUTPUT_2, cvt_val)
523-
self.write(Neosoco.OUTPUT_3, cvt_val)
524-
else:
525-
raise ValueError('Wrong value of port')
513+
self._write_to_output_port(port, cvt_val)
526514
else:
527515
raise TypeError
528516

529517
def led_by_port(self, in_port='in1', out_port='out1'):
530-
if isinstance(out_port, str):
518+
if isinstance(in_port and out_port, str):
531519
in_value = self._convert_scale_from_input_port(in_port, 255)
532520
self._write_to_output_port(out_port, in_value)
533521
else:
534522
raise TypeError
535523

536524
def led_off(self, port='out1'):
537525
if isinstance(port, str):
538-
if port.lower() =='out1':
539-
self.write(Neosoco.OUTPUT_1, 0)
540-
elif port.lower() =='out2':
541-
self.write(Neosoco.OUTPUT_2, 0)
542-
elif port.lower() =='out3':
543-
self.write(Neosoco.OUTPUT_3, 0)
544-
elif port.lower() =='all':
545-
self.write(Neosoco.OUTPUT_1, 0)
546-
self.write(Neosoco.OUTPUT_2, 0)
547-
self.write(Neosoco.OUTPUT_3, 0)
548-
else:
549-
raise ValueError('Wrong value of port')
526+
self._write_to_output_port(port, 0)
550527
else:
551528
raise TypeError
552529

@@ -647,20 +624,6 @@ def motor_rotate(self, motor='both', direction='forward', speed='100'):
647624
else:
648625
raise TypeError
649626

650-
def _write_to_output_port(self, port, out_val):
651-
if port.lower() == 'out1':
652-
self.write(Neosoco.OUTPUT_1, out_val)
653-
elif port.lower() == 'out2':
654-
self.write(Neosoco.OUTPUT_2, out_val)
655-
elif port.lower() == 'out3':
656-
self.write(Neosoco.OUTPUT_3, out_val)
657-
elif port.lower() == 'all':
658-
self.write(Neosoco.OUTPUT_1, out_val)
659-
self.write(Neosoco.OUTPUT_2, out_val)
660-
self.write(Neosoco.OUTPUT_3, out_val)
661-
else:
662-
raise ValueError('Wrong value of out port')
663-
664627
def servo_rotate(self, port='out1', direction='forward', speed='100'):
665628
if isinstance(port, str) and isinstance(direction, str) and isinstance(speed, str):
666629
if speed == 'in1' or speed == 'in2' or speed == 'in3' :
@@ -789,7 +752,6 @@ def buzzer_by_port(self, port='in1'):
789752
value = self._convert_scale_from_input_port(port, 65)
790753
self.write(Neosoco.NOTE, value)
791754
else:
792-
793755
raise TypeError
794756

795757
def buzzer_stop(self):

test.py

+34-44
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# n.set_value('out1', 255)
1212
# wait(1000)
1313

14+
1415
## LED, distance sensor
1516
# case3) Turn on LED when the distance is under 10cm
1617
# while True:
@@ -19,15 +20,20 @@
1920
# else:
2021
# n.led_off('out1')
2122

23+
# case4) Control LED's brightness with sensor
24+
# while True:
25+
# n.led_by_port('in1', 'out1')
26+
27+
2228
## Motors
23-
# case4) Move forth and back during 1s and stop
29+
# case5) Move forth and back during 1s and stop
2430
# n.motor_move('forward')
2531
# wait(500)
2632
# n.motor_move('backward')
2733
# wait(500)
2834
# n.motor_move('stop')
2935

30-
# case5) Moving control by direction keys on the keyboard
36+
# case6) Moving control by direction keys on the keyboard
3137
# while True:
3238
# key = Keyboard.read()
3339

@@ -42,15 +48,16 @@
4248
# elif key == ' ':
4349
# n.motor_move('stop')
4450

45-
# case6) Move forth and back with speed 30% during 1s and stop
51+
# case7) Move forth and back with speed 30% during 1s and stop
4652
# n.motor_rotate('both', 'forward', '30')
4753
# wait(500)
4854
# n.motor_rotate('both', 'backward', '30')
4955
# wait(500)
5056
# n.motor_stop('both')
5157

52-
## Buzzer
53-
# case7) Play same note by pitch, sharp and flat, and a length of note
58+
59+
## Buzzer, Distance sensor
60+
# case8) Play same note by pitch, sharp and flat, and a length of note
5461
# n.buzzer('3', n.NOTE_NAME_C)
5562
# n.buzzer('3', 'c')
5663

@@ -60,14 +67,13 @@
6067
# n.buzzer('5', n.NOTE_NAME_D_FLAT, '16')
6168
# n.buzzer('5', 'db', '16')
6269

63-
# case8) Play a sound by value from input port
70+
# case9) Play a sound by value from input port and Turn off buzzer if the distance is under 10cm
6471
# while True:
65-
# n.buzzer_by_port('in1')
66-
67-
## Color LED, distance sensor
68-
# case9) Color LED on with variable color by input port
69-
# r = g = b = n.convert_scale('in1', 0, 255, 85, 170) # Limit to middle brightness
70-
# color_led_on('out1', r, g, b)
72+
# if n.get_value('in1') < 10:
73+
# n.buzzer_stop()
74+
# else:
75+
# n.buzzer_by_port('in1')
76+
7177

7278
## LED, Angle sensor
7379
# case10) # Turn on LED when a degree of the angle sensor is under 90 degrees
@@ -77,6 +83,7 @@
7783
# else:
7884
# n.led_off('out1')
7985

86+
8087
# Servo Motor
8188
# case 11) Rotate servo motor forth and back with speed 50% during 5s and stop
8289
# n.servo_rotate('out2', 'forward', '50')
@@ -85,47 +92,30 @@
8592
# wait(1000)
8693
# n.servo_rotate('out2', 'backward', '50')
8794
# wait(2000)
88-
# n.servo_rotate('out2', 'forward', '0')
95+
# n.servo_stop('out2')
8996
# wait(1000)
9097

9198
# case 12) Rotate servo motor forward by 120 degrees at 50% speed within 3 seconds
9299
# n.servo_reset_degree('out1')
93100
# n.servo_rotate_by_degree('out1', 'forward', '50', '120')
94101
# wait(3000)
95102

96-
# case 13) When the 1 button of remote controller is pressed, turn on the LED
97-
while True:
98-
if n.remote_button('1'):
99-
n.led_on() # By default value
100-
else:
101-
n.led_off()
102103

103-
# case 14) Control LED's brightness with sensor
104+
## Remote Controller
105+
# case 13) When the 1 button of remote controller is pressed, turn on the LED
104106
# while True:
105-
# n.led_by_port('out1', 'in1')
107+
# if n.remote_button('1'):
108+
# n.led_on() # By default value
109+
# else:
110+
# n.led_off()
106111

107-
# case 15) Turn off buzzer when the distance is under 10cm
108-
# while True:
109-
# if n.get_value('in1') < 10:
110-
# n.buzzer_stop()
111-
# else:
112-
# n.buzzer('2', 'c', '4')
113112

114-
# case 16) Turn off servo motor when the distance is under 10cm
115-
# while True:
116-
# if n.get_value('in1') < 10:
117-
# n.servo_stop('out1')
118-
# else:
119-
# n.servo_rotate('out2', 'forward', '50')
120-
# wait(2000)
121-
# n.servo_rotate('out2', 'backward', '50')
122-
# wait(2000)
123-
124-
# case 17) Turn on LED when sensor detects green color
113+
## Color LED / sensor
114+
# case 14) Turn on LED when sensor detects green color
125115
# while True:
126-
# print(n.get_value('in1'))
127-
# wait(500)
128-
# if n.check_color('in1', 'Green'):
129-
# n.led_on('out1', '100')
130-
# else:
131-
# n.led_off('out1')
116+
# print(n.get_value('in1'))
117+
# wait(500)
118+
# if n.check_color('in1', 'Green'):
119+
# n.led_on('out1', '100')
120+
# else:
121+
# n.led_off('out1')

0 commit comments

Comments
 (0)