Skip to content

Commit

Permalink
setFrequencies and setBaud (#105)
Browse files Browse the repository at this point in the history
* setFrequencies and setBaud

* built

* example buttons

* fix for window var

* fix

* fix

* fix again

* final fix!
  • Loading branch information
jywarren authored May 5, 2019
1 parent 3498536 commit 8ef7977
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
24 changes: 20 additions & 4 deletions dist/webjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,13 @@ WebJack.Connection = Class.extend({
}

var args = ifUndef(args, WebJack.Profiles.SoftModem);
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = ifUndef(args.audioCtx, new AudioContext());

if (typeof args.audioCtx === 'undefined') {
AudioContext = ifUndef(AudioContext, webkitAudioContext);
var audioCtx = new AudioContext();
} else {
var audioCtx = args.audioCtx;
}

var opts = connection.options = {
sampleRate : audioCtx.sampleRate,
Expand Down Expand Up @@ -778,7 +783,7 @@ WebJack.Connection = Class.extend({

// Returns valid JSON object if possible,
// or <false> if not.
connection.validateJSON = function(data) {
connection.validateJSON = function validateJSON(data) {
var object;
try {
object = JSON.parse(data);
Expand All @@ -789,12 +794,23 @@ WebJack.Connection = Class.extend({
}

// Set the connection profile
connection.setProfile = function(profile) {
connection.setProfile = function setProfile(profile) {
encoder.setProfile(profile);
if (typeof decoder === 'object')
decoder.setProfile(profile);
}

connection.setBaud = function setBaud(baud) {
connection.options.baud = baud;
connection.setProfile(connection.options);
}

connection.setFrequencies = function setFrequencies(high, low) {
connection.options.freqHigh = high;
connection.options.freqLow = low;
connection.setProfile(connection.options);
}

}

});
2 changes: 1 addition & 1 deletion dist/webjack.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ <h1>WebJack Demo</h1>
</ul>
</p>

<p>
<button class="btn-baud">Change baud</button>
<button class="btn-freq">Change frequencies</button>
</p>

<script>

var connection;
Expand Down Expand Up @@ -156,6 +161,15 @@ <h1>WebJack Demo</h1>
connection.setProfile(profile);
}

$('.btn-freq').click(function() {
var freqs = prompt('Enter low and high frequencies separated by a comma, like "1200,2400"').split(',');
if (freqs) connection.setBaud(freqs[0], freqs[1]);
});
$('.btn-baud').click(function() {
var baud = prompt('Enter a baud (speed) like 1225');
if (baud) connection.setBaud(baud);
});

</script>
<script src="fft/sketch.js"></script>

Expand Down
24 changes: 20 additions & 4 deletions src/webjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ WebJack.Connection = Class.extend({
}

var args = ifUndef(args, WebJack.Profiles.SoftModem);
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = ifUndef(args.audioCtx, new AudioContext());

if (typeof args.audioCtx === 'undefined') {
AudioContext = ifUndef(AudioContext, webkitAudioContext);
var audioCtx = new AudioContext();
} else {
var audioCtx = args.audioCtx;
}

var opts = connection.options = {
sampleRate : audioCtx.sampleRate,
Expand Down Expand Up @@ -132,7 +137,7 @@ WebJack.Connection = Class.extend({

// Returns valid JSON object if possible,
// or <false> if not.
connection.validateJSON = function(data) {
connection.validateJSON = function validateJSON(data) {
var object;
try {
object = JSON.parse(data);
Expand All @@ -143,12 +148,23 @@ WebJack.Connection = Class.extend({
}

// Set the connection profile
connection.setProfile = function(profile) {
connection.setProfile = function setProfile(profile) {
encoder.setProfile(profile);
if (typeof decoder === 'object')
decoder.setProfile(profile);
}

connection.setBaud = function setBaud(baud) {
connection.options.baud = baud;
connection.setProfile(connection.options);
}

connection.setFrequencies = function setFrequencies(high, low) {
connection.options.freqHigh = high;
connection.options.freqLow = low;
connection.setProfile(connection.options);
}

}

});

0 comments on commit 8ef7977

Please sign in to comment.