23 lines
1.4 KiB
JavaScript
23 lines
1.4 KiB
JavaScript
|
import {revive, persist, labelInput} from '/js/util.js';
|
||
|
export default {
|
||
|
id:'nmea', name:'NMEA 0183 Checksum', about:'Paste without the leading $ and without *XX.',
|
||
|
render(root){
|
||
|
const key='calc_nmea_v1';
|
||
|
const s = revive(key,{sentence:'GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,', expected:''});
|
||
|
const ui = document.createElement('div');
|
||
|
ui.append(labelInput('Sentence (no $ and no *XX)','text','sentence', s.sentence), labelInput('Expected checksum (optional, hex)','text','expected', s.expected,{placeholder:'e.g. 47'}));
|
||
|
const out = document.createElement('div'); out.className='result'; ui.append(out);
|
||
|
function checksum(str){ let x=0; for(const ch of str) x ^= ch.charCodeAt(0); return x; }
|
||
|
function calc(){
|
||
|
const sentence = ui.querySelector('[name=sentence]').value.trim();
|
||
|
const exp = ui.querySelector('[name=expected]').value.trim();
|
||
|
const sum = checksum(sentence);
|
||
|
const hex = sum.toString(16).toUpperCase().padStart(2,'0');
|
||
|
let html = `<div><strong>Computed:</strong> <code class="k">*${hex}</code></div>`;
|
||
|
if(exp){ const ok = hex === exp.toUpperCase(); html += `<div>${ok ? 'Match' : 'Mismatch'} (expected <code class="k">*${exp.toUpperCase()}</code>)</div>`; }
|
||
|
out.innerHTML = html; persist(key,{sentence, expected:exp});
|
||
|
}
|
||
|
ui.addEventListener('input', calc); calc(); root.append(ui);
|
||
|
}
|
||
|
}
|