example for for using the flir lepton on the stm32nucleo_401re

Dependencies:   mbed

main.cpp

Committer:
pureengineering
Date:
2014-09-18
Revision:
0:4065e63beba6
Child:
1:dd6a90cf2191

File content as of revision 0:4065e63beba6:

#include "mbed.h"

Serial pc(SERIAL_TX, SERIAL_RX);
SPI lepton_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
DigitalOut spi_cs(SPI_CS);
 
//DigitalOut myled(LED1);

#define VOSPI_FRAME_SIZE (164) 
uint8_t lepton_frame_packet[VOSPI_FRAME_SIZE]; 
int lepton_image[80][80];

static void print_image_binary(void)
{
    int i;
    int j;
    
    pc.putc(0xDE);
    pc.putc(0xAD);
    pc.putc(0xBE);
    pc.putc(0xEF);
    
    for(i=0;i<60;i++)
    {
        for(j=0;j<80;j++)
        {
            while(pc.writeable() == 0);
            pc.putc((lepton_image[i][j]>>8)&0xff);
            while(pc.writeable() == 0);
            pc.putc(lepton_image[i][j]&0xff);
            wait_us(1);
        }
    }
}

int lost_frame_counter = 0;

int last_frame_number;
int frame_complete = 0;
int start_image = 0;
int need_resync = 0;
int last_crc;
int new_frame = 0;
int frame_counter = 0;

void transfer(void)
{
    int i;
    int frame_number;

    
    spi_cs = 0;
    for(i=0;i<VOSPI_FRAME_SIZE;i++)
    {
        lepton_frame_packet[i] = lepton_spi.write(0x55);
    }
    spi_cs = 1;
    
    //pc.printf("%x %x \n\r",lepton_frame_packet[0], lepton_frame_packet[1]);
    
    //for(i=0;i<VOSPI_FRAME_SIZE;i++)
    //{
    //    pc.printf("%x,",lepton_frame_packet[i]);
   // }
    //pc.printf("\n\r");

    if(((lepton_frame_packet[0]&0xf) != 0x0f))
    {
        if(lepton_frame_packet[1] == 0  )
        {
            if(last_crc != (lepton_frame_packet[3]<<8 | lepton_frame_packet[4]))
            {
                new_frame = 1;
            }
            last_crc = lepton_frame_packet[3]<<8 | lepton_frame_packet[4];
        }
        frame_number = lepton_frame_packet[1];
        if( (frame_number>0) && (frame_number<60))
        {
            pc.printf("%d\n\r",frame_number);
        }

        if(frame_number < 60 )
        {
            lost_frame_counter = 0;
            for(i=0;i<80;i++)
            {
                lepton_image[frame_number][i] = (lepton_frame_packet[2*i+4] << 8 | lepton_frame_packet[2*i+5]);
            }
        }
        if( frame_number == 59)
        {
            frame_complete = 1;
            last_frame_number = 0;
        }
    }
    else
    {
        wait_us(10);
    }

    lost_frame_counter++;
    if(lost_frame_counter>1000)
    {
        need_resync = 1;
        lost_frame_counter = 0;

    }

    if(need_resync)
    {
        pc.printf("resync\n\r");
        wait_us(185);
        need_resync = 0;
    }


    if(frame_complete)
    {
        if(new_frame)
        {
            frame_counter++;
            {
                if(frame_counter%9 ==0)
                {
                    //print_image_binary();
                }
                pc.printf("print_image_binary\n\r");
            }
            new_frame = 0;
        }

        frame_complete = 0;
    }
}
 
 
int main() 
{

    //pc.baud(9600);
  pc.baud(115200);
  //pc.baud(1000000);
  pc.printf("Hello World !\n");
  lepton_spi.format(8,3);
  lepton_spi.frequency(20000000);
  spi_cs = 1;
  
  wait_us(185);
  
  while(1) 
  { 
      //myled = !myled;
      transfer();
  }
}