Binary operators in OpenSCAD

Recently I decided I needed some binary operators in OpenSCAD. As there are no built-in binary operators, and a quick search didn’t reveal any handy libraries, I decided to write some.

The solution is not very elegant, but seems to work.

The binary boxes example and bitSet function I found over at Boolean Operators in OpenScad – Counting in Binary

// Description: Binary operators
// Copyright CC-BY-SA Donotdespisethesnake 2020 
// with help from Andre
// Revision History:
// 1 2020-10-15   Initial version

/* [Hidden] */

/* Not very elegant, but ... */

and_table = [0,0,0,1];
or_table  = [0,1,1,1];
xor_table = [0,1,1,0];

// From Andre 
function bitSet(num, bit) = floor(num / pow(2, bit)) % 2;

// AND
function bitAndN(x,y,n) = (n==0) ? 
  and_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) : 
  and_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) + bitAndN(x,y,n-1); 
function bitAnd (x,y) = bitAndN (x,y,31);

// OR 
function bitOrN(x,y,n) = (n==0) ? 
  or_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) : 
  or_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) + bitOrN(x,y,n-1); 

function bitOr (x,y) = bitOrN (x,y,31);

// XOR 
function bitXorN(x,y,n) = (n==0) ? 
  xor_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) : 
  xor_table[(bitSet (x,n) ? 1 : 0) + (bitSet(y,n) ? 2 : 0)] * pow(2,n) + bitXorN(x,y,n-1); 

function bitXor (x,y) = bitXorN (x,y,31);

// demo 

/* [Parameters] */

num_bits = 8; // [1:32]


unit_size = 20;

// draw a row of bits
module row (a)
  for (j=[0:num_bits-1])
    n = num_bits-1-j;
    translate ([(unit_size+1)*j,0,0])
    difference ()
      cube ([unit_size,unit_size,unit_size], center=true);
      if (!bitSet (a, n))
        translate ([0,0,2])
          cube ([unit_size-2,unit_size-2,unit_size-2], center=true);

// label the bits
for (j=[0:num_bits-1])
  n = num_bits-1-j;
  translate ([ n*(unit_size+1),15,0])
    rotate ([0,0,90])
    text (str("2^", j, "= ", pow(2,j)), halign="left", valign="center");
// Operand A
translate ([-20,-5,0])
text (str(a,"="), halign="right");
  row (a);

// Operand B
translate ([0,-(unit_size+1),0])
  translate ([-20,-5,0])
  text (str(b,"="), halign="right");
  row (b);

// AND function
translate ([0,-(unit_size+1)*3,0])
  translate ([-20,-5,0])
  text (str(a," AND ", b, " ="), halign="right");
  row (bitAnd (a, b));

// OR function
translate ([0,-(unit_size+1)*4,0])
  translate ([-20,-5,0])
  text (str(a," OR ", b, " ="), halign="right");
  row (bitOr (a, b));

// XOR function
translate ([0,-(unit_size+1)*5,0])
  translate ([-20,-5,0])
  text (str(a," XOR ", b, " ="), halign="right");
  row (bitXor (a, b));
Posted in Uncategorized | Leave a comment

Adafruit start hostile “takeover bid” for Arduino?

You have to feel a bit sorry for those who run Arduino. After being involved in a protracted internal battle which saw the organisation split into two, they have resolved their differences and created a way forward.

Unfortunately, now they have more trouble – ironically from Open Source “friends” who seem to have taken a dislike to some Arduino personnel. These “friends” are Limor Fried and Phillip Torrone from Adafruit/MAKE, and Dale Dougherty, CEO of MAKE magazine.

MAKE magazine, which now seems to be a PR channel for Adafruit, has a hit piece calling for a “Free Arduino Foundation” – which basically means taking control of Arduino away from Arduino LLC et al, the legitimate owners of the Arduino brand, and a business that has taken many years to make successful. This crusade is endorsed by Adafruit, who say “Thanks Dale” They have even created a logo and hashtag I suppose many pressure groups use social campaigns, but I don’t recall one business using it as a takeover tactic of another’s business.

With friends like these, who needs enemies?

It seems that Adafruit may have some ulterior motive, I’m not sure what it is.

Perhaps Adafruit have decided their share of the Arduino pie is not enough, and now they want the whole thing? Either way, it’s an unnecessary distraction for the Arduino organisation, who I believe really want to just get on and create cool stuff, without hostile third parties making a grab for their business.

Update: It seems there is a more powerful motive behind this than money – spite! This piece in Wired, explains the background Arduino’s New CEO, Federico Musto, May Have Fabricated His Academic Record

