@@ -39,37 +39,18 @@ class Neosoco(Robot):
39
39
RIGHT_MOTOR = 0x00400009
40
40
NOTE = 0x0040000a
41
41
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"
54
43
COLOR_NAME_RED = "red"
55
44
COLOR_NAME_YELLOW = "yellow"
56
45
COLOR_NAME_GREEN = "green"
57
- COLOR_NAME_SKY_BLUE = "sky blue"
58
46
COLOR_NAME_BLUE = "blue"
59
- COLOR_NAME_PURPLE = "purple"
60
- COLOR_NAME_WHITE = "white"
61
47
62
48
_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
73
54
}
74
55
75
56
_NOTE_OFF = 0
@@ -374,6 +355,9 @@ def _notify_motoring_device_data_changed(self):
374
355
375
356
def set_value (self , port = 'out1' , value = 255 ):
376
357
if isinstance (port , str ) and isinstance (value , int ):
358
+ if value < 0 or value > 255 :
359
+ raise ValueError ('Wrong value of input value' )
360
+
377
361
if port .lower () == 'out1' :
378
362
self .write (Neosoco .OUTPUT_1 , Util .round (value ))
379
363
elif port .lower () == 'out2' :
@@ -493,6 +477,20 @@ def check_color(self, port='in1', color='white'):
493
477
else :
494
478
raise TypeError
495
479
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
+
496
494
def led_on (self , port = 'out1' , brightness = '100' ):
497
495
percent_cvt = {
498
496
'100' : 255 ,
@@ -510,43 +508,22 @@ def led_on(self, port='out1', brightness='100'):
510
508
cvt_val = percent_cvt [brightness ]
511
509
else :
512
510
raise ValueError ('Wrong value of percentage' )
511
+
513
512
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 )
526
514
else :
527
515
raise TypeError
528
516
529
517
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 ):
531
519
in_value = self ._convert_scale_from_input_port (in_port , 255 )
532
520
self ._write_to_output_port (out_port , in_value )
533
521
else :
534
522
raise TypeError
535
523
536
524
def led_off (self , port = 'out1' ):
537
525
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 )
550
527
else :
551
528
raise TypeError
552
529
@@ -647,20 +624,6 @@ def motor_rotate(self, motor='both', direction='forward', speed='100'):
647
624
else :
648
625
raise TypeError
649
626
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
-
664
627
def servo_rotate (self , port = 'out1' , direction = 'forward' , speed = '100' ):
665
628
if isinstance (port , str ) and isinstance (direction , str ) and isinstance (speed , str ):
666
629
if speed == 'in1' or speed == 'in2' or speed == 'in3' :
@@ -789,7 +752,6 @@ def buzzer_by_port(self, port='in1'):
789
752
value = self ._convert_scale_from_input_port (port , 65 )
790
753
self .write (Neosoco .NOTE , value )
791
754
else :
792
-
793
755
raise TypeError
794
756
795
757
def buzzer_stop (self ):
0 commit comments