The Smell of Molten Projects in the Morning

Ed Nisley's Blog: Shop notes, electronics, firmware, machinery, 3D printing, laser cuttery, and curiosities. Contents: 100% human thinking, 0% AI slop.

Category: Software

General-purpose computers doing something specific

  • Bathroom Door Retainer

    The weather got warm enough to open the windows before pollen season started, which led to the front bathroom door slamming closed in the middle of the night when a gusty rainstorm blew through town. After far too many years, I decided this was an annoyance up with which I need no longer put.

    A few minutes with OpenSCAD and Slic3r produces the shape:

    Bathroom Door Retainer - Slic3r
    Bathroom Door Retainer – Slic3r

    It’s basically an extrusion of a 2D shape with a rectangular recess for the door chewed out.

    An hour later, it’s in full effect:

    Bathroom Door Retainer - installed
    Bathroom Door Retainer – installed

    The model now sports a little ball to secure the retainer against the towel bar:

    Bathroom Door Retainer - bump
    Bathroom Door Retainer – bump

    Maybe someday I’ll reprint it.

    That was easy …

    The cast-iron pig sometimes standing guard as a doorstop in the relatively narrow doorway poses a bit of a foot hazard, so he moves into a closet during the off season. He can now remain there, snug and comfy, until a need for ballast arises.

    The OpenSCAD source code as a GitHub Gist:

    // Bathroom Door Retainer
    // Ed Nisley KE4ZNU – May 2017
    Layout = "Show"; // Show Build
    //——-
    //- Extrusion parameters must match reality!
    ThreadThick = 0.20;
    ThreadWidth = 0.40;
    HoleWindage = 0.2;
    Protrusion = 0.1; // make holes end cleanly
    function IntegerMultiple(Size,Unit) = Unit * ceil(Size / Unit);
    //——-
    // Dimensions
    TowelBarSide = 20.5; // towel bar across flat side
    TowelBarAngle = 45; // rotation of top flat from horizontal
    DoorOffset = 16.0; // from towel bar to door
    DoorThick = 36.5;
    WallThick = 4.0; // minimum wall thickness
    RetainerDepth = 10.0; // thickness of retaining notch
    NumSides = 6*4;
    CornerRad = WallThick;
    BarClipOD = TowelBarSide*sqrt(2) + 2*WallThick;
    BarClipRad = BarClipOD/2;
    OAH = RetainerDepth + WallThick;
    module LatchPlan() {
    union() {
    linear_extrude(height=OAH,convexity=4)
    difference() {
    union() {
    circle(d=BarClipOD,$fn=NumSides);
    hull()
    for (i=[0,1], j=[0,1])
    translate([i*(BarClipRad + DoorOffset + DoorThick + WallThick – CornerRad),j*(BarClipRad – CornerRad)])
    circle(r=CornerRad,$fn=4*4);
    }
    rotate(TowelBarAngle) // towel bar shape
    square(size=TowelBarSide,center=true);
    translate([0,-TowelBarSide/sqrt(2)]) // make access slot
    rotate(-TowelBarAngle)
    square(size=[2*TowelBarSide,TowelBarSide],center=false);
    }
    translate([0,-TowelBarSide/sqrt(2),OAH/2])
    rotate([90,0,45])
    sphere(r=TowelBarSide/25,$fn=4*3);
    }
    }
    module Latch() {
    difference() {
    LatchPlan();
    translate([BarClipRad + DoorOffset,-BarClipRad/2,-Protrusion])
    cube([DoorThick,BarClipOD,RetainerDepth + Protrusion],center=false);
    }
    }
    //——-
    // Build it!
    if (Layout == "Show") {
    Latch();
    }
    if (Layout == "Build") {
    translate([0,0,OAH])
    rotate([180,0,0])
    Latch();
    }
  • Arduino vs. Significant Figures: Useful 64-bit Fixed Point

    Devoting eight bytes to every fixed point number may be excessive, but having nine significant figures apiece for the integer and fraction parts pushes the frequency calculations well beyond the limits of the DDS hardware, without involving any floating point library routines. This chunk of code performs a few more calculations using the format laid out earlier and explores a few idioms that may come in handy later.

    Rounding the numbers to a specific number of decimal places gets rid of the repeating-digit problem that turns 0.10 into 0.099999:

    uint64_t RoundFixedPt(union ll_u TheNumber,unsigned Decimals) {
    union ll_u Rnd;
    
      Rnd.fx_64 = (One.fx_64 / 2) / (pow(10LL,Decimals));
      TheNumber.fx_64 = TheNumber.fx_64 + Rnd.fx_64;
      return TheNumber.fx_64;
    }
    

    That pretty well trashes the digits beyond the rounded place, so you shouldn’t display any more of them:

    void PrintFixedPtRounded(char *pBuffer,union ll_u FixedPt,unsigned Decimals) {
    char *pDecPt;
    
      FixedPt.fx_64 = RoundFixedPt(FixedPt,Decimals);
    
      PrintIntegerLL(pBuffer,FixedPt);  // do the integer part
    
      pBuffer += strlen(pBuffer);       // aim pointer beyond integer
    
      pDecPt = pBuffer;                 // save the point location
      *pBuffer++ = '.';                 // drop in the decimal point, tick pointer
    
      PrintFractionLL(pBuffer,FixedPt);
    
      if (Decimals == 0)
        *pDecPt = 0;                    // 0 places means discard the decimal point
      else
        *(pDecPt + Decimals + 1) = 0;   // truncate string to leave . and Decimals chars
    }
    

    Which definitely makes the numbers look prettier:

      Tenth.fx_64 = One.fx_64 / 10;             // Likewise, 0.1
      PrintFixedPt(Buffer,Tenth);
      printf("\n0.1: %s\n",Buffer);
      PrintFixedPtRounded(Buffer,Tenth,9);                    // show rounded value
      printf("0.1 to 9 dec: %s\n",Buffer);
    
      TestFreq.fx_64 = RoundFixedPt(Tenth,3);                 // show full string after rounding
      PrintFixedPt(Buffer,TestFreq);
      printf("0.1 to 3 dec: %s (full string)\n",Buffer);
    
      PrintFixedPtRounded(Buffer,Tenth,3);                    // show truncated string with rounded value
      printf("0.1 to 3 dec: %s (truncated string)\n",Buffer);
    
    0.1: 0.099999999
    0.1 to 9 dec: 0.100000000
    0.1 to 3 dec: 0.100499999 (full string)
    0.1 to 3 dec: 0.100 (truncated string)
    
      CtPerHz.fx_64 = -1;                       // Set up 2^32 - 1, which is close enough
      CtPerHz.fx_64 /= 125 * MEGA;              // divide by nominal oscillator
      PrintFixedPt(Buffer,CtPerHz);
      printf("\nCt/Hz = %s\n",Buffer);
    
      printf("Rounding: \n");
      for (int d = 9; d >= 0; d--) {
        PrintFixedPtRounded(Buffer,CtPerHz,d);
        printf("     %d: %s\n",d,Buffer);
      }
    
    Ct/Hz = 34.359738367
    Rounding:
         9: 34.359738368
         8: 34.35973837
         7: 34.3597384
         6: 34.359738
         5: 34.35974
         4: 34.3597
         3: 34.360
         2: 34.36
         1: 34.4
         0: 34
    

    Multiplying two scaled 64-bit fixed-point numbers should produce a 128-bit result. For all the values we (well, I) care about, the product will fit into a 64-bit result, because the integer parts will always multiply out to less than 232 and we don’t care about more than 32 bits of fraction. This function multiplies two fixed point numbers of the form a.b × c.d by adding up the partial products thusly: ac + bd + ad + bc. The product of the integers ac won’t overflow 32 bits, the cross products ad and bc will always be slightly less than their integer factors, and the fractional product bd will always be less than 1.0.

    Soooo, just multiply ’em out as 64-bit integers, shift the products around to align the appropriate parts, and add up the pieces:

    
    uint64_t MultiplyFixedPt(union ll_u Mcand, union ll_u Mplier) {
    union ll_u Result;
    
      Result.fx_64  = ((uint64_t)Mcand.fx_32.high * (uint64_t)Mplier.fx_32.high) << 32; // integer parts (clear fract) 
      Result.fx_64 += ((uint64_t)Mcand.fx_32.low * (uint64_t)Mplier.fx_32.low) >> 32;   // fraction parts (always < 1)
      Result.fx_64 += (uint64_t)Mcand.fx_32.high * (uint64_t)Mplier.fx_32.low;          // cross products
      Result.fx_64 += (uint64_t)Mcand.fx_32.low * (uint64_t)Mplier.fx_32.high;
    
      return Result.fx_64;
    }
    

    This may be a useful way to set magic numbers with a few decimal places, although it does require keeping the decimal point in mind:

      TestFreq.fx_64 = (599999LL * One.fx_64) / 10;           // set 59999.9 kHz differently
      PrintFixedPt(Buffer,TestFreq);
      printf("\nTest frequency: %s\n",Buffer);
      PrintFixedPtRounded(Buffer,TestFreq,1);
      printf("         round: %s\n",Buffer);
    
    Test frequency: 59999.899999999
             round: 59999.9
    

    Contrary to what I thought, computing the CtPerHz coefficient doesn’t require pre-dividing both 232 and the oscillator by 2, thus preventing the former from overflowing a 32 bit integer. All you do is knock the numerator down by one little itty bitty count you’ll never notice:

      CtPerHz.fx_64 = -1;                       // Set up 2^32 - 1, which is close enough
      CtPerHz.fx_64 /= 125 * MEGA;              // divide by nominal oscillator
      PrintFixedPt(Buffer,CtPerHz);
      printf("\nCt/Hz = %s\n",Buffer);
    
    Ct/Hz = 34.359738367
    

    That’s also the largest possible fixed-point number, because unsigned:

      TempFX.fx_64 = -1;
      PrintFixedPt(Buffer,TempFX);
      printf("Max fixed point: %s\n",Buffer);
    
    Max fixed point: 4294967295.999999999
    

    With nine.nine significant figures in the mix, tweaking the 125 MHz oscillator to within 2 Hz will work:

    Oscillator tune: CtPerHz
     Oscillator: 125000000.00
     -10 -> 34.359741116
      -9 -> 34.359741116
      -8 -> 34.359740566
      -7 -> 34.359740566
      -6 -> 34.359740017
      -5 -> 34.359740017
      -4 -> 34.359739467
      -3 -> 34.359739467
      -2 -> 34.359738917
      -1 -> 34.359738917
      +0 -> 34.359738367
      +1 -> 34.359738367
      +2 -> 34.359737818
      +3 -> 34.359737818
      +4 -> 34.359737268
      +5 -> 34.359737268
      +6 -> 34.359736718
      +7 -> 34.359736718
      +8 -> 34.359736168
      +9 -> 34.359736168
     +10 -> 34.359735619
    

    So, all in all, this looks good. The vast number of strings in the test program bulk it up beyond reason, but in actual practice I think the code will be smaller than the equivalent floating point version, with more significant figures. Speed isn’t an issue either way, because the delays waiting for the crystal tester to settle down at each frequency step should be larger than any possible computation.

    The results were all verified with my trusty HP 50g and HP-15C calculators, both of which wipe the floor with any other way of handling mixed binary / hex / decimal arithmetic. If you do bit-wise calculations, even on an irregular basis, get yourself a SwissMicro DM16L; you can thank me later.

    The Arduino source code as a GitHub Gist:

    // Fixed point exercise for 60 kHz crystal tester
    #include <avr/pgmspace.h>
    char Buffer[10+1+10+1]; // string buffer for long long conversions
    #define GIGA 1000000000LL
    #define MEGA 1000000LL
    #define KILO 1000LL
    struct ll_fx {
    uint32_t low;
    uint32_t high;
    };
    union ll_u {
    uint64_t fx_64;
    struct ll_fx fx_32;
    };
    union ll_u CtPerHz; // will be 2^32 / 125 MHz
    union ll_u HzPerCt; // will be 125 MHz / 2^32
    union ll_u One; // 1.0 as fixed point
    union ll_u Tenth; // 0.1 as fixed point
    union ll_u TenthHzCt; // 0.1 Hz in counts
    union ll_u Oscillator; // nominal oscillator frequency
    union ll_u OscOffset; // oscillator calibration offset
    union ll_u TestFreq,TestCount; // useful variables
    union ll_u TempFX;
    //———–
    // Round scaled fixed point to specific number of decimal places: 0 through 8
    // You should display the value with only Decimals characters beyond the point
    // Must calculate rounding value as separate variable to avoid mystery error
    uint64_t RoundFixedPt(union ll_u TheNumber,unsigned Decimals) {
    union ll_u Rnd;
    // printf(" round before: %08lx %08lx\n",TheNumber.fx_32.high,TheNumber.fx_32.low);
    Rnd.fx_64 = (One.fx_64 / 2) / (pow(10LL,Decimals));
    // printf(" incr: %08lx %08lx\n",Rnd.fx_32.high,Rnd.fx_32.low);
    TheNumber.fx_64 = TheNumber.fx_64 + Rnd.fx_64;
    // printf(" after: %08lx %08lx\n",TheNumber.fx_32.high,TheNumber.fx_32.low);
    return TheNumber.fx_64;
    }
    //———–
    // Multiply two unsigned scaled fixed point numbers without overflowing a 64 bit value
    // The product of the two integer parts mut be < 2^32
    uint64_t MultiplyFixedPt(union ll_u Mcand, union ll_u Mplier) {
    union ll_u Result;
    Result.fx_64 = ((uint64_t)Mcand.fx_32.high * (uint64_t)Mplier.fx_32.high) << 32; // integer parts (clear fract)
    Result.fx_64 += ((uint64_t)Mcand.fx_32.low * (uint64_t)Mplier.fx_32.low) >> 32; // fraction parts (always < 1)
    Result.fx_64 += (uint64_t)Mcand.fx_32.high * (uint64_t)Mplier.fx_32.low; // cross products
    Result.fx_64 += (uint64_t)Mcand.fx_32.low * (uint64_t)Mplier.fx_32.high;
    return Result.fx_64;
    }
    //———–
    // Long long print-to-buffer helpers
    // Assumes little-Endian layout
    void PrintHexLL(char *pBuffer,union ll_u FixedPt) {
    sprintf(pBuffer,"%08lx %08lx",FixedPt.fx_32.high,FixedPt.fx_32.low);
    }
    // converts all 9 decimal digits of fraction, which should suffice
    void PrintFractionLL(char *pBuffer,union ll_u FixedPt) {
    union ll_u Fraction;
    Fraction.fx_64 = FixedPt.fx_32.low; // copy 32 fraction bits, high order = 0
    Fraction.fx_64 *= GIGA; // times 10^9 for conversion
    Fraction.fx_64 >>= 32; // align integer part in low long
    sprintf(pBuffer,"%09lu",Fraction.fx_32.low); // convert low long to decimal
    }
    void PrintIntegerLL(char *pBuffer,union ll_u FixedPt) {
    sprintf(pBuffer,"%lu",FixedPt.fx_32.high);
    }
    void PrintFixedPt(char *pBuffer,union ll_u FixedPt) {
    PrintIntegerLL(pBuffer,FixedPt); // do the integer part
    pBuffer += strlen(pBuffer); // aim pointer beyond integer
    *pBuffer++ = '.'; // drop in the decimal point, tick pointer
    PrintFractionLL(pBuffer,FixedPt);
    }
    void PrintFixedPtRounded(char *pBuffer,union ll_u FixedPt,unsigned Decimals) {
    char *pDecPt;
    //char *pBase;
    // pBase = pBuffer;
    FixedPt.fx_64 = RoundFixedPt(FixedPt,Decimals);
    PrintIntegerLL(pBuffer,FixedPt); // do the integer part
    // printf(" Buffer int: [%s]\n",pBase);
    pBuffer += strlen(pBuffer); // aim pointer beyond integer
    pDecPt = pBuffer; // save the point location
    *pBuffer++ = '.'; // drop in the decimal point, tick pointer
    PrintFractionLL(pBuffer,FixedPt);
    // printf(" Buffer all: [%s]\n",pBase);
    if (Decimals == 0)
    *pDecPt = 0; // 0 places means discard the decimal point
    else
    *(pDecPt + Decimals + 1) = 0; // truncate string to leave . and Decimals chars
    // printf(" Buffer end: [%s]\n",pBase);
    }
    //– Helper routine for printf()
    int s_putc(char c, FILE *t) {
    Serial.write(c);
    }
    //———–
    void setup ()
    {
    Serial.begin (115200);
    fdevopen(&s_putc,0); // set up serial output for printf()
    Serial.println (F("DDS calculation exercise"));
    Serial.println (F("Ed Nisley – KE4ZNU – May 2017\n"));
    // set up useful constants
    TempFX.fx_64 = -1;
    PrintFixedPt(Buffer,TempFX);
    printf("Max fixed point: %s\n",Buffer);
    One.fx_32.high = 1; // Set up 1.0, a very useful constant
    PrintFixedPt(Buffer,One);
    printf("\n1.0: %s\n",Buffer);
    Tenth.fx_64 = One.fx_64 / 10; // Likewise, 0.1
    PrintFixedPt(Buffer,Tenth);
    printf("\n0.1: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,Tenth,9); // show rounded value
    printf("0.1 to 9 dec: %s\n",Buffer);
    TestFreq.fx_64 = RoundFixedPt(Tenth,3); // show full string after rounding
    PrintFixedPt(Buffer,TestFreq);
    printf("0.1 to 3 dec: %s (full string)\n",Buffer);
    PrintFixedPtRounded(Buffer,Tenth,3); // show truncated string with rounded value
    printf("0.1 to 3 dec: %s (truncated string)\n",Buffer);
    CtPerHz.fx_64 = -1; // Set up 2^32 – 1, which is close enough
    CtPerHz.fx_64 /= 125 * MEGA; // divide by nominal oscillator
    PrintFixedPt(Buffer,CtPerHz);
    printf("\nCt/Hz = %s\n",Buffer);
    printf("Rounding: \n");
    for (int d = 9; d >= 0; d–) {
    PrintFixedPtRounded(Buffer,CtPerHz,d);
    printf(" %d: %s\n",d,Buffer);
    }
    HzPerCt.fx_64 = 125 * MEGA; // 125 MHz / 2^32, without actually shifting!
    PrintFixedPt(Buffer,HzPerCt);
    printf("\nHz/Ct: %s\n",Buffer);
    TenthHzCt.fx_64 = MultiplyFixedPt(Tenth,CtPerHz); // 0.1 Hz as delta-phase count
    PrintFixedPt(Buffer,TenthHzCt);
    printf("\n0.1 Hz as ct: %s\n",Buffer);
    printf("Rounding: \n");
    for (int d = 9; d >= 0; d–) {
    PrintFixedPtRounded(Buffer,TenthHzCt,d);
    printf(" %d: %s\n",d,Buffer);
    }
    // Try out various DDS computations
    TestFreq.fx_64 = One.fx_64 * (60 * KILO); // set 60 kHz
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TestFreq.fx_64 += Tenth.fx_64; // set 60000.1 kHz
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TestFreq.fx_64 -= Tenth.fx_64 * 2; // set 59999.9 kHz
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TestFreq.fx_64 = (599999LL * One.fx_64) / 10; // set 59999.9 kHz differently
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TempFX.fx_64 = RoundFixedPt(TestCount,0); // compute frequency from integer count
    TestFreq.fx_64 = MultiplyFixedPt(TempFX,HzPerCt);
    PrintFixedPt(Buffer,TestFreq);
    printf("Int ct -> freq: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestFreq.fx_64 = One.fx_64 * (10 * MEGA); // set 10 MHz
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TempFX.fx_64 = RoundFixedPt(TestCount,0); // compute frequency from integer count
    TestFreq.fx_64 = MultiplyFixedPt(TempFX,HzPerCt);
    PrintFixedPt(Buffer,TestFreq);
    printf("Int ct -> freq: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestFreq.fx_64 = One.fx_64 * (10 * MEGA); // set 10 MHz + 0.1 Hz
    TestFreq.fx_64 += Tenth.fx_64;
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TempFX.fx_64 = RoundFixedPt(TestCount,0); // compute frequency from integer count
    TestFreq.fx_64 = MultiplyFixedPt(TempFX,HzPerCt);
    PrintFixedPt(Buffer,TestFreq);
    printf("Int ct -> freq: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestFreq.fx_64 = One.fx_64 * (10 * MEGA); // set 10 MHz – 0.1 Hz
    TestFreq.fx_64 -= Tenth.fx_64;
    PrintFixedPt(Buffer,TestFreq);
    printf("\nTest frequency: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    TestCount.fx_64 = MultiplyFixedPt(TestFreq,CtPerHz); // convert to counts
    PrintFixedPt(Buffer,TestCount);
    printf("Delta phase ct: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestCount,0);
    printf(" round to int: %s\n",Buffer);
    TempFX.fx_64 = RoundFixedPt(TestCount,0); // compute frequency from integer count
    TestFreq.fx_64 = MultiplyFixedPt(TempFX,HzPerCt);
    PrintFixedPt(Buffer,TestFreq);
    printf("Int ct -> freq: %s\n",Buffer);
    PrintFixedPtRounded(Buffer,TestFreq,1);
    printf(" round: %s\n",Buffer);
    Oscillator.fx_64 = One.fx_64 * (125 * MEGA);
    Serial.println("Oscillator tune: CtPerHz");
    PrintFixedPtRounded(Buffer,Oscillator,2);
    printf(" Oscillator: %s\n",Buffer);
    for (int i=-10; i<=10; i++) {
    OscOffset.fx_64 = i * One.fx_64;
    CtPerHz.fx_64 = 1LL << 63;
    CtPerHz.fx_64 /= (Oscillator.fx_64 + OscOffset.fx_64) >> 33;
    PrintFixedPt(Buffer,CtPerHz);
    printf(" %+3d -> %s\n",i,Buffer);
    }
    }
    //———–
    void loop () {
    }
    view raw DDSCalcTest.ino hosted with ❤ by GitHub
    DDS calculation exercise
    Ed Nisley – KE4ZNU – May 2017
    Max fixed point: 4294967295.999999999
    1.0: 1.000000000
    0.1: 0.099999999
    0.1 to 9 dec: 0.100000000
    0.1 to 3 dec: 0.100499999 (full string)
    0.1 to 3 dec: 0.100 (truncated string)
    Ct/Hz = 34.359738367
    Rounding:
    9: 34.359738368
    8: 34.35973837
    7: 34.3597384
    6: 34.359738
    5: 34.35974
    4: 34.3597
    3: 34.360
    2: 34.36
    1: 34.4
    0: 34
    Hz/Ct: 0.029103830
    0.1 Hz as ct: 3.435973831
    Rounding:
    9: 3.435973832
    8: 3.43597383
    7: 3.4359738
    6: 3.435974
    5: 3.43597
    4: 3.4360
    3: 3.436
    2: 3.44
    1: 3.4
    0: 3
    Test frequency: 60000.000000000
    round: 60000.0
    Delta phase ct: 2061584.302070550
    round to int: 2061584
    Test frequency: 60000.099999999
    round: 60000.1
    Delta phase ct: 2061587.738044382
    round to int: 2061588
    Test frequency: 59999.900000000
    round: 59999.9
    Delta phase ct: 2061580.866096718
    round to int: 2061581
    Test frequency: 59999.899999999
    round: 59999.9
    Delta phase ct: 2061580.866096710
    round to int: 2061581
    Int ct -> freq: 59999.914551639
    round: 59999.9
    Test frequency: 10000000.000000000
    round: 10000000.0
    Delta phase ct: 343597383.678425103
    round to int: 343597384
    Int ct -> freq: 10000000.014506079
    round: 10000000.0
    Test frequency: 10000000.099999999
    round: 10000000.1
    Delta phase ct: 343597387.114398935
    round to int: 343597387
    Int ct -> freq: 10000000.114506079
    round: 10000000.1
    Test frequency: 9999999.900000000
    round: 9999999.9
    Delta phase ct: 343597380.242451271
    round to int: 343597380
    Int ct -> freq: 9999999.914506079
    round: 9999999.9
    Oscillator tune: CtPerHz
    Oscillator: 125000000.00
    -10 -> 34.359741116
    -9 -> 34.359741116
    -8 -> 34.359740566
    -7 -> 34.359740566
    -6 -> 34.359740017
    -5 -> 34.359740017
    -4 -> 34.359739467
    -3 -> 34.359739467
    -2 -> 34.359738917
    -1 -> 34.359738917
    +0 -> 34.359738367
    +1 -> 34.359738367
    +2 -> 34.359737818
    +3 -> 34.359737818
    +4 -> 34.359737268
    +5 -> 34.359737268
    +6 -> 34.359736718
    +7 -> 34.359736718
    +8 -> 34.359736168
    +9 -> 34.359736168
    +10 -> 34.359735619
    view raw DDSCalcTest.txt hosted with ❤ by GitHub
  • Copying Video Files From Action Cameras to a NAS Drive

    For unknown reasons, a recent VLC update caused it to ignore uppercase file extensions: MP4 and AVI files no longer appear in its directory listings, while mp4 and avi files do. The least-awful solution involved renaming the files after copying them:

    find /mnt/video -name \*AVI -print0 | xargs -0 rename -v -f 's/AVI/avi/'
    find /mnt/video -name \*MP4 -print0 | xargs -0 rename -v -f 's/MP4/mp4/'
    find /mnt/video -name \*THM -print0 | xargs -0 rename -v -f 's/THM/thm/'
    

    Yup, that scans the whole drive every time, which takes care of stray files, manual tweaks, and suchlike. The THM files are useless thumbnails; I should just delete them.

    While I had the hood up, I listed the remaining space on the NAS drive and cleaned up a few misfeatures. I manually delete old video files / directories as needed, usually immediately after the script crashes for lack of room.

    The Sony HDR-AS30V can act as a USB memory device, but it dependably segfaults the ExFAT driver; I now transfer its MicroSD card to an adapter and jam it into the media slot on the monitor, where it works fine.

    Protip: always turn the AS30V on to verify the MicroSD card has seated correctly in its socket. Unfortunately, the socket can also hold Sony’s proprietary Memory Stick Micro cards (32 GB maximum capacity = roadkill), but the dual-use / dual-direction socket isn’t a snug fit around MicroSD cards. You (well, I) can insert a card so it looks fine, while sitting slightly canted and not making proper contact. The camera will kvetch about that and it’s easier to fix with the camera in hand.

    I’ve disabled USB device automounting, as I vastly prefer to handle them manually, so the script asks for permission in order to mount the drives. The transfer requires about an hour, so I’ve extended the time the sudo password remains active.

    The script lets both cards transfer data simultaneously; the Fly6 generally finishes first because it produces less data. That produces a jumbled progress display and the script waits for both drives to finish before continuing.

    The Bash source code as a GitHub Gist:

    #!/bin/sh
    thisdate=$(date –rfc-3339=date)
    echo Date is $thisdate
    date
    # MicroSD cards not automounted
    as30v=/mnt/AS30V
    fly6=/mnt/Fly6
    sudo mount -o uid=ed /dev/sdb1 /mnt/AS30V/
    sudo mount -o uid=ed /dev/sdc1 /mnt/Fly6/
    # IOmega NAS defined as /mnt/video in fstab
    sudo mount /mnt/video
    mkdir /mnt/video/$thisdate
    rsync -ahu –progress $as30v/MP_ROOT/100ANV01/ /mnt/video/$thisdate &
    pid1=$!
    rsync -ahu –progress $fly6 /mnt/video
    date
    rc2=$?
    echo Fly6 RC is $rc2
    echo Waiting for $as30v
    wait $pid1
    rc=$(( $rc2 + $? ))
    date
    echo Overall RC: $rc
    if [ $rc -eq 0 ] ; then
    echo Fix capitalized extensions
    find /mnt/video -name \*AVI -print0 | xargs -0 rename -v -f 's/AVI/avi/'
    find /mnt/video -name \*MP4 -print0 | xargs -0 rename -v -f 's/MP4/mp4/'
    find /mnt/video -name \*THM -print0 | xargs -0 rename -v -f 's/THM/thm/'
    echo Space remaining on NAS drive:
    df -h /mnt/video
    echo Remove files on AS30V
    rm $as30v/MP_ROOT/100ANV01/*
    echo Unmount cards and NAS
    sudo umount $as30v
    sudo umount $fly6
    sudo umount /mnt/video
    else
    echo Whoopsie: $rc
    fi
    view raw savevideo.sh hosted with ❤ by GitHub
  • Arduino Joystick

    A bag of sub-one-dollar resistive joysticks arrived from halfway around the planet:

    Arduino UNO - resistive joystick
    Arduino UNO – resistive joystick

    A quick-and-dirty test routine showed the sticks start out close to VCC/2:

    Welcome to minicom 2.7
    
    OPTIONS: I18n
    Compiled on Feb  7 2016, 13:37:27.
    Port /dev/ttyACM0, 10:23:45
    
    Press CTRL-A Z for help on special keys
    
    Joystick exercise
    Ed Nisley - KE4ZNU - May 2017
    00524 - 00513 - 1
    

    That’s from minicom on the serial port, as the Arduino IDE’s built-in serial monitor ignores bare Carriage Return characters.

    The joystick hat tilts ±25° from its spring-loaded center position, but the active region seems to cover only 15° of that arc, with a 5° dead zone around the center and 5° of overtravel at the limits. This is not a high-resolution instrument intended for fine motor control operations.

    The analog input values range from 0x000 to 0x3FF across the active region. Aim the connector at your tummy to make the axes work the way you’d expect: left / down = minimum, right / up = maximum.

    The delay(100) statements may or may not be needed for good analog input values, depending on some imponderables that seem not to apply for this lashup, but they pace the loop() to a reasonable update rate.

    Pushing the hat toward the PCB activates the simple switch you can see in the picture. It requires an external pullup resistor (hence the INPUT_PULLUP configuration) and reports low = 0 when pressed.

    Those are 0.125 inch (exactly!) holes on a 19.5×26.25 mm grid in a 26.5×34.25 mm PCB. Makes no sense to me, either.

    The trivial Arduino source code as a GitHub Gist:

    // Joystick exercise
    #define JOYX A0
    #define JOYY A1
    #define BUTTON 7
    int JoyX,JoyY;
    boolean Button;
    //– Helper routine for printf()
    int s_putc(char c, FILE *t) {
    Serial.write(c);
    }
    void setup() {
    Serial.begin (9600);
    fdevopen(&s_putc,0); // set up serial output for printf()
    Serial.println ("Joystick exercise");
    Serial.println ("Ed Nisley – KE4ZNU – May 2017");
    pinMode(BUTTON,INPUT_PULLUP);
    }
    void loop() {
    JoyX = analogRead(JOYX);
    delay(100);
    JoyY = analogRead(JOYY);
    delay(100);
    Button = digitalRead(BUTTON);
    printf("%05d – %05d – %1d\r",JoyX,JoyY,Button);
    }
  • Dropbox Tour: To Keep Learning, Click Cancel

    After copying a Digital Machinist column to my Dropbox folder, I went to the site to get the link, discovered they improved the UI, declined a Flash-based tour of the new features, and got this baffling confirmation dialog:

    Dropbox - tour exit dialog
    Dropbox – tour exit dialog

    So. Many. Wrongs.

  • Arduino vs. Significant Figures: BigNumber Library

    The BigNumber library wraps the bc arbitrary precision calculator into a set of Arduino routines that seem like a reasonable basis for DDS calculations requiring more than the half-dozen digits of a floating point number or the limited range of scaled fixed point numbers tucked into an long int.

    Treating programming as an experimental science produces some Arduino source code and its output as a GitHub Gist:

    // BigNumber exercise
    #include "BigNumber.h"
    //– Helper routine for printf()
    int s_putc(char c, FILE *t) {
    Serial.write(c);
    }
    void setup ()
    {
    Serial.begin (115200);
    fdevopen(&s_putc,0); // set up serial output for printf()
    Serial.println ("BigNumber exercise");
    Serial.println ("Ed Nisley – KE4ZNU – April 2017");
    #define WHOLES 10
    #define FRACTS 10
    printf("Fraction digits: %d\n",FRACTS);
    BigNumber::begin (FRACTS);
    char *pBigNumber;
    #define BUFFLEN (WHOLES + FRACTS)
    char NumString[BUFFLEN];
    BigNumber Tenth = "0.1"; // useful constants
    BigNumber Half = "0.5";
    BigNumber One = 1;
    BigNumber Two = 2;
    BigNumber ThirtyTwoBits = Two.pow(32);
    Serial.println(ThirtyTwoBits);
    BigNumber Oscillator = "125000000";
    Serial.println(Oscillator);
    BigNumber HertzPerCount;
    HertzPerCount = Oscillator / ThirtyTwoBits;
    Serial.println(HertzPerCount);
    BigNumber CountPerHertz;
    CountPerHertz = ThirtyTwoBits / Oscillator;
    Serial.println(CountPerHertz);
    BigNumber TestFreq = "60000";
    Serial.println(TestFreq);
    BigNumber DeltaPhi;
    DeltaPhi = TestFreq * CountPerHertz;
    Serial.println(DeltaPhi);
    long DeltaPhiL;
    DeltaPhiL = DeltaPhi;
    printf("Long: %ld\n",DeltaPhiL);
    Serial.println("0.1 Hz increment …");
    Serial.println(TestFreq + Tenth);
    DeltaPhi = (TestFreq + Tenth) * CountPerHertz;
    Serial.println(DeltaPhi);
    TestFreq = DeltaPhi * HertzPerCount;
    Serial.println(TestFreq);
    Serial.println("Rounding DeltaPhi up …");
    DeltaPhi += Half;
    Serial.println(DeltaPhi);
    TestFreq = DeltaPhi * HertzPerCount;
    Serial.println(TestFreq);
    pBigNumber = DeltaPhi.toString();
    printf("String: %04x → %s\n",pBigNumber,pBigNumber);
    free(pBigNumber);
    DeltaPhiL = DeltaPhi;
    printf("Unsigned: %ld\n",DeltaPhiL);
    pBigNumber = "59999.9";
    TestFreq = pBigNumber;
    Serial.println(TestFreq);
    DeltaPhi = TestFreq * CountPerHertz;
    Serial.println(DeltaPhi);
    Serial.println("Rounding DeltaPhi up …");
    DeltaPhi = TestFreq * CountPerHertz + Half;
    Serial.println(DeltaPhi);
    DeltaPhiL = DeltaPhi;
    int rc = snprintf(NumString,BUFFLEN,"%ld",DeltaPhiL);
    if (rc > 0 && rc < BUFFLEN) {
    printf("String length: %d\n",rc);
    }
    else {
    printf("Whoops: %d for %ld\n",rc,DeltaPhiL);
    strncpy(NumString,"123456789",sizeof(NumString));
    NumString[BUFFLEN-1] = 0;
    printf(" forced: %s\n",NumString);
    }
    printf("Back from string [%s]\n",NumString);
    DeltaPhi = NumString;
    Serial.println(DeltaPhi);
    TestFreq = DeltaPhi * HertzPerCount;
    Serial.println(TestFreq);
    }
    void loop () {
    }
    view raw BigNumTest.ino hosted with ❤ by GitHub
    BigNumber exercise
    Ed Nisley – KE4ZNU – April 2017
    Fraction digits: 10
    4294967296
    125000000
    0.0291038304
    34.3597383680
    60000
    2061584.3020800000
    Long: 2061584
    0.1 Hz increment …
    60000.1000000000
    2061587.7380538368
    60000.0998830384
    Rounding DeltaPhi up …
    2061588.2380538368
    60000.1144349536
    String: 045e → 2061588.2380538368
    Unsigned: 2061588
    59999.9
    2061580.8661061632
    Rounding DeltaPhi up …
    2061581.3661061632
    String length: 7
    Back from string [2061581]
    2061581
    59999.9037798624
    view raw BigNumTest.txt hosted with ❤ by GitHub

    All that happened incrementally, as you might expect, with the intent of seeing how it works, rather than actually doing anything.

    Some musings, in no particular order:

    The library soaks up quite a hunk of program space:

    Sketch uses 13304 bytes (43%) of program storage space. Maximum is 30720 bytes.
    

    I think you could cut that back a little by eliminating unused bc routines, like square root / exponential / modulus.

    That test code also blots up quite a bit of RAM:

    Global variables use 508 bytes (24%) of dynamic memory, leaving 1540 bytes for local variables. Maximum is 2048 bytes.
    

    All the BigNumber variables live inside the setup() function (or whatever it’s called in Arduino-speak), so they count as local variables. They’re four bytes each, excluding the dynamically allocated storage for the actual numbers at roughly a byte per digit. With 10 decimal places for all numbers, plus (maybe) an average of half a dozen integer digits, those ten BigNumbers soak up 200 = 10 × (4 + 16) bytes of precious RAM.

    You can load a BigNumber from an int (not a long) or a string, then export the results to a long or a string. Given that controlling a DDS frequency with a knob involves mostly adding and subtracting a specific step size, strings would probably work fine, using snprintf() to jam the string equivalent of a long into a BigNumber as needed.

    You must have about ten decimal places to hold enough significant figures in the HertzPerCount and CountPerHertz values. The library scale factor evidently forces all the numbers to have at least that many digits, with the decimal point stuck in front of them during string output conversions.

    The biggest integers happen in the Oscillator and ThirtyTwoBits values, with 9 and 10 digits, respectively.

    It looks useful, although I’m uncomfortable with the program space required. I have no way to estimate the program space for a simpleminded DDS controller, other than knowing it’ll be more than I estimate.

    While poking around, however, I discovered the Arduino compiler does provide (limited) support for long long int variables. Given a 64 bit unit for simple arithmetic operations, a simpler implementation of fixed point numbers may be do-able: 32 bits for the integer and fraction should suffice! More on that shortly.

  • Badge Lanyard Reel Mount

    A certain young engineer of my acquaintance now carries an ID badge and, so I hear, works in a PCB design & test venue. Seeing as how her favorite color is purple, this seemed appropriate:

    Badge Lanyard Reel - front - overall
    Badge Lanyard Reel – front – overall

    The guts came from Circuit Breaker Labs in the form of a recycled PCB trapped in acrylic resin atop a plastic housing with a spring-loaded reel inside.

    It arrived with a plastic bullet at the end of the lanyard:

    Badge Lanyard Reel - plastic bullet link
    Badge Lanyard Reel – plastic bullet link

    Which I immediately replaced with brass, because Steampunk:

    Badge Lanyard Reel - bullet cross-drill
    Badge Lanyard Reel – bullet cross-drill

    That made the plastic housing look weak, so, in a series of stepwise refinements, I conjured a much better case from the vasty digital deep:

    Badge Lanyard Reel - iterations
    Badge Lanyard Reel – iterations

    All of the many, many critical dimensions lie inside the case, where they can’t be measured accurately, so each of those iterations could improve only one or two features. The absolutely wonderful thing about OpenSCAD is having it regenerate the whole model after loosening, say, the carabiner slot by two thread thicknesses; you can do that with a full-on relational CAD drawing, but CAD drawings always seems like a lot of unnecessary work if I must figure out the equations anyway.

    The back sports my favorite Hilbert Curve infill with a nicely textured finish:

    Badge Lanyard Reel - rear - oblique
    Badge Lanyard Reel – rear – oblique

    It’d surely look better in solid brass with Hilbert curve etching.

    Black PETG doesn’t photograph well, but at least you can see the M2 brass inserts:

    Badge Lanyard Reel - lower interior
    Badge Lanyard Reel – lower interior

    The first prototype showed the inserts needed far more traction than the usual reamed holes could provide, so I added internal epoxy grooves in each hole:

    Badge Lanyard Reel Mount - show
    Badge Lanyard Reel Mount – show

    Recessing the screw heads into the top plate made them more decorative and smoother to the touch. Button-head screws would be even smoother, but IMO didn’t look quite as bold.

    After seeing how well the grooves worked, I must conjure a module tabulating all the inserts on hand and automagically generating the grooves.

    The yellow star holds up the roof of the reel recess in the build layout:

    Badge Lanyard Reel Mount - build layout - bottom
    Badge Lanyard Reel Mount – build layout – bottom

    Slic3r produced the rest of the support material for the carabiner exit slot:

    Badge Lanyard Reel Mount - bottom - Slic3r support
    Badge Lanyard Reel Mount – bottom – Slic3r support

    Those two support lumps on the right don’t actually support anything, but tweaking the support settings to disable them also killed the useful support on the left; come to find out Slic3r’s modifier meshes don’t let you disable support generation.

    The top plate required support all the way around the inside of the bezel:

    Badge Lanyard Reel Mount - top - Slic3r support
    Badge Lanyard Reel Mount – top – Slic3r support

    I carved the original plastic housing in half, roughly along its midline, and discarded the bottom section with the belt clip (it’s on the far left of the scrap pile). The top section, with PCB firmly affixed, holds the lanyard reel and anchors the retracting spring in a central slotted peg. No pictures of that, as it’s either a loose assembly of parts or a spring-loaded bomb and I am not taking it apart again.

    The lanyard passes through an eyelet that pays it out to the rotating reel. I’d definitely do that differently, were I building it from scratch, because mounting the eyelet in exactly the proper position to prevent the lanyard from stacking up on the reel and jamming against the inside of the housing turned out to be absolutely critical and nearly impossible.

    The top plate presses the original housing against the carabiner, with the cut-off section inside the carabiner’s circular embrace, which just barely worked: the PCB bezel is a millimeter smaller than the shoulder of the housing.

    All in all, I think it came out really well for a 3D printed object made by a guy who usually builds brackets:

    Badge Lanyard Reel - front - oblique
    Badge Lanyard Reel – front – oblique

    I hope she likes it …

    The OpenSCAD source code as a GitHub Gist:

    // Badge Lanyard Reel Mount
    // Ed Nisley KE4ZNU April 2017
    // Reel center at origin, lanyard exit toward +X
    Layout = "Show";
    Support = true;
    //- Extrusion parameters must match reality!
    ThreadThick = 0.20;
    ThreadWidth = 0.40;
    HoleWindage = 0.2;
    Protrusion = 0.05; // make holes end cleanly
    inch = 25.4;
    function IntegerMultiple(Size,Unit) = Unit * ceil(Size / Unit);
    //———————-
    // Dimensions
    ID = 0; // for round things
    OD = 1;
    LENGTH = 2;
    Carabiner = [30.7,35.3,3.5]; // metal carabiner around original reel
    Latch = [6.0,-15,8.0]; // wire spring latch: offset from OD + thickness
    LatchAngle = 60; // max deflection angle to center from -X direction
    LatchPoints = [[0,0],
    [Latch[1]/tan(LatchAngle),0],
    [Latch[1]/tan(LatchAngle),-Latch[1]]]; // polygon in as-cut orientation
    echo(str("Latch polygon: ",LatchPoints));
    Screw = [2.0,3.8 + 0*ThreadWidth,10.0]; // M2 screw: ID = clear, OD = head
    ScrewHeadLength = 2.0;
    ScrewSides = 8;
    ScrewRecess = 5*ThreadThick;
    MountSides = ScrewSides; // suitably gritty corners
    MountThick = Screw[LENGTH] / cos(180/MountSides) + ScrewRecess + 2.0;
    Insert = [Screw[ID],3.4,4.0]; // brass insert for screws
    BCD = Carabiner[OD] + 2.5*Insert[OD];
    BoltAngles = [20,110]; // ± angles to bolt holes
    Reel = [5.3,25.5 + 2*ThreadWidth,6.0 + 2*ThreadThick]; // lanyard cord reel
    ShimThick = 2*ThreadThick; // covers open side of reel for better sliding
    Bezel = [31.0,32.0,7.5]; // PCB holder + shell, LENGTH = post + shell
    BezelSides = 6*4;
    BezelBlock = [5.5,7.5,3.6] + [ThreadWidth,ThreadWidth,ThreadThick]; // block around lanyard eyelet
    Eyelet = [3.5,4.5,3.0];
    Bullet = [2.0,6.5,2.0]; // brass badge holder, LENGTH = recess into mount
    //———————-
    // Useful routines
    module PolyCyl(Dia,Height,ForceSides=0) { // based on nophead's polyholes
    Sides = (ForceSides != 0) ? ForceSides : (ceil(Dia) + 2);
    FixDia = Dia / cos(180/Sides);
    cylinder(r=(FixDia + HoleWindage)/2,
    h=Height,
    $fn=Sides);
    }
    //– Lanyard reel mockup
    module Reel() {
    cylinder(d=Reel[OD],h=Reel[LENGTH],center=true,$fn=6*4);
    }
    // Carabiner metal mockup
    // Some magic numbers lie in wait
    module Beener() {
    difference() {
    hull() {
    cylinder(d=Carabiner[OD],
    h=Carabiner[LENGTH] + 2*ThreadThick,
    center=true,$fn=BezelSides);
    translate([-Carabiner[OD]/2,0,0])
    cylinder(d=Carabiner[OD] – 2.0,
    h=Carabiner[LENGTH] + 2*ThreadThick,
    center=true,$fn=6*4);
    }
    cylinder(d=Carabiner[ID],
    h=2*Carabiner[LENGTH],
    center=true,$fn=BezelSides);
    translate([Carabiner[ID]/4,0,0])
    cube([Carabiner[ID],7.0,2*Carabiner[LENGTH]],center=true);
    }
    }
    // mockup of PCB holder atop remains of old mount with reel post
    // Z = 0 at midline of case
    module BezelMount() {
    rotate(180/BezelSides) {
    PolyCyl(Bezel[ID] + HoleWindage,MountThick,BezelSides); // PCB punches through mount
    PolyCyl(Bezel[OD] + HoleWindage,Bezel[LENGTH] – Reel[LENGTH]/2,BezelSides);
    }
    translate([Reel[OD]/2,0,BezelBlock[2]/2])
    scale([2,1,1])
    cube(BezelBlock,center=true);
    }
    // Main mount around holder & carabiner
    module Mount(Section="All") {
    render()
    difference() {
    hull() {
    for (a = BoltAngles) // spheres defining corners
    for (i=[-1,1])
    rotate(i*a)
    translate([BCD/2,0,0])
    sphere(d=MountThick,$fn=MountSides);
    cylinder(d=Carabiner[OD] + 4*ThreadWidth,
    h=MountThick,center=true); // capture carabiner ring
    }
    for (a = BoltAngles) // screw & insert holes, head recess
    for (i=[-1,1])
    rotate(i*a)
    translate([BCD/2,0,0])
    rotate(0*i*180/ScrewSides) {
    translate([0,0,-(Insert[LENGTH] + 2*ThreadThick)])
    PolyCyl(Insert[OD],
    Insert[LENGTH] + 2*ThreadThick + Protrusion,ScrewSides);
    for (k = [-2:2]) // epoxy retaining grooves
    translate([0,0,-(k*3*ThreadThick + Insert[LENGTH]/2)])
    PolyCyl(Insert[OD] + 1*ThreadWidth,
    2*ThreadThick,ScrewSides);
    PolyCyl(Screw[ID],Screw[LENGTH],ScrewSides);
    translate([0,0,MountThick/2 – ScrewRecess]) // recess screw heads
    PolyCyl(Screw[OD],Screw[LENGTH],ScrewSides);
    }
    translate([0,0,-1*ThreadThick]) // Minkowski Z extends only top surface!
    minkowski() { // space for metal carabiner
    Beener();
    // cube([ThreadWidth,ThreadWidth,2*ThreadThick]);
    cylinder(d=ThreadWidth,h=2*ThreadThick,$fn=6);
    }
    rotate([0,90,0]) rotate(180/6) // cord channel = brass tube clearance
    PolyCyl(Bullet[ID],Carabiner[ID],6);
    translate([Eyelet[LENGTH] + 2.0,0,0]) // eyelet, large end inward
    rotate([0,90,0]) rotate(180/6)
    PolyCyl(Eyelet[OD] + HoleWindage, Reel[OD]/2,6);
    if (false)
    translate([Reel[OD]/2 + Eyelet[LENGTH]/2,0,0]) // eyelet, small end outward
    rotate([0,90,0]) rotate(180/6)
    PolyCyl(Eyelet[ID],Eyelet[LENGTH],6);
    translate([(BCD/2 + MountThick/2)*cos(BoltAngles[0]) – Bullet[LENGTH],0,0]) // bullet recess
    rotate([0,90,0]) rotate(180/6)
    PolyCyl(Bullet[OD],Carabiner[ID],6);
    BezelMount(); // PCB holder clearance
    Reel(); // reel clearance
    translate([0,0,-(Reel[LENGTH] + ShimThick)/2]) // sliding plate on open side of reel
    cylinder(d=Reel[OD],h=ShimThick,center=true,$fn=6*4);
    translate([-Carabiner[OD]/2 + Latch[0],Latch[1],0])
    linear_extrude(height=Latch[2],center=true)
    polygon(LatchPoints);
    if (Section == "Upper") // display & build section cutting
    translate([0,0,-2*Carabiner[LENGTH]])
    cube(4*Carabiner,center=true);
    else if (Section == "Lower")
    translate([0,0,2*Carabiner[LENGTH]])
    cube(4*Carabiner,center=true);
    }
    if (Support) { // Completely ad-hoc support structures
    color("Yellow", Layout == "Show" ? 0.3 : 1.0) {
    if (false && Section == "Upper") {
    Spokes = BezelSides;
    Offset = 6*ThreadWidth;
    for (i = [2:Spokes – 2])
    rotate(i * 360/Spokes)
    translate([Offset,-ThreadWidth,0*(Carabiner[LENGTH]/2)/2])
    cube([Carabiner[OD]/2 – Offset – 0*ThreadWidth,
    2*ThreadWidth,
    Carabiner[LENGTH]/2],center=false);
    for (i = [0:Spokes – 1])
    rotate(i * 360/Spokes)
    translate([Offset,-ThreadWidth,0])
    cube([Bezel[OD]/2 – Offset,
    2*ThreadWidth,
    Bezel[LENGTH] – Reel[LENGTH]/2 – 2*ThreadThick],center=false);
    Bars = 7;
    render()
    difference() {
    union() {
    for (i = [-floor(Bars/2) : floor(Bars/2)])
    translate([-Carabiner[ID]/2,i*Carabiner[OD]/Bars,Carabiner[LENGTH]/4])
    cube([Carabiner[ID]/3,2*ThreadWidth,Carabiner[LENGTH]/2],center=true);
    translate([-Carabiner[ID]/2,0,ThreadThick/2])
    cube([Carabiner[ID]/3,Carabiner[ID],ThreadThick],center=true);
    }
    cylinder(d=Carabiner[ID] + 2*ThreadWidth,h=Carabiner[LENGTH]);
    }
    }
    if (Section == "Lower") {
    translate([0,0,-(Reel[LENGTH]/4 + ShimThick/2 – ThreadThick/2)])
    for (i = [0:8])
    rotate(i * 360/8)
    cube([Reel[OD] – 2*ThreadWidth,
    2*ThreadWidth,
    Reel[LENGTH]/2 + ShimThick – ThreadThick],center=true);
    if (false) {
    Bars = 7;
    render()
    difference() {
    union() {
    for (i = [-floor(Bars/2) : floor(Bars/2)])
    translate([-Carabiner[ID]/2,i*Carabiner[OD]/Bars,-Carabiner[LENGTH]/4])
    cube([Carabiner[ID]/3,2*ThreadWidth,Carabiner[LENGTH]/2],center=true);
    translate([-Carabiner[ID]/2,0,-ThreadThick/2])
    cube([Carabiner[ID]/3,Carabiner[ID],ThreadThick],center=true);
    }
    translate([0,0,-Carabiner[LENGTH]])
    cylinder(d=Carabiner[ID] + 0*ThreadWidth,h=Carabiner[LENGTH]);
    }
    }
    }
    }
    }
    }
    //———————-
    // Build it
    if (Layout == "Beener")
    Beener();
    if (Layout == "Mount")
    Mount();
    if (Layout == "Reel")
    Reel();
    if (Layout == "BezelMount")
    BezelMount();
    Gap = 25;
    if (Layout == "Show") {
    translate([0,0,Gap/2])
    Mount("Upper");
    translate([0,0,-Gap/2])
    Mount("Lower");
    color("Green",0.3)
    Beener();
    color("Brown",0.3)
    Reel();
    color("Red",0.3)
    translate([0,0,-(Reel[LENGTH] + ShimThick)/2])
    cylinder(d=Reel[OD],h=ShimThick,center=true,$fn=6*4);
    }
    if (Layout == "Build") {
    translate([(BCD + MountThick)/2,0,0])
    rotate(180)
    Mount("Upper");
    rotate([180,0,0])
    translate([-(BCD + MountThick)/2,0,0])
    Mount("Lower");
    }
    if (Layout == "BuildUpper")
    Mount("Upper");
    if (Layout == "BuildLower")
    rotate([180,0,0])
    Mount("Lower");