Sunday, 04 May 2014 11:02

Some useful javascript

Some useful javascript
Create an XML HTTP Request (XHR) object
  1. function create_XMLHttpRequest() { //create new XHR object
  2. if (window.XMLHttpRequest)
  3. {// code for IE7+, Firefox, Chrome, Opera, Safari
  4. xmlhttp=new XMLHttpRequest();
  5. } else {// code for IE6, IE5
  6. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  7. }
  8. return xmlhttp;
  9. }
 
 
Send request to Server and receive response from server
  • Get replay from php web server: serverResponseInfo=httpGetRequest("http://domain/myReply.php")
  • Read content from a text file: serverResponseInfo=httpGetRequest("myTextFile.txt")
  • Read content from a text file: document.getElementById("myDiv").innerHTML=loadXMLDoc("myTextFile.txt");
function httpGetRequest(url)
{
   xmlhttp= create_XMLHttpRequest();
   xmlhttp.open("GET",url,false);
   xmlhttp.send();
   return xmlhttp.responseText;
}
 
 
Check if file exist
  • To check a file if exist in www root directory: fileExists("/settings.xml")
  • To check a file if exist in the current directory: fileExists("settings.xml")
  1. function fileExists(filename) {
  2. xmlhttp= create_XMLHttpRequest(); //create new XHR object
  3. if(!xmlhttp) return false;
  4. try
  5. {
  6. xmlhttp.open("HEAD", filename, false);
  7. xmlhttp.send(null);
  8. return (xmlhttp.status==200) ? true : false;
  9. }
  10. catch(er)
  11. {
  12. return false;
  13. }
  14. }
 
 
Load an XML file using XML HTTP Request
Eg. loadXML("settings.xml")
  1. function loadXML(XML_filename) {
  2. xmlhttp= create_XMLHttpRequest();
  3. xmlhttp.open("GET",XML_filename ,false);
  4. xmlhttp.send();
  5. return xmlhttp.responseXML;
  6. }
 
 
Convert XML to string
  1. function xmlToString(xmlDoc){
  2. if(xmlDoc.xml){
  3. // MSIE
  4. xmlString = xmlDoc.xml;
  5. }else{
  6. xmlString = (new XMLSerializer).serializeToString(xmlDoc);
  7. }
  8. return xmlString;
  9. }
 
 
Create a string based on date
Example of output: 2014_0502
  1. function createString_accordingDate() {
  2. var today = new Date();
  3. var dd = today.getDate();
  4. var mm = today.getMonth()+1;//January is 0!
  5. var yyyy = today.getFullYear();
  6. if(dd<10){dd='0'+dd} ;
  7. if(mm<10){mm='0'+mm};
  8. return (yyyy + "_" + mm + dd);
  9. }
.
Published in Tutorials
 
TPicShow is an image slider VCL that enables you to create amazing splash screens and 176 transitional effects in pure Delphi code.
 
Features:
  1. Image transition can be controlled programmatically
  2. Image can be stretched or centered in the client area of the control
  3. Control can show a background image as centered, stretched, or tiled
  4. Transition process can use a separate thread
  5. New transitional effects can be easily implemented and added.
 
TPicShow's last update is on July 19, 2010 which does not support RAD Studio XE. By creating a package file, you can installing and running TPicShow on RAD Studio XE5 without any problem.
 
Published in Blog
Friday, 27 September 2013 20:40

Arduino BIT manipulations

Code below set third pin to HIGH without changing the state of any of the other pins
bitSet syntax: bitSet(x, n)
  • x: the numeric variable whose bit to set
  • n: which bit to set, starting at 0 for the least-significant (rightmost) bit

//initialized all pins to LOW
byte pinState = B00000000;

//set the third pin high
bitSet(pinState, 2); //pinState = B00000100

 
  
Code below set third pin to LOW without changing the state of any of the other pins
bitClear syntax: bitClear(x, n)
  • x: the numeric variable whose bit to clear
  • n: which bit to clear, starting at 0 for the least-significant (rightmost) bit

//initialized all pins to HIGH
byte pinState = B11111111;

//set the third pin LOW
bitClear(pinState, 2); //pinState = B11111011

 
 
Code below set each pin as LOW or HIGH without changing the state of any of the other pins
bitWrite Syntax: bitWrite(x, n, b)
  • x: the numeric variable to which to write
  • n: which bit of the number to write, starting at 0 for the least-significant (rightmost) bit
  • b: the value to write to the bit (0 or 1)
