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

import ElectricPower
import Resistor
import Pad
from "atopile/mounting-holes/MountingHole.py" import MountingHole
from "parts/PANASONIC_EEEFK1A471AP/PANASONIC_EEEFK1A471AP.ato" import PANASONIC_EEEFK1A471AP_package
from "parts/hongjiacheng_M1/hongjiacheng_M1.ato" import hongjiacheng_M1_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  

# DFPlayer Mini MP3 Module component definition based on its pinout
# component DFPlayer_Mini:
#     # Left Side Pins (as viewed from top)
#     signal VCC; signal RX; signal TX; signal DAC_R; signal DAC_L; signal SPK_1; signal GND_1; signal SPK_2
#     # Right Side Pins
#     signal BUSY; signal USB_M; signal USB_P; signal ADKEY_2; signal ADKEY_1; signal IO_2; signal GND; signal IO_1

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

# Component for JST PH series vertical connectors
component JST_PH_Vertical_2Pin from JST_Sales_America_B2B_PH_SM4_TBT_LF__SN_package:
    footprint = "JST_PH_S2B-PH-K_1x02_P2.00mm_Horizontal"
    # lcsc = "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:
    footprint = "JST_PH_S4B-PH-K_1x04_P2.00mm_Horizontal"
    # lcsc = "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:
    footprint = "Connector_PinSocket_2.54mm/PinSocket_1x08_P2.54mm_Vertical_SMD_Pin1Left"
    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:
    footprint = "Connector_PinSocket_2.54mm/PinSocket_1x08_P2.54mm_Vertical_SMD_Pin1Left"
    signal IO_1 ~ pin 1
    signal GND ~ pin 2
    signal IO_2 ~ pin 3
    signal ADKEY_1 ~ pin 4
    signal ADKEY_2 ~ pin 5
    signal USB_P ~ pin 6
    signal USB_M ~ pin 7
    signal BUSY ~ pin 8

component SOD123_Diode from hongjiacheng_M1_package:
    footprint = "Diode_SMD:D_SOD-123"
    # lcsc = "C18199093"

component Elec_Cap_6_3x5_4 from PANASONIC_EEEFK1A471AP_package:
    footprint = "Capacitor_SMD:CP_Elec_6.3x5.4"
    signal plus ~ pin 1  # positive terminal
    signal minus ~ pin 2  # negative terminal

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

    # Specify voltages on power rails
    power_5v.voltage = 5.1V +/- 3%
    
    # --- Components ---
    # DFPlayer Mini module instance
    # dfplayer = new DFPlayer_Mini
    # dfplayer.exclude_from_bom = True  # Exclude from BOM since it's a module

    # Sockets for DFPlayer Mini
    df_header_left = new DFPlayer_Socket_Left
    df_header_right = new DFPlayer_Socket_Right

    # Power Supply Components
    power_protection_diode = new hongjiacheng_M1_package
    # power_protection_diode.lcsc = "C18199093"

    power_filter_cap = new Elec_Cap_6_3x5_4
    power_filter_cap.capacitance = 470uF +/- 20%
    power_filter_cap.voltage = 10V
    # power_filter_cap.lcsc = "C178529"

    # 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
    rx_protection_resistor.resistance = 1kohm +/- 5%
    rx_protection_resistor.package = "0805"    
    # rx_protection_resistor.lcsc = "C17513"

    # Output Connectors
    speaker_connector = new JST_PH_Vertical_2Pin
    stereo_out_jack = new AudioJack3_5mm

    # --- Mechanical Mounting Holes ---
    # m2_with_pad = new MountingHole<metric_screw_size="M2", pad_type="Pad">
    # m2_with_pad.exclude_from_bom = True 
    # m6_no_pad = new MountingHole<metric_screw_size="M6", pad_type="NoPad">
    # m3_top_pad = new MountingHole<metric_screw_size="M3", pad_type="Pad_TopOnly">
    # m4_pad_with_vias = new MountingHole<metric_screw_size="M4", pad_type="Pad_Via">

    # m2_with_pad.contact ~ m3_top_pad.contact
    # m3_top_pad.contact ~ m4_pad_with_vias.contact
    # m6_no_pad has no contact

    # --- 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.K ~ df_header_left.VCC

    # Capacitor for power filtering (electrolytic, 470uF, 10V)
    df_header_left.VCC ~ power_filter_cap.plus
    power_5v.gnd ~ power_filter_cap.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 ~> df_header_left.RX # Connect UART RX to resistor input
    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
