A Simple Kitchen Timer with CircuitPython

A simple program demonstration of object-oriented CircuitPython

Arduino has been a lot of fun, but Python still remains my most favorite programming language. So, it would be inevitable that I venture over to CircuitPython, right?!

Recently, our digital kitchen timer went kaput, so that gave me an excuse to buy a new microcontroller (or two, or three. Hey, they’re so cheap, why not pick up a few!), and become acquainted with new programming environment. This time, I went with Adafruit’s Gemma M0. Round and as small as a quarter, I liked it not only for being so cute, but also for convenient, quick projects. Gemma input/output pins are in the form of sew pads that also fit a 3mm screw, so it’s easy to alligator clip or screw in wires. Breadboards are fun, yes, but this is quite convenient, and the finished assembly is attractive as is, without a need to create a printed circuit board.

So, here’s my simple kitchen timer, with a piezo buzzer wired to the back via screws:

Continue reading A Simple Kitchen Timer with CircuitPython

Interrupt Programming with Arduino

Setting up interrupt functions on Arduino is quite simple. Search the internet, you’ll find several nice tutorials on the subject. I’m just sharing my own example for style, and to clarify, why do interrupts at all?

Interrupts are immediate, and they allow clean separation of concerns between main (loop) code and special-purpose functions. Like object-oriented programming, they clarify code intent.

The main loop() function typically has delays built in, so while you could test for a button push in the loop, it may take a while before the loop gets to that point in the code, so you’ll see a delay between the push the resulting effect. An interrupt does what it says – it interrupts the main loop and executes all the code in the interrupt function first, before returning back to the main loop.

Continue reading Interrupt Programming with Arduino

Object-oriented Programming with the Arduino Microcontroller

A Traffic Light Simulation with pedestrian crosswalks

These days, when I’m not managing teams and projects, I am primary a Python developer. But, recently I took it upon myself to learn Arduino, and it’s been fun going back to old hobbies – building circuits and C!

The Arduino programming language include some C++ elements as well, especially classes, so this has allowed me to introduce object-oriented design concepts as well. Here’s a simple circuit with two interconnected traffic lights, programmed in such a way that neither can be green at the same time. The connected pedestrian crosswalk switches initiate a countdown, then switching one light to red before allowing the other light to turn green. I’ve added an additional safety consideration for broken red lights.

Continue reading Object-oriented Programming with the Arduino Microcontroller

Cross-Platform CSS for Translucent DIVs

Had a need recently (in 2011!) to make some translucent (semi-opaque) elements on a Web page. CSS3 supports an “alpha” channel, the “a” in rgba, to provide color opaqueness, but IE 7 and 8 do not support this attribute.  So, most web developers resort to using a translucent background-image.  However, there is a pure CSS way that is compatible with all modern browsers today (IE6 ignored, of course).  This takes advantage, though, of some CSS parsing bugs, so may not satisfy the CSS purists out there, but it works for me 🙂

Demo: solid background inner-box surrounded by a translucent box, overlaying a background-image.

Continue reading Cross-Platform CSS for Translucent DIVs

Django

Egads, it’s been a long time since I’ve written a blog post. Yes, let’s blame Twitter for this!! Pithy, short, and easy, tweeting has become the easy way to share thoughts. Of course, not all can be written in just 140 characters, so I need to maintain a blog for deeper, yes, deeper thoughts.

But for now, let me just say one word: Django! Yes, I’ve been doing a lot of Django development over the past year. Enjoying it too. Python is concise, is nice. Not as nice as Smalltalk, of course, but nice enough. And, as a framework, Django delivers a lot of excellent functionality right out of the box. Been very pleased. I’ll have to write up some of my Django apps … when I have time, of cours.e

nGram Dictionary

On a recent project, had to deal with searching of tens of thousands of product descriptions, with a need to find substring matches quickly.  The select: statement in Smalltalk works like a SQL table scan – okay for small collections, but becomes seconds+ response time with larger lists.

An effective solution to this is an nGram Dictionary.  Strings of words can be broken up into sets of tri-grams, quad-grams, quint-grams, and so on.

My approach to this is a Dictionary indexed by nGram length, each element containing dictionaries of nGram strings of collections of the string objects to be searched.  Thus, indexing results as such:

3 -> ana -> ('banana')
ban -> ('banana', 'band')
4 -> bana -> ('banana')
band -> ('band')
anan -> ('banana')
5 -> banan -> ('banana')
...

Continue reading nGram Dictionary

Decorators as Guards

I’m exploring a new pattern – I’m sure it’s been done before, but it’s new to me, and a useful exercise to get to the next stage with an application I’m envisioning.  The pattern is using Seaside Decorators as security guards.

So, last night, finally squeezed in enough time to my decorator guards into action.  Happy to report they’re working fine.  You can review the demo app yourself at agoric.seasidehosting.st/seaside/ibis .  Login as bob@test.com, password bob, or alice@test.com, password alice. Continue reading Decorators as Guards

Seaside – how to change a page’s title

I know I’ve seen the answer to this before, but had a hard time tracking it down, so thought it worthwhile to post.

To change the web page’s HTML title (or any other head information) for a web component, create a method updateRoot: .  This method will be called when the component is rendered on the page – remember to always super the call too.

component>>updateRoot: anHtmlRoot
    super updateRoot: anHtmlRoot.
    anHtmlRoot title: 'fooTitle'.
    "do anything else you'd like to Root here too"

Updater examples with Scriptalicious & Seaside

The first of a series of coding vignettes in Seaside.

 Context: a web app with a form containing multiple input fields.  Instead of waiting to submit the form, I want the page to update another element every time an input is changed.  In this example, a total field.  Solution was used for a simple MoneyCounter application:

MoneyCounter>>renderContentOn: html html form id: 'f'; with: [

html table: [
    html tableRow:
     [html tableData: [html text: 'pennies'];
           tableData:
         [html textInput
           id: 'pennies';
 	   "on: #pennies of: self;"
           callback: [:value | self pennies: value];
 	   onChange: (html updater
                       id: 'total';
		       triggerFormElement: 'pennies';
 		       callback: [ :r  |  self renderTotalOn: r])
       ] ]

    etc.

    html tableRow:
      [html tableData: [html text: 'TOTAL'; space];
            tableData: [html span id: 'total' ; with: self total]]]]


MoneyCounter>>renderTotalOn: html

 html render: self total
MoneyCounter>>pennies: value
   pennies := value

MoneyCounter>>total
	^((pennies asInteger * 0.01) +  etc.   )

Continue reading Updater examples with Scriptalicious & Seaside