Skip to content

Commit ea52689

Browse files
committed
Expose possible property values as class attributes
Unfortunately, python does not allow to add docstrings to class attributes (there was a proposal for this which was rejected: https://www.python.org/dev/peps/pep-0224/). That's why the values descriptions are implemented simply as comments. See discussion in f276d42.
1 parent a906045 commit ea52689

File tree

2 files changed

+183
-93
lines changed

2 files changed

+183
-93
lines changed

ev3dev.py

Lines changed: 176 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -502,37 +502,71 @@ def time_sp(self, value):
502502
#~autogen
503503
#~autogen python_generic-property-value classes.motor>currentClass
504504

505+
# Run the motor until another command is sent.
506+
command_run_forever = 'run-forever'
507+
508+
# Run to an absolute position specified by `position_sp` and then
509+
# stop using the command specified in `stop_command`.
510+
command_run_to_abs_pos = 'run-to-abs-pos'
511+
512+
# Run to a position relative to the current `position` value.
513+
# The new position will be current `position` + `position_sp`.
514+
# When the new position is reached, the motor will stop using
515+
# the command specified by `stop_command`.
516+
command_run_to_rel_pos = 'run-to-rel-pos'
517+
518+
# Run the motor for the amount of time specified in `time_sp`
519+
# and then stop the motor using the command specified by `stop_command`.
520+
command_run_timed = 'run-timed'
521+
522+
# Run the motor at the duty cycle specified by `duty_cycle_sp`.
523+
# Unlike other run commands, changing `duty_cycle_sp` while running *will*
524+
# take effect immediately.
525+
command_run_direct = 'run-direct'
526+
527+
# Stop any of the run commands before they are complete using the
528+
# command specified by `stop_command`.
529+
command_stop = 'stop'
530+
531+
# Reset all of the motor parameter attributes to their default value.
532+
# This will also have the effect of stopping the motor.
533+
command_reset = 'reset'
534+
535+
# Sets the normal polarity of the rotary encoder.
536+
encoder_polarity_normal = 'normal'
537+
538+
# Sets the inversed polarity of the rotary encoder.
539+
encoder_polarity_inversed = 'inversed'
540+
541+
# With `normal` polarity, a positive duty cycle will
542+
# cause the motor to rotate clockwise.
543+
polarity_normal = 'normal'
544+
545+
# With `inversed` polarity, a positive duty cycle will
546+
# cause the motor to rotate counter-clockwise.
547+
polarity_inversed = 'inversed'
548+
549+
# The motor controller will vary the power supplied to the motor
550+
# to try to maintain the speed specified in `speed_sp`.
551+
speed_regulation_on = 'on'
552+
553+
# The motor controller will use the power specified in `duty_cycle_sp`.
554+
speed_regulation_off = 'off'
555+
556+
# Power will be removed from the motor and it will freely coast to a stop.
557+
stop_command_coast = 'coast'
558+
559+
# Power will be removed from the motor and a passive electrical load will
560+
# be placed on the motor. This is usually done by shorting the motor terminals
561+
# together. This load will absorb the energy from the rotation of the motors and
562+
# cause the motor to stop more quickly than coasting.
563+
stop_command_brake = 'brake'
564+
565+
# Does not remove power from the motor. Instead it actively try to hold the motor
566+
# at the current position. If an external force tries to turn the motor, the motor
567+
# will ``push back`` to maintain its position.
568+
stop_command_hold = 'hold'
505569

506-
_propval_command = {
507-
'run-forever':'Run the motor until another command is sent.' ,
508-
'run-to-abs-pos':'Run to an absolute position specified by `position_sp` and thenstop using the command specified in `stop_command`.' ,
509-
'run-to-rel-pos':'Run to a position relative to the current `position` value.The new position will be current `position` + `position_sp`.When the new position is reached, the motor will stop usingthe command specified by `stop_command`.' ,
510-
'run-timed':'Run the motor for the amount of time specified in `time_sp`and then stop the motor using the command specified by `stop_command`.' ,
511-
'run-direct':'Run the motor at the duty cycle specified by `duty_cycle_sp`.Unlike other run commands, changing `duty_cycle_sp` while running *will*take effect immediately.' ,
512-
'stop':'Stop any of the run commands before they are complete using thecommand specified by `stop_command`.' ,
513-
'reset':'Reset all of the motor parameter attributes to their default value.This will also have the effect of stopping the motor.' ,
514-
}
515-
516-
_propval_encoder_polarity = {
517-
'normal':'Sets the normal polarity of the rotary encoder.' ,
518-
'inversed':'Sets the inversed polarity of the rotary encoder.' ,
519-
}
520-
521-
_propval_polarity = {
522-
'normal':'With `normal` polarity, a positive duty cycle willcause the motor to rotate clockwise.' ,
523-
'inversed':'With `inversed` polarity, a positive duty cycle willcause the motor to rotate counter-clockwise.' ,
524-
}
525-
526-
_propval_speed_regulation = {
527-
'on':'The motor controller will vary the power supplied to the motorto try to maintain the speed specified in `speed_sp`.' ,
528-
'off':'The motor controller will use the power specified in `duty_cycle_sp`.' ,
529-
}
530-
531-
_propval_stop_command = {
532-
'coast':'Power will be removed from the motor and it will freely coast to a stop.' ,
533-
'brake':'Power will be removed from the motor and a passive electrical load willbe placed on the motor. This is usually done by shorting the motor terminalstogether. This load will absorb the energy from the rotation of the motors andcause the motor to stop more quickly than coasting.' ,
534-
'hold':'Does not remove power from the motor. Instead it actively try to hold the motorat the current position. If an external force tries to turn the motor, the motorwill ``push back`` to maintain its position.' ,
535-
}
536570

537571
#~autogen
538572
#~autogen python_motor_commands classes.motor>currentClass
@@ -795,23 +829,39 @@ def time_sp(self, value):
795829
#~autogen
796830
#~autogen python_generic-property-value classes.dcMotor>currentClass
797831

832+
# Run the motor until another command is sent.
833+
command_run_forever = 'run-forever'
834+
835+
# Run the motor for the amount of time specified in `time_sp`
836+
# and then stop the motor using the command specified by `stop_command`.
837+
command_run_timed = 'run-timed'
838+
839+
# Run the motor at the duty cycle specified by `duty_cycle_sp`.
840+
# Unlike other run commands, changing `duty_cycle_sp` while running *will*
841+
# take effect immediately.
842+
command_run_direct = 'run-direct'
843+
844+
# Stop any of the run commands before they are complete using the
845+
# command specified by `stop_command`.
846+
command_stop = 'stop'
847+
848+
# With `normal` polarity, a positive duty cycle will
849+
# cause the motor to rotate clockwise.
850+
polarity_normal = 'normal'
851+
852+
# With `inversed` polarity, a positive duty cycle will
853+
# cause the motor to rotate counter-clockwise.
854+
polarity_inversed = 'inversed'
798855

799-
_propval_command = {
800-
'run-forever':'Run the motor until another command is sent.' ,
801-
'run-timed':'Run the motor for the amount of time specified in `time_sp`and then stop the motor using the command specified by `stop_command`.' ,
802-
'run-direct':'Run the motor at the duty cycle specified by `duty_cycle_sp`.Unlike other run commands, changing `duty_cycle_sp` while running *will*take effect immediately.' ,
803-
'stop':'Stop any of the run commands before they are complete using thecommand specified by `stop_command`.' ,
804-
}
856+
# Power will be removed from the motor and it will freely coast to a stop.
857+
stop_command_coast = 'coast'
805858

806-
_propval_polarity = {
807-
'normal':'With `normal` polarity, a positive duty cycle willcause the motor to rotate clockwise.' ,
808-
'inversed':'With `inversed` polarity, a positive duty cycle willcause the motor to rotate counter-clockwise.' ,
809-
}
859+
# Power will be removed from the motor and a passive electrical load will
860+
# be placed on the motor. This is usually done by shorting the motor terminals
861+
# together. This load will absorb the energy from the rotation of the motors and
862+
# cause the motor to stop more quickly than coasting.
863+
stop_command_brake = 'brake'
810864

811-
_propval_stop_command = {
812-
'coast':'Power will be removed from the motor and it will freely coast to a stop.' ,
813-
'brake':'Power will be removed from the motor and a passive electrical load willbe placed on the motor. This is usually done by shorting the motor terminalstogether. This load will absorb the energy from the rotation of the motors andcause the motor to stop more quickly than coasting.' ,
814-
}
815865

816866
#~autogen
817867

@@ -1002,16 +1052,20 @@ def state(self):
10021052
#~autogen
10031053
#~autogen python_generic-property-value classes.servoMotor>currentClass
10041054

1055+
# Drive servo to the position set in the `position_sp` attribute.
1056+
command_run = 'run'
10051057

1006-
_propval_command = {
1007-
'run':'Drive servo to the position set in the `position_sp` attribute.' ,
1008-
'float':'Remove power from the motor.' ,
1009-
}
1058+
# Remove power from the motor.
1059+
command_float = 'float'
1060+
1061+
# With `normal` polarity, a positive duty cycle will
1062+
# cause the motor to rotate clockwise.
1063+
polarity_normal = 'normal'
1064+
1065+
# With `inversed` polarity, a positive duty cycle will
1066+
# cause the motor to rotate counter-clockwise.
1067+
polarity_inversed = 'inversed'
10101068

1011-
_propval_polarity = {
1012-
'normal':'With `normal` polarity, a positive duty cycle willcause the motor to rotate clockwise.' ,
1013-
'inversed':'With `inversed` polarity, a positive duty cycle willcause the motor to rotate counter-clockwise.' ,
1014-
}
10151069

10161070
#~autogen
10171071

@@ -1225,14 +1279,21 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
12251279
#~autogen
12261280
#~autogen python_generic-property-value classes.colorSensor>currentClass
12271281

1282+
# Reflected light. Red LED on.
1283+
mode_col_reflect = 'COL-REFLECT'
1284+
1285+
# Ambient light. Red LEDs off.
1286+
mode_col_ambient = 'COL-AMBIENT'
1287+
1288+
# Color. All LEDs rapidly cycling, appears white.
1289+
mode_col_color = 'COL-COLOR'
1290+
1291+
# Raw reflected. Red LED on
1292+
mode_ref_raw = 'REF-RAW'
1293+
1294+
# Raw Color Components. All LEDs rapidly cycling, appears white.
1295+
mode_rgb_raw = 'RGB-RAW'
12281296

1229-
_propval_mode = {
1230-
'COL-REFLECT':'Reflected light. Red LED on.' ,
1231-
'COL-AMBIENT':'Ambient light. Red LEDs off.' ,
1232-
'COL-COLOR':'Color. All LEDs rapidly cycling, appears white.' ,
1233-
'REF-RAW':'Raw reflected. Red LED on' ,
1234-
'RGB-RAW':'Raw Color Components. All LEDs rapidly cycling, appears white.' ,
1235-
}
12361297

12371298
#~autogen
12381299
#~autogen python_generic-class classes.ultrasonicSensor>currentClass
@@ -1255,14 +1316,25 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
12551316
#~autogen
12561317
#~autogen python_generic-property-value classes.ultrasonicSensor>currentClass
12571318

1319+
# Continuous measurement in centimeters.
1320+
# LEDs: On, steady
1321+
mode_us_dist_cm = 'US-DIST-CM'
1322+
1323+
# Continuous measurement in inches.
1324+
# LEDs: On, steady
1325+
mode_us_dist_in = 'US-DIST-IN'
1326+
1327+
# Listen. LEDs: On, blinking
1328+
mode_us_listen = 'US-LISTEN'
1329+
1330+
# Single measurement in centimeters.
1331+
# LEDs: On momentarily when mode is set, then off
1332+
mode_us_si_cm = 'US-SI-CM'
1333+
1334+
# Single measurement in inches.
1335+
# LEDs: On momentarily when mode is set, then off
1336+
mode_us_si_in = 'US-SI-IN'
12581337

1259-
_propval_mode = {
1260-
'US-DIST-CM':'Continuous measurement in centimeters.LEDs: On, steady' ,
1261-
'US-DIST-IN':'Continuous measurement in inches.LEDs: On, steady' ,
1262-
'US-LISTEN':'Listen. LEDs: On, blinking' ,
1263-
'US-SI-CM':'Single measurement in centimeters.LEDs: On momentarily when mode is set, then off' ,
1264-
'US-SI-IN':'Single measurement in inches.LEDs: On momentarily when mode is set, then off' ,
1265-
}
12661338

12671339
#~autogen
12681340
#~autogen python_generic-class classes.gyroSensor>currentClass
@@ -1285,14 +1357,21 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
12851357
#~autogen
12861358
#~autogen python_generic-property-value classes.gyroSensor>currentClass
12871359

1360+
# Angle
1361+
mode_gyro_ang = 'GYRO-ANG'
1362+
1363+
# Rotational speed
1364+
mode_gyro_rate = 'GYRO-RATE'
1365+
1366+
# Raw sensor value
1367+
mode_gyro_fas = 'GYRO-FAS'
1368+
1369+
# Angle and rotational speed
1370+
mode_gyro_g_a = 'GYRO-G&A'
1371+
1372+
# Calibration ???
1373+
mode_gyro_cal = 'GYRO-CAL'
12881374

1289-
_propval_mode = {
1290-
'GYRO-ANG':'Angle' ,
1291-
'GYRO-RATE':'Rotational speed' ,
1292-
'GYRO-FAS':'Raw sensor value' ,
1293-
'GYRO-G&A':'Angle and rotational speed' ,
1294-
'GYRO-CAL':'Calibration ???' ,
1295-
}
12961375

12971376
#~autogen
12981377
#~autogen python_generic-class classes.infraredSensor>currentClass
@@ -1315,14 +1394,21 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
13151394
#~autogen
13161395
#~autogen python_generic-property-value classes.infraredSensor>currentClass
13171396

1397+
# Proximity
1398+
mode_ir_prox = 'IR-PROX'
1399+
1400+
# IR Seeker
1401+
mode_ir_seek = 'IR-SEEK'
1402+
1403+
# IR Remote Control
1404+
mode_ir_remote = 'IR-REMOTE'
1405+
1406+
# IR Remote Control. State of the buttons is coded in binary
1407+
mode_ir_rem_a = 'IR-REM-A'
1408+
1409+
# Calibration ???
1410+
mode_ir_cal = 'IR-CAL'
13181411

1319-
_propval_mode = {
1320-
'IR-PROX':'Proximity' ,
1321-
'IR-SEEK':'IR Seeker' ,
1322-
'IR-REMOTE':'IR Remote Control' ,
1323-
'IR-REM-A':'IR Remote Control. State of the buttons is coded in binary' ,
1324-
'IR-CAL':'Calibration ???' ,
1325-
}
13261412

13271413
#~autogen
13281414

@@ -1347,11 +1433,12 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
13471433
#~autogen
13481434
#~autogen python_generic-property-value classes.soundSensor>currentClass
13491435

1436+
# Sound pressure level. Flat weighting
1437+
mode_db = 'DB'
1438+
1439+
# Sound pressure level. A weighting
1440+
mode_dba = 'DBA'
13501441

1351-
_propval_mode = {
1352-
'DB':'Sound pressure level. Flat weighting' ,
1353-
'DBA':'Sound pressure level. A weighting' ,
1354-
}
13551442

13561443
#~autogen
13571444
#~autogen python_generic-class classes.lightSensor>currentClass
@@ -1374,11 +1461,12 @@ def __init__(self, port=None, name=SYSTEM_DEVICE_NAME_CONVENTION, **kwargs ):
13741461
#~autogen
13751462
#~autogen python_generic-property-value classes.lightSensor>currentClass
13761463

1464+
# Reflected light. LED on
1465+
mode_reflect = 'REFLECT'
1466+
1467+
# Ambient light. LED off
1468+
mode_ambient = 'AMBIENT'
13771469

1378-
_propval_mode = {
1379-
'REFLECT':'Reflected light. LED on' ,
1380-
'AMBIENT':'Ambient light. LED off' ,
1381-
}
13821470

13831471
#~autogen
13841472
#~autogen python_generic-class classes.touchSensor>currentClass
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{% for prop in currentClass.propertyValues %}{%
22
assign className = currentClass.friendlyName | downcase | underscore_spaces %}{%
3-
assign propName = prop.propertyName | downcase | underscore_spaces %}
4-
5-
_propval_{{propName}} = {
6-
{% for value in prop.values %} '{{value.name}}':'{{value.description}}' ,
7-
{% endfor %} }{%
3+
assign propName = prop.propertyName | downcase | underscore_spaces %}{%
4+
for value in prop.values %}{%
5+
for line in value.description %}
6+
# {{ line }}{%
7+
endfor %}
8+
{{ propName }}_{{ value.name | downcase | underscore_non_wc }} = '{{ value.name }}'
9+
{% endfor %}{%
810
endfor %}

0 commit comments

Comments
 (0)