Chad Dotson

A small town Computer Scientist / Software Engineer. Chad enjoys writing Python and JavaScript as well as tinkering with his Raspberry Pi and Arduino. When not programming, he enjoys Photography (especially lightning) and Sci-Fi.
A small town Computer Scientist / Software Engineer. Chad enjoys writing Python and JavaScript as well as tinkering with his Raspberry Pi and Arduino. When not programming, he enjoys Photography (especially lightning) and Sci-Fi.

Not Invented Here, Not Written By Me, and Reinventing The Wheel

Not invented here and not written by me are both driving factors in reinventing the wheel when developing software.

We limit ourselves if we do not build upon the achievements of others. – Chad Dotson

Not Invented Here

I’m sure everyone has encountered developers that would prefer to implement everything themselves instead of using a library.  An example would be not using jQuery or underscore (or comparable libraries) on a web project.

This is a serious problem for several reasons.

  • It needlessly increases development time.
  • It potentially leads to less robust code and/or increased testing time.
  • It potentially leads to less maintainable code.

I’m not saying that libraries should always be preferred over your own code, but they should be strongly considered.  If you choose to re-implement what a library gives you, you should prepare some defensible reasons for not going with the library.

More: Wikipedia

Not Written by Me

This is a more refined, narrower case of Not Invented Here.  Those developers who don’t want to spend the time or have difficulty understanding code written by others often reimplement code because they view it as the simpler solution.  This is a falsity and they hurt their overall code quality and momentum for it.

Some common things you will hear are:

  • I don’t know what that code does.
  • I would spend a shorter amount of time rewriting it.  (Which is most likely a falsehood.)

The Core of the Issue

As I’ve said, I think the core of the issue is that we find it harder to understand what someone else writes vs what we write ourselves.  We must apply programming best practices and resist the urge to reimplement the past.  To grow, we must push past our tendencies and continue to move forward to bigger and better things.

Posted by Chad Dotson in Doing Things Better, Key Concepts, Programming, Software Engineering, 0 comments

C-Style Unions And Python

So, you’re creating a C Union (used to create a variant type) and writing it to a file, socket, etc and want to read that in Python. There are two ways to deal with this.

Assume the following union definition in C

typedef union {
    int i;
    unsigned int u;
    float f;
} data_t;

In C, reading the value represented by this is easy.  Since its 4 bytes, you simply read 4 bytes and then reference the appropriate element.  In Python, if your looking for functionality to closely match C, it seems not so straight forward.

struct.pack and struct.unpack

The first thing you try to do is look at the struct module and see if pack and unpack can come close to doing what you want.  The problem with pack and unpack is that it requires a data type.

from struct import unpack

# byte_buffer -> binary input
# var_type -> variable type indicator

unpack_code = ''

if var_type == 'INT':
    unpack_code = 'i'
elif var_type == 'UINT':
    unpack_code = 'I'
elif var_type == 'FLOAT'
    unpack_code = 'f'

var = unpack(unpack_code, byte_buffer)[0]

This works just as well as anything and is completely straightforward, the big problem here is speed.  First, we have to do an if around each call to unpack to get the appropriate option.  Second, its faster to pull in arrays in python than single values.

A ctypes addition to struct.pack and struct.unpack

Using ctypes, you can approach a functionality similar C.  Take the following code for example.

from ctypes import c_int, c_uint, c_float, Union
from struct import unpack

class MyType(Union):
    _fields_ = [("i", c_int), ("u", c_uint), ("f", c_float)]

var = MyType()

var.i = unpack("i", byte_buffer)[0]

# then var.i, var.u, var.f contain the integer, unsigned int, and float representations respectively.

Notice that it is always unpacking the data as an integer into the integer part of the union.  This approach has a few advantages.  One, it functions the same as the C version of the code would.  Two, you can unpack entire arrays at once which can be faster.

Conclusions

The first code sample seems to be the simplest and most straightforward though potentially slower.  However, depending on the situation, you may want an implementation similar to the second.

Posted by Chad Dotson in Programming, 0 comments

