Advertisement
Guest User

as

a guest
Jun 16th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 240824
  2. //
  3. //LEDType = LPD8806
  4. //HardwareSPI = True
  5. //LCDDisplay = True
  6. //DualStrip =True
  7. //StripControl1 = 22
  8. //StripControl2 = 23
  9. //bitmapWidth = 600
  10. //WandLEDs = 32
  11. //delayOffset = 0
  12. //numOffset = 0
  13. //sdiPin1 = 1
  14. //sdiPin2 = 1
  15. //clkPin1 = 10
  16. //clkPin2 = 10
  17. //StartDelay = 0
  18. //EndDelay = 0
  19. //StrobeOn = 10
  20. //StrobeOff = 0
  21. // Lightwand code generated by Phil Wright
  22. #include <avr/pgmspace.h>
  23. #include <SPI.h>
  24. #include <Wire.h>
  25. #include <LiquidCrystal.h>
  26. #include <SdFat.h>
  27. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  //Init the LCD
  28.  
  29. #define STRIP_LENGTH 32 // Total LEDs on this wand
  30. //#int STRIP_WIDTH 6620 // Width on this strip
  31. int SS = 27;  // Move the SS pin out of the way of the LCD Display (if installed)
  32. int StripControl1 = 22; //Channel Select Wire for Dual Strip no. 1
  33. int StripControl2 = 23; //Channel Select Wire for Dual Strip no. 2
  34.  
  35. long buffer[STRIP_LENGTH];
  36. char tmpbuffer[20];
  37. long lCntr[1];
  38. boolean firstloop=true;
  39.  
  40. struct bitmapblock {
  41.   byte len;
  42.   byte r;
  43.   byte g;
  44.   byte b;
  45. }
  46. mybitmapblock;
  47.  
  48. char buf[4];
  49. int fileVersion;
  50. uint32_t bitmapOffset;
  51. int bitmapHeight;
  52. uint32_t bitmapWidth;
  53. int StartDelay;
  54. int EndDelay;
  55. int StrobeOn;
  56. int StrobeOff;
  57. byte VReversed;
  58. byte HReversed;
  59.  
  60.  
  61. SdFat sd;
  62. SdFile myFile;
  63.  
  64.  
  65. uint32_t readLong()
  66. {
  67.   uint32_t retValue;
  68.   myFile.read(buf,4);
  69.  
  70.   retValue=(uint32_t)((byte)buf[0])<<24;
  71.   retValue+=(uint32_t)((byte)buf[1])<<16;
  72.   retValue+=(uint32_t)((byte)buf[2])<<8;
  73.   retValue+=(uint32_t)((byte)buf[3]);
  74.  
  75.   return retValue;
  76. }
  77.  
  78. int readInt()
  79. {
  80.   uint16_t retValue;
  81.   myFile.read(buf,2);
  82.  
  83.   retValue=(int)((byte)buf[0])<<8;
  84.   retValue+=(int)((byte)buf[1]);
  85.  
  86.   return retValue;
  87. }
  88.  
  89. int readByte()
  90. {
  91.   byte retValue;
  92.   myFile.read(buf,1);
  93.  
  94.  
  95.   retValue=((byte)buf[0]);
  96.   return retValue;
  97. }
  98.  
  99. void writeRGB(uint8_t r,uint8_t g,uint8_t b,int nStrip)
  100. {
  101.   if(nStrip==StripControl1) {
  102.     digitalWrite(StripControl2, LOW);
  103.     digitalWrite(StripControl1, HIGH);
  104.   }
  105.   else
  106.   {
  107.     digitalWrite(StripControl1, LOW);
  108.     digitalWrite(StripControl2, HIGH);
  109.   }
  110.   SPI.transfer(g);
  111.   SPI.transfer(r);
  112.   SPI.transfer(b);
  113.  
  114.   digitalWrite(StripControl1, LOW);
  115.   digitalWrite(StripControl2, LOW);
  116.  
  117. }
  118.  
  119. void blank_strip()
  120. {
  121.   for(int x = 0 ; x < STRIP_LENGTH; x++)
  122.     buffer[x] = 0;
  123. }
  124. void post_frame (void) {
  125.   for(int LED_number = 0 ; LED_number < STRIP_LENGTH-1 ; LED_number=LED_number+2) {
  126.     long this_led_color = buffer[LED_number]; //24 bits of color data
  127.     int r=(((this_led_color>>16) & 0xff)>>1)|0x80;
  128.     int g=(((this_led_color>>8) & 0xff)>>1)|0x80;
  129.     int b=(((this_led_color) & 0xff)>>1)|0x80;
  130.     delay(50);
  131.     this_led_color = buffer[LED_number+1]; //24 bits of color data
  132.     r=(((this_led_color>>16) & 0xff)>>1)|0x80;
  133.     g=(((this_led_color>>8) & 0xff)>>1)|0x80;
  134.     b=(((this_led_color) & 0xff)>>1)|0x80;
  135.     delay(50);
  136.   }
  137.   for(int i=0;i<STRIP_LENGTH;i=i+2) {
  138.     writeRGB(0,0,0,StripControl2);
  139.     writeRGB(0,0,0,StripControl1);
  140.   }
  141. }
  142. void setup() {
  143.   pinMode(SS, OUTPUT);
  144.   pinMode(StripControl1, OUTPUT);
  145.   pinMode(StripControl2, OUTPUT);
  146.   digitalWrite(StripControl2, LOW);
  147.   digitalWrite(StripControl1, LOW);
  148.  
  149.   SPI.begin();
  150.   blank_strip();
  151.  
  152.  
  153.  
  154.  
  155.   lcd.print("Lightwand Gen.");
  156.   delay(0);
  157.  
  158.   Serial.begin(9600);
  159.   pinMode(StripControl1,OUTPUT);
  160.   pinMode(StripControl2,OUTPUT);
  161.   pinMode(A0,INPUT);    // THIS IS THE BUTTON
  162.   digitalWrite(StripControl1,LOW);
  163.   digitalWrite(StripControl2,LOW);
  164.  
  165.   if (!sd.init(SPI_FULL_SPEED, 27)) lcd.print("SDcard failed");
  166.  
  167.  
  168. }
  169. void loop() {
  170.   // open file in current working directory
  171.  
  172.   while(myFile.openNext(sd.cwd(), O_READ)) {
  173.     myFile.read(buf,4);
  174.     if(strncmp(&buf[0],"WAND",4)==0)
  175.     {
  176.  
  177.       //if(!myFile.open("test.LSB",O_READ))
  178.       //{
  179.       //  lcd.print("open failed");
  180.       //  while(1) {};
  181.       //}
  182.  
  183.       myFile.getFilename(&tmpbuffer[0]);
  184.       lcd.clear();
  185.       lcd.print(&tmpbuffer[0]);
  186.  
  187.       //myFile.read(buf,4);
  188.       //  if(strncmp(&buf[0],"WAND",4)==0)
  189.       //    Serial.println("this is a bitmap file");
  190.       //  else
  191.       //    Serial.println("this is not a bitmap file");
  192.  
  193.       fileVersion=readByte();
  194.       bitmapOffset = readLong();
  195.       bitmapHeight = readInt();
  196.       bitmapWidth = readLong();
  197.       //cout << pstr("Height ") << bitmapHeight << endl;
  198.       //cout << pstr("Width ") << bitmapWidth << endl;
  199.  
  200.       StartDelay = readByte();
  201.       EndDelay = readByte();
  202.       StrobeOn = readInt();
  203.       StrobeOff = readInt();
  204.       /*
  205. cout << pstr("StartDelay ") << StartDelay << endl;
  206.        cout << pstr("EndDelay ") << EndDelay << endl;
  207.        cout << pstr("StrobeOn ") << StrobeOn << endl;
  208.        cout << pstr("StroneOff ") << StrobeOff << endl;
  209.        */
  210.       VReversed = readByte();
  211.       HReversed = readByte();
  212.  
  213.       //cout << pstr("vReversed ") << VReversed << endl;
  214.       //cout << pstr("hReversed ") << HReversed << endl;
  215. #
  216.       blank_strip();
  217.       post_frame();
  218.       lcd.setCursor(0,0);
  219.       //lcd.print(Filename);
  220.       //lcd.print(" OK");
  221.       //sprintf(&tmpbuffer[0],"%ld",(long)bitmapWidth);
  222.       lcd.print((const String)&tmpbuffer[0]);
  223.       lcd.setCursor(0,1);
  224.       lcd.print("Waiting 4 Button");
  225.  
  226.       while(analogRead(A0)<900) {
  227.         delay(10);
  228.       }
  229.  
  230.       /*
  231. for(int a=StartDelay;a>0;a--) {
  232.        lcd.setCursor(0,1);
  233.        lcd.print("Start Delay ");
  234.        lcd.print(a);
  235.        lcd.print("   ");
  236.        delay(StartDelay*1000);
  237.        }
  238.        */
  239.  
  240.       myFile.seekSet(bitmapOffset);
  241.       if(readLong()!=0L) sd.errorHalt("Bitmap offset incorrect");
  242.  
  243.       long filepos=0;
  244.       lcd.setCursor(0,1);
  245.       for(int x=0;x<bitmapWidth;x++) {
  246.         int bufpos=0;
  247.  
  248.         if(x % (bitmapWidth / 16)==0)
  249.         {
  250.           lcd.print("+");
  251.         }
  252.  
  253.         while(bufpos<STRIP_LENGTH) {
  254.  
  255.           myFile.read(&mybitmapblock,4);
  256.  
  257.           if(mybitmapblock.len>bitmapHeight)
  258.           {
  259.             lcd.clear();
  260.             lcd.print("Error Row ");
  261.             lcd.print(x);
  262.             lcd.setCursor(0,1);
  263.             lcd.print("bufpos = ");
  264.             lcd.print(bufpos);
  265.             while(1) {
  266.             };
  267.           }
  268.  
  269.           filepos=filepos+4;
  270.  
  271.           for(int j=0;j<mybitmapblock.len;j++) {
  272.             buffer[bufpos]=(long)mybitmapblock.r << 16;
  273.             buffer[bufpos]|=(long)mybitmapblock.g << 8;
  274.             buffer[bufpos++]|=(long)mybitmapblock.b;
  275.           }
  276.           if(bufpos>STRIP_LENGTH) {
  277.             lcd.print("corrupt data");
  278.             while(1) {
  279.             };
  280.           }
  281.         }
  282.         post_frame();
  283.         delay(StrobeOn);
  284.         if(StrobeOff>0)
  285.         {
  286.           blank_strip();
  287.           post_frame();
  288.           delay(StrobeOff);
  289.         }
  290.       }
  291.     } //if bitmap
  292.     myFile.close();
  293.  
  294.   } // while
  295.   lcd.clear();
  296.   lcd.print("End of bitmaps");
  297.  
  298.   blank_strip();
  299.   post_frame();
  300.  
  301.   while(1) {
  302.   };
  303.  
  304.   /*
  305. for(int a=5;a>0;a--) {
  306.    lcd.setCursor(0,1);
  307.    lcd.print("End Delay ");
  308.    lcd.print(a);
  309.    delay(EndDelay*1000);
  310.    }*/
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement