Hacking The Python For Loop

In the early days of C, you’d occasionally see someone — probably a former Pascal programmer — write something like this:

#define BEGIN {
#define END }

This would usually initiate complaints about abusing the preprocessor and generally being anti-C. Surely no modern language would permit such things, right? Perhaps not. Consider [Tushar Sadhwani] who wanted to create a classic C-style for loop inside of Python. He did it, and the journey is perhaps more interesting than the result.

First, you can’t just transport straight C for loops into Python. There has to be some concession to Python syntax. The initial attempt was clever but not clever enough. However, the disassembly of the Python code was telling. The second attempt, however, was particularly interesting.

That attempt used an odd feature to examine the interpreter’s tree structure for the code and then modify it. This is sort of like a very painful C preprocessor but more powerful. That version works although it is pretty convoluted.

Ironically, [Tushar] then set up a third attempt after seeing code that tries to replace Python indentation with braces using a codec. In Python-speak, a codec lets you convert different text encodings. However, you can do other things than text encoding conversion. This is closest in spirit to the C preprocessor method. You can wade through the source code ahead of processing and make whichever changes you see fit.

Is any of this really useful? Probably not as it is. But you never know when you might need to do something exotic and one of these techniques could save the day. You probably couldn’t get away with some of this on MicroPython, of course. Your mileage may vary depending on where you find your Python running — like the Web.

Hackaday Links Column Banner

Hackaday Links: February 5, 2023

Well, this week’s Links article is likely to prove a bit on the spicy side, thanks in no small part to the Chinese balloon that spent the better part of the week meandering across the United States. Putting aside the politics of the whole thing — which we’ll admit is hard to do, given the state of the world today — there are some interesting technical aspects to this story, which the popular press has predictably ignored. Like the size of this thing — it’s enormous. This is not even remotely on the same scale as the hundreds of radiosonde-carrying balloons sent aloft every day, at least if the back-of-the-envelope math thoughtfully sent to us by [Dr_T] holds up. If the “the size of three buses” description given in most media reports is accurate, that means a diameter of about 40 meters, for a volume of 33,500 cubic meters. If it’s filled with helium — a pretty safe bet — that makes its lifting capacity something like three metric tons. So maybe it was a good idea to wait until it was off the Carolinas to shoot it down.

Continue reading “Hackaday Links: February 5, 2023”

Behold A Microscope That Sees By Squashing Things Into It

“Look with your eyes, not your hands” is something many of us have heard while growing up, but that doesn’t apply to the touch-sensitive microscope [Steve Mould] got to play with.

Gel pad removed, exposing lens and multi-directional lighting.

The wand-like device is made by Gelsight, and instead of an optical lens like a normal microscope, it sports a gel pad on the sensing end. By squashing an object into the gel, the device is able to carefully illuminate and image the impression created. By taking multiple images lit from different angles, a lot of information can be extracted.

The result is a high-resolution magnification — albeit a monochromatic one — that conveys depth extremely well. It’s pretty neat clearly seeing tiny specks of dust or lint present on surfaces when [Steve] demonstrates imaging things like coin cells.

Many a hacker knows that the devil is in the details when it comes to executing an idea. Even so, the basic principles of the Gelsight seem simple enough and possibly within the realm of inspired DIY in the same way that we saw a CNC gantry and USB microscope repurposed as an optical comparator.

Watch the Gelsight in action in the video below, embedded below the page break.

Continue reading “Behold A Microscope That Sees By Squashing Things Into It”

Dishwasher Repair Nightmare: Chasing Down 3 Faults

It all started with a vague error code (shown in the image above) on [nophead]’s Bosch SMS88TW01G/01 dishwasher, and it touched off a months-long repair nightmare that even involved a logic analyzer. [nophead] is normally able to handily diagnose and repair electronic appliances, but this time he had no idea what he was in for.

Not many dishwashers require breaking out a logic analyzer and 3D-printed custom adapters, but this one did.

Not only were three separate and unrelated faults at play (one of them misrepresented as a communications error that caused a lot of head-scratching) but to top it all off, the machine is just not very repair-friendly. The Bosch device utilized components which are not easily accessible. In the end [nophead] prevailed, but it truly was a nightmare repair of the highest order. So what went wrong?

One error appears to have been due to a manufacturing problem. While reverse-engineering the electronics in the appliance, [nophead] noticed a surface-mounted transistor that looked crooked. It was loose to the touch and fell into pieces when he attempted to desolder it. This part was responsible for switching an optical sensor, so that was one problem solved.

