Sunday, 13 August 2017

On GUI's in Python and D…

I recently wrote a forum post regarding my experience with finding a suitable GUI in which to build a small toolkit.  My primary need, was for something I could hit the ground running on (because I already had a partially built application, but was wasting too much time fighting the toolkit), and which I could dump on a friend to just click and run.  Not the best of selection criteria, nor did I do a particularly exhaustive search for options (some very good ones of which were pointed out by other posters on that forum), but these things happen out there in the real world — sometimes your selection is legacy, and/or even just plain comes down to "the devil you know", though I don't think that was the majority case in this instance.  Anyhow, I'm going to re-post that post here, more or less…

Anyhow, here goes:

I've just recently gotten back into building applications with GUI's myself, after a 20 year lull.  Back then, it was Linux with GTK2; GTK3 and Glade were pretty new and a little too flaky to be worth the effort.  Now, just a couple weeks ago, I found myself wanting to throw together a small Modding GUI toolkit for a friend who's, well, not the most technically minded.

So I started with what they already have installed, Python and its Tkinter…  Well, that was a horrendous experience.  The Python→TCL→TK hop-skip-and-jump routine is, frustrating, and at the end of it, TK's not exactly the most capable of GUI's.  Don't get me wrong, I love TCL, it's a fantastic language, which I was very deeply into back in Uni, getting right into the guts of the language.  But TK…  While it kind of works in TCL, it's frustrating as heck in Python.  Take for example, checking to see if a tree item is hidden, and it returns a '0'.  Not a boolean, or even a number, it returns it as a string.  If you forget to explicitly cast it to an int, you get True (in TCL, it's a number with an optional string representation, and it'll do an implicit cast if necessary in any numeric context, but when translating it to Python, you just get the string rep, which is True if not empty).  As for the other Python GUI options, both myself and my friend are presently in Windows and, well, getting many of them to actually install cleanly was… problematic.  And if I had trouble getting those toolkits to install…

So I started looking for better options.  And I came across one I think well worth mentioning.  GtkDD really is a better C/C++ (I remember being rather disappointed when a YouTube'r I follow dismissed D offhand as merely — not a quote, the real quote would be shorter — "D is just a C++ as C plus 1 makes D, pointless naming rubbish", seemingly without actually looking at the language itself); it's an absolutely beautiful under-rated language, IMHO, which pretty much covers the useful parts of C#, without feeling like the proverbial "everything and the kitchen sink" that's all too common with Microsoft frankencreations (don't get me wrong, I use their OS, and some things they do are absolutely brilliant.  It's just that, some, well, go a little overboard, and/or just plain miss the mark entirely).  And paired with GtkD (which has a few minor gaps still, but nothing serious) bringing with it GTK3/Glade, has been a comparative delight to work in.  I did miss GTK, but I didn't miss C (though it was certainly educational in the ways of OOP back in the day), and this combination is just delightful.

Now, I think I'd rather be using Dgui — I'm pretty sure it would be far more effective — rather than GTK through a layer of D wrappers (where all that CTFE goodness has very little to chew on), though it's also a much younger GUI (which might or might not matter), and when I was poking around for options which just work so I could get on with re-implementing that toolkit, it was throwing page fulls of errors with the basic Hello World example, and very little idea what the problem was.  Plus I don't know if it had a GUI builder, though I think there may have been something to do with Visual Studio — which I'm not using mostly on account of it's rather large install size (gvim is my present IDE at the moment).  But, I needed something I could get up and running with now, and more importantly, that just worked for my friend, too.  I'll have a look at Dgui again later, because I'm sticking with D for my personal projects now, but GtkD was the easiest option to get going in again; partly due to my prior familiarity to GTK, and I just had to give my friend the GTK3 runtime installer, along with my compiled binary (a shared Dropbox folder would work wonders, down the line, until I have either reason or inspiration to sort out something better), and they were good to go.

I Made A Blinky! — Version 2

So, finally pulled together the pocket cash, and sent off my orders for the necessary bits and bobs.
The second revision of the board (which I introduced here) cost about $8.50 (US$6.50 + a crappy AU$ exchange rate), with some minor layout improvements, space for a push button (because taking the battery out all the time was getting to be a pain), I removed the positive copper fill on the battery side because I considered that a bad idea when the negative terminal of the battery is scraping over the solder mask on every insertion and removal, and a little piece of test art (the profile graphic I use here on this blog).

Removing the positive fill on the battery side I think was good, I had considered moving the negative fill to the back instead, since the negative terminal of the battery gets pressed against the board, but during insertion of the battery it's likely the positive terminal that does the scraping (since it wraps over and around the battery), so although it should be clear once inserted, there could potentially be an issue in the meantime.  Just not having a copper fill on that side at all is, I think, the better choice.  One big slip-up however, was my misreading of the shop description on the on/off switch I chose; I thought "on/off switch" meant press to turn on, press to turn off.  No.  It means (it appears) that the internals of the switch have a clicky mechanism that makes a nice solid on state when pressed, and a nice solid off when released, rather than the wishy-washy you get with carbonised rubber on PCB style buttons.  Lesson learnt.  It's also the minimum of what I need for this project, since that "switch" is simply sitting between the battery and the rest of the circuit — press it, the circuit has power, release it, it doesn't — so being a toggle (what I'd wanted), or a momentary action (what I got), at least the on/off part is right, plus the added bonus that the extra buttons will come in handy for other projects anyhow.  Again, if this were a real project where it really mattered, acquiring some samples would have been on the cards, or finding someone who already knew, etc. — but this is pure learning experience ground, it was the first component footprint I've drawn up by hand, and it fit first go, so I still consider it a win.

