Category Archives: Programming

Posts that relate to Programming, Software Development, Software Engineering, Software Design or Architecture, and even maybe shell scripting (the programming part of systems administration)

Book Review: BeagleBone Robotics Projects

I was asked by Packt Publishing if I would read and review this book. I’ve owned a BeagleBoneBlack for a little while now. My use case was not robotics. This book might shed some new light on my old Black, so I agreed to review it.

The book starts off very accessible. Chapter 1 covers just about everything I did with my BBB when I first received it, hooking it up like a PC, replacing the default distro, making sure I could SSH to it were all in there. The author, Richard Grimmett, goes a step further and installs XFCE gui and vncserver and walks through connecting from a Windows PC using vncclient. All in all, chapter 1 is a great super basic tour.

Chapter 2 dives into programming on the thing and introduces Python. It does it in a really weird (to me) way. It has the reader running emacs in a putty window remote connected to the device. This must just feel weird to me because I do a lot of remote programming and its never with emacs (I’m a vim guy) and its rarely remote. For a new user, it seems to me like it would have been simpler and more friendly to say “use an editor of your choice” and “here is notepad2 or sublime” along with “here is how you copy files to and from the device.” I think this is mostly my background causing me to see things differently. The emacs in putty walk-through is very adequate.

Its not a programming book, so this is really a nit pick, but technically some of the descriptions of python aren’t really true. For example, if __name__==”__main__”: does not “tell the program to begin its execution at this point.” Again I’m nit picking, but I do feel like a different phrase that isn’t so very false to someone who knows python could have been found. Still, its not a programming book. The beginning of the chapter does list many resources for learning python.

Ugh, and then the book moves on to C++ and has quotes like this, “C++ is the original language of Linux” I’ve used Linux for almost as long as I’ve programmed C, and I am very (perhaps overly?) sensitive to the difference between C and C++.

OMG what do you mean Speech Input and Output? Really?  Chapter 3 tackles it. Really. For real. Speech Input and Output on that tiny little board. I can make my own Siri! This is a really cool topic; espeek is something I’ve only played with a little bit prior to reading this. It looks fun.

Speech recognition is done with software I’ve never used before called PocketSphinx. It isn’t packaged and so one has to compile it. Pretty sweet BBB being able to compile stuff like that. (I’m thinking of iOS and Android where I’ve not seen a compiler run on device.) The demo walks through limiting the grammar of speech input so that you don’t have to train the recognizer.

I’m a programmer, so I’m going to nitpick programmer things. I really wish authors wouldn’t do this, “I like to make a copy of the current file into continuous.c.old, so I can always get back to the starting program if it is required.” I really do wish authors would just say “go read about version control systems.”

Whew, speech is fun. Next step is video. Hook up a webcam and let’s do some image recognition. The book walks through OpenCV and it is as this point that we are forced to do a bunch of Linux sysadmin stuff to make our SD have enough free space to have a dev environment. This really could have gone anywhere in the book. I kind of like that it put it off until it was necessary.

The python image tracking example using OpenCV looks pretty cool. It is a complete example without going too deep or going off in the weeds.

Making the Unit Mobile introduced me to mobile platforms. The Magician Chassis that the book shows first, I found online for under $20! I knew that this stuff was accessible, but this is downright cheap. I feel almost guilty NOT getting one and trying it out.

The motor controller tutorial looks very straightforward. I already have ideas for code changes. Immediately after the simple time based tutorial it goes into speech controlled movement, which is pretty sweet.

After the wheeled robot tutorial is a walking robot example. The author makes a compelling argument for this type of robot, and the Pulse Width Modulation servo motors are cool, but I have to admit, this type of robot just doesn’t excite me. The book also punts on the PWM, using a controller which interprets serial USB commands into the PWM for the servos. For beginners, this is certainly the right choice.

Incidentally, the –help output from UscCmd includes Version, Culture, PublicKeyToken values like a Mono program might. I wonder if it is written in C# and running via Mono. I’m going to assume it is. That is pretty sweet. Indeed the linked download page mentions C#. http://www.pololu.com/docs/0J40/3.b

The sonar sensors section is a straightforward and great introduction to the use of them. I never knew how those things worked or what kind of value they returned. Now I do. Mounting the sensor to a survo makes for a nice subsystem on the bot.

Next, a fully remote control system is built. I don’t know if I like the choice of using an LCD monitor. It seems like overkill, but depending on the particular robotic application it would be a good choice. For the applications I have in mind, I think I’ll skip it. A wireless usb keyboard and mouse makes for an obvious choice. At this point, I just keep thinking about bluetooth and using an extra Wiimote, mostly because I think it would be a more fun control.

