Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cTn-dev committed Apr 8, 2013
1 parent 9eeb22e commit be66960
Show file tree
Hide file tree
Showing 9 changed files with 1,111 additions and 4 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
baseflight-configurator
=======================

Google chrome/chromium based configurator
10 changes: 10 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('main.html', {
frame: 'chrome',
id: 'main-window',
minWidth: 960,
maxWidth: 960,
minHeight: 600,
maxHeight: 600
});
});
100 changes: 100 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
* {
margin: 0;
padding: 0;
border: 0;
list-style: none;
}
body {
font-family: sans-serif;
font-size: 12px;
color: #303030;
-webkit-text-size-adjust: 100%;
background-color: #dfddd0;
}
a {
color: #303030;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.clear-both {
clear: both;
}
.left {
float: left;
}
.right {
float: right;
}
#main-wrapper {
width: 940px;
margin: 10px auto 0 auto;

padding: 0 10px 0 10px;
}
#port-picker {
float: left;

height: 22px;
margin-bottom: 10px;
}
#port-picker li {
float: left;
}
#port-picker select {
height: 20px;
line-height: 20px;

float: left;
margin-right: 10px;

border: 1px solid silver;
}
#port-picker #port {
width: 120px;
}
#port-picker #baud {
width: 80px;
}
#port-picker #delay {
width: 60px;
}
#port-picker a {
float: left;

display: block;

width: 80px;
height: 18px;
border: 1px solid silver;

line-height: 18px;
text-align: center;
}
#port-picker a.refresh {
margin-right: 10px;

background-color: #5569d0;
color: white;
}
#port-picker a.refresh:hover {
text-decoration: none;
background-color: #424d84;
}
#port-picker a.connect {
margin-right: 10px;

background-color: #be2222;
color: white;
}
#port-picker a.connect:hover {
text-decoration: none;
background-color: #ea3131;
}
#port-picker a.connect.active {
background-color: #0fab16;
}
#port-picker a.connect.active:hover {
background-color: #13d81d;
}
5 changes: 5 additions & 0 deletions js/jquery-1.9.1.min.js

Large diffs are not rendered by default.

Empty file added js/main.js
Empty file.
246 changes: 246 additions & 0 deletions js/serial_backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
var connectionId = -1;
var connection_delay = 0; // delay which defines "when" will the configurator request configurator data after connection was established

$(document).ready(function() {
port_picker = $('div#port-picker .port select');
baud_picker = $('div#port-picker #baud');
delay_picker = $('div#port-picker #delay');

$('div#port-picker a.refresh').click(function() {
console.log("Available port list requested.");
port_picker.html('');

chrome.serial.getPorts(function(ports) {
if (ports.length > 0) {
// Port list received

ports.forEach(function(port) {
$(port_picker).append($("<option/>", {
value: port,
text: port
}));
});
} else {
$(port_picker).append($("<option/>", {
value: 0,
text: 'NOT FOUND'
}));

console.log("No serial ports detected");
}
});
});

// software click to refresh port picker select (during initial load)
$('div#port-picker a.refresh').click();

$('div#port-picker a.connect').click(function() {
var clicks = $(this).data('clicks');

selected_port = String($(port_picker).val());
selected_baud = parseInt(baud_picker.val());
connection_delay = parseInt(delay_picker.val());

if (selected_port != '0') {
if (clicks) { // odd number of clicks
chrome.serial.close(connectionId, onClosed);

clearTimeout(connection_delay);
clearInterval(serial_poll);

$(this).text('Connect');
$(this).removeClass('active');
} else { // even number of clicks
console.log('Connecting to: ' + selected_port);

chrome.serial.open(selected_port, {
bitrate: selected_baud
}, onOpen);

$(this).text('Disconnect');
$(this).addClass('active');
}

$(this).data("clicks", !clicks);
}
});
});

function onOpen(openInfo) {
connectionId = openInfo.connectionId;

if (connectionId != -1) {
console.log('Connection was opened with ID: ' + connectionId);

connection_delay = setTimeout(function() {
// start polling
serial_poll = setInterval(readPoll, 10);

// should request some sort of configuration data
send_message(MSP_codes.MSP_ACC_CALIBRATION, 205);

}, connection_delay * 1000);
}
}

