Skip to content

Instantly share code, notes, and snippets.

@goebish
Last active August 29, 2015 14:17
Show Gist options
  • Save goebish/6c3853ef76ca0a1d4f02 to your computer and use it in GitHub Desktop.
Save goebish/6c3853ef76ca0a1d4f02 to your computer and use it in GitHub Desktop.
// hubsan x4 tx frame decoder
// input: data.txt = saleae logic spi decoder exported output.
// output: dataout.txt = transmission data frames
#include <QtCore/QCoreApplication>
#include <qfile.h>
#include <qstringlist.h>
/*
Ex1: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
Ex2: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp
cc : throttle observed range: 0x00 - 0xff (smaller is down)
ee : rudder observed range: 0x34 - 0xcc (smaller is right)
gg : elevator observed range: 0x3e - 0xbc (smaller is up)
ii : aileron observed range: 0x45 - 0xc3 (smaller is right)
llmmnnoo: Transmiter ID(?)
pp : checksum
Checksums:
The checksum is calculated as 256 - ((sum of the 1st 15 bytes) modulo 256)
*/
QString toHex(const char byte)
{
QString hex;
if( byte<16) hex = "0";
hex += QString::number(byte,16);
return hex;
}
int main(int argc, char *argv[])
{
// 1st phase, convert text output to data binary file
QFile in("E:\\Users\\goebish\\Desktop\\data.txt");
QFile::remove("E:\\Users\\goebish\\Desktop\\data.bin");
QFile out("E:\\Users\\goebish\\Desktop\\data.bin");
in.open(QIODevice::ReadOnly);
out.open(QIODevice::WriteOnly);
QString line = in.readLine(); // skip first line
while(!in.atEnd())
{
line = in.readLine();
QString data = line.split("0x")[1].left(2);
bool ok;
unsigned char byte = data.toInt(&ok,16);
out.write((const char*)&byte,1);
}
out.close();
in.close();
// 2nd phase, detect data transmission packets and dump them to text file
out.open(QIODevice::ReadOnly);
QByteArray buffer = out.readAll();
out.close();
QFile::remove("E:\\Users\\goebish\\Desktop\\dataout.txt");
QFile out2("E:\\Users\\goebish\\Desktop\\dataout.txt");
out2.open(QIODevice::WriteOnly);
int pos=0;
int line_size = 21; // 16
int offset = 0;
while(pos<buffer.size()-line_size)
{
/*if(pos%16==0) out2.write("\n ");
unsigned char data = buffer.at(pos);
QString hex = QString::number(data,16)+' ';
if(data < 16)
out2.write("0");
out2.write(hex.toAscii());*/
if( buffer.at(pos) == (char)0x05 // start of TX packet ?
/*||
buffer.at(pos) == (char)0x42 // start of RX packet ? */
)
{
if(buffer.at(pos) == (char)0x05) // A0
{
offset = 1;
//out2.write( toHex(buffer.at(pos+1)).toAscii()+' ');
//out2.write( toHex(buffer.at(pos+2)).toAscii()+' ');
//out2.write( toHex(buffer.at(pos+3)).toAscii()+'\n');
out2.write("\nTx: ");
}
/*else if(buffer.at(pos) == (char)0x42) // 18
{
offset = 1;
out2.write( toHex(buffer.at(pos+1)).toAscii()+' ');
out2.write( toHex(buffer.at(pos+2)).toAscii()+'\n');
out2.write("\nRx: ");
}*/
int pos2 = pos+offset; // start of data;
int dumped=0;
while(pos2 < buffer.size() && dumped < line_size)
{
unsigned char data = buffer.at(pos2);
QString hex = QString::number(data,16)+' ';
if(data < 16)
out2.write("0");
out2.write(hex.toUpper().toAscii());
pos2++;
dumped++;
pos=pos2;
}
}
pos++;
}
out2.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment