Advertisement
Guest User

flirlepton2

a guest
Sep 18th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <avr/io.h>
  3.  
  4. typedef unsigned char u8;
  5. typedef unsigned short u16;
  6.  
  7. #include "SPI.h"
  8. #include "Adafruit_GFX.h"
  9. #include "Adafruit_ILI9340.h"
  10.  
  11. #if defined(__SAM3X8E__)
  12.     #undef __FlashStringHelper::F(string_literal)
  13.     #define F(string_literal) string_literal
  14. #endif
  15.  
  16. int numsteps = 300; //4800
  17.  
  18. //RAM
  19. //SRAM opcodes
  20. #define RDSR 5
  21. #define WRSR 1
  22. #define READ 3
  23. #define WRITE 2
  24.  
  25. uint8_t SpiRAMRead8(uint16_t address) {
  26.   uint8_t read_byte;
  27.  
  28.   PORTB &= ~(1<<PORTB2); //set SPI_SS low
  29.   SPI.transfer(READ);
  30.   SPI.transfer((char)(address >> 8));
  31.   SPI.transfer((char)address);
  32.   read_byte = SPI.transfer(0xFF);
  33.   PORTB |= (1<<PORTB2); //set SPI_SS high
  34.   return read_byte;
  35. }
  36.  
  37. void SpiRAMWrite8(uint16_t address, uint8_t data_byte) {
  38.   PORTB &= ~(1<<PORTB2); //set SPI_SS low
  39.   SPI.transfer(WRITE);
  40.   SPI.transfer((char)(address >> 8));
  41.   SPI.transfer((char)address);
  42.   SPI.transfer(data_byte);
  43.   PORTB |= (1<<PORTB2); //set SPI_SS high
  44. }
  45.  
  46.  
  47.  
  48. void writetestset(){
  49.  
  50. int8_t num = -128;
  51.  
  52. for(int i = 0; i < numsteps;i++){
  53.  // SpiRAMWrite8(i,(uint8_t)i);
  54.   SpiRAMWrite8(i,(int8_t)num);
  55.  
  56.   if(num+1 <128)
  57.   num++;
  58.   else
  59.   num = -127;
  60. }
  61.  
  62. }
  63.  
  64. void readtestset(){
  65.  
  66. uint8_t data;
  67.  
  68.  
  69. for(int i = 0; i < numsteps;i++){
  70.  data = SpiRAMRead8(i);
  71.  Serial.print("DATA : ");
  72.  
  73.  Serial.print((uint8_t)i);
  74.  Serial.print("     ");
  75.  Serial.print((int8_t)data);
  76.  Serial.println("");
  77. }
  78.  
  79. }
  80.  
  81. u8 framebuf[164];
  82. #define CSBIT 1<<7; // chip select bit
  83.  
  84. static void recvspiblock()
  85. {
  86.     u16 len = 164;
  87.     u8 *buf = framebuf;
  88.     PORTB &= ~CSBIT;
  89.     if( SPSR & 1 ) {
  90.         u8 t;
  91.         SPDR = 0xff;
  92.         while(--len) {
  93.             asm( " .rept 9\n nop\n .endr" );
  94.             t = SPDR;
  95.             SPDR = 0xff;
  96.             // there is actuallya race condition if an interrupt occurs here.
  97.             *buf++ = t; // double buffered
  98.         }
  99.         while (!(SPSR & 0x80));
  100.         SPSR &= 0x7f;
  101.         *buf = SPDR;
  102.     }
  103.     while (len--) {
  104.         SPDR = 0xff;
  105.         while (!(SPSR & 0x80));
  106.         SPSR &= 0x7f;
  107.         *buf++ = SPDR;
  108.         SpiRAMWrite8(164-len,(int8_t)*buf);
  109.     }
  110.     PORTB |= CSBIT;
  111.             asm( " .rept 9\n nop\n .endr" );
  112. }
  113.  
  114. static void spihwinit()
  115. {
  116.     DDRB |= CSBIT;                // set port for SPI
  117.     PORTB |= CSBIT;
  118.     SPCR = 0x53;                // Slowest possible for init
  119.     SPSR = 1;
  120.   SPI.setDataMode(SPI_MODE3);
  121.   SPI.setClockDivider(0);
  122.   SPI.begin();
  123.   delay(3000);
  124.   Serial.println(SPCR,HEX);
  125.   Serial.println(SPSR,HEX);
  126.   SPSR = 1;
  127.  
  128. }
  129. char img[17] = " .:;\"-!|+=&*$%@#";
  130. char image[31][41];
  131. void setup()
  132. {
  133.   Serial.begin(115200);
  134.   spihwinit();
  135.   memset(image, '^', sizeof(image));
  136. }
  137. void print_lepton_frame(void)
  138. {
  139.   char *c;
  140.   int i,j;
  141.   image[30][0] = 0;
  142.  recvspiblock();
  143.  return;
  144.   do {
  145.     do {
  146.     recvspiblock();
  147.     } while( framebuf[0] & 0xf == 0x0f );
  148.     j = framebuf[1];
  149.     if( j > 59 )
  150.       continue;
  151.     if( !(j & 1) )
  152.       continue;
  153.     j >>= 1;
  154.     c = image[j];
  155.     for( i = 5; i < 164; i += 4 ) {
  156.       j = framebuf[i+1]<<8;
  157.       j += framebuf[i];
  158.       j -= 7950;
  159.  
  160.       if( j < 0 ) j = 0;
  161.         j >>= 5;
  162.  
  163.       if( j > 15 ) j = 15;
  164.         *c++ = img[j];
  165.     }
  166.   *c = '\n';
  167.  
  168.   } while( framebuf[1] != 59 );
  169.  
  170.  
  171.  // c = image[0];
  172.   //Serial.println(c);
  173.   for(u8 i = 0; i < 164;i++){
  174. //Serial.print(i);
  175. //Serial.print(" ");
  176.   // Serial.println((u8)framebuf[i]);
  177. SpiRAMWrite8(i,(int8_t)framebuf[i]);
  178.  
  179.   }
  180. }
  181.  
  182. bool runonce = 0;
  183. void loop()
  184. {
  185.   //Serial.println("LEPTON FRAME");
  186.   if(runonce == 0){
  187.     print_lepton_frame();
  188. runonce = 1;
  189.  
  190.   for(u8 i = 0; i < 164;i++){
  191. int8_t data = SpiRAMRead8(i);
  192.   Serial.print("DATA: ");
  193.   Serial.print(i);
  194.   Serial.print(" ");
  195.   Serial.print(data);
  196.   Serial.println();
  197.   }
  198.  
  199.   }
  200.    
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement