Date:2016-12-31 21:46:39 (2 years 5 months ago)
Author:Werner Almesberger
Commit:688f2ab9345ca377690cce88dda52fe2cd0ec1ff
Message:dimensions can now be specified in micrometers (um)

Files: README (1 diff)
coord.c (2 diffs)
coord.h (4 diffs)
expr.c (6 diffs)
expr.h (4 diffs)
fpd.l (2 diffs)
test/um (1 diff)

Change Details

README
136136- - -
137137
138138fped can calculate in mm and mil. Units are specified by following a
139number with "mm" or "mil", separated by zero or more spaces or tabs.
139number with "mm", "um", or "mil", separated by zero or more spaces or
140tabs.
140141
141142Examples:
142143
coord.c
11/*
22 * coord.c - Coordinate representation and basic operations
33 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 by Werner Almesberger
4 * Written 2009, 2010, 2016 by Werner Almesberger
5 * Copyright 2009, 2010, 2016 by Werner Almesberger
66 *
77 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
...... 
3232}
3333
3434
35double um_to_mm(double um, int exponent)
36{
37    return um*pow(UM_IN_MM, exponent);
38}
39
40
3541/* ----- convert internal units to best external unit ---------------------- */
3642
3743
coord.h
11/*
22 * coord.h - Coordinate representation and basic operations
33 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 by Werner Almesberger
4 * Written 2009, 2010, 2016 by Werner Almesberger
5 * Copyright 2009, 2010, 2016 by Werner Almesberger
66 *
77 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
...... 
2020#define MICRON_UNITS 10
2121#define MIL_UNITS (25.4*MICRON_UNITS)
2222#define MM_UNITS (1000.0*MICRON_UNITS)
23#define UM_UNITS MICRON_UNITS
2324#define KICAD_UNIT (MIL_UNITS/10.0)
2425
2526#define MIL_IN_MM 0.0254
27#define UM_IN_MM 0.001
2628
2729
2830typedef int32_t unit_type;
...... 
4850}
4951
5052
53static inline unit_type um_to_units(double mm)
54{
55    return mm*UM_UNITS;
56}
57
58
5159static inline double units_to_mm(unit_type u)
5260{
5361    return (double) u/MM_UNITS;
5462}
5563
5664
65static inline double units_to_um(unit_type u)
66{
67    return (double) u/UM_UNITS;
68}
69
70
5771static inline double units_to_mil(unit_type u)
5872{
5973    return (double) u/MIL_UNITS;
...... 
7488
7589double mm_to_mil(double mm, int exponent);
7690double mil_to_mm(double mil, int exponent);
91double um_to_mm(double um, int exponent);
7792
7893double units_to_best(unit_type u, int *mm);
7994
expr.c
11/*
22 * expr.c - Expressions and values
33 *
4 * Written 2009, 2010, 2012 by Werner Almesberger
5 * Copyright 2009, 2010, 2012 by Werner Almesberger
4 * Written 2009, 2010, 2012, 2016 by Werner Almesberger
5 * Copyright 2009, 2010, 2012, 2016 by Werner Almesberger
66 *
77 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
...... 
6161    case nt_mm:
6262        unit = "mm";
6363        break;
64    case nt_um:
65        unit = "um";
66        break;
6467    case nt_mil:
6568        unit = "mil";
6669        break;
...... 
7881{
7982    if (!is_distance(*n)) {
8083        fail("%s^%d is not a distance",
81            n->type == nt_mm ? "mm" : n->type == nt_mil ? "mil" : "?",
82            n->exponent);
84            n->type == nt_mm ? "mm" : n->type == nt_um ? "um" :
85            n->type == nt_mil ? "mil" : "?", n->exponent);
8386        return 0;
8487    }
8588    switch (n->type) {
...... 
8992    case nt_mm:
9093        n->n = mm_to_units(n->n);
9194        break;
95    case nt_um:
96        n->n = um_to_units(n->n);
97        break;
9298    default:
9399        abort();
94100    }
...... 
273279/* ----- arithmetic -------------------------------------------------------- */
274280
275281
282static void converge_to_mm(struct num *a)
283{
284    switch (a->type) {
285    case nt_mil:
286        a->type = nt_mm;
287        a->n = mil_to_mm(a->n, a->exponent);
288        break;
289    case nt_um:
290        a->type = nt_mm;
291        a->n = um_to_mm(a->n, a->exponent);
292        break;
293    case nt_mm:
294        break;
295    default:
296        abort();
297    }
298}
299
300
276301static struct num compatible_sum(struct num *a, struct num *b)
277302{
278303    struct num res;
279304
280305    if (a->type != b->type) {
281        if (a->type == nt_mil) {
282            a->type = nt_mm;
283            a->n = mil_to_mm(a->n, a->exponent);
284        }
285        if (b->type == nt_mil) {
286            b->type = nt_mm;
287            b->n = mil_to_mm(b->n, a->exponent);
288        }
306        converge_to_mm(a);
307        converge_to_mm(b);
289308    }
290309    if (a->exponent != b->exponent) {
291310        fail("incompatible exponents (%d, %d)",
...... 
305324    struct num res;
306325
307326    if (a->type != b->type) {
308        if (a->type == nt_mil) {
309            a->type = nt_mm;
310            a->n = mil_to_mm(a->n, a->exponent);
311        }
312        if (b->type == nt_mil) {
313            b->type = nt_mm;
314            b->n = mil_to_mm(b->n, b->exponent);
315        }
327        converge_to_mm(a);
328        converge_to_mm(b);
316329    }
317330    res.type = a->type;
318331    res.exponent = exponent;
expr.h
11/*
22 * expr.h - Expressions and values
33 *
4 * Written 2009, 2012 by Werner Almesberger
5 * Copyright 2009, 2012 by Werner Almesberger
4 * Written 2009, 2012, 2016 by Werner Almesberger
5 * Copyright 2009, 2012, 2016 by Werner Almesberger
66 *
77 * This program is free software; you can redistribute it and/or modify
88 * it under the terms of the GNU General Public License as published by
...... 
2727enum num_type {
2828    nt_none,
2929    nt_mm,
30    nt_um,
3031    nt_mil,
3132};
3233
...... 
6364
6465static inline int is_distance(struct num num)
6566{
66    return (num.type == nt_mm || num.type == nt_mil) && num.exponent == 1;
67    return (num.type == nt_mm || num.type == nt_um || num.type == nt_mil)
68        && num.exponent == 1;
6769}
6870
6971
...... 
9496}
9597
9698
99static inline struct num make_um(double um)
100{
101    struct num res;
102
103    res.type = nt_um;
104    res.exponent = 1;
105    res.n = um;
106    return res;
107}
108
109
97110static inline struct num make_mil(double mil)
98111{
99112    struct num res;
fpd.l
22/*
33 * fpd.l - FootPrint Definition language
44 *
5 * Written 2009, 2010, 2012 by Werner Almesberger
6 * Copyright 2009, 2010, 2012 by Werner Almesberger
5 * Written 2009, 2010, 2012, 2016 by Werner Almesberger
6 * Copyright 2009, 2010, 2016 by Werner Almesberger
77 *
88 * This program is free software; you can redistribute it and/or modify
99 * it under the terms of the GNU General Public License as published by
...... 
164164                  yylval.num.exponent = 1;
165165                  sscanf(yytext, "%lf", &yylval.num.n);
166166                  return NUMBER; }
167{NUM}{SP}um { yylval.num.type = nt_um;
168                  yylval.num.exponent = 1;
169                  sscanf(yytext, "%lf", &yylval.num.n);
170                  return NUMBER; }
167171{NUM}{SP}mil { yylval.num.type = nt_mil;
168172                  yylval.num.exponent = 1;
169173                  sscanf(yytext, "%lf", &yylval.num.n);
test/um
1#!/bin/sh
2. ./Common
3
4###############################################################################
5
6fped "um: iprint micrometers" <<EOF
7%iprint 100um
8EOF
9expect <<EOF
10100um
11EOF
12
13#------------------------------------------------------------------------------
14
15fped "um: add mm + um" <<EOF
16%iprint 1mm + 100 um
17EOF
18expect <<EOF
191.1mm
20EOF
21
22#------------------------------------------------------------------------------
23
24fped "um: subtract mil - um" <<EOF
25%iprint 100mil - 100 um
26EOF
27expect <<EOF
282.44mm
29EOF
30
31#------------------------------------------------------------------------------
32
33fped "um: multiply um with um" <<EOF
34%iprint 100um * 50um
35EOF
36expect <<EOF
375000um^2
38EOF
39
40#------------------------------------------------------------------------------
41
42fped "um: multiply um with mm" <<EOF
43%iprint 100um * 2mm
44EOF
45expect <<EOF
460.2mm^2
47EOF
48
49#------------------------------------------------------------------------------
50
51fped "um: divide mil by um" <<EOF
52%iprint 20mil / 10um
53EOF
54expect <<EOF
5550.8
56EOF
57
58#------------------------------------------------------------------------------
59
60fped_dump "um: use um in vector" <<EOF
61vec @(100um, 50um)
62EOF
63expect <<EOF
64/* MACHINE-GENERATED ! */
65
66package "_"
67unit mm
68
69__0: vec @(100um, 50um)
70EOF
71
72#------------------------------------------------------------------------------
73
74fped "um: measure distance in um" <<EOF
75a: vec @(0mm, 0mm)
76b: vec .(300um, 0mm)
77c: vec .(0mm, 400um)
78meas a >> c /* dummy */
79m: meas a >> c
80
81%meas m
82EOF
83expect <<EOF
840.5
85EOF

Archive Download the corresponding diff file

Branches:
master



interactive