Torrone says that one of the reasons why he went to the press with the information about Musto’s credentials was precisely that: to defend the community.

To women in the maker movement, who are often accused of being fake geeks and frequently have their expertise questioned, Musto’s apparent lies are personal affronts. “When you go to MIT, there is always this murmur that they had to lower the standards for you,” Fried says. “And after you graduate, you get asked all the time if you were actually smart enough to have earned your credentials. It’s a little bit insane that this guy has gotten this far without ever being questioned.”

It seems clear that Mr Musto does not have the degrees he claims, so that makes him a Bad Guy.  tsk tsk. The sort of Bad Guy who is not just going to step aside. The whole thing about a “Free Arduino Foundation for the benefit of the Arduino community” is a ruse, the real goal is a hate campaign against Musto. Since Musto owns 50% of Arduino, probably the only way for Limor Fried to achieve her goal of ousting Musto is to take over Arduino.

Posted in Corporate Bullshit | Tagged , | Leave a comment

2015 – The Year of Affordable DIY Pick and Place?

Over the years, there have been many attempts at a DIY Pick and Place machine (PnP). Some people have tried conversions of existing CNC machines, others have started from scratch. Either way, it is a tricky thing to do and keep it cheap and simple. There are several things that add complexity, and the task becomes one of creating a nicely integrated package, with both hardware and software.

Clearly, replicating a $60,000 PnP machine for $1500 is going to lead to many compromises, particularly with regard to speed and ability to handle bulk packaging like tape reels. I believe a slow PnP without reel feeders would be useful for hobbyists, maker spaces and possibly prototype jobs for small companies, as long as it is cheap. If you have any requirement for small production runs, then low end or second hand commercial machines go for as little as $3500 and are likely to be a more reliable proposition.

Usually PnP projects are abandoned, ones that reach a workable system come in quite expensive, e.g. £3500. These include PP4 by Volker Besmens, and DIY Pick and Place V2 Project by Brian Dorey.

Several new PnP ideas featured in Hackaday projects recent competition. The Open Source Firepick is probably the most ambitious, aiming for a wide set of features, and for $300 BOM cost. They also have a novel Delta architecture and are aiming to make many parts with 3D printer. So it could be very good, or cheap, I’m not sure both targets can be met.

Also on Hackaday projects was LitePlacer by Juha Kuusama. LitePlacer project is complete, and shipping is planned for later this year. At €1200 it is reasonably priced.

I’ve had a PnP project on the go for a while, but haven’t got much past concept stage. The main construction would borrow some ideas from Reprap 3D printers, using aluminium extrusions, off the shelf precision rods and bearings, and 3D printed parts for custom parts.

Volker Besmen’s PP4 is a real work of genius, and well worth studying. He has released the design under a Open Hardware license too, and there are detailed drawings of most of the machine. In particular, I think his design for an automatic feeder system can be used as a guide for a simple and cost-effective reel feeder. By pulling the cover tape to advance the tape, it solves two problems at once. There are very cheap DC motors available, and it would only need an Arduino with a simple motor driver as a controller.

PP4 auto feeder

I will put up some design files on my github soon.

Posted in Hardware | Tagged | Leave a comment

Sad day for Arduino

The dispute between Arduino SA, and Arduino SRL formerly Smart Projects SRL has gone to court.

Nature of Suit: Trademark
Cause of Action: 15:1121 Trademark Infringement

Smart Projects are official manufacturers of Arduino boards, and it seems they believe they have a claim to the Arduino name and trademarks.

This is bound to distract both companies from supporting and innovating Arduino products, but hopefully will be resolved before too much money is wasted on lawyers.

Posted in Uncategorized | Leave a comment

Generating prototyping board files for Kicad

My love/hate relationship with the Kicad CAD package continues, although I have resolved this year to try to be more positive about it. This week it has been moving more towards love, as I discovered how easy it is to generate Kicad data files for modules and PCBs, and even 3D models.

Kicad uses a syntax for modules and board files based on “S-Expressions”, originally designed for Lisp I think. The syntax is clean, compact and easy to read or edit by hand. It is also really easy to process with software.

Having found a cheap source of PCB manufacture, it’s cheaper to have boards made custom than buy off the shelf stripboard. Since it is rather tedious placing hundreds of pads by hand, I set about generating proto boards for Kicad, using C#.

A couple of evenings later I have this:

pcb_proto_5x10The size of this board is 50 x 100mm to match the budget PCB house, but the bonus of generated data is that it is really easy to adjust the size, layout etc. Previously I have been generating Kicad modules, but for this I generated the kicad_pcb file directly in order to place silkscreen on both sides, which doesn’t appear to work with a module.

