xypwn / scadqr

Effortlessly generate QR codes directly in OpenSCAD! No extra dependencies!
MIT License
92 stars 9 forks source link

Output of qr.scad doesn't match the input #5

Closed dan-p3rry closed 8 months ago

dan-p3rry commented 8 months ago

In the following example, the '3' in the phone number becomes a 2 in QR code, and the ',' after Bardolph becomes -. It works OK with Medium error correction. One more observation -- Quartile error correction seems broken, at least my android phone cannot read it at all.

Thanks & regards, Dan

include <scadqr-main/qr.scad>

$fs = 0.2 + 0;
$fa = 4 + 0;

/* [Contact info] */
name = "Walt McGath";
address = "512 S Chestnut, Bardolph, IL 61416";
phone = "+1 309 555 1212";
email = "myemail@gmail.com";

namestr = name == "" ? "" : str("Name ", name);
addressstr = address == "" ? "" : str(" Address ", address);
phonestr = phone == "" ? "" : str(" Phone ", phone);
emailstr = email == "" ? "" : str(" email ", email);

text = str( namestr, addressstr, phonestr, emailstr );
//text = "https://makerworld.com/en/u/1256459432";
strlen = len(text);
echo(string_length = strlen);

assert(strlen <= 119, "MAX CHARACTERS = 119");

// Width of the QR code
width = 50 + 0; // [1:1000]
// Height of the QR code
height = 50 + 0; // [1:1000]
// Thickness of the QR code
thickness = 0.4 + 0; // [1:50]
// Place the QR code in the center
center = 1 + 0; // [0:false, 1:true]

tag_thickness = 2 + 0;

/* [Hidden] */
qrcode_type = "text";
//* [QR code parameters (advanced)] */
// Error correction level
error_correction = "H"; // [L:"Low (~7%)", M:"Medium (~15%)", Q: "Quartile (~25%)", H: "High (~30%)"]
// Mask pattern
mask_pattern = 0; // [0:"000: (y + x)%2=0", 1:"001: y % 2=0", 2:"010: x % 3=0", 3:"011: (y + x)%3=0", 4:"100: (y/2 + x/3)%2=0", 5:"101: (y*x)%2+(y*x)%3=0", 6:"110: ((y*x)%3+y*x)%2=0", 7:"111: ((y*x)%3+y+x)%2=0"]
// Character encoding
encoding = "UTF-8"; // [ UTF-8:"UTF-8 (Unicode)", Shift_JIS:"Shift JIS (Shift Japanese International Standards)"]

luggage_tag();

module luggage_tag() {
    color("white") tag();
    color("black") {
        translate([0, 0, tag_thickness]) {
            qr(text, error_correction, width, height, thickness, center, mask_pattern, encoding);
            linear_extrude(thickness) {
                translate([0.5*width + 4, 0]) rotate([0, 0, 270])
                    text(str("Return lost luggage to:"), 
                        font = "Arial:style=Bold", size = 3.2, halign = "center");
            }
        }
    }
}
tag_w = width + 4;

module tag() {
    corner_rad = 3.0;
    xy_coord = tag_w/2 - corner_rad;
    difference() {
        hull() {
            translate([-xy_coord, xy_coord, 0]) cylinder(h = tag_thickness, r = corner_rad);
            translate([xy_coord + 10, xy_coord, 0]) cylinder(h = tag_thickness, r = corner_rad);
            // add a piece for a strap
            translate([xy_coord + 20, 15]) cylinder(h = tag_thickness, r = corner_rad);
            translate([xy_coord + 20, -15]) cylinder(h = tag_thickness, r = corner_rad);

            translate([xy_coord + 10, -xy_coord, 0]) cylinder(h = tag_thickness, r = corner_rad);
            translate([-xy_coord, -xy_coord, 0]) cylinder(h = tag_thickness, r = corner_rad);
        }
        // cut a hole for a strap
        hull() {
            translate([xy_coord + 15, 8, -1]) cylinder(h = 4.0, r = 2.5);
            translate([xy_coord + 15, -8, -1]) cylinder(h = 4.0, r = 2.5);
        }
    }
}
xypwn commented 8 months ago

I can reproduce both issues (not scanning with Q and wrong chars with H). Looking at which characters got changed, it seems in both cases the least significant bit must have gotten flipped, either due to an error in the data, or the error correction ('3' = 00110011 -> '2' = 00110010; ',' = 00101100 -> '-' = 00101101).

xypwn commented 8 months ago

After a lot of debugging, it turns out I messed up some of the error correction math. It should have now been fixed in 53090f1 :)

dan-p3rry commented 8 months ago

Excellent, I hope you'll update your regression testing as well 😀. Dan

On Mon, Feb 26, 2024 at 6:58 PM xypwn @.***> wrote:

After a lot of debugging, it turns out I messed up some of the error correction math. It should have now been fixed in 53090f1 https://github.com/xypwn/scadqr/commit/53090f1fd7a67a31824f25b185d1b66676c66f9a :)

— Reply to this email directly, view it on GitHub https://github.com/xypwn/scadqr/issues/5#issuecomment-1964868681, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVJPTT26L2PY4BMP7XJCY7LYVTLMXAVCNFSM6AAAAABD2GGBEOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRUHA3DQNRYGE . You are receiving this because you authored the thread.Message ID: @.***>