// Parametric LED bar vertical stand socket
// For a bar nominally 14 x 17 mm, 2 m long.
// This part is intended to be screwed to an MDF base.

// -------------------------
// User parameters
// -------------------------
bar_w = 14;                 // Bar width (mm)
bar_d = 17;                 // Bar depth (mm)
clearance = 0.4;            // Total clearance added to each axis (mm)

socket_height = 36;         // Height of printed socket body (mm)
wall = 3.2;                 // Socket wall thickness (mm)
base_thickness = 5;         // Printed bottom plate thickness (mm)

// USB cable/connector side opening
usb_notch_enable = true;
usb_notch_w = 11;
usb_notch_h = 9;
usb_notch_from_bottom = 6;
usb_notch_side = "right";   // "right" or "left"

// Mounting ears for MDF screws
ear_enable = true;
ear_len = 16;
ear_w = 16;
ear_thickness = base_thickness;
screw_hole_d = 4.2;         // M4 clearance. Use 3.4 for M3.
screw_hole_edge = 5.5;      // Hole center offset from ear outer corner

// Optional clamp lip at top to reduce wobble
top_lip_enable = true;
top_lip_depth = 2.0;        // Intrudes into opening on each side
top_lip_height = 3.0;

$fn = 48;

// -------------------------
// Derived
// -------------------------
inner_w = bar_w + clearance;
inner_d = bar_d + clearance;

outer_w = inner_w + wall * 2;
outer_d = inner_d + wall * 2;
outer_h = socket_height;

module screw_hole() {
    cylinder(h = ear_thickness + 0.2, d = screw_hole_d);
}

module mounting_ear(sign_y = 1) {
    translate([outer_w / 2, sign_y * (outer_d / 2), 0])
    cube([ear_len, ear_w, ear_thickness], center = false);
}

module top_lip() {
    if (top_lip_enable) {
        // Front and back lips at the top of the socket.
        translate([wall, wall, outer_h - top_lip_height])
            cube([top_lip_depth, inner_d, top_lip_height]);

        translate([outer_w - wall - top_lip_depth, wall, outer_h - top_lip_height])
            cube([top_lip_depth, inner_d, top_lip_height]);
    }
}

difference() {
    union() {
        // Main body
        cube([outer_w, outer_d, outer_h], center = false);

        // Base plate under socket for stiffness
        translate([0, 0, -base_thickness])
            cube([outer_w, outer_d, base_thickness], center = false);

        // Mounting ears
        if (ear_enable) {
            translate([0, 0, -ear_thickness]) {
                mounting_ear(1);
                mounting_ear(-1);
            }
        }

        top_lip();
    }

    // Main bar cavity
    translate([wall, wall, 0])
        cube([inner_w, inner_d, outer_h + 0.2], center = false);

    // USB side notch
    if (usb_notch_enable) {
        if (usb_notch_side == "right") {
            translate([outer_w - wall - 0.1, (outer_d - usb_notch_w) / 2, usb_notch_from_bottom])
                cube([wall + 0.3, usb_notch_w, usb_notch_h], center = false);
        } else {
            translate([-0.2, (outer_d - usb_notch_w) / 2, usb_notch_from_bottom])
                cube([wall + 0.3, usb_notch_w, usb_notch_h], center = false);
        }
    }

    // Screw holes in ears
    if (ear_enable) {
        // Upper ear hole
        translate([
            outer_w / 2 + ear_len - screw_hole_edge,
            outer_d / 2 + ear_w - screw_hole_edge,
            -ear_thickness - 0.05
        ]) screw_hole();

        // Lower ear hole
        translate([
            outer_w / 2 + ear_len - screw_hole_edge,
            -outer_d / 2 + screw_hole_edge,
            -ear_thickness - 0.05
        ]) screw_hole();
    }
}

// Print orientation helper:
// Keep the base/ears on the bed.
// If fit is tight, increase clearance to 0.5 or 0.6.
