Mastermindzh

Dipswitch generator

Jan 10th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //variables
  2.         var off = 'dip0';
  3.        
  4.         //methods to confirm a numeric entry
  5.         function isNumberKey(evt){
  6.             var charCode = (evt.which) ? evt.which : event.keyCode
  7.             if (charCode > 31 && (charCode < 48 || charCode > 57))
  8.                 return false;
  9.             return true;
  10.         }
  11.  
  12.         function isNumeric(n) {
  13.                 return !isNaN(parseFloat(n)) && isFinite(n);
  14.         }
  15.        
  16.         //method to flip the switch
  17.         function invert(){
  18.             if (off == 'dip1'){
  19.             document.getElementById("invertbutton").src = "images/software/dipswitch/down.png";
  20.             off = 'dip0';
  21.             }else{
  22.             document.getElementById("invertbutton").src = "images/software/dipswitch/up.png";
  23.             off = 'dip1';
  24.             }
  25.             convert('decimaltobinair');
  26.         }
  27.         function convert(id){
  28.             if(id == 'decimaltobinair'){ //convert decimal to binary notation
  29.                 error('0');
  30.                 setBinair(parseInt(getDecimal(), 10).toString(2).split("").reverse().join(""));
  31.                 imagecreate(getBinair());
  32.             }else{
  33.                 setDecimal(parseInt(getBinair().split("").reverse().join(""), 2));  //convert binary notation to decimal notation                                      
  34.                 error('0');
  35.             }
  36.         }
  37.         function imagecreate(binair){
  38.             var binairsplit = binair.split("");
  39.             for (var i=0; i<10; i++){ //loop through switches and set all to "off"
  40.                 document.getElementById('dip' + i).className = off;
  41.             }
  42.             for (var i=0; i<binairsplit.length; i++){
  43.                 if (off == 'dip1'){
  44.                     if (binairsplit[i] == 1){
  45.                         binairsplit[i] = 0
  46.                     }else{
  47.                         binairsplit[i] = 1
  48.                     }
  49.                 }
  50.                 document.getElementById('dip' + i).className = 'dip' + binairsplit[i];
  51.             }
  52.         }
  53.         function imagetobinair(id){
  54.             if (document.getElementById(id).className == 'dip0'){ //change dipswitch location
  55.                 document.getElementById(id).className = 'dip1';
  56.             }else{
  57.                 document.getElementById(id).className = 'dip0';
  58.             }
  59.             var binairimage = ""; //generate binary number based on the switch positions
  60.             for (var i=0; i<10; i++){
  61.                 var classnamesplit = document.getElementById('dip' + i).className.split("");
  62.                     if (off == 'dip1'){
  63.                         if ( classnamesplit[3] == '1' ){
  64.                             binairimage = binairimage + '0';
  65.                         }else{
  66.                             binairimage = binairimage + '1';
  67.                         }
  68.                     }else{
  69.                             binairimage = binairimage + classnamesplit[3];
  70.                     }
  71.             }
  72.             setBinair(binairimage);
  73.             convert('binairtodecimal');
  74.         };
  75.    
  76.    
  77.         //methods to catch errors
  78.         function error(errorcode){
  79.             if (errorcode == '0'){
  80.                  document.getElementById('error').innerHTML =  "";
  81.             }else if(errorcode == '1') {
  82.                   document.getElementById('error').innerHTML = "Geef aub een nummer in tussen de 0 en 1023";
  83.                   document.getElementById('dmx_binair').value = 'error';
  84.                   imagecreate('0');
  85.             }
  86.         }
  87.        
  88.         //getters and setters
  89.         function setBinair(value){
  90.             document.getElementById('dmx_binair').value = value;
  91.         }
  92.         function getBinair(){
  93.              return document.getElementById('dmx_binair').value;
  94.         }
  95.         function setDecimal(value){
  96.             document.getElementById('dmx_decimal').value = value;
  97.         }
  98.         function getDecimal(){
  99.             var decimal = document.getElementById('dmx_decimal');
  100.             if (isNumeric(decimal.value) && decimal.value < 1024 && decimal.value >= 0){
  101.                 return decimal.value;
  102.             }else{
  103.                 error('1');
  104.                 return 0;
  105.             }
  106.         }
Advertisement
Add Comment
Please, Sign In to add comment