lunes, 2 de julio de 2018

INICIANDO con EXTENSIONES en SCRATCH

Buscando información

SCRATCHX posee varias extensiones realizadas por usuarios,
http://scratchx.org/?url=http://khanning.github.io/scratch-arduino-extension/arduino_extension.js#scratch

Hay que instalar un Plug-in para Chrome (entre Otros) para que le sirva a uno con el programa online. A mi no me funcionó con el arduino nano (solo probé con ese)
https://scratch.mit.edu/info/ext_download/



http://scratchx.org/?url=http://khanning.github.io/scratch-arduino-extension/arduino_extension.js


Página en donde explican como usarlo:
http://khanning.github.io/scratch-arduino-extension/gettingstarted.html



S4A

Este programa esta muy bien, cumple su objetivo.
http://s4a.cat/index_es.html



COMO HACER UN PROPIA EXTENSION?

*las extensiones son archivos de javascript .js
** las extensiones se prueban solo en
  • Visit http://scratchx.org/#scratch
  • Control-click (or shift-click) on the "Load Experimental Extension" button
  • Navigate to your extension file and open it

Googleando encontré esto:

https://scratch.mit.edu/discuss/topic/69358/?page=1

dice que para hacer un código se necesita algo así (se copia y pega en el blog de notas):

Open up your text editor and paste this code in the editor:



(function(ext) {
    // Limpia la función cunado es quitada
    ext._shutdown = function() {};



    // Muestra si el hareware está conectado.
    // Muestra el status de la extensión 0 = rojo, 1 = amarillo, ó 2 = verde
    ext._getStatus = function() {
        return {status: 2, msg: 'Ready'};
    };


    // Descripción de la función a realizar
    ext.my_first_block = function() {
        // Code that gets executed when the block is run
    };


    // // Descripción de los bloques y menú de las extensiones añadidas
    var descriptor = {

        blocks: [
        // se pone como va a quedar el Bloque
        // son tres cosas  [' ', 'my first block', 'my_first_block'],
        // entre corchetes y separados por una coma

            // Block type, block name, function name
            [' ', 'my first block', 'my_first_block'],
        ]
    };

    // Registrar la extension
    ScratchExtensions.register('My first extension', descriptor, ext);

})({});

ahí encontré esto también, el WIKI de SCRATCHX:

https://github.com/LLK/scratchx/wiki


Empezando a leer hay un ejemplo:

https://raw.githubusercontent.com/LLK/scratchx/master/random_wait_extension.js

(function(ext) {
    // Cleanup function when the extension is unloaded
    ext._shutdown = function() {};

    // Status reporting code
    // Use this to report missing hardware, plugin or unsupported browser
    ext._getStatus = function() {
        return {status: 2, msg: 'Ready'};
    };

    ext.power = function(base, exponent) {
        return Math.pow(base, exponent);
    };

    // Block and block menu descriptions
    var descriptor = {
        blocks: [
            // Block type, block name, function name, param1 default value, param2 default value
            ['r', '%n ^ %n', 'power', 2, 3],
        ]
    };

    // Register the extension
    ScratchExtensions.register('Sample extension', descriptor, ext);
})({});

Dudas:

1. los tipos de funciones son: 
por ejemplo los matemáticos:
https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Math


Probando, Ejemplo para calcular el coseno de un numero

(function(ext) {
    // Cleanup function when the extension is unloaded
    ext._shutdown = function() {};

    // Status reporting code
    // Use this to report missing hardware, plugin or unsupported browser
    ext._getStatus = function() {
        return {status: 2, msg: 'Ready'};
    };
 //caulcula el coseno de un numero
    ext.Coseno = function(num) {
        return Math.cos(num);
    };

    // Block and block menu descriptions
    var descriptor = {
        blocks: [
            // Block type, block name, function name, param1 default value
            ['r', 'Coseno %n', 'Coseno', '90'],
        ]
    };

    // Register the extension
    ScratchExtensions.register('Sample extension', descriptor, ext);
})({});


2. los tipos de bloques  


Op CodeSignificado
' ' (space)Synchronous - Bloque normal de comando
'w'Asynchronous - Bloque normal de comando
'r'Synchronous - Sirve para capturar valores
'R'Asynchronous - Sirve para crear un tipo de variable
'h'Hat block  - sirve para crear un evento (returns Boolean)
'b'Boolean - Sirve para crear condicionales
 %n si son numeros, %s si es texto, and %m para menu (%m.menuName).

3. Poner valores en el bloque con  %n
['r', 'circunferencia, radio %n', 'circun', 1],
por ejemplo 


Ejemplo: Calcula el valor de una circunferencia


(function(ext) {
    // Cleanup function when the extension is unloaded
    ext._shutdown = function() {};

    // Status reporting code
    // Use this to report missing hardware, plugin or unsupported browser
    ext._getStatus = function() {
        return {status: 2, msg: 'Ready'};
    };
 //caulcula el coseno de un numero
    ext.circun = function(radio) {
        return (2 * Math.PI * radio);
    };

    // Block and block menu descriptions
    var descriptor = {
        blocks: [
            // Block type, block name, function name, param1 default value
            ['r', 'circunferencia, radio %n', 'circun', 1],
        ]
    };

    // Register the extension
    ScratchExtensions.register('Sample extension', descriptor, ext);
})({});




ré est