Unionize Your Variables – An Introduction To Advanced Data Types In C

Programming C without variables is like, well, programming C without variables. They are so essential to the language that it doesn’t even require an analogy here. We can declare and use them as wildly as we please, but it often makes sense to have a little bit more structure, and combine data that belongs together in a common collection. Arrays are a good start to bundle data of the same type, especially when there is no specific meaning of the array’s index other than the value’s position, but as soon as you want a more meaningful association of each value, arrays will become limiting. And they’re useless if you want to combine different data types together. Luckily, C provides us with proper alternatives out of the box.

This write-up will introduce structures and unions in C, how to declare and use them, and how unions can be (ab)used as an alternative approach for pointer and bitwise operations.

Continue reading “Unionize Your Variables – An Introduction To Advanced Data Types In C”

Forth: The Hacker’s Language

Let’s start right off with a controversial claim: Forth is the hacker’s programming language. Coding in Forth is a little bit like writing assembly language, interactively, for a strange CPU architecture that doesn’t exist. Forth is a virtual machine, an interpreted command-line, and a compiler all in one. And all of this is simple enough that it’s easily capable of running in a few kilobytes of memory. When your Forth code is right, it reads just like a natural-language sentence but getting there involves a bit of puzzle solving.

robot_forth_had-colors
From Thinking FORTH (PDF)

Forth is what you’d get if Python slept with Assembly Language: interactive, expressive, and without syntactical baggage, but still very close to the metal. Is it a high-level language or a low-level language? Yes! Or rather, it’s the shortest path from one to the other. You can, and must, peek and poke directly into memory in Forth, but you can also build up a body of higher-level code fast enough that you won’t mind. In my opinion, this combination of live coding and proximity to the hardware makes Forth great for exploring new microcontrollers or working them into your projects. It’s a fun language to write a hardware abstraction layer in. Continue reading “Forth: The Hacker’s Language”

All About Eve

Most programming languages today look fairly similar. There’s small differences, of course (Python using spaces, Ruby and Perl have some odd-looking constructs). In the 1960s and 1970s, though, a lot of programming languages were pretty cryptic. Algol, APL, and LISP are great examples of unusual looking programming languages. Even FORTRAN and PL/1 were hard to read. RPG and COBOL were attempts to make programming more accessible, although you could argue that neither of them took over the world. Most programming languages today have more similarity to FORTRAN than either of those two languages.

A new programming language, Eve, claims to be based on years of research in programming from a human perspective instead of from the computer’s. The result is a language that works by pattern matching instead of the usual flow of control. It is also made to live inside of Markdown documents that can serve as documentation. You can see a video about Eve, below.

Neither of these are totally new ideas. SNOBOL, AWK, and Prolog all have some pattern-matching involved. [Donald Knuth] was promoting literate programming back in the 1980s. However, Eve understands modern constructs like web browsers.

Continue reading “All About Eve”

Hacklet 114 – Python Powered Projects

Python is one of today’s most popular programming languages.  It quite literally put the “Pi” in Raspberry Pi. Python’s history stretches back to the late 1980’s, when it was first written by  Guido van Rossum. [Rossum] created Python as a hobby project over the 1989 Christmas holiday. He wanted a language that would appeal to Unix/C hackers. I’d say he was pretty successful in that endeavor. Hackers embraced Python, making it a top choice in their projects. This week’s Hacklet focuses on some of the best Python-powered projects on Hackaday.io.

pytoolWe start with [Jithin] and Python Powered Scientific Instrumentation tool, his entry in the 2015 Hackaday Prize. [Jithin] has created an “electronics lab in a box” style tool that can compete with commercial products with price tags in the thousands. Python Powered Scientific Instrumentation tool uses simple microcontroller powered hardware to create programmable gain amplifiers, waveform generators, LCR meters, CC sources and more. The microcontroller handles all the real-time operations. Data processing happens on a connected PC running Python scripts. Popular Python libraries like Scipy make signal processing and waveform displays easy.

 

pymusicNext up is [Bill Peterson] with jamPi. [Bill] loves his music keyboard, but hates having to lug around a laptop, audio interface, and all the associated cables. He needed a device which would be as flexible as a PC-based synthesizer, but as simple and compact as a MIDI sound module. JamPi does all this and more. [Bill] is using fluidsynth to generate sound. The control and interface software is handled in Python with the help of the fluidsynth.py module. All this functionality is wrapped up in a simple box with a 2 line character LCD. Now [Bill] is ready to jam anytime, anywhere.

openmv-featureNext is [i.abdalkader] with OpenMV, his entry in the 2014 Hackaday Prize. [i.abdalkader’s] goal was to create “the Arduino of machine vision”. He’s well on his way to accomplishing that. In 2015, OpenMV had a successful Kickstarter campaign. After a few manufacturing glitches, customers are now receiving their devices. OpenMV is a low-cost Python-powered machine vision device. An ARM microcontroller coupled to a simple image sensor makes up the core of the device. The camera is programmed in MicroPython, with the help of many image processing libraries created by the OpenMV team. [i.abdalkader] even created his own IDE using Glade and PyGTK.

