diff --git a/software/raspberrypi_video/LeptonThread.cpp b/software/raspberrypi_video/LeptonThread.cpp index 6be567d..281b35b 100644 --- a/software/raspberrypi_video/LeptonThread.cpp +++ b/software/raspberrypi_video/LeptonThread.cpp @@ -1,109 +1,181 @@ #include "LeptonThread.h" - +#include "stdio.h" #include "Palettes.h" #include "SPI.h" #include "Lepton_I2C.h" +#include -#define PACKET_SIZE 164 -#define PACKET_SIZE_UINT16 (PACKET_SIZE/2) -#define PACKETS_PER_FRAME 60 -#define FRAME_SIZE_UINT16 (PACKET_SIZE_UINT16*PACKETS_PER_FRAME) -#define FPS 27; +//We are using a struct for the entire image, made up of 4 segments, 60 lines +//each, with 164 bytes of data. Allows easy use of sizeof() in code. +typedef struct __attribute__((packed)) _packet { + uint16_t packet_number; + uint16_t crc; + uint8_t video_src[160]; +} packet; -LeptonThread::LeptonThread() : QThread() -{ -} +typedef struct __attribute__((packed)) _segment { + packet line[60]; +} segment; -LeptonThread::~LeptonThread() { -} +typedef struct __attribute__((packed)) _image { + segment seg[4]; +} image; + + +#define FPS (27) + +#define HAND_TEMP_THRESHOLD 8050 + +int readyToToggle = 1; + +LeptonThread::LeptonThread() : QThread(){} + +LeptonThread::~LeptonThread() {} void LeptonThread::run() { - //create the initial image - myImage = QImage(80, 60, QImage::Format_RGB888); - - //open spi port - SpiOpenPort(0); - - while(true) { - - //read data packets from lepton over SPI - int resets = 0; - for(int j=0;j>8; + } + + //Read the rest of this 1/3 segment. + read(spi_port_fd, &img.seg[segNum].line[1], 19*sizeof(packet)); + + //Read the remainder of the segment. + for (int part=1;part<3;part++) { + read(spi_port_fd, &img.seg[segNum].line[20*(part%3)], 20*sizeof(packet)); + } + + //If the segment number is 0, the frame is invalid. + if ((img.seg[segNum].line[20].packet_number & 0xff) == 0) { + flag = true; + if ((img.seg[segNum].line[20].packet_number >> 8) != 0x14) { + resets++; } + break; } } - if(resets >= 30) { - qDebug() << "done reading, resets: " << resets; + if (flag) { + continue; + } + + //Check to make sure packet number is correct, or else the Lepton is out of sync. + //We have 750 'resets' before re-syncing. + if (resets == 750) { + SpiClosePort(spi_port); + usleep(750000); + SpiOpenPort(spi_port); + resets = 0; + continue; } + + - frameBuffer = (uint16_t *)result; int row, column; uint16_t value; - uint16_t minValue = 65535; - uint16_t maxValue = 0; - - for(int i=0;i maxValue) { + totalCounts += value; + if (value > maxValue) { maxValue = value; } - if(value < minValue) { + if (value < minValue && value > 0) { minValue = value; } - column = i % PACKET_SIZE_UINT16 - 2; - row = i / PACKET_SIZE_UINT16 ; } - + + //If the difference between Max and Min is 0, we need to get a new frame before emitting. float diff = maxValue - minValue; - float scale = 255/diff; - QRgb color; - for(int i=0;i 255) { + value = 255; + } + + color = qRgb(colormap[3*value], colormap[3*value+1], colormap[3*value+2]); + + + column = (i % (sizeof(packet)/2)) - 2; + row = i / (sizeof(packet)/2); + int newColumn = (row % 2 == 0) ? column : column + 80 ; + int newRow = row/2; + myImage.setPixel(newColumn, newRow, color); + } + //Emit the finalized image for update. + emit updateImage(myImage); + } + minValue = 65535; + maxValue = 0; + totalCounts = 0; + resets = 0; } - //finally, close SPI port just bcuz - SpiClosePort(0); + SpiClosePort(spi_port); } void LeptonThread::performFFC() { - //perform FFC lepton_perform_ffc(); } diff --git a/software/raspberrypi_video/LeptonThread.h b/software/raspberrypi_video/LeptonThread.h index 9d28d65..d8fba84 100644 --- a/software/raspberrypi_video/LeptonThread.h +++ b/software/raspberrypi_video/LeptonThread.h @@ -9,10 +9,10 @@ #include #include -#define PACKET_SIZE 164 -#define PACKET_SIZE_UINT16 (PACKET_SIZE/2) -#define PACKETS_PER_FRAME 60 -#define FRAME_SIZE_UINT16 (PACKET_SIZE_UINT16*PACKETS_PER_FRAME) +#define PACKET_SIZE 3280 +#define PACKET_SIZE_UINT16 1640 //(PACKET_SIZE/2) +#define PACKETS_PER_FRAME 12 +#define FRAME_SIZE_UINT16 19680 //(PACKET_SIZE_UINT16*PACKETS_PER_FRAME) class LeptonThread : public QThread { @@ -36,7 +36,8 @@ public slots: QImage myImage; uint8_t result[PACKET_SIZE*PACKETS_PER_FRAME]; - uint16_t *frameBuffer; +// uint16_t* frameBuffer; + uint16_t frameBuffer[FRAME_SIZE_UINT16]; }; diff --git a/software/raspberrypi_video/Makefile b/software/raspberrypi_video/Makefile new file mode 100644 index 0000000..5b2e25b --- /dev/null +++ b/software/raspberrypi_video/Makefile @@ -0,0 +1,269 @@ +############################################################################# +# Makefile for building: raspberrypi_video +# Generated by qmake (2.01a) (Qt 4.8.6) on: Wed Jul 13 21:59:35 2016 +# Project: raspberrypi_video.pro +# Template: app +# Command: /usr/lib/arm-linux-gnueabihf/qt4/bin/qmake -o Makefile raspberrypi_video.pro +############################################################################# + +####### Compiler, tools and options + +CC = gcc +CXX = g++ +DEFINES = -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED +CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES) +INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../raspberrypi_libs -Igen_mocs +LINK = g++ +LFLAGS = -Wl,-O1 +LIBS = $(SUBLIBS) -L/usr/lib/arm-linux-gnueabihf -L../raspberrypi_libs/leptonSDKEmb32PUB/Debug -lLEPTON_SDK -lQtGui -lQtCore -lpthread +AR = ar cqs +RANLIB = +QMAKE = /usr/lib/arm-linux-gnueabihf/qt4/bin/qmake +TAR = tar -cf +COMPRESS = gzip -9f +COPY = cp -f +SED = sed +COPY_FILE = $(COPY) +COPY_DIR = $(COPY) -r +STRIP = strip +INSTALL_FILE = install -m 644 -p +INSTALL_DIR = $(COPY_DIR) +INSTALL_PROGRAM = install -m 755 -p +DEL_FILE = rm -f +SYMLINK = ln -f -s +DEL_DIR = rmdir +MOVE = mv -f +CHK_DIR_EXISTS= test -d +MKDIR = mkdir -p + +####### Output directory + +OBJECTS_DIR = gen_objs/ + +####### Files + +SOURCES = Lepton_I2C.cpp \ + LeptonThread.cpp \ + main.cpp \ + MyLabel.cpp \ + Palettes.cpp \ + SPI.cpp gen_mocs/moc_LeptonThread.cpp \ + gen_mocs/moc_MyLabel.cpp +OBJECTS = gen_objs/Lepton_I2C.o \ + gen_objs/LeptonThread.o \ + gen_objs/main.o \ + gen_objs/MyLabel.o \ + gen_objs/Palettes.o \ + gen_objs/SPI.o \ + gen_objs/moc_LeptonThread.o \ + gen_objs/moc_MyLabel.o +DIST = /usr/share/qt4/mkspecs/common/unix.conf \ + /usr/share/qt4/mkspecs/common/linux.conf \ + /usr/share/qt4/mkspecs/common/gcc-base.conf \ + /usr/share/qt4/mkspecs/common/gcc-base-unix.conf \ + /usr/share/qt4/mkspecs/common/g++-base.conf \ + /usr/share/qt4/mkspecs/common/g++-unix.conf \ + /usr/share/qt4/mkspecs/qconfig.pri \ + /usr/share/qt4/mkspecs/modules/qt_webkit.pri \ + /usr/share/qt4/mkspecs/features/qt_functions.prf \ + /usr/share/qt4/mkspecs/features/qt_config.prf \ + /usr/share/qt4/mkspecs/features/exclusive_builds.prf \ + /usr/share/qt4/mkspecs/features/default_pre.prf \ + /usr/share/qt4/mkspecs/features/release.prf \ + /usr/share/qt4/mkspecs/features/default_post.prf \ + /usr/share/qt4/mkspecs/features/shared.prf \ + /usr/share/qt4/mkspecs/features/unix/gdb_dwarf_index.prf \ + /usr/share/qt4/mkspecs/features/warn_on.prf \ + /usr/share/qt4/mkspecs/features/qt.prf \ + /usr/share/qt4/mkspecs/features/unix/thread.prf \ + /usr/share/qt4/mkspecs/features/moc.prf \ + /usr/share/qt4/mkspecs/features/resources.prf \ + /usr/share/qt4/mkspecs/features/uic.prf \ + /usr/share/qt4/mkspecs/features/yacc.prf \ + /usr/share/qt4/mkspecs/features/lex.prf \ + /usr/share/qt4/mkspecs/features/include_source_dir.prf \ + raspberrypi_video.pro +QMAKE_TARGET = raspberrypi_video +DESTDIR = +TARGET = raspberrypi_video + +first: all +####### Implicit rules + +.SUFFIXES: .o .c .cpp .cc .cxx .C + +.cpp.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cc.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.cxx.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.C.o: + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<" + +.c.o: + $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<" + +####### Build rules + +all: Makefile $(TARGET) + +$(TARGET): sdk $(OBJECTS) + $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS) + +Makefile: raspberrypi_video.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /usr/share/qt4/mkspecs/common/unix.conf \ + /usr/share/qt4/mkspecs/common/linux.conf \ + /usr/share/qt4/mkspecs/common/gcc-base.conf \ + /usr/share/qt4/mkspecs/common/gcc-base-unix.conf \ + /usr/share/qt4/mkspecs/common/g++-base.conf \ + /usr/share/qt4/mkspecs/common/g++-unix.conf \ + /usr/share/qt4/mkspecs/qconfig.pri \ + /usr/share/qt4/mkspecs/modules/qt_webkit.pri \ + /usr/share/qt4/mkspecs/features/qt_functions.prf \ + /usr/share/qt4/mkspecs/features/qt_config.prf \ + /usr/share/qt4/mkspecs/features/exclusive_builds.prf \ + /usr/share/qt4/mkspecs/features/default_pre.prf \ + /usr/share/qt4/mkspecs/features/release.prf \ + /usr/share/qt4/mkspecs/features/default_post.prf \ + /usr/share/qt4/mkspecs/features/shared.prf \ + /usr/share/qt4/mkspecs/features/unix/gdb_dwarf_index.prf \ + /usr/share/qt4/mkspecs/features/warn_on.prf \ + /usr/share/qt4/mkspecs/features/qt.prf \ + /usr/share/qt4/mkspecs/features/unix/thread.prf \ + /usr/share/qt4/mkspecs/features/moc.prf \ + /usr/share/qt4/mkspecs/features/resources.prf \ + /usr/share/qt4/mkspecs/features/uic.prf \ + /usr/share/qt4/mkspecs/features/yacc.prf \ + /usr/share/qt4/mkspecs/features/lex.prf \ + /usr/share/qt4/mkspecs/features/include_source_dir.prf \ + /usr/lib/arm-linux-gnueabihf/libQtGui.prl \ + /usr/lib/arm-linux-gnueabihf/libQtCore.prl + $(QMAKE) -o Makefile raspberrypi_video.pro +/usr/share/qt4/mkspecs/common/unix.conf: +/usr/share/qt4/mkspecs/common/linux.conf: +/usr/share/qt4/mkspecs/common/gcc-base.conf: +/usr/share/qt4/mkspecs/common/gcc-base-unix.conf: +/usr/share/qt4/mkspecs/common/g++-base.conf: +/usr/share/qt4/mkspecs/common/g++-unix.conf: +/usr/share/qt4/mkspecs/qconfig.pri: +/usr/share/qt4/mkspecs/modules/qt_webkit.pri: +/usr/share/qt4/mkspecs/features/qt_functions.prf: +/usr/share/qt4/mkspecs/features/qt_config.prf: +/usr/share/qt4/mkspecs/features/exclusive_builds.prf: +/usr/share/qt4/mkspecs/features/default_pre.prf: +/usr/share/qt4/mkspecs/features/release.prf: +/usr/share/qt4/mkspecs/features/default_post.prf: +/usr/share/qt4/mkspecs/features/shared.prf: +/usr/share/qt4/mkspecs/features/unix/gdb_dwarf_index.prf: +/usr/share/qt4/mkspecs/features/warn_on.prf: +/usr/share/qt4/mkspecs/features/qt.prf: +/usr/share/qt4/mkspecs/features/unix/thread.prf: +/usr/share/qt4/mkspecs/features/moc.prf: +/usr/share/qt4/mkspecs/features/resources.prf: +/usr/share/qt4/mkspecs/features/uic.prf: +/usr/share/qt4/mkspecs/features/yacc.prf: +/usr/share/qt4/mkspecs/features/lex.prf: +/usr/share/qt4/mkspecs/features/include_source_dir.prf: +/usr/lib/arm-linux-gnueabihf/libQtGui.prl: +/usr/lib/arm-linux-gnueabihf/libQtCore.prl: +qmake: FORCE + @$(QMAKE) -o Makefile raspberrypi_video.pro + +dist: + @$(CHK_DIR_EXISTS) gen_objs/raspberrypi_video1.0.0 || $(MKDIR) gen_objs/raspberrypi_video1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) gen_objs/raspberrypi_video1.0.0/ && $(COPY_FILE) --parents Lepton_I2C.h LeptonThread.h MyLabel.h Palettes.h SPI.h gen_objs/raspberrypi_video1.0.0/ && $(COPY_FILE) --parents Lepton_I2C.cpp LeptonThread.cpp main.cpp MyLabel.cpp Palettes.cpp SPI.cpp gen_objs/raspberrypi_video1.0.0/ && (cd `dirname gen_objs/raspberrypi_video1.0.0` && $(TAR) raspberrypi_video1.0.0.tar raspberrypi_video1.0.0 && $(COMPRESS) raspberrypi_video1.0.0.tar) && $(MOVE) `dirname gen_objs/raspberrypi_video1.0.0`/raspberrypi_video1.0.0.tar.gz . && $(DEL_FILE) -r gen_objs/raspberrypi_video1.0.0 + + +clean:compiler_clean + -$(DEL_FILE) $(OBJECTS) + -$(DEL_FILE) -r $(OBJECTS_DIR) gen_mocs + -$(DEL_FILE) *~ core *.core + + +####### Sub-libraries + +distclean: clean + -$(DEL_FILE) $(TARGET) + -$(DEL_FILE) Makefile + + +sdk: + make -C ../raspberrypi_libs/leptonSDKEmb32PUB + +sdkclean: + make -C ../raspberrypi_libs/leptonSDKEmb32PUB clean + +check: first + +mocclean: compiler_moc_header_clean compiler_moc_source_clean + +mocables: compiler_moc_header_make_all compiler_moc_source_make_all + +compiler_moc_header_make_all: gen_mocs/moc_LeptonThread.cpp gen_mocs/moc_MyLabel.cpp +compiler_moc_header_clean: + -$(DEL_FILE) gen_mocs/moc_LeptonThread.cpp gen_mocs/moc_MyLabel.cpp +gen_mocs/moc_LeptonThread.cpp: LeptonThread.h + /usr/lib/arm-linux-gnueabihf/qt4/bin/moc $(DEFINES) $(INCPATH) LeptonThread.h -o gen_mocs/moc_LeptonThread.cpp + +gen_mocs/moc_MyLabel.cpp: MyLabel.h + /usr/lib/arm-linux-gnueabihf/qt4/bin/moc $(DEFINES) $(INCPATH) MyLabel.h -o gen_mocs/moc_MyLabel.cpp + +compiler_rcc_make_all: +compiler_rcc_clean: +compiler_image_collection_make_all: qmake_image_collection.cpp +compiler_image_collection_clean: + -$(DEL_FILE) qmake_image_collection.cpp +compiler_moc_source_make_all: +compiler_moc_source_clean: +compiler_uic_make_all: +compiler_uic_clean: +compiler_yacc_decl_make_all: +compiler_yacc_decl_clean: +compiler_yacc_impl_make_all: +compiler_yacc_impl_clean: +compiler_lex_make_all: +compiler_lex_clean: +compiler_clean: compiler_moc_header_clean + +####### Compile + +gen_objs/Lepton_I2C.o: Lepton_I2C.cpp Lepton_I2C.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/Lepton_I2C.o Lepton_I2C.cpp + +gen_objs/LeptonThread.o: LeptonThread.cpp LeptonThread.h \ + Palettes.h \ + SPI.h \ + Lepton_I2C.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/LeptonThread.o LeptonThread.cpp + +gen_objs/main.o: main.cpp LeptonThread.h \ + MyLabel.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/main.o main.cpp + +gen_objs/MyLabel.o: MyLabel.cpp MyLabel.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/MyLabel.o MyLabel.cpp + +gen_objs/Palettes.o: Palettes.cpp Palettes.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/Palettes.o Palettes.cpp + +gen_objs/SPI.o: SPI.cpp SPI.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/SPI.o SPI.cpp + +gen_objs/moc_LeptonThread.o: gen_mocs/moc_LeptonThread.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/moc_LeptonThread.o gen_mocs/moc_LeptonThread.cpp + +gen_objs/moc_MyLabel.o: gen_mocs/moc_MyLabel.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o gen_objs/moc_MyLabel.o gen_mocs/moc_MyLabel.cpp + +####### Install + +install: FORCE + +uninstall: FORCE + +FORCE: + diff --git a/software/raspberrypi_video/SPI.cpp b/software/raspberrypi_video/SPI.cpp index 690d743..cb4b0e6 100644 --- a/software/raspberrypi_video/SPI.cpp +++ b/software/raspberrypi_video/SPI.cpp @@ -5,7 +5,7 @@ int spi_cs1_fd = -1; unsigned char spi_mode = SPI_MODE_3; unsigned char spi_bitsPerWord = 8; -unsigned int spi_speed = 10000000; +unsigned int spi_speed = 20000000; int SpiOpenPort (int spi_device) { @@ -13,18 +13,18 @@ int SpiOpenPort (int spi_device) int *spi_cs_fd; - //----- SET SPI MODE ----- + //----- SET SPI MODE -----// //SPI_MODE_0 (0,0) CPOL=0 (Clock Idle low level), CPHA=0 (SDO transmit/change edge active to idle) //SPI_MODE_1 (0,1) CPOL=0 (Clock Idle low level), CPHA=1 (SDO transmit/change edge idle to active) //SPI_MODE_2 (1,0) CPOL=1 (Clock Idle high level), CPHA=0 (SDO transmit/change edge active to idle) //SPI_MODE_3 (1,1) CPOL=1 (Clock Idle high level), CPHA=1 (SDO transmit/change edge idle to active) spi_mode = SPI_MODE_3; - //----- SET BITS PER WORD ----- + //----- SET BITS PER WORD -----// spi_bitsPerWord = 8; - //----- SET SPI BUS SPEED ----- - spi_speed = 10000000; //1000000 = 1MHz (1uS per bit) + //----- SET SPI BUS SPEED -----// + spi_speed = 20000000; if (spi_device)