A blogspot journal showcasing computer programming, personal development, and other hobbies. Featuring Raspberry Pi, Petit Computer, and miscellaneous How-To.
Saturday, December 14, 2013
Nintendo Journal #17
Automatic Blood Pressure Monitor
I'm maintaining my blood pressure. So far, I'm keeping 120/80. It has been at that for a long time. However, with good exercise, my recovery rate has improved tremendously. I'm thinking that in order to lower the levels, I must change my diet. Well, I'll have to think about that. Exercise, yes. Diet, less chance of it happening than a snowball in hell!
Friday, December 13, 2013
Petit Computer Journal #29
A Simple Sprite Game
I've always said that writing a computer program is pretty easy if you know what you're doing. The question is, how easy can it be? I've written a version of Snakes and Ladders game in under 10 minutes. How quickly and easily can I write a sprite based game? Certainly, not too difficult.
I'm curious as to just how easy it is to write one. Certainly, there is the control. I settled for Up-Down-Fire, which is a variation of Left-Right-Fire. I was hoping for a single screen challenge program, and I managed to do it. However, it isn't fun at all. Also, it turns out that I don't need the Up-Down movement at all. Just sit there and hit the fire button!
It's all very well and good for a single screen challenge, but I was curious as to how much better it would be, had it been a better fleshed out game. So, forget about the single screen challenge and just do better!
So far, I'm using standard graphics. It's very convenient that the graphics are all built-in. However, there is always a way to have custom made graphic. I won't list the source code, but check out the QR made for this game.
http://petitcomputer.wikia.com/wiki/Santa_Helper
I've always said that writing a computer program is pretty easy if you know what you're doing. The question is, how easy can it be? I've written a version of Snakes and Ladders game in under 10 minutes. How quickly and easily can I write a sprite based game? Certainly, not too difficult.
I'm curious as to just how easy it is to write one. Certainly, there is the control. I settled for Up-Down-Fire, which is a variation of Left-Right-Fire. I was hoping for a single screen challenge program, and I managed to do it. However, it isn't fun at all. Also, it turns out that I don't need the Up-Down movement at all. Just sit there and hit the fire button!
- CLS:Y=0:A=256:C=128:S=0
- SPSET 1,96,0,0,0,0:T=3600
- SPSET 2,40,0,0,0,0
- SPOFS 2,-16,0,0:SPANIM 2,2,15
- SPSET 10,13,3,0,0,0
- @LOOP
- T=T-1:IF T<0 THEN END
- VSYNC 1:B=BUTTON(0):DX=0:DY=0
- LOCATE 0,0:?"S=";S,"T=";T;" "
- IF SPHIT(10) THEN S=S+1:BEEP
- IF !SPCHK(10) THEN GOSUB @MM
- IF (B AND 16) THEN GOSUB @FR
- IF !(B AND 3) GOTO @LOOP
- DY=(B AND 3)*2-3
- Y=Y+DY:IF Y<0 THEN Y=Y+192
- Y=Y%192:SPOFS 1,0,Y,0
- GOTO @LOOP
- @FR
- SPOFS 2,0,Y,0:SPOFS 2,A,Y,C/2
- RETURN
- @MM
- SPOFS 10,99+RND(99),-20,0
- SPOFS 10,99+RND(99),2*C,C
- RETURN
It's all very well and good for a single screen challenge, but I was curious as to how much better it would be, had it been a better fleshed out game. So, forget about the single screen challenge and just do better!
- CLS:Y=0:A=256:C=128:S=0
- BGFILL 0,0,0,63,63,9,8,0,0
- FOR I=1 TO 32
- BGPUT 0,RND(64),RND(64),32,8,0,0
- NEXT
- SPSET 1,96,0,0,0,0
- SPSET 2,40,0,0,0,0
- SPOFS 2,-16,0,0:SPANIM 2,2,15
- FOR I=10 TO 70
- SPSET I,13,3,0,0,0
- SPOFS I,99,-50,0
- NEXT
- @INIT
- BGMPLAY 23
- T=10800:S=0
- @LOOP
- IF HYC<S THEN HC=S
- IF !BGCHK(0) THEN BGOFS 0,RND(512),RND(512),360
- T=T-1:M=FLOOR(T/180):IF T<0 THEN GOTO @END
- VSYNC 1:B=BUTTON(0):DX=0:DY=0
- LOCATE 0,0:?"$=";S,"TIME=";M;" ","HI $=";HC;" "
- FOR I=10 TO 70-M
- IF SPHITSP(I,2) THEN S=S+1:BEEP 3:GOSUB @HIT
- IF !SPCHK(I) THEN GOSUB @MM
- NEXT
- IF (B AND 16) THEN GOSUB @FR
- IF !(B AND 3) GOTO @LOOP
- DY=(B AND 3)*2-3
- Y=Y+DY:IF Y<0 THEN Y=Y+192
- Y=Y%192:SPOFS 1,0,Y,0
- GOTO @LOOP
- @FR
- SPOFS 2,0,Y,0:SPOFS 2,A,Y,C/2
- RETURN
- @MM
- SPCHR I,13
- SPOFS I,99+RND(99),-20+256*RND(2),0
- SPOFS I,99+RND(99),-20+256*RND(2),99+RND(C)
- RETURN
- @HIT
- SPCHR I,248
- SPANIM I,8,6,1
- RETURN
- @END
- BGMPLAY 6
- WAIT 360
- FOR I=0 TO 16:I=BUTTON(0):NEXT
- GOTO @INIT
So far, I'm using standard graphics. It's very convenient that the graphics are all built-in. However, there is always a way to have custom made graphic. I won't list the source code, but check out the QR made for this game.
http://petitcomputer.wikia.com/wiki/Santa_Helper
Thursday, December 12, 2013
Book Review 14
Book #14 Getting Started with Processing
Casey Reas and Ben Fry
First of all, I love the Processing language. It's fun, portable, and easy to learn. I abhor the name. Why? Because every time I google it, I never found it! Really, I think Proce55ing is better because when I google that, it goes right to the site!
So, having actually learned the language, why did I pick up the book? Because I love the language and I want as many people to use it as possible. The book is interesting because it is the first "Getting Started" book from O'Reilly that I found. And I found out that hand-drawn sketches makes perfectly usable illustrations, even when it can best served with computer graphic or CAD.
I find the book features just the right amount of personal touches for the artist in me, and yet not skimp on the technical side for the scientist in me. A rare treat that is yet to be duplicated from other "Getting Started" books.
The only complaint that I have is that the subject matter is too brief. It touches on other Processing usage, such as Arduino. However, I think that extra projects such as Arduino is best left to other "Getting Started" book. You can put a mention there, of course, but let's not dwell upon them.
All in all, a good book marred only by trying to be comprehensive in a small book. Highly recommended.
Labels:
14,
beginner,
book,
getting,
language,
processing,
programming,
review,
started
Tuesday, December 10, 2013
Raspberry Pi Journal #40
Portable Webcam Software - motion
Last part, we have all the software and hardware installed and ready to go. In this section, we are going to set up the software that does the motion detection: motion.
First, we're going to SSH into the device:
SSH hostname
Enter your password. Next, we're going to set up a few configurations options. We want to set up some directories. We need to be careful here because we're going to run the software as daemon, which means its own user name and priviliges. The user name is called "motion". The password? I don't know. I can tell you that "sudo login" followed by "motion" and no password does not work.
So, the first thing we want is to set up a directory for the webcam. I'm going to set it up under /home/common/webcam
- sudo mkdir /home/common/
- sudo mkdir /home/common/webcam
- sudo cd /home/common
- sudo chown motion webcam
- sudo chgrp motion webcam
- sudo chmod 777 webcam
That will create a special webcam directory, that is accessible by all. Hopefully, nobody will login into it and cause trouble. I would prefer chmod 775, but let's make things easy for now.
Next, we need to configure the motion program and set up some parameters. Specifically, I want:
Save a picture per second everytime motion is detected, continue for about 10 seconds after.
Create a time-lapse movie for the day
Create a time-lapse movie for all motion capture
Set a webserver, so we can view it on the web browser
Create a log directory and file
So, the first thing we need to do is to locate motion.conf file. I found it in /etc/motion/ directory. Otherwise, check /usr/local/etc/ directory. If all else, you can do it the hard way:
- cd /
- ls -lR * | grep motion | grep motion
Yes, I did "grep motion" twice. I did it to filter out the error messages because I run the command as a normal user, instead of root.
So, let's get to /etc/motion directory
- cd /etc/motion
- ls -l
You'll see the files motion.conf, and thread1.conf-thread4.conf. This is because motion software can handle up to 4 simultaneous webcams. I'm using only one, so I don't have to worry about threadN.conf files. The first thing I want to do is make a backup or motion.conf before doing anything else.
- cp motion.conf motion_conf_orig
- sudo nano motion.conf
Next, I'll just scan the file and change the appropriate fields, according to my wish list above.
- daemon on
- width 640
- height 480
- framerate 2
- minimum_frame_time 1
- brightness 200
- threshold 6000
- noise_level 128
- pre_capture 1
- post_capture 10
- gap 120
All right, so that was for the camera settings. Now we go to the Image File Output settings:
- ffmpeg_cap_new on
- ffmpeg_timelapse 5
- ffmpeg_timelapse_mode hourly
- ffmpeg_video_codec mpeg4
- text_double on
Now that we have all the files set up. Let's set up all the file names and directories:
- target_dir /home/common/webcam
- jpeg_filename %H%M%S
- movie_filename %H%M%S
- timelapse_filename lapseview
Finally, we're into the server territory. We want to configure the ports for our webcam. There are two factors here: control and webcam. Control is the user interface for our webcam. webcam is the address that we want to use as mjpeg streamer to see what our camera see
- webcam_port 8081 <-change this to any open port you like
- webcam_quality 50
- webcam_motion on
- webcam_localhost off
- webcam_limit 600
- control_html_output off
And that should do it! There are other settings that I can use, especially the ones regarding picture/movie generation. This is useful if we want to copy the movie files somewhere.
So, now, let's reboot, and see if everything works as expected!
- sudo reboot
and load a web browser, connected to the camera host address and port, and check to see if there's a web stream out there!
Pinging cloudypi seems to work, but I don't see the webcam being on? This where trouble starts. How come I can't start the process? Because there's something that I need to do, that is missing from the step. Reading the magazine, it says to set "start_motion_daemon yes" to motion.conf. And yet, reading the manual "man motion", it does not mention it at all!
So, which should I believe? The printed one? Or the one live on my device? Obviously, the one on my device. Is there no hope for me? There is something about user guide. Let's check it out:
/usr/share/doc/motion/motion_guide.html
Well, that didn't help at all. I know there's something that I need to change in order to start motion from boot, but what?
I tried to run motion from command line. Failed due to lack of permission. Create the motion.pid file, and change all permission.
sudo mkdir /var/run/motion
sudo touch /var/run/motion/motion.pid
sudo chmod 666 /var/run/motion/motion.pid
Now, let's see if it works. Well, the daemon works. The SSH hangs. Not good at all! In the end, I had to unplug the cable, and replug. Second time around, I got clever and use the setup mode:
- motion -s -c /etc/motion/motion.conf
It works fine. I see avi, jpg, and mpg files. Set up is a success, now to automate it. There's quite a bit of assumption that you know how to start a program during boot, but as you can see, it's not obvious. With wrong headed instruction such as "start service" things, then I'm just as confused as ever.
However, there is the salvation that is the net. Digging around for hours did yield something. There is a motion file in /etc/init.d. Aha! Let's check it out.
It sure looks like one of those service files. So, maybe the instruction does mean something. Just not 100% accurate. Looking at the file, I see this:
- check_daemon_enabled () {
- if [ "$start_motion_daemon" = "yes" ] ; then
- return 0
- else
- log_warning_msg "Not starting $NAME daemon, disabled via /etc/default/$NAME"
- return 1
- fi
And the simplest thing is to go open up /etc/default/motion file and see what it says.
- # set to 'yes' to enable the motion daemon
- start_motion_daemon=no
Uh, I think we hit the jackpot, here! Are you kidding me? This is all that's needed? How come there's not one single line telling me to do this in the manual? Do you know how much time I wasted looking for this one line? Sigh.
Well, change it to "yes", and see what happens! Changed the file. Reboot. SSH into it. Run "top". Yes, it's up there. Check webcam directory. Does not seem to write to the directory. Stop the program and check it out.
- sudo service motion stop
Further checking yields images still not being written. There's only one conclusion that I can gather: motion.conf is not being read correctly. Strange thing is, if I just start it normally, it works fine! So, back to /etc/init.d/motion and see if I can force motion.conf to be read.
- DAEMON=/usr/bin/motion -c /etc/motion/motion.conf
I just don't see how nobody is running into these kind of problems! Or if they did, they don't say. Did the setting somehow changed? I don't know. It certainly is frustrating. I know it is possible, but there's absolutely no instructions, and so, I have to try things out myself.
Did not work. After reading the manual for start-stop daemon, I came up with this:
- if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion -- -c /etc/motion/motion.conf ; then
That seems to work. Alas, no new images is loaded. So, maybe the configuration file is read correctly, after all, but then what is wrong with the process? Doing it interactively works fine, after all. Why would putting it into the service fails?
Interestingly, not only starting it manually with service fails to write images, but it somehow kicked the "output_motion on", and yet, checking it via the web control interface shows that it's off. Obviously, there's something seriously wrong here, but I don't know what. Gremlins, that's what it is.
Checked /var/log/user.log
It turns out that the program is having trouble writing to timelapse.mpg. I changed the attribute with
- chmod 777 timelapse.mpg
And restarted the service. It works fine, now. Jeez. Do you know how long it took me to find that? There's just not enough instructions on the web!
So, reboot, and see if the process still hopefully works.
Everything looks good to me! Well done!
And it only took one whole day!
Monday, December 9, 2013
Cooking Journal #16
Cooking Sushi
Probably not the best title. You're probably wondering: It's Sushi! What is there to cook? Actually, sushi refers to the vinegared rice or sumeshi. There are all kinds. Rolled sushi, finger sushi, scattered sushi, and pressed sushi.
The one I like to make is actually scattered sushi, which is vinegared rice in a bowl with ingredient scattered on top.
Finger sushi is the one you find in a good sushi restaurant. It takes skill to press sushi and meat just right.
Rolled sushi is the one you find in a supermarket. It's relatively easy to make, and makes a nice presentation. Better than the scattered sushi type.
I've never seen pressed sushi. I imagine it's a cheap industrialized kind.
The traditional way to cook sushi rice is with wooden bowl. Once the rice is cooked, dump it into a wooden bowl. Sprinkle vinegar+sugar mixture, while fanning vigorously. The rice will then have the proper texture and consistency while having illustrious sheen on the surface.
Does it matter? Having done it that way, I'd say yes. It makes for a better sushi rice. Of course, my motto is "cheap, fast, and easy" and so I don't usually bother doing any of that.
I also just cook my regular long-grain rice, instead of special sushi rice.
As soon as the rice is done, I fluff the rice using bamboo spatula while sprinkling vinegar a little bit. As you can see, I use it straight from the bottle. I don't bother mixing a special solution for it. And no, I don't use wooden bowl to do it.
The result is less than ideal, but close enough to the real thing, that I don't mind at all. Of course, you may have a different opinion about it.
Then I simply put the rice into a bowl and top it off with some meat I have on hand. That usually means hot-dog.
What? You expect maybe some special sashimi for it? ;)
Saturday, December 7, 2013
Nintendo Journal #16
2 Bird Houses
Just a little something I pick up from Thrift stores. I'm using these as inpirations for craft projects. I had in mind using cardboard for the craft projects, but unfortunately, it petered out. Didn't work.
Oh, well. I'll think of something else. In the meantime, the left open birdhouse is used as pen holder. The right one is perched on top of my bookshelf.
Friday, December 6, 2013
Petit Computer Journal #28
Petit BrainF
Yeah, I was wondering just exactly how hard it is to implement a language that has only 8 operators involved. Not hard at all! Of course, I cheated and used graphic screens for my memory. It'll be probably a bit more complicated if I use straight memory. A bit.
The program cannot fit into one screen, but it's certainly a candidate for less than 100 lines program. It took me a couple hours because I was careless in moving the pointers.
The CYCLE subroutine is there to keep the variables within range, and the FB subroutine is there as a debugger that I use when debugging the program. So, I commented it out in the final version.
Another person, IAmAPersson (what a name!) was working on a version of the compiler as well. Check Petit Wikia for it. Maybe he posted it there by now.
I don't know the policy of this blogspot, but I know my policy and that is family friendly. I don't want to use the full name of the program. Thank you. Next time you designed a programming language, make sure you name it proper!
Yeah, I was wondering just exactly how hard it is to implement a language that has only 8 operators involved. Not hard at all! Of course, I cheated and used graphic screens for my memory. It'll be probably a bit more complicated if I use straight memory. A bit.
The program cannot fit into one screen, but it's certainly a candidate for less than 100 lines program. It took me a couple hours because I was careless in moving the pointers.
The CYCLE subroutine is there to keep the variables within range, and the FB subroutine is there as a debugger that I use when debugging the program. So, I commented it out in the final version.
Another person, IAmAPersson (what a name!) was working on a version of the compiler as well. Check Petit Wikia for it. Maybe he posted it there by now.
I don't know the policy of this blogspot, but I know my policy and that is family friendly. I don't want to use the full name of the program. Thank you. Next time you designed a programming language, make sure you name it proper!
- REM PBF BY HARRY HARDJONO
- @INIT
- ACLS:CLEAR
- GPAGE 0:GCLS
- GPAGE 1:GCLS
- MS=30000
- PTR=0
- PC=0
- MPC=0
- CSR=0
- GPAGE1:RESTORE @HELLO
- @LOADP
- READ A$
- IF A$=="" GOTO @RUNP
- FOR I=0 TO LEN(A$)-1
- GPSET PC%256,PC/256,ASC(MID$(A$,I,1))
- PC=PC+1:NEXT
- GOTO @LOADP
- @RUNP
- PC=0:GPAGE 0
- @LOOP
- GPAGE 0:VAR=GSPOIT(PTR%256,PTR/256)
- GPAGE 1:C=GSPOIT(PC%256,PC/256)
- 'GOSUB @FB
- IF C==62 THEN PTR=PTR+1:GOSUB @CYCLE:GOTO @LOOP2
- IF C==60 THEN PTR=PTR-1:GOSUB @CYCLE:GOTO @LOOP2
- IF C==43 THEN VAR=VAR+1:GOSUB @CYCLE
- IF C==45 THEN VAR=VAR-1:GOSUB @CYCLE
- IF C==46 THEN LOCATE CSR%32,CSR/32:?CHR$(VAR);:CSR=CSR+1:GOTO @LOOP2
- IF C==44 THEN GOSUB @GETCHAR
- IF C==91 THEN MPC=1:GOSUB @SR:GOTO @LOOP2
- IF C==93 THEN MPC=1:GOSUB @SL:GOTO @LOOP2
- IF C==0 GOTO @END
- GPAGE 0:GPSET PTR%256,PTR/256,VAR
- @LOOP2
- PC=PC+1:GOSUB @CYCLE
- GOTO @LOOP
- @FB
- LOCATE 0,5:?PTR,PC,VAR
- LOCATE 0,6:?CHR$(C)
- WAIT 6
- RETURN
- @CYCLE
- IF PC<0 THEN PC=PC+49152
- IF PTR<0 THEN PTR=PTR+MS
- IF VAR<0 THEN VAR=VAR+256
- PC=PC%49152
- PTR=PTR%MS
- VAR=VAR%256
- CSR=CSR%768
- RETURN
- @GETCHAR
- A$=INKEY$():IF A$=="" GOTO @GETCHAR
- VAR=ASC(A$):WAIT 30
- RETURN
- @SR
- IF VAR!=0 THEN RETURN
- PC=PC+1:GOSUB @CYCLE
- GPAGE 1:C=GSPOIT(PC%256,PC/256)
- IF C==91 THEN MPC=MPC+1
- IF C==93 THEN MPC=MPC-1
- IF MPC<=0 THEN RETURN
- GOTO @SR
- @SL
- IF VAR==0 THEN RETURN
- PC=PC+1:GOSUB @CYCLE
- GPAGE 1:C=GSPOIT(PC%256,PC/256)
- IF C==91 THEN MPC=MPC-1
- IF C==93 THEN MPC=MPC+1
- IF MPC<=0 THEN RETURN
- GOTO @SL
- @END
- WAIT 600
- END
- @PLOP
- DATA "-[>-[.-]<]"
- DATA ">>++++[.->++++++[.-]<]"
- DATA ""
- @CAT
- DATA ",[.,]"
- DATA ""
- @HELLO
- DATA "+++++ +++++ [>+++++++"
- DATA ">+++++ +++++ >+++ >+ <<<<-]"
- DATA ">++.>+.+++++++ ..+++.>++.<<"
- DATA "+++++ +++++ +++++"
- DATA ".>.+++. ------ .----- --- .>+.>."
- DATA ""
Subscribe to:
Posts (Atom)