DSLR Lightning Trigger 3

DSC_0539

Finally Some Lightning … IT WORKS!

Tonight, I was finally able to deploy the prototype of my lightning trigger.  The storm wasn’t particularly photogenic, but it at least helped prove the concept.  Lightning was slim and no bolts were in the best area for my camera, but the camera did capture the image to the right.  Not very good I know, but if it will work for such a poor example of lightning, I think during a real storm it will perform splendidly.  My next steps are to research moving to a wired trigger (replacing an MC-DC2 ) instead of the IR LED, add a potentiometer to adjust the sensitivity in the field, and installing the circuit into a project box.

Lessons Learned / Observations

Perhaps this won’t be a problem during a storm with a lot of lightning, but one problem that I encountered was that the camera would exit the “Quick-response remote” release mode and return to my prior setting in the absence of regular input.  I guess this was due to the camera entering a suspended state.  I will have to see if I can modify this setting, if not I may attempt to keep the camera awake.

Other Posts in this series:

 

 

Posted by Chad Dotson in Arduino, Hobbies, Photography, Programming, Software Engineering, 1 comment

Decisions in 1 Pomodori

All programmers suffer from analysis paralysis at some point in their career.  The trick is bringing it to a swift conclusion.  Try this to move forward next time.  Give yourself 1 pomorodi (25 minutes) to analyze the problem.  After that make a decision and go with it.  Whatever falls out is whatever falls out.  At least you were not over-analyzing the problem and getting nowhere.

Posted by Chad Dotson in Doing Things Better, Misc, Programming, 0 comments

DSLR Lightning Trigger 2

Lightning Drought

Back in September, I created a simple Arduino-based lightning trigger (original article).  I’ve been waiting since then for the chance to test the circuit in the field.  Yesterday provided the first real chance to test the circuit; unfortunately, I missed it.

Improving the Program

Even though I missed the event, I did decide to improve the program a little.  The major change, if it can be called major, addresses a weakness in the original program.  Originally, every event would attempt to trigger the shutter twice (once when the lightning occured and again when the brightness returned to normal).  To fix this I am removing the absolute value function from the brightness test.  Now, it will only trigger if the new brightness is brighter than the previous.

#include <multiCameraIrControl.h> 

int shutterPin = 13; 
int triggerPin = 0;
int threshold = 10;
int savedLightningValue = 0;
int currentLightningValue = 0;
int delayBetweenShots = 1000;
 
Nikon shutter(shutterPin);

void setup() {
  Serial.begin(9600);
  pinMode(shutterPin, OUTPUT);
  digitalWrite(shutterPin, LOW);
  savedLightningValue = currentLightningValue = analogRead(triggerPin);
}

void loop() {
  currentLightningValue = analogRead(triggerPin);
  Serial.println(savedLightningValue, DEC);
    
  if((currentLightningValue - savedLightningValue) > threshold) {
    Serial.println("Triggering shutter");
    shutter.shutterNow();
    delay(delayBetweenShots);
  }
  
  savedLightningValue = currentLightningValue;
}

Resources:

Other Posts in this series:

Posted by Chad Dotson in Arduino, Hobbies, Photography, Programming, Software Engineering, Technology, 5 comments

Becoming an Entrepreneur as a Software Engineer Vol 2

This is part two of my discussion and thoughts on seeking to become an entrepreneur as a software engineer.  These are my current thoughts on the process and how to achieve my overall goals in becoming an entrepreneur.  This entry centers on the belief that becoming an entrepreneur occurs in several distinct phases.  Currently, I have identified three core stages.  During each of these phases our rolls and responsibilities change and grow drastically.

Stage 1: Working for someone else

This stage is the simple “do work” stage.  We work to fulfill someone elses vision, we work to complete their goals and bring life to their ideas.  We work to solve their problems.  Since we are, at our core, this state is the default for most workers in industry.

Stage 2: Startup