Components for the board, namely the push buttons and SMT LED's (which I totally forgot to put in the order first time around, so the first board has a big ol' through-hole LED attached sideways), came to about $10 — mostly the push buttons, which were minimum 10 quantity, so I have plenty of spares!

But talking about those LED's, they're the most hideous things to solder!  (Even worse than the capacitor.)  One fell apart on me, the second one just would not sit straight while I soldered it, and then there's the whole issue of installing it in the right direction — just trying to figure out which end was the cathode was a nightmare in itself; a quick search on the web yielded several marking options, with the caveat that some manufacturers do it the other way around, so mostly useless anyhow.  But even then, none of the markings I saw described were on the ones I'd bought, instead they just had a bevelled top edge on one end (and the notch that was suggested as a common marking, well, they had a notch kind of thing at both ends!).  I'd tested the board by tacking down a regular LED again, so I knew the board worked, and figured if I could just hold the SMT LED to the pads, I should be able to determine it's orientation.  But, that didn't work.  Not entirely sure why, whether it just wasn't making contact, or whether my metallic tweezers I have were bridging the terminals, or what, but I couldn't get an indication that way.  In the end, I just took a punt, and soldered it down.  And after fighting to get that thing soldered down reasonably, it didn't work.  Neither, did holding a regular LED over the ends, which seemed rather disturbing (had I accidentally damaged something else on the board?).  So all I could do was take it off, and try again with a new LED, hoping that bevelled edge was, at least, the cathode.  And it was, and it worked!  I now had a working Blinky v2!!!  New and (ever so slightly) improved (assuming you don't mind holding down the button)!

That wasn't the end of my purchases, though.  Along with those $10 parts, was another $60 in extras, most notably a much needed flux pen, and one of those nifty "helping hands" gizmo's with the little gripper claws and an attached magnifying glass.  Awful lot better than taping the board to the tabletop with double-sided tape, as I did the first time around — although next time I might still tape down the helping hands gizmo itself.  There's also a lot of other very basic gear I still have to get, like cutters, non-metallic tweezers, a decent soldering iron… but I'll pluck that stuff off my wishlist with each successive purchasing round as I go along.  The important part, is that progress was made!

Saturday, 27 May 2017

I Made A Blinky!

Okay, so, getting back to Electronics.  I decided, last year, to try and dig my way back from the nothingness that's plagued me for over a decade.

A couple months ago I came across the free tutorial Getting To Blinky from Contextual Electronics.  Now, I can't afford the fee's he's wanting for the classes, but he has been nice enough to release a few freebies here and there, and I've been wanting to learn KiCad for a while now, so this was perfect.

Following along with the tutorial, I drew up the circuit (making my own version of the 7555 schematic design along the way), flipped that over into the PCB designer (and making another footprint), had the boards made up by OSH Park (who doesn't love purple PCB's!!!), and watched the tutorial episode where Chris realised he goofed up and had to get medieval on the 7555.  Thanks Chris.  Anyhow…  Ordered the components while I waited for the boards (and forgot the LED's), and then left it all sitting here in my desk for several months.  Yeah, life, it happens, what can you do.

Then this morning I woke up absolutely determined to put the thing together.  Pulled out my crappy cheap soldering iron, perched myself in front of the corner of our TV entertainment unit by the window, with the bits and pieces laid out in front of me; components, tweezers, scissors, solder, de-soldering braid, and my phone, with it's 4x camera zoom in place of a magnifying glass.  No flux, no clamp for the board (used double-sided tape to hold it down instead), but it was enough.  The solder joints look horrible, and I'm sure there's at least one or two on the IC that are only barely holding on, but after I was done, and I'd cleaned up the bits of packaging and put most of the stuff away, I pulled out a spare coin cell battery and threw it into the holder, and it blinked!  To my utter astonishment, the darned thing actually worked!  There it was merrily blinking away at me…  I Made A Blinky!

So now I have a fully functioning Blinky.  After over a decade of feeling like I need to get back into it but never quite getting there, I can't say that enough — as simple and small a start as it is, with so far yet to go to relearn what I've not used in so long, I'm happy.  I've learnt to use KiCad (including making custom schematic components, and footprints), order the board through OSH Park, find and purchase the parts required, and managed to assemble it successfully with barely adequate gear (and SMT to boot).  Most importantly, I've proven to myself that I can — and that little spark of confidence regained, is what Blinky was all about.

Where to now…  Well, first, I'd fixed up the little mistake from the tutorial in the design files, right after having ordered the boards.  So I think I'll order up a new set rather than persist with the nasty clipped IC leg hack I had to do with this one.  That, and purchase some flux, decent tweezers, a 10x magnifying loupe to check the joints and read part numbers, a clamp to hold the board rather than the horrid double-sided tape, and a hands-free magnifying lens so I can actually see what I'm doing.

Beyond that, I think, it's back to my desk clock with better display driving, an RTC module, and some updates to the firmware I've been planning.  Get that going on an Arduino prototyping shield, then off to OSH Park again (what can I say, I like purple!) for a single-board version.

Friday, 19 May 2017

A touch of Religeon.

To begin, let me start by making my view on religion reasonably clear: I think religion is mostly nonsense, the church exponentially so…  I however think the core ideals of the religion known as Christianity are well worth upholding, and modern Christianity has the ideal of being a predominantly private choice (which in my mind makes it a prime contender for the mantle of "the one true religion").  And I do believe that the intelligent design philosophy is at least plausible.  With that in mind, on with the story…

A little debate recently happened in one little corner of my family, a new round of a recurring debate on the evolution vs creation issue.  I've heard it a few times, the argument that the chances of a species like us evolving being so very unlikely, that it must have been intentional.  But is it?  Really?

Is it really just 1 chance in whatever sized space?  In an infinite universe, even if just one in every nearly infinite number of possibilities is us, there's still an infinite number of possibilities we will exist, within an infinite space of possibilities — infinities are weird that way, and I often hear it said, and agree, humans just really are rather crappy at correctly judging extreme quantities; really small numbers like 1 in infinity, or really big ones, like that infinity itself.

And why should we be the only possibility.  While it's true we're the only us, so far as we know, would a different version of us also not think, "we're the only us"?  I think it is, perhaps, a little egotistical to think we're THAT special.  Unique, sure, but what makes us so special?  Did we achieve universal peace quicker?  Have we figured out our purpose for existing yet?  We're a million years old, on a planet several billions of years old.  What have we done to make ourselves as special as we think we are, among all the species that may or may not exist out there.

Then, even if we are indeed alone in all the knowable universe (and I kind of hope we're not, personally), there's still a fair bit more of it out there.  There is the suggestion the solution to the Fermi Paradox, is that the universe itself has an age-wise Goldilocks Zone for the creation of life, so we may just be one of the earlier species to appear in this region of the universe, and there may well be the radio bubble of another species screaming it's way towards us right now, just as ours does towards them.  And speaking of which, there may well be more us's that we'll never in the lifetime of the universe have a chance to meet, and we'll never know because the expansion of the universe is taking even their radio signals away from us too fast to reach us in time.  (And even assuming future FTL travel, that simply moves the bar, it still doesn't actually remove it.)  Assuming the universe is as big as scientists seem to believe, there's still a distance beyond which we will never be able to observe, and a far more populated region of our universe could begin just the other side of that line, and neither us nor them will ever know.

Now, does any of that make creationism impossible?  Well, before the universe existed, what was there?  If some entity was responsible for our existence, perhaps they simply chose a beginning that they knew would lead to the end they wanted.  Perhaps without even knowing how, and they're looking down thinking, "wow, look at these weird little creatures I made".  Or perhaps they did choose the one that will have us in it, with all that we are, and all that we will be.  But no matter how much you play statistician, that question, along with whether in fact an external entity was responsible for any of this at all, is for the time being still the provenance of religion, and we have only our faith (or lack thereof) for answering the question of whether religion is even real at all, or just a creation of our own making intended to make us feel a little better, and/or behave a little better too.

All I know for sure, is that if God — in any of the forms imagined by mankind — is real, and He's aware that I exist, I hope He isn't too disappointed in me.

Monday, 27 March 2017

The problem with personal projects…

I, I have to confess, tend to have a lot of unfinished or broken projects laying about.

I've done the odd bit of work for a friend, even a couple small projects by contract.  It's a different issue when you're working for someone else — be it a free, or paid — where the goals are in support of someone else's endeavour, and the reward is the respect of a job well done (with appropriate emphasis on the word done), and a pay-day to back it up.

But when it comes to my own projects … the goal is typically more so to learn something.  So I tend to find myself wondering along with only the most vague of plans, and reinventing a whole lot of wheels, partly simply to prove to myself that I can.  Though, reinventing some of those wheels are  not only about knowing how to do something, but how it actually works — it's one thing to stick a CSRF token in your web forms because your company policy tells you to, it's something else to understand what goes into that token and it's application, because you've implemented it yourself, and done the research into how and when it should (and should not) be used.  The risk, of course, is that of having done it incorrectly — but in your own projects, aimed at personal consumption, that's usually a risk well worth taking.  And besides, it's not like that same risk isn't there when using someone else's product to do the same job — the industry is littered with issues caused by flaws in well known and regarded standard implementations.  Everything from long-standing bugs, brand new latest release bugs, through to intentional backdoors, and even the odd spot of governmental interference.  Those implementations do, however, have the benefit of real life usage time, and hopefully the attention of a few experts … unlike your own pet wheel which has, well, you.

So now, while I consider it advantageous to reinvent all the wheels I can for the understanding it gives me — and I do exactly that in my own website, eschewing frameworks for nuts and bolts — there is a downside, too.  I spend so much time working at the lower levels building up the foundations, or learning to work with some new tool or functionality, I often don't get to finish the project — usually, simply because it stops being fun, I've learnt what I wanted, something else caught my attention, and/or requires the injection of funds which are needed elsewhere — so it languishes on the hard drive forgotten and unloved for months (or years) at a time.

Of more recent note, I finally decided to put the time in to add a feature to one of my more functional coding projects — one which took me ages to get around to finishing just recently — tearing into it without bothering with the niceties of version control (or even making a backup of the script first – although I could hunt down a past version if I got desperate, thanks to the working folder being on Dropbox), and then didn't finish the update.  So it's presently in a non-functional state, again.  Well, was fun while it lasted…  I used to use revision control regularly through University, and when my main PC was running Linux, keeping a log of changes, and all the rest.  So I'm certainly no stranger to it all.  I suppose it just doesn't seem quite so handy on the little bits and pieces I do these days, and doesn't seem to fit my Windows flow nearly as well.

In any case, my more recent projects:
  • I have a desk clock that got broken over a year ago now, and I've not yet fixed (waiting mostly on the designing of revision 2) — this was working quite well for a time.
  • A game I started building to learn OpenGL, but got distracted by building a — still unfinished — UI library (they're surprisingly complex little beasts).
  • … and several more …
Though, not all my projects go unfinished, and I have to admit, it is rather nice to have a "done" (at least to the point of functional) project.  The empty space on my desk, in particular, keeps pulling me back to my desk clock project.  I should do a proper post on that sometime … sounds like another project to add it to my TODO list.

Arduino, my DeskClock, and a sprinkling of IoT to flavour…

Okay, so I said in my introductory post that I'm a fan of Arduino, and hope to get back into that side of my hobbies…  And though this isn't the post I was planning to do as number two, I think it should be.

I went to University to learn Home Automation.  Basically.  Though I didn't realise it until I was already there.  I was never into electronics enough to do the whole engineering route, with all that messy analogue stuff — I much preferred the nice clean and crisp digital life.  Of course, that's not the reality of electronics, but hopefully you know what I mean.  On the flip-side, while programming is definitely a hobby of mine, and I'm reasonably good at it (at least as good as a couple friends of mine who are actually in the industry), I don't enjoy the process as much as I wish I did.  My first Arduino Blinky Light project, however, gave me that warm fuzzy glow of success I've been craving all these years!  (Well, okay, maybe not quite that glowing, but it was a definite step in the right direction…)  And I've gotten ahead of myself again…

During Year 12, my science teacher put a small stack of flyers for various University courses on a table while he talked to us about where we were planning on going next, at the end, he told us to all come and grab a couple of the flyers and have a look through them, see if there's anything we liked.  He also picked up one flyer in particular, and handed it to me, specifically.  Now this guy I used to call Mr Smiley, because he was always smiling.  But this was one of the times when he seemed positively pleased with himself, and as I looked over the flyer, it just clicked.  Embedded Systems.  Controlling stuff with little computers.  It was perfect.  And that's what I applied to study.

As you can probably guess, I floundered a little bit on the analogue electronics, I was okay at it, and I passed, but I wasn't fantastic.  Digital electronics, though, building up a basic processor from its fundamental components, writing its microcode, through to making it run a simple machine code program using that same microcode, now that was an experience to behold!  (Better than sex, even!  Sex is messy and awkward, especially on your first time, this was beautiful and clean!  …ish…)

Anyhow, my plan was set…  I wanted to finish Uni (I had hoped to do it at Uni, preferably as part of my honours year, and/or post-grad, but things didn't work out that way, and I quickly decided I was better off pursuing it later on…), get a job, buy myself a house (…like now), and deck the crap out of it with Home Automation.  And not that media centre rubbish, but the real stuff — lights, doors, windows, blinds, aircon, the tea jug, fridge, ovan, washing machine, shower, toilet, you know… all the important things in life.  I wanted it all under my control!  (Insert evil laughter here.)

Well…  As I think I said something about (or at least intended to) in my introductory post, that didn't quite go as planned either.  In fact, it's a decade and a half later, and I've done a grand total of two Arduino projects: one blinky lights tutorial through to turning a simple servo, and a desk clock.  I will say, it's a pretty snazzy little clock (and totally my own creation, unlike the blinky light); a single 4-digit 7-segment display (with dimming of selected characters — mostly non-edit characters during configuation, and the likes), an RGB LED (which was used purely as a quick temperature indicator), and a single lonesome push button to drive it all.  And "it", in that context, was time (HH:MM, or :SS), temperature (in °C, °F, or °K - it had °R available too, if you were desperate enough to turn it on in the configuration, though there's only so much you can do with 7 segments, so I left it off by default), alarm (HH:MM), and a configuration menu (set the time, zero the seconds, set the alarm, set display brightness, select the order you want your temperature measurements to show in, that sort of thing, complete with little 7-segent animated icon things).  Oh, and no buzzer — that was intended for version 2, along with improved display driving.

Now…  In the meantime, I've been watching — and wincing — at the state of security and privacy in the IoT field, particularly with respect to Home Automation.  All these gadgets, often sporting fairly potent processors, and bugger all security.  Routers, thermostats, TV's, and all manner of other things being hacked and taken over, sometimes with the hack lying dormant for months, just gathering information by monitoring your network traffic, thermostats just waiting until you seem to have disappeared for a long weekend, or your new smart TV listening in on your conversations, for example.  And as much as I love the Arduino platform, totally want to support Arduino and the fantastic job they're doing, especially love the MKR1000, I have to admit, I'm leaning a little more towards birds of a different feather…  Sorry Massimo, you've done some awesome stuff, and you were the flag that renewed my interest anew, but you've kind of been left behind a little for me…  (Not your fault, you're doing a whole lot of really good stuff that I whole heartedly encourage people to pay attention to, just not so much that swings my particular way, unfortunately.)

And so I finally get back to what I wanted to say…  Right now, we seem to be on the cusp of something (well, one more something at any rate).  The world has only just woken up to the fact that we need security in our IoT, we can't just assume that no one's going to hack our refrigerator or TV, because these things have cameras and/or microphones on them now, amongst other things, even our internet controlled light globes are spreading malware, and they're within the primary network defence perimeter that is, for most people, their internet modem/router.  And we're starting to realise that even the innocuous thermostat stuck to the wall, might just be harbouring a secret foothold that's watching our behaviour, looking around to see what else it can sneak into, or trying to find an opportunity to slip some ransomware onto our computers, and we'd never know this was even happening because it's just a little device with a tiny screen (or maybe no screen at all!) and a couple buttons, that we barely even think about, and which just happens to be exposed on the internet, fetching weather forecasts or vaguely secured firmware updates with default passwords on its internet-facing configuration ports.  Oh my…

Now, I'm not an expert in all that, there are people much better at talking about that than me, people whom I listen to myself to keep up to date on these things.  So I'm going to get back to me… and as I sit here with my busted Arduino desk clock (the dogs got caught in the power cable and yanked my PC along with the clock sitting atop it all onto the floor one day, and the clock landed up on the bottom of the pile somehow), I can't help feeling a little disturbed trying to figure out how to best go about making sure version 2 has a bit of security as well — because you just know I'm going to want it to have a web interface of some kind.  You see, the industry has only recently realised they need to make a move on the issue, and there seems to be a flurry of new products coming out.  But as quickly as new products appear, new chips come along as well, and you want them because they have better hardware support, which means you won't be wasting battery power doing so much crypto gruntwork in software, and so forth.  But it takes time for the Arduino's of the world to pick up and start using them in new products, and at the same time they're getting too advanced to hack together on a breadboard or a scrap of veroboard you have laying around, and unfortunately, I don't have a budget for buying any of these gadgets… at all.  That is, after all, the primary thing that's kept me sliding time and again from electronics back to merely programming.  Arduino allowed me to start making moves back into electronics, though I have to admit I often find myself feeling like a noob all over again.  Luckily, this old dog does so enjoy the challenge of learning new tricks…

And then, when you do look at adding secure internet connectivity to your IoT device, to make that little light on the wall flash when you get a tweet, there's this huge disconnect.  The internet is big, with protocols designed for computers, packet headers, even larger HTTP headers, and compression.  Security is hard, with a ton of complex math, manipulating kilobyte keys and those data buffers again.  That's lots of processing power, and storage buffers holding your keys and data…  To securely turn a light on and off — a task that can be otherwise accomplished by a dinky little 8-bit µC that draws a few µW's of power at most, and less storage than a single good sized cryptography key.  Which leads to HUB's.  Now, if our IoT devices talk to HUBs, and it's the HUB that does the heavy lifting onto the internet.  This works.  Except the IoT device is probably using Wifi, that computer protocol that really should have security, which is now being provided only by your router again.  And those HUBs — when they exist at all, because people don't want to buy yet another extra piece of hardware — are different for every company, so you need several of them, and each one has to be designed with proper security, because the malware doesn't care about branding!  There's a whole world of further discussion just there…

But whether my next steps into the IoT is with the MKR1000, or a feather, one thing is certain…  I for one won't be connecting an embedded development board direct to the internet, that doesn't have crypto.  Just like it's no longer safe to put a PC online without the protection of both a firewall and a NAT router, it's not safe for an IoT device to go out and play among the intertubes without some serious crypto security, as those many many devices with token efforts at security (all too often, little more than the absolute minimum required to be able to use the relevant buzzwords on the packaging) are starting to discover — mostly at their customers expense.