aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcnlohr <lohr85@gmail.com>2017-02-16 14:44:54 -0500
committercnlohr <lohr85@gmail.com>2017-02-16 14:44:54 -0500
commitf2d014016d73f067c224ee222bc3eab910848914 (patch)
treee98df6e5d4e85f8bd35c184b0136968c7e07af90
parentf782146df94b3b54965c2aed696d49e86870046d (diff)
parent05ff11b162145e327ba5839f77be09df5289f4d1 (diff)
downloadlibsurvive-f2d014016d73f067c224ee222bc3eab910848914.tar.gz
libsurvive-f2d014016d73f067c224ee222bc3eab910848914.tar.bz2
Merge branch 'axlecrusher-master'
-rw-r--r--Makefile9
-rw-r--r--calibrate.c124
-rw-r--r--data_recorder.c4
-rw-r--r--include/survive.h23
-rw-r--r--redist/crc32.c104
-rw-r--r--redist/crc32.h17
-rw-r--r--src/ootx_decoder.c287
-rw-r--r--src/ootx_decoder.h72
-rw-r--r--src/survive_cal.c72
-rw-r--r--src/survive_cal.h44
-rw-r--r--src/survive_data.c12
-rw-r--r--src/survive_internal.h26
-rw-r--r--src/survive_process.c15
-rw-r--r--src/survive_usb.c6
-rw-r--r--tools/ootx_decode/HMD_Datagen.c103
-rw-r--r--tools/ootx_decode/Makefile7
-rw-r--r--tools/ootx_decode/ootx_decode.c121
17 files changed, 1027 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index dbd4869..797c257 100644
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,23 @@
-all : lib data_recorder test
+all : lib data_recorder test calibrate
CFLAGS:=-Iinclude -fPIC -g -Os -Iredist -flto
LDFLAGS:=-lpthread -lusb-1.0 -lz -lX11 -flto
+# unused: redist/crc32.c
+
test : test.c lib/libsurvive.so redist/os_generic.o
gcc -o $@ $^ $(LDFLAGS) $(CFLAGS)
data_recorder : data_recorder.c lib/libsurvive.so redist/os_generic.o redist/DrawFunctions.o redist/XDriver.o
gcc -o $@ $^ $(LDFLAGS) $(CFLAGS)
+calibrate : calibrate.c lib/libsurvive.so redist/os_generic.c redist/DrawFunctions.c redist/XDriver.c
+ gcc -o $@ $^ $(LDFLAGS) $(CFLAGS)
+
lib:
mkdir lib
-lib/libsurvive.so : src/survive.o src/survive_usb.o src/survive_data.o src/survive_process.o redist/jsmn.o $(DEBUGSTUFF)
+lib/libsurvive.so : src/survive.o src/survive_usb.o src/survive_data.o src/survive_process.o redist/jsmn.o src/survive_cal.o src/ootx_decoder.o $(DEBUGSTUFF)
gcc -o $@ $^ $(LDFLAGS) -shared
clean :
diff --git a/calibrate.c b/calibrate.c
new file mode 100644
index 0000000..4db2ed0
--- /dev/null
+++ b/calibrate.c
@@ -0,0 +1,124 @@
+//Data recorder mod with GUI showing light positions.
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <survive.h>
+#include <string.h>
+#include <os_generic.h>
+#include <DrawFunctions.h>
+
+struct SurviveContext * ctx;
+
+void HandleKey( int keycode, int bDown )
+{
+ if( !bDown ) return;
+
+ if( keycode == 'O' || keycode == 'o' )
+ {
+ survive_usb_send_magic(ctx,1);
+ }
+ if( keycode == 'F' || keycode == 'f' )
+ {
+ survive_usb_send_magic(ctx,0);
+ }
+}
+
+void HandleButton( int x, int y, int button, int bDown )
+{
+}
+
+void HandleMotion( int x, int y, int mask )
+{
+}
+
+int bufferpts[32*2*3];
+char buffermts[32*128*3];
+int buffertimeto[32*3];
+
+void my_light_process( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length )
+{
+ survive_default_light_process( so, sensor_id, acode, timeinsweep, timecode, length );
+}
+
+void my_imu_process( struct SurviveObject * so, int16_t * accelgyro, uint32_t timecode, int id )
+{
+ survive_default_imu_process( so, accelgyro, timecode, id );
+
+return;
+ //if( so->codename[0] == 'H' )
+ if( 1 )
+ {
+ printf( "I %s %d %d %d %d %d %d %d %d\n", so->codename, timecode, accelgyro[0], accelgyro[1], accelgyro[2], accelgyro[3], accelgyro[4], accelgyro[5], id );
+ }
+}
+
+
+
+
+void * GuiThread( void * v )
+{
+ short screenx, screeny;
+ while(1)
+ {
+ CNFGHandleInput();
+ CNFGClearFrame();
+ CNFGColor( 0xFFFFFF );
+ CNFGGetDimensions( &screenx, &screeny );
+
+ int i;
+ for( i = 0; i < 32*3; i++ )
+ {
+ if( buffertimeto[i] < 50 )
+ {
+ uint32_t color = i * 3231349;
+ uint8_t r = color & 0xff;
+ uint8_t g = (color>>8) & 0xff;
+ uint8_t b = (color>>16) & 0xff;
+ r = (r * (5-buffertimeto[i])) / 5 ;
+ g = (g * (5-buffertimeto[i])) / 5 ;
+ b = (b * (5-buffertimeto[i])) / 5 ;
+ CNFGColor( (b<<16) | (g<<8) | r );
+ CNFGTackRectangle( bufferpts[i*2+0], bufferpts[i*2+1], bufferpts[i*2+0] + 5, bufferpts[i*2+1] + 5 );
+ CNFGPenX = bufferpts[i*2+0]; CNFGPenY = bufferpts[i*2+1];
+ CNFGDrawText( buffermts, 2 );
+ buffertimeto[i]++;
+ }
+ }
+
+
+ CNFGSwapBuffers();
+ OGUSleep( 10000 );
+ }
+}
+
+
+
+int main()
+{
+ ctx = survive_init( );
+
+ survive_install_light_fn( ctx, my_light_process );
+ survive_install_imu_fn( ctx, my_imu_process );
+
+ survive_cal_install( ctx );
+
+ CNFGBGColor = 0x000000;
+ CNFGDialogColor = 0x444444;
+ CNFGSetup( "Survive GUI Debug", 640, 480 );
+ OGCreateThread( GuiThread, 0 );
+
+
+ if( !ctx )
+ {
+ fprintf( stderr, "Fatal. Could not start\n" );
+ return 1;
+ }
+
+ while(survive_poll(ctx) == 0)
+ {
+ //Do stuff.
+ }
+}
+
diff --git a/data_recorder.c b/data_recorder.c
index 4c5627f..ced82c4 100644
--- a/data_recorder.c
+++ b/data_recorder.c
@@ -39,6 +39,8 @@ int buffertimeto[32*3];
void my_light_process( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length )
{
+ survive_default_light_process( so, sensor_id, acode, timeinsweep, timecode, length );
+
if( acode == -1 ) return;
//return;
int jumpoffset = sensor_id;
@@ -77,6 +79,8 @@ void my_light_process( struct SurviveObject * so, int sensor_id, int acode, int
void my_imu_process( struct SurviveObject * so, int16_t * accelgyro, uint32_t timecode, int id )
{
+ survive_default_imu_process( so, accelgyro, timecode, id );
+
return;
//if( so->codename[0] == 'H' )
if( 1 )
diff --git a/include/survive.h b/include/survive.h
index d2f5bfe..4a95a2f 100644
--- a/include/survive.h
+++ b/include/survive.h
@@ -10,6 +10,12 @@ struct SurviveContext;
//DANGER: This structure may be redefined. Note that it is logically split into 64-bit chunks
//for optimization on 32- and 64-bit systems.
+
+//Careful with this, you can't just add another one right now, would take minor changes in survive_data.c and the cal tools.
+//It will also require a recompile. TODO: revisit this and correct the comment once fixed.
+#define NUM_LIGHTHOUSES 2
+
+
struct SurviveObject
{
struct SurviveContext * ctx;
@@ -32,10 +38,10 @@ struct SurviveObject
//Flood info, for calculating which laser is currently sweeping.
int8_t oldcode;
- uint32_t last_time[2]; //0 = master, 1 = slave. Hardcoded, because it cannot simply be expanded.
- uint32_t last_length[2];
- int8_t sync_set_number; //0 = master, 1 = slave, -1 = fault. Possibly more lighthouses???
- int8_t did_handle_ootx;
+ uint32_t last_time[NUM_LIGHTHOUSES];
+ uint32_t last_length[NUM_LIGHTHOUSES];
+ int8_t sync_set_number; //0 = master, 1 = slave, -1 = fault.
+ int8_t did_handle_ootx; //If unset, will send lightcap data for sync pulses next time a sensor is hit.
uint32_t recent_sync_time;
uint32_t last_lighttime; //May be a 24- or 32- bit number depending on what device.
@@ -64,8 +70,15 @@ struct SurviveObject * survive_get_so_by_name( struct SurviveContext * ctx, cons
//Utilitiy functions.
int survive_simple_inflate( struct SurviveContext * ctx, const char * input, int inlen, char * output, int outlen );
-//TODO: Need to make this do haptic responses for hands.
+//TODO: Need to make this do haptic responses for hands.
int survive_usb_send_magic( struct SurviveContext * ctx, int on );
+//Install the calibrator.
+void survive_cal_install( struct SurviveContext * ctx );
+
+//Call these from your callback if overridden.
+void survive_default_light_process( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length );
+void survive_default_imu_process( struct SurviveObject * so, int16_t * accelgyro, uint32_t timecode, int id );
+
#endif
diff --git a/redist/crc32.c b/redist/crc32.c
new file mode 100644
index 0000000..834e7f2
--- /dev/null
+++ b/redist/crc32.c
@@ -0,0 +1,104 @@
+/*-
+ * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
+ * code or tables extracted from it, as desired without restriction.
+ *
+ * First, the polynomial itself and its table of feedback terms. The
+ * polynomial is
+ * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
+ *
+ * Note that we take it "backwards" and put the highest-order term in
+ * the lowest-order bit. The X^32 term is "implied"; the LSB is the
+ * X^31 term, etc. The X^0 term (usually shown as "+1") results in
+ * the MSB being 1
+ *
+ * Note that the usual hardware shift register implementation, which
+ * is what we're using (we're merely optimizing it by doing eight-bit
+ * chunks at a time) shifts bits into the lowest-order term. In our
+ * implementation, that means shifting towards the right. Why do we
+ * do it this way? Because the calculated CRC must be transmitted in
+ * order from highest-order term to lowest-order term. UARTs transmit
+ * characters in order from LSB to MSB. By storing the CRC this way
+ * we hand it to the UART in the order low-byte to high-byte; the UART
+ * sends each low-bit to hight-bit; and the result is transmission bit
+ * by bit from highest- to lowest-order term without requiring any bit
+ * shuffling on our part. Reception works similarly
+ *
+ * The feedback terms table consists of 256, 32-bit entries. Notes
+ *
+ * The table can be generated at runtime if desired; code to do so
+ * is shown later. It might not be obvious, but the feedback
+ * terms simply represent the results of eight shift/xor opera
+ * tions for all combinations of data and CRC register values
+ *
+ * The values must be right-shifted by eight bits by the "updcrc
+ * logic; the shift must be unsigned (bring in zeroes). On some
+ * hardware you could probably optimize the shift in assembler by
+ * using byte-swap instructions
+ * polynomial $edb88320
+ *
+ *
+ * CRC32 code derived from work by Gary S. Brown.
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+
+static uint32_t crc32_tab[] = {
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+ 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+ 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+ 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+ 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+ 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+ 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+ 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+ 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+ 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+ 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+ 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+ 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+ 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+ 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+ 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+ 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+ 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+ 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
+};
+
+uint32_t crc32(uint32_t crc, const void *buf, size_t size)
+{
+ const uint8_t *p;
+
+ p = buf;
+ crc = crc ^ ~0U;
+
+ while (size--)
+ crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+
+ return crc ^ ~0U;
+}
+
diff --git a/redist/crc32.h b/redist/crc32.h
new file mode 100644
index 0000000..5a8d16a
--- /dev/null
+++ b/redist/crc32.h
@@ -0,0 +1,17 @@
+// (C) 2016 Joshua Allen, MIT/x11 License.
+//
+//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
+//crc32.c under liberal license.
+//
+// You should only include this file if you are going to include the calibration subsystem of libsurvive.
+
+#ifndef CRC32_H
+#define CRC32_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+uint32_t crc32(uint32_t crc, uint8_t *buf, size_t size);
+
+#endif
+
diff --git a/src/ootx_decoder.c b/src/ootx_decoder.c
new file mode 100644
index 0000000..8ec16f2
--- /dev/null
+++ b/src/ootx_decoder.c
@@ -0,0 +1,287 @@
+// (C) 2017 Joshua Allen, MIT/x11 License.
+//
+//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
+
+/* ootx data decoder */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <zlib.h>
+#include <assert.h>
+#include "ootx_decoder.h"
+//#include "crc32.h"
+
+//char* fmt_str = "L Y HMD %d 5 1 206230 %d\n";
+
+#define MAX_BUFF_SIZE 64
+
+void (*ootx_packet_clbk)(ootx_decoder_context * ctx, ootx_packet* packet) = NULL;
+void (*ootx_bad_crc_clbk)(ootx_decoder_context * ctx, ootx_packet* packet, uint32_t crc) = NULL;
+
+void ootx_pump_bit(ootx_decoder_context *ctx, uint8_t dbit);
+
+void ootx_init_decoder_context(ootx_decoder_context *ctx) {
+ ctx->buf_offset = 0;
+ ctx->bits_written = 0;
+
+ ctx->preamble = 0XFFFFFFFF;
+ ctx->bits_processed = 0;
+ ctx->found_preamble = 0;
+
+ ctx->buffer = (uint8_t*)malloc(MAX_BUFF_SIZE);
+ ctx->payload_size = (uint16_t*)ctx->buffer;
+ *(ctx->payload_size) = 0;
+}
+
+void ootx_free_decoder_context(ootx_decoder_context *ctx) {
+ free(ctx->buffer);
+ ctx->buffer = NULL;
+ ctx->payload_size = NULL;
+}
+
+uint8_t ootx_decode_bit(uint32_t length) {
+ uint8_t t = (length - 2750) / 500; //why 2750?
+// return ((t & 0x02)>0)?0xFF:0x00; //easier if we need to bitshift right
+ return ((t & 0x02)>>1);
+}
+
+uint8_t ootx_detect_preamble(ootx_decoder_context *ctx, uint8_t dbit) {
+ ctx->preamble <<= 1;
+// ctx->preamble |= (0x01 & dbit);
+ ctx->preamble |= dbit;
+ if ((ctx->preamble & 0x0003ffff) == 0x00000001) return 1;
+ return 0;
+}
+
+void ootx_reset_buffer(ootx_decoder_context *ctx) {
+ ctx->buf_offset = 0;
+ ctx->buffer[0] = 0;
+ ctx->bits_written = 0;
+ *(ctx->payload_size) = 0;
+}
+
+void ootx_inc_buffer_offset(ootx_decoder_context *ctx) {
+ ++(ctx->buf_offset);
+
+// assert(ctx->buf_offset<MAX_BUFF_SIZE);
+
+ /* the buffer is going to overflow, wrap the buffer and don't write more data until the preamble is found again */
+ if(ctx->buf_offset>=MAX_BUFF_SIZE) {
+ ctx->buf_offset = 0;
+ ctx->found_preamble = 0;
+ }
+
+ ctx->buffer[ctx->buf_offset] = 0;
+}
+
+void ootx_write_to_buffer(ootx_decoder_context *ctx, uint8_t dbit) {
+ uint8_t *current_byte = ctx->buffer + ctx->buf_offset;
+
+ *current_byte <<= 1;
+// *current_byte |= (0x01 & dbit);
+ *current_byte |= dbit;
+
+ ++(ctx->bits_written);
+ if (ctx->bits_written>7) {
+ ctx->bits_written=0;
+// printf("%d\n", *current_byte);
+ ootx_inc_buffer_offset(ctx);
+ }
+}
+
+uint8_t ootx_process_bit(ootx_decoder_context *ctx, uint32_t length) {
+ uint8_t dbit = ootx_decode_bit(length);
+ ootx_pump_bit( ctx, dbit );
+ return dbit;
+}
+
+void ootx_pump_bit(ootx_decoder_context *ctx, uint8_t dbit) {
+// uint8_t dbit = ootx_decode_bit(length);
+ ++(ctx->bits_processed);
+
+ if ( ootx_detect_preamble(ctx, dbit) ) {
+ /* data stream can start over at any time so we must
+ always look for preamble bits */
+ //printf("Preamble found\n");
+ ootx_reset_buffer(ctx);
+ ctx->bits_processed = 0;
+ ctx->found_preamble = 1;
+ }
+ else if(ctx->bits_processed>16) {
+ //every 17th bit needs to be dropped (sync bit)
+// printf("drop %d\n", dbit);
+ if( !dbit )
+ {
+ printf("Bad sync bit\n");
+ ootx_reset_buffer(ctx);
+ }
+ ctx->bits_processed = 0;
+ }
+ else if (ctx->found_preamble > 0)
+ {
+ /* only write to buffer if the preamble is found.
+ if the buffer overflows, found_preamble will be cleared
+ and writing will stop. data would be corrupted, so there is no point in continuing
+ */
+
+ ootx_write_to_buffer(ctx, dbit);
+
+ uint16_t padded_length = *(ctx->payload_size);
+ padded_length += (padded_length&0x01); //extra null byte if odd
+
+/* int k;
+ printf( ":" );
+ for( k = 0; k < 36; k++ )
+ {
+ printf( "%02x ", ctx->buffer[k] );
+ }
+ printf( "\n" );*/
+
+ if (ctx->buf_offset >= (padded_length+6)) {
+ /* once we have a complete ootx packet, send it out in the callback */
+ ootx_packet op;
+
+ op.length = *(ctx->payload_size);
+ op.data = ctx->buffer+2;
+ op.crc32 = *(uint32_t*)(op.data+padded_length);
+
+ uint32_t crc = crc32( 0L, Z_NULL, 0 );
+ crc = crc32( crc, op.data,op.length);
+
+ if (crc != op.crc32) {
+ if (ootx_bad_crc_clbk != NULL) ootx_bad_crc_clbk(ctx, &op,crc);
+ }
+ else if (ootx_packet_clbk != NULL) {
+ ootx_packet_clbk(ctx,&op);
+ }
+
+ ootx_reset_buffer(ctx);
+ }
+ }
+}
+
+uint8_t* get_ptr(uint8_t* data, uint8_t bytes, uint16_t* idx) {
+ uint8_t* x = data + *idx;
+ *idx += bytes;
+ return x;
+}
+
+/* simply doing:
+float f = 0;
+uint32_t *ftmp = (uint32_t*)&f; //use the allocated floating point memory
+This can cause problem when strict aliasing (-O2) is used.
+Reads and writes to f and ftmp would be considered independent and could be
+be reordered by the compiler. A union solves that problem.
+*/
+union iFloat {
+ uint32_t i;
+ float f;
+};
+
+float _half_to_float(uint8_t* data) {
+ uint16_t x = *(uint16_t*)data;
+ union iFloat fnum;
+ fnum.f = 0;
+
+ //sign
+ fnum.i = (x & 0x8000)<<16;
+
+ if ((x & 0x7FFF) == 0) return fnum.f; //signed zero
+
+ if ((x & 0x7c00) == 0) {
+ //denormalized
+ x = (x&0x3ff)<<1; //only mantissa, advance intrinsic bit forward
+ uint8_t e = 0;
+ //shift until intrinsic bit of mantissa overflows into exponent
+ //increment exponent each time
+ while ((x&0x0400) == 0) {
+ x<<=1;
+ e++;
+ }
+ fnum.i |= ((uint32_t)(112-e))<<23; //bias exponent to 127, half floats are biased 15 so only need to go 112 more.
+ fnum.i |= ((uint32_t)(x&0x3ff))<<13; //insert mantissa
+ return fnum.f;
+ }
+
+ if((x&0x7c00) == 0x7c00) {
+ //for infinity, fraction is 0
+ //for NaN, fraction is anything non zero
+ //we could just copy in bits and not shift, but the mantissa of a NaN can have meaning
+ fnum.i |= 0x7f800000 | ((uint32_t)(x & 0x3ff))<<13;
+ return fnum.f;
+ }
+
+ fnum.i |= ((((uint32_t)(x & 0x7fff)) + 0x1c000u) << 13);
+
+ return fnum.f;
+}
+
+void init_lighthouse_info_v6(lighthouse_info_v6* lhi, uint8_t* data) {
+ uint16_t idx = 0;
+ /*
+ uint16_t fw_version;//Firmware version (bit 15..6), protocol version (bit 5..0)
+ uint32_t id; //Unique identifier of the base station
+ float fcal_0_phase; //"phase" for rotor 0
+ float fcal_1_phase; //"phase" for rotor 1
+ float fcal_0_tilt; //"tilt" for rotor 0
+ float fcal_1_tilt; //"tilt" for rotor 1
+ uint8_t sys_unlock_count; //Lowest 8 bits of the rotor desynchronization counter
+ uint8_t hw_version; //Hardware version
+ float fcal_0_curve; //"curve" for rotor 0
+ float fcal_1_curve; //"curve" for rotor 1
+ int8_t accel_dir_x; //"orientation vector"
+ int8_t accel_dir_y; //"orientation vector"
+ int8_t accel_dir_z; //"orientation vector"
+ float fcal_0_gibphase; //"gibbous phase" for rotor 0 (normalized angle)
+ float fcal_1_gibphase; //"gibbous phase" for rotor 1 (normalized angle)
+ float fcal_0_gibmag; //"gibbous magnitude" for rotor 0
+ float fcal_1_gibmag; //"gibbous magnitude" for rotor 1
+ uint8_t mode_current; //Currently selected mode (default: 0=A, 1=B, 2=C)
+ uint8_t sys_faults; //"fault detect flags" (should be 0)
+ */
+
+ lhi->fw_version = *(uint16_t*)get_ptr(data,sizeof(uint16_t),&idx);
+ lhi->id = *(uint32_t*)get_ptr(data,sizeof(uint32_t),&idx);
+ lhi->fcal_0_phase = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_1_phase = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_0_tilt = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_1_tilt = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->sys_unlock_count = *get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->hw_version = *get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->fcal_0_curve = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_1_curve = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->accel_dir_x = *(int8_t*)get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->accel_dir_y = *(int8_t*)get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->accel_dir_z = *(int8_t*)get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->fcal_0_gibphase = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_1_gibphase = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_0_gibmag = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->fcal_1_gibmag = _half_to_float( get_ptr(data,sizeof(uint16_t),&idx) );
+ lhi->mode_current = *get_ptr(data,sizeof(uint8_t),&idx);
+ lhi->sys_faults = *get_ptr(data,sizeof(uint8_t),&idx);
+
+}
+
+void print_lighthouse_info_v6(lighthouse_info_v6* lhi) {
+
+ printf("\t%X\n\t%X\n\t%f\n\t%f\n\t%f\n\t%f\n\t%d\n\t%d\n\t%f\n\t%f\n\t%d\n\t%d\n\t%d\n\t%f\n\t%f\n\t%f\n\t%f\n\t%d\n\t%d\n",
+ lhi->fw_version,
+ lhi->id,
+ lhi->fcal_0_phase,
+ lhi->fcal_1_phase,
+ lhi->fcal_0_tilt,
+ lhi->fcal_1_tilt,
+ lhi->sys_unlock_count,
+ lhi->hw_version,
+ lhi->fcal_0_curve,
+ lhi->fcal_1_curve,
+ lhi->accel_dir_x,
+ lhi->accel_dir_y,
+ lhi->accel_dir_z,
+ lhi->fcal_0_gibphase,
+ lhi->fcal_1_gibphase,
+ lhi->fcal_0_gibmag,
+ lhi->fcal_1_gibmag,
+ lhi->mode_current,
+ lhi->sys_faults);
+}
diff --git a/src/ootx_decoder.h b/src/ootx_decoder.h
new file mode 100644
index 0000000..8ddf527
--- /dev/null
+++ b/src/ootx_decoder.h
@@ -0,0 +1,72 @@
+// (C) 2017 Joshua Allen, MIT/x11 License.
+//
+//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
+
+#ifndef OOTX_DECODER_H
+#define OOTX_DECODER_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ uint16_t length;
+ uint8_t* data;
+ uint32_t crc32;
+} ootx_packet;
+
+typedef struct {
+ uint8_t* buffer;
+ uint16_t buf_offset;
+ uint8_t bits_written;
+ uint16_t* payload_size;
+
+ uint32_t preamble;
+ uint8_t bits_processed;
+ uint8_t found_preamble;
+
+ uint8_t bit_count[2];
+
+ void * user;
+ int user1;
+} ootx_decoder_context;
+
+
+typedef float float16;
+
+typedef struct {
+ uint16_t fw_version;//Firmware version (bit 15..6), protocol version (bit 5..0)
+ uint32_t id; //Unique identifier of the base station
+ float16 fcal_0_phase; //"phase" for rotor 0
+ float16 fcal_1_phase; //"phase" for rotor 1
+ float16 fcal_0_tilt; //"tilt" for rotor 0
+ float16 fcal_1_tilt; //"tilt" for rotor 1
+ uint8_t sys_unlock_count; //Lowest 8 bits of the rotor desynchronization counter
+ uint8_t hw_version; //Hardware version
+ float16 fcal_0_curve; //"curve" for rotor 0
+ float16 fcal_1_curve; //"curve" for rotor 1
+ int8_t accel_dir_x; //"orientation vector"
+ int8_t accel_dir_y; //"orientation vector"
+ int8_t accel_dir_z; //"orientation vector"
+ float16 fcal_0_gibphase; //"gibbous phase" for rotor 0 (normalized angle)
+ float16 fcal_1_gibphase; //"gibbous phase" for rotor 1 (normalized angle)
+ float16 fcal_0_gibmag; //"gibbous magnitude" for rotor 0
+ float16 fcal_1_gibmag; //"gibbous magnitude" for rotor 1
+ uint8_t mode_current; //Currently selected mode (default: 0=A, 1=B, 2=C)
+ uint8_t sys_faults; //"fault detect flags" (should be 0)
+} lighthouse_info_v6;
+
+void init_lighthouse_info_v6(lighthouse_info_v6* lhi, uint8_t* data);
+void print_lighthouse_info_v6(lighthouse_info_v6* lhi);
+
+void ootx_init_decoder_context(ootx_decoder_context *ctx);
+void ootx_free_decoder_context(ootx_decoder_context *ctx);
+
+uint8_t ootx_process_bit(ootx_decoder_context *ctx, uint32_t length);
+void ootx_pump_bit(ootx_decoder_context *ctx, uint8_t dbit);
+
+uint8_t ootx_decode_bit(uint32_t length);
+
+extern void (*ootx_packet_clbk)(ootx_decoder_context *ctx, ootx_packet* packet);
+extern void (*ootx_bad_crc_clbk)(ootx_decoder_context *ctx, ootx_packet* packet, uint32_t crc);
+
+#endif
diff --git a/src/survive_cal.c b/src/survive_cal.c
new file mode 100644
index 0000000..adcb7bc
--- /dev/null
+++ b/src/survive_cal.c
@@ -0,0 +1,72 @@
+// (C) 2016, 2017 Joshua Allen, MIT/x11 License.
+// (C) 2016, 2017 <>< C. N. Lohr, Under MIT/x11 License.
+
+// All OOTX code was written by J. Allen. Rest of the code is probably mostly CNLohr.
+
+#include "survive_cal.h"
+#include "survive_internal.h"
+
+void ootx_packet_clbk_d(ootx_decoder_context *ct, ootx_packet* packet)
+{
+ struct SurviveContext * ctx = (struct SurviveContext*)(ct->user);
+ struct SurviveCalData * cd = ctx->calptr;
+ int id = ct->user1;
+
+ printf( "Got OOTX packet %d %p\n", id, cd );
+
+ lighthouse_info_v6 v6;
+ init_lighthouse_info_v6(&v6, packet->data);
+ print_lighthouse_info_v6(&v6);
+}
+
+
+void survive_cal_install( struct SurviveContext * ctx )
+{
+ int i;
+ struct SurviveCalData * cd = ctx->calptr = calloc( 1, sizeof( struct SurviveCalData ) );
+
+ for( i = 0; i < NUM_LIGHTHOUSES; i++ )
+ {
+ ootx_init_decoder_context(&cd->ootx_decoders[i]);
+ cd->ootx_decoders[i].user = ctx;
+ cd->ootx_decoders[i].user1 = i;
+ }
+
+ cd->stage = 1;
+
+ ootx_packet_clbk = ootx_packet_clbk_d;
+
+ ctx->calptr = cd;
+}
+
+
+void survive_cal_light( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length )
+{
+ struct SurviveContext * ctx = so->ctx;
+ struct SurviveCalData * cd = ctx->calptr;
+
+ if( !cd ) return;
+
+ switch( cd->stage )
+ {
+ default:
+ case 0: //Default, inactive.
+ break;
+
+ case 1:
+ //Collecting OOTX data.
+ if( sensor_id < 0 )
+ {
+ int lhid = -sensor_id-1;
+ if( lhid < NUM_LIGHTHOUSES && so->codename[0] == 'H' )
+ {
+ uint8_t dbit = (acode & 2)>>1;
+ //printf( "%s %d %d %d\n", so->codename, lhid, acode, dbit );
+ ootx_pump_bit( &cd->ootx_decoders[lhid], dbit );
+ }
+ }
+
+ break;
+ }
+}
+
diff --git a/src/survive_cal.h b/src/survive_cal.h
new file mode 100644
index 0000000..a74bf95
--- /dev/null
+++ b/src/survive_cal.h
@@ -0,0 +1,44 @@
+// (C) 2016, 2017 <>< C. N. Lohr, Under MIT/x11 License.
+
+// All OOTX code was written by J. Allen. Rest of the code is probably mostly CNLohr.
+
+#ifndef _SURVIVE_CAL_H
+#define _SURVIVE_CAL_H
+
+//This is a file that is intended for use with capturing vive data during the
+//setup phase. This and survive_cal.c/.h should not be included on embedded
+//uses of libsurvive.
+
+//This file handles the following:
+// 1: Decoding the OOTX data from the lighthouses.
+// 2: Setting OOTX props in the survive context.
+// 3: Collect a bunch of data with the vive pointed up and the watchment to either side.
+// 4: Running the code to find the lighthouses.
+// 5: Setting the information needed to develop the worldspace model in the SurviveContext.
+
+
+#include <stdint.h>
+#include "ootx_decoder.h"
+#include "survive_internal.h"
+
+void survive_cal_install( struct SurviveContext * ctx );
+int survive_cal_get_status( struct SurviveContext * ctx, char * description, int max_data );
+
+//void survive_cal_teardown( struct SurviveContext * ctx );
+
+//Called from survive_default_light_process
+void survive_cal_light( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length );
+
+struct SurviveCalData
+{
+ //Stage:
+ // 0: Idle
+ // 1: Collecting OOTX data.
+ int stage;
+
+ //OOTX Data is sync'd off of
+ ootx_decoder_context ootx_decoders[NUM_LIGHTHOUSES];
+};
+
+#endif
+
diff --git a/src/survive_data.c b/src/survive_data.c
index b79f96e..c557951 100644
--- a/src/survive_data.c
+++ b/src/survive_data.c
@@ -81,7 +81,7 @@ static void handle_lightcap( struct SurviveObject * so, struct LightcapElement *
else
{
ssn = ++so->sync_set_number;
- if( so->sync_set_number > 1 )
+ if( so->sync_set_number >= NUM_LIGHTHOUSES )
{
SV_INFO( "Warning. Received an extra, unassociated sync pulse." );
ssn = so->sync_set_number = -1;
@@ -106,12 +106,20 @@ static void handle_lightcap( struct SurviveObject * so, struct LightcapElement *
}
}
}
+
+
+
//See if this is a valid actual pulse.
else if( le->length < 1800 && le->length > 40 && delta > 30000 && ssn >= 0 )
{
int32_t dl = so->last_time[0];
int32_t tpco = so->last_length[0];
+
+#if NUM_LIGHTHOUSES != 2
+ #error You are going to have to fix the code around here to allow for something other than two base stations.
+#endif
+
//Adding length
//Long pulse-code from IR flood.
//Make sure it fits nicely into a divisible-by-500 time.
@@ -128,6 +136,7 @@ static void handle_lightcap( struct SurviveObject * so, struct LightcapElement *
acode_array[0] = (acode_array[0]>>1) - 6;
acode_array[1] = (acode_array[1]>>1) - 6;
+
int acode = acode_array[0];
if( !so->did_handle_ootx )
@@ -135,7 +144,6 @@ static void handle_lightcap( struct SurviveObject * so, struct LightcapElement *
int32_t delta1 = so->last_time[0] - so->recent_sync_time;
int32_t delta2 = so->last_time[1] - so->last_time[0];
- //XXX Axlecrusher -> Add your code here!!!
ctx->lightproc( so, -1, acode_array[0], delta1, so->last_time[0], so->last_length[0] );
ctx->lightproc( so, -2, acode_array[1], delta2, so->last_time[1], so->last_length[1] );
diff --git a/src/survive_internal.h b/src/survive_internal.h
index 446f3c0..11c9d89 100644
--- a/src/survive_internal.h
+++ b/src/survive_internal.h
@@ -62,6 +62,22 @@ struct SurviveUSBInterface
//This is defined in survive.h
struct SurviveObject;
+struct SurviveCalData;
+
+struct BaseStationData
+{
+ uint8_t PositionSet:1;
+ float Position[3];
+ float Quaternion[4];
+
+ uint8_t OOTXSet:1;
+ uint32_t BaseStationID;
+ float fcalphase[2];
+ float fcaltilt[2];
+ float fcalcurve[2];
+ float fcalgibpha[2];
+ float fcalgibmag[2];
+};
struct SurviveContext
{
@@ -75,9 +91,15 @@ struct SurviveContext
light_process_func lightproc;
imu_process_func imuproc;
- //Data Subsystem
+ //Calibration data:
+ struct BaseStationData bsd[NUM_LIGHTHOUSES];
+
+ struct SurviveCalData * calptr; //If and only if the calibration subsystem is attached.
+
+ //Data Subsystem. These should be last, as there may be additional surviveobjects.
struct SurviveObject headset;
- struct SurviveObject watchman[2];
+ struct SurviveObject watchman[2]; //Currently only two supported watchmen.
+
};
diff --git a/src/survive_process.c b/src/survive_process.c
index d3a8c4a..184532b 100644
--- a/src/survive_process.c
+++ b/src/survive_process.c
@@ -1,15 +1,19 @@
//<>< (C) 2016 C. N. Lohr, FULLY Under MIT/x11 License.
//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
-#include "survive_internal.h"
+#include "survive_cal.h"
-
-int bufferpts[32*2];
-char buffermts[32*128];
-int buffertimeto[32];
+//XXX TODO: Once data is avialble in the context, use the stuff here to handle converting from time codes to
+//proper angles, then from there perform the rest of the solution.
void survive_default_light_process( struct SurviveObject * so, int sensor_id, int acode, int timeinsweep, uint32_t timecode, uint32_t length )
{
+
+ if( so->ctx->calptr )
+ {
+ survive_cal_light( so, sensor_id, acode, timeinsweep, timecode, length );
+ }
+
//TODO: Writeme!
}
@@ -18,4 +22,3 @@ void survive_default_imu_process( struct SurviveObject * so, int16_t * accelgyro
//TODO: Writeme!
}
-
diff --git a/src/survive_usb.c b/src/survive_usb.c
index cf12280..aec76db 100644
--- a/src/survive_usb.c
+++ b/src/survive_usb.c
@@ -125,7 +125,8 @@ static inline int getupdate_feature_report(libusb_device_handle* dev, uint16_t i
static inline int hid_get_feature_report_timeout(libusb_device_handle* device, uint16_t interface, unsigned char *buf, size_t len )
{
int ret;
- for (unsigned i = 0; i < 100; i++)
+ uint8_t i = 0;
+ for (i = 0; i < 100; i++)
{
ret = getupdate_feature_report(device, interface, buf, len);
if( ret != -9 && ( ret != -1 || errno != EPIPE ) ) return ret;
@@ -146,6 +147,7 @@ int survive_usb_init( struct SurviveContext * ctx )
}
int i;
+ int16_t j;
libusb_device** devs;
int ret = libusb_get_device_list(ctx->usbctx, &devs);
@@ -201,7 +203,7 @@ int survive_usb_init( struct SurviveContext * ctx )
}
libusb_set_auto_detach_kernel_driver( ctx->udev[i], 1 );
- for (int j = 0; j < conf->bNumInterfaces; j++ )
+ for (j = 0; j < conf->bNumInterfaces; j++ )
{
#if 0
if (libusb_kernel_driver_active(ctx->udev[i], j) == 1) {
diff --git a/tools/ootx_decode/HMD_Datagen.c b/tools/ootx_decode/HMD_Datagen.c
new file mode 100644
index 0000000..15ed62c
--- /dev/null
+++ b/tools/ootx_decode/HMD_Datagen.c
@@ -0,0 +1,103 @@
+// (C) 2017 Joshua Allen, MIT/x11 License.
+//
+//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
+
+/* generate data to test ootx decoding */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <time.h>
+#include <stdlib.h>
+#include <zlib.h>
+
+//this program is broken and does not produce useable data.
+
+uint32_t time_stamp = -525198892;
+
+char* fmt_str = "L Y HMD %d 5 1 206230 %d\n";
+
+void print_bit(uint8_t data);
+void print_preamble();
+void print_uint16(uint16_t d);
+void print_uint32(uint32_t d);
+void print_payload(char* data, uint16_t length);
+
+
+int main(int argc, char* argv[])
+{
+ char* str = "Hello World!";
+// printf("%s\n", str);
+
+ srand(time(NULL));
+
+ print_preamble();
+
+ uint16_t payload_lenth = strlen(str);
+ uint32_t crc = crc32( 0L, Z_NULL, 0 );
+ crc = crc32( crc, (uint8_t*)str,payload_lenth);
+
+ print_uint16(payload_lenth);
+ print_payload(str,payload_lenth);
+ print_uint32(crc);
+
+ return 0;
+}
+
+void print_preamble() {
+ int i;
+ for (i=0;i<17;++i) print_bit(0);
+ print_bit(1);
+}
+
+void print_uint16(uint16_t d) {
+ int i;
+ for (i=0;i<16;++i) {
+ print_bit(d & 0x0001);
+ d>>=1;
+ }
+ print_bit(1);
+}
+
+void print_uint32(uint32_t d) {
+ int i = 0;
+ for (;i<16;++i) {
+ print_bit(d & 0x01);
+ d>>=1;
+ }
+ print_bit(1);
+
+ for (;i<32;++i) {
+ print_bit(d & 0x01);
+ d>>=1;
+ }
+ print_bit(1);
+}
+
+void print_payload(char* data, uint16_t length) {
+ int i;
+ for(i=0;i<length;i+=2) {
+ uint16_t d = *((uint16_t*)(data+i));
+// printf("%d\n", d);
+ print_uint16(d);
+ }
+}
+
+void print_bit(uint8_t data) {
+ uint32_t length = 3000 + (rand()%2)*500 + data*1000 + (rand()%2)*2000;
+ length -= rand()%500;
+ printf(fmt_str, time_stamp, length);
+
+ time_stamp++;
+
+ /*
+ //to decode
+ // 3000 + x*500 + dbit*1000 + y*2000
+ length -= 3000;
+ if (length>=2000) { length-=2000; y = 0x01; }
+ if (length>=1000) { length-=1000; dbit = 0x01; }
+ if (length>=500) { x = 0x01; }
+ */
+
+ //fire off a callback when a full OOTX packet is received
+}
diff --git a/tools/ootx_decode/Makefile b/tools/ootx_decode/Makefile
new file mode 100644
index 0000000..b3b07b4
--- /dev/null
+++ b/tools/ootx_decode/Makefile
@@ -0,0 +1,7 @@
+all: ootx_decode hmd_datagen
+
+hmd_datagen: HMD_Datagen.c
+ gcc -Wall HMD_Datagen.c -lz -o hmd_datagen
+
+ootx_decode: ootx_decode.c ../../src/ootx_decoder.c ../../src/ootx_decoder.h
+ gcc -Wall ootx_decode.c ../../src/ootx_decoder.c -lz -o ootx_decode -I ../../src/
diff --git a/tools/ootx_decode/ootx_decode.c b/tools/ootx_decode/ootx_decode.c
new file mode 100644
index 0000000..1823aaa
--- /dev/null
+++ b/tools/ootx_decode/ootx_decode.c
@@ -0,0 +1,121 @@
+// (C) 2017 Joshua Allen, MIT/x11 License.
+//
+//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
+
+/* ootx data decoder test*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "ootx_decoder.h"
+
+void my_test(ootx_decoder_context *ctx, ootx_packet* packet) {
+ packet->data[packet->length] = 0;
+ printf("%d %s 0x%X\n", packet->length, packet->data, packet->crc32);
+}
+
+void my_test2(ootx_decoder_context *ctx, ootx_packet* packet) {
+ printf("completed ootx packet\n");
+
+ lighthouse_info_v6 lhi;
+ init_lighthouse_info_v6(&lhi,packet->data);
+ print_lighthouse_info_v6(&lhi);
+// packet->data[packet->length] = 0;
+// printf("%d %s 0x%X\n", packet->length, packet->data, packet->crc32);
+}
+
+
+void print_crc32(uint32_t crc) {
+// uint8_t* p = (uint32_t*)&crc;
+// uint8_t i = 0;
+
+ printf("%X\n", crc);
+}
+
+void write_to_file(uint8_t *d, uint16_t length){
+ FILE *fp = fopen("binary.data","w");
+ fwrite(d, length, 1, fp);
+ fclose(fp);
+}
+
+void bad_crc(ootx_decoder_context *ctx, ootx_packet* packet, uint32_t crc) {
+ printf("CRC mismatch\n");
+
+ printf("r:");
+ print_crc32(packet->crc32);
+
+ printf("c:");
+ print_crc32(crc);
+ write_to_file(packet->data,packet->length);
+}
+
+ootx_decoder_context ctx[2];
+
+void cnlohr_code_test() {
+ ootx_packet_clbk = my_test2;
+ ootx_bad_crc_clbk = bad_crc;
+
+ char* line = NULL;
+ size_t line_len = 0;
+ char trash[100] = "";
+ int8_t lh_id = 0x00;
+ uint32_t ticks = 0x00;
+ int32_t delta = 0x00;
+ uint8_t acode = 0x00;
+
+ ootx_decoder_context *c_ctx = ctx;
+
+ while (getline(&line,&line_len,stdin)>0) {
+ //R Y HMD -1575410734 -2 7 19714 6485
+ sscanf(line,"%s %s %s %s %hhd %hhd %d %d",
+ trash,
+ trash,
+ trash,
+ trash,
+ &lh_id,
+ &acode,
+ &delta,
+ &ticks);
+
+// int8_t lh = lighthouse_code=='R'?0:1;
+// printf("LH:%d %s\n", lh_id, line);
+ int8_t lh = (lh_id*-1)-1;
+ if (lh_id < 0) {
+// uint8_t bit = 0x01; //bit for debugging purposes
+
+ //change to newly found lighthouse
+ c_ctx = ctx+lh;
+
+// uint8_t dbit = ootx_decode_bit(ticks);
+// printf("LH:%d ticks:%d bit:%X %s", lh, ticks, dbit, line);
+
+// ootx_process_bit(c_ctx, ticks);
+ ootx_pump_bit( c_ctx, (acode&0x02)>>1 );
+/*
+ uint16_t s = *(c_ctx->payload_size);
+ uint16_t fwv = *(c_ctx->buffer+2);
+ uint16_t pv = 0x3f & fwv; //protocol version
+ fwv>>=6; //firmware version
+
+ //this will print after any messages from ootx_pump
+// if (c_ctx->found_preamble>0) printf("LH:%d s:%d 0x%x fw:%d pv:%d bo:%d bit:%d\t%s", current_lighthouse, s, s, fwv, pv, c_ctx->buf_offset, bit, line);
+*/
+ }
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ ootx_init_decoder_context(ctx);
+ ootx_init_decoder_context(ctx+1);
+
+ cnlohr_code_test();
+
+ ootx_free_decoder_context(ctx);
+ ootx_free_decoder_context(ctx+1);
+
+ return 0;
+}