dimensions can now be specified in micrometers (um)

This commit is contained in:
Werner Almesberger 2016-12-31 17:46:39 -03:00
parent 0acff965b0
commit 688f2ab934
7 changed files with 167 additions and 30 deletions

3
README
View File

@ -136,7 +136,8 @@ Units
- - -
fped can calculate in mm and mil. Units are specified by following a
number with "mm" or "mil", separated by zero or more spaces or tabs.
number with "mm", "um", or "mil", separated by zero or more spaces or
tabs.
Examples:

10
coord.c
View File

@ -1,8 +1,8 @@
/*
* coord.c - Coordinate representation and basic operations
*
* Written 2009, 2010 by Werner Almesberger
* Copyright 2009, 2010 by Werner Almesberger
* Written 2009, 2010, 2016 by Werner Almesberger
* Copyright 2009, 2010, 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -32,6 +32,12 @@ double mil_to_mm(double mil, int exponent)
}
double um_to_mm(double um, int exponent)
{
return um*pow(UM_IN_MM, exponent);
}
/* ----- convert internal units to best external unit ---------------------- */

19
coord.h
View File

@ -1,8 +1,8 @@
/*
* coord.h - Coordinate representation and basic operations
*
* Written 2009, 2010 by Werner Almesberger
* Copyright 2009, 2010 by Werner Almesberger
* Written 2009, 2010, 2016 by Werner Almesberger
* Copyright 2009, 2010, 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,9 +20,11 @@
#define MICRON_UNITS 10
#define MIL_UNITS (25.4*MICRON_UNITS)
#define MM_UNITS (1000.0*MICRON_UNITS)
#define UM_UNITS MICRON_UNITS
#define KICAD_UNIT (MIL_UNITS/10.0)
#define MIL_IN_MM 0.0254
#define UM_IN_MM 0.001
typedef int32_t unit_type;
@ -48,12 +50,24 @@ static inline unit_type mm_to_units(double mm)
}
static inline unit_type um_to_units(double mm)
{
return mm*UM_UNITS;
}
static inline double units_to_mm(unit_type u)
{
return (double) u/MM_UNITS;
}
static inline double units_to_um(unit_type u)
{
return (double) u/UM_UNITS;
}
static inline double units_to_mil(unit_type u)
{
return (double) u/MIL_UNITS;
@ -74,6 +88,7 @@ static inline int coord_eq(struct coord a, struct coord b)
double mm_to_mil(double mm, int exponent);
double mil_to_mm(double mil, int exponent);
double um_to_mm(double um, int exponent);
double units_to_best(unit_type u, int *mm);

53
expr.c
View File

@ -1,8 +1,8 @@
/*
* expr.c - Expressions and values
*
* Written 2009, 2010, 2012 by Werner Almesberger
* Copyright 2009, 2010, 2012 by Werner Almesberger
* Written 2009, 2010, 2012, 2016 by Werner Almesberger
* Copyright 2009, 2010, 2012, 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -61,6 +61,9 @@ const char *str_unit(struct num n)
case nt_mm:
unit = "mm";
break;
case nt_um:
unit = "um";
break;
case nt_mil:
unit = "mil";
break;
@ -78,8 +81,8 @@ int to_unit(struct num *n)
{
if (!is_distance(*n)) {
fail("%s^%d is not a distance",
n->type == nt_mm ? "mm" : n->type == nt_mil ? "mil" : "?",
n->exponent);
n->type == nt_mm ? "mm" : n->type == nt_um ? "um" :
n->type == nt_mil ? "mil" : "?", n->exponent);
return 0;
}
switch (n->type) {
@ -89,6 +92,9 @@ int to_unit(struct num *n)
case nt_mm:
n->n = mm_to_units(n->n);
break;
case nt_um:
n->n = um_to_units(n->n);
break;
default:
abort();
}
@ -273,19 +279,32 @@ int var_eq(const struct frame *frame, const char *name,
/* ----- arithmetic -------------------------------------------------------- */
static void converge_to_mm(struct num *a)
{
switch (a->type) {
case nt_mil:
a->type = nt_mm;
a->n = mil_to_mm(a->n, a->exponent);
break;
case nt_um:
a->type = nt_mm;
a->n = um_to_mm(a->n, a->exponent);
break;
case nt_mm:
break;
default:
abort();
}
}
static struct num compatible_sum(struct num *a, struct num *b)
{
struct num res;
if (a->type != b->type) {
if (a->type == nt_mil) {
a->type = nt_mm;
a->n = mil_to_mm(a->n, a->exponent);
}
if (b->type == nt_mil) {
b->type = nt_mm;
b->n = mil_to_mm(b->n, a->exponent);
}
converge_to_mm(a);
converge_to_mm(b);
}
if (a->exponent != b->exponent) {
fail("incompatible exponents (%d, %d)",
@ -305,14 +324,8 @@ static struct num compatible_mult(struct num *a, struct num *b,
struct num res;
if (a->type != b->type) {
if (a->type == nt_mil) {
a->type = nt_mm;
a->n = mil_to_mm(a->n, a->exponent);
}
if (b->type == nt_mil) {
b->type = nt_mm;
b->n = mil_to_mm(b->n, b->exponent);
}
converge_to_mm(a);
converge_to_mm(b);
}
res.type = a->type;
res.exponent = exponent;

19
expr.h
View File

@ -1,8 +1,8 @@
/*
* expr.h - Expressions and values
*
* Written 2009, 2012 by Werner Almesberger
* Copyright 2009, 2012 by Werner Almesberger
* Written 2009, 2012, 2016 by Werner Almesberger
* Copyright 2009, 2012, 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,6 +27,7 @@ struct value;
enum num_type {
nt_none,
nt_mm,
nt_um,
nt_mil,
};
@ -63,7 +64,8 @@ extern struct num undef;
static inline int is_distance(struct num num)
{
return (num.type == nt_mm || num.type == nt_mil) && num.exponent == 1;
return (num.type == nt_mm || num.type == nt_um || num.type == nt_mil)
&& num.exponent == 1;
}
@ -94,6 +96,17 @@ static inline struct num make_mm(double mm)
}
static inline struct num make_um(double um)
{
struct num res;
res.type = nt_um;
res.exponent = 1;
res.n = um;
return res;
}
static inline struct num make_mil(double mil)
{
struct num res;

8
fpd.l
View File

@ -2,8 +2,8 @@
/*
* fpd.l - FootPrint Definition language
*
* Written 2009, 2010, 2012 by Werner Almesberger
* Copyright 2009, 2010, 2012 by Werner Almesberger
* Written 2009, 2010, 2012, 2016 by Werner Almesberger
* Copyright 2009, 2010, 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -164,6 +164,10 @@ SP [\t ]*
yylval.num.exponent = 1;
sscanf(yytext, "%lf", &yylval.num.n);
return NUMBER; }
{NUM}{SP}um { yylval.num.type = nt_um;
yylval.num.exponent = 1;
sscanf(yytext, "%lf", &yylval.num.n);
return NUMBER; }
{NUM}{SP}mil { yylval.num.type = nt_mil;
yylval.num.exponent = 1;
sscanf(yytext, "%lf", &yylval.num.n);

85
test/um Executable file
View File

@ -0,0 +1,85 @@
#!/bin/sh
. ./Common
###############################################################################
fped "um: iprint micrometers" <<EOF
%iprint 100um
EOF
expect <<EOF
100um
EOF
#------------------------------------------------------------------------------
fped "um: add mm + um" <<EOF
%iprint 1mm + 100 um
EOF
expect <<EOF
1.1mm
EOF
#------------------------------------------------------------------------------
fped "um: subtract mil - um" <<EOF
%iprint 100mil - 100 um
EOF
expect <<EOF
2.44mm
EOF
#------------------------------------------------------------------------------
fped "um: multiply um with um" <<EOF
%iprint 100um * 50um
EOF
expect <<EOF
5000um^2
EOF
#------------------------------------------------------------------------------
fped "um: multiply um with mm" <<EOF
%iprint 100um * 2mm
EOF
expect <<EOF
0.2mm^2
EOF
#------------------------------------------------------------------------------
fped "um: divide mil by um" <<EOF
%iprint 20mil / 10um
EOF
expect <<EOF
50.8
EOF
#------------------------------------------------------------------------------
fped_dump "um: use um in vector" <<EOF
vec @(100um, 50um)
EOF
expect <<EOF
/* MACHINE-GENERATED ! */
package "_"
unit mm
__0: vec @(100um, 50um)
EOF
#------------------------------------------------------------------------------
fped "um: measure distance in um" <<EOF
a: vec @(0mm, 0mm)
b: vec .(300um, 0mm)
c: vec .(0mm, 400um)
meas a >> c /* dummy */
m: meas a >> c
%meas m
EOF
expect <<EOF
0.5
EOF