Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SD.h>
- #include <LEDStrip.h>
- #include <LiquidCrystal.h>
- #define SIPIN 30
- #define DIPIN 31
- #define CLKPIN 32
- #define LATCHPIN 33
- LEDStrip mystrip(DIPIN,SIPIN,LATCHPIN,CLKPIN);
- #define SPULSES 5000
- #define STRIP_LENGTH 48
- #define BACKLIGHT 10
- byte BL = B10000000; // Black / Off
- #define SDssPin 53 //SD card CS pin
- int frameDelay = 10; // default for the frame delay
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Init the LCD
- //BacklightControl to save battery Life
- boolean BackLightTimer = false;
- int BackLightTimeout = 1000;
- int BackLightTemp = BackLightTimeout;
- //Stuff for the Keypad
- int adc_key_val[5] ={
- 30, 150, 360, 535, 760 };
- int NUM_KEYS = 5;
- int adc_key_in;
- int key=-1;
- int oldkey=-1;
- File root;
- File dataFile;
- String m_CurrentFilename = "";
- int m_FileIndex = 0;
- int m_NumberOfFiles = 0;
- String m_FileNames[200]; //yep this is bad, but unless you are going to have over 200 images on your lightwand..
- long buffer[STRIP_LENGTH];
- char tmpbuffer[20];
- long lCntr[1];
- 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;
- //Colour weights for the HL1606..
- uint8_t HL1606highred = 100;
- uint8_t HL1606highgreen = 100;
- uint8_t HL1606highblue = 100;
- uint8_t HL1606lowred = 20;
- uint8_t HL1606lowgreen = 20;
- uint8_t HL1606lowblue = 20;
- void setup()
- {
- Serial.begin(9600);
- setupLEDs();
- setupLCDdisplay();
- setupSDcard();
- }
- void setupLEDs()
- {
- ClearStrip(100);
- }
- void setupLCDdisplay()
- {
- lcd.begin(16, 2);
- lcd.print(" *** DLW ***");
- lcd.setCursor(0, 1);
- lcd.print("Initializing...");
- delay(1000);
- lcd.clear();
- pinMode(BACKLIGHT, OUTPUT);
- digitalWrite(BACKLIGHT,LOW);
- }
- void setupSDcard()
- {
- pinMode(SDssPin, OUTPUT);
- while (!SD.begin(SDssPin)) {
- lcd.print("SD init failed!");
- delay(1000);
- lcd.clear();
- delay(500);
- }
- lcd.print("SD init done.");
- delay(1000);
- root = SD.open("/");
- lcd.clear();
- lcd.print("Scanning files");
- GetFileNamesFromSD(root);
- isort(m_FileNames, m_NumberOfFiles);
- m_CurrentFilename = m_FileNames[0];
- DisplayCurrentFilename();
- }
- void latchanddelay(int dur)
- {
- mystrip.latch();
- delay(dur);
- }
- void ClearStrip(int duration)
- {
- int x;
- for(x=0;x<STRIP_LENGTH;x++)
- {
- mystrip.pushCmd(BL);
- }
- latchanddelay(duration);
- }
- void MultiPush(int Cmd, int Cnt) {
- while(Cnt-->0) mystrip.pushCmd(Cmd);
- }
- int ReadKeypad()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- digitalWrite(13, HIGH);
- key = get_key(adc_key_in); // convert into key press
- if (key != oldkey) // if keypress is detected
- {
- delay(50); // wait for debounce time
- adc_key_in = analogRead(0); // read the value from the sensor
- key = get_key(adc_key_in); // convert into key press
- if (key != oldkey)
- {
- oldkey = key;
- if (key >=0){
- return key;
- }
- }
- }
- return key;
- }
- // Convert ADC value to key number
- int get_key(unsigned int input)
- {
- int k;
- for (k = 0; k < NUM_KEYS; k++)
- {
- if (input < adc_key_val[k])
- {
- return k;
- }
- }
- if (k >= NUM_KEYS)
- k = -1; // No valid key pressed
- return k;
- }
- //The Main menu starts here...
- void loop()
- {
- int keypress = ReadKeypad();
- if ( keypress == 1) //up key (step up through the filenames)
- {
- BackLightOn();
- if (m_FileIndex > 0)
- {
- m_FileIndex--;
- }
- else
- {
- m_FileIndex = m_NumberOfFiles -1; //wrap round to the last file
- }
- DisplayCurrentFilename();
- delay(500);
- }
- if ( keypress == 2) //down key (step down through the filenames)
- {
- BackLightOn();
- if (m_FileIndex < m_NumberOfFiles -1)
- {
- m_FileIndex++;
- }
- else
- {
- m_FileIndex = 0;//wrap round to the 1st file again
- }
- DisplayCurrentFilename();
- delay(500);
- }
- if (keypress == 4)//select key (send out the selected file)
- {
- SendFile(m_CurrentFilename);
- }
- if(keypress == 0) //right key (frame delay +)
- {
- BackLightOn();
- if (frameDelay < 200)
- {
- frameDelay+=5;
- }
- ShowFrameDelay();
- }
- if(keypress == 3)//left key (frame delay -)
- {
- BackLightOn();
- if (frameDelay > 5)
- {
- frameDelay-=5;
- }
- ShowFrameDelay();
- }
- if (BackLightTimer == true) BackLightTime();
- }
- void BackLightOn()
- {
- digitalWrite(BACKLIGHT,HIGH);
- BackLightTimer = true;
- int BackLightTemp = BackLightTimeout;
- }
- void BackLightTime()
- {
- if (BackLightTemp <= 0)
- {
- BackLightTimer = false;
- BackLightTemp = BackLightTimeout;
- digitalWrite(BACKLIGHT,LOW);
- }
- else
- {
- BackLightTemp --;
- delay(1);
- }
- }
- void ShowFrameDelay()
- {
- lcd.clear();
- lcd.print("Frame delay:");
- lcd.setCursor(0,1);
- lcd.print(frameDelay);
- delay(500);
- DisplayCurrentFilename();
- }
- void SendFile(String Filename)
- {
- lcd.clear();
- lcd.print("Sending File");
- lcd.setCursor(0, 1);
- lcd.print(Filename);
- char temp[14];
- Filename.toCharArray(temp,14);
- dataFile = SD.open(temp);
- // if the file is available send it to the LED's
- if (dataFile)
- {
- ReadTheFile();
- dataFile.close();
- ClearStrip(100);
- }
- else
- {
- lcd.clear();
- lcd.print("Error reading file");
- delay(1000);
- setupSDcard();//try and re-init the SD card...
- return;
- }
- DisplayCurrentFilename();
- }
- void DisplayCurrentFilename()
- {
- m_CurrentFilename = m_FileNames[m_FileIndex];
- lcd.clear();
- lcd.print(m_CurrentFilename);
- }
- void GetFileNamesFromSD(File dir)
- {
- int fileCount = 0;
- String CurrentFilename = "";
- while(1)
- {
- File entry = dir.openNextFile();
- if (! entry) {
- // no more files
- m_NumberOfFiles = fileCount;
- break;
- }
- else
- {
- if (entry.isDirectory()) {
- //GetNextFileName(root);
- }
- else {
- CurrentFilename = entry.name();
- if (CurrentFilename.endsWith(".LSB"))//find files with our extension only
- {
- m_FileNames[fileCount] = entry.name();
- fileCount++;
- }
- }
- }
- }
- }
- uint32_t readLong()
- {
- uint32_t retValue;
- int incomingbyte;
- incomingbyte=readByte();
- retValue=(uint32_t)((byte)incomingbyte)<<24;
- incomingbyte=readByte();
- retValue+=(uint32_t)((byte)incomingbyte)<<16;
- incomingbyte=readByte();
- retValue+=(uint32_t)((byte)incomingbyte)<<8;
- incomingbyte=readByte();
- retValue+=(uint32_t)((byte)incomingbyte);
- return retValue;
- }
- int readInt()
- {
- int incomingbyte;
- uint16_t retValue;
- incomingbyte=readByte();
- retValue+=(int)((byte)incomingbyte)<<8;
- incomingbyte=readByte();
- retValue+=(int)((byte)incomingbyte);
- return retValue;
- }
- int readByte()
- {
- int retbyte=-1;
- while(retbyte<0) retbyte= dataFile.read();
- return retbyte;
- }
- void writeRGB(uint8_t Red,uint8_t Green,uint8_t Blue)
- {
- byte colorByte = 128;
- //
- //My strip seemed to have B + G swapped so I altered it here!
- //
- if (Blue > HL1606highblue) colorByte |= 16; //Bright Blue
- else if (Blue > HL1606lowblue) colorByte |= 32; //Dim Blue
- if (Red > HL1606highred) colorByte |= 4; //Bright Red
- else if (Red > HL1606lowred) colorByte |= 8; //Dim Red
- if (Green > HL1606highgreen) colorByte |= 1; //Bright Green
- else if (Green > HL1606lowgreen) colorByte |= 2; //Dim Green
- mystrip.pushCmd(colorByte);
- }
- void post_frame (void) {
- for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++)
- {
- long this_led_color = buffer[LED_number]; //24 bits of color data
- int r=(((this_led_color>>16) & 0xff)>>1);
- int g=(((this_led_color>>8) & 0xff)>>1);
- int b=(((this_led_color) & 0xff)>>1);
- writeRGB(r,g,b);
- }
- latchanddelay(frameDelay);
- }
- void ReadTheFile()
- {
- long WAND=readLong();
- fileVersion=readByte();
- bitmapOffset = readLong();
- bitmapHeight = readInt();
- bitmapWidth = readLong();
- StartDelay = readByte();
- EndDelay = readByte();
- StrobeOn = readInt();
- StrobeOff = readInt();
- VReversed = readByte();
- HReversed = readByte();
- Serial.println("file version ");
- Serial.println(fileVersion,DEC);
- Serial.println("bitmap offset");
- Serial.println(bitmapOffset,DEC);
- Serial.println("bitmap height");
- Serial.println(bitmapHeight,DEC);
- Serial.println("bitmap Width");
- Serial.println(bitmapWidth,DEC);
- Serial.println("start delay");
- Serial.println(StartDelay,DEC);
- Serial.println("end delay");
- Serial.println(EndDelay,DEC);
- Serial.println("strobe off");
- Serial.println(StrobeOff,DEC);
- Serial.println("strobe on");
- Serial.println(StrobeOn,DEC);
- Serial.println("vReversed");
- Serial.println(VReversed,DEC);
- Serial.println("hReversed");
- Serial.println(HReversed,DEC);
- if(readLong()!=0L) Serial.println("Invalid file");
- else Serial.println("Read in the header successfully");
- //ClearStrip(100);
- long filepos=0;
- for(int x=0;x<bitmapWidth;x++) {
- int bufpos=0;
- //Serial.println("Serial buffer is at ");
- //Serial.println(Serial1.available(),DEC);
- // Serial.println("At column" + x );
- while(bufpos<bitmapHeight) {
- mybitmapblock.len=readByte();
- mybitmapblock.r=readByte();
- mybitmapblock.g=readByte();
- mybitmapblock.b=readByte();
- 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>bitmapHeight)
- {
- Serial.println("FATAL ERROR - Problem with length of bitmap");
- while(1) delay(1);
- }
- }
- post_frame();
- }
- Serial.println("Finished");
- ClearStrip(100);
- }
- //sort the filenames in alphabetical order
- void isort(String *filenames, int n)
- {
- for (int i = 1; i < n; ++i)
- {
- String j = filenames[i];
- int k;
- for (k = i - 1; (k >= 0) && (j < filenames[k]); k--)
- {
- filenames[k + 1] = filenames[k];
- }
- filenames[k + 1] = j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement