Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //variables
- var off = 'dip0';
- //methods to confirm a numeric entry
- function isNumberKey(evt){
- var charCode = (evt.which) ? evt.which : event.keyCode
- if (charCode > 31 && (charCode < 48 || charCode > 57))
- return false;
- return true;
- }
- function isNumeric(n) {
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
- //method to flip the switch
- function invert(){
- if (off == 'dip1'){
- document.getElementById("invertbutton").src = "images/software/dipswitch/down.png";
- off = 'dip0';
- }else{
- document.getElementById("invertbutton").src = "images/software/dipswitch/up.png";
- off = 'dip1';
- }
- convert('decimaltobinair');
- }
- function convert(id){
- if(id == 'decimaltobinair'){ //convert decimal to binary notation
- error('0');
- setBinair(parseInt(getDecimal(), 10).toString(2).split("").reverse().join(""));
- imagecreate(getBinair());
- }else{
- setDecimal(parseInt(getBinair().split("").reverse().join(""), 2)); //convert binary notation to decimal notation
- error('0');
- }
- }
- function imagecreate(binair){
- var binairsplit = binair.split("");
- for (var i=0; i<10; i++){ //loop through switches and set all to "off"
- document.getElementById('dip' + i).className = off;
- }
- for (var i=0; i<binairsplit.length; i++){
- if (off == 'dip1'){
- if (binairsplit[i] == 1){
- binairsplit[i] = 0
- }else{
- binairsplit[i] = 1
- }
- }
- document.getElementById('dip' + i).className = 'dip' + binairsplit[i];
- }
- }
- function imagetobinair(id){
- if (document.getElementById(id).className == 'dip0'){ //change dipswitch location
- document.getElementById(id).className = 'dip1';
- }else{
- document.getElementById(id).className = 'dip0';
- }
- var binairimage = ""; //generate binary number based on the switch positions
- for (var i=0; i<10; i++){
- var classnamesplit = document.getElementById('dip' + i).className.split("");
- if (off == 'dip1'){
- if ( classnamesplit[3] == '1' ){
- binairimage = binairimage + '0';
- }else{
- binairimage = binairimage + '1';
- }
- }else{
- binairimage = binairimage + classnamesplit[3];
- }
- }
- setBinair(binairimage);
- convert('binairtodecimal');
- };
- //methods to catch errors
- function error(errorcode){
- if (errorcode == '0'){
- document.getElementById('error').innerHTML = "";
- }else if(errorcode == '1') {
- document.getElementById('error').innerHTML = "Geef aub een nummer in tussen de 0 en 1023";
- document.getElementById('dmx_binair').value = 'error';
- imagecreate('0');
- }
- }
- //getters and setters
- function setBinair(value){
- document.getElementById('dmx_binair').value = value;
- }
- function getBinair(){
- return document.getElementById('dmx_binair').value;
- }
- function setDecimal(value){
- document.getElementById('dmx_decimal').value = value;
- }
- function getDecimal(){
- var decimal = document.getElementById('dmx_decimal');
- if (isNumeric(decimal.value) && decimal.value < 1024 && decimal.value >= 0){
- return decimal.value;
- }else{
- error('1');
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment