(() => {

    'use strict';
    goog.provide('Blockly.Arduino.PWM_Servo');
    goog.require('Blockly.Arduino');

    Blockly.Arduino.forBlock.inmp441 = function() {
        var value_SCK = Blockly.Arduino.valueToCode(this, 'SCK', Blockly.Arduino.ORDER_ATOMIC);
        var value_SD = Blockly.Arduino.valueToCode(this, 'SD', Blockly.Arduino.ORDER_ATOMIC);
        var value_WS = Blockly.Arduino.valueToCode(this, 'WS', Blockly.Arduino.ORDER_ATOMIC);
        var value_Gain = Blockly.Arduino.valueToCode(this, 'Gain', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_'+'NAME1'] = '#include "Arduino.h"\n'
                                                            + '#include "driver/i2s.h"';
        Blockly.Arduino.definitions_['define_'+'NAME2'] = '#define SAMPLE_RATE (44100)\n'
                                                            + '#define I2S_MIC_WS ('+value_WS+')\n'
                                                            + '#define I2S_MIC_SD ('+value_SD+')\n'
                                                            + '#define I2S_MIC_BCK ('+value_SCK+')\n'
                                                            + '#define I2S_PORT_0 (I2S_NUM_0)\n'
                                                            + '#define bufferLen (1024)';
        Blockly.Arduino.definitions_['var_declare_'+'NAME3'] = 'int16_t sBuffer_RX[bufferLen];\n'
                                                                + 'size_t bytesIn = 0;';
        Blockly.Arduino.definitions_['function_'+'NAME4'] = 'void I2s_Mic_Init()\n {\n   i2s_config_t i2s_config_RX{};\n   i2s_config_RX.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX);\n   i2s_config_RX.sample_rate = SAMPLE_RATE;\n   i2s_config_RX.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;\n   i2s_config_RX.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;\n   i2s_config_RX.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB);\n   i2s_config_RX.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;\n   i2s_config_RX.dma_buf_count = 8;\n   i2s_config_RX.dma_buf_len = bufferLen;\n   i2s_config_RX.use_apll = false;\n   i2s_driver_install(I2S_PORT_0, &i2s_config_RX, 0, NULL);\n   i2s_pin_config_t pin_config_RX{};\n   pin_config_RX.mck_io_num = I2S_PIN_NO_CHANGE;\n   pin_config_RX.bck_io_num = I2S_MIC_BCK;\n   pin_config_RX.ws_io_num = I2S_MIC_WS;\n   pin_config_RX.data_out_num = I2S_PIN_NO_CHANGE;\n   pin_config_RX.data_in_num = I2S_MIC_SD;\n   i2s_set_pin(I2S_PORT_0, &pin_config_RX);\n   i2s_start(I2S_PORT_0);\n }\n'
                                                            + 'int16_t* Mic_Value()\n {\n   i2s_read(I2S_PORT_0, sBuffer_RX, sizeof(sBuffer_RX), &bytesIn, portMAX_DELAY);\n   for (int i = 0; i < bufferLen; i++)\n   {\n     sBuffer_RX[i] = sBuffer_RX[i]*'+value_Gain+';\n     if (sBuffer_RX[i] > (32767))\n       sBuffer_RX[i] = (32767);\n     else if (sBuffer_RX[i] < (-32768))\n       sBuffer_RX[i] = (-32768);\n   }\n   return sBuffer_RX;\n }';
        Blockly.Arduino.setups_['NAME5'] = 'I2s_Mic_Init();';
        var code = '';
        return code;
    };
    
    Blockly.Arduino.forBlock.int16_p = function() {
        var dropdown_type = this.getFieldValue('type');
        var text_name = this.getFieldValue('name');
        var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
        var code = ''+dropdown_type+''+text_name+' = '+value_value+';\n';
        return code;
    };

    Blockly.Arduino.forBlock.mic_api = function() {
        var code = 'Mic_Value()';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock.MAX98357 = function() {
        var value_Din = Blockly.Arduino.valueToCode(this, 'Din', Blockly.Arduino.ORDER_ATOMIC);
        var value_Bclk = Blockly.Arduino.valueToCode(this, 'Bclk', Blockly.Arduino.ORDER_ATOMIC);
        var value_Lrclk = Blockly.Arduino.valueToCode(this, 'Lrclk', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_'+'NAME11'] = '#include "Arduino.h"\n'
                                                            + '#include "driver/i2s.h"';
        Blockly.Arduino.definitions_['define_'+'NAME12'] = '#define SAMPLE_RATE (44100)\n'
                                                            + '#define bufferLen (1024)\n'
                                                            + '#define I2S_SPK_DIN ('+value_Din+')\n'
                                                            + '#define I2S_SPK_BCLK ('+value_Bclk+')\n'
                                                            + '#define I2S_SPK_LRCLK ('+value_Lrclk+')\n'
                                                            + '#define I2S_PORT_1 (I2S_NUM_1)';
        Blockly.Arduino.definitions_['var_declare_'+'NAME13'] = 'int16_t sBuffer_TX[bufferLen*2];\n'
                                                                + 'size_t bytesOut = 0;';
        Blockly.Arduino.definitions_['function_'+'NAME14'] = 'void I2s_Spk_Init()\n {\n   i2s_config_t i2s_config_TX{};\n   i2s_config_TX.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);\n   i2s_config_TX.sample_rate = SAMPLE_RATE;\n   i2s_config_TX.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;\n   i2s_config_TX.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;\n   i2s_config_TX.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB);\n   i2s_config_TX.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;\n   i2s_config_TX.dma_buf_count = 8;\n   i2s_config_TX.dma_buf_len = bufferLen;\n   i2s_config_TX.use_apll = false;\n   i2s_driver_install(I2S_PORT_1, &i2s_config_TX, 0, NULL);\n   i2s_pin_config_t pin_config_TX{};\n   pin_config_TX.mck_io_num = I2S_PIN_NO_CHANGE;\n   pin_config_TX.bck_io_num = I2S_SPK_BCLK;\n   pin_config_TX.ws_io_num = I2S_SPK_LRCLK;\n   pin_config_TX.data_out_num = I2S_SPK_DIN;\n   pin_config_TX.data_in_num = I2S_PIN_NO_CHANGE;\n   i2s_set_pin(I2S_PORT_1, &pin_config_TX);\n   i2s_start(I2S_PORT_1);\n }\n'
                                                            + 'void Spk_Value(int16_t* sBuffer_RX)\n {\n   for (int i = 0; i < bufferLen; i++)\n   {\n     sBuffer_TX[i*2] = sBuffer_RX[i];\n     sBuffer_TX[i*2+1] = sBuffer_RX[i];\n   }\n   i2s_write(I2S_PORT_1, sBuffer_TX, sizeof(sBuffer_TX), &bytesOut, portMAX_DELAY);\n }';
        Blockly.Arduino.setups_['NAME15'] = 'I2s_Spk_Init();';
        var code = '';
        return code;
    };

    Blockly.Arduino.forBlock.spk_api = function() {
        var value_spk_value = Blockly.Arduino.valueToCode(this, 'spk_value', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'Spk_Value('+value_spk_value+');';
        return code;
    };
    
})();