#pragma experiment("BRIDGE_CONNECT") 
#pragma experiment("MODULE_TEMPLATING")

import ElectricPower
from "parts/UNI_ROYAL_0805W8F1001T5E/UNI_ROYAL_0805W8F1001T5E.ato" import UNI_ROYAL_0805W8F1001T5E_package
from "atopile/mounting-holes/MountingHole.py" import MountingHole
from "parts/AVX_TAJB107K006RNJ/AVX_TAJB107K006RNJ.ato" import AVX_TAJB107K006RNJ_package                            
from "parts/MDD_Microdiode_Electronics_SM4007PL/MDD_Microdiode_Electronics_SM4007PL.ato" import MDD_Microdiode_Electronics_SM4007PL_package 
from "parts/hanxia_HX_PM2_54_1x8P_TP_H8_5_YQ/hanxia_HX_PM2_54_1x8P_TP_H8_5_YQ.ato" import hanxia_HX_PM2_54_1x8P_TP_H8_5_YQ_package 
from "parts/JST_Sales_America_B4B_PH_SM4_TB_LF__SN/JST_Sales_America_B4B_PH_SM4_TB_LF__SN.ato" import JST_Sales_America_B4B_PH_SM4_TB_LF__SN_package
from "parts/JST_Sales_America_B2B_PH_SM4_TBT_LF__SN/JST_Sales_America_B2B_PH_SM4_TBT_LF__SN.ato" import JST_Sales_America_B2B_PH_SM4_TBT_LF__SN_package              
from "parts/SOFNG_PJ_320B/SOFNG_PJ_320B.ato" import SOFNG_PJ_320B_package  

# Component for the SMD 3.5mm Audio Jack
component AudioJack3_5mm from SOFNG_PJ_320B_package:
    lcsc_id = "C22355831"
    signal SLEEVE ~ pin 1  # Ground
    signal RING ~ pin 2    # Right Channel  
    signal TIP ~ pin 3     # Left Channel

# Component for JST PH series vertical connectors
component JST_PH_Vertical_2Pin from JST_Sales_America_B2B_PH_SM4_TBT_LF__SN_package:
    lcsc_id = "C265003"
    signal SPK_1 ~ pin 1
    signal SPK_2 ~ pin 2

# Component for JST PH series vertical connectors
component JST_PH_Vertical_4Pin from JST_Sales_America_B4B_PH_SM4_TB_LF__SN_package:
    lcsc_id = "C160354"
    signal VCC ~ pin 1
    signal GND ~ pin 2
    signal RX ~ pin 3
    signal TX ~ pin 4

# Components for the SMD 1x8 pin sockets for the DFPlayer
component DFPlayer_Socket_Left from hanxia_HX_PM2_54_1x8P_TP_H8_5_YQ_package:
    lcsc_id = "C46635844"
    signal VCC ~ pin 1
    signal RX ~ pin 2
    signal TX ~ pin 3
    signal DAC_R ~ pin 4
    signal DAC_L ~ pin 5
    signal SPK_1 ~ pin 6
    signal GND ~ pin 7
    signal SPK_2 ~ pin 8

component DFPlayer_Socket_Right from hanxia_HX_PM2_54_1x8P_TP_H8_5_YQ_package:
    lcsc_id = "C46635844"
    signal BUSY ~ pin 1
    signal USB_M ~ pin 2
    signal USB_P ~ pin 3
    signal ADKEY_2 ~ pin 4
    signal ADKEY_1 ~ pin 5
    signal IO_2 ~ pin 6
    signal GND ~ pin 7
    signal IO_1 ~ pin 8

component SM4007_Diode from MDD_Microdiode_Electronics_SM4007PL_package:
    lcsc_id = "C64898"

component Cap_100uF from AVX_TAJB107K006RNJ_package:
    lcsc_id = "C16133"
    signal plus ~ pin 1  # positive terminal
    signal minus ~ pin 2  # negative terminal

component Resistor_SMD from UNI_ROYAL_0805W8F1001T5E_package:
    lcsc_id = "C17513"

module FourMountingHoles:
    m2_no_pad1 = new MountingHole<metric_screw_size="M2", pad_type="NoPad">
    m2_no_pad2 = new MountingHole<metric_screw_size="M2", pad_type="NoPad">
    m2_no_pad3 = new MountingHole<metric_screw_size="M2", pad_type="NoPad">
    m2_no_pad4 = new MountingHole<metric_screw_size="M2", pad_type="NoPad">

module DFPlayerCarrierBoard:
    # --- Signals and Power rails ---
    power_5v = new ElectricPower

    # Specify voltages on power rails
    power_5v.voltage = 5.1V +/- 3%
    
    # --- Components ---
    # Sockets for DFPlayer Mini
    df_header_left = new DFPlayer_Socket_Left
    df_header_right = new DFPlayer_Socket_Right

    # Diode for dropout protection (cathode to VCC)
    power_protection_diode = new SM4007_Diode

    # Capacitor for power filtering
    power_filter_cap1 = new Cap_100uF
    power_filter_cap2 = new Cap_100uF
    power_filter_cap3 = new Cap_100uF
    power_filter_cap4 = new Cap_100uF
    power_filter_cap5 = new Cap_100uF

    # Input Connector (Power and UART)
    uart_connector = new JST_PH_Vertical_4Pin

    # 1k Ohm series resistor for the DFPlayer's RX line for 5V tolerance.
    rx_protection_resistor = new Resistor_SMD

    # Output Connectors
    speaker_connector = new JST_PH_Vertical_2Pin
    stereo_out_jack = new AudioJack3_5mm

    # --- Mechanical Mounting Holes ---
    mounting_holes = new FourMountingHoles

    # --- Connections ---

    # 1. Power Supply Circuit
    uart_connector.VCC ~ power_5v.vcc
    uart_connector.GND ~ power_5v.gnd

    # 2. DFPlayer Power and Ground Connections, diode and capacitor
    # Diode for dropout protection (cathode to VCC)
    uart_connector.VCC ~ power_protection_diode.A
    power_protection_diode.C ~ df_header_left.VCC

    # Capacitor for power filtering (5x electrolytic, 100uF/6.3V)
    df_header_left.VCC ~ power_filter_cap1.plus
    power_5v.gnd ~ power_filter_cap1.minus

    df_header_left.VCC ~ power_filter_cap2.plus
    power_5v.gnd ~ power_filter_cap2.minus
    
    df_header_left.VCC ~ power_filter_cap3.plus
    power_5v.gnd ~ power_filter_cap3.minus
    
    df_header_left.VCC ~ power_filter_cap4.plus
    power_5v.gnd ~ power_filter_cap4.minus
    
    df_header_left.VCC ~ power_filter_cap5.plus
    power_5v.gnd ~ power_filter_cap5.minus

    # Connect another ground pin
    power_5v.gnd ~ df_header_right.GND
    power_5v.gnd ~ df_header_left.GND

    # 3. UART Serial Connection
    uart_connector.RX ~ rx_protection_resistor.1; rx_protection_resistor.2 ~ df_header_left.RX  # Connect UART RX through resistor
    uart_connector.TX ~ df_header_left.TX  # TX (direct connection)
    # 4. Speaker Output Connection
    speaker_connector.SPK_1 ~ df_header_left.SPK_1
    speaker_connector.SPK_2 ~ df_header_left.SPK_2

    # 5. Stereo Line Out Connection
    stereo_out_jack.TIP ~ df_header_left.DAC_L
    stereo_out_jack.RING ~ df_header_left.DAC_R
    stereo_out_jack.SLEEVE ~ power_5v.gnd
