[Updated 05/06/2014: I made a few improvements to the program although it’s still very flakey. It now decodes and prints the letter/number you entered on the screen (but not until you start the next character) & although it doesn’t exit as cleanly as it should if you enter 8 dots, pause then enter a dot the program will exit. you can find the updated program at https://gist.github.com/ukscone/b0b28d6d0cece3961f56 ]
As those of you who follow me on twitter will already know i’m a bit of Pimoroni fanboy. Partly because I like their stuff and partly because I think Paul, Jon & the rest of the crew are good guys (& gals) and that is in spite of the fact that Paul has a gruff exterior that hides an equally gruff interior 😀 Cyntech, Ryanteck, 4tronix et al are also good guys & gals and make good quality STUFF! but I have a softspot for the Pimoroni crew, probably as i’ve met a few of them. I have 2 PiGlows that do nothing but wink & blink at me all day as they sit on my desk calculating Pi to a gazillion digits in COBOL & I love them dearly but when the Pibrella was released they got moved behind the Raspberry Pi tea (ok i admit it i’m turning American & it’s used as a coffee) mug to make space for a Pibrella as not only did it look cool it was made by Pimoroni (& Cyntech) and I have a project in mind for it. However, The parts I need are still on their scenic cruise and somewhere in the Pacific.
The Pibrella has to be the most cost efficient add-on boards for the Raspberry Pi there is. Not only do you get some blinky leds, a buzzy buzzer & some “protect your raspi” i/o you get a buttony button. So what do you do when you want to play with a Pibrella & the parts you are waiting for have not yet arrived? You do the obvious thing and make something blinky, noisey & pressy and make a Morse Key
I’ll readily admit I can’t program in Python. COBOL yes, BASIC yes, Z80 assembler yes, A smattering of C, LISP, OpenEuphoria, Ruby yes and a few other depreated languages & 4GL yes. Anything over a couple of decades old and/or less than a few hundred speakers i’ll give a go but Python just never appealed but I really didn’t want to mess around with writing my own Pibrella control software & as Phil Howard aka @Gadgetoid has written to my non-pythonist mind a very easy to understand Python library I thought i’d give it a go & see what I could knock up.
After a cumulative couple of hours today, separated with requests for tea, coffee, toast & being sent on errands I ended up with a Python program that mostly works The timing is a bit iffy and sometimes it doesn’t recognise button releases and it must be very poor python code but it mostly works so it might form the basis for someone to improve as a learning experience in how not to Python The code is pretty much a slight expansion of Phil’s readme examples but it does show how easy his code and the Pibrella are to use in a useful way.
The code follows:-
import pibrella, sys, time def button_changed(pin): global start global char global word_sep if pin.read() == 1: start = time.time() if (start - word_sep) > 2.1: char+="/" elif (start - word_sep) > 0.9: char+=" " pibrella.light.on() pibrella.buzzer.buzz(440) else: pibrella.light.off() pibrella.buzzer.stop() word_sep=time.time() held = time.time()-start if held < 0.3: char+="." else: char+="-" pibrella.button.changed(button_changed) start=0 word_sep=0 char="" while True: print '\r', char, sys.stdout.flush()
An here is a video of it in action.