Another issue was a “communications error”. This actually came down to ground leakage due to a corroded and faulty heater, and to say that it was a pain to access is an understatement. Accessing this part requires the machine to be turned upside down, because the only way to get to it is by removing the base of the dishwasher, which itself requires a bizarre series of awkward and unintuitive steps to remove. Oh, and prior to turning the machine upside down, one has to purge the sump pump, which required a 3D-printed adapter… and the list goes on.

And the E02 error code, the thing that started it all? This was solved early in troubleshooting by changing a resistor value by a tiny amount. [nophead] is perfectly aware that this fix makes no sense, but perhaps it was in fact related to the ground leakage problem caused by the corroded heater. It may return to haunt the future, but in the meantime, the machine seems happy.

It goes to show that even though every fault has a cause and a reason, sometimes they are far from clear or accessible, and the road to repair is just a long slog. Heck, even phones these days can be bricked by accidentally swapping a 1.3 mm screw for a 1.2 mm screw.

Dumping script window, showing the bytes being dumped one by one from the STM chip

Need To Dump A Protected STM32F0x? Use Your Pico!

Sometimes, security mechanisms can be bypassed if you just do things slightly out of the ordinary. For instance, readout protection on microcontrollers is a given nowadays, to the point where it’s intentionally enabled and relied upon as a major technical measure to protect intellectual property. The gist is — when you connect to a microcontroller over its debug interface and then ask to read its flash memory, it will politely refuse. However, [Racerxdl] shows us that in practice, it’s not flawless protection – for certain chips, you just need to be a little quicker than usual.

Usually, flashing and debugging software will chat with the microcontroller for a bit, and probe parameters before going for any direct requests. However, if you skip the courtesy and bluntly get to the point immediately right after power is applied to the microcontroller, you can intimidate them just enough to give you one byte of its memory before it refuses to cooperate further. Since that can be any byte you wish, you can read the entire flash — one byte at a time.

You need to power cycle the chip before you can progress, so the hardware does involve a bit more than just an SWD interface, and it will take a fair bit more time than reading out a non-protected chip the usual way; plus, of course, the debugging interface needs to be active for this in the first place, which isn’t always the case. However, it still beats paying a few thousand dollars for a factory in China to decap your chip and read it out using a fancy machine.

[Racerxdl] didn’t just write a proof-of-concept for this attack – they implemented it for one of our favourite chips, the RP2040. As such, you no longer need an unobtainium STM32 to dump an unobtainium STM32.

To be clear, [Racerxdl] didn’t design this attack — it’s been around for some time now. Credit for that goes to Johanes Obermaier. All in all, this is a wonderful reminder that seemingly reliable security mechanisms can be foiled by the simplest tricks. For instance, if your chip erases the flash when you unlock its protection, you can just tell it not to.

The Effect Of Filament Color On Print Strength And More

What is an FDM filament coloring’s purpose but to be an aesthetic choice? As it turns out, the additives that create these changes in coloring and transparency also affect the base properties of the polymer, whether it’s PLA, PETG, or another material. This is where a recent video by [CNC Kitchen] is rather illustrative, using a collection of colored PLA filaments from a single filament manufacturer.

[CNC Kitchen] ran a range of tests including tensile strength, ductility, layer adhesion, impact resistance, and annealing performance. The results showed no clear overall winner between plain PLA polymer and any specific color. Perhaps most fascinating was just how much these color additives change the material’s response to annealing. Baking the PLA at 100°C for 30 minutes generally improves material properties, but also can cause warping and shrinking. The effective warping and shrinking differed wildly between the filament.

The general conclusion would seem to be that the natural polymer isn’t necessarily the optimal choice, but that you should test and pick the filament from a specific manufacturer to fit your project’s needs.

Continue reading “The Effect Of Filament Color On Print Strength And More”

Sundial Collection Is 2D Printed

We see a lot of clocks, and many of the better ones have some 3D printed elements to them. But [Carl Sabanski] shows us his kits for making sundials for either hemisphere using a conventional printer (you know, one that puts ink on paper), some styrofoam, and possibly some other materials like wire coat hangers, threaded rods, thumbtacks, glue, and different papers like transparencies or card stock.

In all, there are 21 different kinds of sundials. Some are pretty standard-looking fare, but there are others, like the pinwheel equatorial sundial or the cycloid polar sundial, which might be surprising. One even uses a CD as a kind of indicator.

Continue reading “Sundial Collection Is 2D Printed”