Thursday, August 24, 2017

Using Google Sheets to Manage SDR Touch Presets

One of my hurdles to playing with Software Defined Radio is that experimenting with a fresh list of potential radio frequencies is a cumbersome process. SDR Touch, my radio app of choice, has the ability setup presets, which makes jumping around to frequencies easy. But getting the initial list in there is a pain. I noticed that SDR Touch offers the ability to import and export presets, which I figured would vastly simplify this problem.

The first solution I tried was to create some presets, export them and try changing the XML file by hand. Unfortunately, the XML format was just a bit too low level to comfortably manage this way. On to Plan B, which was to create a simplified format and convert it to XML using some code.

While the Unix Geek in me wanted this 'simple format' to be text based, I figured this was a good opportunity for me to play around with AppScript, Google's solution for extending Docs, Sheets, etc. My plan was to manage the frequencies in a Google Spreadsheet and then write some AppScript to generate the ugly XML.

After about 15 minutes of playing with AppScript, I had a crude version of this solution implemented. Here's the spreadsheet and here's a few screenshots showing my code in action.

First off, I cataloged a bunch of interesting presets:

If you were paying close attention, you may have noticed in the screenshot above that there's a new top level menu: SDR Tools. Clicking on it, shows the entry point to the Export code I wrote. Pretty sweet, right?

And finally, clicking the above menu item causes the system to generate a dialog box with the relevant XML in place:

Ultimately, I had to copy and paste this XML into a file named SDRTouchPresets.xml and store this file in the root directory of my LG G6. With that done, I could import the file. And bam!, all the frequencies mentioned in the spreadsheet were loaded up and are now a press away from me accessing them:

While this worked, the solution still has a ways to go. Right now, only the currently selected sheet in the spreadsheet is included in the export file. I need to change it so that all sheets are processed and stored in the XML. Also, I should really be storing the XML in Google Drive, and not depending on Copy and Paste. But still, this is a good start and really opens my eyes up to a whole new world of possibilities in Google Sheets and Docs.

Here's the code that powers the above:

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Export Frequencies', functionName: 'sdrFreqExport'}
  ];
  spreadsheet.addMenu('SDR Tools', menuItems); 
}

function sdrFreqExport() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var name = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();

  // This represents ALL the data
  var range = sheet.getDataRange();
  var values = range.getValues();

  var doc    = ['<?xml version="1.0" encoding="UTF-8"?>',
                '<sdr_presets version="1">',
                ' <category id="1" name="' + name + '">'];
  for (var i = 2; i < values.length; i++) {
    doc.push('  <preset id="' + i + '" ' +
             '          name="' + values[i][2] + '" ' +
             '          freq="' + rawFreq(values[i][1]) + '" ' +
             '          centfreq="' + rawFreq(values[i][1]) + '" ' +
             '          offset="0" ' +
             '          order="' + i + '" ' +
             '          filter="13686" dem="0"/>');
  }
  doc.push(' </category>');
  doc.push('</sdr_presets>');
  
  showDoc(doc.join("\n"));
}

function rawFreq(freq, band) {
  return freq * 1000000;
}

function showDoc(doc) {
  var src = "<pre><![CDATA[" + doc + "]]></pre>";  
  var html = HtmlService.createHtmlOutput(src)
      .setWidth(400)
      .setHeight(300);
  SpreadsheetApp.getUi()
      .showModalDialog(html, 'SDR Export');
}

Feel free to grab it and improve it.

1 comment:

  1. Ben get your Ham license. I used this https://www.hamradiolicenseexam.com/ . My call sign is NY5N, so you know I am serious.

    ReplyDelete