lunedì 9 aprile 2012

A 4x5 5mm Red LED Display

If you have at least 20 LED and some wires you can create a very little character display to print some sliding phrases. The idea is extendible: of course if you want to extend the rows/cols of display you can as you desire.
This time I'll use a Digilent Nexys2 Board (with spartan3e 1200k FPGA) in which there is a Leon3 IPCore with GRGPIO controller to drive the expansion port. Nothing forbids you to use Arduino Board: in fact you need only 9 pins in this configuration.
For convenience I'll use a software driver very similar to Arduino (i.e. pinMode, digitalWrite, etc.) plus some useful function that accept mask as parameter (i.e. pinModeMask, digitalWriteMask, etc.): in this way in the "same cycle" it's possible to change the mode or the logical value to some pins. In any case it would be easy to implement a function that accepts input masks on Arduino. Maybe I'll do in next article =P.

How you can use only 9 pins to drive 20 LED? It's very easy, of course! You have to build a LED matrix and link cathodes along the rows (columns) and anodes along the columns (rows); in this way you cannot drive all LEDs in the same time. But our eyes are (very) slow and we have to exploit this feature.
In my case I'll connect the 4x5 LEDs in this way: the 5 rows connect the cathodes (5 cathodes), the 4 columns connect the anodes (4 anodes).

The resistors (100Ω) connect the pins to anodes
You can power up each anode in a loop (powering down the others) and configure the cathodes to light the LEDs you want (this is because the anode is less than the cathode: in this way every cycle has a shorter duration). If the LED has to be power off you have to change the pinMode of cathode pin to input (in this way the pin sets high-impedance, preventing the current passage) else as output with low value (allowing the current passage). In this way only one row (column) is working, the others are switched off; if the loop frequency is at least 30/40Hz our eyes don't perceive that a line is turned on while the others are off, but they see all LEDs lit at the same time.
So the first thing you have to define 4 pin consecutive what will drive anodes: in every loop highAnode = highAnode + 1 mod 4. In each cycle you have to configure the cathodes too.
Animation at 1 Hz of explained concept
Now you have to create a coder that transform ASCII chars into cathodes setting (maybe this requires several minutes and tests). Also, if you want, you can implement a shift mechanism to display some characters. Watch the video below to see a rough draft of the display.


Cya!

domenica 4 marzo 2012

Rock-Paper-Scissors Game for Bash Terminals

Maybe the Rock-Paper-Scissors is the most knew hand game of the world, specially by kids, used to toss up or as pastime. The name goes up from the three gestures used in the game: two players keep a hand closed; they count aloud to three and each time raising one hand in a fist and swinging it down on the count. At the third time (when they say "Scissors!") the players change their hands into one of three gestures:

  • Rock: expressed as a clenched fist
  • Scissors: represented by two fingers separated
  • Paper: expressed ad an open hand with all finger extended
The winner is determined by these simple rules:

  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock
  • Rock versus rock, scissors versus scissors, paper versus paper results in a draw
You often (or not xD) doesn't have a friend to play with. So, for me, I've write a simple bash/C application to play Rock-Paper-Scissors with your PC. It is so rough, but funny too!
Download the .tar.gz package (by link below) and extract it; after you have to compile the whowins.c file (you can do this simply launching gcc -O3 -o whowins whowins.c on your terminal). Now you can execute rps.sh as script specifying rock (or 1), paper (or 2), scissor (or 3) or random to play versus PC.
Eventually I'll develop a Java porting for all non-unix like platform (maybe with a GUI).



The package contains:

  • align.sh, center.sh and rps.sh: bash scripts  to prepare the environment and align the text
  • whowins.c: a C dummy program that establishes who player wins
  • strings: contains a "raw" configuration strings (be careful to change it, let the lines into the original ordering)


Enjoy

Download Rock-Paper-Scissor Game for Bash terminal