pyfaceFinally we have [osannolik] with Calibration and Measurement Tool. Have you ever want to display a few debug parameters from your embedded project, but didn’t have the display real estate (or any display at all)? What about changing a parameter without pulling out your JTAG setup and firing up your debugger? [Osannolik] has created a simple Python powered PC-based front end which can be used as a Swiss army knife for developing embedded systems. Variables can be displayed in real-time, parameters changed. Even graphs are available thanks to pyqtgraph.

If you want more Python-powered goodness, check out our new Python-powered project list! Did I miss your project? Don’t be shy, just drop me a message on Hackaday.io. That’s it for this week’s Hacklet. As always, see you next week. Same hack time, same hack channel, bringing you the best of Hackaday.io!

Pen-Plotter Firmware Written Completely In Ada

[Fabian Chouteau] built a plotter out of CD-ROM parts. Yawn, you say? Besides being a beautiful physical build, this one has a twist. He wrote the software and firmware for the entire project himself, in Ada.

Ada is currently number two on our list of oddball programming languages that should be useful for embedded programming. It’s vaguely Pascal-y, but with some modern object-oriented twists. It was developed for safety-critical, real-time embedded systems (by the US Department of Defense), and is used in things like airplanes, rockets, and the French TGV trains. If that sounds like overkill for your projects, [Fabian]’s project shows that it’s still very tractable.

In his GitHub, he re-implements the GRBL G-code generator and then writes a GUI front-end for it. In his writeup, he mentions that the firmware and its simulator for the front-end use exactly the same code which is quite a nice trick, and guarantees no (firmware) surprises when moving from the modelled device to the real thing.

We looked quickly around for Ada resources and came up with: GNAT, the GNU Ada compiler, and its derivatives: GNAT for ARM (STM32-flavor), ARM-Ada (LPC21xx flavor), AVR-Ada, and MSP430-Ada.

Any of you out there use Ada in embedded work? We’d love to hear your thoughts.

Continue reading “Pen-Plotter Firmware Written Completely In Ada”

Learn Functional Reactive Programming On Your Arduino

Everyone loves learning a new programming language, right? Well, even if you don’t like it, you should do it anyway, because thinking about problems from different perspectives is great for the imagination.

Juniper is a functional reactive programming language for the Arduino platform. What that means is that you’ll be writing your code using anonymous functions, map/fold operations, recursion, and signals. It’s like taking the event-driven style that you should be programming in one step further; you write a=b+3 and when b changes, the compiler takes care of changing a automatically for you. (That’s the “reactive” part.)

functionalIf you’re used to the first-do-this-then-do-that style of Arduino (and most C/C++) programming, this is going to be mind expanding. But we do notice that a lot of microcontroller code looks for changes in the environment, and then acts (more or less asynchronously) on that data. At that level of abstraction, something like Juniper looks like a good fit.

Continue reading “Learn Functional Reactive Programming On Your Arduino”

The rust language logo being branded onto a microcontroller housing

Programming With Rust

Do hardware hackers need a new programming language? Your first answer might be no, but hold off a bit until you hear about a new language called Rust before you decide for sure.

We all know real hackers use assembly language to program CPUs directly, right? Well, most of us don’t do as much assembly language as we used to do. Languages like C can generate tight, predictable code and are easier to manage.

Although some people use more abstract languages in some embedded systems, it is no secret that for real-time systems, device driver development, and other similar tasks, you want a language that doesn’t obscure underlying details or generate code that’s difficult to reason about (like, for example, garbage collection). It is possible to use special techniques (like the Real-Time Java Specification) to help languages, but in the general case a lean language is still what most programmers reach for when you have to program bare metal.

Even C++, which is very popular, obscures some details if you use things like virtual functions (a controversial subject) although it is workable. It is attractive to get the benefit of modern programming tools even if it does conceal some of the underlying code more than straight C.

About Rust

That’s where Rust comes in. I could describe what Rust attempts to achieve, but it is probably easier to just quote the first part of the Rust documentation:

Rust is a systems programming language focused on three goals: safety, speed, and concurrency. It maintains these goals without having a garbage collector, making it a useful language for a number of use cases other languages aren’t good at: embedding in other languages, programs with specific space and time requirements, and writing low-level code, like device drivers and operating systems. It improves on current languages targeting this space by having a number of compile-time safety checks that produce no runtime overhead, while eliminating all data races. Rust also aims to achieve ‘zero-cost abstractions’ even though some of these abstractions feel like those of a high-level language. Even then, Rust still allows precise control like a low-level language would.

Continue reading “Programming With Rust”