1
+ /**
2
+ * Define Ruby code generator for LEGO_EV3 Blocks
3
+ * @param {RubyGenerator } Generator The RubyGenerator
4
+ * @return {RubyGenerator } same as param.
5
+ */
6
+ export default function ( Generator ) {
7
+ const Ev3SensorMenu = [ '1' , '2' , '3' , '4' ] ;
8
+ const Ev3MotorMenu = [ 'A' , 'B' , 'C' , 'D' ] ;
9
+
10
+ Generator . ev3_menu_sensorPorts = function ( block ) {
11
+ const index = Generator . getFieldValue ( block , 'sensorPorts' ) || 0 ;
12
+ const port = Generator . quote_ ( Ev3SensorMenu [ index ] ) ;
13
+ return [ port , Generator . ORDER_ATOMIC ] ;
14
+ } ;
15
+
16
+ Generator . ev3_whenButtonPressed = function ( block ) {
17
+ block . isStatement = true ;
18
+ const port = Generator . valueToCode ( block , 'PORT' , Generator . ORDER_NONE ) || null ;
19
+ return `${ Generator . spriteName ( ) } .when(:ev3_button_pressed, ${ port } ) do\n` ;
20
+ } ;
21
+
22
+ Generator . ev3_menu_motorPorts = function ( block ) {
23
+ const index = Generator . getFieldValue ( block , 'motorPorts' ) || 0 ;
24
+ const port = Generator . quote_ ( Ev3MotorMenu [ index ] ) ;
25
+ return [ port , Generator . ORDER_ATOMIC ] ;
26
+ } ;
27
+
28
+ Generator . ev3_motorSetPower = function ( block ) {
29
+ const port = Generator . valueToCode ( block , 'PORT' , Generator . ORDER_NONE ) || null ;
30
+ const power = Generator . valueToCode ( block , 'POWER' , Generator . ORDER_NONE ) || null ;
31
+ return `ev3_motor_set_power(${ port } , ${ power } )\n` ;
32
+ } ;
33
+
34
+ Generator . ev3_motorTurnClockwise = function ( block ) {
35
+ const port = Generator . valueToCode ( block , 'PORT' , Generator . ORDER_NONE ) || null ;
36
+ const time = Generator . valueToCode ( block , 'TIME' , Generator . ORDER_NONE ) || null ;
37
+ return `ev3_motor_turn_this_way_for(${ port } , ${ time } )\n` ;
38
+ } ;
39
+
40
+ Generator . ev3_motorTurnCounterClockwise = function ( block ) {
41
+ const port = Generator . valueToCode ( block , 'PORT' , Generator . ORDER_NONE ) || null ;
42
+ const time = Generator . valueToCode ( block , 'TIME' , Generator . ORDER_NONE ) || null ;
43
+ return `ev3_motor_turn_that_way_for(${ port } , ${ time } )\n` ;
44
+ } ;
45
+
46
+ Generator . ev3_getMotorPosition = function ( block ) {
47
+ const order = Generator . ORDER_FUNCTION_CALL ;
48
+ const port = Generator . valueToCode ( block , 'PORT' , Generator . ORDER_NONE ) || null ;
49
+ return [ `ev3_motor_position(${ port } )` , order ] ;
50
+ } ;
51
+
52
+ Generator . ev3_whenDistanceLessThan = function ( block ) {
53
+ block . isStatement = true ;
54
+ const distance = Generator . valueToCode ( block , 'DISTANCE' , Generator . ORDER_NONE ) || null ;
55
+ return `${ Generator . spriteName ( ) } .when(:ev3_distance_gt, ${ distance } ) do\n` ;
56
+ } ;
57
+
58
+ Generator . ev3_whenBrightnessLessThan = function ( block ) {
59
+ block . isStatement = true ;
60
+ const distance = Generator . valueToCode ( block , 'DISTANCE' , Generator . ORDER_NONE ) || null ;
61
+ return `${ Generator . spriteName ( ) } .when(:ev3_brightness_gt, ${ distance } ) do\n` ;
62
+ } ;
63
+
64
+ Generator . ev3_buttonPressed = function ( block ) {
65
+ const order = Generator . ORDER_FUNCTION_CALL ;
66
+ const port = Generator . valueToCode ( block , 'PORT' , order ) || null ;
67
+ return [ `ev3_button_pressed?(${ port } )` , order ] ;
68
+ } ;
69
+
70
+ Generator . ev3_getDistance = function ( ) {
71
+ return [ 'ev3_distance' , Generator . ORDER_ATOMIC ] ;
72
+ } ;
73
+
74
+ Generator . ev3_getBrightness = function ( ) {
75
+ return [ 'ev3_brightness' , Generator . ORDER_ATOMIC ] ;
76
+ } ;
77
+
78
+ Generator . ev3_beep = function ( block ) {
79
+ const note = Generator . valueToCode ( block , 'NOTE' , Generator . ORDER_NONE ) || null ;
80
+ const time = Generator . valueToCode ( block , 'TIME' , Generator . ORDER_NONE ) || null ;
81
+ return `ev3_beep_note(${ note } , ${ time } )\n` ;
82
+ } ;
83
+
84
+ return Generator ;
85
+ }
0 commit comments