The 3D rendering in Kicad is now much nicer, it doesn’t really make any difference to the finished product, but it does help visualise it and gives a more professional feel.

Posted in Hardware | Leave a comment

Maple compatible and generic STM32 development boards

Maple compatibles





Baite maple mini ARM STM32




Generic STM32 boards

Unbranded mini-style


ARM Cortex-M3 STM32F103C8T6 mini STM32 core board development board


Matchbox ARM


Posted in Embedded | Tagged , | Leave a comment

Why experts don’t know shit

The recent, and frankly weird case of Gareth Williams, the MI6 spy who was found dead locked inside a holdall, presents a puzzle. How could anyone but an expert contortionist perform such a feat by himself? It seems obvious that a third party must have been involved, and given the covert nature of the guy’s work, it is easy to suspect all sorts of shady goings on.

Indeed, a series of experts at the inquest into his death asserted that such a feat of self imprisonment was virtually impossible, or actually impossible, although one at least suggested it was not impossible to rule out. I was surprised to hear there is an “expert in confined spaces”, but I guess every field has its expert.

But now a reporter has proved most of them wrong, by performing the “impossible” feat and locking herself into an identical holdall. Woman locks herself in holdall And she manages to maintain decorum throughout. It apparently is just a matter of the right technique, which the experts were not aware of.

Given that the unfortunate Mr Williams had at least once before engaged in apparent self-bondage, common sense would perhaps suggest that it was a repeat attempt gone tragically wrong. It really makes you wonder, these professed experts, are they at all worth listening to? I think a true expert would say “we just don’t know”.

Posted in Uncategorized | Leave a comment

Impressed with Mint

Searching for an alternative to Ubuntu and it’s new interface more friendly for tablets than desktops, I came across Linux Mint. Apparently this is topping the tables at distrowatch, whatever that is. Anyway, I thought I would give it a go on my new PC while it is still fresh out of the box.

Mint has some basis on Ubuntu, and the install proceeded on a familiar path. However, starting the desktop was immediately a breath of fresh air. The interface is clean and crisp. I think they may have overdone the “green mint” theme a tad, but colors are easy enough to change. What impressed me most was the ease of getting to applications and navigating between windows. It strikes me that Mint would also work well on a tablet, but they have managed to do it in such a way that it is equally usable on a desktop.

Linux Mint has a great look, and is easy to use… just like Ubuntu used to be!

Posted in Uncategorized | Leave a comment

How to ruin a great product

It seems that Canonical, the creators of Ubuntu, are taking a leaf out of Microsoft’s play book. Ubuntu was based on the excellent, robust Linux, but most importantly made easy to use. Microsoft also made Windows easy to use, but forgot to make it robust.

Now, I really don’t care about which UI is better, Windows, Mac, Gnome… they all have their fanatics. I have used Windows a lot, I have to use it at work, so I just want something with similar look and feel in Linux – and there was.

Microsoft like to ignore the users, and do what they think is best. So they like to rip up the UI and enforce something completely different. I am still getting used to Vista/Windows 7. At work, we still use XP. This attitude of Microsoft is frankly a pain in the butt, and I was happy to adopt Linux at home instead of following the Microsoft soup wagon.

Only now Canonical have pulled the same stunt. Frankly, Unity sucks. I don’t care if it is technically better than everything else. What I want is a computer I can use! So having weaned me off Windows, and I have gotten to find my way around Ubuntu, Canonical pull the rug and are now making me look for an alternative to Ubuntu.

At least Linux is open unlike Windows, so I am hoping someone will do the necessary work to restore “old” themes like Clearlooks to Ubuntu. Whoever that is, my thanks in advance!

Posted in Uncategorized | Leave a comment

Automatic reboot = automatic loss of data

So it turns out, if you have automatic update enabled in Windows 7, you will end up losing data if Windows decides that because you had to dash off to do something more important without saving everything, it is a good idea to reboot your computer for you.

I sometimes wonder if Microsoft is entirely staffed by idiots. In any normal company, anyone implementing such a brain dead auto reboot idea would be taken round the back and shot. But at Microsoft, losing customer’s data becomes part of the official policy.

Obviously, with any version of Windows getting frequent updates is important, because the OS is so full of security holes it is an easy target for hackers.

So now I have had to turn off automatic updates, in case Windows automatically dumps my work down the can, because I had to leave my computer to do a more important task (there is more to life than PCs you know…).

Great choice Microsoft, lose your work or risk being exposed to malware!.

Posted in Uncategorized | Leave a comment