chao 1 mesiac pred
rodič
commit
c78fb6391a

+ 40 - 22
blockly/blocks/python/esp32.js

@@ -201,7 +201,15 @@ Blockly.Blocks['esp32_main_controller_io_from_digital_pin'] = {
                 ["MOSI", "PC2"],
                 ["MISO", "PC3"],
                 ["TXD", "PG6"],
-                ["RXD", "PG7"]
+                ["RXD", "PG7"],
+                ["P0", "21"],
+                ["P1", "22"],
+                ["P2", "23"],
+                ["P3", "24"],
+                ["A0", "25"],
+                ["A1", "26"],
+                ["A2", "27"],
+                ["A3", "28"]
                 // ["12", "12"]
             ]), "analog_pin");
         this.appendDummyInput()
@@ -259,19 +267,26 @@ sys.path.append("/root/")`
     os.system("echo PG6 5 > /sys/kernel/debug/sunxi_pinctrl/function")
     os.system("echo PG7 5 > /sys/kernel/debug/sunxi_pinctrl/function")
 `)
-        Blockly.Python.addVariable(`pinMode${value_name}`, `pinMode("${value_name}","0")`, true)
+        Blockly.Python.addVariable(`pinMode${value_name}`,`pinMode("${value_name}","0")`,true)
         code = `digitalRead("${value_name}")`
     }
-    else {
-        Blockly.Python.addFunction("convert",`def BooleraConvertNum(value):
-    if value:
+    else if(value_name == 7 || value_name == 8 || value_name == 13) {
+        Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON `;
+        Blockly.Python.addFunction("getBUTTONNum",`def getBUTTONNum(data):
+    if data:
         return 1
     else:
         return 0
 `)
-        Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON `;
         Blockly.Python.addVariable('_Math_gpio_' + value_name, `_Math_gpio_${value_name} = BUTTON(${value_name})`, true);
-        code = `BooleraConvertNum(_Math_gpio_${value_name}.is_pressed())`;
+        code = `getBUTTONNum(_Math_gpio_${value_name}.is_pressed())`;
+    }else{
+        let gpioNum = value_name - 21
+        Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
+        Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `InputGpio = extGpio()`;
+        Blockly.Python.addVariable('InputGpio' + value_name, `InputGpio.pinMode(${gpioNum},0)`, true);
+
+        code = `InputGpio.digitalRead(${gpioNum})`
     }
 
     // TODO: Change ORDER_NONE to the correct strength.
@@ -436,11 +451,15 @@ Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
                 ["MOSI", "PC2"],
                 ["MISO", "PC3"],
                 ["TXD", "PG6"],
-                ["RXD", "PG7"]
-                // ["3", "231"],
-                // ["4", "232"],
-                // ["5", "237"],
-                // ["6", "238"]
+                ["RXD", "PG7"],
+                ["P0", "10"],
+                ["P1", "11"],
+                ["P2", "12"],
+                ["P3", "13"],
+                ["A0", "14"],
+                ["A1", "15"],
+                ["A2", "16"],
+                ["A3", "17"]
             ]), "digital_set_type_gpio").appendField("");
         this.appendDummyInput()
             .appendField(Blockly.Msg.digital_set_as);
@@ -465,7 +484,7 @@ Blockly.Blocks['esp32_main_controller_io_set_digital_pin'] = {
 
 Blockly.Python['esp32_main_controller_io_set_digital_pin'] = function (block) {
     var high_low = Blockly.Python.valueToCode(block, 'high_low', Blockly.Python.ORDER_ATOMIC).slice(1, 2);
-    var timer_type = block.getFieldValue('digital_set_type');
+    // var timer_type = block.getFieldValue('digital_set_type');
     let value_name = block.getFieldValue('digital_set_type_gpio');
 
     Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
@@ -479,8 +498,8 @@ sys.path.append("/root/")`
         Blockly.Python.addVariable('MATHOUT' + value_name, `MATHOUT${value_name} = multiFuncGpio(${value_name - 1},3)`, true);
         code = `MATHOUT${value_name}.digitalWrite(${high_low})
 `
-    } else if (value_name == "PG6" || value_name == "PG7" || value_name == "PC0" || value_name == "PC1" || value_name == "PC2" || value_name == "PC3") {
-
+    } else if (value_name == "PG6" || value_name == "PG7" ||value_name == "PC0" || value_name == "PC1" || value_name == "PC2" || value_name == "PC3") {
+        
         Blockly.Python.addFunction("pinMode", `def pinMode(pin,mode):
     os.system(f"echo {pin} {mode} > /sys/kernel/debug/sunxi_pinctrl/function")
 `)
@@ -509,18 +528,17 @@ sys.path.append("/root/")`
     os.system("echo PG6 5 > /sys/kernel/debug/sunxi_pinctrl/function")
     os.system("echo PG7 5 > /sys/kernel/debug/sunxi_pinctrl/function")
 `)
-        Blockly.Python.addVariable(`pinModeOut(${value_name}`, `pinMode("${value_name}","1")`, false)
+        Blockly.Python.addVariable(`pinModeOut${value_name}`,`pinMode("${value_name}","1")`,false)
         code = `digitalWrite("${value_name}","${high_low}")
 `
     } else {
-        Blockly.Python.definitions_['v831_import_CocoPi_LED'] = `from CocoPi import LED`;
-        Blockly.Python.addVariable('MATHOUT' + value_name, `MATHOUT${value_name} = LED(${value_name})`, true);
-        code = `MATHOUT${value_name}.out(${high_low})
-`
+        let gpioNum = value_name - 10
+        Blockly.Python.definitions_['v831_import_CocoPi_extGpio'] = `from CocoPi import extGpio`;
+        Blockly.Python.definitions_['v831_import_CocoPi_extGpio_init'] = `extGpio = extGpio()`;
+        Blockly.Python.addVariable('ExtGpio' + value_name, `extGpio.pinMode(${gpioNum},1)`, true);
+        code = `extGpio.digitalWrite(${gpioNum},${high_low})\n`
     }
 
