Showing posts with label hacking. Show all posts
Showing posts with label hacking. Show all posts

20130206

DIY TinyMarquee: an Attiny24 based scrolling marquee

This project uses charlieplexing and software serial to push the capabilities of the AtTiny24. The source code and design files are up on a github repository.

Other DIY LED marquee projects may be of interest, including a large one hand-routed on protoboard, one made from Christmas lights, a network enabled display, one using ping-pong balls, and one implemented in the rear window of a car.



A terminal stock ticker

The python module ystockquote pulls stock prices and other information from Yahoo finance, "get_price(stockname)". Here is a short python script that downloads and formats stock prices and writes them to a local file.

Bitmap fonts 

We need to translate text into a format that we can send to a scrolling marquee. We send raw pixels, so that we can adjust fonts and graphics without re-loading the firmware. Columns of pixel data are sent as 5-bit integers over serial. I used Gimp to draw a font and this Jython script to convert it into bit-packed integers representing columns of pixels for each letter.*


To test converting text to the bitmap font, the script "scroll" takes the scraped data and scrolls it across a simulated marquee in the terminal ( there is also the short "terminal_marquee" script which scrolls indefinitely, but updates intermittently in the background )

Hardware

The hardware consists of 90 3mm LEDs arranged in a 5x18 grid. These are driven by 10 IO lines of an AtTiny24. The remaining free IO line is used to poll and listen for serial data.

There is also a 10K pull-up resistor on the AtTiny's reset pin, and a 0.1μF decoupling capacitor near the power pins. The surface mount AtTiny bridges one row of the LED pins, a bit unusual but this allows a compact layout with through-hole LEDs. The marquee gets power and data from a USB to TTL serial adapter.


Charlieplexing

Charlieplexing is a way to drive tons of LEDs form only a few pins. Since LEDs only light up when current is passed in one direction, you can place two LEDs for every unique pair of IO lines at your disposal. This lets you drive N*(N-1) LEDs from N IO pins.**

PCB design

While it is possible to wire up the grid of LEDs by hand, I would not recommend it. Instead of going through this tedium, you can design a custom board and get it professionally fabricated. I use the free version of Eagle Cad to design and prepare boards for manufacture. A full Eagle tutorial is beyond the scope of this writeup, but numerous tutorials can be found elsewhere online. The Eagle design files for this project can be found here.




Exporting gerber files for board fabrication

One you have finalized a board, you need to prepare design files for fabrication. PCB designs get exported to so called "Gerber" files, which are like the PDFs of circuit board design. Once you have these files you can send them off to a fab house for production. My favorite tutorial for this is on Hackaday.

For one-off boards, BatchPCB is the go-to place. For small runs, consider Advanced Circuits or Seeed studio's Fusion PCB service. For larger runs (more than 30), depending on board size, Goldphoenix is the place to go. Depending on which service you choose, you should get boards in a few days to five weeks. I used Seeed because it is relatively cheap, and it took about a month to ship to the US.

Part sourcing 

For cheap LEDs I use Ebay. For all other components ( especially the AVR microcontollers ), I source from Mouser or Digikey. For a low cost USB to Serial adaptor, look for "USB To RS232 TTL PL2303HX" on Ebay . These are cheaper than, say, an FTDI cable from Sparkfun, and have worked great for me. I'd hoped to save a few bucks by using charlieplexing and the AtTiny14 in the design -- the total cost of each board, shipping and USB-TTL converter included, from a lot of 10, is about $7.50:
10   boards     $30   
1000 LEDs       $10   
10   AtTiny     $14   
50   Resistors  $ 1   
10   Capacitors $ 1   
10   Headers    $ 1   
10   USB-TTL    $18   
-------------------   
                $75   
       /10      $ 7.50

Cleaning the boards 

After you're finished soldering, remove any solder flux adhered to the board. Apart from being unsightly, flux can be conductive and corrosive and damage the board over time. Hackaday has a good tutorial on this. We filled an old jar with 90% Isopropanol, dunked the boards in there, and shook them around for a while -- it worked wonderfully.

Software serial 

The Attiny24 does not have hardware support for serial ( UART ), so we'll have to make a software implementation. For information about the RS-232 communication protocol, see the wiki. I used polling at twice the serial rate (4800Hz) to monitor incoming serial data, which is works for transmitting five bit packets. Further details about the firmware can be found in the C source file.

