Search This Blog

Saturday, March 28, 2020

A Time of #EmbraceTheSuck


What a wild start to 2020! I have been swamped with too many things happening, some good, and... as you can guess from the title... some not so good.

The Next Book

Work on my second book for Packt continues. It is focusing on Qt. Unfortunately, it encountered real life. We are still looking for publication this year, but not quite when expected. I'm still really excited about the project. As in my previous book, I am learning so much. There's an old phrase about teaching: Learn-Do-Teach. That's exactly what I am doing, and I love it!

Something Old Comes Back

In the midst of all of that, I got a totally unexpected call for help with a project I last worked on over 6 years ago.

I had setup a Jenkins CI Build machine to build and test a C# project. We used NCover, NUnit, and some custom tools I created. Sometime after development on the product finally finished, someone decided they could get rid of the VM the build machine was running on. Not being able to find a image to restore from, they have called me in to rebuild it.

The cool thing is I get to work with some of the best SW developers I know. I also finally got a job with a consulting company, First Consulting Inc. of Rochester, NY, that I have admired and hoped to work for. It's only a small project, but it should be fun!

The Bad

Amidst the good have been some bad things that have adjusted my focus and challenged my resolve: A family member was diagnosed with cancer, a family friend unexpectedly died, COVID-19 came to New York State, and I was laid off from my full time job. COVID-19's even impacted that. (Most of the past week has been dealing with all of the issues around the latter.)

[For those noting the cancer diagnosis and job loss, and assuming benefits loss: You are right. Luckily the cancer patient qualifies for Emergency Medical Coverage.]

What Now?

Honestly, the first thing is looking for a job that provides benefits. I'm a giver and not much of a receiver. The first time I had to apply for any government assistance was about 10 years ago, and I resisted do it until a friend sat me down and said "John, you paid into the system so it will be there when you need it. Swallow your pride and use it." So, this time I've done my due diligence in that regard and also kicked off the job search.