-
-
     return code;
 };
 

+ 53 - 51
blockly/blocks/python/extension_module.js

@@ -505,10 +505,10 @@ Blockly.Blocks['extension_servo_write_on_ai'] = {
             .appendField(new Blockly.FieldDropdown([
                 ["S1", "S1"],
                 ["S2", "S2"],
-                ["P0", "0"],
-                ["P1", "1"],
-                ["P2", "2"],
-                ["P3", "3"]
+                ["P0", "P0"],
+                ["P1", "P1"],
+                ["P2", "P2"],
+                ["P3", "P3"]
             ]), "gpio");
         this.appendValueInput("degree")
             .setCheck(null)
@@ -524,26 +524,38 @@ Blockly.Blocks['extension_servo_write_on_ai'] = {
     }
 };
 
+
+let extension_servo_write_on_ai_list = []
 Blockly.Python['extension_servo_write_on_ai'] = function (block) {
     var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
     var value_gpio = block.getFieldValue('gpio');
     Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
     Blockly.Python.definitions_['v831_import_time'] = `import time`
-    var code = ``
-    if (value_gpio == "S1" || value_gpio == "S2") {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
+    Blockly.Python.definitions_['v831_import_sys'] = `import sys
 sys.path.append("/root/")`
+    let code = ""
+    if (value_gpio == "S1" || value_gpio == "S2") {
         Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
-        Blockly.Python.definitions_["v831_servo_init" + value_gpio] = `${value_gpio}= multiFuncGpio(${value_gpio[1] - 1},1)`
-        code = `${value_gpio}.servoCtrl(${value_degree})
-`
+        const allBlocks = getBlocksByTypeName("extension_servo_write_on_ai")
+        let onedegree = allBlocks[0].innerText
+        let degree = 0
+        let degree1 = 0
+        try {
+            if (value_gpio == "S1") {
+                degree = onedegree.indexOf("S1") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S1")) / 4)].children[1].innerText
+            } else {
+                degree1 = onedegree.indexOf("S2") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S2")) / 4)].children[1].innerText
+            }
+        } catch (e) {
+            console.log("error", e)
+        }
+        Blockly.Python.definitions_["v831_servo_init" + value_gpio] = `${value_gpio}= multiFuncGpio(${value_gpio[1] - 1},1)
+${value_gpio}.servoCtrl(${allBlocks.length > 1 ? value_gpio == "S1" ? degree : degree1 : value_degree})`
+        code = `${value_gpio}.servoCtrl(${value_degree})\n`
     } else {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
-sys.path.append("/home/drivers/pylib/")`
-        Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from power import CocoPiPCA9685`;
-        Blockly.Python.definitions_['v831_import_CocoPiPCA9685_init'] = `Servo=CocoPiPCA9685()`
-        code = `Servo.position(${value_gpio},${value_degree})
-`
+        Blockly.Python.definitions_['v831_import_CocoPi_extServo'] = `from CocoPi import extServo`;
+        Blockly.Python.definitions_["v831_servo_init_" + value_gpio] = `${value_gpio} = extServo(${value_gpio[1]})`
+        code = `${value_gpio}.position(${value_degree})\n`
     }
 
     return code;
@@ -791,38 +803,33 @@ Blockly.Python['ai_motor_run'] = function (block) {
     var dropdown_motor_type = block.getFieldValue('motor_type');
     var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
     var dropdown_direction = block.getFieldValue('direction');
-    console.log(dropdown_direction,value_speed)
     // TODO: Assemble Python into code variable.
-    // Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
-    // Blockly.Python.definitions_['v831_import_time'] = `import time`
-    if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
+    Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
+    Blockly.Python.definitions_['v831_import_time'] = `import time`
+    Blockly.Python.definitions_['v831_import_sys'] = `import sys
 sys.path.append("/root/")`
+    var code = ''
+    if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
         Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
 iic_slaver=stm8s()
 iic_slaver.clear()
 del iic_slaver`;
         Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
         Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
-    } else {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
-sys.path.append("/home/drivers/pylib/")`
-        Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from power import CocoPiPCA9685`;
-        Blockly.Python.definitions_['v831_import_CocoPiPCA9685_motor_init'] = `Motor=CocoPiPCA9685()`
-    }
-    var code = ''
-    if (dropdown_direction == 'pos') {
-        if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
-            code = `${dropdown_motor_type}.dcMotorCtrl(1,${value_speed})     \n`
-        } else {
-            code = `Motor.speedControl("${dropdown_motor_type}",${value_speed})\n`
+        if (dropdown_direction == 'pos') {
+            code = `${dropdown_motor_type}.dcMotorCtrl(1,${value_speed})\n`
         }
-    }
-    else {
-        if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
+        else {
             code = `${dropdown_motor_type}.dcMotorCtrl(0,${value_speed})\n`
-        } else {
-            code = `Motor.speedControl("${dropdown_motor_type}",-${value_speed})\n`
+        }
+    } else {
+        Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
+        Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
+        if (dropdown_direction == 'pos') {
+            code = `${dropdown_motor_type}.speedControl(${value_speed})\n`
+        }
+        else {
+            code = `${dropdown_motor_type}.speedControl(-${value_speed})\n`
         }
     }
     return code;
@@ -856,28 +863,23 @@ Blockly.Python['ai_motor_stop'] = function (block) {
     // TODO: Assemble Python into code variable.
     Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
     Blockly.Python.definitions_['v831_import_time'] = `import time`
-    if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
+    Blockly.Python.definitions_['v831_import_sys'] = `import sys
 sys.path.append("/root/")`
+    var code = ''
+    if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
         Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
 iic_slaver=stm8s()
 iic_slaver.clear()
 del iic_slaver`;
         Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
         Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
+        
+        code = `${dropdown_motor_type}.dcMotorCtrl(0,0)\n`
     } else {
-        Blockly.Python.definitions_['v831_import_sys'] = `import sys
-sys.path.append("/home/drivers/pylib/")`
-        Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from power import CocoPiPCA9685`;
-        Blockly.Python.definitions_['v831_import_CocoPiPCA9685_motor_init'] = `Motor=CocoPiPCA9685()`
-    }
-    var code = ''
-    if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
-        code = `${dropdown_motor_type}.dcMotorCtrl(1,0)\n`
-    } else {
-        code = `Motor.speedControl("${dropdown_motor_type}",0)\n`
+        Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
+        Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
+        code = `${dropdown_motor_type}.speedControl(0)\n`
     }
-
     return code;
 };
 /*