feat: support alphabets smaller than 26, automatically determine suitable font size

This commit is contained in:
s3lph 2025-03-30 13:13:20 +02:00
parent 50f1def8d5
commit bc6c4822eb
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 18 additions and 11 deletions

View file

@ -1,25 +1,26 @@
module key_dial(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", key="A", width=7, dia=50, thick=5.5, key_width=4, key_height=3, text_depth=1, text_width=4, antipick=1.5, tolerance = 0.2, $fn = 60) {
module key_dial(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", key="C", width=7, dia=50, thick=5.5, key_width=4, key_height=3, text_depth=1, font_size=-1, antipick=1.5, tolerance = 0.2, $fn = 60) {
key_indices = search(key, alphabet, 0);
assert(len(key_indices[0]) == 1, "key character not unique");
key_index = key_indices[0][0];
_font_size = font_size > 0 ? font_size : min(dia*PI/len(alphabet)/1.6, dia/10);
rotate([-0.5/len(alphabet)*360,0,0]) {
difference() {
rotate([0,90,0]) {
cylinder($fn=len(alphabet), h=width, d=dia, center=true);
cylinder($fn=$fn, h=width, d=dia, center=true);
}
// inner ring
rotate([0,90,0]) {
cylinder($fn=60, h=width+2, d=dia-thick*2, center=true);
cylinder($fn=$fn, h=width+2, d=dia-thick*2, center=true);
}
// key
rotate([((0.5)/len(alphabet)) * 360,0,0]) {
rotate([((0.5+key_index)/len(alphabet)) * 360,0,0]) {
translate([0,0,dia/2-thick]) {
cube([width+2, key_width, key_height*2], center=true);
}
}
// anti-picking keys
for (i = [1:len(alphabet)-1]) {
for (i = [1:len(alphabet)]) {
rotate([((i+0.5)/len(alphabet)) * 360,0,0]) {
translate([width/2,0,dia/2-thick]) {
cube([2*antipick, key_width, key_height*2], center=true);
@ -29,9 +30,12 @@ module key_dial(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", key="A", width=7, dia=50,
// text
for (i = [0:len(alphabet)]) {
rotate([((i+0.5)/len(alphabet)) * 360,0,0]) {
translate([0,0,dia/2-text_depth]) {
translate([-width/2-1,-dia/2,dia/2-0.3]) {
cube([width+2,dia,5]);
}
translate([0,0,dia/2-text_depth-0.3]) {
linear_extrude(height=text_depth+1, convexity=3) {
text(alphabet[(i+key_index)%len(alphabet)], size=text_width, halign="center", valign="center");
text(alphabet[i], size=_font_size, halign="center", valign="center");
}
}
}
@ -44,10 +48,10 @@ module key_dial(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", key="A", width=7, dia=50,
// DEFAULTS FOR COMMAND LINE INVOCATION
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
KEY = "A";
KEY = "C";
DIAMETER = 50;
TOLERANCE = 0.2;
FN = 60;
FN = 120;
INDEX = 0;
rotate([0,-90,0]) {

View file

@ -5,12 +5,15 @@ use <case.scad>;
use <plug.scad>;
module lock(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", password="CHAOS", text_width=4, diameter=50, tolerance=0.2, $fn= 60) {
module lock(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", password="CHAOS", diameter=50, tolerance=0.2, $fn= 60) {
n = len(password);
key_indices=search(password, alphabet);
for (i = [0:n-1]) {
translate([i*10,0,0]) key_dial(alphabet, key=password[i], dia=diameter, text_width=text_width);
rotate([-key_indices[i]/len(alphabet)*360,0,0]) {
translate([i*10,0,0]) key_dial(alphabet, key=password[i], dia=diameter);
}
}
for (i = [0:n]) {