This stage where we work to get our company off the ground.  We are working to fulfill our own vision.  We have identified a product or service area and are actively working to produce something.  We may or may not have a small team but at worst case we are the pitchman, sales rep, accountant, architect and coder all in one.  We are still the problem solvers, just with more hats to wear.

Stage 3: Liftoff

In the other stages we still participated in the day to day work being done, in this one we have progressed to something else.  What that may be is up to the structure of the company.  We have at the very least transitioned from problem solver at the code level to problem solver at the company level.  We are actively deciding the direction of our company and product.  We transitioned to a form of problem creator for the people in our company.

Posted by Chad Dotson in Programming, Software Engineering, Work, 0 comments

On Software Engineering

Spring

Writing from the patio on a much deserved day off. What a day, sunny and 68 this 3rd day of spring. It sometimes makes it real hard to work inside in a windowless box. It’s been a busy day of a different sort, but dang I could get used to it.

Software Engineering Is Consuming

Software Engineering can and is absolutely consuming work.  The short of it is, we like what we do so we tend to focus a lot of attention on it.  We are problem solvers, designers, learners, and the list goes on.  I am of the philosophy that you should find and do something you like because anything else is a waste.  I guess that philosophy has its pros and its cons.  Is it so bad when your day consists of writing code, solving problems, researching, and learning new things?

Achieving Balance

I guess everyone talks about work/life balance and its true; you must always take time for yourself.  Get outside, do some walking.  It will help clear your head and it is a good stress reliever.  Maybe you’ll come back to the task with some fresh ideas and renewed vigor.  The schedule will always be there.

Personally as of April, I will have been on a diet and exercise plan for 2 years.  I’ve lost a lot and still need to lose more.  One of my biggest problems has been shorting myself on time to maintain my walking and weight lifting.  I seem to always get wrapped up in something.

Having a rewarding hobby is probably a good idea.  I’m not talking about coding a side project (don’t we all seem to have an overabundance of those), I’m talking about something else entirely.  My non-programming hobby is photography.  I don’t get to do it much it seems anymore it seems, but I do find winter a dreary time to take photos.  I did have a major accomplishment in this area last fall.  I officially photographed a wedding.  While I was originally scared to take on such a task, the photos turned out wonderfully.  I took the photo below today.

2015_03_23_Spring_Bradford_Pear

Bradford Pear Blooming Spring 2015

 

Posted by Chad Dotson in Hobbies, Key Concepts, Photography, Programming, Ramblings, Software Engineering, 0 comments

A Better AsyncTestCase for Python

If you need asynchronous support in python unit tests, there are a few possibilities.  A simple google search points you to a few packages, one being tornado.  Tornado’s tornado.testing.AsyncTestCase seems straight-forward.  However it does have a shortcoming: it’s not thread-safe.  Trying to use it with a thread will result in a sporadic exception.  Consider this thread-safe, functionally similar alternative.

from threading import Event
from unittest import TestCase


class TimeoutError(Exception):
    pass


class AsyncTestCase(TestCase):
    """ Provides functionality similar to tornado.testing.AsyncTestCase except is thread-safe.
    """
    def __init__(self):
        self._done = Event()
    
    def setUp(self):
        super(AsyncTestCase, self).setUp()
        self._done.clear()

    def tearDown(self):
        super(AsyncTestCase, self).tearDown()
        self._done.clear()

    def wait(self, timeout=None):
        """ Wait for event to be set.  Raise an exception if the event isn't set by the timeout.
        """
        if timeout is None:
            timeout = get_async_test_timeout()

        self._done.wait(timeout)

        if not self.is_done():
            raise TimeoutError()
        
    def stop(self):
        """ Set the event to terminate wait.
        """
        self._done.set()
    
    def is_done(self):
        return self._done.isSet()

# taken from tornado.
def get_async_test_timeout(default=5):
    """Get the global timeout setting for async tests.
    Returns a float, the timeout in seconds.
    """
    try:
        return float(os.environ.get('ASYNC_TEST_TIMEOUT'))
    except (ValueError, TypeError):
        return default

 

 

 

Posted by Chad Dotson in Doing Things Better, Programming, 0 comments