Tutorials
View the current display configuration
tvservice -s
 
View the /boot/config.txt settings
vcgencmd get_config int
 
Get the list of what is supported by your monitor:
tvservice -d dataFile #dump information to dataFile
edidparser dataFile #load dataFile & display on screen
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. }
.
Count number of digits in a number(integer). Eg. countDigits(5678) return 4
  1. byte countDigits(int num){
  2. byte count=0;
  3. while(num){
  4. num=num/10;
  5. count++;
  6. }
  7. return count;
  8. }
 
 
Extract a digit from an number(integer). Eg. getDigit(5678,2) return 6
  1. int getDigit(unsigned int number, int digit) {
  2. for (int i=0; i<digit-1; i++) {
  3. number /= 10;
  4. }
  5. return number % 10;
  6. }
 
 
Extract all digits from a number and store them to an array
  1. #define MAX_NUMBER_OF_DIGITS 5
  2. byte array_to_hold_digit[MAX_NUMBER_OF_DIGITS];
  3.  
  4. void setup(void) {
  5. Serial.begin(9600);
  6. extractDigit_Save2Arrary(5678);
  7. Serial.println(array_to_hold_digit[0]);
  8. Serial.println(array_to_hold_digit[1]);
  9. Serial.println(array_to_hold_digit[2]);
  10. Serial.println(array_to_hold_digit[3]);
  11. }
  12.  
  13. void loop(void) {
  14.  
  15. }
  16.  
  17. /*----------------------------------------------------------------------------
  18.  count number of digits in a number(integer)
  19. ----------------------------------------------------------------------------*/
  20. void extractDigit_Save2Arrary(int number){
  21. byte number_of_digit = countDigits(number);
  22. for (byte i=0; i<number_of_digit; i++) {
  23. array_to_hold_digit[i] = getDigit(number,number_of_digit-i); //store each digit to array
  24. }
  25.  
  26. /*
  27.   for (byte i=0; i<number_of_digit; i++) {
  28.   Serial.print(array_to_hold_digit[i]);
  29.   }
  30.   Serial.println();
  31.   */
  32. }
  33.  
  34. /*----------------------------------------------------------------------------
  35.  count number of digits in a number(integer)
  36. ----------------------------------------------------------------------------*/
  37. byte countDigits(int number){
  38. byte count=0;
  39. while(number){
  40. number=number/10;
  41. count++;
  42. }
  43. return count;
  44. }
  45.  
  46. /*----------------------------------------------------------------------------
  47.  extract a digit from an number(integer)
  48. ----------------------------------------------------------------------------*/
  49. byte getDigit(unsigned int number, int digit) {
  50. for (int i=0; i<digit-1; i++) {
  51. number /= 10;
  52. }
  53. return number % 10;
  54. }
 
 
Saturday, 21 December 2013 22:21

Debouncing Multiple Switches

If you want to input a manual switch signal into a digital circuit you'll need to debounce the signal so a single press doesn't appear like multiple presses. 
 
There is a Bounce library for Arduino which is very easy to use. However there is limitations with this Bounce library.
  • You are unlikely to enable the internal pull-up resistor on switch pins, therefore each switch require an external pull-up resistor
  • It is not possible to detect the switch state before main loop (setup). When you press a switch during power on, you will never get the state of the switch.
Tuesday, 17 December 2013 21:51

Arduino function with optional argument(s)

Function Declaration and Function Prototypes: function prototype allowing the function to be used before it is defined.
functionReturnType myFunctionName(optionalArgumentType optionalArgument = defaultValue);
 
Function Implementation

functionReturnType myFunctionName(optionalArgumentType optionalArgument);
{
   // put your code here
}

 
Function calls

myFunctionName(); // Use the default value
myFunctionName(1); // Use specific value

 
Example
  1. int myFunction(int optionalArgument = 1);
  2.  
  3. void setup() {
  4. Serial.begin(9600);
  5. }
  6.  
  7. void loop() {
  8. Serial.println(myFunction()); // Use the default value
  9. Serial.println(myFunction(2)); // Use specific value
  10. }
  11.  
  12. int myFunction(int optionalArgument)
  13. {
  14. return optionalArgument;
  15. }
 
Page 3 of 6