'use strict';

goog.provide('Blockly.Python.touch');
goog.require('Blockly.Python');

Blockly.Python['touch_setup'] = function(block) {
    var wireh = '#include <Wire.h>\n';
    Blockly.Python.addDeclaration('wire.h', wireh);

    var decl = '#include "Adafruit_MPR121.h"\n' +
        'Adafruit_MPR121 cap = Adafruit_MPR121();\n' +
        'uint16_t lasttouched = 0;\n' +
        'uint16_t currtouched = 0;\n';
    Blockly.Python.addDeclaration('touch_sensor_decl', decl);

    var setup = 'Serial.begin(9600);\n' +
    'Serial.println("Adafruit MPR121 Capacitive Touch sensor test");\n' +
        '  if (!cap.begin(0x5A)) {\n' +
        '    Serial.println("MPR121 not found, check wiring?");\n' +
        '    while (1);\n  }\n' +
        '  Serial.println("MPR121 found!");\n';

    Blockly.Python.addSetup('music_keyboard_setup', setup);

    return '';
};

Blockly.Python['touch_keyboardset'] = function(block) {
    var variable_name = "i_";
    var codePre = "currtouched = cap.touched();\n" +
        "for (uint8_t " + variable_name + "=0; " + variable_name + "<12; " + variable_name + "++) {\n" +
        "  if ((currtouched & _BV(" + variable_name + ")) && !(lasttouched & _BV(" + variable_name + ")) ) {\n";

    var code = "    switch (" + variable_name + ") {\n";
    var argument, branch;
    for (var n = 0; n <= block.caseCount_; n++) {
        argument = block.getFieldValue('keyboard' + n) || n;
        branch = Blockly.Python.statementToCode(block, 'DO' + n);
        code += "      case " + argument + ":\n      " + branch + "      break;\n";
    }
    return codePre + code + '    }\n' + 'lasttouched = currtouched;\n' + '   }\n' + '}\n';
}

Blockly.Python['touch_keyboardset_loose'] = function(block) {
    var variable_name = "i_";
    var codePre = "for (uint8_t " + variable_name + "=0; " + variable_name + "<12; " + variable_name + "++) {\n" +
        "  if (!(currtouched & _BV(" + variable_name + ")) && (lasttouched & _BV(" + variable_name + ")) ) {\n";

    var code = "    switch (" + variable_name + ") {\n";
    var argument, branch;
    for (var n = 0; n <= block.caseCount_; n++) {
        argument = block.getFieldValue('keyboard' + n) || n;
        branch = Blockly.Python.statementToCode(block, 'DO' + n);
        code += "      case " + argument + ":\n      " + branch + "      break;\n";
    }
    return codePre + code + '    }\n' + 'lasttouched = 0;\n' + '   }\n' + '}\n';
}


/* new version touch */
Blockly.Python['touch_setup_1'] = function(block) {
    var wireh = '#include <Wire.h>\n';
    Blockly.Python.addDeclaration('wire.h', wireh);

    var decl = '#include "Adafruit_MPR121.h"\n' +
        'Adafruit_MPR121 cap = Adafruit_MPR121();\n' +
        'uint16_t lasttouched = 0;\n' +
        'uint16_t currtouched = 0;\n';
    Blockly.Python.addDeclaration('touch_sensor_decl', decl);

    var setup = 'Serial.println("Adafruit MPR121 Capacitive Touch sensor test");\n' +
        '  if (!cap.begin(0x5A)) {\n' +
        '    Serial.println("MPR121 not found, check wiring?");\n' +
        '    while (1);\n  }\n' +
        '  Serial.println("MPR121 found!");\n';
    Blockly.Python.addSetup('music_keyboard_setup', setup);

    var code = "currtouched = cap.touched();\n"
    return code;
};

Blockly.Python['touch_each'] = function(block) {
    var key = block.getFieldValue("keyboard");
    var branch0 = Blockly.Python.statementToCode(block, 'DO0');
    var branch1 = Blockly.Python.statementToCode(block, 'DO1');
    var code = "if ((currtouched & _BV(" + key + "))&& !(lasttouched & _BV(" + key + "))){\n" + branch0 +
        "  lasttouched = currtouched;\n" +
        "}\n" +
        "if ((lasttouched & _BV(" + key + ")) && !(currtouched & _BV(" + key + ")) ) {\n" + branch1 +
        "  lasttouched = 0;\n" +
        "}\n";
    return code;
}