Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20 mhz lepton3 #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 143 additions & 71 deletions software/raspberrypi_video/LeptonThread.cpp
Original file line number Diff line number Diff line change
@@ -1,109 +1,181 @@
#include "LeptonThread.h"

#include "stdio.h"
#include "Palettes.h"
#include "SPI.h"
#include "Lepton_I2C.h"
#include <errno.h>

#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<PACKETS_PER_FRAME;j++) {
//if it's a drop packet, reset j to 0, set to -1 so he'll be at 0 again loop
read(spi_cs0_fd, result+sizeof(uint8_t)*PACKET_SIZE*j, sizeof(uint8_t)*PACKET_SIZE);
int packetNumber = result[j*PACKET_SIZE+1];
if(packetNumber != j) {
j = -1;
resets += 1;
usleep(1000);
//Note: we've selected 750 resets as an arbitrary limit, since there should never be 750 "null" packets between two valid transmissions at the current poll rate
//By polling faster, developers may easily exceed this count, and the down period between frames may then be flagged as a loss of sync
if(resets == 750) {
SpiClosePort(0);
usleep(750000);
SpiOpenPort(0);
//Create the initial image and open the Spi port.
uint16_t spi_port = 1;
myImage = QImage(160, 120, QImage::Format_RGB888);
SpiOpenPort(spi_port);
uint32_t totalCounts = 0;
uint16_t minValue = 65535;
uint16_t maxValue = 0;
int resets = 0;

//Camera Loop
while (true) {
int spi_port_fd = spi_port == 1 ? spi_cs1_fd : spi_cs0_fd;
image img;
bool flag = false;
for (int segNum = 0; segNum < 4; segNum++) {

//Read through until the 0th packet.
int packet_num = 1;
while (packet_num != 0) {
read(spi_port_fd, &img.seg[segNum], sizeof(packet));
packet_num = img.seg[segNum].line[0].packet_number>>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<FRAME_SIZE_UINT16;i++) {
//skip the first 2 uint16_t's of every packet, they're 4 header bytes
if(i % PACKET_SIZE_UINT16 < 2) {
//Iterate through the current segment pixel at a time. In this loop, we find the
//minimum and maximum values of the image for AGC.
int lineNum = 0;
int pixelNum = 0;
int segNum = 0;
for (unsigned i=0;i<sizeof(image)/2;i++) {

//Skip the header of each line
if (i % (sizeof(packet)/2) < 2) {
continue;
}

//80 pixels per line, 60 lines per segment.
pixelNum++;
if (pixelNum == 80) {
pixelNum = 0;
lineNum++;
}
if (lineNum == 60) {
lineNum = 0;
segNum++;
}

//flip the MSB and LSB at the last second
int temp = result[i*2];
result[i*2] = result[i*2+1];
result[i*2+1] = temp;
//Flip the MSB and LSB for correct coloring.
frameBuffer[i] = img.seg[segNum].
line[lineNum].
video_src[2*pixelNum] << 8 |
img.seg[segNum].
line[lineNum].
video_src[2*pixelNum+1] << 0;

value = frameBuffer[i];
if(value > 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<FRAME_SIZE_UINT16;i++) {
if(i % PACKET_SIZE_UINT16 < 2) {
continue;
}
value = (frameBuffer[i] - minValue) * scale;
const int *colormap = colormap_ironblack;
color = qRgb(colormap[3*value], colormap[3*value+1], colormap[3*value+2]);
column = (i % PACKET_SIZE_UINT16 ) - 2;
row = i / PACKET_SIZE_UINT16;
myImage.setPixel(column, row, color);
}
if (diff != 0) {
float scale = 255/diff;
QRgb color;

//Iterates through the entire frame, one pixel at a time, for colorization.
for (unsigned i=0;i<sizeof(image)/2;i++) {

//Skip the header of each line.
if (i % (sizeof(packet)/2) < 2) {
continue;
}

//lets emit the signal for update
emit updateImage(myImage);
value = (frameBuffer[i] - minValue) * scale;
const int *colormap = colormap_ironblack;
if (value > 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();
}
11 changes: 6 additions & 5 deletions software/raspberrypi_video/LeptonThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <QPixmap>
#include <QImage>

#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
{
Expand All @@ -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];

};

Expand Down
Loading