Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cylindrical placement for key legends #199

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/legends_dish.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include <../includes.scad>

/* here's how to use the legends() command.
The first argument is the legend itself, which can also be a whole string.
The second argument is the "position" of the legend relative to center.
Legends currently have to all be inset or outset at the same time, but you
can have as many of them as you want.
The numbers used are some magic constant, so just fudge them until it looks good.
*/

/* $outset_legends = true; */
legends = ["A", "b", "c", "uwu"];

$font_size = 4;

for (x=[0,1,2,3,4]) {
for(y=[1,2,3])
translate_u(y * y / 2,x) {
u(y) cherry_row(x)
legend(legends[0], [-1,-1]) {
legend(legends[1], [1,-1]) {
legend(legends[2], [-1,1]) {
legend(legends[3], [-.8,0]) {
key();
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/dishes/cylindrical.scad
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module cylindrical_dish(width, height, depth, inverted){
* the width of the keycap.
*/
// the distance you have to move the dish so it digs in depth millimeters
chord_length = (pow(width, 2) - 4 * pow(depth, 2)) / (8 * depth);
chord_length = cylindrical_dish_chord_length(width=width, depth=depth);
//the radius of the dish
rad = (pow(width, 2) + 4 * pow(depth, 2)) / (8 * depth);
rad = cylindrical_dish_radius(width=width, depth=depth);
direction = inverted ? -1 : 1;

translate([0,0, chord_length * direction]){
Expand Down
48 changes: 45 additions & 3 deletions src/features/legends.scad
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use <libraries/text_on.scad>

module keytext(text, position, font_size, depth) {
woffset = (top_total_key_width()/3.5) * position[0];
hoffset = (top_total_key_height()/3.5) * -position[1];
Expand All @@ -8,6 +10,39 @@ module keytext(text, position, font_size, depth) {
}
}

module keytext_cylindrical(text, position, font_size, depth) {
woffset = (top_total_key_width()/3.5) * position[0];
hoffset = (top_total_key_height()/3.5) * -position[1] - font_size/2;
// copied from module _dish()
dish_width = top_total_key_width() + $dish_overdraw_width;
dish_height = top_total_key_height() + $dish_overdraw_height;
dish_rad = cylindrical_dish_radius(width=dish_width, depth=$dish_depth);
// XXX inverted legends
chord_length = cylindrical_dish_chord_length(width=dish_width, depth=$dish_depth);
dish_circumference = 6.283 * dish_rad;
theta = woffset * 360 / dish_circumference;
echo("dish radius =", dish_rad, "woffset = ", woffset, "theta =", theta);
translate([$dish_offset_x,0,chord_length]) {
mirror([1,0,0])
rotate(180)
rotate([90,0,0])
color($tertiary_color) {
text_on_cylinder(t=text,
r=dish_rad-.1,
h=top_total_key_height()/3.5,
updown=hoffset,
eastwest=theta,
face=$font, size=font_size,
// unsupported: halign="center", valign="center",
center=false,
cylinder_center=true,
extrusion_center=true,
extrusion_height = 2*depth,
rotate=0);
}
}
}

module legends(depth=0) {
if (len($front_legends) > 0) {
front_of_key() {
Expand All @@ -17,9 +52,16 @@ module legends(depth=0) {
}
}
if (len($legends) > 0) {
top_of_key() {
for (i=[0:len($legends)-1]) {
keytext($legends[i][0], $legends[i][1], $legends[i][2], depth);
if($dish_type == "cylindrical"){
top_placement()
for (i=[0:len($legends)-1]) {
keytext_cylindrical($legends[i][0], $legends[i][1], $legends[i][2], depth);
}
} else {
top_of_key() {
for (i=[0:len($legends)-1]) {
keytext($legends[i][0], $legends[i][1], $legends[i][2], depth);
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/functions.scad
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ function extra_keytop_length_for_flat_sides() = ($width_difference * vertical_in
function add_rounding(p, radius)=[for(i=[0:len(p)-1])[p[i].x,p[i].y, radius]];
// computes millimeter length from unit length
function unit_length(length) = $unit * (length - 1) + 18.16;

/* we do some funky math here
* basically you want to have the dish "dig in" to the keycap x millimeters
* in order to do that you have to solve a small (2d) system of equations
* where the chord of the spherical cross section of the dish is
* the width of the keycap.
*/
// the distance you have to move the dish so it digs in depth millimeters
function cylindrical_dish_chord_length(width, depth) =
(pow(width, 2) - 4 * pow(depth, 2)) / (8 * depth);

//the radius of the dish
function cylindrical_dish_radius(width, depth) =
(pow(width, 2) + 4 * pow(depth, 2)) / (8 * depth);
Loading