// ==========================================
// ARDUINO UNO R4: DAC (ANALOGER AUSGANG A0)
// ==========================================

Blockly.defineBlocksWithJsonArray([
  {
    "type": "r4_dac_preset",
    "message0": "Spannung (0-5 V) an A0: %1 , %2 Volt",
    "args0": [
      {
        "type": "field_dropdown",
        "name": "VOLT_WHOLE",
        "options": [
          ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"]
        ]
      },
      {
        "type": "field_dropdown",
        "name": "VOLT_DECIMAL",
        "options": [
          ["0", "0"], ["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], 
          ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"]
        ]
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": 160,
    "tooltip": "Gibt eine Gleichspannung am DAC (Pin A0) aus."
  },
  {
    "type": "r4_dac_voltage_var",
    "message0": "Spannung an A0: %1 Volt",
    "args0": [
      {
        "type": "input_value",
        "name": "VOLTAGE",
        "check": "Number"
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": 160,
    "tooltip": "Gibt die angegebene Volt-Zahl (0.0 bis 5.0) am DAC aus."
  },
  {
    "type": "r4_dac_raw_var",
    "message0": "DAC-Rohwert (0-4095) A0: %1",
    "args0": [
      {
        "type": "input_value",
        "name": "RAW_VALUE",
        "check": "Number"
      }
    ],
    "previousStatement": null,
    "nextStatement": null,
    "colour": 160,
    "tooltip": "Gibt einen 12-Bit Wert (0 bis 4095) an den DAC aus."
  }
]);

// --- DEZENTRALER SCANNER FÜR R4 DAC ---
function ensureR4DacSetup() {
    // Legt die Variable const int pinA0 = A0; global an, passend zur LogicKiste
    ArduinoGenerator.globals_.add("const int pinA0 = A0;");
    
    // analogWriteResolution(12) nur einmalig ins setup() pushen, um Mehrfacheinträge zu verhindern
    if (!ArduinoGenerator.autoSetup_.includes("  analogWriteResolution(12);\n")) {
        ArduinoGenerator.autoSetup_.push("  analogWriteResolution(12);\n");
    }
}

// Scanner an die drei Blöcke binden
ArduinoGenerator.hardwareScanners['r4_dac_preset'] = ensureR4DacSetup;
ArduinoGenerator.hardwareScanners['r4_dac_voltage_var'] = ensureR4DacSetup;
ArduinoGenerator.hardwareScanners['r4_dac_raw_var'] = ensureR4DacSetup;


// --- C++ GENERATOREN ---

// 1. Anfänger-Block (Preset)
ArduinoGenerator.forBlock['r4_dac_preset'] = function(block) {
    const whole = parseInt(block.getFieldValue('VOLT_WHOLE'));
    const decimal = parseInt(block.getFieldValue('VOLT_DECIMAL'));
    
    // Millivolt berechnen (deckeln auf max. 5000 mV, falls jemand "5,9" auswählt)
    let millivolts = (whole * 1000) + (decimal * 100);
    if (millivolts > 5000) millivolts = 5000;
    
    // In 12-Bit DAC Wert (0-4095) umrechnen
    const dacValue = Math.round((millivolts / 5000.0) * 4095);
    
    return `  analogWrite(pinA0, ${dacValue}); // DAC: ${(millivolts/1000).toFixed(1)} V\n`;
};

// 2. Volt über Variable (Fortgeschritten)
ArduinoGenerator.forBlock['r4_dac_voltage_var'] = function(block) {
    const voltage = ArduinoGenerator.valueToCode(block, 'VOLTAGE', 0) || '0';
    return `  analogWrite(pinA0, map(constrain(${voltage} * 1000, 0, 5000), 0, 5000, 0, 4095));\n`;
};

// 3. Rohwert (Profi)
ArduinoGenerator.forBlock['r4_dac_raw_var'] = function(block) {
    const rawVal = ArduinoGenerator.valueToCode(block, 'RAW_VALUE', 0) || '0';
    return `  analogWrite(pinA0, constrain(${rawVal}, 0, 4095));\n`;
};