Lepton sensor on the DISCO-F746NG board. Based on the rapberry_pi example code at https://github.com/groupgets/LeptonModule

Dependencies:   mbed-src BSP_DISCO_F746NG LCD_DISCO_F746NG

Committer:
pryd
Date:
Thu Dec 10 12:42:55 2015 +0000
Revision:
9:74e00a8d537f
Parent:
8:f57dc76095f3
bugfixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pryd 0:291e05a4e8e9 1 #include "mbed.h"
Patrik Ryd 4:15c97a3e1995 2 #include "palettes.h"
pryd 7:c0f3fc856506 3 #include "LCD_DISCO_F746NG.h"
pryd 0:291e05a4e8e9 4
Patrik Ryd 4:15c97a3e1995 5 #define BYTES_PER_PACKET (164)
Patrik Ryd 4:15c97a3e1995 6 #define DATA_PER_PACKET (BYTES_PER_PACKET/2)
Patrik Ryd 4:15c97a3e1995 7 #define PACKETS_PER_FRAME (60)
pryd 0:291e05a4e8e9 8
pryd 0:291e05a4e8e9 9 Serial pc(USBTX, USBRX);
pryd 0:291e05a4e8e9 10 SPI lepton_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
pryd 0:291e05a4e8e9 11 DigitalOut spi_cs(SPI_CS);
Patrik Ryd 6:5c25567a46df 12 LCD_DISCO_F746NG lcd;
pryd 0:291e05a4e8e9 13
Patrik Ryd 4:15c97a3e1995 14 static uint8_t lepton_buffer[BYTES_PER_PACKET * PACKETS_PER_FRAME];
pryd 9:74e00a8d537f 15 static uint32_t lepton_image[60][80];
pryd 0:291e05a4e8e9 16
Patrik Ryd 2:a0eb7bff9571 17 void setup(void)
Patrik Ryd 2:a0eb7bff9571 18 {
Patrik Ryd 2:a0eb7bff9571 19 lepton_spi.format(8,3);
Patrik Ryd 2:a0eb7bff9571 20 lepton_spi.frequency(20000000);
Patrik Ryd 2:a0eb7bff9571 21 spi_cs = 1;
Patrik Ryd 2:a0eb7bff9571 22 spi_cs = 0;
Patrik Ryd 2:a0eb7bff9571 23 spi_cs = 1;
Patrik Ryd 2:a0eb7bff9571 24 wait_ms(185);
Patrik Ryd 6:5c25567a46df 25 lcd.Clear(LCD_COLOR_BLACK);
Patrik Ryd 2:a0eb7bff9571 26 }
Patrik Ryd 2:a0eb7bff9571 27
Patrik Ryd 2:a0eb7bff9571 28 void get_frame(void)
Patrik Ryd 2:a0eb7bff9571 29 {
pryd 7:c0f3fc856506 30 int p;
Patrik Ryd 4:15c97a3e1995 31 int i;
Patrik Ryd 4:15c97a3e1995 32 int packet_num;
pryd 7:c0f3fc856506 33
pryd 7:c0f3fc856506 34 spi_cs = 0;
Patrik Ryd 4:15c97a3e1995 35
Patrik Ryd 4:15c97a3e1995 36 // No error handling. Assume we don't loose any packets
pryd 7:c0f3fc856506 37 for(p=0; p<PACKETS_PER_FRAME; p++) {
pryd 7:c0f3fc856506 38 for(i=0; i<BYTES_PER_PACKET; i++) {
pryd 7:c0f3fc856506 39 lepton_buffer[p*BYTES_PER_PACKET + i] = lepton_spi.write(0x00);
Patrik Ryd 4:15c97a3e1995 40 }
pryd 7:c0f3fc856506 41 packet_num = lepton_buffer[p*BYTES_PER_PACKET + 1];
pryd 7:c0f3fc856506 42 if(packet_num != p) {
pryd 7:c0f3fc856506 43 //pc.printf("-");
pryd 7:c0f3fc856506 44 i = -1;
pryd 7:c0f3fc856506 45 p = -1;
pryd 7:c0f3fc856506 46 wait_ms(1);
pryd 7:c0f3fc856506 47 } else {
pryd 7:c0f3fc856506 48 //pc.printf("+");
Patrik Ryd 4:15c97a3e1995 49 }
Patrik Ryd 4:15c97a3e1995 50 }
Patrik Ryd 4:15c97a3e1995 51
pryd 7:c0f3fc856506 52 spi_cs = 1;
Patrik Ryd 2:a0eb7bff9571 53 }
Patrik Ryd 2:a0eb7bff9571 54
Patrik Ryd 4:15c97a3e1995 55 void create_image(void)
Patrik Ryd 4:15c97a3e1995 56 {
Patrik Ryd 4:15c97a3e1995 57 uint16_t min_val = 65535;
Patrik Ryd 4:15c97a3e1995 58 uint16_t max_val = 0;
Patrik Ryd 4:15c97a3e1995 59 uint16_t value;
pryd 9:74e00a8d537f 60 int x;
pryd 9:74e00a8d537f 61 int y;
Patrik Ryd 4:15c97a3e1995 62 int i;
pryd 7:c0f3fc856506 63 int p;
Patrik Ryd 4:15c97a3e1995 64
pryd 7:c0f3fc856506 65 for(p=0; p<PACKETS_PER_FRAME; p++) {
pryd 9:74e00a8d537f 66 for(i=2; i<DATA_PER_PACKET; i++) {
pryd 8:f57dc76095f3 67 value = (lepton_buffer[p*BYTES_PER_PACKET+i*2+0] << 8) + lepton_buffer[p*BYTES_PER_PACKET+i*2+1];
pryd 8:f57dc76095f3 68 //pc.printf("val = %d\n",value);
pryd 7:c0f3fc856506 69 if(value > max_val) {
pryd 7:c0f3fc856506 70 max_val = value;
pryd 7:c0f3fc856506 71 }
pryd 7:c0f3fc856506 72 if(value < min_val) {
pryd 7:c0f3fc856506 73 min_val = value;
pryd 7:c0f3fc856506 74 }
Patrik Ryd 4:15c97a3e1995 75 }
Patrik Ryd 4:15c97a3e1995 76 }
pryd 8:f57dc76095f3 77
pryd 8:f57dc76095f3 78 //pc.printf("max %d, min %d\n", max_val, min_val);
pryd 7:c0f3fc856506 79
Patrik Ryd 4:15c97a3e1995 80 float diff = max_val - min_val;
Patrik Ryd 4:15c97a3e1995 81 float scale = 255/diff;
Patrik Ryd 4:15c97a3e1995 82 uint8_t r;
Patrik Ryd 4:15c97a3e1995 83 uint8_t g;
Patrik Ryd 4:15c97a3e1995 84 uint8_t b;
Patrik Ryd 4:15c97a3e1995 85
pryd 7:c0f3fc856506 86 for(p=0; p<PACKETS_PER_FRAME; p++) {
pryd 9:74e00a8d537f 87 for(i=2; i<DATA_PER_PACKET; i++) {
pryd 9:74e00a8d537f 88
pryd 8:f57dc76095f3 89 value = (lepton_buffer[p*BYTES_PER_PACKET+i*2+0] << 8) + lepton_buffer[p*BYTES_PER_PACKET+i*2+1];
pryd 8:f57dc76095f3 90
pryd 9:74e00a8d537f 91 x = i - 2;
pryd 9:74e00a8d537f 92 y = p;
pryd 8:f57dc76095f3 93 #if 1
pryd 8:f57dc76095f3 94 value = (value - min_val) * scale;
pryd 8:f57dc76095f3 95 //pc.printf("val = %d\n", value);
pryd 7:c0f3fc856506 96 r = colormap_ironblack[3*value];
pryd 7:c0f3fc856506 97 g = colormap_ironblack[3*value+1];
pryd 7:c0f3fc856506 98 b = colormap_ironblack[3*value+2];
pryd 7:c0f3fc856506 99
pryd 9:74e00a8d537f 100 lepton_image[y][x] = (0xFF << 24) | (r << 16) | (g << 8) | b;
pryd 8:f57dc76095f3 101 #else
pryd 9:74e00a8d537f 102 lepton_image[y][x] = (0xFF << 24) | (value % 255) << 16 | (value % 255) << 8 | (value % 255);
pryd 7:c0f3fc856506 103 #endif
Patrik Ryd 4:15c97a3e1995 104 }
pryd 7:c0f3fc856506 105 }
pryd 7:c0f3fc856506 106 }
Patrik Ryd 4:15c97a3e1995 107
pryd 7:c0f3fc856506 108 void dummy_image(void)
pryd 7:c0f3fc856506 109 {
pryd 9:74e00a8d537f 110 int x;
pryd 9:74e00a8d537f 111 int y;
Patrik Ryd 6:5c25567a46df 112
pryd 9:74e00a8d537f 113 for(y=0; y<60; y++) {
pryd 9:74e00a8d537f 114 for(x=0; x<80; x++) {
pryd 9:74e00a8d537f 115 lepton_image[y][x] = (0xFF << 24) | (x << 16) | (y << 8);
pryd 7:c0f3fc856506 116 }
Patrik Ryd 4:15c97a3e1995 117 }
Patrik Ryd 4:15c97a3e1995 118 }
Patrik Ryd 4:15c97a3e1995 119
Patrik Ryd 6:5c25567a46df 120 void show_image(void)
Patrik Ryd 6:5c25567a46df 121 {
pryd 9:74e00a8d537f 122 int x;
pryd 9:74e00a8d537f 123 int y;
Patrik Ryd 6:5c25567a46df 124
pryd 9:74e00a8d537f 125 for(y=0; y<60; y++) {
pryd 9:74e00a8d537f 126 for(x=0; x<80; x++) {
pryd 9:74e00a8d537f 127 lcd.SetTextColor(lepton_image[y][x]);
pryd 9:74e00a8d537f 128 lcd.FillRect(x*5, y*4, 5, 4);
Patrik Ryd 6:5c25567a46df 129 }
Patrik Ryd 6:5c25567a46df 130 }
Patrik Ryd 6:5c25567a46df 131 }
Patrik Ryd 6:5c25567a46df 132
pryd 7:c0f3fc856506 133 void dump_frame(void)
pryd 7:c0f3fc856506 134 {
pryd 7:c0f3fc856506 135 int i;
pryd 7:c0f3fc856506 136 int p;
pryd 7:c0f3fc856506 137
pryd 7:c0f3fc856506 138 for(p=0; p<PACKETS_PER_FRAME; p++) {
pryd 7:c0f3fc856506 139 pc.printf("\n\nPacket %d:\n", p);
pryd 8:f57dc76095f3 140 for(i=0; i<BYTES_PER_PACKET; i++) {
pryd 7:c0f3fc856506 141 pc.printf("%x ", lepton_buffer[p*BYTES_PER_PACKET+i]);
pryd 7:c0f3fc856506 142 }
pryd 7:c0f3fc856506 143 }
pryd 7:c0f3fc856506 144 }
Patrik Ryd 6:5c25567a46df 145
pryd 0:291e05a4e8e9 146 int main()
pryd 0:291e05a4e8e9 147 {
pryd 0:291e05a4e8e9 148 pc.printf("Alive\n");
Patrik Ryd 6:5c25567a46df 149 setup();
Patrik Ryd 6:5c25567a46df 150 pc.printf("Up...\n");
Patrik Ryd 6:5c25567a46df 151
pryd 7:c0f3fc856506 152 while(1) {
Patrik Ryd 6:5c25567a46df 153 get_frame();
pryd 8:f57dc76095f3 154 //dump_frame();
pryd 8:f57dc76095f3 155 create_image();
pryd 7:c0f3fc856506 156 //dummy_image();
Patrik Ryd 6:5c25567a46df 157 show_image();
Patrik Ryd 6:5c25567a46df 158 }
pryd 0:291e05a4e8e9 159 }