/*
---
title: Edge bracket
summary: Rail with a side wall and front lip that holds the edge of a panel or tablet.
tags: [rail, bracket, holder]
hardware: [M3 screws, M3 nuts]
print: Print flat, rail side down, no supports.
---
A 1 unit wide rail (screw holes on the grid) ending in a wall with a lip that
reaches over the front of a flat device, e.g. a tablet in a frame
(`builds/ipadmini4-frame.scad` uses eight of them: two per side).

- **Units** and **Offset** set where the wall is: `units × 15 mm + offset`
  from the start of the rail. Use the offset for devices that aren't a
  multiple of 15 mm.
- **Gap** is the thickness of the device, **Ledge** raises it off the rail.
- **Label** is engraved on top of the rail (e.g. L, R, T, B).
*/
use <gridunit/lib/roundcube.scad>;
use <gridunit/core/rail.scad>;

/* [Size] */

// Rail length to the wall, in 15 mm units
units = 3; // [1:1:20]

// Extra length to the wall in mm (devices that aren't a multiple of 15 mm)
offset = 1; // [0:0.1:14.9]

// Thickness of the held device in mm
gap = 6.1; // [0.5:0.1:30]

// Raise the device off the rail by this much (mm)
ledge = 0; // [0:0.5:20]

/* [Wall and lip] */

// Wall thickness in mm
sideWidth = 3; // [2:0.5:10]

// How far the lip reaches over the device, and its thickness, in mm
frontLip = 3; // [1:0.5:10]

/* [Label] */

// Text engraved on the rail (empty for none)
label = "";

edgeBracket(units, gap, ledge, offset, label, sideWidth, frontLip);

module edgeBracket(units = 3, gap = 0, ledge = 0, offset = 0, label = "", sideWidth = 3, frontLip = 3) {
    $fn = 32;
    e = 0.01;
    gu = 15;
    ru = 4;
    cr = 1;

    difference() {
        rail(units * gu + offset + sideWidth, gu, holes=true, alignX="start");

        if (label != "")
            translate([0.8 * gu, gu * 0.8, ru - 1 + e]) rotate([0, 0, -90])
                linear_extrude(1) text(text=label, size=7, font="Liberation Sans:style=Bold");
    }

    // Wall
    translate([units * gu + offset, 0, 0]) roundcube([sideWidth, gu, ru + gap + ledge + frontLip], cr);
    // Lip over the device
    translate([units * gu + offset - frontLip, 0, ru + gap + ledge]) roundcube([sideWidth + frontLip, gu, frontLip], cr);
}