byte pinState= 0; //initialized all pins to LOW (B00000000)
bitWrite(pinState, 0, HIGH); //set first pin to HIGH, pinState = B00000001
bitWrite(pinState, 3, HIGH); //set third pin to HIGH, pinState = B00001001
bitWrite(pinState, 0, LOW); //set first pin to LOW, pinState = B00001000
 
 
Reads a bit of a number.
bitRead Syntax: bitRead(x, n)
  • x: the number from which to read
  • n: which bit to read, starting at 0 for the least-significant (rightmost) bit

byte pinState = B10101010;
for (int i=0; i<8; i++) {
   theBit = bitRead(pinState, i);
}

 

 

Published in Tutorials
Saturday, 20 July 2013 22:31

DIYLC电子绘图软件

DIYCL V3
DIYLC (DIY Layout Creato)是电子爱好者用来设计电路图的绘图软件,它可以设计原理电路图(Schematic)、洞洞电板路板(Strip board)、面包电路板等(Breadboard)。
 
DIYLC是一个免费与开源软件现在是3.27.0,应该是正式使用版本了。今天试用了这版本,感觉比之前的旧版本好用了许多,很适合初学者,所以决定在此与大家分享一下。
Published in 电子与电脑
Aarduino communicate with Vixen
Vixen is a free and popular light show creator software. With a PC and some hardware, anyone can have a professional-looking lighting display synchronized to music.
 
The latest version is Vixen3, the most attractive feature is supported for preview. Compare to Vixen2, Vixen3 is a bit difficult to use & lack of some features in Vixen2. Please correct me if I'm wrong.
 
This tutorial is based on Vixen2, please download Vixen2 and install it to your computer. Prior to Vixen2, you must have install Microsot.NET Framework 2.0 in your computer.
Published in Blog
Sunday, 10 February 2013 19:03

Develop Android Application without Coding

App Inventor is an application use to develop Android application without programming. Its graphical interface is very similar to Scratch   that allow users to drag and drop visual objects to develop application which runs on many mobile devices.

Google released App Inventor on  December 15, 2010 and terminated as the Google product on August 2011. App Inventor is maintained by Massachusetts Institute of Technology and the code is become "Open Source".
Published in Blog
Sunday, 03 February 2013 18:35

Backing Up Raspberry Pi SD card

  • Insert SD card to computer
  • Launch Win32 Disk Imager
  • Select Device (source) which you want to backup from
  • Under Image File, give it a file name as shown in figure below
Backup Raspberry Pi SD card
 
  • Click Read button to start backing up
  • As shown in figure below, the image file size is equivalent to the size of SD card since I had resized the partition of my SD card
After Backup
Published in Tutorials
Monday, 28 January 2013 23:20

Gambas for Raspberry Pi

Gambas is a BASIC interpreter which is very similar to Visual Basic but running on Linux. Gambas from version 3.2 can run on Raspberry Pi and Gambas3 for Raspberry Pi image file is available here.
 
 
Installation
  • After download the image file (it is zipped), extract it to any location of computer. Now your computer should have an image file named as 2012-07-15-wheezy-gambas3.img
  • Write this img file to SD card using Win32DiskImager as describe in this article
  • Remove the SD card from computer once writing completed
  • Insert the SD card to Raspberry Pi & power on it.
  • Remote access to Raspberry using TightVNC as describe in this article
  • You should see Gambas icon appear on the desktop screen.
  • To start Gambas3, click the first icon at the left bottom screen then select Programming>Gambas3

People find that they are facing 100% CPU usage when using Gambas3, you can use the patch to overcome this problem.

Published in Blog
Sunday, 27 January 2013 22:19

TinyBASIC for Raspberry Pi

TinyBASIC is a simple BASIC interpreter which allow beginners to easily get into programming
 
Installing
You need to install libsdl-gfx1.2-4 package before install TinyBASIC. 
  • SSH to Raspberry Pi and execute command below:
sudo apt-get install libsdl-gfx1.2-4
  • Download TinyBASIC deb file and save it to any location of computer
  • Transfer the downloaded deb file from computer to /tmp folder of Raspberry Pi using WinSCP
  • Execute command below to start install TinyBasic
sudo dpkg -i /tmp/tinybasic_2.1-1_armhf.deb
Published in Blog
Saturday, 19 January 2013 20:28

Virtualbox Raspberry Pi Emulator

Would like to have Raspberry Pi experience before you own one of the boards? This article show you how to use VirtualBox as a emulator of Raspberry Pi
 
 
Prerequisite
VirtualBox running in Ms Windows

Download

  • Download the zip file and extract the included torrent file to any folder
  • Open the torrent file with any torrent client. I'm using flashget to open the torrent file.
  • The download should start.
  • You will have a 658,763KB ova file upon completion of downloading
Published in Blog
Back to Top