ArduinoGenerator.hardwareScanners['rsv2_shield_detected'] = function(block) {
    if (block.type.startsWith('rsv2_')) {
        ArduinoGenerator.includes_.add(`#include <LogicKiste_StarterShields.h>`);
        ArduinoGenerator.globals_.add(`Shield_RichShield2 rsv2;`);
        
        if (!ArduinoGenerator.autoSetup_.includes(`  rsv2.begin();\n`)) {
            ArduinoGenerator.autoSetup_.push(`  rsv2.begin();\n`);
        }
        if (!ArduinoGenerator.autoLoop_.includes(`  rsv2.update();\n`)) {
            ArduinoGenerator.autoLoop_.push(`  rsv2.update();\n`);
        }
    }
};

// --- SENSOREN ---
ArduinoGenerator.forBlock['rsv2_read_sensor'] = function(block) {
    const sensor = block.getFieldValue('SENSOR');
    let code = '0';
    if (sensor === 'TEMP') code = 'rsv2.getTemperature()';
    else if (sensor === 'US') code = 'rsv2.getDistance()';
    else if (sensor === 'MIC_RAW') code = 'rsv2.getMicLevel()';
    else if (sensor === 'MIC_PCT') code = 'rsv2.getMicPercent()';
    else if (sensor === 'JOY_X') code = 'rsv2.getJoyX()';
    else if (sensor === 'JOY_Y') code = 'rsv2.getJoyY()';
    return [code, ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv2_pir_motion'] = function(block) {
    return ['rsv2.isMotionDetected()', ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv2_joy_dir'] = function(block) {
    const dir = block.getFieldValue('DIR');
    const code = `(rsv2.getJoyDirection() == "${dir}")`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

// --- ENCODER ---
ArduinoGenerator.forBlock['rsv2_encoder_pos'] = function(block) {
    return ['rsv2.getEncoderPosition()', ArduinoGenerator.PRECEDENCE];
};

ArduinoGenerator.forBlock['rsv2_encoder_reset'] = function(block) {
    return `  rsv2.resetEncoder();\n`;
};

ArduinoGenerator.forBlock['rsv2_button_event'] = function(block) {
    const event = block.getFieldValue('EVENT');
    const code = `(rsv2.btnEncoder.getEvent() == ${event})`;
    return [code, ArduinoGenerator.PRECEDENCE];
};

// --- AUSGABEN ---
ArduinoGenerator.forBlock['rsv2_battery_level'] = function(block) {
    const lvl = ArduinoGenerator.valueToCode(block, 'LVL', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv2.setBatteryLevel(${lvl});\n`;
};

ArduinoGenerator.forBlock['rsv2_battery_pct'] = function(block) {
    const pct = ArduinoGenerator.valueToCode(block, 'PCT', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv2.setBatteryPercent(${pct});\n`;
};

ArduinoGenerator.forBlock['rsv2_rgb_led'] = function(block) {
    const ledNum = block.getFieldValue('LED_NUM');
    const r = ArduinoGenerator.valueToCode(block, 'R', ArduinoGenerator.PRECEDENCE) || '0';
    const g = ArduinoGenerator.valueToCode(block, 'G', ArduinoGenerator.PRECEDENCE) || '0';
    const b = ArduinoGenerator.valueToCode(block, 'B', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv2.setRGB(${ledNum}, ${r}, ${g}, ${b});\n`;
};

// --- MATRIX ---
ArduinoGenerator.forBlock['rsv2_matrix_text'] = function(block) {
    // 1. Direktes Textfeld auslesen (kein valueToCode mehr nötig)
    const txt = block.getFieldValue('TXT');
    
    // 2. Zustand der Checkbox prüfen (Blockly liefert hier 'TRUE' oder 'FALSE' als String)
    const scroll = block.getFieldValue('SCROLL') === 'TRUE';
    
    if (scroll) {
        // Falls Laufschrift aktiv: Tempo-Wert auslesen (Fallback auf 100ms, falls leer)
        const speed = ArduinoGenerator.valueToCode(block, 'SPEED', ArduinoGenerator.PRECEDENCE) || '100';
        return `  rsv2.showMatrixTextScroll("${txt}", ${speed});\n`;
    } else {
        // Normale statische Anzeige
        return `  rsv2.showMatrixText("${txt}");\n`;
    }
};

ArduinoGenerator.forBlock['rsv2_matrix_num'] = function(block) {
    const num = ArduinoGenerator.valueToCode(block, 'NUM', ArduinoGenerator.PRECEDENCE) || '0';
    return `  rsv2.showMatrixNumber(${num});\n`;
};

ArduinoGenerator.forBlock['rsv2_matrix_pixel'] = function(block) {
    const x = ArduinoGenerator.valueToCode(block, 'X', ArduinoGenerator.PRECEDENCE) || '0';
    const y = ArduinoGenerator.valueToCode(block, 'Y', ArduinoGenerator.PRECEDENCE) || '0';
    const state = block.getFieldValue('STATE');
    return `  rsv2.setMatrixPixel(${x}, ${y}, ${state});\n`;
};

ArduinoGenerator.forBlock['rsv2_matrix_clear'] = function(block) {
    return `  rsv2.clearMatrix();\n`;
};

const rsv2Blocks = [
    'rsv2_read_sensor', 'rsv2_pir_motion', 'rsv2_joy_dir',
    'rsv2_encoder_pos', 'rsv2_encoder_reset', 'rsv2_button_event',
    'rsv2_battery_level', 'rsv2_battery_pct', 'rsv2_rgb_led',
    'rsv2_matrix_text', 'rsv2_matrix_num', 'rsv2_matrix_pixel', 'rsv2_matrix_clear'
];
rsv2Blocks.forEach(type => {
    ArduinoGenerator.hardwareScanners[type] = ArduinoGenerator.hardwareScanners['rsv2_shield_detected'];
});