// GoPro Mount Adapter for T-Slot Sensor Rail
// Issue #195 - GoPro 3-prong interface with tilt detents
// T-slot mounting with thumbscrew retention

// GoPro 3-Prong Interface Standards
// Center prong: 14.5mm wide
// Side prongs: 12.5mm wide
// Prong depth: 8-10mm
// Spacing between prongs: ~25mm

// Constants
GOPRO_PRONG_WIDTH_CENTER = 14.5;
GOPRO_PRONG_WIDTH_SIDE = 12.5;
GOPRO_PRONG_DEPTH = 9;
GOPRO_PRONG_SPACING = 25;        // Between center of side prongs

T_SLOT_WIDTH = 20;                // T-slot rail 20x20mm
CLAMP_JAW_DEPTH = 18;             // Clamp jaw depth
CLAMP_WIDTH = 40;                 // Clamp internal width

TILT_PIVOT_DIAMETER = 12;         // Pivot pin diameter
TILT_MIN = 0;                     // Minimum tilt angle
TILT_MAX = 90;                    // Maximum tilt angle
TILT_DETENT_INTERVAL = 15;        // Detent every 15 degrees

THUMBSCREW_DIAMETER = 10;         // M5 equivalent
THUMBSCREW_DEPTH = 8;             // Engagement depth

// Detent positions: 0, 15, 30, 45, 60, 75, 90 degrees
DETENT_POSITIONS = [0, 15, 30, 45, 60, 75, 90];

// ==================== GOPRO INTERFACE ====================

module gopro_mounting_base() {
    // Base plate that receives GoPro 3-prong connector
    // Dimensions accommodate standard GoPro mounts

    linear_extrude(height = 8) {
        // Main base: 50x35mm
        square([50, 35], center = true);
    }
}

module gopro_prong_slot_center() {
    // Center prong slot (larger for main mounting)
    // Slot dimensions: 14.5mm wide x 9mm deep x 12mm tall
    translate([0, 0, 4]) {
        cube([GOPRO_PRONG_WIDTH_CENTER, GOPRO_PRONG_DEPTH + 2, 12], center = true);
    }
}

module gopro_prong_slot_side(x_offset) {
    // Side prong slots (two of them, mirrored)
    translate([x_offset, 0, 4]) {
        cube([GOPRO_PRONG_WIDTH_SIDE, GOPRO_PRONG_DEPTH + 2, 12], center = true);
    }
}

module gopro_interface_slots() {
    // All three prong slots for GoPro mount
    gopro_prong_slot_center();                          // Center
    gopro_prong_slot_side(GOPRO_PRONG_SPACING / 2);    // Right
    gopro_prong_slot_side(-GOPRO_PRONG_SPACING / 2);   // Left
}

module gopro_mounting_assembly() {
    difference() {
        gopro_mounting_base();
        gopro_interface_slots();
    }

    // Add retaining tabs (small lips to keep mount from sliding out)
    translate([0, GOPRO_PRONG_DEPTH/2 + 1, 7]) {
        cube([50, 2, 2], center = true);
    }
}

// ==================== TILT MECHANISM ====================

module tilt_pivot_pin() {
    // Central pivot for tilt adjustment
    // M8 equivalent bolt hole with shoulder

    // Main pin body
    cylinder(h = 40, r = TILT_PIVOT_DIAMETER/2 + 1, $fn = 32);

    // Bolt hole through center
    cylinder(h = 42, r = 2.2, $fn = 16, center = true);
}

module detent_pocket(angle) {
    // Spherical detent pocket at specific angle
    // Located on tilt arm, engages with detent spring ball

    sphere_r = 2;
    translate([0, 0, 15]) {
        rotate([angle, 0, 0]) {
            translate([0, TILT_PIVOT_DIAMETER/2 + 4, 0]) {
                sphere(r = sphere_r, $fn = 16);
            }
        }
    }
}

module detent_pockets() {
    // Create all detent pockets for 0-90 degree range
    for (angle = DETENT_POSITIONS) {
        detent_pocket(angle);
    }
}

