$fn = 20;

baseplate_thickness = 2;
inner_clearance = 6.5; // space inside the case for the battery/mcu to occupy
pcb_thickness = 1.6; // thickness of the pcb

module roundsquare (size, r) {
	translate([r, r]) minkowski() {
		square([size.x-2*r, size.y-2*r]);
		circle(r=r);
	}
}

module shell () {
	difference() {
		// block
		linear_extrude(baseplate_thickness + inner_clearance + pcb_thickness) {
			translate([-1, -1]) roundsquare([214+1*2, 67+1*2], r=2+1);
		}
		// cut for shelf
		translate([0, 0, baseplate_thickness + inner_clearance]) linear_extrude(pcb_thickness + 2) {
			roundsquare([214, 67], r=2-0.25); // pcb r=2 but it's not exact
		}
		// cut for inner clearance
		translate([0, 0, baseplate_thickness]) linear_extrude(inner_clearance + 10) {
			translate([1, 1]) roundsquare([214 - 1 * 2, 67 - 1 * 2], r=1);
		}
	}
}

module standoffs () {
	// for M2 3x3 standoffs
	hole_diameter = 3;
	hole_depth = 3 * 1.5;
	wall_thickness = 1;

	// offsets the top of the standoffs down from where the pcb will actually rest; this allows the screws to actually hold the pcb down against the outer lip of the case and prevent rattling if the pcb is not perfectly flat
	standoff_top_face_offset = -0.25;

	for (point = [
		[17 + 18 * 0, 16.5 + 17 * 0],
		[17 + 18 * 2, 16.5 + 17 * 2],
		[17 + 18 * 4, 16.5 + 17 * 0],
		[17 + 18 * 5, 16.5 + 17 * 2],
		[17 + 18 * 6, 16.5 + 17 * 0],
		[17 + 18 * 8, 16.5 + 17 * 2],
		[17 + 18 * 10, 16.5 + 17 * 0],
	]) {
		translate([point.x, point.y, baseplate_thickness - 0.01]) difference() {
			cylinder(h = inner_clearance + standoff_top_face_offset + .01, d = hole_diameter + 2*wall_thickness);
			translate([0, 0, inner_clearance + standoff_top_face_offset + .1 - hole_depth]) cylinder(h = hole_depth + .02, d = hole_diameter); // hole inner - adjust for insert size
		}
	}
}

difference() {
	shell();
	translate([2, 65-.01, baseplate_thickness])
//		cube([34, 2, 30]);
//		cube([34, 5, 6]);
		cube([18+17-2, 2, 30]);
	// usb-c cutout
	translate([17 + 18/2 - 9/2, 71, baseplate_thickness + inner_clearance - 6.5]) {
		rotate([90, 0, 0]) linear_extrude(8) {
			roundsquare([9, 3.75], r = 1.5);
		}
	}
	// power switch cutout
	translate([4, 71, baseplate_thickness + inner_clearance - 2]) {
		rotate([90, 0, 0]) linear_extrude(8) {
			roundsquare([6.5, 2], r = 0.5);
		}
	}
}

standoffs();
