Ethan McTague

A Messy Project

For my 12th grade Computer Engineering final project, I decided to build a 3-D scanner. It didn't quite turn out as I had hoped due to time and hardware constraints, unfortunately, but it does work!

Here's the source code in all its messy glory.

Python and Decorators

The python script, meant to run on a Raspberry Pi, operates a stepper motor and ultrasonic sensor to measure the distance to objects surrounding it. Not the most impressive thing ever, but I was proud of this bit, one of the few times I've actually used decorators:

indlevel = 0

def task(name, resname):
    def func_decorator(func):
        def func_wrapper(*args, **kwargs):
            global indlevel
            print("%s[Begin %s]" % (("\t" * indlevel), name), file=sys.stderr)
            indlevel += 1
            result = func(*args, **kwargs)
            indlevel -= 1
            print("%s[End %s=%s]" % (("\t" * indlevel), resname, result), file=sys.stderr)
            return result
        return func_wrapper
return func_decorator

This decorator can wrap a function to assist in logging. It logs the beginning and end of a function's execution, as well as its result, in a perfectly indented tree.

Here's an example:

@task("Doing A Thing", "result")
def doThing():
    return 10.5

@task("Doing An Outer Thing", "result")
def doOuterThing():
        doThing()
        doThing()
        return 20.0

doOuterThing()
[Begin Doing An Outer Thing]
    [Begin Doing A Thing]
        [End result=10.5]
        [Begin Doing A Thing]
        [End result=10.5]
[End result=20.0]

This little decorator made debugging much, much simpler, because a single line above a function definition would allow me to see exactly when it's called and what it returns.

'HERE' strings in C++?

It turns out that C++11 adds a new type of string literal, and that I didn't even realize until I set out to work on this project! Wrapping a string in R"<delim>( and )<delim>" allows you to write raw, multi-line string literals, similar to here/EOS strings in other languages!

I used this to avoid implementing file loading for some shader files, instead embedding them at compile-time. To accomplish this, simply put the beginning and ending of the new string literal syntax at the start and ends of the file you want to embed, and then #include it:

embeddable.txt

R"HERE(
This is the text to be embedded!
Look, it's multiple lines!
Works like a charm!
)HERE";

main.cpp

std::string embeddedText =
#include "embeddable.txt"

Non-blocking stdin? Sorta...

I needed a game loop to run in parallel with reading from standard input. Unfortunately, there's no reasonable, reliable way to check for new input in a non-blocking manner.

It seems the best workaround is to spawn a thread that reads from standard input and notifies the main thread of new input. It works, as shown here, but the fact that it's just sorta killed mid-execution when the main thread needs to terminate is a little sketchy to me. If anyone has any idea how to cleanly kill off the input thread, please get in touch - ethan@tague.me.

inputThread = std::thread([&]
{
        float value;

        while (std::cin >> value) {
                lockNewData.lock();
                newData.push_back(value);
                notifyNewData = true;
                lockNewData.unlock();
        }
});
if (notifyNewData) {
    lockNewData.lock();
    // Handle new data from newData here!
    notifyNewData = false;
    lockNewData.unlock();
}
Monday, June 17, 2019 at 05:12 PM
Log In