module tilt_arm() {
    // Arm that connects GoPro mount to T-slot clamp
    // Rotates around pivot pin for tilt adjustment

    // Main arm structure (tapered for strength)
    hull() {
        // Top section (connects to GoPro mount)
        translate([0, 0, 35]) {
            cube([40, 25, 10], center = true);
        }

        // Bottom section (connects to T-slot clamp)
        translate([0, 0, 0]) {
            cube([35, 20, 8], center = true);
        }
    }
}

module tilt_mechanism_assembly() {
    // Pivot pin at center
    translate([0, 0, 8]) {
        tilt_pivot_pin();
    }

    // Tilt arm with GoPro mount
    translate([0, 0, 8]) {
        rotate([0, 0, 0]) {  // Default position: 0 degrees tilt
            difference() {
                union() {
                    tilt_arm();
                    gopro_mounting_assembly();
                }

                // Pivot hole
                translate([0, 0, -5]) {
                    cylinder(h = 50, r = 2.2, $fn = 16);
                }
            }
        }
    }

    // Detent ball pockets (visible for assembly reference)
    translate([0, 0, 8]) {
        detent_pockets();
    }
}

// ==================== THUMBSCREW RETENTION ====================

module thumbscrew_mechanism() {
    // Thumbscrew for locking tilt position
    // M5 equivalent (10mm head)

    // Engagement hole on tilt arm
    translate([15, 0, 20]) {
        cylinder(h = THUMBSCREW_DEPTH + 2, r = THUMBSCREW_DIAMETER/2 + 0.5, $fn = 16);
    }
}

// ==================== T-SLOT CLAMP ====================

module tslot_clamp_base() {
    // Base connector for T-slot mounting
    // Similar to phone mount but simplified for GoPro (lighter load)

    linear_extrude(height = 12) {
        square([25, 25], center = true);
    }
}

module tslot_mounting_holes() {
    // M4 bolt holes for T-nut attachment
    hole_inset = 7;
    translate([hole_inset, hole_inset, -1])
        cylinder(h = 14, r = 2.2, $fn = 16);
    translate([-hole_inset, hole_inset, -1])
        cylinder(h = 14, r = 2.2, $fn = 16);
    translate([hole_inset, -hole_inset, -1])
        cylinder(h = 14, r = 2.2, $fn = 16);
    translate([-hole_inset, -hole_inset, -1])
        cylinder(h = 14, r = 2.2, $fn = 16);
}

module tslot_clamp_assembly() {
    difference() {
        tslot_clamp_base();
        tslot_mounting_holes();
    }
}

// ==================== PIVOT HOUSING ====================

module pivot_housing() {
    // Connects T-slot clamp to tilt mechanism
    // Houses the pivot pin

    translate([0, 0, 12]) {
        // Main housing body
        cube([30, 30, 15], center = true);

        // Pivot socket (receives pivot pin)
        translate([0, 0, 2]) {
            cylinder(h = 10, r = TILT_PIVOT_DIAMETER/2 + 1.5, $fn = 32);
        }
    }
}

module pivot_housing_assembly() {
    difference() {
        pivot_housing();

        // Clearance hole for pivot pin
        translate([0, 0, 10]) {
            cylinder(h = 20, r = TILT_PIVOT_DIAMETER/2 + 0.2, $fn = 32);
        }
    }
}

// ==================== COMPLETE ASSEMBLY ====================

module complete_gopro_mount() {
    // Fixed base: T-slot clamp
    tslot_clamp_assembly();

    // Pivot housing
    pivot_housing_assembly();

    // Tilt mechanism with GoPro mount
    tilt_mechanism_assembly();

    // Thumbscrew engagement hole marker
    thumbscrew_mechanism();
}

// ==================== RENDER ====================
complete_gopro_mount();

// Visualization aids (comment out for export):
// Show tilt range visualization
// translate([80, 0, 0]) rotate([15, 0, 0]) complete_gopro_mount();
// translate([160, 0, 0]) rotate([90, 0, 0]) complete_gopro_mount();

// Show T-slot envelope
// %translate([0, 0, 0]) cube([20, 20, 30], center = true);
