joplin-mdbooks-website/content/ardupilot/bitmask-calculator.md

192 lines
5.4 KiB
Markdown
Raw Normal View History

2022-08-13 07:12:56 -04:00
# Bitmask calculator
<style>
div.box{
width: 90%;
}
2022-08-13 09:06:42 -04:00
table {
border: 1px solid #222;
}
2022-08-13 07:12:56 -04:00
</style>
2022-08-13 09:06:42 -04:00
2022-08-13 07:12:56 -04:00
<script type="text/javascript">
2022-08-13 09:06:42 -04:00
// 1660392804
var bitmaskDescArr = [];
2022-08-13 07:12:56 -04:00
function dec2bin(dec) {
return reverse((dec >>> 0).toString(2));
}
function reverse(str) {
return str.split("").reverse().join("");
}
2022-08-13 09:06:42 -04:00
async function loadBitmask(){
// using the function:
removeAll(document.getElementById('paramSelect'));
2022-08-13 07:12:56 -04:00
url = document.getElementById("built").value;
2022-08-13 09:06:42 -04:00
console.log("fetching: "+ url);
if (url == ""){
2022-08-13 07:12:56 -04:00
return;
}
2022-08-13 09:06:42 -04:00
const response = await fetch(url,{
method: 'GET',
headers: {
'Content-Type': 'text/plain'
}
})
2022-08-13 07:12:56 -04:00
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
const xml = parser.parseFromString(data, "application/xml");
var params = xml.getElementsByTagName("param");
2022-08-13 09:06:42 -04:00
console.log("retrieved "+ params.length +" params")
for (var i = 0; i < params.length; i++) {
2022-08-13 07:12:56 -04:00
var param = params[i].attributes.name.nodeValue; //works, param name
2022-08-13 09:06:42 -04:00
param = param.replace("ArduPlane:","");
2022-08-13 07:12:56 -04:00
if(params[i].childNodes[1]){
if( typeof params[i].childNodes[1].attributes.name == "object"){
if(params[i].childNodes[1].attributes.name.nodeValue && params[i].childNodes[1].attributes.name.nodeValue == "Bitmask"){
var bitmaskDescTxt = params[i].textContent.trim();
bitmaskDescArr[param] = bitmaskDescTxt.split(',');
}
}
}
}
2022-08-13 09:06:42 -04:00
2022-08-13 07:12:56 -04:00
})
.catch(console.error);
2022-08-13 09:06:42 -04:00
// console.log(bitmaskDescArr.length);
drawSelect();
}
function drawSelect(){
bitmaskDescArr.sort();
Object.keys(bitmaskDescArr)
.sort()
.forEach(function(p, i) {
// console.log(p);
var el = document.createElement("option");
el.textContent = p;
el.value = p;
document.getElementById("paramSelect").appendChild(el);
});
}
function removeAll(selectBox) {
while (selectBox.options.length > 0) {
selectBox.remove(0);
}
2022-08-13 07:12:56 -04:00
}
function bitbox(bit){
dec = parseInt(document.getElementById("decimal").value);
2022-08-13 09:06:42 -04:00
if(!dec ){dec = parseInt(0);}
2022-08-13 07:12:56 -04:00
if(document.getElementById(bit).checked == true){
dec += parseInt(bit);
}else{
dec -= parseInt(bit);
}
document.getElementById("decimal").value = dec;
parseBitmask();
}
function parseBitmask() {
console.log("found "+Object.keys(bitmaskDescArr).length+" bitmask parameters");
document.getElementById("result").innerHTML="";
var tbl = document.createElement('table');
2022-08-13 09:06:42 -04:00
tbl.style="border: 1px solid black; width: 100%";
2022-08-13 07:12:56 -04:00
var tbdy = document.createElement('tbody');
dec = document.getElementById("decimal").value;
binary=dec2bin(document.getElementById("decimal").value);
2022-08-13 09:06:42 -04:00
if(!binary){binary = parseInt(0);}
if(!dec ){dec = parseInt(0);}
2022-08-13 07:12:56 -04:00
var param = document.getElementById("paramSelect").value;
for (var bit = 0; bit < bitmaskDescArr[param].length; bit++) {
var val = binary[bit];
var tr = document.createElement('tr');
var td = document.createElement('td');
td.style="border: 1px solid black;";
var singleBitDecvalue = Math.pow(2,bit); // convert the bit to a decimal
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "checked";
checkbox.id = singleBitDecvalue;
checkbox.setAttribute("onclick","bitbox("+singleBitDecvalue+")");
if (val == "1"){
checkbox.checked = true;
}else{
checkbox.checked = false;
}
td.appendChild(checkbox);
tr.appendChild(td);
var td = document.createElement('td');
td.appendChild(document.createTextNode(bitmaskDescArr[param][bit]));
if (val == "1"){
td.style = "border: 1px solid black; color: green";
}else{
td.style = "border: 1px solid black; color: red";
}
tr.appendChild(td);
var td = document.createElement('td');
td.style="border: 1px solid black; color: grey";
td.appendChild(document.createTextNode(Math.pow(2,bit)));
tr.appendChild(td);
tbdy.appendChild(tr);
tbl.appendChild(tbdy);
}
document.getElementById("result").appendChild(tbl);
}
</script>
<div class="box">
<div id="input">
<select id="built" onChange="loadBitmask();">
2022-08-13 09:06:42 -04:00
<option value='https://arducustom.github.io/v/dev/ArduPlane/apm.pdef.xml'>ArduCustom Plane master</option>
<option value='https://arducustom.github.io/v/10.0/ArduPlane/apm.pdef.xml'>ArduCustom Plane v10</option>
<option value='https://arducustom.github.io/v/9.0/ArduPlane/apm.pdef.xml'>ArduCustom Plane v9.0</option>
2022-08-13 07:12:56 -04:00
</select>
<select id="paramSelect" onChange="parseBitmask();">
</select>
<input type="text" id="decimal" value="1" oninput="parseBitmask();">
</div>
<br>
<div id="result">
</div>
</div>
2022-08-13 09:06:42 -04:00
<script type="text/javascript">
loadBitmask();
</script>
2022-08-13 07:12:56 -04:00
_(Many thanks to mfoos for writing this note.)_
2022-08-13 09:06:42 -04:00
2022-08-13 07:12:56 -04:00
* * *
<p style="font-size:80%; font-style: italic">
Last updated on August 13, 2022. For any questions/feedback,
email me at <a href="mailto:hi@stavros.io">hi@stavros.io</a>.
</p>