Compiling with avr-gcc 

I never remember the commands to compile and upload firmware, so here are the commands for future reference.

#compile source to a file a.o targeting the AtTiny24
avr-gcc -Os -mmcu=attiny24 ./display_serial.c -o a.o 

#extract the text and data sections to make a binary for the AVR
avr-objcopy -j .text -j .data -O binary a.o a.bin 

# check the size ( this should be smaller than the amount of available flash )
du -b ./a.bin

# upload the binary to ( in this case ) the AtTiny24 
avrdude -c avrispmkII -p t24 -B4 -P /dev/ttyUSB1 -U flash:w:a.bin 


_____________________________

*If you don't have Jython or a working JVM installed, it may be easier for you to re-enter the font as text data and write a short conversion routine in python.

**If you're familiar with multiplexing there's a simple way to conceptualize board layout for charlieplexing. When you multiplex an NxN grid of LEDs, you use N IO lines to control power to the (-) ends of the LEDs, and N IO lines to control power to the (+) ends of the LEDs.  To go from multiplexing to charlieplexing, note that microcontroller pins can take on three sates : Low (-), High (+), and Off ("high impedence"). Each IO line can serve as both a (+) line and a (-) line. What happens if we use the same N pins to drive both the (-) and the (+) a multiplexed display? Everything works fine, as long as we use the "off" state to stop current. One problem: there some LEDs along the diagonal of the matrix that have their (+) and (-) driven by the same IO pin -- there is no way to make these light up. But, no worries, since we are laying out our own board, we can just delete these LEDs, or connect their (+) terminals to a reserved IO pin to regain control of them! Charlieplexed PCB design can be relatively simple: lay out a grid of LEDs as if you were to multiplex them, but connect the N anodes to the N cathodes, and either delete the N LEDs that end up being connected to the same IO line at both ends, or wire these up to a separate IO pin to finish things off.


20110721

Fractals on the Master Boot Record

WeAlone contributor Keegan has adapted the video feedback method of rendering Julia sets to fit in 512 bytes of Intel machine code that runs from the master boot record. This program was created for the IO MBR demo competition.



When a computer starts up, a very small program begins the process of loading and booting up progressively more complex programs, until an entire modern operating system is loaded. With some cleverness and optimization, we were able to make a program that fits in this space, and rather than booting the machine, renders animated Julia set fractals.

The source code is written in assembly, and can be downloaded here. The compiled program image can be downloaded from here.

If you're running Linux, you can try this out yourself using the qemu machine emulator, which can be retrieved from the package manager ( menu → system → administration → synaptic package manager, search for and install 'qemu-kvm' ). Once installed, simply typing "qemu phosphene.mbr" in a terminal should suffice.

You can also create a USB thumb drive that can boot most Intel architecture machines into this fractal rendering mode. Once booted, it is possible to remove the USB stick and leave the machine in a fractal-rendering coma until it is power cycled. Be careful here, if you overwrite the MBR on your own machine, you will trash your partition table and leave your system only able to boot as a fractal.

Assuming your USB thumb drive is /dev/sdb, the following commands will create a bootable USB stick. For the love of humanity do not write to /dev/sda, since this is probably your system boot partition.

$: sudo dd if=phosphene.mbr of=/dev/sdb
$: sync
( wait until IO lights stop blinking and remove the drive )

In one instance, we found that writing to /dev/sdb1 worked while writing to /dev/sdb did not. I'm not sure if this was a fluke, but you can try this if it doesn't seem to work on the first try.

These programs are so small, they can be distributed as plaintext in base64.


with a more flickery, rapidly changing colorscheme :

