Friday, October 18, 2013

Petit Computer Journal #21


A Simple Timer

This here is a simplified version of the timer program. I put it here just to clarify that there are other ways to do this. The previous version was used to show LOOP construct, but this version is an optimized version of it. Notice that duplicate codes are collapsed into one, thus simplifying the whole structure.



  1. 'TIMER PROGRAM BY HARRY HARDJONO
  2. SPSET 1,156,0,0,0,0
  3. SPOFS 1,-50,-50
  4. SPANIM 1,4,15
  5. SPSCALE 1,200

  6. @INIT
  7. CLS
  8. SPOFS 1,0,96,0
  9. SPREAD(1),X,Y
  10. ?"HOW MANY SECONDS?"
  11. INPUT T
  12. IF 0>=T GOTO @END
  13. CLS
  14. ?"A STARTS TIMER"
  15. ?"B SETS NEW TIME"
  16. ?"X EXIT"
  17. ?"Y PAUSE"

  18. @LOOP
  19. VSYNC 1:B=BTRIG()
  20. IF (SPCHK(1) AND 1) THEN SPOFS 1,X,Y,0 
  21. IF (B AND 16) THEN SPOFS 1,224,96,FLOOR((224-X)*T/224)*60:GOTO @TICK
  22. IF (B AND 32) GOTO @INIT
  23. IF (B AND 64) GOTO @END
  24. GOTO @LOOP
  25. @TICK
  26. IF (BUTTON(0) AND 128) GOTO @LOOP
  27. LOCATE 0,8:SPREAD(1),X,Y:?"TIME LEFT: ";FLOOR((224-X)*T/224);"   "
  28. IF (SPCHK(1) AND 1) THEN BEEP 28:WAIT 60:GOTO @TICK
  29. ?"TIME'S UP!":SPOFS 1,0,96,0:X=0:BEEP 50:WAIT 120
  30. GOTO @LOOP

  31. @END
  32. ?"GOODBYE!"
  33. SPCLR 1:WAIT 300


Thursday, October 17, 2013

Book Review #6


The Linux Command Line
William E. Shotts, Jr

I've been looking for a good shell scripting book when I chance upon this book. Reading the book gives me a good understanding, not only in scripting capabilities, but also the standard Linux shell programs. Things like lp, cat, grep, and sed are explored in details. I have come to the understanding that, in fact, I need this book more than anything. In my work with Raspberry Pi, I am dealing with Linux command line every day. I'm happy to say that I have written a few significant shell scripts that do not rely on powerful scripting language such as Perl or Python. Although the resulting scripts run slower, I don't mind at all. It's the learning process that matters.

In regard to Raspberry Pi, it is a great enabler device. However, I am with the opinion that a great enabler device by itself is not enough. I have numerous books about making this project or that project with Raspberry Pi. However, in the process of making the project, I have often come into the stumbling blocks of every day occurence. How do I format SD card? How to disable the screensaver? And so on. This book not only explains all the tools you need in order to navigate the Linux shell, but also gives you the powerful bash scripting technique that you can use to whip up a quick script leveraging the various Linux shell programs.

This is just my opinion, but I think this book is a must have for all Raspberry Pi users, if not all Linux users. Highly Recommended.

Tuesday, October 15, 2013

Raspberry Pi Journal #32


Adafruit LCD w/ Keypad First Test



I finally installed the script to run Adafruit LCD w/ Keypad Kit. Mine is the RGB version, so I get all the colors. It was rather scary trying to enter the unknown. I did it with some trepidation. Obviously, I backed up my system!




Turns out there's no trouble at all.

I'm not running Occidentalis, so I have to do things the hard way, which isn't hard at all. I opened up the file /etc/modules and there's only one entry on it. I simply added these two lines at the end (per instruction)

i2c-bcm2708
i2c-dev

And that's it. Reboot for it to take effect, and install some programs. Again, this is all mentioned in the helpful instructions.


  • sudo apt-get install python-smbus
  • sudo apt-get install i2c-tools


By the way, I make sure that I don't already have the package. But it's not necessary. The installation will install the latest package version if necessary, but that's about it. Also, i2c-tools install a set of program into /usr/sbin.

Next, I went and installed the source code from github. It turns out that I already have git on my computer. So, all I have to do is clone it from the directory.


  • git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git


And that's it. Very quickly done. Next, install update Python to allow GPIO programming:


  • sudo apt-get install python-dev
  • sudo apt-get install python-rpi.gpio


Thankfully the python demo script auto-detect my device. At first, I wasn't sure that the program is running. Then I remembered that the LCD is contrast sensitive. I pulled out my trusty Swiss Army Knife. Popped out philips screwdriver, and adjusted the contrast. I was elated when I see the lettering, and downright happy when the text and the color changes per button presses!

Oh, yeah! I guess I'm a hardware engineer, now! Well, I guess I should learn Python programming so I can do cool stuff with it!


Monday, October 14, 2013

