Category: computers suck

  • Adding a Bluetooth serial terminal to Raspberry Pi

    Hey! This article is really old. The advice given here will not work on a Raspberry Pi 3, and will need some care with recent versions of Raspbian.

    Sometimes you find a computer component that’s so cheap, that works so well, that you’re amazed you managed to live without it for so long. The JY-MCU Arduino Bluetooth Wireless Serial Port Module is that component for me right now.

    JY-MCU Arduino Bluetooth Wireless Serial Port Module from dx.com
    JY-MCU Arduino Bluetooth Wireless Serial Port Module from dx.com

    This little board is a cheap ($8.50!) Bluetooth serial port. It’s happy with the Raspberry Pi’s 3.3 V logic levels, and will communicate at standard rates between 1200 and 1,382,400 baud. It even comes with a nifty little cable which is just the right polarity for the Raspberry Pi’s GPIO pins. It’s really meant to do serial comms on an Arduino, but it’s not limited to that.

    What this board allows you to do is connect to your Raspberry Pi’s serial console via Bluetooth. That way, you can have your Raspberry Pi hidden away somewhere, and yet still log in as if you were talking to it directly through a serial cable. Combine this with a USB wireless adaptor (like the Belkin N150 that I use) and you’ve got a wireless device you can always connect to, even if your network goes down.

    In order to use this device with your Raspberry Pi, you’re going to have to do some reconfiguration. Exactly what reconfiguration you do depends on some additional hardware:

    1. If you have a USB-TTL Serial converter (like an FTDI Friend, FTDI Basic Breakout – 3.3V, or the one I use, the OSEPP FTDI), you can reconfigure the Bluetooth module to run at 115,200 baud, the default speed of the Raspberry Pi’s serial port.
    2. If you don’t have the serial converter, you’ll need to reconfigure the Raspberry Pi’s serial terminal to run at the JY-MCU Bluetooth adapter’s default 9600 baud.

    To reconfigure the Bluetooth module to run at 115,200 baud

    (I chose this option, as it allows me to use the Bluetooth module with Firmata on an Arduino, too.)

    The JY-MCU board comes with no instructions, but all the reconfiguration commands you’ll need are explained here: hc06_linvor_1.5_at_command_set [[hc06_linvor_1.5_at_command_set]] (cached copy; original has gone) While you’re setting the communications speed, you’ll probably also want to change the device name (so you can more easily recognize your own board, as the default is something like “Linvor”) and PIN (for that warm feeling of security that only a four digit code can provide). The device is configured using AT commands (or as we eldsters call them, Hayes commands) by plugging it directly into a USB-TTL Serial device attached to your computer. Here’s how you wire it:

    USB-TTL Serial    Bluetooth Serial
    ================= =================
    GND               GND
    VCC               VCC
    TXD               RXD
    RXD               TXD

    Note that TXD and RXD are crossed. The Bluetooth unit runs on a 3.6-6V supply, but 3.3V logic. To enter the AT commands, start a serial terminal (Hyperterm, minicom, screen …) at 9600 baud talking to the USB-Serial adapter, and copy and paste these commands in:

    AT+NAMEBluey
    AT+PIN4321
    AT+BAUD8

    You’ll have to disconnect the terminal and reconnect at 115,200 baud, as that last command just reset the Bluetooth device’s speed. You might want to use other settings than Bluey for the name and 4321 for the PIN, too.

    Update: check that your Raspberry Pi’s /boot/cmdline.txt contains:

    console=ttyAMA0,115200

    You will not get a login prompt otherwise.

    Now go to Using the Device.

    To reconfigure the Raspberry Pi’s serial terminal to run at 9600 baud

    Serial terminals traditionally ran at 9600 baud, and that seems a bit slow these days. But, if you don’t have a way of setting up the Bluetooth device differently, 9600 is what you’re stuck with. You’ll need to edit your Raspberry Pi’s /boot/cmdline.txt so that the part that previously read:

    console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

    to

    console=ttyAMA0,9600 kgdboc=ttyAMA0,9600

    Note that this file should only contain one line, so be careful you don’t add extra line breaks or your Raspberry Pi won’t boot. Save the file, reboot your Raspberry Pi, and go to the next section.

    Using the Device

    On your Raspberry Pi, connect the Bluetooth Wireless Serial Port Module as follows:

    Raspberry Pi      Bluetooth Serial
    ================= =================
    5V  (GPIO Pin  2) VCC
    GND (GPIO Pin  6) GND
    TXD (GPIO Pin  8) RXD
    RXD (GPIO Pin 10) TXD

    (Despite the minimum 3.6V rating, I’m happily running mine from the 3V3 power, GPIO Pin 1. YMMV.)

    When the board gets power, but isn’t paired, the LEDs on the Bluetooth module flash quickly. Now you need to pair the device with your computer (use 0000 as the PIN, or whatever you chose if you changed it), and it will appear as a serial port on your machine. On my Mac, that’s a device called /dev/tty.Bluey-DevB. The LEDs stop flashing when the port goes into use. Open up a serial terminal, set the device and speed correctly, and if all goes well, you should see:

    Debian GNU/Linux wheezy/sid raspberrypi ttyAMA0
    
    raspberrypi login:

    Success!

  • X10 home automation with Raspberry Pi: heyu

    I never quite get the hang of setting timers for lights. Either I forget daylight savings completely, or I set something so general that I find the lights coming on mid-afternoon when it’s still light. Minor annoyances require the over-application of technology, and fast!

    I scored an X10 ActiveHome Starter Kit for cheap(ish) on eBay. X10 is a pretty old technology (1970s! Scottish!) and has some severe limitations (slow! prone to interference! unencrypted!) but has a large user base, and did I mention it’s pretty cheap?

    The key component of a computer controlled X10 system is the CM11 computer interface. It takes serial commands from a computer, and pushes them out (slowly) as signals modulated over your house wiring. Various plug-in modules pick up these signals, and if the device address in the command matches that of the module, the module turns on (or off, or dims).

    Since the version of the CM11 interface that I have is serial, I’ll need a USB→Serial converter. All I had lying around was a very old Prolific PL2303 interface, which works fine with Raspbian, but I’d prefer an FTDI one for more reliability. Long-term stability of USB Serial on the Raspberry Pi is currently questionable; there’s some good discussion on kernel parameters that might help.

    To send X10 commands from a Raspberry Pi (or indeed, any Linux computer) you need heyu. You have to build it from source, but the instructions are clear, and it takes about 10 minutes to build on a 256 MB Raspberry Pi. The install script asks you where your serial port is, and for my device it is /dev/ttyUSB0.

    (Update: I re-imaged the Raspberry Pi that runs these tasks today and rebuilt heyu without success. Don’t assume you can do a ./configure; make; sudo make install here. You have to run heyu’s own ./Configure.sh first before make. It does some non-obvious magic. Read the README and you’ll be fine, unlike me …)

    Most of the lights in our house are fluorescent, which is a problem for the standard X10 lamp modules. CFLs are not dimmable, and the standard lamp module doesn’t work with them. The lamp modules don’t work very well with low-voltage halogen lamps, either; extreme buzzing ensues, with a faint brownish light oozing out from the bulb and a vague burning smell. Best avoided, and better to use an appliance module, which is a simple mechanical relay.

    The only controller that came with the kit that would work with my lights was the X10 transceiver, which also includes an appliance switch. I gave this device an address of H9 (house code H, unit code 9), and plugged in a lamp. To turn it on, I issued this command:

    heyu on H9

    After about 8-10 a couple of seconds and a loud CLUNK from the controller’s relay, the light came on (if it’s taking longer, read this comment). To turn it off, I told it:

    heyu off H9

    Whoa! Raw power! I can now turn AC devices on and off from my Raspberry Pi (Martin Creed, watch out!). I guess I could set up cron jobs to control the lights, but cron doesn’t know about solar time (Sunwait and SunCron do, if you want to futz with them). I’ve got MisterHouse running on the Raspberry Pi for more clever control, but more on setting that up later.

    Incidentally, if you’re in Europe, Marmitek sell a variety of 220 V 50 Hz X10 modules. Their website is much clearer than the angry-fruit-salad that is x10.com. It looks like X10 have updated their starter kit to include the newer CM15 USB interface which will likely not work with heyu.

  • The shell of dead media lives on

    You have to be of a certain age to recognize this:

    … not just as an artist’s travel palette, but as a repurposed case for a 9-track tape spool. While tape drives were iconic for mainframe computers (so much so, there’s a Unicode glyph for them: ✇), the last drives and tapes came off the line a decade ago. They’re not truly dead until everybody forgets what these cases were originally for.

  • Mesmerizing, isn’t it?

    This is my profile on Microsoft’s new social thing, socl:

    Every link produces no content, and the name looks suspiciously like “sod” if over-kerned. Well, at least it’ll make Google+ feel busy.

    I can’t seem to do anything; creating a post gives me this:

  • Sorry, folks …

    Sorry, folks at 10 PRINT, I think the Amstrad CPC’s version simply pwns the C64:

    10 PRINT CHR$(199+2*RND);: GOTO 10
  • Raspberry Pi as a USB audio capture device

    The Raspberry Pi’s hardware and software support has come a long way in the few months it has been in the wild. I first tried this application in the summer, and the results were dismal. Now, thanks much improved USB driver support under Raspbian, I’m pleased to say it works flawlessly.

    Earlier this year, I bought a turntable (ack!) for transferring vinyl to mp3. I have a TC-772 USB phono preamp, which spits out a 48 kHz stereo audio stream. If you plug the USB output of the preamp into a Rapberry Pi (running Raspbian Wheezy with all the updates), it’s instantly recognized as an audio device:

    $ lsusb
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
    Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
    Bus 001 Device 004: ID 08bb:2902 Texas Instruments Japan PCM2902 Audio Codec

    If you install the ALSA recording utilities (sudo apt-get install alsa-utils pulseaudio – this should pull in a whole bunch of necessary packages), you can record directly from this device with the following command:

    arecord -D 'pulse' -V stereo -c 2 -f dat -d 900 out.wav

    which records from the ‘pulse’ audio device, displaying a stereo text VU meter (handy for setting levels), writing to a two channel 16-bit 48 kHz file called ‘out.wav’ for a maximum of 900 seconds (15 minutes). arecord has a baffling number of recording source options; arecord -L will show them. ‘pulse’ was the first one I tried.

    So how does it sound? Here’s a 30 second excerpt from the only single I owned for years, The Music Tapes‘ “The Television Tells Us/Freeing Song by Reindeer”: Freeing Song by Reindeer – excerpt [mp3]. I’ve saved an even smaller snippet as lossless FLAC so you can see that the waveform’s pretty clean: FreeingSongbyReindeer-tiny_excerpt [flac].

    Sounds pretty good. Not quite as good as having Julian play it in your house, I’ll allow, but not bad for a first try with a $35 computer.

  • Ever heard the term “Appropriate Use of Technology”?

    Well, this is not that: For He’s a Jolly Good Fellow [mp3], as played on this:

    — an Arduino driving a stepper motor driving a Sankyo musical box. And yes, heat-shrink tubing ‘reinforced’ with dental floss doesn’t make a very robust flexible coupling.

  • Potentially handy Raspberry Pi power source

    This looks handy; it’s the Globe Electric 2-Outlet Tap with Surge Protection and 2 USB Chargers (#46082). The USB outlets are rated at 1A. It was $12 at Loblaws. I’ll let you know how it works out for powering the Raspberry Pi.

    Update: Yes! It works!

  • python + Arduino + Tk: Toggling an LED

    Whoa! This is so old I don’t even know where to start!

    • It’s using Python 2, so if it works at all it probably won’t for much longer, and Tkinter is something completely different under Python 3
      (grrreat planning there, Python guys …)
    • pyfirmata is likely ancient history too.

    Phil sent me a note last week asking how to turn an LED on or off using Python talking through Firmata to an Arduino. This was harder than it looked.

    It turns out the hard part is getting the value from the Tkinter Checkbutton itself. It seems that some widgets don’t return values directly, so you must read the widget’s value with a get() method. This appears to work:

    #!/usr/bin/python
    # turn an LED on/off with a Tk Checkbutton - scruss 2012/11/13
    # Connection:
    # - small LED connected from D3, through a resistor, to GND
    
    import pyfirmata
    from Tkinter import *
    
    # Create a new board, specifying serial port
    # board = pyfirmata.Arduino('/dev/ttyACM0') # Raspberry Pi
    board = pyfirmata.Arduino('/dev/tty.usbmodem411') # Mac
    
    root = Tk()
    var = BooleanVar()
    
    # set up pins
    pin3 = board.get_pin('d:3:o') # D3 On/Off Output (LED)
    
    def set_led():  # set LED on/off
        ledval = var.get()
        print "Toggled", ledval
        pin3.write(ledval)
    
    # now set up GUI
    b = Checkbutton(root, text = "LED", command = set_led,
                    variable = var)
    b.pack(anchor = CENTER)
    
    root.mainloop()
    

    This is explained quite well here: Tkinter Checkbutton doesn’t change my variable – Stack Overflow. I also learnt a couple of things about my previous programs:

    • You don’t really need to set up an Iterator unless you’re reading analogue inputs
    • My “clever” cleanup-on-exit code actually made the script hang on Mac OS.
  • Power Cost Monitor signals — for reals!

    After doing almost nothing with the Arduino and the elusive Power Cost Monitor signal, I’m finally getting something from it. Matt Colyer’s Power Monitor sketch does all the heavy lifting, for he managed to reverse engineer the protocol. His solution is a rather complex one, involving ethernet shields and Ruby. All I wanted was a simple serial logger, so I cut down Matt’s code, and modified the network output to simple print statements:

    == Waiting (38464857ms, 1676 bytes) ==
    == Sending (38467858ms, 1668 bytes) ==
    sample[data]=0000000a
    sample[data]=0000015a
    == Waiting (38467863ms, 1676 bytes) ==
    == Received (38592156ms, 1670 bytes) ==
    == Waiting (38592157ms, 1676 bytes) ==
    == Sending (38595158ms, 1668 bytes) ==
    sample[data]=00000000
    == Waiting (38595159ms, 1676 bytes) ==
    == Received (38624334ms, 1670 bytes) ==
    == Waiting (38624335ms, 1676 bytes) ==
    == Sending (38627336ms, 1668 bytes) ==
    sample[data]=ffffe746
    == Waiting (38627337ms, 1676 bytes) ==
    == Received (38688007ms, 1670 bytes) ==
    == Waiting (38688009ms, 1676 bytes) ==
    == Received (38688045ms, 1670 bytes) ==
    == Waiting (38688046ms, 1676 bytes) ==
    == Sending (38691047ms, 1668 bytes) ==
    sample[data]=00001748
    sample[data]=00000008
    == Waiting (38691052ms, 1676 bytes) ==
    == Received (38720026ms, 1670 bytes) ==
    == Waiting (38720027ms, 1676 bytes) ==
    == Sending (38723028ms, 1668 bytes) ==
    sample[data]=00000000
    == Waiting (38723030ms, 1676 bytes) ==
    == Received (38783686ms, 1670 bytes) ==
    == Waiting (38783687ms, 1676 bytes) ==
    == Sending (38786688ms, 1668 bytes) ==
    sample[data]=00000011
    == Waiting (38786689ms, 1676 bytes) ==
    == Received (38815636ms, 1670 bytes) ==
    == Waiting (38815637ms, 1676 bytes) ==
    == Sending (38818638ms, 1668 bytes) ==
    sample[data]=fffff322
    == Waiting (38818639ms, 1676 bytes) ==
    == Received (38847547ms, 1670 bytes) ==
    == Waiting (38847548ms, 1676 bytes) ==
    == Sending (38850549ms, 1668 bytes) ==
    sample[data]=00000001
    == Waiting (38850550ms, 1676 bytes) ==
    == Received (38879444ms, 1670 bytes) ==
    == Waiting (38879446ms, 1676 bytes) ==
    == Sending (38882445ms, 1668 bytes) ==
    sample[data]=0000374f
    == Waiting (38882446ms, 1676 bytes) ==
    == Received (38911210ms, 1670 bytes) ==
    == Waiting (38911211ms, 1676 bytes) ==
    == Sending (38914212ms, 1668 bytes) ==
    sample[data]=00000043
    == Waiting (38914213ms, 1676 bytes) ==
    == Received (39006981ms, 1670 bytes) ==
    == Waiting (39006982ms, 1676 bytes) ==
    == Sending (39009982ms, 1668 bytes) ==
    sample[data]=00000000
    == Waiting (39009983ms, 1676 bytes) ==
    == Received (39038895ms, 1670 bytes) ==
    == Waiting (39038896ms, 1676 bytes) ==
    == Sending (39041897ms, 1668 bytes) ==
    sample[data]=00000001
    == Waiting (39041898ms, 1676 bytes) ==
    == Received (39102999ms, 1670 bytes) ==
    == Waiting (39103000ms, 1676 bytes) ==
    == Sending (39106001ms, 1668 bytes) ==
    sample[data]=00000000
    == Waiting (39106002ms, 1676 bytes) ==
    == Received (39134880ms, 1670 bytes) ==
    == Waiting (39134881ms, 1676 bytes) ==
    == Sending (39137882ms, 1668 bytes) ==
    sample[data]=00000001
    == Waiting (39137883ms, 1676 bytes) ==

    Now it’s just a small matter of programming to work out what Matt’s Ruby code does. Ruby always looks to me like two programmers started coding at different ends of the same line, and collided in the middle. I’m hoping there’s enough processing power in the Arduino to do the conversion in the chip, and output useful log data as a serial stream.

    More later …

     

     

  • The MagPi

    Yay! My article on Arduino and Raspberry Pi is this month’s cover of The MagPi! Read it on Issuu, or download the PDF.

    Thanks to Ash and the rest of the team at the magazine.

  • Servo Control from pyfirmata + arduino

    Hey! This article is really old! So old, in fact, that I clearly thought that saying (ahem) “w00t w00t” was a good idea. Information here may be misleading and possibly wrong. You probably want to be using a newer client library and you definitely want to use an Arduino IDE ≥ 1.6 and not the ancient one that comes with Raspbian.

    pyFirmata‘s documentation is, to be charitable, sparse. After writing Raspberry Pi, Python & Arduino *and* a GUI (which should be making an appearance in The MagPi soon, w00t w00t yeet!), I looked at pyFirmata again to see what it could do. That pretty much meant digging through the source.

    Firmata can drive hobby servos, and if you’re not driving too many, you can run them straight from the Arduino with no additional power. I used a standard cheapo-but-decent Futaba S3003, which gives you about 180° of motion. The particular one I tried started to make little growly noises past 175°, so in the example below, that’s hardcoded as the limit.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    # move a servo from a Tk slider - scruss 2012-10-28
    
    import pyfirmata
    from Tkinter import *
    
    # don't forget to change the serial port to suit
    board = pyfirmata.Arduino('/dev/tty.usbmodem26271')
    
    # start an iterator thread so
    # serial buffer doesn't overflow
    iter8 = pyfirmata.util.Iterator(board)
    iter8.start()
    
    # set up pin D9 as Servo Output
    pin9 = board.get_pin('d:9:s')
    
    def move_servo(a):
        pin9.write(a)
    
    # set up GUI
    root = Tk()
    
    # draw a nice big slider for servo position
    scale = Scale(root,
        command = move_servo,
        to = 175,
        orient = HORIZONTAL,
        length = 400,
        label = 'Angle')
    scale.pack(anchor = CENTER)
    
    # run Tk event loop
    root.mainloop()
    

    The code above makes a slider (oh, okay, a Tkinter Scale widget) that moves the servo connected to Arduino pin D9 through its whole range. To set the servo position, you just need to write the angle value to the pin.

    I haven’t tried this with the Raspberry Pi yet. It wouldn’t surprise me if it needed external power to drive the Arduino and the servo. This might be a good excuse to use my Omega-328U board — it’s Arduino code compatible, runs from an external power supply, and has Signal-Voltage-Ground (SVG) connectors that the servo cable would just plug straight into.

  • a paucity of pi posts

    I would have been posting more Raspberry Pi posts, but my latest Made-in-UK 512MB board seems to have a raft of problems. Left to its own devices, it will happily corrupt any SD card I put in it. This is some fairly typical dmesg output:

    [36218.109865] mmc0: final write to SD card still running
    [36228.126345] mmc0: Timeout waiting for hardware interrupt - cmd12.
    [36228.127534] mmcblk0: error -110 sending stop command, original cmd response 0x900, card status 0x900
    [36248.152121] mmc0: final write to SD card still running
    [36258.163655] mmc0: Timeout waiting for hardware interrupt - cmd12.
    [36258.164865] mmcblk0: error -110 sending stop command, original cmd response 0x900, card status 0x900
    [36269.084446] mmc0: final write to SD card still running
    [36279.101766] mmc0: Timeout waiting for hardware interrupt - cmd12.
    [36279.102953] mmcblk0: error -110 sending stop command, original cmd response 0x900, card status 0x900
    [36309.899006] mmc0: Timeout waiting for hardware interrupt - cmd25.
    [36309.899047] mmc0: resetting ongoing cmd 25DMA before 4096/4096 [84]/[96] complete
    [36309.902774] mmcblk0: error -110 transferring data, sector 1721928, nr 848, cmd response 0x900, card status 0xc00
    [36309.902964] mmc0: DMA IRQ 6 ignored - results were reset
    [36309.903200] end_request: I/O error, dev mmcblk0, sector 1722649
    [36309.903227] end_request: I/O error, dev mmcblk0, sector 1722656
    ...
    [36309.903462] end_request: I/O error, dev mmcblk0, sector 1722768
    [36309.903823] Aborting journal on device mmcblk0p2-8.
    [36310.460263] journal commit I/O error
    [36310.653420] EXT4-fs error (device mmcblk0p2): ext4_journal_start_sb:327: Detected aborted journal
    [36310.667118] EXT4-fs (mmcblk0p2): Remounting filesystem read-only

    I’m not alone in having this problem; it’s reported by other people at Raspberry Pi • PI freezes and Filesystem corruption on the SD card – Stack Exchange. I don’t see any solutions or responses from the Foundation yet.

    Update: for no reason I can explain, setting over_voltage=2 in the /boot/config.txt file seems to make this problem go away. For me, at least.

  • LaunchPad MSP430 pomodoro timer using Energia

    I know a lot of people who bought the Texas Instruments MSP430 LaunchPad development kit but never really got into it. I’m one of them; the $4.30 purchase price was compelling, but the requirement to install a huge proprietary IDE (with its own embedded C dialect) was just too much.

    For this reason, Energia is great. It’s a fork of the Wiring/Arduino development environment for the MSP430. Looks just like Arduino, but quite red:

    The basics of the language are the same, although the pin names and functions are different from the Arduino. The basic board also has a red and a green LED, a user button, and the obligatory reset button.

    Just to try out Energia, I wanted a useful project that only used the onboard hardware. I came up with a version of a pomodoro timer, similar to the one used in The Pomodoro Technique® time management method. The one I made has the following features:

    • 25 minute “green light” work period
    • 5 minute “red light” rest period
    • At the end of the rest period, the green and red lights flash. You can choose to restart the timer (with the reset button), or turn the lights off with the user button.
    • You can turn the timer off at any time with the user button, and restart it from zero with the reset button.

    For me, this electronic version has several advantages over the wind-up official timer:

    • it doesn’t tick or ring distractingly
    • it’s cheaper than the real thing
    • it always resets to the same time.

    It does have a couple of possible disadvantages, though:

    • it doesn’t give an indication of time left
    • it’s not very accurate, since it uses the MSP430’s internal oscillator. Mine seems to give an approximately 26 minute work period, with a rest time that’s proportionally longer.

    Here’s the code (tested on a MSP-EXP430G2 board, version 1.4):

    /*
    
     launchpomodoro - simple task/rest timer for TMS430/Energia
    
     On reset: Green LED on for 25 minutes
     After 25 minutes: Red LED
     After 30 minutes: Flashing Red/Green LED (until reset)
     On button press: all lights off.
    
     scruss - 2012-10-15
    
     */
    
    // 25 minutes
    #define ENDWORKTIME 1500000
    // 30 minutes
    #define ENDRESTTIME 1800000
    
    unsigned long start=0;
    int red=1; // for light flashing state at end of rest time
    int green=0;
    
    void setup() {
      pinMode(GREEN_LED, OUTPUT);
      pinMode(RED_LED, OUTPUT);
      pinMode(PUSH2, INPUT);
      digitalWrite(GREEN_LED, HIGH); // start work
      digitalWrite(RED_LED, LOW); // stop rest
      start=millis();
    }
    
    void loop () {
      if ((millis()-start)>ENDWORKTIME) {
        digitalWrite(GREEN_LED, LOW); // stop work
        digitalWrite(RED_LED, HIGH); // start rest
      }
    
      if ((millis()-start)>ENDRESTTIME) {
        flicker(); // warn that we've overrun
      }
    
      if (digitalRead(PUSH2) == LOW) { // push quiet/off switch
        off();
      }
    }
    
    void flicker() { // toggle LEDs and wait a bit
      red=1-red;
      green=1-green;
      digitalWrite(GREEN_LED, green);
      digitalWrite(RED_LED, red);
      delay(333);
    }
    
    void off() { // appear to be off
      digitalWrite(GREEN_LED, LOW); // lights off
      digitalWrite(RED_LED, LOW);
      while (1) { // loop until reset
        delay(50);
      }
    }
    

    (or if you’d rather have the archived sketch: launchpomodoro-121015a.zip.)

    It would be trivial to port this to Arduino, but you would need some external LEDs, resistors and a pushbutton.

  • optar: paper-based archiving

    I’ve spent most of the day messing around with Twibright Optar, a way of creating printed archives of binary data that can be scanned back in and restored.  It looks like it was written as a proof-of-concept, as the only way to change options is to modify the code and recompile. Eppur si muove.

    To compile the code on OS X, I found I had to change this line in the Makefile from:

    LDFLAGS=-lm

    to

    LDFLAGS=-lm  `libpng-config --L_opts`

    After trying to print some samples at the default resolution, I had no luck, so for reliability I halved the data density settings in the file optar.h:

    #define XCROSSES 33 /* Number of crosses horizontally */
    #define YCROSSES 43 /* Number of crosses vertically */

    It’s quite important that your image prints and scans with a whole number of printer dots to image pixels. This used to be quite easy to do, before the advent of PDF’s “Scale to fit” misfeature, and also printer drivers that do a tonne of work in the background to “improve” the image. Add the mismatch between laser printer resolutions (300, 600, 1200 dpi …) and inkjets (360, 720, 1440 dpi …), and you’ve got lots of ways that this can go wrong.

    Thankfully, there’s one common resolution that works across both types of printers. If you output the image at 120 dpi, that’s 5 laser printer dots at 600 dpi, or six inkjet dots at 720 dpi. And there was peace in the kingdom.

    Here’s a demo, based on this:

    So I took this track (which I used to have as a 7″, got at a jumble sale in the mid-70s) and converted it to a really low quality MPEG-2.5: MichelinJingle8kbit — that’s 175KB for just shy of three minutes of music (which, at this bitrate, sounds like it’s played through a layer of socks at the bottom of the Marianas Trench, but still).

    Passing it through optar (which I wish wouldn’t produce PGM files; its output is mono) and bundling the pages into a PDF, I get this: optar_mj.pdf (760KB). Scanning that printout at 600dpi and running the pages through unoptar, I got this: optar1_mj.mp3. It’s the same as the input file, except padded with zeros at the end.

    Sometimes, the scanning and conversion doesn’t do so well:

    • mjoptar300dpi.mp3 — this is what happens when you scan at too low a resolution.
    • mjx.mp3 — I have no idea what went wrong here, but: glitchtastic!
  • ATTiny programming board

    It’s amazing what you can do when you raid your parts bin …

    Featuring fully configurable M-F jumpers so I can program (pretty much) any Atmel ATTiny microcontroller up to 28 pins. Might be able to do some ATMegas too.

  • Painfully Slow Amstrad CPC Emulation on the Raspberry Pi

    Yeah, you can do it, but whether you should, I don’t know. Download the latest Arnold/Linux source, then (according to this post) after installing the dependencies, you just need to change

    TARGET_FMT=elf32-i386

    to

    TARGET_FMT=elf32-littlearm

    in src/Makefile.in.

    It works, for very slow values of “works”. Mind you, I was running it through a remote X session, so 2 fps is all I could have hoped for …

  • Sometimes, things do not go exactly as planned … C development for Amstrad CPC on Raspberry Pi

    Hey! This is ancient! But since we’re talking about even more ancient computers, those bits still work. I’d recommend looking at the current installation instructions for z88dk rather than what I’ve got here.

    a very very crashed Amstrad CPC screen

    If you crash an Amstrad CPC, you often got some pretty patterns. Like the one above, which was supposed to print the alphabet, but got about as far as R, then started making coloured spots on the screen. My alphabet doesn’t (usually) contain coloured spots, so something went wrong.

    This post is only about the Raspberry Pi in that it’s the nearest always-on Linux system that I have. This would likely work fine on any Linux machine. While the Z80 cross compiler I use (z88dk) is available in the repos, I can’t get it to build anything, so I just pulled down the latest version. To build the compiler:

    wget http://nightly.z88dk.org/z88dk-latest.tgz
    tar xvzf z88dk-latest.tgz
    cd z88dk
    export Z80_OZFILES=$(pwd)/lib/
    export ZCCCFG=${Z80_OZFILES}config/
    export PATH=${PATH}:$(pwd)/bin
    ./build.sh
    

    This should result in a working environment. We can test it with a simple C program:

    /* alfa.c - print the alphabet */
    #include <stdio.h>
    
    int main(void) {
      char a='A';
      char b=26;
      while (b>0) {
        putchar(a);
        a++;
        b--;
      }
    }
    

    You can build it with:

    zcc +cpc -create-app -make-app -O3 -unsigned -o alfa.bin alfa.c -lcpcfs -zorg=16384
    

    You should end up with a file alpha.bin of approximately 4749 (!) bytes. You can copy it to a disc image using iDSK:

    iDSK blank.dsk -i alfa.bin -c 4000 -e 4000 -t 1
    

    It runs like this:

    You can do the same with Z80 assembly language (shown here in the most gratuitously pretty Amstrad assembler, Maxam):
    Although this results in only 11 bytes of code, it’s not portable; the C code above compiled and ran on both my Raspberry Pi and my Mac. It wouldn’t even run properly on a different Z80 system, as only the Amstrad CPC knows that call #bb5a prints the character in the A register. On the ZX Spectrum, for example, it was the completely different instruction rst 16 to print a character.

    (There’s a lot more on z88dk on the CPCWiki.)

  • Wireless, Cordless Raspberry Pi

    Yay! I found a wireless adapter that the Raspberry Pi is able to power without an external hub. It’s a Belkin N150 Micro Wireless USB Adapter. Not sure of the range, but it works well enough, and is cheap.

    Just to show you how hard-core I am, that’s a screenshot of a browser (showing a picture of the Raspberry Pi + LSTech Solar charger) running on that Raspberry Pi with an X session tunnelled to the laptop you can see in the background. My head hurts now.