McCO2I7AuOAHjtC8ABC4EwDNELsPALQOvpl9rM0QhMB1+R5oAKAfMfa/AJ65AArzpR8xwM0QuAFP
uQEBvwB+zRCwQDHS93UEULgCT7sBAc0QusgDMMDuQrkAAVDuiNjuiPjuWAQCgcMDBeLv2+PHBYUA
3wXGBbHfBb0AAWgAEA+hFh+M4IDEEI7AvgAgv8B4sRCs0OgmAAVH4veBxjABgcfwAYH/AJhy5zH2
Mf+M4ID0UI7AiSzfBNjy2cDZ/9nJ2ereydn/2ejYwNz63vnZ7d7puoABuQACYNnqiRTfBNj12OGJ
DN8E2PXY4t3S3MrZwdjI3uveydjA2MPZwt7C2ercwt7B2M3fHIsU2MvfHIscgOcBieiB+oABcxXR
4sDmBIzgAPSO6DD2weIIAdNligcmiAVhR+Kghf91B4zAgMQQjsBKdY9F3tmM4ID0UI7gjthoAKAH
MdLoNgAx9r9AeLuAAbkAAqSF/3UKNgIW/g9T6B0AW+LvgceAAEv2w391CYzYgMQQjtgx9oXbddXp
8P64BU8x280Qw0kDDQppbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////
////////////////////////////////////////////////////////////////////////Vao=



A nice colorscheme with a black background :

McCO2I7AuOAHjtC8ABC4EwDNELsPALQOvp99rM0QhMB1+R5oAKAfMfa/AJ65AArzpR8xwM0QuAFP
uQEBvwB+zRCwQDHS93UEULgCT7sBAc0QusgDMMDuQrkAAVDuiNjuiPjuWAQCgcMDBeLv2+PHBYUA
3wXGBbHfBb0AAWgAEA+hFh+M4IDEEI7AvgAgv8B4sRCs0OgmAAVH4veBxjABgcfwAYH/AJhy5zH2
Mf+M4ID0UI7AiSzfBNjy2cDZ/9nJ2ereydn/2ejYwNz63vnZ7d7puoABuQACYNnqiRTfBNj12OGJ
DN8E2PXY4t3S3MrZwdjI3uveydjA2MPZwt7C2ercwt7B2M3fHIsU2MvfHIscgOcBMMCB+oABcxvR
4sDmBIzgAPSO6DD2weIIAdNligc8/3QC/sAmiAVhR+Kahf91B4zAgMQQjsBKdYlF3tmM4ID0UI7g
jthoAKAHMdLoNgAx9r9AeLuAAbkAAqSF/3UKNgIW/g9T6B0AW+LvgceAAEv2w391CYzYgMQQjtgx
9oXbddXp6v64BU8x280Qw0kDDQppbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////
////////////////////////////////////////////////////////////////////////Vao=


To convert these strings into a usable program, in Linux, use the base64 command. Type "base64 -d > phosphene.mbr" in the terminal, and press enter. Then, paste one of the base 64 encoded programs in the terminal. Press enter, and then control+D ( end of file ). This will convert the text into the compiled machine code for phosphene.mbr. Run it as explained above using qemu or making a bootable USB drive.

$: base64 -d > foo.mbr
McCO2I7AuOAHjtC8ABC4EwDNELsPALQOvp99rM0QhMB1+R5oAKAfMfa/AJ65AArzpR8xwM0QuAFP
uQEBvwB+zRCwQDHS93UEULgCT7sBAc0QusgDMMDuQrkAAVDuiNjuiPjuWAQCgcMDBeLv2+PHBYUA
3wXGBbHfBb0AAWgAEA+hFh+M4IDEEI7AvgAgv8B4sRCs0OgmAAVH4veBxjABgcfwAYH/AJhy5zH2
Mf+M4ID0UI7AiSzfBNjy2cDZ/9nJ2ereydn/2ejYwNz63vnZ7d7puoABuQACYNnqiRTfBNj12OGJ
DN8E2PXY4t3S3MrZwdjI3uveydjA2MPZwt7C2ercwt7B2M3fHIsU2MvfHIscgOcBMMCB+oABcxvR
4sDmBIzgAPSO6DD2weIIAdNligc8/3QC/sAmiAVhR+Kahf91B4zAgMQQjsBKdYlF3tmM4ID0UI7g
jthoAKAHMdLoNgAx9r9AeLuAAbkAAqSF/3UKNgIW/g9T6B0AW+LvgceAAEv2w391CYzYgMQQjtgx
9oXbddXp6v64BU8x280Qw0kDDQppbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////
////////////////////////////////////////////////////////////////////////Vao=
$: qemu foo.mbr