ArduinoGenerator.hardwareScanners['mfs_v2_shield_detected'] = function(block) {
    if (block.type.startsWith('mfs_v2_')) {
        ArduinoGenerator.includes_.add(`#include <Shield_MultiFunctionV2.h>`);
        ArduinoGenerator.globals_.add(`Shield_MultiFunction mfsv2;`);
        
        if (!ArduinoGenerator.autoSetup_.includes(`  mfsv2.begin();\n`)) {
            ArduinoGenerator.autoSetup_.push(`  mfsv2.begin();\n`);
        }
        if (!ArduinoGenerator.autoLoop_.includes(`  mfsv2.update();\n`)) {
            ArduinoGenerator.autoLoop_.push(`  mfsv2.update();\n`);
        }
    }
};

// --- TASTER ---
ArduinoGenerator.forBlock['mfs_v2_button_event'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    const event = block.getFieldValue('EVENT');
    const code = `(mfsv2.${button}.getEvent() == ${event})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['mfs_v2_button_counter'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    const code = `mfsv2.${button}.getClickCount()`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['mfs_v2_button_reset'] = function(block) {
    const button = block.getFieldValue('BUTTON');
    return `  mfsv2.${button}.resetClickCount();\n`;
};

// --- LEDs ---
ArduinoGenerator.forBlock['mfs_v2_led_digital'] = function(block) {
    const led = block.getFieldValue('LED');
    const state = block.getFieldValue('STATE');
    return `  mfsv2.setLED(${led}, ${state});\n`;
};

ArduinoGenerator.forBlock['mfs_v2_led_pwm'] = function(block) {
    const led = block.getFieldValue('LED');
    const val = ArduinoGenerator.valueToCode(block, 'VALUE', ArduinoGenerator.PRECEDENCE) || '0';
    return `  mfsv2.setLEDPWM(${led}, ${val});\n`;
};

// --- SUMMER ---
ArduinoGenerator.forBlock['mfs_v2_buzzer_digital'] = function(block) {
    const state = block.getFieldValue('STATE');
    return `  mfsv2.setBuzzer(${state});\n`;
};

ArduinoGenerator.forBlock['mfs_v2_buzzer_tone'] = function(block) {
    const freq = ArduinoGenerator.valueToCode(block, 'FREQ', ArduinoGenerator.PRECEDENCE) || '440';
    const dur = ArduinoGenerator.valueToCode(block, 'DURATION', ArduinoGenerator.PRECEDENCE) || '1';
    return `  mfsv2.playTone(${freq}, ${dur});\n`;
};

// --- DISPLAY ---
ArduinoGenerator.forBlock['mfs_v2_display_number'] = function(block) {
    const val = ArduinoGenerator.valueToCode(block, 'VALUE', ArduinoGenerator.PRECEDENCE) || '0';
    return `  mfsv2.showNumber(${val});\n`;
};

ArduinoGenerator.forBlock['mfs_v2_display_word'] = function(block) {
    const c1 = block.getFieldValue('C1');
    const c2 = block.getFieldValue('C2');
    const c3 = block.getFieldValue('C3');
    const c4 = block.getFieldValue('C4');
    return `  mfsv2.showWord(${c1}, ${c2}, ${c3}, ${c4});\n`;
};

ArduinoGenerator.forBlock['mfs_v2_display_clear'] = function(block) {
    return `  mfsv2.clearDisplay();\n`;
};

// --- POTI ---
ArduinoGenerator.forBlock['mfs_v2_read_poti'] = function(block) {
    return [`mfsv2.readPoti()`, ArduinoGenerator.PRECEDENCE];
};

// Den Scanner für alle MFS-V2-Blöcke registrieren
const mfsv2Blocks = [
    'mfs_v2_button_event', 'mfs_v2_button_counter', 'mfs_v2_button_reset',
    'mfs_v2_led_digital', 'mfs_v2_led_pwm', 'mfs_v2_buzzer_digital', 'mfs_v2_buzzer_tone',
    'mfs_v2_display_number', 'mfs_v2_display_word', 'mfs_v2_display_clear',
    'mfs_v2_read_poti'
];
mfsv2Blocks.forEach(type => {
    ArduinoGenerator.hardwareScanners[type] = ArduinoGenerator.hardwareScanners['mfs_v2_shield_detected'];
});