Oh, a GPS receiver! This could be necessary for when I lose my robot in a parking lot or the woods. As with the LCD Monitor and KB chapter, I kind of feel like I know how to do this since I’ve looked into it before. It is great coverage and good intro to the topic.

Much of my day job is what would traditionally be called Systems Programming so Chapter 10 is kind of a duh to me. I’d have started there, but that is just how I think about coding these days. Its great to have this in a chapter to tie some things together. In other words, read this chapter!

Using the BBB in sea, air and submarine applications is an interesting idea. I don’t think it is for me yet, but the book gives introduction to some ideas on the topic. The introduction to feedback control is very welcome.

Overall this is a great book. It really gave me a lot of ideas. It also showed me how easy it is to get started, something which I’d been a little hesitant to do. I’m actually a little excited to dive in now. I’ll be doing a bunch of this stuff with my 6yo over the next few years.

When Reversing the Interview Process becomes How Would You Do Fun Things in C

My boss’s boss’s pals wrote this: http://blog.exodusintel.com/2012/09/18/reversing-the-interview-process/

Its a story about how someone was asked a crazy fun C question in an interview and how the new team decided to try it.

After reading this and discussing it with coworkers, I decided to try it and of course the first thing that came to my mind was a way to use tail recursion to do it.

 1 #include <stdio.h>
 2 
 3 int c;
 4 //auto func0 = [&] () -> int { c++; _strlen(s+1);};
 5 //auto funcn = [&] () -> int { return c; };
 6 int rs(char* s);
 7 int go(char* s) { c++; return rs(s+1); }
 8 int ret(char* s) { return c; }
 9 int rs(char* s) {
10     int (*func[2]) (char *s) = {ret,go};
11     char i = (*s>>7 | *s>>6 | *s>>5 | *s>>4 | 
        *s>>3 | *s>>2 | *s>>1) & 1;
12     return func[i](s);
13 }
14 int _strlen(char* s) {
15     c = 0;
16     return rs(s);
17 }
18 
19 int main(int argc, char* argv[]) {
20     printf("_strlen(%s): %d\n", argv[1], 
        _strlen(argv[1]));
21     return 0;
22 }

After writing it, I went and looked at the other fella solutions for the second time. I should also mention that I haven’t written C on the job in 11 years, and when I did then, it was one tiny program which was quickly replaced with perl. I have never been anything other than an intro beginner C programmer.

Things I noticed after going back is that my solution is somewhat similar to Brandon and Zef’s solution, but I think both my use of function pointers and bit shifting are more elementary. I’m still not sure about how some parts of their solution works.

Storyboard Custom Segue For Custom PushViewController Animation

While there are a lot of google hits when searching for custom pushViewController Animation, I found nothing regarding use of a Custom Segue to make it reusable and I also found a lot of misinformation like “it can’t be done with the default UINavigationController.” It can.

From your Button, View, Gesture Recognizer or whatever, instead of dragging from Push, drag from Custom.

Then select the Segue that is created and type in a class name the custom Segue.

Now we can create the FromTopReplaceSegue class. Use Add File or however you like to create new classes in XCode.

//FromTypeReplaceSegue.h

#import <UIKit/UIKit.h>
@interface FromTopReplaceSegue : UIStoryboardSegue
@end

//FromTypeReplaceSegue.m

#import “FromTopReplaceSegue.h”
@implementation FromTopReplaceSegue
-(void)perform{
UIViewController *dst = [self destinationViewController];
UIViewController *src = [self sourceViewController];
[dst viewWillAppear:NO];
[dst viewDidAppear:NO];

[src retain];

[src.view addSubview:dst.view];

CGRect original = dst.view.frame;

dst.view.frame = CGRectMake(dst.view.frame.origin.x, 0-dst.view.frame.size.height, dst.view.frame.size.width, dst.view.frame.size.height);

[UIView beginAnimations:nil context:nil];
dst.view.frame = CGRectMake(original.origin.x, original.origin.y, original.size.height, original.size.width);
[UIView commitAnimations];

[self performSelector:@selector(animationDone:) withObject:dst afterDelay:0.2f];
}
- (void)animationDone:(id)vc{
UIViewController *dst = (UIViewController*)vc;
UINavigationController *nav = [[self sourceViewController] navigationController];
[nav popViewControllerAnimated:NO];
[nav pushViewController:dst animated:NO];
[[self sourceViewController] release];
}
@end