Cooking Journal #8

Chicken Legs


Now I can get used with this. I like chicken legs, especially deep fried! There is only one problem: Tremendous amount of oil is being used. As it is, I don't want to use too much oil, and as you can see, the legs are only half submerged. This means that I cannot leave the cooking alone. I have to watch the legs being fried and keep turning them around.

On my previous posts, I mentioned how I never use condiment, or spices on all my chicken. Since this cooking method is so time consuming anyway, I decided to bread my chicken. Simply wash the chicken legs, and shake them around in cooking flour.

The result is that the wetness is somewhat contained, and the chicken becomes softer and juicier. Definitely tastier than simple frying without flour. Another thing: The wok will have to be cleaned thoroughly after this. So, good eating, yes, but tremendous amount of work, not only in cooking, but also in cleaning up afterwards.

Saturday, October 12, 2013

Nintendo Journal 8


TopGun LCD game


I also picked up an old LCD games. TopGun by Konami. It's just a single board game with LCD screen. There's a small speaker on the back. It's amazing how simple these things are. I think games like these are perfect for Petit Computer projects. I will be rustling up a few.

I marvelled at how thin the LCD screen is. It's just a thin plastic, nothing more. I looked at the contacts, and they're sooo tiny! I really doubt that they can align it perfectly, and yet, when I reassembled the device, it works fine!

I don't know how they do it. As simple as it is, that's not something any rookie can do. There must have been some degree of experimentation in order to fit things just right.

Friday, October 11, 2013

Petit Computer Journal #20


IdleLoop


I'm going to do something different this time around. Usually I would make a new program, illustrating a new concept. However, this time, I won't be doing something new and exciting. I'll be doing something that is old and boring: idle loop.

What is idle loop? Well, you know the main loop on the program that goes like this:


  1. @MAINLOOP
  2. 'get player input
  3. 'process player input
  4. GOTO @MAINLOOP


That's it. Idle loop is so called because if the player has no input, it'll just wait around doing nothing. In other words, being idle.

So, you're probably wondering why I want to show that here. Shouldn't that process be known to everybody? Yes, it should. But not to everybody. Just like loop, there are various situations that you have to watch out for. For example, where would you want to put the @INIT function?

INIT function deals with initial state of the program. This is where default values come into play. Also, I like to put in @QUIT function, where the program does clean up when it exits. I don't put it in all my programs, especially simple ones, but for completeness, I want my programs to clean up after itself. It's just good programming practice.

So, the structure would be


  1. Global settings, DIM, and such
  2. INIT function
  3. MAIN function
  4. END/QUIT/CLEANUP function


There is a difference between Global Settings and INIT. In Global Settings, you allocate memory to the variables, and load any static resource needed. Fortunately, Petit Computer takes care most of it for you. Very little needed to be here. Some programming language requires you to load modules/libraries here as well.

INIT function deals with initializing the variables to start-up state. That means loading any custom screens if any. Backgrounds, Icons, and the like. This function gets called whenever the player quit to the title screen. It basically start fresh. If you have game states then you load that separately from this function. And yes, you want to have that special loading function, so you can override default graphics and provide the player with custom sprites and skin.

QUIT function basically says good-bye. There's not much here for Petit Computer. If you don't need anything, just leave it out, or just put out a good-bye message to the player.

The MAIN function will handle several types of cases:


  1. Infinite Loop
  2. Loop with Timer
  3. Loop with VSYNC
  4. Loop with Player input (wait)
  5. Loop with Player input (no wait)
  6. Loop with Player input + background process
  7. Loop with Player input + multi-thread


1. Infinite Loop


The first case is the simplest. You want the program to run forever, like in showing demo.


  1. @LOOP
  2. ?"Hello World"
  3. GOTO @LOOP


2. Loop with Timer


Sometimes, you want to have the program run for a certain length of time. Say, like a Timer. You can do it like this:


  1. 'TIMER PROGRAM BY HARRY HARDJONO
  2. SPSET 1,156,0,0,0,0
  3. SPOFS 1,-50,-50
  4. SPANIM 1,4,15
  5. SPSCALE 1,200

  6. @INIT
  7. CLS
  8. ?"HOW MANY SECONDS?"
  9. INPUT T
  10. IF 0>=T GOTO @END
  11. SPOFS 1,0,48,0
  12. SPOFS 1,224,48,T*60

  13. @LOOP
  14. IF (SPCHK(1) AND 1)==0 GOTO @END
  15. BEEP 28:WAIT 60
  16. GOTO @LOOP

  17. @END
  18. ?"TIME'S UP!"
  19. SPCLR 1
  20. BEEP 50
  21. WAIT 300


And that's it. The code before the INIT creates and sets the sprite. INIT is used to send the sprite across the screen, timed according to T variable. The loop then simply waits for the sprite to stop moving, and gave a "ding" sound when it's done. Also, sprite clean up is done at the END.


3. Loop with Player input (wait)


