Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 240824
- //
- //LEDType = LPD8806
- //HardwareSPI = True
- //LCDDisplay = True
- //DualStrip =True
- //StripControl1 = 22
- //StripControl2 = 23
- //bitmapWidth = 600
- //WandLEDs = 32
- //delayOffset = 0
- //numOffset = 0
- //sdiPin1 = 1
- //sdiPin2 = 1
- //clkPin1 = 10
- //clkPin2 = 10
- //StartDelay = 0
- //EndDelay = 0
- //StrobeOn = 10
- //StrobeOff = 0
- // Lightwand code generated by Phil Wright
- #include <avr/pgmspace.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <LiquidCrystal.h>
- #include <SdFat.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Init the LCD
- #define STRIP_LENGTH 32 // Total LEDs on this wand
- //#int STRIP_WIDTH 6620 // Width on this strip
- int SS = 27; // Move the SS pin out of the way of the LCD Display (if installed)
- int StripControl1 = 22; //Channel Select Wire for Dual Strip no. 1
- int StripControl2 = 23; //Channel Select Wire for Dual Strip no. 2
- long buffer[STRIP_LENGTH];
- char tmpbuffer[20];
- long lCntr[1];
- boolean firstloop=true;
- struct bitmapblock {
- byte len;
- byte r;
- byte g;
- byte b;
- }
- mybitmapblock;
- char buf[4];
- int fileVersion;
- uint32_t bitmapOffset;
- int bitmapHeight;
- uint32_t bitmapWidth;
- int StartDelay;
- int EndDelay;
- int StrobeOn;
- int StrobeOff;
- byte VReversed;
- byte HReversed;
- SdFat sd;
- SdFile myFile;
- uint32_t readLong()
- {
- uint32_t retValue;
- myFile.read(buf,4);
- retValue=(uint32_t)((byte)buf[0])<<24;
- retValue+=(uint32_t)((byte)buf[1])<<16;
- retValue+=(uint32_t)((byte)buf[2])<<8;
- retValue+=(uint32_t)((byte)buf[3]);
- return retValue;
- }
- int readInt()
- {
- uint16_t retValue;
- myFile.read(buf,2);
- retValue=(int)((byte)buf[0])<<8;
- retValue+=(int)((byte)buf[1]);
- return retValue;
- }
- int readByte()
- {
- byte retValue;
- myFile.read(buf,1);
- retValue=((byte)buf[0]);
- return retValue;
- }
- void writeRGB(uint8_t r,uint8_t g,uint8_t b,int nStrip)
- {
- if(nStrip==StripControl1) {
- digitalWrite(StripControl2, LOW);
- digitalWrite(StripControl1, HIGH);
- }
- else
- {
- digitalWrite(StripControl1, LOW);
- digitalWrite(StripControl2, HIGH);
- }
- SPI.transfer(g);
- SPI.transfer(r);
- SPI.transfer(b);
- digitalWrite(StripControl1, LOW);
- digitalWrite(StripControl2, LOW);
- }
- void blank_strip()
- {
- for(int x = 0 ; x < STRIP_LENGTH; x++)
- buffer[x] = 0;
- }
- void post_frame (void) {
- for(int LED_number = 0 ; LED_number < STRIP_LENGTH-1 ; LED_number=LED_number+2) {
- long this_led_color = buffer[LED_number]; //24 bits of color data
- int r=(((this_led_color>>16) & 0xff)>>1)|0x80;
- int g=(((this_led_color>>8) & 0xff)>>1)|0x80;
- int b=(((this_led_color) & 0xff)>>1)|0x80;
- delay(50);
- this_led_color = buffer[LED_number+1]; //24 bits of color data
- r=(((this_led_color>>16) & 0xff)>>1)|0x80;
- g=(((this_led_color>>8) & 0xff)>>1)|0x80;
- b=(((this_led_color) & 0xff)>>1)|0x80;
- delay(50);
- }
- for(int i=0;i<STRIP_LENGTH;i=i+2) {
- writeRGB(0,0,0,StripControl2);
- writeRGB(0,0,0,StripControl1);
- }
- }
- void setup() {
- pinMode(SS, OUTPUT);
- pinMode(StripControl1, OUTPUT);
- pinMode(StripControl2, OUTPUT);
- digitalWrite(StripControl2, LOW);
- digitalWrite(StripControl1, LOW);
- SPI.begin();
- blank_strip();
- lcd.print("Lightwand Gen.");
- delay(0);
- Serial.begin(9600);
- pinMode(StripControl1,OUTPUT);
- pinMode(StripControl2,OUTPUT);
- pinMode(A0,INPUT); // THIS IS THE BUTTON
- digitalWrite(StripControl1,LOW);
- digitalWrite(StripControl2,LOW);
- if (!sd.init(SPI_FULL_SPEED, 27)) lcd.print("SDcard failed");
- }
- void loop() {
- // open file in current working directory
- while(myFile.openNext(sd.cwd(), O_READ)) {
- myFile.read(buf,4);
- if(strncmp(&buf[0],"WAND",4)==0)
- {
- //if(!myFile.open("test.LSB",O_READ))
- //{
- // lcd.print("open failed");
- // while(1) {};
- //}
- myFile.getFilename(&tmpbuffer[0]);
- lcd.clear();
- lcd.print(&tmpbuffer[0]);
- //myFile.read(buf,4);
- // if(strncmp(&buf[0],"WAND",4)==0)
- // Serial.println("this is a bitmap file");
- // else
- // Serial.println("this is not a bitmap file");
- fileVersion=readByte();
- bitmapOffset = readLong();
- bitmapHeight = readInt();
- bitmapWidth = readLong();
- //cout << pstr("Height ") << bitmapHeight << endl;
- //cout << pstr("Width ") << bitmapWidth << endl;
- StartDelay = readByte();
- EndDelay = readByte();
- StrobeOn = readInt();
- StrobeOff = readInt();
- /*
- cout << pstr("StartDelay ") << StartDelay << endl;
- cout << pstr("EndDelay ") << EndDelay << endl;
- cout << pstr("StrobeOn ") << StrobeOn << endl;
- cout << pstr("StroneOff ") << StrobeOff << endl;
- */
- VReversed = readByte();
- HReversed = readByte();
- //cout << pstr("vReversed ") << VReversed << endl;
- //cout << pstr("hReversed ") << HReversed << endl;
- #
- blank_strip();
- post_frame();
- lcd.setCursor(0,0);
- //lcd.print(Filename);
- //lcd.print(" OK");
- //sprintf(&tmpbuffer[0],"%ld",(long)bitmapWidth);
- lcd.print((const String)&tmpbuffer[0]);
- lcd.setCursor(0,1);
- lcd.print("Waiting 4 Button");
- while(analogRead(A0)<900) {
- delay(10);
- }
- /*
- for(int a=StartDelay;a>0;a--) {
- lcd.setCursor(0,1);
- lcd.print("Start Delay ");
- lcd.print(a);
- lcd.print(" ");
- delay(StartDelay*1000);
- }
- */
- myFile.seekSet(bitmapOffset);
- if(readLong()!=0L) sd.errorHalt("Bitmap offset incorrect");
- long filepos=0;
- lcd.setCursor(0,1);
- for(int x=0;x<bitmapWidth;x++) {
- int bufpos=0;
- if(x % (bitmapWidth / 16)==0)
- {
- lcd.print("+");
- }
- while(bufpos<STRIP_LENGTH) {
- myFile.read(&mybitmapblock,4);
- if(mybitmapblock.len>bitmapHeight)
- {
- lcd.clear();
- lcd.print("Error Row ");
- lcd.print(x);
- lcd.setCursor(0,1);
- lcd.print("bufpos = ");
- lcd.print(bufpos);
- while(1) {
- };
- }
- filepos=filepos+4;
- for(int j=0;j<mybitmapblock.len;j++) {
- buffer[bufpos]=(long)mybitmapblock.r << 16;
- buffer[bufpos]|=(long)mybitmapblock.g << 8;
- buffer[bufpos++]|=(long)mybitmapblock.b;
- }
- if(bufpos>STRIP_LENGTH) {
- lcd.print("corrupt data");
- while(1) {
- };
- }
- }
- post_frame();
- delay(StrobeOn);
- if(StrobeOff>0)
- {
- blank_strip();
- post_frame();
- delay(StrobeOff);
- }
- }
- } //if bitmap
- myFile.close();
- } // while
- lcd.clear();
- lcd.print("End of bitmaps");
- blank_strip();
- post_frame();
- while(1) {
- };
- /*
- for(int a=5;a>0;a--) {
- lcd.setCursor(0,1);
- lcd.print("End Delay ");
- lcd.print(a);
- delay(EndDelay*1000);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement