@@ -400,7 +400,7 @@ def convert_scale(self, port='in1', omin=0, omax=255, tmin=0, tmax=100):
400
400
raise TypeError
401
401
402
402
def led_on (self , port = 'out1' , brightness = '100' ):
403
- cvt_dic = {
403
+ percent_cvt = {
404
404
'100' : 255 ,
405
405
'90' : 230 ,
406
406
'80' : 204 ,
@@ -412,8 +412,8 @@ def led_on(self, port='out1', brightness='100'):
412
412
'20' : 51 ,
413
413
'10' : 26
414
414
}
415
- if brightness in cvt_dic .keys ():
416
- cvt_val = cvt_dic [brightness ]
415
+ if brightness in percent_cvt .keys ():
416
+ cvt_val = percent_cvt [brightness ]
417
417
else :
418
418
raise ValueError ('Wrong value of percentage' )
419
419
if isinstance (port , str ):
@@ -451,18 +451,19 @@ def led_off(self, port='port1'):
451
451
452
452
def motor_move (self , direction = 'forward' ):
453
453
if isinstance (direction , str ):
454
+ speed = self ._MOTOR_PERCENT_CVT ['60' ]
454
455
if direction .lower () == 'forward' :
455
- self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['forward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
456
- self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['forward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
456
+ self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['forward' ]+ speed )
457
+ self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['forward' ]+ speed )
457
458
elif direction .lower () == 'backward' :
458
- self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['backward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
459
- self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['backward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
459
+ self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['backward' ]+ speed )
460
+ self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['backward' ]+ speed )
460
461
elif direction .lower () == 'left' :
461
- self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['backward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
462
- self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['forward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
462
+ self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['backward' ]+ speed )
463
+ self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['forward' ]+ speed )
463
464
elif direction .lower () == 'right' :
464
- self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['forward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
465
- self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['backward' ]+ self . _MOTOR_PERCENT_CVT [ '60' ] )
465
+ self .write (Neosoco .LEFT_MOTOR , self ._MOTOR_DIR ['forward' ]+ speed )
466
+ self .write (Neosoco .RIGHT_MOTOR , self ._MOTOR_DIR ['backward' ]+ speed )
466
467
elif direction .lower () == 'stop' :
467
468
self .write (Neosoco .LEFT_MOTOR , 0 )
468
469
self .write (Neosoco .RIGHT_MOTOR , 0 )
@@ -472,6 +473,59 @@ def motor_move(self, direction='forward'):
472
473
raise TypeError
473
474
Runner .wait (100 ) # Since broadcast from controller is per 100ms
474
475
476
+ def _convert_input_port_scale (self , port , limit_val ):
477
+ if port .lower () == 'in1' :
478
+ value = self .read (Neosoco .INPUT_1 )
479
+ elif port .lower () == 'in2' :
480
+ value = self .read (Neosoco .INPUT_2 )
481
+ elif port .lower () == 'in3' :
482
+ value = self .read (Neosoco .INPUT_3 )
483
+ else :
484
+ raise ValueError ('Wrong value of port' )
485
+
486
+ if value :
487
+ value = max (value , 0 )
488
+ value = min (value , 100 )
489
+ value = math .ceil (value / 100 * limit_val )
490
+ return value
491
+
492
+ def motor_rotate (self , motor = 'both' , direction = 'forward' , speed = '100' ):
493
+ if isinstance (motor , str ) and isinstance (direction , str ) and isinstance (speed , str ):
494
+ if speed == 'in1' or speed == 'in2' or speed == 'in3' :
495
+ # Map to 0~15 from 0~100(max), it's same as Entry
496
+ speed = self ._convert_input_port_scale (speed , 15 )
497
+ elif speed in self ._MOTOR_PERCENT_CVT .keys ():
498
+ speed = self ._MOTOR_PERCENT_CVT [speed ]
499
+ else :
500
+ raise ValueError ('Wrong value of speed' )
501
+
502
+ if direction .lower () == 'forward' :
503
+ l_direction = self ._MOTOR_DIR ['forward' ]
504
+ r_direction = self ._MOTOR_DIR ['forward' ]
505
+ elif direction .lower () == 'backward' :
506
+ l_direction = self ._MOTOR_DIR ['backward' ]
507
+ r_direction = self ._MOTOR_DIR ['backward' ]
508
+ elif direction .lower () == 'left' :
509
+ l_direction = self ._MOTOR_DIR ['backward' ]
510
+ r_direction = self ._MOTOR_DIR ['forward' ]
511
+ elif direction .lower () == 'right' :
512
+ l_direction = self ._MOTOR_DIR ['forward' ]
513
+ r_direction = self ._MOTOR_DIR ['backward' ]
514
+ else :
515
+ raise ValueError ('Wrong value of direction' )
516
+
517
+ if motor .lower () == 'both' :
518
+ self .write (Neosoco .LEFT_MOTOR , l_direction + speed )
519
+ self .write (Neosoco .RIGHT_MOTOR , r_direction + speed )
520
+ elif motor .lower () == 'left' :
521
+ self .write (Neosoco .LEFT_MOTOR , l_direction + speed )
522
+ elif motor .lower () == 'right' :
523
+ self .write (Neosoco .RIGHT_MOTOR , r_direction + speed )
524
+ else :
525
+ raise ValueError ('Wrong value of motor' )
526
+ else :
527
+ raise TypeError
528
+
475
529
def buzzer (self , pitch = '3' , note = 'c' , beats = '4' ):
476
530
self .write (Neosoco .NOTE , 0 ) # init
477
531
if not isinstance (pitch , str ) or not (int (pitch ) >= 1 and int (pitch ) <= 6 ):
@@ -507,21 +561,9 @@ def buzzer(self, pitch='3', note='c', beats='4'):
507
561
508
562
def buzzer_by_port (self , port = 'in1' ):
509
563
if isinstance (port , str ):
510
- if port .lower () == 'in1' :
511
- value = self .read (Neosoco .INPUT_1 )
512
- elif port .lower () == 'in2' :
513
- value = self .read (Neosoco .INPUT_2 )
514
- elif port .lower () == 'in3' :
515
- value = self .read (Neosoco .INPUT_3 )
516
- else :
517
- raise ValueError ('Wrong value of port' )
518
-
519
- if value :
520
- # Map to 0~65 from 0~100, it's same as Entry
521
- value = max (value , 0 )
522
- value = min (value , 100 )
523
- value = math .ceil (value / 100 * 65 )
524
- self .write (Neosoco .NOTE , value )
564
+ # Map to 0~65 from 0~100(max), it's same as Entry
565
+ value = self ._convert_input_port_scale (port , 65 )
566
+ self .write (Neosoco .NOTE , value )
525
567
else :
526
568
raise TypeError
527
569
0 commit comments