While I am searching, I plan to be continuing work on my short contract and writing both my book and blog. But, these will probably be at a slower pace. In New York State, every day you "work" is a day you can't collect unemployment. It doesn't matter how much work it is or how much you made from doing it. It just matters that you worked. (I am trying to find answers on what qualifies as "work." Is writing a book if I don't see money immediately? How about this blog? Lots of questions to be answered.)

Want to Help? Here's How

I hate asking for help, but I'm swallowing my pride here:
  • Buy Hands On Embedded Programing with Qt from Packt or Amazon. I doubt I will sell enough to make up what I lost from my job, but everything helps.
  • Read my book and leave reviews.
  • Tell others about my book!
  • If you know of a company looking for a Qt developer, C++11 programmer, and/or author that will allow remote work or is in the Rochester, NY area:  Please let me know and feel free to pass my name on to them. Visit my professional website at http://john.werner-ny.net for my resume and more!

#EmbraceTheSuck

The day before I lost my job I wrote "This is the year of #EmbraceTheSuck." How ironic that it would get suckier.

The phrase "Embrace the suck," comes out of the military. The basic essence of it is "I know the conditions you are in suck, but you are going to have to choose to 'ring the bell' and give up, or push through it. So, embrace the situation, make the best of it you can, and push through it!"
("Ring the bell" is another military expression. It means giving up.)

I have to remind myself everyday to Embrace The Suck and Not Ring The Bell. I even keep a reminder I designed on my desk as a mouse pad.


(The star field was captured by me.)

#EmbraceTheSuck is hard, but it is worth it. Feel free to use the artwork under CC-BY-SA

Stay Safe Everyone!
-John

Monday, February 3, 2020

It's Been A While...

Greetings!


When I started this blog, I promised myself I would do at least one post every month and try for one every two weeks.

That hasn't happened because of... you know.... life.

Anyway, now that we a month into the new year, I thought I would at least post something to let you know I am still alive.

What's Been Happening

After returning from a whirlwind trip and speaking in Pittsburgh back in October, I got to work on my next book, Hands-On Design Patterns with Qt. It has been taking a bit more of my time than I expected. I will say that I am learning a lot during the process and I am already starting to see both where I have been using design patterns for years and how I can more efficiently design things I am coding.

Finding A New Tool

One of the big things I had to figure out while writing my upcoming book was how to draw the pretty pictures I am using for illustrations. I am doing most of my work on Ubuntu Linux Machines, so Microsoft's Visio wasn't a good answer. What else was there?

State Machine Drawings

For state diagrams, I have been using the excellent SCXML Designer that is part of Qt Creator. It has allowed me to create state diagrams in Qt Creator, turn them into executable code, and even export them as PNG files.

This state diagram was a rather complicated example I came up with for a simple Cruise Control system. The really cool thing is that to add it to an existing Qt Project, I only had to tell the Qt project that I was using SCXML, create the diagram, and then import the header file for the working state machine.

I talk more about designing with State Machines in an earlier blog post, Designing with State Machines, and also in my book, Hands-On Embedded Programming with Qt.

With state machine drawings figured out, it left me looking for a tool for Class and Sequence Diagrams. Qt Creator has some support for modelling them, but it wasn't quite a feature rich as I wanted.

Class and Sequence Diagrams

I looked at a variety of solutions. I tried LibreOffice, but it didn't quite do what I wanted. Finally, I settled on the Dia Diagram Editor. It comes with optional support for UML and is available for Windows, Mac OSX and Linux. This GPL'd software works very well for drawing both Class and Sequence Diagrams.  Here are a couple of examples I did for Hands-On Design Patterns with Qt.

The fist is a simple class diagram for the Observer Pattern.



This second is a Sequence Diagram for one of the exercises. It uses the Broker Pattern.


When Hands-On Design Patterns comes out, you will find many more diagrams created with Dia.

Speaking of books, I really should have mentioned this promotion about a month ago, when it started.

Book Sale!

Writing books is one of the things I really enjoy. Part of the enjoyment is in explaining and teaching new things, and another part is in learning new things on the way. 

I keep a copy of my first book, Hands-On Embedded Programming with Qt, on my desk. I treat it much like a notebook. I will often crack the book open and look up the solution to the problem I am trying to solve in Qt. My skill is really in knowing where to find answers, not knowing all of the answers. That's why I refer to my book and other resources like Stack Overflow or the all encompassing search engine of my choice.

Lately, it has been Hands-On Embedded Programming with Qt. I put a lot of work covering just about every aspect of Qt I could fit in the book. While I wrote it for others to learn, I also wrote it for me so I could find the answers to problems I know I have already solved.  (I expect my next book will be the same.)

Right now, Packt is finishing up a sales promotion on all of their books.  You can pick up Hands-On Embedded Programming with Qt for $5 until the end of the promotion. Just follow this link:

Other Things

I have way too many other interests. One of those, Digital Photography, came back in a big way. A few months ago, I purchased a OnePlus 7 Pro [right before the 7T and 7T Pro came out -- duh!]. If you don't know the OnePlus brand, I wouldn't be surprised. They are a bit of a niche Android phone maker for people who are interested in good solid hardware, Android unburdened with bloatware, and reasonable prices. They listen to their customers and try hard to be customer focused.  They have become known for their camera sections. The OnePlus 7Pro (and 7T and 7T Pro) feature three different hardware cameras: Normal, 3X and 0.3X, and they produce very nice quality pictures, sometimes exceeding older DSLRs. (I also carry a Nikon D7000, so I have a basis for comparison.)

Seeing as the OnePlus 7 Pro is easier to carry than my D7000, and I always have my phone on me, I am finding I am taking a lot of pictures with it and looking for opportunities to use it.

I also discovered a myriad of ports of Google's Pixel Phone Camera App. These ports can run on almost any Android phone and often offer a variety of customizations. This led to me seeking out even more software and configuration files to try and taking more photos.  It's an endless cycle, but there are some cool things that have come out.


If you want to check out more of my photos, you can follow me on Instagram, Flickr, or SmugMug.

-John

#Qt5 #Qt #cplusplus #softwareengineering #embeddedprogramming #embeddedDevelopment #programming #embedded #designpatterns #swengineering #scxml #statecharts #statemachines #flckr #instagram #smugmug

Wednesday, October 23, 2019

Designing With State Machines

I can still remember the first time I was introduced to state machines. It was around 1988 when I took a course on compiler design. We used "The Dragon Book,"otherwise known as "Compilers: Principles, Techniques, and Tools."

State Machines

One of the assignments was to build a lexical analyzer (a piece of code that reads texts and turns it into meaningful tokens) to scan through a language called "Babbage."  To do this, we were tasked to build a state machine. If you aren't familiar with state machines, the best way to describe them is that you have a graph of different "states" with "transitions" that move between them.  The following shows a simple state machine that recognizes the words "cat," "cap," and "cub."


State machines are applicable across many different fields.  We already have seen them in relation to tokenizing input, but they can also be used in machine control.  Consider the following state machine that could be part of a simple thermostat.
In this state machine, there are three states: Heating, Cooling, and Idle.  If the system is in the idle state, and "TooCold" is declared, the state machine transitions to the Heating state. Likewise, if the system is in the Heating state and the target temperature is reached ("TargetReached"), the system transitions back to the Idle state.

Compound States

If you look at the thermostat state machine, you will see that it only handles heating and cooling. There is nothing in the state machine that controls the fan on the system. In the following diagram, the FanOn and FanOff states have been added along with a Wait state to introduce hysteresis to the system.

Reading the diagram, we see that when we are in the Idle state we are also in the FanOff state. When we are in the Heating, Wait, or Cooling states, we are also in the FanOn state. We don't need extra transitions to go between the FanOff and FanOn states. The transition to FanOn is automatically made when entering Heating or Cooling, and the transition to FanOff is made automatically when entering Idle.

Knowing what a state machine is is one thing. Knowing how to use them in code is another.

Working With State Machines in Code

There are several ways that we can implement state machines in code. I'll start with a brute force method, then move on successively simpler methods.

Logic Driven State Machines

The first method is through brute force coding of the state machine in the code itself. Switch statements and conditionals are used to control the state transitions. This is one of the first ways I started using state machines, and the method can still be found some "big loop" style control code. Let's look at what this would look like in terms of our first (simplest) thermostat:

    /// Brute Force
    switch (currentState) {
    case Idle:
        if (TooCold) {
            nextState = Heating;
        }
        else if (TooHot) {
            nextState = Cooling;
        }
        break;
        
    case Heating:
    case Cooling:
        if (TargetReached) {
            nextState = Idle;
        }
        break;
    }
    currentState = nextState;
The code doesn't look too bad for only three states, but consider what it would be like for our simple lexical analyzer!  There must be a better way!

Table Driven State Machines

I need to be honest here, for the lexical analyzer assignment I drew a state machine. Instead, I used a piece of software called "lex." "lex" builds a lexical analyzer state machine for you. It takes a set of words (tokens) to recognize and generates C code with state tables. Some might say I cheated, but the professor did not dock my grade.

Most people who code state machines will resort to a table approach. It works well for simple state machines with few states.

    /// Table
    const states stateTable[NUM_STATES][NUM_CONDITIONS] {
        /*              TooHot   TooCold  TargetReached */
        /* Idle    */  {Heating, Cooling, Idle},
        /* Heating */  {Idle,    Heating, Idle},
        /* Cooling */  {Cooling, Idle,    Idle}
    };

    newState = stateTable[currentState][condition];

The table is relatively concise and fast. There are no conditional tests, just simple table lookups.

Problems come when we get more states, need to deal with combined states (e.g. FanOn, FanOff), or try to change the table. Introduce a new state and/or condition, and everything needs to change.

What if we could have the speed of a table driven state machine and we could easily modify the state machine without having manually regenerate or modify the tables?

Graphically Driven State Machines

In case you missed it: I love programming for the Qt Framework. There are many reasons, but here is one of my favorites: 

Starting with Qt 5.7, you can draw state machines in Qt Creator and then easily export and use them in your code.

Modifying your state machine only costs you the rebuilding of your code, not hours of work re-coding brute force machines or modifying state tables.  Even combined states are handled gracefully.

Once you have graphically drawn your state machine, you just need to use it. You start by instantiating the state machine, then you simply connect the signals and slots as appropriate. The following code from my book Hands-On Embedded Programming with Qt shows how the thermostat state machine can be used.

    // Connect state machine states to UI indicators
    m_hvacSM.connectToState("Heating", ui->heatingInd, &QLabel::setEnabled);
    m_hvacSM.connectToState("Cooling", ui->coolingInd, &QLabel::setEnabled);
    m_hvacSM.connectToState("FanOn",   ui->fanInd,     &QLabel::setEnabled);

    // Connect state machine states to the HVAC controller
    m_hvacSM.connectToState("Heating", &m_hvacCtrl, &HVACController::setHeatingOn);
    m_hvacSM.connectToState("Cooling", &m_hvacCtrl, &HVACController::setCoolingOn);
    m_hvacSM.connectToState("FanOn",   &m_hvacCtrl, &HVACController::setFanOn);
    m_hvacSM.start();   // start the state machine

. . .
    // update the state machine based on current temp and the settings
    QString transition;
    if (temperature > ui->maxTemperatureSpinBox->value()) {
        transition = "TooHot";
    } else if (temperature < ui->minTemperatureSpinBox->value()) {
        transition = "TooCold";
    } else {
        transition = "TargetReached";
    }
    m_hvacSM.submitEvent(transition);
While it seems like the code is more complex, it is actually doing a lot more than the other examples. In the other examples, we only advanced the state machine. In this code example, we are also turning on GUI indicators, and we are causing the system to control the fan and heating and cooling elements. Best of all, changing the behavior of the system (like inserting a wait state) doesn't require re-writing the code! Only the state machine changes!

Behind the Scenes

Truth be told, both the second and third implementations are using tables. The real difference is in the second method we have to create and maintain the tables ourselves. In the third method, tooling takes our drawings, creates the tables, and provides a easy to use Qt/C++ Interface.

Conclusion

Hopefully, now that you have learned about state machines you will start using them in your own designs and maybe even use Qt for its built in state machine support.

If you want to learn more about graphically working with state machines in Qt, get a copy of my book Hands-On Embedded Programming with Qt and check out "Designing with State Machines in Chapter 7.  You can find it either on the Packt Website or on Amazon.

Tuesday, October 22, 2019

Meet Qt Pittsburgh 2019 Presentation

I recently presented a Lightning Talk at the Meet Qt event held in Pittsburgh, PA on October 17, 2018.  the topic of my talk was Embedded Development without Hardware. While I plan do a full blog entry about the topic in the future, I thought I would share the presentation with you now.

The presentation file can be be viewed here: http://bit.ly/NoHardwareDev-2019

Enjoy,
-John

Monday, October 7, 2019

Self Promotion, Paying the Bills, or Just some Good Resources

I've spent the last 28 years developing embedded software, the last 10 years working with Qt, and the last year writing a book on how they can be used together.  The book has been published by Packt.

If you haven't checked out Packt's Hands-on books and are interested in learning new software development ideas, you really should see what they have. I have bought several books myself so that I learn things like GPU programming.

For a limited time, Packt is offering some great discounts on their books in print and e-formats, including mine. Check it out: https://bit.ly/2Moffku.

embedded programming with QT

-John

Friday, September 20, 2019

Upcoming Event: Meet Qt Pittsburgh, PA Greater Area

Someone once said, "When it rains, it pours."  Usually, that is a bad thing, but in my case it appears to be a good thing.  I already hinted that I may soon be working on another book about Qt.  That project seems to be going well.

Just after finding out about that, I stumbled into an opportunity to do a Lightning Talk at a Qt event.

I will be giving a Lightning Talk at Meet Qt in Pittsburgh, PA on October 17, 2019.

The "Meet Qt Pittsburgh Greater Area" is a free, half day conference about Qt.

As soon as I heard about the event, I immediately thought of a couple of topics.

The first one was working with State Charts (Machines) in Qt Creator. With a couple of clicks you can turn them into C++ code that you can add to your project.  We have done it several times on a couple of projects here at Caliber Imaging & Diagnostics, and it is really nice to be able to simply update a drawing and fix the behaviour of a system.

While I really like to talk about Qt's SCXML support, I didn't think I could justice to it in 10-15 minutes.

The other topic came out of a question asked during a presentation of Qt's new level of MCU support. Imagine running Qt directly on an MCU without needing an OS!

The question was simple, "How can I develop Qt code if I don't have the hardware?" Great question, and the topic I will be dealing with at Meet Qt, Pittsburgh.

If you are interested in more information about this free event, check out the event page for it at https://www.qt.io/events/meet-qt-pittsburg-1565888110.

I hope to see you there!
-John

Friday, August 30, 2019

And Now For Something Completely Different...

In restarting this blog, my intent is to publish useful, software engineering related material every week or so.  To that point, I have almost 2 months of ideas planned out.  They including everything from using drawings to build state machines to some of the more useful design patterns I have found.

But something came up. Something I think you will enjoy when it is completed, but something that needs some immediate attention.

I have been contacted to write another book for Packt, and I am working to develop the proposal.

What's all this about writing another book?

One of the more frequent questions I when I tell people that I am a published author is "How did that happen?"

There are a couple of ways to get a book published. One method is to write the book and self-publish.

The next is to write a book and then shop it around to different publisher. This is the way many authors start out. The trick is to find a publishing house with an editor that will say yes. It is not easy.

In many ways it is like trying to find a job.  You network, send out "resumes" (or samples of your writing), make calls, etc. etc.

When I was looking for my last two jobs, I did all of that stuff, and it got me nowhere.  In the case of my last two jobs, the companies reached out to me (through a recruiter).  In the case of my first book, the same thing happened.

For "Hands-on Embedded Programming with Qt," an Acquisitions Editor reached out to me to see if I would be interested in writing a book on the book.  I actually declined at first, but he talked me into it, and I am glad he did.  (I like to say I was commissioned to write the book.) It was hard work, and there were some sleep deprived days, but in the end I really enjoyed it.

Recently, the Acquisitions Editor reached out to me with two book ideas.  I read both them and discovered that one proposal is almost exactly the next book I was thinking I should write.

So there you have it.  I've been commissioned to write another Packt book titled "REDACTED" that will be tentatively published in REDACTED of 2020.  I'm really looking forward to it, and I am hoping that now that I know the process, it will go quite easily.

And In the Meantime...

In the meantime, I still plan on covering the topics I had planned for the blog, but will also probably slip hints in about my progress on the new project.

Selling the Merch...

Please Check-out "Hands-on Embedded Programming with Qt".  It is available on both Amazon and Packt websites.