function onClosed(result) {
if (result) { // All went as expected
connectionId = -1; // reset connection id
console.log('Connection closed successfully.');
} else { // Something went wrong
if (connectionId > 0) {
console.log('There was an error that happened during "connection-close" procedure.');
}
}
}

function readPoll() {
chrome.serial.read(connectionId, 128, onCharRead);
}

var message_state = 0;
var message_status = 1;
var message_code = 0;
var message_length_expected = 0;
var message_length_received = 0;
var message_buffer;
var message_buffer_uint8_view;

function onCharRead(readInfo) {
if (readInfo && readInfo.bytesRead > 0 && readInfo.data) {
var data = new Uint8Array(readInfo.data);

for (var i = 0; i < data.length; i++) {
switch (message_state) {
case 0: // sync char 1
if (data[i] == 36) { // $
message_state++;
}
break;
case 1: // sync char 2
if (data[i] == 77) { // M
message_state++;
} else { // restart and try again
message_state = 0;
}
break;
case 2: // direction (should be >)
if (data[i] == 62) { // >
message_status = 1;
} else { // unknown
message_status = 0;
}

message_state++;
break;
case 3:
message_length_expected = data[i]; // data length

// setup arraybuffer
message_buffer = new ArrayBuffer(message_length_expected);
message_buffer_uint8_view = new Uint8Array(message_buffer);

message_state++;
break;
case 4:
message_code = data[i]; // code

message_state++;
break;
case 5: // data / payload
message_buffer_uint8_view[message_length_received] = data[i];
message_length_received++;

if (message_length_received >= message_length_expected) {
// message received, process
console.log(message_code);
console.log(message_buffer_uint8_view);

// Reset variables
message_length_received = 0;
message_state = 0;
}
break;
}
}
}
}

var MSP_codes = {
MSP_IDENT: 100,
MSP_STATUS: 101,
MSP_RAW_IMU: 102,
MSP_SERVO: 103,
MSP_MOTOR: 104,
MSP_RC: 105,
MSP_RAW_GPS: 106,
MSP_COMP_GPS: 107,
MSP_ATTITUDE: 108,
MSP_ALTITUDE: 109,
MSP_BAT: 110,
MSP_RC_TUNING: 111,
MSP_PID: 112,
MSP_BOX: 113,
MSP_MISC: 114,
MSP_MOTOR_PINS: 115,
MSP_BOXNAMES: 116,
MSP_PIDNAMES: 117,

MSP_SET_RAW_RC: 200,
MSP_SET_RAW_GPS: 201,
MSP_SET_PID: 202,
MSP_SET_BOX: 203,
MSP_SET_RC_TUNING: 204,
MSP_ACC_CALIBRATION: 205,
MSP_MAG_CALIBRATION: 206,
MSP_SET_MISC: 207,
MSP_RESET_CONF: 208,
MSP_SELECT_SETTING: 210,

MSP_BIND: 240,

MSP_EEPROM_WRITE: 250,

MSP_DEBUGMSG: 253,
MSP_DEBUG: 254
};

function send_message(code, data, bytes_n) {
if (typeof data === 'array') { // array portion of this code is untested
var size = 6 + data.length;
var checksum = 0;

var bufferOut = new ArrayBuffer(size);
var bufView = new Uint8Array(bufferOut);

bufView[0] = 36; // $
bufView[1] = 77; // M
bufView[2] = 60; // <
bufView[3] = data.length; // data length
bufView[4] = code; // code

checksum = bufView[3] | bufView[4];
for (var i = 5; i < data.length; i++) {
bufView[i] = data[i - 5];

checksum |= bufView[i];
}

bufView[5 + data.length] = checksum;
} else {
var bufferOut = new ArrayBuffer(7);
var bufView = new Uint8Array(bufferOut);

bufView[0] = 36; // $
bufView[1] = 77; // M
bufView[2] = 60; // <
bufView[3] = bytes_n; // data length
bufView[4] = code; // code
bufView[5] = data; // data
bufView[6] = bufView[3] | bufView[4] | bufView[5]; // checksum
}

chrome.serial.write(connectionId, bufferOut, function(writeInfo) {
console.log("Wrote: " + writeInfo.bytesWritten + " bytes");
});
}
Loading

0 comments on commit be66960

Please sign in to comment.