In this CustomSegue not only are we doing custom animation from Top to Bottom (just like the default push navigation of Right to Left) but instead of pushing, we are replacing the top view controller.

In my current project I have a nearly identical FromButtomReplaceSegue that does the replace but animates from Button. I hope for a library of these with varying animation transitions and Push/Replace variants of each. Then anytime you want to use a different animation you can simply use a Custom Segue instead of writing a bunch of code in ViewDidLoad or wherever. Hurray Storyboard!

  1. http://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app
  2. http://stackoverflow.com/questions/5878732/how-to-create-uinavigationcontroller-animation-top-to-bottom
  3. http://dmunsie.wordpress.com/2009/08/07/custom-animations-between-uiviewcontrollers/

Build MVVM Applications in F#… or C#

Last month Chris Marinos had an article in MSDN Magazine titled “Build MVVM Applications in F#”

I liked it a lot. I jotted down some notes as I read it. My learning processed amused me as I went from WTF… to Hrm… to Oh I see… in a period of about 15 minutes or so.

It went something like this:

> My first thought on skimming the first 3 pages…
>
> Ok, no offense… but… Dude! Really?!?
>
> PM> Install-Package NotifyPropertyWeaver
>
> and my "viewmodel" can be class MovieVieModelCSharp { public string Name
> {get;set;} public string Genre { get;set;} public int Rating {get;set;}
> public MovieVieModelCSharp(Movie movie) { Name=movie.Name;
> Genre=movie.Genre; if(OptionModule.IsSome(movie.Rating)
> Rating=movie.Rating.Value; else Rating=0;} }
>
> But then I realize that isn’t the point of that part of the article.
>
> Everything looks cool. I’m wondering if NotifyPropertyWeaver would work
> with the Dummy F# view model in Figure 7.
>
> Then I see in Figure 9 you use the ViewModelBase. That is cool I guess,
> but I think NPW might allow for writing WAY less code.
>
> …
> OK… i spent some time exploring these idea and realized I’m clueless.
> F# has no concept of autoproperites (WART!) and its A LOT of code to
> write a mutable property (warts!). NPW sure isn’t going to work with
> non-autoproperties (it picks up on C#/VB autoproperty naming convention
> of the compiler IIRC).
>
> So all that said… I think your article is great.
>
> It challenged me to investigate some things. It shows me that F# really
> sucks for WPF. It makes me really appreciate C# autoproperties and NPW.
> Damn that is some ugly ass code in that last "all F#" section.

 

Now some interesting parts to this is that NotifyPropertyWeaver apparently DOES support weaving PropertyChanged into non-autoproperties. That is pretty cool, but even with that, I think that this is a case where C# is actually more appropriate than F#.

Where Windows Is Going

I’ve been stewing for about a day.

I did not go to Build Conference.

Once I figured out that WinRT is for Metro style apps and ONLY Metro style apps, and I tried to make a call from a .NET 4.5 app into a very simple “GetUserInfo” WinRT API and it threw an Access Denied Exception, I was upset.

Then I realized something.

This is not just a new way to program on Windows.

It is also not THE new way to program on Windows.

Microsoft is segregating their application types on purpose. Metro style apps and WinRT is not about “the next windows”. What was shown with the hybrid classic desktop and the new Metro UI is NOT the future vision that MSFT has for Windows. It is a transition.

Microsoft is posing Metro style and ONLY metro style to be the next development platform of a version of Windows that will not have the classic desktop. Some future version of Windows that won’t have Win32 and won’t have .NET. It will be WinRT on top of a Windows kernel and THAT IS ALL.

This is why that line between WinRT and .NET/Win32 has to stay thick and hard. There will be no hybrid apps. That ruins the vision.

win8-platform-and-tools Imagine a thick wall between the light blue and green.

Microsoft knows that it has to participate in a segregated market. They won’t be selling $100 Windows Professional licenses to OEMs of $300 tablet devices. The $10 license of Windows Started Edition is more like it. (I’m guessing at OEM costs.)

The operating system has to run fast on low power hardware. Any applications which run on it have to run on all the better and faster hardware, right on up to $5000 laptops. The Metro style along side classic desktop fits this mold.

I’m a little bit sad that they wouldn’t just come right out and say this. Come right out and say: “Hey! Our phones are going to run Windows 8! There will be 7”, 8”, 9”, 10”, 11”, 12” tablets all running Windows! But its not like any Windows that you’ve ever known!”

samsung-galaxy-tab-10-1-side-2-small-2

Talk about shaking things up! What if that were the first 3 sentences of the Build keynote?

I’m less angry about not being able to call WinRT from my .NET apps. But I’m a .NET developer. I’m not a Windows developer. I’m not a Silverlight developer. I’m not a WinRT developer.

I think in what I’m most disappointed is all that awesome stuff that WinRT makes easy, and out in .NET land, talking to a webcam for photos and video is still painful. Transcoding video is still painful. (Did you see how easy it is to transcode with WinRT? That is SWEET!)

Build Windows Day 2 From Afar: WTF is WinRT

Watching the developer reaction to Metro style and WinRT has been both depressing and comical. I suppose its like watching anyone else learn something from nothing. We can get it very wrong at first. It is part of our learning process.

I know I’ve not figured what the existence of WinRT means for .NET or what I can and cannot do between WinRT and.NET4.5. I’m also not sure what the difference is between Metro style and WinRT. It sure seems like there are some cool non-UI APIs in WinRT that I might want to use from .NET. I sure hope that I can.

At first glance it feels like Metro style apps aren’t .NET. At least not really. It feels like Metro style apps run in a different CLR. They feel like Silverlight on the desktop since they have no direct access to file system and the types available for use are limited.

I’ve read that it is not a different CLR. I’ve read that it is .NET but that the verifier and other tools in the chain of building a Metro style app enforce the restrictions. I’ll have to wait and see how the pieces fit together.

Missing the APIs can be a challenge: http://winmd.tumblr.com/post/10211942442/missing-net-apis

Looks like I can do the above when I say use some WinRT from .NET4.5. Cool, because those file pickers sure are pretty. http://ermau.com/using-winrt-from-net/

Doug Seven has an excellent post detailing what is being released and how it fits: http://dougseven.com/2011/09/14/i-know-what-youre-thinking-and-youre-wrong/

Take a look at this slide from the keynote:

win8-platform-and-tools

I’d like to speculate about a couple things related to this picture.

  1. This is not in the scale of overall importance to all developers. It is in the scale of what is new.
  2. All of the things in blue are still there and still supported.

Consider an alternative pitch for Metro style apps. Metro style apps, like all apps, are all launched from the new “Start Screen”. The start screen replaces the start menu. Metro style apps keep the look and feel of the start screen. It is as if you are never leaving the start screen. Metro style apps are apps that you program into the start screen. Imagine being able to program apps into the start menu. It sounds crazy. Now that your start menu is full screen and interactive, those metro tiles (they have a different name) are programmable by you. I can think of another little square in windows that is programmable: Sidebar Widgets. When was the last time you wrote a sidebar widget? (Mike Ward, you are the only one I know.)

Considered from the point of view, very few of us Line of Business app developers will be writing Metro style apps. They are not meant to replace all desktop apps. You will not see Office apps like Word, Excel or Powerpoint built as Metro style apps. They will likely get some theming that is Metro influenced, but they will still run on the traditional Windows desktop and not in the Start Screen. That said, there might be augmentations in the Metro style apps. After all, Windows on a tablet is far more usable with Office. So I think you will see stripped down, touch optimized, Metro versions of Word, Excel and Powerpoint. The use case for these will be “when you aren’t at your desktop or laptop".

Believe it or not, there was actually announcements around Azure and ASP.NET yesterday. You’d never know it given all the focus on WinRT. I’m super excited about the new async support for controller actions in MVC4. I also think Azure’s replication story around tables and blobs is pretty sweet now.

So much more to come. Its an exciting time be a developer. Just try to remember that ships often look like they are sinking when you see them far off the horizon Smile

Thoughts after the Build Windows keynote: This changes nothing

Nothing has changed, and this is a good thing.

Here is what I took away from yesterdays keynote:

  • Everything I’m doing today with Windows desktop apps will work on Windows 8. No surprise there.
  • If I want to write these new WinRT apps I’ll have to leave Winforms or WPF behind and use what looks like some kind of desktop Silverlight UI control library which has been enhanced with some more controls. (The GridView was shown in the keynote.)
  • There is nothing else to say about the developer story. Everything else shown in the keynote was about consumer experience.

Watching my own twitter feed was rather comical. Everyone seemed to be hearing exactly what they wanted to hear. Sometimes it was the opposite of what I thought I just heard.

There are still lots of unanswered questions, but I’m sure that in the next few days these will all be answered via sessions and blog posts containing the content of the sessions. I’m surprised people aren’t more patient. I’m actually seeing a bit of FUD email and tweets asking questions that should be answered by the end of the week. Be patient.

Writing Software is not Comparable to Using a Hammer

I’m going to rant.

Take a look at the first two paragraphs and bullet list here: http://www.peteonsoftware.com/index.php/2011/08/23/programming-language-skills/

Which part of programming is as simple as using a hammer, a manual saw, a table saw, a router, a clamp?

I’d argue that these are fundamentals. If you want to compare, compare them to programmer fundamentals.

  • using the if statement – 20 years experience
  • using the for statement – 20 years experience
  • using recursion – 20 years experience
  • writing functions – 20 years experience
  • using variable assignment – 20 years experience
  • using math operators – 20 years experience
  • doing string manipulation – 20 years experience

Just as the Programming Language “Skiills” post says, you never really need to put this on your resume.

You can have many years of carpentry experience and just as in programming, maybe you have repeated the same one year many times, or maybe you have done something new every year.

A quick search for Cabinet Maker at monster.com shows a job posting: http://jobview.monster.com/Cabinet-Maker-Job-Rome-GA-101712507.aspx

Notice how the requirements are nothing like “we want 20 years experience using a hammer’” ?

Its because metaphor does not work.

Can we as programmers please stop using this metaphor?

Are Access Modifiers Antiquated?

I just wrote a rather lengthy reply to a Programmers StackExchange question which I’d summarize as “why are the types and or methods I want to extend often sealed in common Java and .NET frameworks and libraries?” Its actually titled “Why shouldn’t classes be designed to be ‘open’?’”

My reply is here:

Because they don’t know any better.

The original authors are probably clinging to a misunderstanding of SOLID principles which originated in a confused and complicated C++ world.

I hope you will notice that the ruby, python, and perl worlds don’t have the problems that the answers here claim to be the reason for sealing. Note that its orthogonal to dynamic typing. Access modifiers are easy to work in most (all?) languages. C++ fields can be mucked with by casting to some other type (C++ is more weak). Java and C# can use reflection. Access modifiers make things just difficult enough to prevent you from doing it unless you REALLY want to.

Sealing classes and marking any members private explicitly violates the principle that simple things should be simple and hard things should be possible. Suddenly things that should be simple, aren’t.

I’d encourage you to try to understand the viewpoint of the original authors. Much of it is from an academic idea of encapsulation that has never demonstrated absolute success in the real world. I’ve never seen a framework or library where some developer somewhere didn’t wish it worked slightly differently and didn’t have good reason to change it. There are two possibilities that may have plagued the original software developers which sealed and made members private.

1. Arrogance – they really did believe they were open for extension and closed for modification
2. Complacence – they knew there might be other use cases but decided not to write for those use cases

I think in the corporate framework world, #2 is probably the case. Those C++, Java and .NET frameworks have to be "done" and they have to follow certain guidelines. Those guidelines usually mean sealed types unless the type was explicitly designed as part of a type hierarchy and private members for many things which might be useful for others use.. but since they don’t directly relate to that class, they aren’t exposed. Extracting a new type would be too expensive to support, document, etc…

The entire idea behind access modifiers is that programmers should be protected from themselves. "C programming is bad because it lets you shoot yourself in the foot." It is not a philosophy that I agree with as a programmer.

I much prefer python’s name mangling approach. You can easily (much easier than reflection) replace privates if you need. A great write up on it is available here: http://bytebaker.com/2009/03/31/python-properties-vs-java-access-modifiers/

Ruby’s private modifier is actually more like protected in C# and doesn’t have a private as C# modifier. Protected is a little different. There is great explanation here: http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/

Remember, your static language doesn’t have to conform to the antiquated styles of the code written in that language past.

Nuget could be more usable outside of visual studio

This is a reply to http://blog.davidebbo.com/2011/05/thoughts-on-installing-and-updating.html

 

I think you are wrong.

Everything in this post points out that you were wrong to base nuget on DTE.

The points here do explain "why this isn’t supported today" but it sounds like they are defending the lack of features as a good thing. It is not a good thing.

I sympathize with the lack of a strong foundation upon which to build. (limits of DTE) However, the idea of updating the csproj outside of visual studio point 1-3 and 7 in your post, is a sound idea.

The fact that #7 allows arbitrary code execution is something you will never overcome. As a packager might also automate something which is prevented by a GPO in a corporate environment, or utilize a COM object that doesn’t exist. You cannot solve all problems.