Let's modify the program a bit. We want to have a player input, and we want it to wait for each input. Let's say A to start the timer, B to set the timer, and X to end the program.


  1. @INIT
  2. CLS
  3. ?"HOW MANY SECONDS?"
  4. INPUT T
  5. IF 0>=T GOTO @END
  6. ?"A STARTS TIMER"
  7. ?"B SETS NEW TIME"
  8. ?"X EXIT"

  9. @LOOP
  10. VSYNC 1:B=BTRIG()
  11. IF (B AND 16) GOTO @TICK
  12. IF (B AND 32) GOTO @INIT
  13. IF (B AND 64) GOTO @END
  14. GOTO @LOOP
  15. @TICK
  16. SPOFS 1,0,48,0
  17. SPOFS 1,224,48,T*60
  18. @TOCK
  19. LOCATE 0,4:SPREAD(1),X,Y:?"TIME LEFT: ";FLOOR((224-X)*T/224);"   "
  20. IF (SPCHK(1) AND 1) THEN BEEP 28:WAIT 60:GOTO @TOCK
  21. ?"TIME'S UP!":SPOFS 1,-50,-50:BEEP 50
  22. GOTO @LOOP

  23. @END
  24. ?"GOODBYE!"
  25. SPCLR 1:WAIT 300


If you notice, there's TICK and TOCK. Normally, I would put these on separate subroutine, but since I'm showing you how to do LOOP while waiting for user input, I simply group it all at once. Does it look like spaghetti code to you?

4. Loop with Player input (no wait)


Let's do one more improvement, and this time, we're going to do player input without waiting. Let's implement a PAUSE button. The truth is, it's very simple. Petit Computer, by default, does not wait for user input.

Here, we simply check if the sprite is moving. If so, then we stop it. Else, we start it.


  1. 'TIMER PROGRAM BY HARRY HARDJONO
  2. SPSET 1,156,0,0,0,0
  3. SPOFS 1,-50,-50
  4. SPANIM 1,4,15
  5. SPSCALE 1,200

  6. @INIT
  7. CLS
  8. ?"HOW MANY SECONDS?"
  9. INPUT T
  10. IF 0>=T GOTO @END
  11. ?"A STARTS TIMER"
  12. ?"B SETS NEW TIME"
  13. ?"X EXIT"
  14. ?"Y PAUSE"

  15. @LOOP
  16. VSYNC 1:B=BTRIG()
  17. IF (B AND 16) GOTO @TICK
  18. IF (B AND 32) GOTO @INIT
  19. IF (B AND 64) GOTO @END
  20. GOTO @LOOP
  21. @TICK
  22. SPOFS 1,0,48,0
  23. SPOFS 1,224,48,T*60
  24. @TOCK
  25. LOCATE 0,8:SPREAD(1),X,Y:?"TIME LEFT: ";FLOOR((224-X)*T/224);"   "
  26. IF (BUTTON(0) AND 128) GOTO @PAUSE
  27. IF (SPCHK(1) AND 1) THEN BEEP 28:WAIT 60:GOTO @TOCK
  28. ?"TIME'S UP!":SPOFS 1,-50,-50:BEEP 50
  29. GOTO @LOOP

  30. @PAUSE
  31. IF (SPCHK(1) AND 1) THEN SPOFS 1,X,Y,0 
  32. IF (BUTTON(0) AND 16) THEN SPOFS 1,224,48,FLOOR((224-X)*T/224)*60:GOTO @TOCK
  33. IF (BUTTON(0) AND 32) GOTO @INIT
  34. IF (BUTTON(0) AND 64) GOTO @END
  35. GOTO @PAUSE

  36. @END
  37. ?"GOODBYE!"
  38. SPCLR 1:WAIT 300



Simple as that. We simply use the variable values from TIME LEFT to stop the sprite where it stands, and starts it in according to TIME LEFT. There's a trick here, that we use BUTTON(0) instead of BTRIG. We do that because of WAIT 60 that we have will cause BTRIG signal to get lost. BUTTON(0) will read the input continuously. Press A to restart the timer.

Petit Computer Programming can be so easy! Next, we'll examine loops with background processes.

Thursday, October 10, 2013

Book Review #5


Zen and the Art of Well Being
Eric Chaline

This is an old book, published in 2001. I find it interesting that current self-help book deals with medicine. As in, take this medicine for brain, this medicine for stomach, this miracle diet for being thin, and so on. Rarely, that there is a book that emphasize balance. This book, with its Taichi principles does have it. It also has well-being as the core concept. It's certainly a very unique self-help book. It's closer to philosophical look at being healthy, than it is a definite step-by-step method to being well.

And that's why I like it. There is a sense of effortlessness throughout the book, that I enjoy reading. It's the most enjoyable self-help book I've read so far. Not much for resources, though. That I will have to research on my own. Yet, I do not regret purchasing this book. It's worth a read from time to time, as I strive to achieve a balance between work and play. Yin and Yang. Zen.